annotation.js 72 KB

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