annotation_spec.js 135 KB

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