annotation.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _core_utils = require("./core_utils.js");
  30. var _default_appearance = require("./default_appearance.js");
  31. var _primitives = require("./primitives.js");
  32. var _bidi = require("./bidi.js");
  33. var _catalog = require("./catalog.js");
  34. var _colorspace = require("./colorspace.js");
  35. var _file_spec = require("./file_spec.js");
  36. var _object_loader = require("./object_loader.js");
  37. var _operator_list = require("./operator_list.js");
  38. var _stream = require("./stream.js");
  39. var _writer = require("./writer.js");
  40. class AnnotationFactory {
  41. static create(xref, ref, pdfManager, idFactory, collectFields) {
  42. return Promise.all([pdfManager.ensureCatalog("acroForm"), collectFields ? this._getPageIndex(xref, ref, pdfManager) : -1]).then(([acroForm, pageIndex]) => pdfManager.ensure(this, "_create", [xref, ref, pdfManager, idFactory, acroForm, collectFields, pageIndex]));
  43. }
  44. static _create(xref, ref, pdfManager, idFactory, acroForm, collectFields, pageIndex = -1) {
  45. const dict = xref.fetchIfRef(ref);
  46. if (!(0, _primitives.isDict)(dict)) {
  47. return undefined;
  48. }
  49. const id = (0, _primitives.isRef)(ref) ? ref.toString() : `annot_${idFactory.createObjId()}`;
  50. let subtype = dict.get("Subtype");
  51. subtype = (0, _primitives.isName)(subtype) ? subtype.name : null;
  52. const parameters = {
  53. xref,
  54. ref,
  55. dict,
  56. subtype,
  57. id,
  58. pdfManager,
  59. acroForm: acroForm instanceof _primitives.Dict ? acroForm : _primitives.Dict.empty,
  60. collectFields,
  61. pageIndex
  62. };
  63. switch (subtype) {
  64. case "Link":
  65. return new LinkAnnotation(parameters);
  66. case "Text":
  67. return new TextAnnotation(parameters);
  68. case "Widget":
  69. let fieldType = (0, _core_utils.getInheritableProperty)({
  70. dict,
  71. key: "FT"
  72. });
  73. fieldType = (0, _primitives.isName)(fieldType) ? fieldType.name : null;
  74. switch (fieldType) {
  75. case "Tx":
  76. return new TextWidgetAnnotation(parameters);
  77. case "Btn":
  78. return new ButtonWidgetAnnotation(parameters);
  79. case "Ch":
  80. return new ChoiceWidgetAnnotation(parameters);
  81. case "Sig":
  82. return new SignatureWidgetAnnotation(parameters);
  83. }
  84. (0, _util.warn)(`Unimplemented widget field type "${fieldType}", ` + "falling back to base field type.");
  85. return new WidgetAnnotation(parameters);
  86. case "Popup":
  87. return new PopupAnnotation(parameters);
  88. case "FreeText":
  89. return new FreeTextAnnotation(parameters);
  90. case "Line":
  91. return new LineAnnotation(parameters);
  92. case "Square":
  93. return new SquareAnnotation(parameters);
  94. case "Circle":
  95. return new CircleAnnotation(parameters);
  96. case "PolyLine":
  97. return new PolylineAnnotation(parameters);
  98. case "Polygon":
  99. return new PolygonAnnotation(parameters);
  100. case "Caret":
  101. return new CaretAnnotation(parameters);
  102. case "Ink":
  103. return new InkAnnotation(parameters);
  104. case "Highlight":
  105. return new HighlightAnnotation(parameters);
  106. case "Underline":
  107. return new UnderlineAnnotation(parameters);
  108. case "Squiggly":
  109. return new SquigglyAnnotation(parameters);
  110. case "StrikeOut":
  111. return new StrikeOutAnnotation(parameters);
  112. case "Stamp":
  113. return new StampAnnotation(parameters);
  114. case "FileAttachment":
  115. return new FileAttachmentAnnotation(parameters);
  116. default:
  117. if (!collectFields) {
  118. if (!subtype) {
  119. (0, _util.warn)("Annotation is missing the required /Subtype.");
  120. } else {
  121. (0, _util.warn)(`Unimplemented annotation type "${subtype}", ` + "falling back to base annotation.");
  122. }
  123. }
  124. return new Annotation(parameters);
  125. }
  126. }
  127. static async _getPageIndex(xref, ref, pdfManager) {
  128. try {
  129. const annotDict = await xref.fetchIfRefAsync(ref);
  130. if (!(0, _primitives.isDict)(annotDict)) {
  131. return -1;
  132. }
  133. const pageRef = annotDict.getRaw("P");
  134. if (!(0, _primitives.isRef)(pageRef)) {
  135. return -1;
  136. }
  137. const pageIndex = await pdfManager.ensureCatalog("getPageIndex", [pageRef]);
  138. return pageIndex;
  139. } catch (ex) {
  140. (0, _util.warn)(`_getPageIndex: "${ex}".`);
  141. return -1;
  142. }
  143. }
  144. }
  145. exports.AnnotationFactory = AnnotationFactory;
  146. function getRgbColor(color, defaultColor = new Uint8ClampedArray(3)) {
  147. if (!Array.isArray(color)) {
  148. return defaultColor;
  149. }
  150. const rgbColor = defaultColor || new Uint8ClampedArray(3);
  151. switch (color.length) {
  152. case 0:
  153. return null;
  154. case 1:
  155. _colorspace.ColorSpace.singletons.gray.getRgbItem(color, 0, rgbColor, 0);
  156. return rgbColor;
  157. case 3:
  158. _colorspace.ColorSpace.singletons.rgb.getRgbItem(color, 0, rgbColor, 0);
  159. return rgbColor;
  160. case 4:
  161. _colorspace.ColorSpace.singletons.cmyk.getRgbItem(color, 0, rgbColor, 0);
  162. return rgbColor;
  163. default:
  164. return defaultColor;
  165. }
  166. }
  167. function getQuadPoints(dict, rect) {
  168. if (!dict.has("QuadPoints")) {
  169. return null;
  170. }
  171. const quadPoints = dict.getArray("QuadPoints");
  172. if (!Array.isArray(quadPoints) || quadPoints.length === 0 || quadPoints.length % 8 > 0) {
  173. return null;
  174. }
  175. const quadPointsLists = [];
  176. for (let i = 0, ii = quadPoints.length / 8; i < ii; i++) {
  177. quadPointsLists.push([]);
  178. for (let j = i * 8, jj = i * 8 + 8; j < jj; j += 2) {
  179. const x = quadPoints[j];
  180. const y = quadPoints[j + 1];
  181. if (rect !== null && (x < rect[0] || x > rect[2] || y < rect[1] || y > rect[3])) {
  182. return null;
  183. }
  184. quadPointsLists[i].push({
  185. x,
  186. y
  187. });
  188. }
  189. }
  190. return quadPointsLists.map(quadPointsList => {
  191. const [minX, maxX, minY, maxY] = quadPointsList.reduce(([mX, MX, mY, MY], quadPoint) => [Math.min(mX, quadPoint.x), Math.max(MX, quadPoint.x), Math.min(mY, quadPoint.y), Math.max(MY, quadPoint.y)], [Number.MAX_VALUE, Number.MIN_VALUE, Number.MAX_VALUE, Number.MIN_VALUE]);
  192. return [{
  193. x: minX,
  194. y: maxY
  195. }, {
  196. x: maxX,
  197. y: maxY
  198. }, {
  199. x: minX,
  200. y: minY
  201. }, {
  202. x: maxX,
  203. y: minY
  204. }];
  205. });
  206. }
  207. function getTransformMatrix(rect, bbox, matrix) {
  208. const [minX, minY, maxX, maxY] = _util.Util.getAxialAlignedBoundingBox(bbox, matrix);
  209. if (minX === maxX || minY === maxY) {
  210. return [1, 0, 0, 1, rect[0], rect[1]];
  211. }
  212. const xRatio = (rect[2] - rect[0]) / (maxX - minX);
  213. const yRatio = (rect[3] - rect[1]) / (maxY - minY);
  214. return [xRatio, 0, 0, yRatio, rect[0] - minX * xRatio, rect[1] - minY * yRatio];
  215. }
  216. class Annotation {
  217. constructor(params) {
  218. const dict = params.dict;
  219. this.setTitle(dict.get("T"));
  220. this.setContents(dict.get("Contents"));
  221. this.setModificationDate(dict.get("M"));
  222. this.setFlags(dict.get("F"));
  223. this.setRectangle(dict.getArray("Rect"));
  224. this.setColor(dict.getArray("C"));
  225. this.setBorderStyle(dict);
  226. this.setAppearance(dict);
  227. this.setBorderAndBackgroundColors(dict.get("MK"));
  228. this._streams = [];
  229. if (this.appearance) {
  230. this._streams.push(this.appearance);
  231. }
  232. this.data = {
  233. annotationFlags: this.flags,
  234. borderStyle: this.borderStyle,
  235. color: this.color,
  236. backgroundColor: this.backgroundColor,
  237. borderColor: this.borderColor,
  238. contentsObj: this._contents,
  239. hasAppearance: !!this.appearance,
  240. id: params.id,
  241. modificationDate: this.modificationDate,
  242. rect: this.rectangle,
  243. subtype: params.subtype
  244. };
  245. if (params.collectFields) {
  246. const kids = dict.get("Kids");
  247. if (Array.isArray(kids)) {
  248. const kidIds = [];
  249. for (const kid of kids) {
  250. if ((0, _primitives.isRef)(kid)) {
  251. kidIds.push(kid.toString());
  252. }
  253. }
  254. if (kidIds.length !== 0) {
  255. this.data.kidIds = kidIds;
  256. }
  257. }
  258. this.data.actions = (0, _core_utils.collectActions)(params.xref, dict, _util.AnnotationActionEventType);
  259. this.data.fieldName = this._constructFieldName(dict);
  260. this.data.pageIndex = params.pageIndex;
  261. }
  262. this._fallbackFontDict = null;
  263. }
  264. _hasFlag(flags, flag) {
  265. return !!(flags & flag);
  266. }
  267. _isViewable(flags) {
  268. return !this._hasFlag(flags, _util.AnnotationFlag.INVISIBLE) && !this._hasFlag(flags, _util.AnnotationFlag.NOVIEW);
  269. }
  270. _isPrintable(flags) {
  271. return this._hasFlag(flags, _util.AnnotationFlag.PRINT) && !this._hasFlag(flags, _util.AnnotationFlag.INVISIBLE);
  272. }
  273. mustBeViewed(annotationStorage) {
  274. const storageEntry = annotationStorage && annotationStorage.get(this.data.id);
  275. if (storageEntry && storageEntry.hidden !== undefined) {
  276. return !storageEntry.hidden;
  277. }
  278. return this.viewable && !this._hasFlag(this.flags, _util.AnnotationFlag.HIDDEN);
  279. }
  280. mustBePrinted(annotationStorage) {
  281. const storageEntry = annotationStorage && annotationStorage.get(this.data.id);
  282. if (storageEntry && storageEntry.print !== undefined) {
  283. return storageEntry.print;
  284. }
  285. return this.printable;
  286. }
  287. get viewable() {
  288. if (this.data.quadPoints === null) {
  289. return false;
  290. }
  291. if (this.flags === 0) {
  292. return true;
  293. }
  294. return this._isViewable(this.flags);
  295. }
  296. get printable() {
  297. if (this.data.quadPoints === null) {
  298. return false;
  299. }
  300. if (this.flags === 0) {
  301. return false;
  302. }
  303. return this._isPrintable(this.flags);
  304. }
  305. _parseStringHelper(data) {
  306. const str = typeof data === "string" ? (0, _util.stringToPDFString)(data) : "";
  307. const dir = str && (0, _bidi.bidi)(str).dir === "rtl" ? "rtl" : "ltr";
  308. return {
  309. str,
  310. dir
  311. };
  312. }
  313. setTitle(title) {
  314. this._title = this._parseStringHelper(title);
  315. }
  316. setContents(contents) {
  317. this._contents = this._parseStringHelper(contents);
  318. }
  319. setModificationDate(modificationDate) {
  320. this.modificationDate = (0, _util.isString)(modificationDate) ? modificationDate : null;
  321. }
  322. setFlags(flags) {
  323. this.flags = Number.isInteger(flags) && flags > 0 ? flags : 0;
  324. }
  325. hasFlag(flag) {
  326. return this._hasFlag(this.flags, flag);
  327. }
  328. setRectangle(rectangle) {
  329. if (Array.isArray(rectangle) && rectangle.length === 4) {
  330. this.rectangle = _util.Util.normalizeRect(rectangle);
  331. } else {
  332. this.rectangle = [0, 0, 0, 0];
  333. }
  334. }
  335. setColor(color) {
  336. this.color = getRgbColor(color);
  337. }
  338. setBorderAndBackgroundColors(mk) {
  339. if (mk instanceof _primitives.Dict) {
  340. this.borderColor = getRgbColor(mk.getArray("BC"), null);
  341. this.backgroundColor = getRgbColor(mk.getArray("BG"), null);
  342. } else {
  343. this.borderColor = this.backgroundColor = null;
  344. }
  345. }
  346. setBorderStyle(borderStyle) {
  347. this.borderStyle = new AnnotationBorderStyle();
  348. if (!(0, _primitives.isDict)(borderStyle)) {
  349. return;
  350. }
  351. if (borderStyle.has("BS")) {
  352. const dict = borderStyle.get("BS");
  353. const dictType = dict.get("Type");
  354. if (!dictType || (0, _primitives.isName)(dictType, "Border")) {
  355. this.borderStyle.setWidth(dict.get("W"), this.rectangle);
  356. this.borderStyle.setStyle(dict.get("S"));
  357. this.borderStyle.setDashArray(dict.getArray("D"));
  358. }
  359. } else if (borderStyle.has("Border")) {
  360. const array = borderStyle.getArray("Border");
  361. if (Array.isArray(array) && array.length >= 3) {
  362. this.borderStyle.setHorizontalCornerRadius(array[0]);
  363. this.borderStyle.setVerticalCornerRadius(array[1]);
  364. this.borderStyle.setWidth(array[2], this.rectangle);
  365. if (array.length === 4) {
  366. this.borderStyle.setDashArray(array[3]);
  367. }
  368. }
  369. } else {
  370. this.borderStyle.setWidth(0);
  371. }
  372. }
  373. setAppearance(dict) {
  374. this.appearance = null;
  375. const appearanceStates = dict.get("AP");
  376. if (!(0, _primitives.isDict)(appearanceStates)) {
  377. return;
  378. }
  379. const normalAppearanceState = appearanceStates.get("N");
  380. if ((0, _primitives.isStream)(normalAppearanceState)) {
  381. this.appearance = normalAppearanceState;
  382. return;
  383. }
  384. if (!(0, _primitives.isDict)(normalAppearanceState)) {
  385. return;
  386. }
  387. const as = dict.get("AS");
  388. if (!(0, _primitives.isName)(as) || !normalAppearanceState.has(as.name)) {
  389. return;
  390. }
  391. this.appearance = normalAppearanceState.get(as.name);
  392. }
  393. loadResources(keys) {
  394. return this.appearance.dict.getAsync("Resources").then(resources => {
  395. if (!resources) {
  396. return undefined;
  397. }
  398. const objectLoader = new _object_loader.ObjectLoader(resources, keys, resources.xref);
  399. return objectLoader.load().then(function () {
  400. return resources;
  401. });
  402. });
  403. }
  404. getOperatorList(evaluator, task, renderForms, annotationStorage) {
  405. if (!this.appearance) {
  406. return Promise.resolve(new _operator_list.OperatorList());
  407. }
  408. const appearance = this.appearance;
  409. const data = this.data;
  410. const appearanceDict = appearance.dict;
  411. const resourcesPromise = this.loadResources(["ExtGState", "ColorSpace", "Pattern", "Shading", "XObject", "Font"]);
  412. const bbox = appearanceDict.getArray("BBox") || [0, 0, 1, 1];
  413. const matrix = appearanceDict.getArray("Matrix") || [1, 0, 0, 1, 0, 0];
  414. const transform = getTransformMatrix(data.rect, bbox, matrix);
  415. return resourcesPromise.then(resources => {
  416. const opList = new _operator_list.OperatorList();
  417. opList.addOp(_util.OPS.beginAnnotation, [data.id, data.rect, transform, matrix]);
  418. return evaluator.getOperatorList({
  419. stream: appearance,
  420. task,
  421. resources,
  422. operatorList: opList,
  423. fallbackFontDict: this._fallbackFontDict
  424. }).then(() => {
  425. opList.addOp(_util.OPS.endAnnotation, []);
  426. this.reset();
  427. return opList;
  428. });
  429. });
  430. }
  431. async save(evaluator, task, annotationStorage) {
  432. return null;
  433. }
  434. getFieldObject() {
  435. if (this.data.kidIds) {
  436. return {
  437. id: this.data.id,
  438. actions: this.data.actions,
  439. name: this.data.fieldName,
  440. strokeColor: this.data.borderColor,
  441. fillColor: this.data.backgroundColor,
  442. type: "",
  443. kidIds: this.data.kidIds,
  444. page: this.data.pageIndex
  445. };
  446. }
  447. return null;
  448. }
  449. reset() {
  450. for (const stream of this._streams) {
  451. stream.reset();
  452. }
  453. }
  454. _constructFieldName(dict) {
  455. if (!dict.has("T") && !dict.has("Parent")) {
  456. (0, _util.warn)("Unknown field name, falling back to empty field name.");
  457. return "";
  458. }
  459. if (!dict.has("Parent")) {
  460. return (0, _util.stringToPDFString)(dict.get("T"));
  461. }
  462. const fieldName = [];
  463. if (dict.has("T")) {
  464. fieldName.unshift((0, _util.stringToPDFString)(dict.get("T")));
  465. }
  466. let loopDict = dict;
  467. const visited = new _primitives.RefSet();
  468. if (dict.objId) {
  469. visited.put(dict.objId);
  470. }
  471. while (loopDict.has("Parent")) {
  472. loopDict = loopDict.get("Parent");
  473. if (!(loopDict instanceof _primitives.Dict) || loopDict.objId && visited.has(loopDict.objId)) {
  474. break;
  475. }
  476. if (loopDict.objId) {
  477. visited.put(loopDict.objId);
  478. }
  479. if (loopDict.has("T")) {
  480. fieldName.unshift((0, _util.stringToPDFString)(loopDict.get("T")));
  481. }
  482. }
  483. return fieldName.join(".");
  484. }
  485. }
  486. exports.Annotation = Annotation;
  487. class AnnotationBorderStyle {
  488. constructor() {
  489. this.width = 1;
  490. this.style = _util.AnnotationBorderStyleType.SOLID;
  491. this.dashArray = [3];
  492. this.horizontalCornerRadius = 0;
  493. this.verticalCornerRadius = 0;
  494. }
  495. setWidth(width, rect = [0, 0, 0, 0]) {
  496. if ((0, _primitives.isName)(width)) {
  497. this.width = 0;
  498. return;
  499. }
  500. if (Number.isInteger(width)) {
  501. if (width > 0) {
  502. const maxWidth = (rect[2] - rect[0]) / 2;
  503. const maxHeight = (rect[3] - rect[1]) / 2;
  504. if (maxWidth > 0 && maxHeight > 0 && (width > maxWidth || width > maxHeight)) {
  505. (0, _util.warn)(`AnnotationBorderStyle.setWidth - ignoring width: ${width}`);
  506. width = 1;
  507. }
  508. }
  509. this.width = width;
  510. }
  511. }
  512. setStyle(style) {
  513. if (!(0, _primitives.isName)(style)) {
  514. return;
  515. }
  516. switch (style.name) {
  517. case "S":
  518. this.style = _util.AnnotationBorderStyleType.SOLID;
  519. break;
  520. case "D":
  521. this.style = _util.AnnotationBorderStyleType.DASHED;
  522. break;
  523. case "B":
  524. this.style = _util.AnnotationBorderStyleType.BEVELED;
  525. break;
  526. case "I":
  527. this.style = _util.AnnotationBorderStyleType.INSET;
  528. break;
  529. case "U":
  530. this.style = _util.AnnotationBorderStyleType.UNDERLINE;
  531. break;
  532. default:
  533. break;
  534. }
  535. }
  536. setDashArray(dashArray) {
  537. if (Array.isArray(dashArray) && dashArray.length > 0) {
  538. let isValid = true;
  539. let allZeros = true;
  540. for (const element of dashArray) {
  541. const validNumber = +element >= 0;
  542. if (!validNumber) {
  543. isValid = false;
  544. break;
  545. } else if (element > 0) {
  546. allZeros = false;
  547. }
  548. }
  549. if (isValid && !allZeros) {
  550. this.dashArray = dashArray;
  551. } else {
  552. this.width = 0;
  553. }
  554. } else if (dashArray) {
  555. this.width = 0;
  556. }
  557. }
  558. setHorizontalCornerRadius(radius) {
  559. if (Number.isInteger(radius)) {
  560. this.horizontalCornerRadius = radius;
  561. }
  562. }
  563. setVerticalCornerRadius(radius) {
  564. if (Number.isInteger(radius)) {
  565. this.verticalCornerRadius = radius;
  566. }
  567. }
  568. }
  569. exports.AnnotationBorderStyle = AnnotationBorderStyle;
  570. class MarkupAnnotation extends Annotation {
  571. constructor(parameters) {
  572. super(parameters);
  573. const dict = parameters.dict;
  574. if (dict.has("IRT")) {
  575. const rawIRT = dict.getRaw("IRT");
  576. this.data.inReplyTo = (0, _primitives.isRef)(rawIRT) ? rawIRT.toString() : null;
  577. const rt = dict.get("RT");
  578. this.data.replyType = (0, _primitives.isName)(rt) ? rt.name : _util.AnnotationReplyType.REPLY;
  579. }
  580. if (this.data.replyType === _util.AnnotationReplyType.GROUP) {
  581. const parent = dict.get("IRT");
  582. this.setTitle(parent.get("T"));
  583. this.data.titleObj = this._title;
  584. this.setContents(parent.get("Contents"));
  585. this.data.contentsObj = this._contents;
  586. if (!parent.has("CreationDate")) {
  587. this.data.creationDate = null;
  588. } else {
  589. this.setCreationDate(parent.get("CreationDate"));
  590. this.data.creationDate = this.creationDate;
  591. }
  592. if (!parent.has("M")) {
  593. this.data.modificationDate = null;
  594. } else {
  595. this.setModificationDate(parent.get("M"));
  596. this.data.modificationDate = this.modificationDate;
  597. }
  598. this.data.hasPopup = parent.has("Popup");
  599. if (!parent.has("C")) {
  600. this.data.color = null;
  601. } else {
  602. this.setColor(parent.getArray("C"));
  603. this.data.color = this.color;
  604. }
  605. } else {
  606. this.data.titleObj = this._title;
  607. this.setCreationDate(dict.get("CreationDate"));
  608. this.data.creationDate = this.creationDate;
  609. this.data.hasPopup = dict.has("Popup");
  610. if (!dict.has("C")) {
  611. this.data.color = null;
  612. }
  613. }
  614. }
  615. setCreationDate(creationDate) {
  616. this.creationDate = (0, _util.isString)(creationDate) ? creationDate : null;
  617. }
  618. _setDefaultAppearance({
  619. xref,
  620. extra,
  621. strokeColor,
  622. fillColor,
  623. blendMode,
  624. strokeAlpha,
  625. fillAlpha,
  626. pointsCallback
  627. }) {
  628. let minX = Number.MAX_VALUE;
  629. let minY = Number.MAX_VALUE;
  630. let maxX = Number.MIN_VALUE;
  631. let maxY = Number.MIN_VALUE;
  632. const buffer = ["q"];
  633. if (extra) {
  634. buffer.push(extra);
  635. }
  636. if (strokeColor) {
  637. buffer.push(`${strokeColor[0]} ${strokeColor[1]} ${strokeColor[2]} RG`);
  638. }
  639. if (fillColor) {
  640. buffer.push(`${fillColor[0]} ${fillColor[1]} ${fillColor[2]} rg`);
  641. }
  642. let pointsArray = this.data.quadPoints;
  643. if (!pointsArray) {
  644. pointsArray = [[{
  645. x: this.rectangle[0],
  646. y: this.rectangle[3]
  647. }, {
  648. x: this.rectangle[2],
  649. y: this.rectangle[3]
  650. }, {
  651. x: this.rectangle[0],
  652. y: this.rectangle[1]
  653. }, {
  654. x: this.rectangle[2],
  655. y: this.rectangle[1]
  656. }]];
  657. }
  658. for (const points of pointsArray) {
  659. const [mX, MX, mY, MY] = pointsCallback(buffer, points);
  660. minX = Math.min(minX, mX);
  661. maxX = Math.max(maxX, MX);
  662. minY = Math.min(minY, mY);
  663. maxY = Math.max(maxY, MY);
  664. }
  665. buffer.push("Q");
  666. const formDict = new _primitives.Dict(xref);
  667. const appearanceStreamDict = new _primitives.Dict(xref);
  668. appearanceStreamDict.set("Subtype", _primitives.Name.get("Form"));
  669. const appearanceStream = new _stream.StringStream(buffer.join(" "));
  670. appearanceStream.dict = appearanceStreamDict;
  671. formDict.set("Fm0", appearanceStream);
  672. const gsDict = new _primitives.Dict(xref);
  673. if (blendMode) {
  674. gsDict.set("BM", _primitives.Name.get(blendMode));
  675. }
  676. if (typeof strokeAlpha === "number") {
  677. gsDict.set("CA", strokeAlpha);
  678. }
  679. if (typeof fillAlpha === "number") {
  680. gsDict.set("ca", fillAlpha);
  681. }
  682. const stateDict = new _primitives.Dict(xref);
  683. stateDict.set("GS0", gsDict);
  684. const resources = new _primitives.Dict(xref);
  685. resources.set("ExtGState", stateDict);
  686. resources.set("XObject", formDict);
  687. const appearanceDict = new _primitives.Dict(xref);
  688. appearanceDict.set("Resources", resources);
  689. const bbox = this.data.rect = [minX, minY, maxX, maxY];
  690. appearanceDict.set("BBox", bbox);
  691. this.appearance = new _stream.StringStream("/GS0 gs /Fm0 Do");
  692. this.appearance.dict = appearanceDict;
  693. this._streams.push(this.appearance, appearanceStream);
  694. }
  695. }
  696. exports.MarkupAnnotation = MarkupAnnotation;
  697. class WidgetAnnotation extends Annotation {
  698. constructor(params) {
  699. super(params);
  700. const dict = params.dict;
  701. const data = this.data;
  702. this.ref = params.ref;
  703. data.annotationType = _util.AnnotationType.WIDGET;
  704. if (data.fieldName === undefined) {
  705. data.fieldName = this._constructFieldName(dict);
  706. }
  707. if (data.actions === undefined) {
  708. data.actions = (0, _core_utils.collectActions)(params.xref, dict, _util.AnnotationActionEventType);
  709. }
  710. const fieldValue = (0, _core_utils.getInheritableProperty)({
  711. dict,
  712. key: "V",
  713. getArray: true
  714. });
  715. data.fieldValue = this._decodeFormValue(fieldValue);
  716. const defaultFieldValue = (0, _core_utils.getInheritableProperty)({
  717. dict,
  718. key: "DV",
  719. getArray: true
  720. });
  721. data.defaultFieldValue = this._decodeFormValue(defaultFieldValue);
  722. if (fieldValue === undefined && data.defaultFieldValue !== null) {
  723. data.fieldValue = data.defaultFieldValue;
  724. }
  725. data.alternativeText = (0, _util.stringToPDFString)(dict.get("TU") || "");
  726. const defaultAppearance = (0, _core_utils.getInheritableProperty)({
  727. dict,
  728. key: "DA"
  729. }) || params.acroForm.get("DA");
  730. this._defaultAppearance = (0, _util.isString)(defaultAppearance) ? defaultAppearance : "";
  731. data.defaultAppearanceData = (0, _default_appearance.parseDefaultAppearance)(this._defaultAppearance);
  732. const fieldType = (0, _core_utils.getInheritableProperty)({
  733. dict,
  734. key: "FT"
  735. });
  736. data.fieldType = (0, _primitives.isName)(fieldType) ? fieldType.name : null;
  737. const localResources = (0, _core_utils.getInheritableProperty)({
  738. dict,
  739. key: "DR"
  740. });
  741. const acroFormResources = params.acroForm.get("DR");
  742. const appearanceResources = this.appearance && this.appearance.dict.get("Resources");
  743. this._fieldResources = {
  744. localResources,
  745. acroFormResources,
  746. appearanceResources,
  747. mergedResources: _primitives.Dict.merge({
  748. xref: params.xref,
  749. dictArray: [localResources, appearanceResources, acroFormResources],
  750. mergeSubDicts: true
  751. })
  752. };
  753. data.fieldFlags = (0, _core_utils.getInheritableProperty)({
  754. dict,
  755. key: "Ff"
  756. });
  757. if (!Number.isInteger(data.fieldFlags) || data.fieldFlags < 0) {
  758. data.fieldFlags = 0;
  759. }
  760. data.readOnly = this.hasFieldFlag(_util.AnnotationFieldFlag.READONLY);
  761. data.hidden = this._hasFlag(data.annotationFlags, _util.AnnotationFlag.HIDDEN);
  762. }
  763. _decodeFormValue(formValue) {
  764. if (Array.isArray(formValue)) {
  765. return formValue.filter(item => (0, _util.isString)(item)).map(item => (0, _util.stringToPDFString)(item));
  766. } else if ((0, _primitives.isName)(formValue)) {
  767. return (0, _util.stringToPDFString)(formValue.name);
  768. } else if ((0, _util.isString)(formValue)) {
  769. return (0, _util.stringToPDFString)(formValue);
  770. }
  771. return null;
  772. }
  773. hasFieldFlag(flag) {
  774. return !!(this.data.fieldFlags & flag);
  775. }
  776. getOperatorList(evaluator, task, renderForms, annotationStorage) {
  777. if (renderForms && !(this instanceof SignatureWidgetAnnotation)) {
  778. return Promise.resolve(new _operator_list.OperatorList());
  779. }
  780. if (!this._hasText) {
  781. return super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  782. }
  783. return this._getAppearance(evaluator, task, annotationStorage).then(content => {
  784. if (this.appearance && content === null) {
  785. return super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  786. }
  787. const operatorList = new _operator_list.OperatorList();
  788. if (!this._defaultAppearance || content === null) {
  789. return operatorList;
  790. }
  791. const matrix = [1, 0, 0, 1, 0, 0];
  792. const bbox = [0, 0, this.data.rect[2] - this.data.rect[0], this.data.rect[3] - this.data.rect[1]];
  793. const transform = getTransformMatrix(this.data.rect, bbox, matrix);
  794. operatorList.addOp(_util.OPS.beginAnnotation, [this.data.id, this.data.rect, transform, matrix]);
  795. const stream = new _stream.StringStream(content);
  796. return evaluator.getOperatorList({
  797. stream,
  798. task,
  799. resources: this._fieldResources.mergedResources,
  800. operatorList
  801. }).then(function () {
  802. operatorList.addOp(_util.OPS.endAnnotation, []);
  803. return operatorList;
  804. });
  805. });
  806. }
  807. async save(evaluator, task, annotationStorage) {
  808. if (!annotationStorage) {
  809. return null;
  810. }
  811. const storageEntry = annotationStorage.get(this.data.id);
  812. const value = storageEntry && storageEntry.value;
  813. if (value === this.data.fieldValue || value === undefined) {
  814. return null;
  815. }
  816. let appearance = await this._getAppearance(evaluator, task, annotationStorage);
  817. if (appearance === null) {
  818. return null;
  819. }
  820. const {
  821. xref
  822. } = evaluator;
  823. const dict = xref.fetchIfRef(this.ref);
  824. if (!(0, _primitives.isDict)(dict)) {
  825. return null;
  826. }
  827. const bbox = [0, 0, this.data.rect[2] - this.data.rect[0], this.data.rect[3] - this.data.rect[1]];
  828. const xfa = {
  829. path: (0, _util.stringToPDFString)(dict.get("T") || ""),
  830. value
  831. };
  832. const newRef = xref.getNewRef();
  833. const AP = new _primitives.Dict(xref);
  834. AP.set("N", newRef);
  835. const encrypt = xref.encrypt;
  836. let originalTransform = null;
  837. let newTransform = null;
  838. if (encrypt) {
  839. originalTransform = encrypt.createCipherTransform(this.ref.num, this.ref.gen);
  840. newTransform = encrypt.createCipherTransform(newRef.num, newRef.gen);
  841. appearance = newTransform.encryptString(appearance);
  842. }
  843. dict.set("V", (0, _util.isAscii)(value) ? value : (0, _util.stringToUTF16BEString)(value));
  844. dict.set("AP", AP);
  845. dict.set("M", `D:${(0, _util.getModificationDate)()}`);
  846. const appearanceDict = new _primitives.Dict(xref);
  847. appearanceDict.set("Length", appearance.length);
  848. appearanceDict.set("Subtype", _primitives.Name.get("Form"));
  849. appearanceDict.set("Resources", this._getSaveFieldResources(xref));
  850. appearanceDict.set("BBox", bbox);
  851. const bufferOriginal = [`${this.ref.num} ${this.ref.gen} obj\n`];
  852. (0, _writer.writeDict)(dict, bufferOriginal, originalTransform);
  853. bufferOriginal.push("\nendobj\n");
  854. const bufferNew = [`${newRef.num} ${newRef.gen} obj\n`];
  855. (0, _writer.writeDict)(appearanceDict, bufferNew, newTransform);
  856. bufferNew.push(" stream\n", appearance, "\nendstream\nendobj\n");
  857. return [{
  858. ref: this.ref,
  859. data: bufferOriginal.join(""),
  860. xfa
  861. }, {
  862. ref: newRef,
  863. data: bufferNew.join(""),
  864. xfa: null
  865. }];
  866. }
  867. async _getAppearance(evaluator, task, annotationStorage) {
  868. const isPassword = this.hasFieldFlag(_util.AnnotationFieldFlag.PASSWORD);
  869. if (!annotationStorage || isPassword) {
  870. return null;
  871. }
  872. const storageEntry = annotationStorage.get(this.data.id);
  873. let value = storageEntry && storageEntry.value;
  874. if (value === undefined) {
  875. return null;
  876. }
  877. value = value.trim();
  878. if (value === "") {
  879. return "";
  880. }
  881. let lineCount = -1;
  882. if (this.data.multiLine) {
  883. lineCount = value.split(/\r\n|\r|\n/).length;
  884. }
  885. const defaultPadding = 2;
  886. const hPadding = defaultPadding;
  887. const totalHeight = this.data.rect[3] - this.data.rect[1];
  888. const totalWidth = this.data.rect[2] - this.data.rect[0];
  889. if (!this._defaultAppearance) {
  890. this.data.defaultAppearanceData = (0, _default_appearance.parseDefaultAppearance)(this._defaultAppearance = "/Helvetica 0 Tf 0 g");
  891. }
  892. const [defaultAppearance, fontSize] = this._computeFontSize(totalHeight, lineCount);
  893. const font = await this._getFontData(evaluator, task);
  894. let descent = font.descent;
  895. if (isNaN(descent)) {
  896. descent = 0;
  897. }
  898. const vPadding = defaultPadding + Math.abs(descent) * fontSize;
  899. const alignment = this.data.textAlignment;
  900. if (this.data.multiLine) {
  901. return this._getMultilineAppearance(defaultAppearance, value, font, fontSize, totalWidth, totalHeight, alignment, hPadding, vPadding);
  902. }
  903. const encodedString = font.encodeString(value).join("");
  904. if (this.data.comb) {
  905. return this._getCombAppearance(defaultAppearance, font, encodedString, totalWidth, hPadding, vPadding);
  906. }
  907. if (alignment === 0 || alignment > 2) {
  908. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 ${hPadding} ${vPadding} Tm (${(0, _util.escapeString)(encodedString)}) Tj` + " ET Q EMC";
  909. }
  910. const renderedText = this._renderText(encodedString, font, fontSize, totalWidth, alignment, hPadding, vPadding);
  911. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 0 0 Tm ${renderedText}` + " ET Q EMC";
  912. }
  913. async _getFontData(evaluator, task) {
  914. const operatorList = new _operator_list.OperatorList();
  915. const initialState = {
  916. font: null,
  917. clone() {
  918. return this;
  919. }
  920. };
  921. const {
  922. fontName,
  923. fontSize
  924. } = this.data.defaultAppearanceData;
  925. await evaluator.handleSetFont(this._fieldResources.mergedResources, [fontName && _primitives.Name.get(fontName), fontSize], null, operatorList, task, initialState, null);
  926. return initialState.font;
  927. }
  928. _computeFontSize(height, lineCount) {
  929. let {
  930. fontSize
  931. } = this.data.defaultAppearanceData;
  932. if (!fontSize) {
  933. const roundWithOneDigit = x => Math.round(x * 10) / 10;
  934. const FONT_FACTOR = 0.8;
  935. if (lineCount === -1) {
  936. fontSize = roundWithOneDigit(FONT_FACTOR * height);
  937. } else {
  938. fontSize = 10;
  939. let lineHeight = fontSize / FONT_FACTOR;
  940. let numberOfLines = Math.round(height / lineHeight);
  941. numberOfLines = Math.max(numberOfLines, lineCount);
  942. lineHeight = height / numberOfLines;
  943. fontSize = roundWithOneDigit(FONT_FACTOR * lineHeight);
  944. }
  945. const {
  946. fontName,
  947. fontColor
  948. } = this.data.defaultAppearanceData;
  949. this._defaultAppearance = (0, _default_appearance.createDefaultAppearance)({
  950. fontSize,
  951. fontName,
  952. fontColor
  953. });
  954. }
  955. return [this._defaultAppearance, fontSize];
  956. }
  957. _renderText(text, font, fontSize, totalWidth, alignment, hPadding, vPadding) {
  958. const glyphs = font.charsToGlyphs(text);
  959. const scale = fontSize / 1000;
  960. let width = 0;
  961. for (const glyph of glyphs) {
  962. width += glyph.width * scale;
  963. }
  964. let shift;
  965. if (alignment === 1) {
  966. shift = (totalWidth - width) / 2;
  967. } else if (alignment === 2) {
  968. shift = totalWidth - width - hPadding;
  969. } else {
  970. shift = hPadding;
  971. }
  972. shift = shift.toFixed(2);
  973. vPadding = vPadding.toFixed(2);
  974. return `${shift} ${vPadding} Td (${(0, _util.escapeString)(text)}) Tj`;
  975. }
  976. _getSaveFieldResources(xref) {
  977. const {
  978. localResources,
  979. appearanceResources,
  980. acroFormResources
  981. } = this._fieldResources;
  982. const fontName = this.data.defaultAppearanceData && this.data.defaultAppearanceData.fontName;
  983. if (!fontName) {
  984. return localResources || _primitives.Dict.empty;
  985. }
  986. for (const resources of [localResources, appearanceResources]) {
  987. if (resources instanceof _primitives.Dict) {
  988. const localFont = resources.get("Font");
  989. if (localFont instanceof _primitives.Dict && localFont.has(fontName)) {
  990. return resources;
  991. }
  992. }
  993. }
  994. if (acroFormResources instanceof _primitives.Dict) {
  995. const acroFormFont = acroFormResources.get("Font");
  996. if (acroFormFont instanceof _primitives.Dict && acroFormFont.has(fontName)) {
  997. const subFontDict = new _primitives.Dict(xref);
  998. subFontDict.set(fontName, acroFormFont.getRaw(fontName));
  999. const subResourcesDict = new _primitives.Dict(xref);
  1000. subResourcesDict.set("Font", subFontDict);
  1001. return _primitives.Dict.merge({
  1002. xref,
  1003. dictArray: [subResourcesDict, localResources],
  1004. mergeSubDicts: true
  1005. });
  1006. }
  1007. }
  1008. return localResources || _primitives.Dict.empty;
  1009. }
  1010. getFieldObject() {
  1011. return null;
  1012. }
  1013. }
  1014. class TextWidgetAnnotation extends WidgetAnnotation {
  1015. constructor(params) {
  1016. super(params);
  1017. this._hasText = true;
  1018. const dict = params.dict;
  1019. if (!(0, _util.isString)(this.data.fieldValue)) {
  1020. this.data.fieldValue = "";
  1021. }
  1022. let alignment = (0, _core_utils.getInheritableProperty)({
  1023. dict,
  1024. key: "Q"
  1025. });
  1026. if (!Number.isInteger(alignment) || alignment < 0 || alignment > 2) {
  1027. alignment = null;
  1028. }
  1029. this.data.textAlignment = alignment;
  1030. let maximumLength = (0, _core_utils.getInheritableProperty)({
  1031. dict,
  1032. key: "MaxLen"
  1033. });
  1034. if (!Number.isInteger(maximumLength) || maximumLength < 0) {
  1035. maximumLength = null;
  1036. }
  1037. this.data.maxLen = maximumLength;
  1038. this.data.multiLine = this.hasFieldFlag(_util.AnnotationFieldFlag.MULTILINE);
  1039. 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;
  1040. }
  1041. _getCombAppearance(defaultAppearance, font, text, width, hPadding, vPadding) {
  1042. const combWidth = (width / this.data.maxLen).toFixed(2);
  1043. const buf = [];
  1044. const positions = font.getCharPositions(text);
  1045. for (const [start, end] of positions) {
  1046. buf.push(`(${(0, _util.escapeString)(text.substring(start, end))}) Tj`);
  1047. }
  1048. const renderedComb = buf.join(` ${combWidth} 0 Td `);
  1049. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 ${hPadding} ${vPadding} Tm ${renderedComb}` + " ET Q EMC";
  1050. }
  1051. _getMultilineAppearance(defaultAppearance, text, font, fontSize, width, height, alignment, hPadding, vPadding) {
  1052. const lines = text.split(/\r\n|\r|\n/);
  1053. const buf = [];
  1054. const totalWidth = width - 2 * hPadding;
  1055. for (const line of lines) {
  1056. const chunks = this._splitLine(line, font, fontSize, totalWidth);
  1057. for (const chunk of chunks) {
  1058. const padding = buf.length === 0 ? hPadding : 0;
  1059. buf.push(this._renderText(chunk, font, fontSize, width, alignment, padding, -fontSize));
  1060. }
  1061. }
  1062. const renderedText = buf.join("\n");
  1063. return "/Tx BMC q BT " + defaultAppearance + ` 1 0 0 1 0 ${height} Tm ${renderedText}` + " ET Q EMC";
  1064. }
  1065. _splitLine(line, font, fontSize, width) {
  1066. line = font.encodeString(line).join("");
  1067. const glyphs = font.charsToGlyphs(line);
  1068. if (glyphs.length <= 1) {
  1069. return [line];
  1070. }
  1071. const positions = font.getCharPositions(line);
  1072. const scale = fontSize / 1000;
  1073. const chunks = [];
  1074. let lastSpacePosInStringStart = -1,
  1075. lastSpacePosInStringEnd = -1,
  1076. lastSpacePos = -1,
  1077. startChunk = 0,
  1078. currentWidth = 0;
  1079. for (let i = 0, ii = glyphs.length; i < ii; i++) {
  1080. const [start, end] = positions[i];
  1081. const glyph = glyphs[i];
  1082. const glyphWidth = glyph.width * scale;
  1083. if (glyph.unicode === " ") {
  1084. if (currentWidth + glyphWidth > width) {
  1085. chunks.push(line.substring(startChunk, start));
  1086. startChunk = start;
  1087. currentWidth = glyphWidth;
  1088. lastSpacePosInStringStart = -1;
  1089. lastSpacePos = -1;
  1090. } else {
  1091. currentWidth += glyphWidth;
  1092. lastSpacePosInStringStart = start;
  1093. lastSpacePosInStringEnd = end;
  1094. lastSpacePos = i;
  1095. }
  1096. } else {
  1097. if (currentWidth + glyphWidth > width) {
  1098. if (lastSpacePosInStringStart !== -1) {
  1099. chunks.push(line.substring(startChunk, lastSpacePosInStringEnd));
  1100. startChunk = lastSpacePosInStringEnd;
  1101. i = lastSpacePos + 1;
  1102. lastSpacePosInStringStart = -1;
  1103. currentWidth = 0;
  1104. } else {
  1105. chunks.push(line.substring(startChunk, start));
  1106. startChunk = start;
  1107. currentWidth = glyphWidth;
  1108. }
  1109. } else {
  1110. currentWidth += glyphWidth;
  1111. }
  1112. }
  1113. }
  1114. if (startChunk < line.length) {
  1115. chunks.push(line.substring(startChunk, line.length));
  1116. }
  1117. return chunks;
  1118. }
  1119. getFieldObject() {
  1120. return {
  1121. id: this.data.id,
  1122. value: this.data.fieldValue,
  1123. defaultValue: this.data.defaultFieldValue,
  1124. multiline: this.data.multiLine,
  1125. password: this.hasFieldFlag(_util.AnnotationFieldFlag.PASSWORD),
  1126. charLimit: this.data.maxLen,
  1127. comb: this.data.comb,
  1128. editable: !this.data.readOnly,
  1129. hidden: this.data.hidden,
  1130. name: this.data.fieldName,
  1131. rect: this.data.rect,
  1132. actions: this.data.actions,
  1133. page: this.data.pageIndex,
  1134. strokeColor: this.data.borderColor,
  1135. fillColor: this.data.backgroundColor,
  1136. type: "text"
  1137. };
  1138. }
  1139. }
  1140. class ButtonWidgetAnnotation extends WidgetAnnotation {
  1141. constructor(params) {
  1142. super(params);
  1143. this.checkedAppearance = null;
  1144. this.uncheckedAppearance = null;
  1145. this.data.checkBox = !this.hasFieldFlag(_util.AnnotationFieldFlag.RADIO) && !this.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  1146. this.data.radioButton = this.hasFieldFlag(_util.AnnotationFieldFlag.RADIO) && !this.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  1147. this.data.pushButton = this.hasFieldFlag(_util.AnnotationFieldFlag.PUSHBUTTON);
  1148. this.data.isTooltipOnly = false;
  1149. if (this.data.checkBox) {
  1150. this._processCheckBox(params);
  1151. } else if (this.data.radioButton) {
  1152. this._processRadioButton(params);
  1153. } else if (this.data.pushButton) {
  1154. this._processPushButton(params);
  1155. } else {
  1156. (0, _util.warn)("Invalid field flags for button widget annotation");
  1157. }
  1158. }
  1159. async getOperatorList(evaluator, task, renderForms, annotationStorage) {
  1160. if (this.data.pushButton) {
  1161. return super.getOperatorList(evaluator, task, false, annotationStorage);
  1162. }
  1163. let value = null;
  1164. if (annotationStorage) {
  1165. const storageEntry = annotationStorage.get(this.data.id);
  1166. value = storageEntry ? storageEntry.value : null;
  1167. }
  1168. if (value === null) {
  1169. if (this.appearance) {
  1170. return super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  1171. }
  1172. if (this.data.checkBox) {
  1173. value = this.data.fieldValue === this.data.exportValue;
  1174. } else {
  1175. value = this.data.fieldValue === this.data.buttonValue;
  1176. }
  1177. }
  1178. const appearance = value ? this.checkedAppearance : this.uncheckedAppearance;
  1179. if (appearance) {
  1180. const savedAppearance = this.appearance;
  1181. this.appearance = appearance;
  1182. const operatorList = super.getOperatorList(evaluator, task, renderForms, annotationStorage);
  1183. this.appearance = savedAppearance;
  1184. return operatorList;
  1185. }
  1186. return new _operator_list.OperatorList();
  1187. }
  1188. async save(evaluator, task, annotationStorage) {
  1189. if (this.data.checkBox) {
  1190. return this._saveCheckbox(evaluator, task, annotationStorage);
  1191. }
  1192. if (this.data.radioButton) {
  1193. return this._saveRadioButton(evaluator, task, annotationStorage);
  1194. }
  1195. return null;
  1196. }
  1197. async _saveCheckbox(evaluator, task, annotationStorage) {
  1198. if (!annotationStorage) {
  1199. return null;
  1200. }
  1201. const storageEntry = annotationStorage.get(this.data.id);
  1202. const value = storageEntry && storageEntry.value;
  1203. if (value === undefined) {
  1204. return null;
  1205. }
  1206. const defaultValue = this.data.fieldValue === this.data.exportValue;
  1207. if (defaultValue === value) {
  1208. return null;
  1209. }
  1210. const dict = evaluator.xref.fetchIfRef(this.ref);
  1211. if (!(0, _primitives.isDict)(dict)) {
  1212. return null;
  1213. }
  1214. const xfa = {
  1215. path: (0, _util.stringToPDFString)(dict.get("T") || ""),
  1216. value: value ? this.data.exportValue : ""
  1217. };
  1218. const name = _primitives.Name.get(value ? this.data.exportValue : "Off");
  1219. dict.set("V", name);
  1220. dict.set("AS", name);
  1221. dict.set("M", `D:${(0, _util.getModificationDate)()}`);
  1222. const encrypt = evaluator.xref.encrypt;
  1223. let originalTransform = null;
  1224. if (encrypt) {
  1225. originalTransform = encrypt.createCipherTransform(this.ref.num, this.ref.gen);
  1226. }
  1227. const buffer = [`${this.ref.num} ${this.ref.gen} obj\n`];
  1228. (0, _writer.writeDict)(dict, buffer, originalTransform);
  1229. buffer.push("\nendobj\n");
  1230. return [{
  1231. ref: this.ref,
  1232. data: buffer.join(""),
  1233. xfa
  1234. }];
  1235. }
  1236. async _saveRadioButton(evaluator, task, annotationStorage) {
  1237. if (!annotationStorage) {
  1238. return null;
  1239. }
  1240. const storageEntry = annotationStorage.get(this.data.id);
  1241. const value = storageEntry && storageEntry.value;
  1242. if (value === undefined) {
  1243. return null;
  1244. }
  1245. const defaultValue = this.data.fieldValue === this.data.buttonValue;
  1246. if (defaultValue === value) {
  1247. return null;
  1248. }
  1249. const dict = evaluator.xref.fetchIfRef(this.ref);
  1250. if (!(0, _primitives.isDict)(dict)) {
  1251. return null;
  1252. }
  1253. const xfa = {
  1254. path: (0, _util.stringToPDFString)(dict.get("T") || ""),
  1255. value: value ? this.data.buttonValue : ""
  1256. };
  1257. const name = _primitives.Name.get(value ? this.data.buttonValue : "Off");
  1258. let parentBuffer = null;
  1259. const encrypt = evaluator.xref.encrypt;
  1260. if (value) {
  1261. if ((0, _primitives.isRef)(this.parent)) {
  1262. const parent = evaluator.xref.fetch(this.parent);
  1263. let parentTransform = null;
  1264. if (encrypt) {
  1265. parentTransform = encrypt.createCipherTransform(this.parent.num, this.parent.gen);
  1266. }
  1267. parent.set("V", name);
  1268. parentBuffer = [`${this.parent.num} ${this.parent.gen} obj\n`];
  1269. (0, _writer.writeDict)(parent, parentBuffer, parentTransform);
  1270. parentBuffer.push("\nendobj\n");
  1271. } else if ((0, _primitives.isDict)(this.parent)) {
  1272. this.parent.set("V", name);
  1273. }
  1274. }
  1275. dict.set("AS", name);
  1276. dict.set("M", `D:${(0, _util.getModificationDate)()}`);
  1277. let originalTransform = null;
  1278. if (encrypt) {
  1279. originalTransform = encrypt.createCipherTransform(this.ref.num, this.ref.gen);
  1280. }
  1281. const buffer = [`${this.ref.num} ${this.ref.gen} obj\n`];
  1282. (0, _writer.writeDict)(dict, buffer, originalTransform);
  1283. buffer.push("\nendobj\n");
  1284. const newRefs = [{
  1285. ref: this.ref,
  1286. data: buffer.join(""),
  1287. xfa
  1288. }];
  1289. if (parentBuffer !== null) {
  1290. newRefs.push({
  1291. ref: this.parent,
  1292. data: parentBuffer.join(""),
  1293. xfa: null
  1294. });
  1295. }
  1296. return newRefs;
  1297. }
  1298. _getDefaultCheckedAppearance(params, type) {
  1299. const width = this.data.rect[2] - this.data.rect[0];
  1300. const height = this.data.rect[3] - this.data.rect[1];
  1301. const bbox = [0, 0, width, height];
  1302. const FONT_RATIO = 0.8;
  1303. const fontSize = Math.min(width, height) * FONT_RATIO;
  1304. let metrics, char;
  1305. if (type === "check") {
  1306. metrics = {
  1307. width: 0.755 * fontSize,
  1308. height: 0.705 * fontSize
  1309. };
  1310. char = "\x33";
  1311. } else if (type === "disc") {
  1312. metrics = {
  1313. width: 0.791 * fontSize,
  1314. height: 0.705 * fontSize
  1315. };
  1316. char = "\x6C";
  1317. } else {
  1318. (0, _util.unreachable)(`_getDefaultCheckedAppearance - unsupported type: ${type}`);
  1319. }
  1320. const xShift = (width - metrics.width) / 2;
  1321. const yShift = (height - metrics.height) / 2;
  1322. const appearance = `q BT /PdfJsZaDb ${fontSize} Tf 0 g ${xShift} ${yShift} Td (${char}) Tj ET Q`;
  1323. const appearanceStreamDict = new _primitives.Dict(params.xref);
  1324. appearanceStreamDict.set("FormType", 1);
  1325. appearanceStreamDict.set("Subtype", _primitives.Name.get("Form"));
  1326. appearanceStreamDict.set("Type", _primitives.Name.get("XObject"));
  1327. appearanceStreamDict.set("BBox", bbox);
  1328. appearanceStreamDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1329. appearanceStreamDict.set("Length", appearance.length);
  1330. const resources = new _primitives.Dict(params.xref);
  1331. const font = new _primitives.Dict(params.xref);
  1332. font.set("PdfJsZaDb", this.fallbackFontDict);
  1333. resources.set("Font", font);
  1334. appearanceStreamDict.set("Resources", resources);
  1335. this.checkedAppearance = new _stream.StringStream(appearance);
  1336. this.checkedAppearance.dict = appearanceStreamDict;
  1337. this._streams.push(this.checkedAppearance);
  1338. }
  1339. _processCheckBox(params) {
  1340. const customAppearance = params.dict.get("AP");
  1341. if (!(0, _primitives.isDict)(customAppearance)) {
  1342. return;
  1343. }
  1344. const normalAppearance = customAppearance.get("N");
  1345. if (!(0, _primitives.isDict)(normalAppearance)) {
  1346. return;
  1347. }
  1348. const asValue = this._decodeFormValue(params.dict.get("AS"));
  1349. if (typeof asValue === "string") {
  1350. this.data.fieldValue = asValue;
  1351. }
  1352. const yes = this.data.fieldValue !== null && this.data.fieldValue !== "Off" ? this.data.fieldValue : "Yes";
  1353. const exportValues = normalAppearance.getKeys();
  1354. if (exportValues.length === 0) {
  1355. exportValues.push("Off", yes);
  1356. } else if (exportValues.length === 1) {
  1357. if (exportValues[0] === "Off") {
  1358. exportValues.push(yes);
  1359. } else {
  1360. exportValues.unshift("Off");
  1361. }
  1362. } else if (exportValues.includes(yes)) {
  1363. exportValues.length = 0;
  1364. exportValues.push("Off", yes);
  1365. } else {
  1366. const otherYes = exportValues.find(v => v !== "Off");
  1367. exportValues.length = 0;
  1368. exportValues.push("Off", otherYes);
  1369. }
  1370. if (!exportValues.includes(this.data.fieldValue)) {
  1371. this.data.fieldValue = "Off";
  1372. }
  1373. this.data.exportValue = exportValues[1];
  1374. this.checkedAppearance = normalAppearance.get(this.data.exportValue) || null;
  1375. this.uncheckedAppearance = normalAppearance.get("Off") || null;
  1376. if (this.checkedAppearance) {
  1377. this._streams.push(this.checkedAppearance);
  1378. } else {
  1379. this._getDefaultCheckedAppearance(params, "check");
  1380. }
  1381. if (this.uncheckedAppearance) {
  1382. this._streams.push(this.uncheckedAppearance);
  1383. }
  1384. this._fallbackFontDict = this.fallbackFontDict;
  1385. }
  1386. _processRadioButton(params) {
  1387. this.data.fieldValue = this.data.buttonValue = null;
  1388. const fieldParent = params.dict.get("Parent");
  1389. if ((0, _primitives.isDict)(fieldParent)) {
  1390. this.parent = params.dict.getRaw("Parent");
  1391. const fieldParentValue = fieldParent.get("V");
  1392. if ((0, _primitives.isName)(fieldParentValue)) {
  1393. this.data.fieldValue = this._decodeFormValue(fieldParentValue);
  1394. }
  1395. }
  1396. const appearanceStates = params.dict.get("AP");
  1397. if (!(0, _primitives.isDict)(appearanceStates)) {
  1398. return;
  1399. }
  1400. const normalAppearance = appearanceStates.get("N");
  1401. if (!(0, _primitives.isDict)(normalAppearance)) {
  1402. return;
  1403. }
  1404. for (const key of normalAppearance.getKeys()) {
  1405. if (key !== "Off") {
  1406. this.data.buttonValue = this._decodeFormValue(key);
  1407. break;
  1408. }
  1409. }
  1410. this.checkedAppearance = normalAppearance.get(this.data.buttonValue) || null;
  1411. this.uncheckedAppearance = normalAppearance.get("Off") || null;
  1412. if (this.checkedAppearance) {
  1413. this._streams.push(this.checkedAppearance);
  1414. } else {
  1415. this._getDefaultCheckedAppearance(params, "disc");
  1416. }
  1417. if (this.uncheckedAppearance) {
  1418. this._streams.push(this.uncheckedAppearance);
  1419. }
  1420. this._fallbackFontDict = this.fallbackFontDict;
  1421. }
  1422. _processPushButton(params) {
  1423. if (!params.dict.has("A") && !params.dict.has("AA") && !this.data.alternativeText) {
  1424. (0, _util.warn)("Push buttons without action dictionaries are not supported");
  1425. return;
  1426. }
  1427. this.data.isTooltipOnly = !params.dict.has("A") && !params.dict.has("AA");
  1428. _catalog.Catalog.parseDestDictionary({
  1429. destDict: params.dict,
  1430. resultObj: this.data,
  1431. docBaseUrl: params.pdfManager.docBaseUrl
  1432. });
  1433. }
  1434. getFieldObject() {
  1435. let type = "button";
  1436. let exportValues;
  1437. if (this.data.checkBox) {
  1438. type = "checkbox";
  1439. exportValues = this.data.exportValue;
  1440. } else if (this.data.radioButton) {
  1441. type = "radiobutton";
  1442. exportValues = this.data.buttonValue;
  1443. }
  1444. return {
  1445. id: this.data.id,
  1446. value: this.data.fieldValue || "Off",
  1447. defaultValue: this.data.defaultFieldValue,
  1448. exportValues,
  1449. editable: !this.data.readOnly,
  1450. name: this.data.fieldName,
  1451. rect: this.data.rect,
  1452. hidden: this.data.hidden,
  1453. actions: this.data.actions,
  1454. page: this.data.pageIndex,
  1455. strokeColor: this.data.borderColor,
  1456. fillColor: this.data.backgroundColor,
  1457. type
  1458. };
  1459. }
  1460. get fallbackFontDict() {
  1461. const dict = new _primitives.Dict();
  1462. dict.set("BaseFont", _primitives.Name.get("ZapfDingbats"));
  1463. dict.set("Type", _primitives.Name.get("FallbackType"));
  1464. dict.set("Subtype", _primitives.Name.get("FallbackType"));
  1465. dict.set("Encoding", _primitives.Name.get("ZapfDingbatsEncoding"));
  1466. return (0, _util.shadow)(this, "fallbackFontDict", dict);
  1467. }
  1468. }
  1469. class ChoiceWidgetAnnotation extends WidgetAnnotation {
  1470. constructor(params) {
  1471. super(params);
  1472. this.data.options = [];
  1473. const options = (0, _core_utils.getInheritableProperty)({
  1474. dict: params.dict,
  1475. key: "Opt"
  1476. });
  1477. if (Array.isArray(options)) {
  1478. const xref = params.xref;
  1479. for (let i = 0, ii = options.length; i < ii; i++) {
  1480. const option = xref.fetchIfRef(options[i]);
  1481. const isOptionArray = Array.isArray(option);
  1482. this.data.options[i] = {
  1483. exportValue: this._decodeFormValue(isOptionArray ? xref.fetchIfRef(option[0]) : option),
  1484. displayValue: this._decodeFormValue(isOptionArray ? xref.fetchIfRef(option[1]) : option)
  1485. };
  1486. }
  1487. }
  1488. if ((0, _util.isString)(this.data.fieldValue)) {
  1489. this.data.fieldValue = [this.data.fieldValue];
  1490. } else if (!this.data.fieldValue) {
  1491. this.data.fieldValue = [];
  1492. }
  1493. this.data.combo = this.hasFieldFlag(_util.AnnotationFieldFlag.COMBO);
  1494. this.data.multiSelect = this.hasFieldFlag(_util.AnnotationFieldFlag.MULTISELECT);
  1495. this._hasText = true;
  1496. }
  1497. getFieldObject() {
  1498. const type = this.data.combo ? "combobox" : "listbox";
  1499. const value = this.data.fieldValue.length > 0 ? this.data.fieldValue[0] : null;
  1500. return {
  1501. id: this.data.id,
  1502. value,
  1503. defaultValue: this.data.defaultFieldValue,
  1504. editable: !this.data.readOnly,
  1505. name: this.data.fieldName,
  1506. rect: this.data.rect,
  1507. numItems: this.data.fieldValue.length,
  1508. multipleSelection: this.data.multiSelect,
  1509. hidden: this.data.hidden,
  1510. actions: this.data.actions,
  1511. items: this.data.options,
  1512. page: this.data.pageIndex,
  1513. strokeColor: this.data.borderColor,
  1514. fillColor: this.data.backgroundColor,
  1515. type
  1516. };
  1517. }
  1518. }
  1519. class SignatureWidgetAnnotation extends WidgetAnnotation {
  1520. constructor(params) {
  1521. super(params);
  1522. this.data.fieldValue = null;
  1523. }
  1524. getFieldObject() {
  1525. return {
  1526. id: this.data.id,
  1527. value: null,
  1528. page: this.data.pageIndex,
  1529. type: "signature"
  1530. };
  1531. }
  1532. }
  1533. class TextAnnotation extends MarkupAnnotation {
  1534. constructor(parameters) {
  1535. const DEFAULT_ICON_SIZE = 22;
  1536. super(parameters);
  1537. const dict = parameters.dict;
  1538. this.data.annotationType = _util.AnnotationType.TEXT;
  1539. if (this.data.hasAppearance) {
  1540. this.data.name = "NoIcon";
  1541. } else {
  1542. this.data.rect[1] = this.data.rect[3] - DEFAULT_ICON_SIZE;
  1543. this.data.rect[2] = this.data.rect[0] + DEFAULT_ICON_SIZE;
  1544. this.data.name = dict.has("Name") ? dict.get("Name").name : "Note";
  1545. }
  1546. if (dict.has("State")) {
  1547. this.data.state = dict.get("State") || null;
  1548. this.data.stateModel = dict.get("StateModel") || null;
  1549. } else {
  1550. this.data.state = null;
  1551. this.data.stateModel = null;
  1552. }
  1553. }
  1554. }
  1555. class LinkAnnotation extends Annotation {
  1556. constructor(params) {
  1557. super(params);
  1558. this.data.annotationType = _util.AnnotationType.LINK;
  1559. const quadPoints = getQuadPoints(params.dict, this.rectangle);
  1560. if (quadPoints) {
  1561. this.data.quadPoints = quadPoints;
  1562. }
  1563. _catalog.Catalog.parseDestDictionary({
  1564. destDict: params.dict,
  1565. resultObj: this.data,
  1566. docBaseUrl: params.pdfManager.docBaseUrl
  1567. });
  1568. }
  1569. }
  1570. class PopupAnnotation extends Annotation {
  1571. constructor(parameters) {
  1572. super(parameters);
  1573. this.data.annotationType = _util.AnnotationType.POPUP;
  1574. let parentItem = parameters.dict.get("Parent");
  1575. if (!parentItem) {
  1576. (0, _util.warn)("Popup annotation has a missing or invalid parent annotation.");
  1577. return;
  1578. }
  1579. const parentSubtype = parentItem.get("Subtype");
  1580. this.data.parentType = (0, _primitives.isName)(parentSubtype) ? parentSubtype.name : null;
  1581. const rawParent = parameters.dict.getRaw("Parent");
  1582. this.data.parentId = (0, _primitives.isRef)(rawParent) ? rawParent.toString() : null;
  1583. const parentRect = parentItem.getArray("Rect");
  1584. if (Array.isArray(parentRect) && parentRect.length === 4) {
  1585. this.data.parentRect = _util.Util.normalizeRect(parentRect);
  1586. } else {
  1587. this.data.parentRect = [0, 0, 0, 0];
  1588. }
  1589. const rt = parentItem.get("RT");
  1590. if ((0, _primitives.isName)(rt, _util.AnnotationReplyType.GROUP)) {
  1591. parentItem = parentItem.get("IRT");
  1592. }
  1593. if (!parentItem.has("M")) {
  1594. this.data.modificationDate = null;
  1595. } else {
  1596. this.setModificationDate(parentItem.get("M"));
  1597. this.data.modificationDate = this.modificationDate;
  1598. }
  1599. if (!parentItem.has("C")) {
  1600. this.data.color = null;
  1601. } else {
  1602. this.setColor(parentItem.getArray("C"));
  1603. this.data.color = this.color;
  1604. }
  1605. if (!this.viewable) {
  1606. const parentFlags = parentItem.get("F");
  1607. if (this._isViewable(parentFlags)) {
  1608. this.setFlags(parentFlags);
  1609. }
  1610. }
  1611. this.setTitle(parentItem.get("T"));
  1612. this.data.titleObj = this._title;
  1613. this.setContents(parentItem.get("Contents"));
  1614. this.data.contentsObj = this._contents;
  1615. }
  1616. }
  1617. class FreeTextAnnotation extends MarkupAnnotation {
  1618. constructor(parameters) {
  1619. super(parameters);
  1620. this.data.annotationType = _util.AnnotationType.FREETEXT;
  1621. }
  1622. }
  1623. class LineAnnotation extends MarkupAnnotation {
  1624. constructor(parameters) {
  1625. super(parameters);
  1626. this.data.annotationType = _util.AnnotationType.LINE;
  1627. const lineCoordinates = parameters.dict.getArray("L");
  1628. this.data.lineCoordinates = _util.Util.normalizeRect(lineCoordinates);
  1629. if (!this.appearance) {
  1630. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1631. const strokeAlpha = parameters.dict.get("CA");
  1632. let fillColor = null,
  1633. interiorColor = parameters.dict.getArray("IC");
  1634. if (interiorColor) {
  1635. interiorColor = getRgbColor(interiorColor, null);
  1636. fillColor = interiorColor ? Array.from(interiorColor).map(c => c / 255) : null;
  1637. }
  1638. const fillAlpha = fillColor ? strokeAlpha : null;
  1639. const borderWidth = this.borderStyle.width || 1,
  1640. borderAdjust = 2 * borderWidth;
  1641. const bbox = [this.data.lineCoordinates[0] - borderAdjust, this.data.lineCoordinates[1] - borderAdjust, this.data.lineCoordinates[2] + borderAdjust, this.data.lineCoordinates[3] + borderAdjust];
  1642. if (!_util.Util.intersect(this.rectangle, bbox)) {
  1643. this.rectangle = bbox;
  1644. }
  1645. this._setDefaultAppearance({
  1646. xref: parameters.xref,
  1647. extra: `${borderWidth} w`,
  1648. strokeColor,
  1649. fillColor,
  1650. strokeAlpha,
  1651. fillAlpha,
  1652. pointsCallback: (buffer, points) => {
  1653. buffer.push(`${lineCoordinates[0]} ${lineCoordinates[1]} m`, `${lineCoordinates[2]} ${lineCoordinates[3]} l`, "S");
  1654. return [points[0].x - borderWidth, points[1].x + borderWidth, points[3].y - borderWidth, points[1].y + borderWidth];
  1655. }
  1656. });
  1657. }
  1658. }
  1659. }
  1660. class SquareAnnotation extends MarkupAnnotation {
  1661. constructor(parameters) {
  1662. super(parameters);
  1663. this.data.annotationType = _util.AnnotationType.SQUARE;
  1664. if (!this.appearance) {
  1665. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1666. const strokeAlpha = parameters.dict.get("CA");
  1667. let fillColor = null,
  1668. interiorColor = parameters.dict.getArray("IC");
  1669. if (interiorColor) {
  1670. interiorColor = getRgbColor(interiorColor, null);
  1671. fillColor = interiorColor ? Array.from(interiorColor).map(c => c / 255) : null;
  1672. }
  1673. const fillAlpha = fillColor ? strokeAlpha : null;
  1674. this._setDefaultAppearance({
  1675. xref: parameters.xref,
  1676. extra: `${this.borderStyle.width} w`,
  1677. strokeColor,
  1678. fillColor,
  1679. strokeAlpha,
  1680. fillAlpha,
  1681. pointsCallback: (buffer, points) => {
  1682. const x = points[2].x + this.borderStyle.width / 2;
  1683. const y = points[2].y + this.borderStyle.width / 2;
  1684. const width = points[3].x - points[2].x - this.borderStyle.width;
  1685. const height = points[1].y - points[3].y - this.borderStyle.width;
  1686. buffer.push(`${x} ${y} ${width} ${height} re`);
  1687. if (fillColor) {
  1688. buffer.push("B");
  1689. } else {
  1690. buffer.push("S");
  1691. }
  1692. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1693. }
  1694. });
  1695. }
  1696. }
  1697. }
  1698. class CircleAnnotation extends MarkupAnnotation {
  1699. constructor(parameters) {
  1700. super(parameters);
  1701. this.data.annotationType = _util.AnnotationType.CIRCLE;
  1702. if (!this.appearance) {
  1703. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1704. const strokeAlpha = parameters.dict.get("CA");
  1705. let fillColor = null;
  1706. let interiorColor = parameters.dict.getArray("IC");
  1707. if (interiorColor) {
  1708. interiorColor = getRgbColor(interiorColor, null);
  1709. fillColor = interiorColor ? Array.from(interiorColor).map(c => c / 255) : null;
  1710. }
  1711. const fillAlpha = fillColor ? strokeAlpha : null;
  1712. const controlPointsDistance = 4 / 3 * Math.tan(Math.PI / (2 * 4));
  1713. this._setDefaultAppearance({
  1714. xref: parameters.xref,
  1715. extra: `${this.borderStyle.width} w`,
  1716. strokeColor,
  1717. fillColor,
  1718. strokeAlpha,
  1719. fillAlpha,
  1720. pointsCallback: (buffer, points) => {
  1721. const x0 = points[0].x + this.borderStyle.width / 2;
  1722. const y0 = points[0].y - this.borderStyle.width / 2;
  1723. const x1 = points[3].x - this.borderStyle.width / 2;
  1724. const y1 = points[3].y + this.borderStyle.width / 2;
  1725. const xMid = x0 + (x1 - x0) / 2;
  1726. const yMid = y0 + (y1 - y0) / 2;
  1727. const xOffset = (x1 - x0) / 2 * controlPointsDistance;
  1728. const yOffset = (y1 - y0) / 2 * controlPointsDistance;
  1729. buffer.push(`${xMid} ${y1} m`, `${xMid + xOffset} ${y1} ${x1} ${yMid + yOffset} ${x1} ${yMid} c`, `${x1} ${yMid - yOffset} ${xMid + xOffset} ${y0} ${xMid} ${y0} c`, `${xMid - xOffset} ${y0} ${x0} ${yMid - yOffset} ${x0} ${yMid} c`, `${x0} ${yMid + yOffset} ${xMid - xOffset} ${y1} ${xMid} ${y1} c`, "h");
  1730. if (fillColor) {
  1731. buffer.push("B");
  1732. } else {
  1733. buffer.push("S");
  1734. }
  1735. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1736. }
  1737. });
  1738. }
  1739. }
  1740. }
  1741. class PolylineAnnotation extends MarkupAnnotation {
  1742. constructor(parameters) {
  1743. super(parameters);
  1744. this.data.annotationType = _util.AnnotationType.POLYLINE;
  1745. this.data.vertices = [];
  1746. const rawVertices = parameters.dict.getArray("Vertices");
  1747. if (!Array.isArray(rawVertices)) {
  1748. return;
  1749. }
  1750. for (let i = 0, ii = rawVertices.length; i < ii; i += 2) {
  1751. this.data.vertices.push({
  1752. x: rawVertices[i],
  1753. y: rawVertices[i + 1]
  1754. });
  1755. }
  1756. if (!this.appearance) {
  1757. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1758. const strokeAlpha = parameters.dict.get("CA");
  1759. const borderWidth = this.borderStyle.width || 1,
  1760. borderAdjust = 2 * borderWidth;
  1761. const bbox = [Infinity, Infinity, -Infinity, -Infinity];
  1762. for (const vertex of this.data.vertices) {
  1763. bbox[0] = Math.min(bbox[0], vertex.x - borderAdjust);
  1764. bbox[1] = Math.min(bbox[1], vertex.y - borderAdjust);
  1765. bbox[2] = Math.max(bbox[2], vertex.x + borderAdjust);
  1766. bbox[3] = Math.max(bbox[3], vertex.y + borderAdjust);
  1767. }
  1768. if (!_util.Util.intersect(this.rectangle, bbox)) {
  1769. this.rectangle = bbox;
  1770. }
  1771. this._setDefaultAppearance({
  1772. xref: parameters.xref,
  1773. extra: `${borderWidth} w`,
  1774. strokeColor,
  1775. strokeAlpha,
  1776. pointsCallback: (buffer, points) => {
  1777. const vertices = this.data.vertices;
  1778. for (let i = 0, ii = vertices.length; i < ii; i++) {
  1779. buffer.push(`${vertices[i].x} ${vertices[i].y} ${i === 0 ? "m" : "l"}`);
  1780. }
  1781. buffer.push("S");
  1782. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1783. }
  1784. });
  1785. }
  1786. }
  1787. }
  1788. class PolygonAnnotation extends PolylineAnnotation {
  1789. constructor(parameters) {
  1790. super(parameters);
  1791. this.data.annotationType = _util.AnnotationType.POLYGON;
  1792. }
  1793. }
  1794. class CaretAnnotation extends MarkupAnnotation {
  1795. constructor(parameters) {
  1796. super(parameters);
  1797. this.data.annotationType = _util.AnnotationType.CARET;
  1798. }
  1799. }
  1800. class InkAnnotation extends MarkupAnnotation {
  1801. constructor(parameters) {
  1802. super(parameters);
  1803. this.data.annotationType = _util.AnnotationType.INK;
  1804. this.data.inkLists = [];
  1805. const rawInkLists = parameters.dict.getArray("InkList");
  1806. if (!Array.isArray(rawInkLists)) {
  1807. return;
  1808. }
  1809. const xref = parameters.xref;
  1810. for (let i = 0, ii = rawInkLists.length; i < ii; ++i) {
  1811. this.data.inkLists.push([]);
  1812. for (let j = 0, jj = rawInkLists[i].length; j < jj; j += 2) {
  1813. this.data.inkLists[i].push({
  1814. x: xref.fetchIfRef(rawInkLists[i][j]),
  1815. y: xref.fetchIfRef(rawInkLists[i][j + 1])
  1816. });
  1817. }
  1818. }
  1819. if (!this.appearance) {
  1820. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1821. const strokeAlpha = parameters.dict.get("CA");
  1822. const borderWidth = this.borderStyle.width || 1,
  1823. borderAdjust = 2 * borderWidth;
  1824. const bbox = [Infinity, Infinity, -Infinity, -Infinity];
  1825. for (const inkLists of this.data.inkLists) {
  1826. for (const vertex of inkLists) {
  1827. bbox[0] = Math.min(bbox[0], vertex.x - borderAdjust);
  1828. bbox[1] = Math.min(bbox[1], vertex.y - borderAdjust);
  1829. bbox[2] = Math.max(bbox[2], vertex.x + borderAdjust);
  1830. bbox[3] = Math.max(bbox[3], vertex.y + borderAdjust);
  1831. }
  1832. }
  1833. if (!_util.Util.intersect(this.rectangle, bbox)) {
  1834. this.rectangle = bbox;
  1835. }
  1836. this._setDefaultAppearance({
  1837. xref: parameters.xref,
  1838. extra: `${borderWidth} w`,
  1839. strokeColor,
  1840. strokeAlpha,
  1841. pointsCallback: (buffer, points) => {
  1842. for (const inkList of this.data.inkLists) {
  1843. for (let i = 0, ii = inkList.length; i < ii; i++) {
  1844. buffer.push(`${inkList[i].x} ${inkList[i].y} ${i === 0 ? "m" : "l"}`);
  1845. }
  1846. buffer.push("S");
  1847. }
  1848. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1849. }
  1850. });
  1851. }
  1852. }
  1853. }
  1854. class HighlightAnnotation extends MarkupAnnotation {
  1855. constructor(parameters) {
  1856. super(parameters);
  1857. this.data.annotationType = _util.AnnotationType.HIGHLIGHT;
  1858. const quadPoints = this.data.quadPoints = getQuadPoints(parameters.dict, null);
  1859. if (quadPoints) {
  1860. const resources = this.appearance && this.appearance.dict.get("Resources");
  1861. if (!this.appearance || !(resources && resources.has("ExtGState"))) {
  1862. if (this.appearance) {
  1863. (0, _util.warn)("HighlightAnnotation - ignoring built-in appearance stream.");
  1864. }
  1865. const fillColor = this.color ? Array.from(this.color).map(c => c / 255) : [1, 1, 0];
  1866. const fillAlpha = parameters.dict.get("CA");
  1867. this._setDefaultAppearance({
  1868. xref: parameters.xref,
  1869. fillColor,
  1870. blendMode: "Multiply",
  1871. fillAlpha,
  1872. pointsCallback: (buffer, points) => {
  1873. buffer.push(`${points[0].x} ${points[0].y} m`, `${points[1].x} ${points[1].y} l`, `${points[3].x} ${points[3].y} l`, `${points[2].x} ${points[2].y} l`, "f");
  1874. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1875. }
  1876. });
  1877. }
  1878. } else {
  1879. this.data.hasPopup = false;
  1880. }
  1881. }
  1882. }
  1883. class UnderlineAnnotation extends MarkupAnnotation {
  1884. constructor(parameters) {
  1885. super(parameters);
  1886. this.data.annotationType = _util.AnnotationType.UNDERLINE;
  1887. const quadPoints = this.data.quadPoints = getQuadPoints(parameters.dict, null);
  1888. if (quadPoints) {
  1889. if (!this.appearance) {
  1890. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1891. const strokeAlpha = parameters.dict.get("CA");
  1892. this._setDefaultAppearance({
  1893. xref: parameters.xref,
  1894. extra: "[] 0 d 1 w",
  1895. strokeColor,
  1896. strokeAlpha,
  1897. pointsCallback: (buffer, points) => {
  1898. buffer.push(`${points[2].x} ${points[2].y} m`, `${points[3].x} ${points[3].y} l`, "S");
  1899. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1900. }
  1901. });
  1902. }
  1903. } else {
  1904. this.data.hasPopup = false;
  1905. }
  1906. }
  1907. }
  1908. class SquigglyAnnotation extends MarkupAnnotation {
  1909. constructor(parameters) {
  1910. super(parameters);
  1911. this.data.annotationType = _util.AnnotationType.SQUIGGLY;
  1912. const quadPoints = this.data.quadPoints = getQuadPoints(parameters.dict, null);
  1913. if (quadPoints) {
  1914. if (!this.appearance) {
  1915. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1916. const strokeAlpha = parameters.dict.get("CA");
  1917. this._setDefaultAppearance({
  1918. xref: parameters.xref,
  1919. extra: "[] 0 d 1 w",
  1920. strokeColor,
  1921. strokeAlpha,
  1922. pointsCallback: (buffer, points) => {
  1923. const dy = (points[0].y - points[2].y) / 6;
  1924. let shift = dy;
  1925. let x = points[2].x;
  1926. const y = points[2].y;
  1927. const xEnd = points[3].x;
  1928. buffer.push(`${x} ${y + shift} m`);
  1929. do {
  1930. x += 2;
  1931. shift = shift === 0 ? dy : 0;
  1932. buffer.push(`${x} ${y + shift} l`);
  1933. } while (x < xEnd);
  1934. buffer.push("S");
  1935. return [points[2].x, xEnd, y - 2 * dy, y + 2 * dy];
  1936. }
  1937. });
  1938. }
  1939. } else {
  1940. this.data.hasPopup = false;
  1941. }
  1942. }
  1943. }
  1944. class StrikeOutAnnotation extends MarkupAnnotation {
  1945. constructor(parameters) {
  1946. super(parameters);
  1947. this.data.annotationType = _util.AnnotationType.STRIKEOUT;
  1948. const quadPoints = this.data.quadPoints = getQuadPoints(parameters.dict, null);
  1949. if (quadPoints) {
  1950. if (!this.appearance) {
  1951. const strokeColor = this.color ? Array.from(this.color).map(c => c / 255) : [0, 0, 0];
  1952. const strokeAlpha = parameters.dict.get("CA");
  1953. this._setDefaultAppearance({
  1954. xref: parameters.xref,
  1955. extra: "[] 0 d 1 w",
  1956. strokeColor,
  1957. strokeAlpha,
  1958. pointsCallback: (buffer, points) => {
  1959. buffer.push(`${(points[0].x + points[2].x) / 2} ` + `${(points[0].y + points[2].y) / 2} m`, `${(points[1].x + points[3].x) / 2} ` + `${(points[1].y + points[3].y) / 2} l`, "S");
  1960. return [points[0].x, points[1].x, points[3].y, points[1].y];
  1961. }
  1962. });
  1963. }
  1964. } else {
  1965. this.data.hasPopup = false;
  1966. }
  1967. }
  1968. }
  1969. class StampAnnotation extends MarkupAnnotation {
  1970. constructor(parameters) {
  1971. super(parameters);
  1972. this.data.annotationType = _util.AnnotationType.STAMP;
  1973. }
  1974. }
  1975. class FileAttachmentAnnotation extends MarkupAnnotation {
  1976. constructor(parameters) {
  1977. super(parameters);
  1978. const file = new _file_spec.FileSpec(parameters.dict.get("FS"), parameters.xref);
  1979. this.data.annotationType = _util.AnnotationType.FILEATTACHMENT;
  1980. this.data.file = file.serializable;
  1981. }
  1982. }