2
0

annotation_spec.js 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _annotation = require("../../core/annotation.js");
  24. var _util = require("../../shared/util.js");
  25. var _test_utils = require("./test_utils.js");
  26. var _primitives = require("../../core/primitives.js");
  27. var _parser = require("../../core/parser.js");
  28. var _api = require("../../display/api.js");
  29. var _evaluator = require("../../core/evaluator.js");
  30. var _stream = require("../../core/stream.js");
  31. var _worker = require("../../core/worker.js");
  32. describe("annotation", function () {
  33. class PDFManagerMock {
  34. constructor(params) {
  35. this.docBaseUrl = params.docBaseUrl || null;
  36. this.pdfDocument = {
  37. catalog: {
  38. acroForm: new _primitives.Dict()
  39. }
  40. };
  41. }
  42. ensure(obj, prop, args) {
  43. return new Promise(function (resolve) {
  44. const value = obj[prop];
  45. if (typeof value === "function") {
  46. resolve(value.apply(obj, args));
  47. } else {
  48. resolve(value);
  49. }
  50. });
  51. }
  52. ensureCatalog(prop, args) {
  53. return this.ensure(this.pdfDocument.catalog, prop, args);
  54. }
  55. ensureDoc(prop, args) {
  56. return this.ensure(this.pdfDocument, prop, args);
  57. }
  58. }
  59. function HandlerMock() {
  60. this.inputs = [];
  61. }
  62. HandlerMock.prototype = {
  63. send(name, data) {
  64. this.inputs.push({
  65. name,
  66. data
  67. });
  68. }
  69. };
  70. let pdfManagerMock, idFactoryMock, partialEvaluator;
  71. beforeAll(async function (done) {
  72. pdfManagerMock = new PDFManagerMock({
  73. docBaseUrl: null
  74. });
  75. const CMapReaderFactory = new _api.DefaultCMapReaderFactory({
  76. baseUrl: _test_utils.CMAP_PARAMS.cMapUrl,
  77. isCompressed: _test_utils.CMAP_PARAMS.cMapPacked
  78. });
  79. const builtInCMapCache = new Map();
  80. builtInCMapCache.set("UniJIS-UTF16-H", await CMapReaderFactory.fetch({
  81. name: "UniJIS-UTF16-H"
  82. }));
  83. builtInCMapCache.set("Adobe-Japan1-UCS2", await CMapReaderFactory.fetch({
  84. name: "Adobe-Japan1-UCS2"
  85. }));
  86. idFactoryMock = (0, _test_utils.createIdFactory)(0);
  87. partialEvaluator = new _evaluator.PartialEvaluator({
  88. xref: new _test_utils.XRefMock(),
  89. handler: new HandlerMock(),
  90. pageIndex: 0,
  91. idFactory: (0, _test_utils.createIdFactory)(0),
  92. fontCache: new _primitives.RefSetCache(),
  93. builtInCMapCache
  94. });
  95. done();
  96. });
  97. afterAll(function () {
  98. pdfManagerMock = null;
  99. idFactoryMock = null;
  100. partialEvaluator = null;
  101. });
  102. describe("AnnotationFactory", function () {
  103. it("should get id for annotation", function (done) {
  104. const annotationDict = new _primitives.Dict();
  105. annotationDict.set("Type", _primitives.Name.get("Annot"));
  106. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  107. const annotationRef = _primitives.Ref.get(10, 0);
  108. const xref = new _test_utils.XRefMock([{
  109. ref: annotationRef,
  110. data: annotationDict
  111. }]);
  112. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  113. data
  114. }) => {
  115. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  116. expect(data.id).toEqual("10R");
  117. done();
  118. }, done.fail);
  119. });
  120. it("should handle, and get fallback IDs for, annotations that are not " + "indirect objects (issue 7569)", function (done) {
  121. const annotationDict = new _primitives.Dict();
  122. annotationDict.set("Type", _primitives.Name.get("Annot"));
  123. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  124. const xref = new _test_utils.XRefMock();
  125. const idFactory = (0, _test_utils.createIdFactory)(0);
  126. const annotation1 = _annotation.AnnotationFactory.create(xref, annotationDict, pdfManagerMock, idFactory).then(({
  127. data
  128. }) => {
  129. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  130. expect(data.id).toEqual("annot_p0_1");
  131. });
  132. const annotation2 = _annotation.AnnotationFactory.create(xref, annotationDict, pdfManagerMock, idFactory).then(({
  133. data
  134. }) => {
  135. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  136. expect(data.id).toEqual("annot_p0_2");
  137. });
  138. Promise.all([annotation1, annotation2]).then(done, done.fail);
  139. });
  140. it("should handle missing /Subtype", function (done) {
  141. const annotationDict = new _primitives.Dict();
  142. annotationDict.set("Type", _primitives.Name.get("Annot"));
  143. const annotationRef = _primitives.Ref.get(1, 0);
  144. const xref = new _test_utils.XRefMock([{
  145. ref: annotationRef,
  146. data: annotationDict
  147. }]);
  148. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  149. data
  150. }) => {
  151. expect(data.annotationType).toBeUndefined();
  152. done();
  153. }, done.fail);
  154. });
  155. });
  156. describe("getQuadPoints", function () {
  157. let dict, rect;
  158. beforeEach(function (done) {
  159. dict = new _primitives.Dict();
  160. rect = [];
  161. done();
  162. });
  163. afterEach(function () {
  164. dict = null;
  165. rect = null;
  166. });
  167. it("should ignore missing quadpoints", function () {
  168. expect((0, _annotation.getQuadPoints)(dict, rect)).toEqual(null);
  169. });
  170. it("should ignore non-array values", function () {
  171. dict.set("QuadPoints", "foo");
  172. expect((0, _annotation.getQuadPoints)(dict, rect)).toEqual(null);
  173. });
  174. it("should ignore arrays where the length is not a multiple of eight", function () {
  175. dict.set("QuadPoints", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  176. expect((0, _annotation.getQuadPoints)(dict, rect)).toEqual(null);
  177. });
  178. it("should ignore quadpoints if one coordinate lies outside the rectangle", function () {
  179. rect = [10, 10, 20, 20];
  180. const inputs = [[11, 11, 12, 12, 9, 13, 14, 14], [11, 11, 12, 12, 13, 9, 14, 14], [11, 11, 12, 12, 21, 13, 14, 14], [11, 11, 12, 12, 13, 21, 14, 14]];
  181. for (const input of inputs) {
  182. dict.set("QuadPoints", input);
  183. expect((0, _annotation.getQuadPoints)(dict, rect)).toEqual(null);
  184. }
  185. });
  186. it("should process quadpoints in the standard order", function () {
  187. rect = [10, 10, 20, 20];
  188. dict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10, 11, 19, 19, 19, 11, 11, 19, 11]);
  189. expect((0, _annotation.getQuadPoints)(dict, rect)).toEqual([[{
  190. x: 10,
  191. y: 20
  192. }, {
  193. x: 20,
  194. y: 20
  195. }, {
  196. x: 10,
  197. y: 10
  198. }, {
  199. x: 20,
  200. y: 10
  201. }], [{
  202. x: 11,
  203. y: 19
  204. }, {
  205. x: 19,
  206. y: 19
  207. }, {
  208. x: 11,
  209. y: 11
  210. }, {
  211. x: 19,
  212. y: 11
  213. }]]);
  214. });
  215. it("should normalize and process quadpoints in non-standard orders", function () {
  216. rect = [10, 10, 20, 20];
  217. const nonStandardOrders = [[10, 20, 20, 20, 20, 10, 10, 10], [10, 10, 20, 10, 10, 20, 20, 20], [10, 10, 20, 10, 20, 20, 10, 20]];
  218. for (const nonStandardOrder of nonStandardOrders) {
  219. dict.set("QuadPoints", nonStandardOrder);
  220. expect((0, _annotation.getQuadPoints)(dict, rect)).toEqual([[{
  221. x: 10,
  222. y: 20
  223. }, {
  224. x: 20,
  225. y: 20
  226. }, {
  227. x: 10,
  228. y: 10
  229. }, {
  230. x: 20,
  231. y: 10
  232. }]]);
  233. }
  234. });
  235. });
  236. describe("Annotation", function () {
  237. let dict, ref;
  238. beforeAll(function (done) {
  239. dict = new _primitives.Dict();
  240. ref = _primitives.Ref.get(1, 0);
  241. done();
  242. });
  243. afterAll(function () {
  244. dict = ref = null;
  245. });
  246. it("should set and get valid contents", function () {
  247. const annotation = new _annotation.Annotation({
  248. dict,
  249. ref
  250. });
  251. annotation.setContents("Foo bar baz");
  252. expect(annotation.contents).toEqual("Foo bar baz");
  253. });
  254. it("should not set and get invalid contents", function () {
  255. const annotation = new _annotation.Annotation({
  256. dict,
  257. ref
  258. });
  259. annotation.setContents(undefined);
  260. expect(annotation.contents).toEqual("");
  261. });
  262. it("should set and get a valid modification date", function () {
  263. const annotation = new _annotation.Annotation({
  264. dict,
  265. ref
  266. });
  267. annotation.setModificationDate("D:20190422");
  268. expect(annotation.modificationDate).toEqual("D:20190422");
  269. });
  270. it("should not set and get an invalid modification date", function () {
  271. const annotation = new _annotation.Annotation({
  272. dict,
  273. ref
  274. });
  275. annotation.setModificationDate(undefined);
  276. expect(annotation.modificationDate).toEqual(null);
  277. });
  278. it("should set and get flags", function () {
  279. const annotation = new _annotation.Annotation({
  280. dict,
  281. ref
  282. });
  283. annotation.setFlags(13);
  284. expect(annotation.hasFlag(_util.AnnotationFlag.INVISIBLE)).toEqual(true);
  285. expect(annotation.hasFlag(_util.AnnotationFlag.NOZOOM)).toEqual(true);
  286. expect(annotation.hasFlag(_util.AnnotationFlag.PRINT)).toEqual(true);
  287. expect(annotation.hasFlag(_util.AnnotationFlag.READONLY)).toEqual(false);
  288. expect(annotation.hasFlag(_util.AnnotationFlag.HIDDEN)).toEqual(false);
  289. });
  290. it("should be viewable and not printable by default", function () {
  291. const annotation = new _annotation.Annotation({
  292. dict,
  293. ref
  294. });
  295. expect(annotation.viewable).toEqual(true);
  296. expect(annotation.printable).toEqual(false);
  297. });
  298. it("should set and get a valid rectangle", function () {
  299. const annotation = new _annotation.Annotation({
  300. dict,
  301. ref
  302. });
  303. annotation.setRectangle([117, 694, 164.298, 720]);
  304. expect(annotation.rectangle).toEqual([117, 694, 164.298, 720]);
  305. });
  306. it("should not set and get an invalid rectangle", function () {
  307. const annotation = new _annotation.Annotation({
  308. dict,
  309. ref
  310. });
  311. annotation.setRectangle([117, 694, 164.298]);
  312. expect(annotation.rectangle).toEqual([0, 0, 0, 0]);
  313. });
  314. it("should reject a color if it is not an array", function () {
  315. const annotation = new _annotation.Annotation({
  316. dict,
  317. ref
  318. });
  319. annotation.setColor("red");
  320. expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 0]));
  321. });
  322. it("should set and get a transparent color", function () {
  323. const annotation = new _annotation.Annotation({
  324. dict,
  325. ref
  326. });
  327. annotation.setColor([]);
  328. expect(annotation.color).toEqual(null);
  329. });
  330. it("should set and get a grayscale color", function () {
  331. const annotation = new _annotation.Annotation({
  332. dict,
  333. ref
  334. });
  335. annotation.setColor([0.4]);
  336. expect(annotation.color).toEqual(new Uint8ClampedArray([102, 102, 102]));
  337. });
  338. it("should set and get an RGB color", function () {
  339. const annotation = new _annotation.Annotation({
  340. dict,
  341. ref
  342. });
  343. annotation.setColor([0, 0, 1]);
  344. expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  345. });
  346. it("should set and get a CMYK color", function () {
  347. const annotation = new _annotation.Annotation({
  348. dict,
  349. ref
  350. });
  351. annotation.setColor([0.1, 0.92, 0.84, 0.02]);
  352. expect(annotation.color).toEqual(new Uint8ClampedArray([234, 59, 48]));
  353. });
  354. it("should not set and get an invalid color", function () {
  355. const annotation = new _annotation.Annotation({
  356. dict,
  357. ref
  358. });
  359. annotation.setColor([0.4, 0.6]);
  360. expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 0]));
  361. });
  362. });
  363. describe("AnnotationBorderStyle", function () {
  364. it("should set and get a valid width", function () {
  365. const borderStyle = new _annotation.AnnotationBorderStyle();
  366. borderStyle.setWidth(3);
  367. expect(borderStyle.width).toEqual(3);
  368. });
  369. it("should not set and get an invalid width", function () {
  370. const borderStyle = new _annotation.AnnotationBorderStyle();
  371. borderStyle.setWidth("three");
  372. expect(borderStyle.width).toEqual(1);
  373. });
  374. it("should set the width to zero, when the input is a `Name` (issue 10385)", function () {
  375. const borderStyleZero = new _annotation.AnnotationBorderStyle();
  376. borderStyleZero.setWidth(_primitives.Name.get("0"));
  377. const borderStyleFive = new _annotation.AnnotationBorderStyle();
  378. borderStyleFive.setWidth(_primitives.Name.get("5"));
  379. expect(borderStyleZero.width).toEqual(0);
  380. expect(borderStyleFive.width).toEqual(0);
  381. });
  382. it("should set and get a valid style", function () {
  383. const borderStyle = new _annotation.AnnotationBorderStyle();
  384. borderStyle.setStyle(_primitives.Name.get("D"));
  385. expect(borderStyle.style).toEqual(_util.AnnotationBorderStyleType.DASHED);
  386. });
  387. it("should not set and get an invalid style", function () {
  388. const borderStyle = new _annotation.AnnotationBorderStyle();
  389. borderStyle.setStyle("Dashed");
  390. expect(borderStyle.style).toEqual(_util.AnnotationBorderStyleType.SOLID);
  391. });
  392. it("should set and get a valid dash array", function () {
  393. const borderStyle = new _annotation.AnnotationBorderStyle();
  394. borderStyle.setDashArray([1, 2, 3]);
  395. expect(borderStyle.dashArray).toEqual([1, 2, 3]);
  396. });
  397. it("should not set and get an invalid dash array", function () {
  398. const borderStyle = new _annotation.AnnotationBorderStyle();
  399. borderStyle.setDashArray([0, 0]);
  400. expect(borderStyle.dashArray).toEqual([3]);
  401. });
  402. it("should set and get a valid horizontal corner radius", function () {
  403. const borderStyle = new _annotation.AnnotationBorderStyle();
  404. borderStyle.setHorizontalCornerRadius(3);
  405. expect(borderStyle.horizontalCornerRadius).toEqual(3);
  406. });
  407. it("should not set and get an invalid horizontal corner radius", function () {
  408. const borderStyle = new _annotation.AnnotationBorderStyle();
  409. borderStyle.setHorizontalCornerRadius("three");
  410. expect(borderStyle.horizontalCornerRadius).toEqual(0);
  411. });
  412. it("should set and get a valid vertical corner radius", function () {
  413. const borderStyle = new _annotation.AnnotationBorderStyle();
  414. borderStyle.setVerticalCornerRadius(3);
  415. expect(borderStyle.verticalCornerRadius).toEqual(3);
  416. });
  417. it("should not set and get an invalid vertical corner radius", function () {
  418. const borderStyle = new _annotation.AnnotationBorderStyle();
  419. borderStyle.setVerticalCornerRadius("three");
  420. expect(borderStyle.verticalCornerRadius).toEqual(0);
  421. });
  422. });
  423. describe("MarkupAnnotation", function () {
  424. let dict, ref;
  425. beforeAll(function (done) {
  426. dict = new _primitives.Dict();
  427. ref = _primitives.Ref.get(1, 0);
  428. done();
  429. });
  430. afterAll(function () {
  431. dict = ref = null;
  432. });
  433. it("should set and get a valid creation date", function () {
  434. const markupAnnotation = new _annotation.MarkupAnnotation({
  435. dict,
  436. ref
  437. });
  438. markupAnnotation.setCreationDate("D:20190422");
  439. expect(markupAnnotation.creationDate).toEqual("D:20190422");
  440. });
  441. it("should not set and get an invalid creation date", function () {
  442. const markupAnnotation = new _annotation.MarkupAnnotation({
  443. dict,
  444. ref
  445. });
  446. markupAnnotation.setCreationDate(undefined);
  447. expect(markupAnnotation.creationDate).toEqual(null);
  448. });
  449. it("should not parse IRT/RT when not defined", function (done) {
  450. dict.set("Type", _primitives.Name.get("Annot"));
  451. dict.set("Subtype", _primitives.Name.get("Text"));
  452. const xref = new _test_utils.XRefMock([{
  453. ref,
  454. data: dict
  455. }]);
  456. _annotation.AnnotationFactory.create(xref, ref, pdfManagerMock, idFactoryMock).then(({
  457. data
  458. }) => {
  459. expect(data.inReplyTo).toBeUndefined();
  460. expect(data.replyType).toBeUndefined();
  461. done();
  462. }, done.fail);
  463. });
  464. it("should parse IRT and set default RT when not defined.", function (done) {
  465. const annotationRef = _primitives.Ref.get(819, 0);
  466. const annotationDict = new _primitives.Dict();
  467. annotationDict.set("Type", _primitives.Name.get("Annot"));
  468. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  469. const replyRef = _primitives.Ref.get(820, 0);
  470. const replyDict = new _primitives.Dict();
  471. replyDict.set("Type", _primitives.Name.get("Annot"));
  472. replyDict.set("Subtype", _primitives.Name.get("Text"));
  473. replyDict.set("IRT", annotationRef);
  474. const xref = new _test_utils.XRefMock([{
  475. ref: annotationRef,
  476. data: annotationDict
  477. }, {
  478. ref: replyRef,
  479. data: replyDict
  480. }]);
  481. annotationDict.assignXref(xref);
  482. replyDict.assignXref(xref);
  483. _annotation.AnnotationFactory.create(xref, replyRef, pdfManagerMock, idFactoryMock).then(({
  484. data
  485. }) => {
  486. expect(data.inReplyTo).toEqual(annotationRef.toString());
  487. expect(data.replyType).toEqual("R");
  488. done();
  489. }, done.fail);
  490. });
  491. it("should parse IRT/RT for a group type", function (done) {
  492. const annotationRef = _primitives.Ref.get(819, 0);
  493. const annotationDict = new _primitives.Dict();
  494. annotationDict.set("Type", _primitives.Name.get("Annot"));
  495. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  496. annotationDict.set("T", "ParentTitle");
  497. annotationDict.set("Contents", "ParentText");
  498. annotationDict.set("CreationDate", "D:20180423");
  499. annotationDict.set("M", "D:20190423");
  500. annotationDict.set("C", [0, 0, 1]);
  501. const popupRef = _primitives.Ref.get(820, 0);
  502. const popupDict = new _primitives.Dict();
  503. popupDict.set("Type", _primitives.Name.get("Annot"));
  504. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  505. popupDict.set("Parent", annotationRef);
  506. annotationDict.set("Popup", popupRef);
  507. const replyRef = _primitives.Ref.get(821, 0);
  508. const replyDict = new _primitives.Dict();
  509. replyDict.set("Type", _primitives.Name.get("Annot"));
  510. replyDict.set("Subtype", _primitives.Name.get("Text"));
  511. replyDict.set("IRT", annotationRef);
  512. replyDict.set("RT", _primitives.Name.get("Group"));
  513. replyDict.set("T", "ReplyTitle");
  514. replyDict.set("Contents", "ReplyText");
  515. replyDict.set("CreationDate", "D:20180523");
  516. replyDict.set("M", "D:20190523");
  517. replyDict.set("C", [0.4]);
  518. const xref = new _test_utils.XRefMock([{
  519. ref: annotationRef,
  520. data: annotationDict
  521. }, {
  522. ref: popupRef,
  523. data: popupDict
  524. }, {
  525. ref: replyRef,
  526. data: replyDict
  527. }]);
  528. annotationDict.assignXref(xref);
  529. popupDict.assignXref(xref);
  530. replyDict.assignXref(xref);
  531. _annotation.AnnotationFactory.create(xref, replyRef, pdfManagerMock, idFactoryMock).then(({
  532. data
  533. }) => {
  534. expect(data.inReplyTo).toEqual(annotationRef.toString());
  535. expect(data.replyType).toEqual("Group");
  536. expect(data.title).toEqual("ParentTitle");
  537. expect(data.contents).toEqual("ParentText");
  538. expect(data.creationDate).toEqual("D:20180423");
  539. expect(data.modificationDate).toEqual("D:20190423");
  540. expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  541. expect(data.hasPopup).toEqual(true);
  542. done();
  543. }, done.fail);
  544. });
  545. it("should parse IRT/RT for a reply type", function (done) {
  546. const annotationRef = _primitives.Ref.get(819, 0);
  547. const annotationDict = new _primitives.Dict();
  548. annotationDict.set("Type", _primitives.Name.get("Annot"));
  549. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  550. annotationDict.set("T", "ParentTitle");
  551. annotationDict.set("Contents", "ParentText");
  552. annotationDict.set("CreationDate", "D:20180423");
  553. annotationDict.set("M", "D:20190423");
  554. annotationDict.set("C", [0, 0, 1]);
  555. const popupRef = _primitives.Ref.get(820, 0);
  556. const popupDict = new _primitives.Dict();
  557. popupDict.set("Type", _primitives.Name.get("Annot"));
  558. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  559. popupDict.set("Parent", annotationRef);
  560. annotationDict.set("Popup", popupRef);
  561. const replyRef = _primitives.Ref.get(821, 0);
  562. const replyDict = new _primitives.Dict();
  563. replyDict.set("Type", _primitives.Name.get("Annot"));
  564. replyDict.set("Subtype", _primitives.Name.get("Text"));
  565. replyDict.set("IRT", annotationRef);
  566. replyDict.set("RT", _primitives.Name.get("R"));
  567. replyDict.set("T", "ReplyTitle");
  568. replyDict.set("Contents", "ReplyText");
  569. replyDict.set("CreationDate", "D:20180523");
  570. replyDict.set("M", "D:20190523");
  571. replyDict.set("C", [0.4]);
  572. const xref = new _test_utils.XRefMock([{
  573. ref: annotationRef,
  574. data: annotationDict
  575. }, {
  576. ref: popupRef,
  577. data: popupDict
  578. }, {
  579. ref: replyRef,
  580. data: replyDict
  581. }]);
  582. annotationDict.assignXref(xref);
  583. popupDict.assignXref(xref);
  584. replyDict.assignXref(xref);
  585. _annotation.AnnotationFactory.create(xref, replyRef, pdfManagerMock, idFactoryMock).then(({
  586. data
  587. }) => {
  588. expect(data.inReplyTo).toEqual(annotationRef.toString());
  589. expect(data.replyType).toEqual("R");
  590. expect(data.title).toEqual("ReplyTitle");
  591. expect(data.contents).toEqual("ReplyText");
  592. expect(data.creationDate).toEqual("D:20180523");
  593. expect(data.modificationDate).toEqual("D:20190523");
  594. expect(data.color).toEqual(new Uint8ClampedArray([102, 102, 102]));
  595. expect(data.hasPopup).toEqual(false);
  596. done();
  597. }, done.fail);
  598. });
  599. });
  600. describe("TextAnnotation", function () {
  601. it("should not parse state model and state when not defined", function (done) {
  602. const annotationRef = _primitives.Ref.get(819, 0);
  603. const annotationDict = new _primitives.Dict();
  604. annotationDict.set("Type", _primitives.Name.get("Annot"));
  605. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  606. annotationDict.set("Contents", "TestText");
  607. const replyRef = _primitives.Ref.get(820, 0);
  608. const replyDict = new _primitives.Dict();
  609. replyDict.set("Type", _primitives.Name.get("Annot"));
  610. replyDict.set("Subtype", _primitives.Name.get("Text"));
  611. replyDict.set("IRT", annotationRef);
  612. replyDict.set("RT", _primitives.Name.get("R"));
  613. replyDict.set("Contents", "ReplyText");
  614. const xref = new _test_utils.XRefMock([{
  615. ref: annotationRef,
  616. data: annotationDict
  617. }, {
  618. ref: replyRef,
  619. data: replyDict
  620. }]);
  621. annotationDict.assignXref(xref);
  622. replyDict.assignXref(xref);
  623. _annotation.AnnotationFactory.create(xref, replyRef, pdfManagerMock, idFactoryMock).then(({
  624. data
  625. }) => {
  626. expect(data.stateModel).toBeNull();
  627. expect(data.state).toBeNull();
  628. done();
  629. }, done.fail);
  630. });
  631. it("should correctly parse state model and state when defined", function (done) {
  632. const annotationRef = _primitives.Ref.get(819, 0);
  633. const annotationDict = new _primitives.Dict();
  634. annotationDict.set("Type", _primitives.Name.get("Annot"));
  635. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  636. const replyRef = _primitives.Ref.get(820, 0);
  637. const replyDict = new _primitives.Dict();
  638. replyDict.set("Type", _primitives.Name.get("Annot"));
  639. replyDict.set("Subtype", _primitives.Name.get("Text"));
  640. replyDict.set("IRT", annotationRef);
  641. replyDict.set("RT", _primitives.Name.get("R"));
  642. replyDict.set("StateModel", "Review");
  643. replyDict.set("State", "Rejected");
  644. const xref = new _test_utils.XRefMock([{
  645. ref: annotationRef,
  646. data: annotationDict
  647. }, {
  648. ref: replyRef,
  649. data: replyDict
  650. }]);
  651. annotationDict.assignXref(xref);
  652. replyDict.assignXref(xref);
  653. _annotation.AnnotationFactory.create(xref, replyRef, pdfManagerMock, idFactoryMock).then(({
  654. data
  655. }) => {
  656. expect(data.stateModel).toEqual("Review");
  657. expect(data.state).toEqual("Rejected");
  658. done();
  659. }, done.fail);
  660. });
  661. });
  662. describe("LinkAnnotation", function () {
  663. it("should correctly parse a URI action", function (done) {
  664. const actionDict = new _primitives.Dict();
  665. actionDict.set("Type", _primitives.Name.get("Action"));
  666. actionDict.set("S", _primitives.Name.get("URI"));
  667. actionDict.set("URI", "http://www.ctan.org/tex-archive/info/lshort");
  668. const annotationDict = new _primitives.Dict();
  669. annotationDict.set("Type", _primitives.Name.get("Annot"));
  670. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  671. annotationDict.set("A", actionDict);
  672. const annotationRef = _primitives.Ref.get(820, 0);
  673. const xref = new _test_utils.XRefMock([{
  674. ref: annotationRef,
  675. data: annotationDict
  676. }]);
  677. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  678. data
  679. }) => {
  680. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  681. expect(data.url).toEqual("http://www.ctan.org/tex-archive/info/lshort");
  682. expect(data.unsafeUrl).toEqual("http://www.ctan.org/tex-archive/info/lshort");
  683. expect(data.dest).toBeUndefined();
  684. done();
  685. }, done.fail);
  686. });
  687. it("should correctly parse a URI action, where the URI entry " + "is missing a protocol", function (done) {
  688. const actionDict = new _primitives.Dict();
  689. actionDict.set("Type", _primitives.Name.get("Action"));
  690. actionDict.set("S", _primitives.Name.get("URI"));
  691. actionDict.set("URI", "www.hmrc.gov.uk");
  692. const annotationDict = new _primitives.Dict();
  693. annotationDict.set("Type", _primitives.Name.get("Annot"));
  694. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  695. annotationDict.set("A", actionDict);
  696. const annotationRef = _primitives.Ref.get(353, 0);
  697. const xref = new _test_utils.XRefMock([{
  698. ref: annotationRef,
  699. data: annotationDict
  700. }]);
  701. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  702. data
  703. }) => {
  704. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  705. expect(data.url).toEqual("http://www.hmrc.gov.uk/");
  706. expect(data.unsafeUrl).toEqual("http://www.hmrc.gov.uk");
  707. expect(data.dest).toBeUndefined();
  708. done();
  709. }, done.fail);
  710. });
  711. it("should correctly parse a URI action, where the URI entry " + "has an incorrect encoding (bug 1122280)", function (done) {
  712. const actionStream = new _stream.StringStream("<<\n" + "/Type /Action\n" + "/S /URI\n" + "/URI (http://www.example.com/\\303\\274\\303\\266\\303\\244)\n" + ">>\n");
  713. const parser = new _parser.Parser({
  714. lexer: new _parser.Lexer(actionStream),
  715. xref: null
  716. });
  717. const actionDict = parser.getObj();
  718. const annotationDict = new _primitives.Dict();
  719. annotationDict.set("Type", _primitives.Name.get("Annot"));
  720. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  721. annotationDict.set("A", actionDict);
  722. const annotationRef = _primitives.Ref.get(8, 0);
  723. const xref = new _test_utils.XRefMock([{
  724. ref: annotationRef,
  725. data: annotationDict
  726. }]);
  727. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  728. data
  729. }) => {
  730. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  731. expect(data.url).toEqual(new URL((0, _util.stringToUTF8String)("http://www.example.com/\xC3\xBC\xC3\xB6\xC3\xA4")).href);
  732. expect(data.unsafeUrl).toEqual((0, _util.stringToUTF8String)("http://www.example.com/\xC3\xBC\xC3\xB6\xC3\xA4"));
  733. expect(data.dest).toBeUndefined();
  734. done();
  735. }, done.fail);
  736. });
  737. it("should correctly parse a GoTo action", function (done) {
  738. const actionDict = new _primitives.Dict();
  739. actionDict.set("Type", _primitives.Name.get("Action"));
  740. actionDict.set("S", _primitives.Name.get("GoTo"));
  741. actionDict.set("D", "page.157");
  742. const annotationDict = new _primitives.Dict();
  743. annotationDict.set("Type", _primitives.Name.get("Annot"));
  744. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  745. annotationDict.set("A", actionDict);
  746. const annotationRef = _primitives.Ref.get(798, 0);
  747. const xref = new _test_utils.XRefMock([{
  748. ref: annotationRef,
  749. data: annotationDict
  750. }]);
  751. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  752. data
  753. }) => {
  754. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  755. expect(data.url).toBeUndefined();
  756. expect(data.unsafeUrl).toBeUndefined();
  757. expect(data.dest).toEqual("page.157");
  758. done();
  759. }, done.fail);
  760. });
  761. it("should correctly parse a GoToR action, where the FileSpec entry " + "is a string containing a relative URL", function (done) {
  762. const actionDict = new _primitives.Dict();
  763. actionDict.set("Type", _primitives.Name.get("Action"));
  764. actionDict.set("S", _primitives.Name.get("GoToR"));
  765. actionDict.set("F", "../../0013/001346/134685E.pdf");
  766. actionDict.set("D", "4.3");
  767. actionDict.set("NewWindow", true);
  768. const annotationDict = new _primitives.Dict();
  769. annotationDict.set("Type", _primitives.Name.get("Annot"));
  770. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  771. annotationDict.set("A", actionDict);
  772. const annotationRef = _primitives.Ref.get(489, 0);
  773. const xref = new _test_utils.XRefMock([{
  774. ref: annotationRef,
  775. data: annotationDict
  776. }]);
  777. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  778. data
  779. }) => {
  780. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  781. expect(data.url).toBeUndefined();
  782. expect(data.unsafeUrl).toEqual("../../0013/001346/134685E.pdf#4.3");
  783. expect(data.dest).toBeUndefined();
  784. expect(data.newWindow).toEqual(true);
  785. done();
  786. }, done.fail);
  787. });
  788. it("should correctly parse a GoToR action, containing a relative URL, " + 'with the "docBaseUrl" parameter specified', function (done) {
  789. const actionDict = new _primitives.Dict();
  790. actionDict.set("Type", _primitives.Name.get("Action"));
  791. actionDict.set("S", _primitives.Name.get("GoToR"));
  792. actionDict.set("F", "../../0013/001346/134685E.pdf");
  793. actionDict.set("D", "4.3");
  794. const annotationDict = new _primitives.Dict();
  795. annotationDict.set("Type", _primitives.Name.get("Annot"));
  796. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  797. annotationDict.set("A", actionDict);
  798. const annotationRef = _primitives.Ref.get(489, 0);
  799. const xref = new _test_utils.XRefMock([{
  800. ref: annotationRef,
  801. data: annotationDict
  802. }]);
  803. const pdfManager = new PDFManagerMock({
  804. docBaseUrl: "http://www.example.com/test/pdfs/qwerty.pdf"
  805. });
  806. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManager, idFactoryMock).then(({
  807. data
  808. }) => {
  809. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  810. expect(data.url).toEqual("http://www.example.com/0013/001346/134685E.pdf#4.3");
  811. expect(data.unsafeUrl).toEqual("../../0013/001346/134685E.pdf#4.3");
  812. expect(data.dest).toBeUndefined();
  813. done();
  814. }, done.fail);
  815. });
  816. it("should correctly parse a GoToR action, with named destination", function (done) {
  817. const actionDict = new _primitives.Dict();
  818. actionDict.set("Type", _primitives.Name.get("Action"));
  819. actionDict.set("S", _primitives.Name.get("GoToR"));
  820. actionDict.set("F", "http://www.example.com/test.pdf");
  821. actionDict.set("D", "15");
  822. const annotationDict = new _primitives.Dict();
  823. annotationDict.set("Type", _primitives.Name.get("Annot"));
  824. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  825. annotationDict.set("A", actionDict);
  826. const annotationRef = _primitives.Ref.get(495, 0);
  827. const xref = new _test_utils.XRefMock([{
  828. ref: annotationRef,
  829. data: annotationDict
  830. }]);
  831. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  832. data
  833. }) => {
  834. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  835. expect(data.url).toEqual("http://www.example.com/test.pdf#15");
  836. expect(data.unsafeUrl).toEqual("http://www.example.com/test.pdf#15");
  837. expect(data.dest).toBeUndefined();
  838. expect(data.newWindow).toBeFalsy();
  839. done();
  840. }, done.fail);
  841. });
  842. it("should correctly parse a GoToR action, with explicit destination array", function (done) {
  843. const actionDict = new _primitives.Dict();
  844. actionDict.set("Type", _primitives.Name.get("Action"));
  845. actionDict.set("S", _primitives.Name.get("GoToR"));
  846. actionDict.set("F", "http://www.example.com/test.pdf");
  847. actionDict.set("D", [14, _primitives.Name.get("XYZ"), null, 298.043, null]);
  848. const annotationDict = new _primitives.Dict();
  849. annotationDict.set("Type", _primitives.Name.get("Annot"));
  850. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  851. annotationDict.set("A", actionDict);
  852. const annotationRef = _primitives.Ref.get(489, 0);
  853. const xref = new _test_utils.XRefMock([{
  854. ref: annotationRef,
  855. data: annotationDict
  856. }]);
  857. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  858. data
  859. }) => {
  860. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  861. expect(data.url).toEqual(new URL("http://www.example.com/test.pdf#" + '[14,{"name":"XYZ"},null,298.043,null]').href);
  862. expect(data.unsafeUrl).toEqual("http://www.example.com/test.pdf#" + '[14,{"name":"XYZ"},null,298.043,null]');
  863. expect(data.dest).toBeUndefined();
  864. expect(data.newWindow).toBeFalsy();
  865. done();
  866. }, done.fail);
  867. });
  868. it("should correctly parse a Launch action, where the FileSpec dict " + 'contains a relative URL, with the "docBaseUrl" parameter specified', function (done) {
  869. const fileSpecDict = new _primitives.Dict();
  870. fileSpecDict.set("Type", _primitives.Name.get("FileSpec"));
  871. fileSpecDict.set("F", "Part II/Part II.pdf");
  872. fileSpecDict.set("UF", "Part II/Part II.pdf");
  873. const actionDict = new _primitives.Dict();
  874. actionDict.set("Type", _primitives.Name.get("Action"));
  875. actionDict.set("S", _primitives.Name.get("Launch"));
  876. actionDict.set("F", fileSpecDict);
  877. actionDict.set("NewWindow", true);
  878. const annotationDict = new _primitives.Dict();
  879. annotationDict.set("Type", _primitives.Name.get("Annot"));
  880. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  881. annotationDict.set("A", actionDict);
  882. const annotationRef = _primitives.Ref.get(88, 0);
  883. const xref = new _test_utils.XRefMock([{
  884. ref: annotationRef,
  885. data: annotationDict
  886. }]);
  887. const pdfManager = new PDFManagerMock({
  888. docBaseUrl: "http://www.example.com/test/pdfs/qwerty.pdf"
  889. });
  890. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManager, idFactoryMock).then(({
  891. data
  892. }) => {
  893. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  894. expect(data.url).toEqual(new URL("http://www.example.com/test/pdfs/Part II/Part II.pdf").href);
  895. expect(data.unsafeUrl).toEqual("Part II/Part II.pdf");
  896. expect(data.dest).toBeUndefined();
  897. expect(data.newWindow).toEqual(true);
  898. done();
  899. }, done.fail);
  900. });
  901. it("should recover valid URLs from JavaScript actions having certain " + "white-listed formats", function (done) {
  902. function checkJsAction(params) {
  903. const jsEntry = params.jsEntry;
  904. const expectedUrl = params.expectedUrl;
  905. const expectedUnsafeUrl = params.expectedUnsafeUrl;
  906. const expectedNewWindow = params.expectedNewWindow;
  907. const actionDict = new _primitives.Dict();
  908. actionDict.set("Type", _primitives.Name.get("Action"));
  909. actionDict.set("S", _primitives.Name.get("JavaScript"));
  910. actionDict.set("JS", jsEntry);
  911. const annotationDict = new _primitives.Dict();
  912. annotationDict.set("Type", _primitives.Name.get("Annot"));
  913. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  914. annotationDict.set("A", actionDict);
  915. const annotationRef = _primitives.Ref.get(46, 0);
  916. const xref = new _test_utils.XRefMock([{
  917. ref: annotationRef,
  918. data: annotationDict
  919. }]);
  920. return _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  921. data
  922. }) => {
  923. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  924. expect(data.url).toEqual(expectedUrl);
  925. expect(data.unsafeUrl).toEqual(expectedUnsafeUrl);
  926. expect(data.dest).toBeUndefined();
  927. expect(data.newWindow).toEqual(expectedNewWindow);
  928. });
  929. }
  930. const annotation1 = checkJsAction({
  931. jsEntry: 'function someFun() { return "qwerty"; } someFun();',
  932. expectedUrl: undefined,
  933. expectedUnsafeUrl: undefined,
  934. expectedNewWindow: undefined
  935. });
  936. const annotation2 = checkJsAction({
  937. jsEntry: "window.open('http://www.example.com/test.pdf')",
  938. expectedUrl: new URL("http://www.example.com/test.pdf").href,
  939. expectedUnsafeUrl: "http://www.example.com/test.pdf",
  940. expectedNewWindow: undefined
  941. });
  942. const annotation3 = checkJsAction({
  943. jsEntry: new _stream.StringStream('app.launchURL("http://www.example.com/test.pdf", true)'),
  944. expectedUrl: new URL("http://www.example.com/test.pdf").href,
  945. expectedUnsafeUrl: "http://www.example.com/test.pdf",
  946. expectedNewWindow: true
  947. });
  948. Promise.all([annotation1, annotation2, annotation3]).then(done, done.fail);
  949. });
  950. it("should correctly parse a Named action", function (done) {
  951. const actionDict = new _primitives.Dict();
  952. actionDict.set("Type", _primitives.Name.get("Action"));
  953. actionDict.set("S", _primitives.Name.get("Named"));
  954. actionDict.set("N", _primitives.Name.get("GoToPage"));
  955. const annotationDict = new _primitives.Dict();
  956. annotationDict.set("Type", _primitives.Name.get("Annot"));
  957. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  958. annotationDict.set("A", actionDict);
  959. const annotationRef = _primitives.Ref.get(12, 0);
  960. const xref = new _test_utils.XRefMock([{
  961. ref: annotationRef,
  962. data: annotationDict
  963. }]);
  964. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  965. data
  966. }) => {
  967. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  968. expect(data.url).toBeUndefined();
  969. expect(data.unsafeUrl).toBeUndefined();
  970. expect(data.action).toEqual("GoToPage");
  971. done();
  972. }, done.fail);
  973. });
  974. it("should correctly parse a simple Dest", function (done) {
  975. const annotationDict = new _primitives.Dict();
  976. annotationDict.set("Type", _primitives.Name.get("Annot"));
  977. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  978. annotationDict.set("Dest", _primitives.Name.get("LI0"));
  979. const annotationRef = _primitives.Ref.get(583, 0);
  980. const xref = new _test_utils.XRefMock([{
  981. ref: annotationRef,
  982. data: annotationDict
  983. }]);
  984. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  985. data
  986. }) => {
  987. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  988. expect(data.url).toBeUndefined();
  989. expect(data.unsafeUrl).toBeUndefined();
  990. expect(data.dest).toEqual("LI0");
  991. done();
  992. }, done.fail);
  993. });
  994. it("should correctly parse a simple Dest, with explicit destination array", function (done) {
  995. const annotationDict = new _primitives.Dict();
  996. annotationDict.set("Type", _primitives.Name.get("Annot"));
  997. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  998. annotationDict.set("Dest", [_primitives.Ref.get(17, 0), _primitives.Name.get("XYZ"), 0, 841.89, null]);
  999. const annotationRef = _primitives.Ref.get(10, 0);
  1000. const xref = new _test_utils.XRefMock([{
  1001. ref: annotationRef,
  1002. data: annotationDict
  1003. }]);
  1004. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  1005. data
  1006. }) => {
  1007. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  1008. expect(data.url).toBeUndefined();
  1009. expect(data.unsafeUrl).toBeUndefined();
  1010. expect(data.dest).toEqual([{
  1011. num: 17,
  1012. gen: 0
  1013. }, {
  1014. name: "XYZ"
  1015. }, 0, 841.89, null]);
  1016. done();
  1017. }, done.fail);
  1018. });
  1019. it("should correctly parse a Dest, which violates the specification " + "by containing a dictionary", function (done) {
  1020. const destDict = new _primitives.Dict();
  1021. destDict.set("Type", _primitives.Name.get("Action"));
  1022. destDict.set("S", _primitives.Name.get("GoTo"));
  1023. destDict.set("D", "page.157");
  1024. const annotationDict = new _primitives.Dict();
  1025. annotationDict.set("Type", _primitives.Name.get("Annot"));
  1026. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  1027. annotationDict.set("Dest", destDict);
  1028. const annotationRef = _primitives.Ref.get(798, 0);
  1029. const xref = new _test_utils.XRefMock([{
  1030. ref: annotationRef,
  1031. data: annotationDict
  1032. }]);
  1033. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  1034. data
  1035. }) => {
  1036. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  1037. expect(data.url).toBeUndefined();
  1038. expect(data.unsafeUrl).toBeUndefined();
  1039. expect(data.dest).toEqual("page.157");
  1040. done();
  1041. }, done.fail);
  1042. });
  1043. it("should not set quadpoints if not defined", function (done) {
  1044. const annotationDict = new _primitives.Dict();
  1045. annotationDict.set("Type", _primitives.Name.get("Annot"));
  1046. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  1047. const annotationRef = _primitives.Ref.get(121, 0);
  1048. const xref = new _test_utils.XRefMock([{
  1049. ref: annotationRef,
  1050. data: annotationDict
  1051. }]);
  1052. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  1053. data
  1054. }) => {
  1055. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  1056. expect(data.quadPoints).toBeUndefined();
  1057. done();
  1058. }, done.fail);
  1059. });
  1060. it("should set quadpoints if defined", function (done) {
  1061. const annotationDict = new _primitives.Dict();
  1062. annotationDict.set("Type", _primitives.Name.get("Annot"));
  1063. annotationDict.set("Subtype", _primitives.Name.get("Link"));
  1064. annotationDict.set("Rect", [10, 10, 20, 20]);
  1065. annotationDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  1066. const annotationRef = _primitives.Ref.get(121, 0);
  1067. const xref = new _test_utils.XRefMock([{
  1068. ref: annotationRef,
  1069. data: annotationDict
  1070. }]);
  1071. _annotation.AnnotationFactory.create(xref, annotationRef, pdfManagerMock, idFactoryMock).then(({
  1072. data
  1073. }) => {
  1074. expect(data.annotationType).toEqual(_util.AnnotationType.LINK);
  1075. expect(data.quadPoints).toEqual([[{
  1076. x: 10,
  1077. y: 20
  1078. }, {
  1079. x: 20,
  1080. y: 20
  1081. }, {
  1082. x: 10,
  1083. y: 10
  1084. }, {
  1085. x: 20,
  1086. y: 10
  1087. }]]);
  1088. done();
  1089. }, done.fail);
  1090. });
  1091. });
  1092. describe("WidgetAnnotation", function () {
  1093. let widgetDict;
  1094. beforeEach(function (done) {
  1095. widgetDict = new _primitives.Dict();
  1096. widgetDict.set("Type", _primitives.Name.get("Annot"));
  1097. widgetDict.set("Subtype", _primitives.Name.get("Widget"));
  1098. done();
  1099. });
  1100. afterEach(function () {
  1101. widgetDict = null;
  1102. });
  1103. it("should handle unknown field names", function (done) {
  1104. const widgetRef = _primitives.Ref.get(20, 0);
  1105. const xref = new _test_utils.XRefMock([{
  1106. ref: widgetRef,
  1107. data: widgetDict
  1108. }]);
  1109. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(({
  1110. data
  1111. }) => {
  1112. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1113. expect(data.fieldName).toEqual("");
  1114. done();
  1115. }, done.fail);
  1116. });
  1117. it("should construct the field name when there are no ancestors", function (done) {
  1118. widgetDict.set("T", "foo");
  1119. const widgetRef = _primitives.Ref.get(21, 0);
  1120. const xref = new _test_utils.XRefMock([{
  1121. ref: widgetRef,
  1122. data: widgetDict
  1123. }]);
  1124. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(({
  1125. data
  1126. }) => {
  1127. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1128. expect(data.fieldName).toEqual("foo");
  1129. done();
  1130. }, done.fail);
  1131. });
  1132. it("should construct the field name when there are ancestors", function (done) {
  1133. const firstParent = new _primitives.Dict();
  1134. firstParent.set("T", "foo");
  1135. const secondParent = new _primitives.Dict();
  1136. secondParent.set("Parent", firstParent);
  1137. secondParent.set("T", "bar");
  1138. widgetDict.set("Parent", secondParent);
  1139. widgetDict.set("T", "baz");
  1140. const widgetRef = _primitives.Ref.get(22, 0);
  1141. const xref = new _test_utils.XRefMock([{
  1142. ref: widgetRef,
  1143. data: widgetDict
  1144. }]);
  1145. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(({
  1146. data
  1147. }) => {
  1148. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1149. expect(data.fieldName).toEqual("foo.bar.baz");
  1150. done();
  1151. }, done.fail);
  1152. });
  1153. it("should construct the field name if a parent is not a dictionary " + "(issue 8143)", function (done) {
  1154. const parentDict = new _primitives.Dict();
  1155. parentDict.set("Parent", null);
  1156. parentDict.set("T", "foo");
  1157. widgetDict.set("Parent", parentDict);
  1158. widgetDict.set("T", "bar");
  1159. const widgetRef = _primitives.Ref.get(22, 0);
  1160. const xref = new _test_utils.XRefMock([{
  1161. ref: widgetRef,
  1162. data: widgetDict
  1163. }]);
  1164. _annotation.AnnotationFactory.create(xref, widgetRef, pdfManagerMock, idFactoryMock).then(({
  1165. data
  1166. }) => {
  1167. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1168. expect(data.fieldName).toEqual("foo.bar");
  1169. done();
  1170. }, done.fail);
  1171. });
  1172. });
  1173. describe("TextWidgetAnnotation", function () {
  1174. let textWidgetDict, helvRefObj, gothRefObj;
  1175. beforeEach(function (done) {
  1176. textWidgetDict = new _primitives.Dict();
  1177. textWidgetDict.set("Type", _primitives.Name.get("Annot"));
  1178. textWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  1179. textWidgetDict.set("FT", _primitives.Name.get("Tx"));
  1180. const helvDict = new _primitives.Dict();
  1181. helvDict.set("BaseFont", _primitives.Name.get("Helvetica"));
  1182. helvDict.set("Type", _primitives.Name.get("Font"));
  1183. helvDict.set("Subtype", _primitives.Name.get("Type1"));
  1184. const gothDict = new _primitives.Dict();
  1185. gothDict.set("BaseFont", _primitives.Name.get("MSGothic"));
  1186. gothDict.set("Type", _primitives.Name.get("Font"));
  1187. gothDict.set("Subtype", _primitives.Name.get("Type0"));
  1188. gothDict.set("Encoding", _primitives.Name.get("UniJIS-UTF16-H"));
  1189. gothDict.set("Name", _primitives.Name.get("MSGothic"));
  1190. const cidSysInfoDict = new _primitives.Dict();
  1191. cidSysInfoDict.set("Ordering", "Japan1");
  1192. cidSysInfoDict.set("Registry", "Adobe");
  1193. cidSysInfoDict.set("Supplement", "5");
  1194. const fontDescriptorDict = new _primitives.Dict();
  1195. fontDescriptorDict.set("FontName", _primitives.Name.get("MSGothic"));
  1196. fontDescriptorDict.set("CapHeight", "680");
  1197. const gothDescendantDict = new _primitives.Dict();
  1198. gothDescendantDict.set("BaseFont", _primitives.Name.get("MSGothic"));
  1199. gothDescendantDict.set("CIDSystemInfo", cidSysInfoDict);
  1200. gothDescendantDict.set("Subtype", _primitives.Name.get("CIDFontType2"));
  1201. gothDescendantDict.set("Type", _primitives.Name.get("Font"));
  1202. gothDescendantDict.set("FontDescriptor", fontDescriptorDict);
  1203. gothDict.set("DescendantFonts", [gothDescendantDict]);
  1204. const helvRef = _primitives.Ref.get(314, 0);
  1205. const gothRef = _primitives.Ref.get(159, 0);
  1206. helvRefObj = {
  1207. ref: helvRef,
  1208. data: helvDict
  1209. };
  1210. gothRefObj = {
  1211. ref: gothRef,
  1212. data: gothDict
  1213. };
  1214. const resourceDict = new _primitives.Dict();
  1215. const fontDict = new _primitives.Dict();
  1216. fontDict.set("Helv", helvRef);
  1217. resourceDict.set("Font", fontDict);
  1218. textWidgetDict.set("DA", "/Helv 5 Tf");
  1219. textWidgetDict.set("DR", resourceDict);
  1220. textWidgetDict.set("Rect", [0, 0, 32, 10]);
  1221. done();
  1222. });
  1223. afterEach(function () {
  1224. textWidgetDict = helvRefObj = gothRefObj = null;
  1225. });
  1226. it("should handle unknown text alignment, maximum length and flags", function (done) {
  1227. textWidgetDict.set("DV", "foo");
  1228. const textWidgetRef = _primitives.Ref.get(124, 0);
  1229. const xref = new _test_utils.XRefMock([{
  1230. ref: textWidgetRef,
  1231. data: textWidgetDict
  1232. }]);
  1233. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1234. data
  1235. }) => {
  1236. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1237. expect(data.textAlignment).toEqual(null);
  1238. expect(data.maxLen).toEqual(null);
  1239. expect(data.readOnly).toEqual(false);
  1240. expect(data.hidden).toEqual(false);
  1241. expect(data.multiLine).toEqual(false);
  1242. expect(data.comb).toEqual(false);
  1243. expect(data.defaultFieldValue).toEqual("foo");
  1244. done();
  1245. }, done.fail);
  1246. });
  1247. it("should not set invalid text alignment, maximum length and flags", function (done) {
  1248. textWidgetDict.set("Q", "center");
  1249. textWidgetDict.set("MaxLen", "five");
  1250. textWidgetDict.set("Ff", "readonly");
  1251. const textWidgetRef = _primitives.Ref.get(43, 0);
  1252. const xref = new _test_utils.XRefMock([{
  1253. ref: textWidgetRef,
  1254. data: textWidgetDict
  1255. }]);
  1256. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1257. data
  1258. }) => {
  1259. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1260. expect(data.textAlignment).toEqual(null);
  1261. expect(data.maxLen).toEqual(null);
  1262. expect(data.readOnly).toEqual(false);
  1263. expect(data.hidden).toEqual(false);
  1264. expect(data.multiLine).toEqual(false);
  1265. expect(data.comb).toEqual(false);
  1266. done();
  1267. }, done.fail);
  1268. });
  1269. it("should set valid text alignment, maximum length and flags", function (done) {
  1270. textWidgetDict.set("Q", 1);
  1271. textWidgetDict.set("MaxLen", 20);
  1272. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.MULTILINE);
  1273. const textWidgetRef = _primitives.Ref.get(84, 0);
  1274. const xref = new _test_utils.XRefMock([{
  1275. ref: textWidgetRef,
  1276. data: textWidgetDict
  1277. }]);
  1278. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1279. data
  1280. }) => {
  1281. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1282. expect(data.textAlignment).toEqual(1);
  1283. expect(data.maxLen).toEqual(20);
  1284. expect(data.readOnly).toEqual(true);
  1285. expect(data.hidden).toEqual(false);
  1286. expect(data.multiLine).toEqual(true);
  1287. done();
  1288. }, done.fail);
  1289. });
  1290. it("should reject comb fields without a maximum length", function (done) {
  1291. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.COMB);
  1292. const textWidgetRef = _primitives.Ref.get(46, 0);
  1293. const xref = new _test_utils.XRefMock([{
  1294. ref: textWidgetRef,
  1295. data: textWidgetDict
  1296. }]);
  1297. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1298. data
  1299. }) => {
  1300. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1301. expect(data.comb).toEqual(false);
  1302. done();
  1303. }, done.fail);
  1304. });
  1305. it("should accept comb fields with a maximum length", function (done) {
  1306. textWidgetDict.set("MaxLen", 20);
  1307. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.COMB);
  1308. const textWidgetRef = _primitives.Ref.get(46, 0);
  1309. const xref = new _test_utils.XRefMock([{
  1310. ref: textWidgetRef,
  1311. data: textWidgetDict
  1312. }]);
  1313. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1314. data
  1315. }) => {
  1316. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1317. expect(data.comb).toEqual(true);
  1318. done();
  1319. }, done.fail);
  1320. });
  1321. it("should only accept comb fields when the flags are valid", function (done) {
  1322. const invalidFieldFlags = [_util.AnnotationFieldFlag.MULTILINE, _util.AnnotationFieldFlag.PASSWORD, _util.AnnotationFieldFlag.FILESELECT];
  1323. let flags = _util.AnnotationFieldFlag.COMB + _util.AnnotationFieldFlag.MULTILINE + _util.AnnotationFieldFlag.PASSWORD + _util.AnnotationFieldFlag.FILESELECT;
  1324. let promise = Promise.resolve();
  1325. for (let i = 0, ii = invalidFieldFlags.length; i <= ii; i++) {
  1326. promise = promise.then(() => {
  1327. textWidgetDict.set("MaxLen", 20);
  1328. textWidgetDict.set("Ff", flags);
  1329. const textWidgetRef = _primitives.Ref.get(93, 0);
  1330. const xref = new _test_utils.XRefMock([{
  1331. ref: textWidgetRef,
  1332. data: textWidgetDict
  1333. }]);
  1334. return _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1335. data
  1336. }) => {
  1337. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1338. const valid = invalidFieldFlags.length === 0;
  1339. expect(data.comb).toEqual(valid);
  1340. if (!valid) {
  1341. flags -= invalidFieldFlags.pop();
  1342. }
  1343. });
  1344. });
  1345. }
  1346. promise.then(done, done.fail);
  1347. });
  1348. it("should render regular text for printing", function (done) {
  1349. const textWidgetRef = _primitives.Ref.get(271, 0);
  1350. const xref = new _test_utils.XRefMock([{
  1351. ref: textWidgetRef,
  1352. data: textWidgetDict
  1353. }, helvRefObj]);
  1354. const task = new _worker.WorkerTask("test print");
  1355. partialEvaluator.xref = xref;
  1356. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1357. const id = annotation.data.id;
  1358. const annotationStorage = {};
  1359. annotationStorage[id] = {
  1360. value: "test\\print"
  1361. };
  1362. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1363. }, done.fail).then(appearance => {
  1364. expect(appearance).toEqual("/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm" + " 2.00 2.00 Td (test\\\\print) Tj ET Q EMC");
  1365. done();
  1366. }, done.fail);
  1367. });
  1368. it("should render regular text in Japanese for printing", function (done) {
  1369. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1370. textWidgetDict.set("DA", "/Goth 5 Tf");
  1371. const textWidgetRef = _primitives.Ref.get(271, 0);
  1372. const xref = new _test_utils.XRefMock([{
  1373. ref: textWidgetRef,
  1374. data: textWidgetDict
  1375. }, gothRefObj]);
  1376. const task = new _worker.WorkerTask("test print");
  1377. partialEvaluator.xref = xref;
  1378. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1379. const id = annotation.data.id;
  1380. const annotationStorage = {};
  1381. annotationStorage[id] = {
  1382. value: "こんにちは世界の"
  1383. };
  1384. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1385. }, done.fail).then(appearance => {
  1386. const utf16String = "\x30\x53\x30\x93\x30\x6b\x30\x61" + "\x30\x6f\x4e\x16\x75\x4c\x30\x6e";
  1387. expect(appearance).toEqual("/Tx BMC q BT /Goth 5 Tf 1 0 0 1 0 0 Tm" + ` 2.00 2.00 Td (${utf16String}) Tj ET Q EMC`);
  1388. done();
  1389. }, done.fail);
  1390. });
  1391. it("should render regular text for printing using normal appearance", function (done) {
  1392. const textWidgetRef = _primitives.Ref.get(271, 0);
  1393. const appearanceStatesDict = new _primitives.Dict();
  1394. const normalAppearanceDict = new _primitives.Dict();
  1395. const normalAppearanceStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1396. normalAppearanceStream.dict = normalAppearanceDict;
  1397. appearanceStatesDict.set("N", normalAppearanceStream);
  1398. textWidgetDict.set("AP", appearanceStatesDict);
  1399. const xref = new _test_utils.XRefMock([{
  1400. ref: textWidgetRef,
  1401. data: textWidgetDict
  1402. }, helvRefObj]);
  1403. const task = new _worker.WorkerTask("test print");
  1404. partialEvaluator.xref = xref;
  1405. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1406. const annotationStorage = {};
  1407. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  1408. }).then(opList => {
  1409. expect(opList.argsArray.length).toEqual(3);
  1410. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1411. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1412. done();
  1413. }).catch(done.fail);
  1414. });
  1415. it("should render auto-sized text for printing", function (done) {
  1416. textWidgetDict.set("DA", "/Helv 0 Tf");
  1417. const textWidgetRef = _primitives.Ref.get(271, 0);
  1418. const xref = new _test_utils.XRefMock([{
  1419. ref: textWidgetRef,
  1420. data: textWidgetDict
  1421. }, helvRefObj]);
  1422. const task = new _worker.WorkerTask("test print");
  1423. partialEvaluator.xref = xref;
  1424. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1425. const id = annotation.data.id;
  1426. const annotationStorage = {};
  1427. annotationStorage[id] = {
  1428. value: "test (print)"
  1429. };
  1430. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1431. }, done.fail).then(appearance => {
  1432. expect(appearance).toEqual("/Tx BMC q BT /Helv 11 Tf 0 g 1 0 0 1 0 0 Tm" + " 2.00 2.00 Td (test \\(print\\)) Tj ET Q EMC");
  1433. done();
  1434. }, done.fail);
  1435. });
  1436. it("should render auto-sized text in Japanese for printing", function (done) {
  1437. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1438. textWidgetDict.set("DA", "/Goth 0 Tf");
  1439. const textWidgetRef = _primitives.Ref.get(271, 0);
  1440. const xref = new _test_utils.XRefMock([{
  1441. ref: textWidgetRef,
  1442. data: textWidgetDict
  1443. }, gothRefObj]);
  1444. const task = new _worker.WorkerTask("test print");
  1445. partialEvaluator.xref = xref;
  1446. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1447. const id = annotation.data.id;
  1448. const annotationStorage = {};
  1449. annotationStorage[id] = {
  1450. value: "こんにちは世界の"
  1451. };
  1452. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1453. }, done.fail).then(appearance => {
  1454. const utf16String = "\x30\x53\x30\x93\x30\x6b\x30\x61" + "\x30\x6f\x4e\x16\x75\x4c\x30\x6e";
  1455. expect(appearance).toEqual("/Tx BMC q BT /Goth 9 Tf 0 g 1 0 0 1 0 0 Tm" + ` 2.00 2.00 Td (${utf16String}) Tj ET Q EMC`);
  1456. done();
  1457. }, done.fail);
  1458. });
  1459. it("should not render a password for printing", function (done) {
  1460. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.PASSWORD);
  1461. const textWidgetRef = _primitives.Ref.get(271, 0);
  1462. const xref = new _test_utils.XRefMock([{
  1463. ref: textWidgetRef,
  1464. data: textWidgetDict
  1465. }, helvRefObj]);
  1466. const task = new _worker.WorkerTask("test print");
  1467. partialEvaluator.xref = xref;
  1468. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1469. const id = annotation.data.id;
  1470. const annotationStorage = {};
  1471. annotationStorage[id] = {
  1472. value: "mypassword"
  1473. };
  1474. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1475. }, done.fail).then(appearance => {
  1476. expect(appearance).toEqual(null);
  1477. done();
  1478. }, done.fail);
  1479. });
  1480. it("should render multiline text for printing", function (done) {
  1481. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTILINE);
  1482. const textWidgetRef = _primitives.Ref.get(271, 0);
  1483. const xref = new _test_utils.XRefMock([{
  1484. ref: textWidgetRef,
  1485. data: textWidgetDict
  1486. }, helvRefObj]);
  1487. const task = new _worker.WorkerTask("test print");
  1488. partialEvaluator.xref = xref;
  1489. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1490. const id = annotation.data.id;
  1491. const annotationStorage = {};
  1492. annotationStorage[id] = {
  1493. value: "a aa aaa aaaa aaaaa aaaaaa " + "pneumonoultramicroscopicsilicovolcanoconiosis"
  1494. };
  1495. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1496. }, done.fail).then(appearance => {
  1497. expect(appearance).toEqual("/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 10 Tm " + "2.00 -5.00 Td (a aa aaa ) Tj\n" + "0.00 -5.00 Td (aaaa aaaaa ) Tj\n" + "0.00 -5.00 Td (aaaaaa ) Tj\n" + "0.00 -5.00 Td (pneumonoultr) Tj\n" + "0.00 -5.00 Td (amicroscopi) Tj\n" + "0.00 -5.00 Td (csilicovolca) Tj\n" + "0.00 -5.00 Td (noconiosis) Tj ET Q EMC");
  1498. done();
  1499. }, done.fail);
  1500. });
  1501. it("should render multiline text in Japanese for printing", function (done) {
  1502. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTILINE);
  1503. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1504. textWidgetDict.set("DA", "/Goth 5 Tf");
  1505. const textWidgetRef = _primitives.Ref.get(271, 0);
  1506. const xref = new _test_utils.XRefMock([{
  1507. ref: textWidgetRef,
  1508. data: textWidgetDict
  1509. }, gothRefObj]);
  1510. const task = new _worker.WorkerTask("test print");
  1511. partialEvaluator.xref = xref;
  1512. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1513. const id = annotation.data.id;
  1514. const annotationStorage = {};
  1515. annotationStorage[id] = {
  1516. value: "こんにちは世界の"
  1517. };
  1518. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1519. }, done.fail).then(appearance => {
  1520. expect(appearance).toEqual("/Tx BMC q BT /Goth 5 Tf 1 0 0 1 0 10 Tm " + "2.00 -5.00 Td (\x30\x53\x30\x93\x30\x6b\x30\x61\x30\x6f) Tj\n" + "0.00 -5.00 Td (\x4e\x16\x75\x4c\x30\x6e) Tj ET Q EMC");
  1521. done();
  1522. }, done.fail);
  1523. });
  1524. it("should render multiline text with various EOL for printing", function (done) {
  1525. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTILINE);
  1526. textWidgetDict.set("Rect", [0, 0, 128, 10]);
  1527. const textWidgetRef = _primitives.Ref.get(271, 0);
  1528. const xref = new _test_utils.XRefMock([{
  1529. ref: textWidgetRef,
  1530. data: textWidgetDict
  1531. }, helvRefObj]);
  1532. const task = new _worker.WorkerTask("test print");
  1533. partialEvaluator.xref = xref;
  1534. const expectedAppearance = "/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 10 Tm " + "2.00 -5.00 Td " + "(Lorem ipsum dolor sit amet, consectetur adipiscing elit.) Tj\n" + "0.00 -5.00 Td " + "(Aliquam vitae felis ac lectus bibendum ultricies quis non) Tj\n" + "0.00 -5.00 Td " + "( diam.) Tj\n" + "0.00 -5.00 Td " + "(Morbi id porttitor quam, a iaculis dui.) Tj\n" + "0.00 -5.00 Td " + "(Pellentesque habitant morbi tristique senectus et netus ) Tj\n" + "0.00 -5.00 Td " + "(et malesuada fames ac turpis egestas.) Tj\n" + "0.00 -5.00 Td () Tj\n" + "0.00 -5.00 Td () Tj\n" + "0.00 -5.00 Td " + "(Nulla consectetur, ligula in tincidunt placerat, velit ) Tj\n" + "0.00 -5.00 Td " + "(augue consectetur orci, sed mattis libero nunc ut massa.) Tj\n" + "0.00 -5.00 Td " + "(Etiam facilisis tempus interdum.) Tj ET Q EMC";
  1535. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1536. const id = annotation.data.id;
  1537. const annotationStorage = {};
  1538. annotationStorage[id] = {
  1539. value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r" + "Aliquam vitae felis ac lectus bibendum ultricies quis non diam.\n" + "Morbi id porttitor quam, a iaculis dui.\r\n" + "Pellentesque habitant morbi tristique senectus et " + "netus et malesuada fames ac turpis egestas.\n\r\n\r" + "Nulla consectetur, ligula in tincidunt placerat, " + "velit augue consectetur orci, sed mattis libero nunc ut massa.\r" + "Etiam facilisis tempus interdum."
  1540. };
  1541. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1542. }, done.fail).then(appearance => {
  1543. expect(appearance).toEqual(expectedAppearance);
  1544. done();
  1545. }, done.fail);
  1546. });
  1547. it("should render comb for printing", function (done) {
  1548. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.COMB);
  1549. textWidgetDict.set("MaxLen", 4);
  1550. const textWidgetRef = _primitives.Ref.get(271, 0);
  1551. const xref = new _test_utils.XRefMock([{
  1552. ref: textWidgetRef,
  1553. data: textWidgetDict
  1554. }, helvRefObj]);
  1555. const task = new _worker.WorkerTask("test print");
  1556. partialEvaluator.xref = xref;
  1557. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1558. const id = annotation.data.id;
  1559. const annotationStorage = {};
  1560. annotationStorage[id] = {
  1561. value: "aa(aa)a\\"
  1562. };
  1563. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1564. }, done.fail).then(appearance => {
  1565. expect(appearance).toEqual("/Tx BMC q BT /Helv 5 Tf 1 0 0 1 2 2 Tm" + " (a) Tj 8.00 0 Td (a) Tj 8.00 0 Td (\\() Tj" + " 8.00 0 Td (a) Tj 8.00 0 Td (a) Tj" + " 8.00 0 Td (\\)) Tj 8.00 0 Td (a) Tj" + " 8.00 0 Td (\\\\) Tj ET Q EMC");
  1566. done();
  1567. }, done.fail);
  1568. });
  1569. it("should render comb with Japanese text for printing", function (done) {
  1570. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.COMB);
  1571. textWidgetDict.set("MaxLen", 4);
  1572. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1573. textWidgetDict.set("DA", "/Goth 5 Tf");
  1574. textWidgetDict.set("Rect", [0, 0, 32, 10]);
  1575. const textWidgetRef = _primitives.Ref.get(271, 0);
  1576. const xref = new _test_utils.XRefMock([{
  1577. ref: textWidgetRef,
  1578. data: textWidgetDict
  1579. }, gothRefObj]);
  1580. const task = new _worker.WorkerTask("test print");
  1581. partialEvaluator.xref = xref;
  1582. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1583. const id = annotation.data.id;
  1584. const annotationStorage = {};
  1585. annotationStorage[id] = {
  1586. value: "こんにちは世界の"
  1587. };
  1588. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1589. }, done.fail).then(appearance => {
  1590. expect(appearance).toEqual("/Tx BMC q BT /Goth 5 Tf 1 0 0 1 2 2 Tm" + " (\x30\x53) Tj 8.00 0 Td (\x30\x93) Tj 8.00 0 Td (\x30\x6b) Tj" + " 8.00 0 Td (\x30\x61) Tj 8.00 0 Td (\x30\x6f) Tj" + " 8.00 0 Td (\x4e\x16) Tj 8.00 0 Td (\x75\x4c) Tj" + " 8.00 0 Td (\x30\x6e) Tj ET Q EMC");
  1591. done();
  1592. }, done.fail);
  1593. });
  1594. it("should save text", function (done) {
  1595. const textWidgetRef = _primitives.Ref.get(123, 0);
  1596. const xref = new _test_utils.XRefMock([{
  1597. ref: textWidgetRef,
  1598. data: textWidgetDict
  1599. }, helvRefObj]);
  1600. partialEvaluator.xref = xref;
  1601. const task = new _worker.WorkerTask("test save");
  1602. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1603. const annotationStorage = {};
  1604. annotationStorage[annotation.data.id] = {
  1605. value: "hello world"
  1606. };
  1607. return annotation.save(partialEvaluator, task, annotationStorage);
  1608. }, done.fail).then(data => {
  1609. expect(data.length).toEqual(2);
  1610. const [oldData, newData] = data;
  1611. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1612. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  1613. oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)");
  1614. expect(oldData.data).toEqual("123 0 obj\n" + "<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Helv 5 Tf) /DR " + "<< /Font << /Helv 314 0 R>>>> /Rect [0 0 32 10] " + "/V (hello world) /AP << /N 2 0 R>> /M (date)>>\nendobj\n");
  1615. expect(newData.data).toEqual("2 0 obj\n<< /Length 77 /Subtype /Form /Resources " + "<< /Font << /Helv 314 0 R>>>> /BBox [0 0 32 10]>> stream\n" + "/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm 2.00 2.00 Td (hello world) Tj " + "ET Q EMC\nendstream\nendobj\n");
  1616. done();
  1617. }, done.fail);
  1618. });
  1619. it("should get field object for usage in JS sandbox", function (done) {
  1620. const textWidgetRef = _primitives.Ref.get(123, 0);
  1621. const xDictRef = _primitives.Ref.get(141, 0);
  1622. const dDictRef = _primitives.Ref.get(262, 0);
  1623. const next0Ref = _primitives.Ref.get(314, 0);
  1624. const next1Ref = _primitives.Ref.get(271, 0);
  1625. const next2Ref = _primitives.Ref.get(577, 0);
  1626. const next00Ref = _primitives.Ref.get(413, 0);
  1627. const xDict = new _primitives.Dict();
  1628. const dDict = new _primitives.Dict();
  1629. const next0Dict = new _primitives.Dict();
  1630. const next1Dict = new _primitives.Dict();
  1631. const next2Dict = new _primitives.Dict();
  1632. const next00Dict = new _primitives.Dict();
  1633. const xref = new _test_utils.XRefMock([{
  1634. ref: textWidgetRef,
  1635. data: textWidgetDict
  1636. }, {
  1637. ref: xDictRef,
  1638. data: xDict
  1639. }, {
  1640. ref: dDictRef,
  1641. data: dDict
  1642. }, {
  1643. ref: next0Ref,
  1644. data: next0Dict
  1645. }, {
  1646. ref: next00Ref,
  1647. data: next00Dict
  1648. }, {
  1649. ref: next1Ref,
  1650. data: next1Dict
  1651. }, {
  1652. ref: next2Ref,
  1653. data: next2Dict
  1654. }]);
  1655. const JS = _primitives.Name.get("JavaScript");
  1656. const additionalActionsDict = new _primitives.Dict();
  1657. const eDict = new _primitives.Dict();
  1658. eDict.set("JS", "hello()");
  1659. eDict.set("S", JS);
  1660. additionalActionsDict.set("E", eDict);
  1661. xDict.set("JS", "world()");
  1662. xDict.set("S", JS);
  1663. xDict.set("Next", [next0Ref, next1Ref, next2Ref, xDictRef]);
  1664. next0Dict.set("JS", "olleh()");
  1665. next0Dict.set("S", JS);
  1666. next0Dict.set("Next", next00Ref);
  1667. next00Dict.set("JS", "foo()");
  1668. next00Dict.set("S", JS);
  1669. next00Dict.set("Next", next0Ref);
  1670. next1Dict.set("JS", "dlrow()");
  1671. next1Dict.set("S", JS);
  1672. next1Dict.set("Next", xDictRef);
  1673. next2Dict.set("JS", "oof()");
  1674. next2Dict.set("S", JS);
  1675. dDict.set("JS", "bar()");
  1676. dDict.set("S", JS);
  1677. dDict.set("Next", dDictRef);
  1678. additionalActionsDict.set("D", dDictRef);
  1679. additionalActionsDict.set("X", xDictRef);
  1680. textWidgetDict.set("AA", additionalActionsDict);
  1681. partialEvaluator.xref = xref;
  1682. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1683. return annotation.getFieldObject();
  1684. }).then(object => {
  1685. const actions = object.actions;
  1686. expect(actions["Mouse Enter"]).toEqual(["hello()"]);
  1687. expect(actions["Mouse Exit"]).toEqual(["world()", "olleh()", "foo()", "dlrow()", "oof()"]);
  1688. expect(actions["Mouse Down"]).toEqual(["bar()"]);
  1689. done();
  1690. }, done.fail);
  1691. });
  1692. it("should save Japanese text", function (done) {
  1693. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1694. textWidgetDict.set("DA", "/Goth 5 Tf");
  1695. const textWidgetRef = _primitives.Ref.get(123, 0);
  1696. const xref = new _test_utils.XRefMock([{
  1697. ref: textWidgetRef,
  1698. data: textWidgetDict
  1699. }, gothRefObj]);
  1700. partialEvaluator.xref = xref;
  1701. const task = new _worker.WorkerTask("test save");
  1702. _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1703. const annotationStorage = {};
  1704. annotationStorage[annotation.data.id] = {
  1705. value: "こんにちは世界の"
  1706. };
  1707. return annotation.save(partialEvaluator, task, annotationStorage);
  1708. }, done.fail).then(data => {
  1709. const utf16String = "\x30\x53\x30\x93\x30\x6b\x30\x61" + "\x30\x6f\x4e\x16\x75\x4c\x30\x6e";
  1710. expect(data.length).toEqual(2);
  1711. const [oldData, newData] = data;
  1712. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1713. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  1714. oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)");
  1715. expect(oldData.data).toEqual("123 0 obj\n" + "<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Goth 5 Tf) /DR " + "<< /Font << /Helv 314 0 R /Goth 159 0 R>>>> /Rect [0 0 32 10] " + `/V (\xfe\xff${utf16String}) /AP << /N 2 0 R>> /M (date)>>\nendobj\n`);
  1716. expect(newData.data).toEqual("2 0 obj\n<< /Length 82 /Subtype /Form /Resources " + "<< /Font << /Helv 314 0 R /Goth 159 0 R>>>> /BBox [0 0 32 10]>> stream\n" + `/Tx BMC q BT /Goth 5 Tf 1 0 0 1 0 0 Tm 2.00 2.00 Td (${utf16String}) Tj ` + "ET Q EMC\nendstream\nendobj\n");
  1717. done();
  1718. }, done.fail);
  1719. });
  1720. });
  1721. describe("ButtonWidgetAnnotation", function () {
  1722. let buttonWidgetDict;
  1723. beforeEach(function (done) {
  1724. buttonWidgetDict = new _primitives.Dict();
  1725. buttonWidgetDict.set("Type", _primitives.Name.get("Annot"));
  1726. buttonWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  1727. buttonWidgetDict.set("FT", _primitives.Name.get("Btn"));
  1728. done();
  1729. });
  1730. afterEach(function () {
  1731. buttonWidgetDict = null;
  1732. });
  1733. it("should handle checkboxes with export value", function (done) {
  1734. buttonWidgetDict.set("V", _primitives.Name.get("1"));
  1735. buttonWidgetDict.set("DV", _primitives.Name.get("2"));
  1736. const appearanceStatesDict = new _primitives.Dict();
  1737. const normalAppearanceDict = new _primitives.Dict();
  1738. normalAppearanceDict.set("Off", 0);
  1739. normalAppearanceDict.set("Checked", 1);
  1740. appearanceStatesDict.set("N", normalAppearanceDict);
  1741. buttonWidgetDict.set("AP", appearanceStatesDict);
  1742. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1743. const xref = new _test_utils.XRefMock([{
  1744. ref: buttonWidgetRef,
  1745. data: buttonWidgetDict
  1746. }]);
  1747. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1748. data
  1749. }) => {
  1750. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1751. expect(data.checkBox).toEqual(true);
  1752. expect(data.fieldValue).toEqual("1");
  1753. expect(data.defaultFieldValue).toEqual("2");
  1754. expect(data.radioButton).toEqual(false);
  1755. expect(data.exportValue).toEqual("Checked");
  1756. done();
  1757. }, done.fail);
  1758. });
  1759. it("should handle checkboxes without export value", function (done) {
  1760. buttonWidgetDict.set("V", _primitives.Name.get("1"));
  1761. buttonWidgetDict.set("DV", _primitives.Name.get("2"));
  1762. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1763. const xref = new _test_utils.XRefMock([{
  1764. ref: buttonWidgetRef,
  1765. data: buttonWidgetDict
  1766. }]);
  1767. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1768. data
  1769. }) => {
  1770. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1771. expect(data.checkBox).toEqual(true);
  1772. expect(data.fieldValue).toEqual("1");
  1773. expect(data.defaultFieldValue).toEqual("2");
  1774. expect(data.radioButton).toEqual(false);
  1775. done();
  1776. }, done.fail);
  1777. });
  1778. it("should handle checkboxes without /Off appearance", function (done) {
  1779. buttonWidgetDict.set("V", _primitives.Name.get("1"));
  1780. buttonWidgetDict.set("DV", _primitives.Name.get("2"));
  1781. const appearanceStatesDict = new _primitives.Dict();
  1782. const normalAppearanceDict = new _primitives.Dict();
  1783. normalAppearanceDict.set("Checked", 1);
  1784. appearanceStatesDict.set("N", normalAppearanceDict);
  1785. buttonWidgetDict.set("AP", appearanceStatesDict);
  1786. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1787. const xref = new _test_utils.XRefMock([{
  1788. ref: buttonWidgetRef,
  1789. data: buttonWidgetDict
  1790. }]);
  1791. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  1792. data
  1793. }) => {
  1794. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1795. expect(data.checkBox).toEqual(true);
  1796. expect(data.fieldValue).toEqual("1");
  1797. expect(data.defaultFieldValue).toEqual("2");
  1798. expect(data.radioButton).toEqual(false);
  1799. expect(data.exportValue).toEqual("Checked");
  1800. done();
  1801. }, done.fail);
  1802. });
  1803. it("should render checkbox with fallback font for printing", function (done) {
  1804. const appearanceStatesDict = new _primitives.Dict();
  1805. const normalAppearanceDict = new _primitives.Dict();
  1806. const checkedAppearanceDict = new _primitives.Dict();
  1807. const uncheckedAppearanceDict = new _primitives.Dict();
  1808. const checkedStream = new _stream.StringStream("/ 12 Tf (4) Tj");
  1809. checkedStream.dict = checkedAppearanceDict;
  1810. const uncheckedStream = new _stream.StringStream("");
  1811. uncheckedStream.dict = uncheckedAppearanceDict;
  1812. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1813. checkedAppearanceDict.set("FormType", 1);
  1814. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1815. normalAppearanceDict.set("Checked", checkedStream);
  1816. normalAppearanceDict.set("Off", uncheckedStream);
  1817. appearanceStatesDict.set("N", normalAppearanceDict);
  1818. buttonWidgetDict.set("AP", appearanceStatesDict);
  1819. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1820. const xref = new _test_utils.XRefMock([{
  1821. ref: buttonWidgetRef,
  1822. data: buttonWidgetDict
  1823. }]);
  1824. const task = new _worker.WorkerTask("test print");
  1825. partialEvaluator.options = {
  1826. ignoreErrors: true
  1827. };
  1828. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1829. const annotationStorage = {};
  1830. annotationStorage[annotation.data.id] = {
  1831. value: true
  1832. };
  1833. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  1834. }).then(opList => {
  1835. expect(opList.argsArray.length).toEqual(5);
  1836. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.dependency, _util.OPS.setFont, _util.OPS.showText, _util.OPS.endAnnotation]);
  1837. expect(opList.argsArray[3][0][0].fontChar).toEqual("✔");
  1838. done();
  1839. }).catch(done.fail);
  1840. });
  1841. it("should render checkboxes for printing", function (done) {
  1842. const appearanceStatesDict = new _primitives.Dict();
  1843. const normalAppearanceDict = new _primitives.Dict();
  1844. const checkedAppearanceDict = new _primitives.Dict();
  1845. const uncheckedAppearanceDict = new _primitives.Dict();
  1846. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1847. checkedStream.dict = checkedAppearanceDict;
  1848. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  1849. uncheckedStream.dict = uncheckedAppearanceDict;
  1850. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1851. checkedAppearanceDict.set("FormType", 1);
  1852. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1853. normalAppearanceDict.set("Checked", checkedStream);
  1854. normalAppearanceDict.set("Off", uncheckedStream);
  1855. appearanceStatesDict.set("N", normalAppearanceDict);
  1856. buttonWidgetDict.set("AP", appearanceStatesDict);
  1857. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1858. const xref = new _test_utils.XRefMock([{
  1859. ref: buttonWidgetRef,
  1860. data: buttonWidgetDict
  1861. }]);
  1862. const task = new _worker.WorkerTask("test print");
  1863. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1864. const annotationStorage = {};
  1865. annotationStorage[annotation.data.id] = {
  1866. value: true
  1867. };
  1868. return Promise.all([annotation, annotation.getOperatorList(partialEvaluator, task, false, annotationStorage)]);
  1869. }, done.fail).then(([annotation, opList]) => {
  1870. expect(opList.argsArray.length).toEqual(3);
  1871. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1872. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1873. return annotation;
  1874. }, done.fail).then(annotation => {
  1875. const annotationStorage = {};
  1876. annotationStorage[annotation.data.id] = {
  1877. value: false
  1878. };
  1879. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  1880. }, done.fail).then(opList => {
  1881. expect(opList.argsArray.length).toEqual(3);
  1882. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1883. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([76, 51, 26]));
  1884. done();
  1885. }, done.fail);
  1886. });
  1887. it("should render checkboxes for printing two times", function (done) {
  1888. const appearanceStatesDict = new _primitives.Dict();
  1889. const normalAppearanceDict = new _primitives.Dict();
  1890. const checkedAppearanceDict = new _primitives.Dict();
  1891. const uncheckedAppearanceDict = new _primitives.Dict();
  1892. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1893. checkedStream.dict = checkedAppearanceDict;
  1894. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  1895. uncheckedStream.dict = uncheckedAppearanceDict;
  1896. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1897. checkedAppearanceDict.set("FormType", 1);
  1898. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1899. normalAppearanceDict.set("Checked", checkedStream);
  1900. normalAppearanceDict.set("Off", uncheckedStream);
  1901. appearanceStatesDict.set("N", normalAppearanceDict);
  1902. buttonWidgetDict.set("AP", appearanceStatesDict);
  1903. buttonWidgetDict.set("AS", _primitives.Name.get("Off"));
  1904. const buttonWidgetRef = _primitives.Ref.get(1249, 0);
  1905. const xref = new _test_utils.XRefMock([{
  1906. ref: buttonWidgetRef,
  1907. data: buttonWidgetDict
  1908. }]);
  1909. const task = new _worker.WorkerTask("test print");
  1910. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1911. const annotationStorage = {};
  1912. annotationStorage[annotation.data.id] = {
  1913. value: true
  1914. };
  1915. return Promise.all([annotation, annotation.getOperatorList(partialEvaluator, task, false, annotationStorage)]);
  1916. }).then(([annotation, opList]) => {
  1917. expect(opList.argsArray.length).toEqual(3);
  1918. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1919. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1920. return annotation;
  1921. }).then(annotation => {
  1922. const annotationStorage = {};
  1923. annotationStorage[annotation.data.id] = {
  1924. value: true
  1925. };
  1926. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  1927. }).then(opList => {
  1928. expect(opList.argsArray.length).toEqual(3);
  1929. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1930. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1931. done();
  1932. }).catch(done.fail);
  1933. });
  1934. it("should render checkboxes for printing using normal appearance", function (done) {
  1935. const appearanceStatesDict = new _primitives.Dict();
  1936. const normalAppearanceDict = new _primitives.Dict();
  1937. const checkedAppearanceDict = new _primitives.Dict();
  1938. const uncheckedAppearanceDict = new _primitives.Dict();
  1939. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1940. checkedStream.dict = checkedAppearanceDict;
  1941. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  1942. uncheckedStream.dict = uncheckedAppearanceDict;
  1943. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1944. checkedAppearanceDict.set("FormType", 1);
  1945. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1946. normalAppearanceDict.set("Checked", checkedStream);
  1947. normalAppearanceDict.set("Off", uncheckedStream);
  1948. appearanceStatesDict.set("N", normalAppearanceDict);
  1949. buttonWidgetDict.set("AP", appearanceStatesDict);
  1950. buttonWidgetDict.set("AS", _primitives.Name.get("Checked"));
  1951. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1952. const xref = new _test_utils.XRefMock([{
  1953. ref: buttonWidgetRef,
  1954. data: buttonWidgetDict
  1955. }]);
  1956. const task = new _worker.WorkerTask("test print");
  1957. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1958. const annotationStorage = {};
  1959. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  1960. }).then(opList => {
  1961. expect(opList.argsArray.length).toEqual(3);
  1962. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1963. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1964. done();
  1965. }).catch(done.fail);
  1966. });
  1967. it("should save checkboxes", function (done) {
  1968. const appearanceStatesDict = new _primitives.Dict();
  1969. const normalAppearanceDict = new _primitives.Dict();
  1970. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  1971. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  1972. appearanceStatesDict.set("N", normalAppearanceDict);
  1973. buttonWidgetDict.set("AP", appearanceStatesDict);
  1974. buttonWidgetDict.set("V", _primitives.Name.get("Off"));
  1975. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  1976. const xref = new _test_utils.XRefMock([{
  1977. ref: buttonWidgetRef,
  1978. data: buttonWidgetDict
  1979. }]);
  1980. partialEvaluator.xref = xref;
  1981. const task = new _worker.WorkerTask("test save");
  1982. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  1983. const annotationStorage = {};
  1984. annotationStorage[annotation.data.id] = {
  1985. value: true
  1986. };
  1987. return Promise.all([annotation, annotation.save(partialEvaluator, task, annotationStorage)]);
  1988. }, done.fail).then(([annotation, [oldData]]) => {
  1989. oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)");
  1990. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1991. expect(oldData.data).toEqual("123 0 obj\n" + "<< /Type /Annot /Subtype /Widget /FT /Btn " + "/AP << /N << /Checked 314 0 R /Off 271 0 R>>>> " + "/V /Checked /AS /Checked /M (date)>>\nendobj\n");
  1992. return annotation;
  1993. }, done.fail).then(annotation => {
  1994. const annotationStorage = {};
  1995. annotationStorage[annotation.data.id] = {
  1996. value: false
  1997. };
  1998. return annotation.save(partialEvaluator, task, annotationStorage);
  1999. }, done.fail).then(data => {
  2000. expect(data).toEqual(null);
  2001. done();
  2002. }, done.fail);
  2003. });
  2004. it("should handle radio buttons with a field value", function (done) {
  2005. const parentDict = new _primitives.Dict();
  2006. parentDict.set("V", _primitives.Name.get("1"));
  2007. const normalAppearanceStateDict = new _primitives.Dict();
  2008. normalAppearanceStateDict.set("2", null);
  2009. const appearanceStatesDict = new _primitives.Dict();
  2010. appearanceStatesDict.set("N", normalAppearanceStateDict);
  2011. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2012. buttonWidgetDict.set("Parent", parentDict);
  2013. buttonWidgetDict.set("AP", appearanceStatesDict);
  2014. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2015. const xref = new _test_utils.XRefMock([{
  2016. ref: buttonWidgetRef,
  2017. data: buttonWidgetDict
  2018. }]);
  2019. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2020. data
  2021. }) => {
  2022. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2023. expect(data.checkBox).toEqual(false);
  2024. expect(data.radioButton).toEqual(true);
  2025. expect(data.fieldValue).toEqual("1");
  2026. expect(data.buttonValue).toEqual("2");
  2027. done();
  2028. }, done.fail);
  2029. });
  2030. it("should handle radio buttons with a field value not an ascii string", function (done) {
  2031. const parentDict = new _primitives.Dict();
  2032. parentDict.set("V", _primitives.Name.get("\x91I=\x91\xf0\x93\xe0\x97e3"));
  2033. const normalAppearanceStateDict = new _primitives.Dict();
  2034. normalAppearanceStateDict.set("\x91I=\x91\xf0\x93\xe0\x97e3", null);
  2035. const appearanceStatesDict = new _primitives.Dict();
  2036. appearanceStatesDict.set("N", normalAppearanceStateDict);
  2037. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2038. buttonWidgetDict.set("Parent", parentDict);
  2039. buttonWidgetDict.set("AP", appearanceStatesDict);
  2040. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2041. const xref = new _test_utils.XRefMock([{
  2042. ref: buttonWidgetRef,
  2043. data: buttonWidgetDict
  2044. }]);
  2045. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2046. data
  2047. }) => {
  2048. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2049. expect(data.checkBox).toEqual(false);
  2050. expect(data.radioButton).toEqual(true);
  2051. expect(data.fieldValue).toEqual("‚I=‚ðfiàŠe3");
  2052. expect(data.buttonValue).toEqual("‚I=‚ðfiàŠe3");
  2053. done();
  2054. }, done.fail);
  2055. });
  2056. it("should handle radio buttons without a field value", function (done) {
  2057. const normalAppearanceStateDict = new _primitives.Dict();
  2058. normalAppearanceStateDict.set("2", null);
  2059. const appearanceStatesDict = new _primitives.Dict();
  2060. appearanceStatesDict.set("N", normalAppearanceStateDict);
  2061. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2062. buttonWidgetDict.set("AP", appearanceStatesDict);
  2063. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2064. const xref = new _test_utils.XRefMock([{
  2065. ref: buttonWidgetRef,
  2066. data: buttonWidgetDict
  2067. }]);
  2068. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2069. data
  2070. }) => {
  2071. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2072. expect(data.checkBox).toEqual(false);
  2073. expect(data.radioButton).toEqual(true);
  2074. expect(data.fieldValue).toEqual(null);
  2075. expect(data.buttonValue).toEqual("2");
  2076. done();
  2077. }, done.fail);
  2078. });
  2079. it("should render radio buttons for printing", function (done) {
  2080. const appearanceStatesDict = new _primitives.Dict();
  2081. const normalAppearanceDict = new _primitives.Dict();
  2082. const checkedAppearanceDict = new _primitives.Dict();
  2083. const uncheckedAppearanceDict = new _primitives.Dict();
  2084. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  2085. checkedStream.dict = checkedAppearanceDict;
  2086. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  2087. uncheckedStream.dict = uncheckedAppearanceDict;
  2088. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  2089. checkedAppearanceDict.set("FormType", 1);
  2090. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  2091. normalAppearanceDict.set("Checked", checkedStream);
  2092. normalAppearanceDict.set("Off", uncheckedStream);
  2093. appearanceStatesDict.set("N", normalAppearanceDict);
  2094. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2095. buttonWidgetDict.set("AP", appearanceStatesDict);
  2096. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2097. const xref = new _test_utils.XRefMock([{
  2098. ref: buttonWidgetRef,
  2099. data: buttonWidgetDict
  2100. }]);
  2101. const task = new _worker.WorkerTask("test print");
  2102. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2103. const annotationStorage = {};
  2104. annotationStorage[annotation.data.id] = {
  2105. value: true
  2106. };
  2107. return Promise.all([annotation, annotation.getOperatorList(partialEvaluator, task, false, annotationStorage)]);
  2108. }, done.fail).then(([annotation, opList]) => {
  2109. expect(opList.argsArray.length).toEqual(3);
  2110. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  2111. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  2112. return annotation;
  2113. }, done.fail).then(annotation => {
  2114. const annotationStorage = {};
  2115. annotationStorage[annotation.data.id] = {
  2116. value: false
  2117. };
  2118. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  2119. }, done.fail).then(opList => {
  2120. expect(opList.argsArray.length).toEqual(3);
  2121. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  2122. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([76, 51, 26]));
  2123. done();
  2124. }, done.fail);
  2125. });
  2126. it("should render radio buttons for printing using normal appearance", function (done) {
  2127. const appearanceStatesDict = new _primitives.Dict();
  2128. const normalAppearanceDict = new _primitives.Dict();
  2129. const checkedAppearanceDict = new _primitives.Dict();
  2130. const uncheckedAppearanceDict = new _primitives.Dict();
  2131. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  2132. checkedStream.dict = checkedAppearanceDict;
  2133. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  2134. uncheckedStream.dict = uncheckedAppearanceDict;
  2135. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  2136. checkedAppearanceDict.set("FormType", 1);
  2137. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  2138. normalAppearanceDict.set("Checked", checkedStream);
  2139. normalAppearanceDict.set("Off", uncheckedStream);
  2140. appearanceStatesDict.set("N", normalAppearanceDict);
  2141. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2142. buttonWidgetDict.set("AP", appearanceStatesDict);
  2143. buttonWidgetDict.set("AS", _primitives.Name.get("Off"));
  2144. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2145. const xref = new _test_utils.XRefMock([{
  2146. ref: buttonWidgetRef,
  2147. data: buttonWidgetDict
  2148. }]);
  2149. const task = new _worker.WorkerTask("test print");
  2150. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2151. const annotationStorage = {};
  2152. return annotation.getOperatorList(partialEvaluator, task, false, annotationStorage);
  2153. }).then(opList => {
  2154. expect(opList.argsArray.length).toEqual(3);
  2155. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  2156. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([76, 51, 26]));
  2157. done();
  2158. }).catch(done.fail);
  2159. });
  2160. it("should save radio buttons", function (done) {
  2161. const appearanceStatesDict = new _primitives.Dict();
  2162. const normalAppearanceDict = new _primitives.Dict();
  2163. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  2164. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  2165. appearanceStatesDict.set("N", normalAppearanceDict);
  2166. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2167. buttonWidgetDict.set("AP", appearanceStatesDict);
  2168. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  2169. const parentRef = _primitives.Ref.get(456, 0);
  2170. const parentDict = new _primitives.Dict();
  2171. parentDict.set("V", _primitives.Name.get("Off"));
  2172. parentDict.set("Kids", [buttonWidgetRef]);
  2173. buttonWidgetDict.set("Parent", parentRef);
  2174. const xref = new _test_utils.XRefMock([{
  2175. ref: buttonWidgetRef,
  2176. data: buttonWidgetDict
  2177. }, {
  2178. ref: parentRef,
  2179. data: parentDict
  2180. }]);
  2181. parentDict.xref = xref;
  2182. buttonWidgetDict.xref = xref;
  2183. partialEvaluator.xref = xref;
  2184. const task = new _worker.WorkerTask("test save");
  2185. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2186. const annotationStorage = {};
  2187. annotationStorage[annotation.data.id] = {
  2188. value: true
  2189. };
  2190. return Promise.all([annotation, annotation.save(partialEvaluator, task, annotationStorage)]);
  2191. }, done.fail).then(([annotation, data]) => {
  2192. expect(data.length).toEqual(2);
  2193. const [radioData, parentData] = data;
  2194. radioData.data = radioData.data.replace(/\(D:[0-9]+\)/, "(date)");
  2195. expect(radioData.ref).toEqual(_primitives.Ref.get(123, 0));
  2196. expect(radioData.data).toEqual("123 0 obj\n" + "<< /Type /Annot /Subtype /Widget /FT /Btn /Ff 32768 " + "/AP << /N << /Checked 314 0 R /Off 271 0 R>>>> " + "/Parent 456 0 R /AS /Checked /M (date)>>\nendobj\n");
  2197. expect(parentData.ref).toEqual(_primitives.Ref.get(456, 0));
  2198. expect(parentData.data).toEqual("456 0 obj\n<< /V /Checked /Kids [123 0 R]>>\nendobj\n");
  2199. return annotation;
  2200. }, done.fail).then(annotation => {
  2201. const annotationStorage = {};
  2202. annotationStorage[annotation.data.id] = {
  2203. value: false
  2204. };
  2205. return annotation.save(partialEvaluator, task, annotationStorage);
  2206. }, done.fail).then(data => {
  2207. expect(data).toEqual(null);
  2208. done();
  2209. }, done.fail);
  2210. });
  2211. it("should save radio buttons without a field value", function (done) {
  2212. const appearanceStatesDict = new _primitives.Dict();
  2213. const normalAppearanceDict = new _primitives.Dict();
  2214. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  2215. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  2216. appearanceStatesDict.set("N", normalAppearanceDict);
  2217. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2218. buttonWidgetDict.set("AP", appearanceStatesDict);
  2219. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  2220. const parentRef = _primitives.Ref.get(456, 0);
  2221. const parentDict = new _primitives.Dict();
  2222. parentDict.set("Kids", [buttonWidgetRef]);
  2223. buttonWidgetDict.set("Parent", parentRef);
  2224. const xref = new _test_utils.XRefMock([{
  2225. ref: buttonWidgetRef,
  2226. data: buttonWidgetDict
  2227. }, {
  2228. ref: parentRef,
  2229. data: parentDict
  2230. }]);
  2231. parentDict.xref = xref;
  2232. buttonWidgetDict.xref = xref;
  2233. partialEvaluator.xref = xref;
  2234. const task = new _worker.WorkerTask("test save");
  2235. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2236. const annotationStorage = {};
  2237. annotationStorage[annotation.data.id] = {
  2238. value: true
  2239. };
  2240. return Promise.all([annotation, annotation.save(partialEvaluator, task, annotationStorage)]);
  2241. }).then(([annotation, data]) => {
  2242. expect(data.length).toEqual(2);
  2243. const [radioData, parentData] = data;
  2244. radioData.data = radioData.data.replace(/\(D:[0-9]+\)/, "(date)");
  2245. expect(radioData.ref).toEqual(_primitives.Ref.get(123, 0));
  2246. expect(radioData.data).toEqual("123 0 obj\n" + "<< /Type /Annot /Subtype /Widget /FT /Btn /Ff 32768 " + "/AP << /N << /Checked 314 0 R /Off 271 0 R>>>> " + "/Parent 456 0 R /AS /Checked /M (date)>>\nendobj\n");
  2247. expect(parentData.ref).toEqual(_primitives.Ref.get(456, 0));
  2248. expect(parentData.data).toEqual("456 0 obj\n<< /Kids [123 0 R] /V /Checked>>\nendobj\n");
  2249. done();
  2250. }).catch(done.fail);
  2251. });
  2252. it("should save nothing", function (done) {
  2253. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2254. const xref = new _test_utils.XRefMock([{
  2255. ref: buttonWidgetRef,
  2256. data: buttonWidgetDict
  2257. }]);
  2258. const task = new _worker.WorkerTask("test save");
  2259. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2260. return annotation.save(partialEvaluator, task, {});
  2261. }).then(data => {
  2262. expect(data).toEqual(null);
  2263. done();
  2264. }).catch(done.fail);
  2265. });
  2266. it("should handle push buttons", function (done) {
  2267. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2268. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2269. const actionDict = new _primitives.Dict();
  2270. actionDict.set("S", _primitives.Name.get("JavaScript"));
  2271. actionDict.set("JS", "do_something();");
  2272. buttonWidgetDict.set("A", actionDict);
  2273. const xref = new _test_utils.XRefMock([{
  2274. ref: buttonWidgetRef,
  2275. data: buttonWidgetDict
  2276. }]);
  2277. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2278. data
  2279. }) => {
  2280. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2281. expect(data.pushButton).toEqual(true);
  2282. expect(data.actions.Action).toEqual(["do_something();"]);
  2283. done();
  2284. }, done.fail);
  2285. });
  2286. it("should handle push buttons that act as a tooltip only", function (done) {
  2287. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2288. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2289. buttonWidgetDict.set("TU", "An alternative text");
  2290. const xref = new _test_utils.XRefMock([{
  2291. ref: buttonWidgetRef,
  2292. data: buttonWidgetDict
  2293. }]);
  2294. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2295. data
  2296. }) => {
  2297. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2298. expect(data.pushButton).toEqual(true);
  2299. expect(data.alternativeText).toEqual("An alternative text");
  2300. done();
  2301. }, done.fail);
  2302. });
  2303. it("should handle URL in A dict in push buttons", function (done) {
  2304. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2305. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2306. const actionDict = new _primitives.Dict();
  2307. actionDict.set("S", _primitives.Name.get("JavaScript"));
  2308. actionDict.set("JS", "app.launchURL('https://developer.mozilla.org/en-US/', true)");
  2309. buttonWidgetDict.set("A", actionDict);
  2310. const xref = new _test_utils.XRefMock([{
  2311. ref: buttonWidgetRef,
  2312. data: buttonWidgetDict
  2313. }]);
  2314. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2315. data
  2316. }) => {
  2317. expect(data.url).toEqual("https://developer.mozilla.org/en-US/");
  2318. done();
  2319. }, done.fail);
  2320. });
  2321. it("should handle URL in AA dict in push buttons", function (done) {
  2322. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2323. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2324. const dDict = new _primitives.Dict();
  2325. dDict.set("S", _primitives.Name.get("JavaScript"));
  2326. dDict.set("JS", "app.launchURL('https://developer.mozilla.org/en-US/', true)");
  2327. const actionDict = new _primitives.Dict();
  2328. actionDict.set("D", dDict);
  2329. buttonWidgetDict.set("AA", actionDict);
  2330. const xref = new _test_utils.XRefMock([{
  2331. ref: buttonWidgetRef,
  2332. data: buttonWidgetDict
  2333. }]);
  2334. _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2335. data
  2336. }) => {
  2337. expect(data.url).toEqual("https://developer.mozilla.org/en-US/");
  2338. done();
  2339. }, done.fail);
  2340. });
  2341. });
  2342. describe("ChoiceWidgetAnnotation", function () {
  2343. let choiceWidgetDict, fontRefObj;
  2344. beforeEach(function (done) {
  2345. choiceWidgetDict = new _primitives.Dict();
  2346. choiceWidgetDict.set("Type", _primitives.Name.get("Annot"));
  2347. choiceWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  2348. choiceWidgetDict.set("FT", _primitives.Name.get("Ch"));
  2349. const helvDict = new _primitives.Dict();
  2350. helvDict.set("BaseFont", _primitives.Name.get("Helvetica"));
  2351. helvDict.set("Type", _primitives.Name.get("Font"));
  2352. helvDict.set("Subtype", _primitives.Name.get("Type1"));
  2353. const fontRef = _primitives.Ref.get(314, 0);
  2354. fontRefObj = {
  2355. ref: fontRef,
  2356. data: helvDict
  2357. };
  2358. const resourceDict = new _primitives.Dict();
  2359. const fontDict = new _primitives.Dict();
  2360. fontDict.set("Helv", fontRef);
  2361. resourceDict.set("Font", fontDict);
  2362. choiceWidgetDict.set("DA", "/Helv 5 Tf");
  2363. choiceWidgetDict.set("DR", resourceDict);
  2364. choiceWidgetDict.set("Rect", [0, 0, 32, 10]);
  2365. done();
  2366. });
  2367. afterEach(function () {
  2368. choiceWidgetDict = fontRefObj = null;
  2369. });
  2370. it("should handle missing option arrays", function (done) {
  2371. const choiceWidgetRef = _primitives.Ref.get(122, 0);
  2372. const xref = new _test_utils.XRefMock([{
  2373. ref: choiceWidgetRef,
  2374. data: choiceWidgetDict
  2375. }]);
  2376. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2377. data
  2378. }) => {
  2379. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2380. expect(data.options).toEqual([]);
  2381. done();
  2382. }, done.fail);
  2383. });
  2384. it("should handle option arrays with array elements", function (done) {
  2385. const optionBarRef = _primitives.Ref.get(20, 0);
  2386. const optionBarStr = "Bar";
  2387. const optionOneRef = _primitives.Ref.get(10, 0);
  2388. const optionOneArr = ["bar_export", optionBarRef];
  2389. const options = [["foo_export", "Foo"], optionOneRef];
  2390. const expected = [{
  2391. exportValue: "foo_export",
  2392. displayValue: "Foo"
  2393. }, {
  2394. exportValue: "bar_export",
  2395. displayValue: "Bar"
  2396. }];
  2397. choiceWidgetDict.set("Opt", options);
  2398. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2399. const xref = new _test_utils.XRefMock([{
  2400. ref: choiceWidgetRef,
  2401. data: choiceWidgetDict
  2402. }, {
  2403. ref: optionBarRef,
  2404. data: optionBarStr
  2405. }, {
  2406. ref: optionOneRef,
  2407. data: optionOneArr
  2408. }]);
  2409. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2410. data
  2411. }) => {
  2412. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2413. expect(data.options).toEqual(expected);
  2414. done();
  2415. }, done.fail);
  2416. });
  2417. it("should handle option arrays with string elements", function (done) {
  2418. const optionBarRef = _primitives.Ref.get(10, 0);
  2419. const optionBarStr = "Bar";
  2420. const options = ["Foo", optionBarRef];
  2421. const expected = [{
  2422. exportValue: "Foo",
  2423. displayValue: "Foo"
  2424. }, {
  2425. exportValue: "Bar",
  2426. displayValue: "Bar"
  2427. }];
  2428. choiceWidgetDict.set("Opt", options);
  2429. const choiceWidgetRef = _primitives.Ref.get(981, 0);
  2430. const xref = new _test_utils.XRefMock([{
  2431. ref: choiceWidgetRef,
  2432. data: choiceWidgetDict
  2433. }, {
  2434. ref: optionBarRef,
  2435. data: optionBarStr
  2436. }]);
  2437. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2438. data
  2439. }) => {
  2440. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2441. expect(data.options).toEqual(expected);
  2442. done();
  2443. }, done.fail);
  2444. });
  2445. it("should handle inherited option arrays (issue 8094)", function (done) {
  2446. const options = [["Value1", "Description1"], ["Value2", "Description2"]];
  2447. const expected = [{
  2448. exportValue: "Value1",
  2449. displayValue: "Description1"
  2450. }, {
  2451. exportValue: "Value2",
  2452. displayValue: "Description2"
  2453. }];
  2454. const parentDict = new _primitives.Dict();
  2455. parentDict.set("Opt", options);
  2456. choiceWidgetDict.set("Parent", parentDict);
  2457. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2458. const xref = new _test_utils.XRefMock([{
  2459. ref: choiceWidgetRef,
  2460. data: choiceWidgetDict
  2461. }]);
  2462. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2463. data
  2464. }) => {
  2465. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2466. expect(data.options).toEqual(expected);
  2467. done();
  2468. }, done.fail);
  2469. });
  2470. it("should decode form values", function (done) {
  2471. const encodedString = "\xFE\xFF\x00F\x00o\x00o";
  2472. const decodedString = "Foo";
  2473. choiceWidgetDict.set("Opt", [encodedString]);
  2474. choiceWidgetDict.set("V", encodedString);
  2475. choiceWidgetDict.set("DV", _primitives.Name.get("foo"));
  2476. const choiceWidgetRef = _primitives.Ref.get(984, 0);
  2477. const xref = new _test_utils.XRefMock([{
  2478. ref: choiceWidgetRef,
  2479. data: choiceWidgetDict
  2480. }]);
  2481. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2482. data
  2483. }) => {
  2484. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2485. expect(data.fieldValue).toEqual([decodedString]);
  2486. expect(data.defaultFieldValue).toEqual("foo");
  2487. expect(data.options).toEqual([{
  2488. exportValue: decodedString,
  2489. displayValue: decodedString
  2490. }]);
  2491. done();
  2492. }, done.fail);
  2493. });
  2494. it("should convert the field value to an array", function (done) {
  2495. const inputs = [null, "Foo", ["Foo", "Bar"]];
  2496. const outputs = [[], ["Foo"], ["Foo", "Bar"]];
  2497. let promise = Promise.resolve();
  2498. for (let i = 0, ii = inputs.length; i < ii; i++) {
  2499. promise = promise.then(() => {
  2500. choiceWidgetDict.set("V", inputs[i]);
  2501. const choiceWidgetRef = _primitives.Ref.get(968, 0);
  2502. const xref = new _test_utils.XRefMock([{
  2503. ref: choiceWidgetRef,
  2504. data: choiceWidgetDict
  2505. }]);
  2506. return _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2507. data
  2508. }) => {
  2509. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2510. expect(data.fieldValue).toEqual(outputs[i]);
  2511. });
  2512. });
  2513. }
  2514. promise.then(done, done.fail);
  2515. });
  2516. it("should handle unknown flags", function (done) {
  2517. const choiceWidgetRef = _primitives.Ref.get(166, 0);
  2518. const xref = new _test_utils.XRefMock([{
  2519. ref: choiceWidgetRef,
  2520. data: choiceWidgetDict
  2521. }]);
  2522. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2523. data
  2524. }) => {
  2525. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2526. expect(data.readOnly).toEqual(false);
  2527. expect(data.hidden).toEqual(false);
  2528. expect(data.combo).toEqual(false);
  2529. expect(data.multiSelect).toEqual(false);
  2530. done();
  2531. }, done.fail);
  2532. });
  2533. it("should not set invalid flags", function (done) {
  2534. choiceWidgetDict.set("Ff", "readonly");
  2535. const choiceWidgetRef = _primitives.Ref.get(165, 0);
  2536. const xref = new _test_utils.XRefMock([{
  2537. ref: choiceWidgetRef,
  2538. data: choiceWidgetDict
  2539. }]);
  2540. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2541. data
  2542. }) => {
  2543. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2544. expect(data.readOnly).toEqual(false);
  2545. expect(data.hidden).toEqual(false);
  2546. expect(data.combo).toEqual(false);
  2547. expect(data.multiSelect).toEqual(false);
  2548. done();
  2549. }, done.fail);
  2550. });
  2551. it("should set valid flags", function (done) {
  2552. choiceWidgetDict.set("Ff", _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.COMBO + _util.AnnotationFieldFlag.MULTISELECT);
  2553. const choiceWidgetRef = _primitives.Ref.get(512, 0);
  2554. const xref = new _test_utils.XRefMock([{
  2555. ref: choiceWidgetRef,
  2556. data: choiceWidgetDict
  2557. }]);
  2558. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2559. data
  2560. }) => {
  2561. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2562. expect(data.readOnly).toEqual(true);
  2563. expect(data.hidden).toEqual(false);
  2564. expect(data.combo).toEqual(true);
  2565. expect(data.multiSelect).toEqual(true);
  2566. done();
  2567. }, done.fail);
  2568. });
  2569. it("should render choice for printing", function (done) {
  2570. const choiceWidgetRef = _primitives.Ref.get(271, 0);
  2571. const xref = new _test_utils.XRefMock([{
  2572. ref: choiceWidgetRef,
  2573. data: choiceWidgetDict
  2574. }, fontRefObj]);
  2575. const task = new _worker.WorkerTask("test print");
  2576. partialEvaluator.xref = xref;
  2577. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2578. const id = annotation.data.id;
  2579. const annotationStorage = {};
  2580. annotationStorage[id] = {
  2581. value: "a value"
  2582. };
  2583. return annotation._getAppearance(partialEvaluator, task, annotationStorage);
  2584. }, done.fail).then(appearance => {
  2585. expect(appearance).toEqual("/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm" + " 2.00 2.00 Td (a value) Tj ET Q EMC");
  2586. done();
  2587. }, done.fail);
  2588. });
  2589. it("should save choice", function (done) {
  2590. choiceWidgetDict.set("Opt", ["A", "B", "C"]);
  2591. choiceWidgetDict.set("V", "A");
  2592. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2593. const xref = new _test_utils.XRefMock([{
  2594. ref: choiceWidgetRef,
  2595. data: choiceWidgetDict
  2596. }]);
  2597. partialEvaluator.xref = xref;
  2598. const task = new _worker.WorkerTask("test save");
  2599. _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(annotation => {
  2600. const annotationStorage = {};
  2601. annotationStorage[annotation.data.id] = {
  2602. value: "C"
  2603. };
  2604. return annotation.save(partialEvaluator, task, annotationStorage);
  2605. }, done.fail).then(data => {
  2606. expect(data.length).toEqual(2);
  2607. const [oldData, newData] = data;
  2608. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  2609. expect(newData.ref).toEqual(_primitives.Ref.get(1, 0));
  2610. oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)");
  2611. expect(oldData.data).toEqual("123 0 obj\n" + "<< /Type /Annot /Subtype /Widget /FT /Ch /DA (/Helv 5 Tf) /DR " + "<< /Font << /Helv 314 0 R>>>> " + "/Rect [0 0 32 10] /Opt [(A) (B) (C)] /V (C) " + "/AP << /N 1 0 R>> /M (date)>>\nendobj\n");
  2612. expect(newData.data).toEqual("1 0 obj\n" + "<< /Length 67 /Subtype /Form /Resources << /Font << /Helv 314 0 R>>>> " + "/BBox [0 0 32 10]>> stream\n" + "/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm 2.00 2.00 Td (C) Tj ET Q EMC\n" + "endstream\nendobj\n");
  2613. done();
  2614. }, done.fail);
  2615. });
  2616. });
  2617. describe("LineAnnotation", function () {
  2618. it("should set the line coordinates", function (done) {
  2619. const lineDict = new _primitives.Dict();
  2620. lineDict.set("Type", _primitives.Name.get("Annot"));
  2621. lineDict.set("Subtype", _primitives.Name.get("Line"));
  2622. lineDict.set("L", [1, 2, 3, 4]);
  2623. const lineRef = _primitives.Ref.get(122, 0);
  2624. const xref = new _test_utils.XRefMock([{
  2625. ref: lineRef,
  2626. data: lineDict
  2627. }]);
  2628. _annotation.AnnotationFactory.create(xref, lineRef, pdfManagerMock, idFactoryMock).then(({
  2629. data
  2630. }) => {
  2631. expect(data.annotationType).toEqual(_util.AnnotationType.LINE);
  2632. expect(data.lineCoordinates).toEqual([1, 2, 3, 4]);
  2633. done();
  2634. }, done.fail);
  2635. });
  2636. });
  2637. describe("FileAttachmentAnnotation", function () {
  2638. it("should correctly parse a file attachment", function (done) {
  2639. const fileStream = new _stream.StringStream("<<\n" + "/Type /EmbeddedFile\n" + "/Subtype /text#2Fplain\n" + ">>\n" + "stream\n" + "Test attachment" + "endstream\n");
  2640. const parser = new _parser.Parser({
  2641. lexer: new _parser.Lexer(fileStream),
  2642. xref: null,
  2643. allowStreams: true
  2644. });
  2645. const fileStreamRef = _primitives.Ref.get(18, 0);
  2646. const fileStreamDict = parser.getObj();
  2647. const embeddedFileDict = new _primitives.Dict();
  2648. embeddedFileDict.set("F", fileStreamRef);
  2649. const fileSpecRef = _primitives.Ref.get(19, 0);
  2650. const fileSpecDict = new _primitives.Dict();
  2651. fileSpecDict.set("Type", _primitives.Name.get("Filespec"));
  2652. fileSpecDict.set("Desc", "");
  2653. fileSpecDict.set("EF", embeddedFileDict);
  2654. fileSpecDict.set("UF", "Test.txt");
  2655. const fileAttachmentRef = _primitives.Ref.get(20, 0);
  2656. const fileAttachmentDict = new _primitives.Dict();
  2657. fileAttachmentDict.set("Type", _primitives.Name.get("Annot"));
  2658. fileAttachmentDict.set("Subtype", _primitives.Name.get("FileAttachment"));
  2659. fileAttachmentDict.set("FS", fileSpecRef);
  2660. fileAttachmentDict.set("T", "Topic");
  2661. fileAttachmentDict.set("Contents", "Test.txt");
  2662. const xref = new _test_utils.XRefMock([{
  2663. ref: fileStreamRef,
  2664. data: fileStreamDict
  2665. }, {
  2666. ref: fileSpecRef,
  2667. data: fileSpecDict
  2668. }, {
  2669. ref: fileAttachmentRef,
  2670. data: fileAttachmentDict
  2671. }]);
  2672. embeddedFileDict.assignXref(xref);
  2673. fileSpecDict.assignXref(xref);
  2674. fileAttachmentDict.assignXref(xref);
  2675. _annotation.AnnotationFactory.create(xref, fileAttachmentRef, pdfManagerMock, idFactoryMock).then(({
  2676. data
  2677. }) => {
  2678. expect(data.annotationType).toEqual(_util.AnnotationType.FILEATTACHMENT);
  2679. expect(data.file.filename).toEqual("Test.txt");
  2680. expect(data.file.content).toEqual((0, _util.stringToBytes)("Test attachment"));
  2681. done();
  2682. }, done.fail);
  2683. });
  2684. });
  2685. describe("PopupAnnotation", function () {
  2686. it("should inherit properties from its parent", function (done) {
  2687. const parentDict = new _primitives.Dict();
  2688. parentDict.set("Type", _primitives.Name.get("Annot"));
  2689. parentDict.set("Subtype", _primitives.Name.get("Text"));
  2690. parentDict.set("M", "D:20190423");
  2691. parentDict.set("C", [0, 0, 1]);
  2692. const popupDict = new _primitives.Dict();
  2693. popupDict.set("Type", _primitives.Name.get("Annot"));
  2694. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2695. popupDict.set("Parent", parentDict);
  2696. const popupRef = _primitives.Ref.get(13, 0);
  2697. const xref = new _test_utils.XRefMock([{
  2698. ref: popupRef,
  2699. data: popupDict
  2700. }]);
  2701. _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock).then(({
  2702. data,
  2703. viewable
  2704. }) => {
  2705. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  2706. expect(data.modificationDate).toEqual("D:20190423");
  2707. expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  2708. done();
  2709. }, done.fail);
  2710. });
  2711. it("should handle missing parent properties", function (done) {
  2712. const parentDict = new _primitives.Dict();
  2713. parentDict.set("Type", _primitives.Name.get("Annot"));
  2714. parentDict.set("Subtype", _primitives.Name.get("Text"));
  2715. const popupDict = new _primitives.Dict();
  2716. popupDict.set("Type", _primitives.Name.get("Annot"));
  2717. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2718. popupDict.set("Parent", parentDict);
  2719. const popupRef = _primitives.Ref.get(13, 0);
  2720. const xref = new _test_utils.XRefMock([{
  2721. ref: popupRef,
  2722. data: popupDict
  2723. }]);
  2724. _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock).then(({
  2725. data,
  2726. viewable
  2727. }) => {
  2728. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  2729. expect(data.modificationDate).toEqual(null);
  2730. expect(data.color).toEqual(null);
  2731. done();
  2732. }, done.fail);
  2733. });
  2734. it("should inherit the parent flags when the Popup is not viewable, " + "but the parent is (PR 7352)", function (done) {
  2735. const parentDict = new _primitives.Dict();
  2736. parentDict.set("Type", _primitives.Name.get("Annot"));
  2737. parentDict.set("Subtype", _primitives.Name.get("Text"));
  2738. parentDict.set("F", 28);
  2739. const popupDict = new _primitives.Dict();
  2740. popupDict.set("Type", _primitives.Name.get("Annot"));
  2741. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2742. popupDict.set("F", 25);
  2743. popupDict.set("Parent", parentDict);
  2744. const popupRef = _primitives.Ref.get(13, 0);
  2745. const xref = new _test_utils.XRefMock([{
  2746. ref: popupRef,
  2747. data: popupDict
  2748. }]);
  2749. _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock).then(({
  2750. data,
  2751. viewable
  2752. }) => {
  2753. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  2754. expect(data.annotationFlags).toEqual(25);
  2755. expect(viewable).toEqual(true);
  2756. done();
  2757. }, done.fail);
  2758. });
  2759. it("should correctly inherit Contents from group-master annotation " + "if parent has ReplyType == Group", function (done) {
  2760. const annotationRef = _primitives.Ref.get(819, 0);
  2761. const annotationDict = new _primitives.Dict();
  2762. annotationDict.set("Type", _primitives.Name.get("Annot"));
  2763. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  2764. annotationDict.set("T", "Correct Title");
  2765. annotationDict.set("Contents", "Correct Text");
  2766. annotationDict.set("M", "D:20190423");
  2767. annotationDict.set("C", [0, 0, 1]);
  2768. const replyRef = _primitives.Ref.get(820, 0);
  2769. const replyDict = new _primitives.Dict();
  2770. replyDict.set("Type", _primitives.Name.get("Annot"));
  2771. replyDict.set("Subtype", _primitives.Name.get("Text"));
  2772. replyDict.set("IRT", annotationRef);
  2773. replyDict.set("RT", _primitives.Name.get("Group"));
  2774. replyDict.set("T", "Reply Title");
  2775. replyDict.set("Contents", "Reply Text");
  2776. replyDict.set("M", "D:20190523");
  2777. replyDict.set("C", [0.4]);
  2778. const popupRef = _primitives.Ref.get(821, 0);
  2779. const popupDict = new _primitives.Dict();
  2780. popupDict.set("Type", _primitives.Name.get("Annot"));
  2781. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2782. popupDict.set("T", "Wrong Title");
  2783. popupDict.set("Contents", "Wrong Text");
  2784. popupDict.set("Parent", replyRef);
  2785. popupDict.set("M", "D:20190623");
  2786. popupDict.set("C", [0.8]);
  2787. replyDict.set("Popup", popupRef);
  2788. const xref = new _test_utils.XRefMock([{
  2789. ref: annotationRef,
  2790. data: annotationDict
  2791. }, {
  2792. ref: replyRef,
  2793. data: replyDict
  2794. }, {
  2795. ref: popupRef,
  2796. data: popupDict
  2797. }]);
  2798. annotationDict.assignXref(xref);
  2799. popupDict.assignXref(xref);
  2800. replyDict.assignXref(xref);
  2801. _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock).then(({
  2802. data
  2803. }) => {
  2804. expect(data.title).toEqual("Correct Title");
  2805. expect(data.contents).toEqual("Correct Text");
  2806. expect(data.modificationDate).toEqual("D:20190423");
  2807. expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  2808. done();
  2809. }, done.fail);
  2810. });
  2811. });
  2812. describe("InkAnnotation", function () {
  2813. it("should handle a single ink list", function (done) {
  2814. const inkDict = new _primitives.Dict();
  2815. inkDict.set("Type", _primitives.Name.get("Annot"));
  2816. inkDict.set("Subtype", _primitives.Name.get("Ink"));
  2817. inkDict.set("InkList", [[1, 1, 1, 2, 2, 2, 3, 3]]);
  2818. const inkRef = _primitives.Ref.get(142, 0);
  2819. const xref = new _test_utils.XRefMock([{
  2820. ref: inkRef,
  2821. data: inkDict
  2822. }]);
  2823. _annotation.AnnotationFactory.create(xref, inkRef, pdfManagerMock, idFactoryMock).then(({
  2824. data
  2825. }) => {
  2826. expect(data.annotationType).toEqual(_util.AnnotationType.INK);
  2827. expect(data.inkLists.length).toEqual(1);
  2828. expect(data.inkLists[0]).toEqual([{
  2829. x: 1,
  2830. y: 1
  2831. }, {
  2832. x: 1,
  2833. y: 2
  2834. }, {
  2835. x: 2,
  2836. y: 2
  2837. }, {
  2838. x: 3,
  2839. y: 3
  2840. }]);
  2841. done();
  2842. }, done.fail);
  2843. });
  2844. it("should handle multiple ink lists", function (done) {
  2845. const inkDict = new _primitives.Dict();
  2846. inkDict.set("Type", _primitives.Name.get("Annot"));
  2847. inkDict.set("Subtype", _primitives.Name.get("Ink"));
  2848. inkDict.set("InkList", [[1, 1, 1, 2], [3, 3, 4, 5]]);
  2849. const inkRef = _primitives.Ref.get(143, 0);
  2850. const xref = new _test_utils.XRefMock([{
  2851. ref: inkRef,
  2852. data: inkDict
  2853. }]);
  2854. _annotation.AnnotationFactory.create(xref, inkRef, pdfManagerMock, idFactoryMock).then(({
  2855. data
  2856. }) => {
  2857. expect(data.annotationType).toEqual(_util.AnnotationType.INK);
  2858. expect(data.inkLists.length).toEqual(2);
  2859. expect(data.inkLists[0]).toEqual([{
  2860. x: 1,
  2861. y: 1
  2862. }, {
  2863. x: 1,
  2864. y: 2
  2865. }]);
  2866. expect(data.inkLists[1]).toEqual([{
  2867. x: 3,
  2868. y: 3
  2869. }, {
  2870. x: 4,
  2871. y: 5
  2872. }]);
  2873. done();
  2874. }, done.fail);
  2875. });
  2876. });
  2877. describe("HightlightAnnotation", function () {
  2878. it("should set quadpoints to null if not defined", function (done) {
  2879. const highlightDict = new _primitives.Dict();
  2880. highlightDict.set("Type", _primitives.Name.get("Annot"));
  2881. highlightDict.set("Subtype", _primitives.Name.get("Highlight"));
  2882. const highlightRef = _primitives.Ref.get(121, 0);
  2883. const xref = new _test_utils.XRefMock([{
  2884. ref: highlightRef,
  2885. data: highlightDict
  2886. }]);
  2887. _annotation.AnnotationFactory.create(xref, highlightRef, pdfManagerMock, idFactoryMock).then(({
  2888. data
  2889. }) => {
  2890. expect(data.annotationType).toEqual(_util.AnnotationType.HIGHLIGHT);
  2891. expect(data.quadPoints).toEqual(null);
  2892. done();
  2893. }, done.fail);
  2894. });
  2895. it("should set quadpoints if defined", function (done) {
  2896. const highlightDict = new _primitives.Dict();
  2897. highlightDict.set("Type", _primitives.Name.get("Annot"));
  2898. highlightDict.set("Subtype", _primitives.Name.get("Highlight"));
  2899. highlightDict.set("Rect", [10, 10, 20, 20]);
  2900. highlightDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  2901. const highlightRef = _primitives.Ref.get(121, 0);
  2902. const xref = new _test_utils.XRefMock([{
  2903. ref: highlightRef,
  2904. data: highlightDict
  2905. }]);
  2906. _annotation.AnnotationFactory.create(xref, highlightRef, pdfManagerMock, idFactoryMock).then(({
  2907. data
  2908. }) => {
  2909. expect(data.annotationType).toEqual(_util.AnnotationType.HIGHLIGHT);
  2910. expect(data.quadPoints).toEqual([[{
  2911. x: 10,
  2912. y: 20
  2913. }, {
  2914. x: 20,
  2915. y: 20
  2916. }, {
  2917. x: 10,
  2918. y: 10
  2919. }, {
  2920. x: 20,
  2921. y: 10
  2922. }]]);
  2923. done();
  2924. }, done.fail);
  2925. });
  2926. it("should set quadpoints to null when empty", function (done) {
  2927. const highlightDict = new _primitives.Dict();
  2928. highlightDict.set("Type", _primitives.Name.get("Annot"));
  2929. highlightDict.set("Subtype", _primitives.Name.get("Highlight"));
  2930. highlightDict.set("Rect", [10, 10, 20, 20]);
  2931. highlightDict.set("QuadPoints", []);
  2932. const highlightRef = _primitives.Ref.get(121, 0);
  2933. const xref = new _test_utils.XRefMock([{
  2934. ref: highlightRef,
  2935. data: highlightDict
  2936. }]);
  2937. _annotation.AnnotationFactory.create(xref, highlightRef, pdfManagerMock, idFactoryMock).then(({
  2938. data
  2939. }) => {
  2940. expect(data.annotationType).toEqual(_util.AnnotationType.HIGHLIGHT);
  2941. expect(data.quadPoints).toEqual(null);
  2942. done();
  2943. }, done.fail);
  2944. });
  2945. });
  2946. describe("UnderlineAnnotation", function () {
  2947. it("should set quadpoints to null if not defined", function (done) {
  2948. const underlineDict = new _primitives.Dict();
  2949. underlineDict.set("Type", _primitives.Name.get("Annot"));
  2950. underlineDict.set("Subtype", _primitives.Name.get("Underline"));
  2951. const underlineRef = _primitives.Ref.get(121, 0);
  2952. const xref = new _test_utils.XRefMock([{
  2953. ref: underlineRef,
  2954. data: underlineDict
  2955. }]);
  2956. _annotation.AnnotationFactory.create(xref, underlineRef, pdfManagerMock, idFactoryMock).then(({
  2957. data
  2958. }) => {
  2959. expect(data.annotationType).toEqual(_util.AnnotationType.UNDERLINE);
  2960. expect(data.quadPoints).toEqual(null);
  2961. done();
  2962. }, done.fail);
  2963. });
  2964. it("should set quadpoints if defined", function (done) {
  2965. const underlineDict = new _primitives.Dict();
  2966. underlineDict.set("Type", _primitives.Name.get("Annot"));
  2967. underlineDict.set("Subtype", _primitives.Name.get("Underline"));
  2968. underlineDict.set("Rect", [10, 10, 20, 20]);
  2969. underlineDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  2970. const underlineRef = _primitives.Ref.get(121, 0);
  2971. const xref = new _test_utils.XRefMock([{
  2972. ref: underlineRef,
  2973. data: underlineDict
  2974. }]);
  2975. _annotation.AnnotationFactory.create(xref, underlineRef, pdfManagerMock, idFactoryMock).then(({
  2976. data
  2977. }) => {
  2978. expect(data.annotationType).toEqual(_util.AnnotationType.UNDERLINE);
  2979. expect(data.quadPoints).toEqual([[{
  2980. x: 10,
  2981. y: 20
  2982. }, {
  2983. x: 20,
  2984. y: 20
  2985. }, {
  2986. x: 10,
  2987. y: 10
  2988. }, {
  2989. x: 20,
  2990. y: 10
  2991. }]]);
  2992. done();
  2993. }, done.fail);
  2994. });
  2995. });
  2996. describe("SquigglyAnnotation", function () {
  2997. it("should set quadpoints to null if not defined", function (done) {
  2998. const squigglyDict = new _primitives.Dict();
  2999. squigglyDict.set("Type", _primitives.Name.get("Annot"));
  3000. squigglyDict.set("Subtype", _primitives.Name.get("Squiggly"));
  3001. const squigglyRef = _primitives.Ref.get(121, 0);
  3002. const xref = new _test_utils.XRefMock([{
  3003. ref: squigglyRef,
  3004. data: squigglyDict
  3005. }]);
  3006. _annotation.AnnotationFactory.create(xref, squigglyRef, pdfManagerMock, idFactoryMock).then(({
  3007. data
  3008. }) => {
  3009. expect(data.annotationType).toEqual(_util.AnnotationType.SQUIGGLY);
  3010. expect(data.quadPoints).toEqual(null);
  3011. done();
  3012. }, done.fail);
  3013. });
  3014. it("should set quadpoints if defined", function (done) {
  3015. const squigglyDict = new _primitives.Dict();
  3016. squigglyDict.set("Type", _primitives.Name.get("Annot"));
  3017. squigglyDict.set("Subtype", _primitives.Name.get("Squiggly"));
  3018. squigglyDict.set("Rect", [10, 10, 20, 20]);
  3019. squigglyDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  3020. const squigglyRef = _primitives.Ref.get(121, 0);
  3021. const xref = new _test_utils.XRefMock([{
  3022. ref: squigglyRef,
  3023. data: squigglyDict
  3024. }]);
  3025. _annotation.AnnotationFactory.create(xref, squigglyRef, pdfManagerMock, idFactoryMock).then(({
  3026. data
  3027. }) => {
  3028. expect(data.annotationType).toEqual(_util.AnnotationType.SQUIGGLY);
  3029. expect(data.quadPoints).toEqual([[{
  3030. x: 10,
  3031. y: 20
  3032. }, {
  3033. x: 20,
  3034. y: 20
  3035. }, {
  3036. x: 10,
  3037. y: 10
  3038. }, {
  3039. x: 20,
  3040. y: 10
  3041. }]]);
  3042. done();
  3043. }, done.fail);
  3044. });
  3045. });
  3046. describe("StrikeOutAnnotation", function () {
  3047. it("should set quadpoints to null if not defined", function (done) {
  3048. const strikeOutDict = new _primitives.Dict();
  3049. strikeOutDict.set("Type", _primitives.Name.get("Annot"));
  3050. strikeOutDict.set("Subtype", _primitives.Name.get("StrikeOut"));
  3051. const strikeOutRef = _primitives.Ref.get(121, 0);
  3052. const xref = new _test_utils.XRefMock([{
  3053. ref: strikeOutRef,
  3054. data: strikeOutDict
  3055. }]);
  3056. _annotation.AnnotationFactory.create(xref, strikeOutRef, pdfManagerMock, idFactoryMock).then(({
  3057. data
  3058. }) => {
  3059. expect(data.annotationType).toEqual(_util.AnnotationType.STRIKEOUT);
  3060. expect(data.quadPoints).toEqual(null);
  3061. done();
  3062. }, done.fail);
  3063. });
  3064. it("should set quadpoints if defined", function (done) {
  3065. const strikeOutDict = new _primitives.Dict();
  3066. strikeOutDict.set("Type", _primitives.Name.get("Annot"));
  3067. strikeOutDict.set("Subtype", _primitives.Name.get("StrikeOut"));
  3068. strikeOutDict.set("Rect", [10, 10, 20, 20]);
  3069. strikeOutDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  3070. const strikeOutRef = _primitives.Ref.get(121, 0);
  3071. const xref = new _test_utils.XRefMock([{
  3072. ref: strikeOutRef,
  3073. data: strikeOutDict
  3074. }]);
  3075. _annotation.AnnotationFactory.create(xref, strikeOutRef, pdfManagerMock, idFactoryMock).then(({
  3076. data
  3077. }) => {
  3078. expect(data.annotationType).toEqual(_util.AnnotationType.STRIKEOUT);
  3079. expect(data.quadPoints).toEqual([[{
  3080. x: 10,
  3081. y: 20
  3082. }, {
  3083. x: 20,
  3084. y: 20
  3085. }, {
  3086. x: 10,
  3087. y: 10
  3088. }, {
  3089. x: 20,
  3090. y: 10
  3091. }]]);
  3092. done();
  3093. }, done.fail);
  3094. });
  3095. });
  3096. });