2
0

annotation_spec.js 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429
  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 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 2 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 {
  1356. opList
  1357. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  1358. expect(opList.argsArray.length).toEqual(3);
  1359. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1360. expect(opList.argsArray[0]).toEqual(["271R", [0, 0, 32, 10], [32, 0, 0, 10, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  1361. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1362. });
  1363. it("should render auto-sized text for printing", async function () {
  1364. textWidgetDict.set("DA", "/Helv 0 Tf");
  1365. const textWidgetRef = _primitives.Ref.get(271, 0);
  1366. const xref = new _test_utils.XRefMock([{
  1367. ref: textWidgetRef,
  1368. data: textWidgetDict
  1369. }, helvRefObj]);
  1370. const task = new _worker.WorkerTask("test print");
  1371. partialEvaluator.xref = xref;
  1372. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1373. const annotationStorage = new Map();
  1374. annotationStorage.set(annotation.data.id, {
  1375. value: "test (print)"
  1376. });
  1377. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1378. expect(appearance).toEqual("/Tx BMC q BT /Helv 5.92 Tf 0 g 1 0 0 1 0 0 Tm" + " 2 3.23 Td (test \\(print\\)) Tj ET Q EMC");
  1379. });
  1380. it("should render auto-sized text in Japanese for printing", async function () {
  1381. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1382. textWidgetDict.set("DA", "/Goth 0 Tf");
  1383. const textWidgetRef = _primitives.Ref.get(271, 0);
  1384. const xref = new _test_utils.XRefMock([{
  1385. ref: textWidgetRef,
  1386. data: textWidgetDict
  1387. }, gothRefObj]);
  1388. const task = new _worker.WorkerTask("test print");
  1389. partialEvaluator.xref = xref;
  1390. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1391. const annotationStorage = new Map();
  1392. annotationStorage.set(annotation.data.id, {
  1393. value: "こんにちは世界の"
  1394. });
  1395. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1396. const utf16String = "\x30\x53\x30\x93\x30\x6b\x30\x61\x30\x6f\x4e\x16\x75\x4c\x30\x6e";
  1397. expect(appearance).toEqual("/Tx BMC q BT /Goth 3.5 Tf 0 g 1 0 0 1 0 0 Tm" + ` 2 2 Td (${utf16String}) Tj ET Q EMC`);
  1398. });
  1399. it("should not render a password for printing", async function () {
  1400. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.PASSWORD);
  1401. const textWidgetRef = _primitives.Ref.get(271, 0);
  1402. const xref = new _test_utils.XRefMock([{
  1403. ref: textWidgetRef,
  1404. data: textWidgetDict
  1405. }, helvRefObj]);
  1406. const task = new _worker.WorkerTask("test print");
  1407. partialEvaluator.xref = xref;
  1408. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1409. const annotationStorage = new Map();
  1410. annotationStorage.set(annotation.data.id, {
  1411. value: "mypassword"
  1412. });
  1413. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1414. expect(appearance).toEqual(null);
  1415. });
  1416. it("should render multiline text for printing", async function () {
  1417. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTILINE);
  1418. const textWidgetRef = _primitives.Ref.get(271, 0);
  1419. const xref = new _test_utils.XRefMock([{
  1420. ref: textWidgetRef,
  1421. data: textWidgetDict
  1422. }, helvRefObj]);
  1423. const task = new _worker.WorkerTask("test print");
  1424. partialEvaluator.xref = xref;
  1425. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1426. const annotationStorage = new Map();
  1427. annotationStorage.set(annotation.data.id, {
  1428. value: "a aa aaa aaaa aaaaa aaaaaa " + "pneumonoultramicroscopicsilicovolcanoconiosis"
  1429. });
  1430. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1431. expect(appearance).toEqual("/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 10 Tm " + "2 -5 Td (a aa aaa ) Tj\n" + "0 -5 Td (aaaa aaaaa ) Tj\n" + "0 -5 Td (aaaaaa ) Tj\n" + "0 -5 Td (pneumonoultr) Tj\n" + "0 -5 Td (amicroscopi) Tj\n" + "0 -5 Td (csilicovolca) Tj\n" + "0 -5 Td (noconiosis) Tj ET Q EMC");
  1432. });
  1433. it("should render multiline text in Japanese for printing", async function () {
  1434. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTILINE);
  1435. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1436. textWidgetDict.set("DA", "/Goth 5 Tf");
  1437. const textWidgetRef = _primitives.Ref.get(271, 0);
  1438. const xref = new _test_utils.XRefMock([{
  1439. ref: textWidgetRef,
  1440. data: textWidgetDict
  1441. }, gothRefObj]);
  1442. const task = new _worker.WorkerTask("test print");
  1443. partialEvaluator.xref = xref;
  1444. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1445. const annotationStorage = new Map();
  1446. annotationStorage.set(annotation.data.id, {
  1447. value: "こんにちは世界の"
  1448. });
  1449. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1450. expect(appearance).toEqual("/Tx BMC q BT /Goth 5 Tf 1 0 0 1 0 10 Tm " + "2 -5 Td (\x30\x53\x30\x93\x30\x6b\x30\x61\x30\x6f) Tj\n" + "0 -5 Td (\x4e\x16\x75\x4c\x30\x6e) Tj ET Q EMC");
  1451. });
  1452. it("should render multiline text with various EOL for printing", async function () {
  1453. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTILINE);
  1454. textWidgetDict.set("Rect", [0, 0, 128, 10]);
  1455. const textWidgetRef = _primitives.Ref.get(271, 0);
  1456. const xref = new _test_utils.XRefMock([{
  1457. ref: textWidgetRef,
  1458. data: textWidgetDict
  1459. }, helvRefObj]);
  1460. const task = new _worker.WorkerTask("test print");
  1461. partialEvaluator.xref = xref;
  1462. const expectedAppearance = "/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 10 Tm " + "2 -5 Td " + "(Lorem ipsum dolor sit amet, consectetur adipiscing elit.) Tj\n" + "0 -5 Td " + "(Aliquam vitae felis ac lectus bibendum ultricies quis non) Tj\n" + "0 -5 Td " + "( diam.) Tj\n" + "0 -5 Td " + "(Morbi id porttitor quam, a iaculis dui.) Tj\n" + "0 -5 Td " + "(Pellentesque habitant morbi tristique senectus et netus ) Tj\n" + "0 -5 Td " + "(et malesuada fames ac turpis egestas.) Tj\n" + "0 -5 Td () Tj\n" + "0 -5 Td () Tj\n" + "0 -5 Td " + "(Nulla consectetur, ligula in tincidunt placerat, velit ) Tj\n" + "0 -5 Td " + "(augue consectetur orci, sed mattis libero nunc ut massa.) Tj\n" + "0 -5 Td " + "(Etiam facilisis tempus interdum.) Tj ET Q EMC";
  1463. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1464. const annotationStorage = new Map();
  1465. annotationStorage.set(annotation.data.id, {
  1466. 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."
  1467. });
  1468. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1469. expect(appearance).toEqual(expectedAppearance);
  1470. });
  1471. it("should render comb for printing", async function () {
  1472. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.COMB);
  1473. textWidgetDict.set("MaxLen", 4);
  1474. const textWidgetRef = _primitives.Ref.get(271, 0);
  1475. const xref = new _test_utils.XRefMock([{
  1476. ref: textWidgetRef,
  1477. data: textWidgetDict
  1478. }, helvRefObj]);
  1479. const task = new _worker.WorkerTask("test print");
  1480. partialEvaluator.xref = xref;
  1481. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1482. const annotationStorage = new Map();
  1483. annotationStorage.set(annotation.data.id, {
  1484. value: "aa(aa)a\\"
  1485. });
  1486. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1487. expect(appearance).toEqual("/Tx BMC q BT /Helv 5 Tf 1 0 0 1 2 3.035 Tm" + " (a) Tj 8 0 Td (a) Tj 8 0 Td (\\() Tj" + " 8 0 Td (a) Tj 8 0 Td (a) Tj" + " 8 0 Td (\\)) Tj 8 0 Td (a) Tj" + " 8 0 Td (\\\\) Tj ET Q EMC");
  1488. });
  1489. it("should render comb with Japanese text for printing", async function () {
  1490. textWidgetDict.set("Ff", _util.AnnotationFieldFlag.COMB);
  1491. textWidgetDict.set("MaxLen", 4);
  1492. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1493. textWidgetDict.set("DA", "/Goth 5 Tf");
  1494. textWidgetDict.set("Rect", [0, 0, 32, 10]);
  1495. const textWidgetRef = _primitives.Ref.get(271, 0);
  1496. const xref = new _test_utils.XRefMock([{
  1497. ref: textWidgetRef,
  1498. data: textWidgetDict
  1499. }, gothRefObj]);
  1500. const task = new _worker.WorkerTask("test print");
  1501. partialEvaluator.xref = xref;
  1502. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1503. const annotationStorage = new Map();
  1504. annotationStorage.set(annotation.data.id, {
  1505. value: "こんにちは世界の"
  1506. });
  1507. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  1508. expect(appearance).toEqual("/Tx BMC q BT /Goth 5 Tf 1 0 0 1 2 2 Tm" + " (\x30\x53) Tj 8 0 Td (\x30\x93) Tj 8 0 Td (\x30\x6b) Tj" + " 8 0 Td (\x30\x61) Tj 8 0 Td (\x30\x6f) Tj" + " 8 0 Td (\x4e\x16) Tj 8 0 Td (\x75\x4c) Tj" + " 8 0 Td (\x30\x6e) Tj ET Q EMC");
  1509. });
  1510. it("should save text", async function () {
  1511. const textWidgetRef = _primitives.Ref.get(123, 0);
  1512. const xref = new _test_utils.XRefMock([{
  1513. ref: textWidgetRef,
  1514. data: textWidgetDict
  1515. }, helvRefObj]);
  1516. partialEvaluator.xref = xref;
  1517. const task = new _worker.WorkerTask("test save");
  1518. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1519. const annotationStorage = new Map();
  1520. annotationStorage.set(annotation.data.id, {
  1521. value: "hello world"
  1522. });
  1523. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  1524. expect(data.length).toEqual(2);
  1525. const [oldData, newData] = data;
  1526. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1527. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  1528. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  1529. 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");
  1530. expect(newData.data).toEqual("2 0 obj\n<< /Length 74 /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 3.04 Td (hello world) Tj " + "ET Q EMC\nendstream\nendobj\n");
  1531. });
  1532. it("should save rotated text", async function () {
  1533. const textWidgetRef = _primitives.Ref.get(123, 0);
  1534. const xref = new _test_utils.XRefMock([{
  1535. ref: textWidgetRef,
  1536. data: textWidgetDict
  1537. }, helvRefObj]);
  1538. partialEvaluator.xref = xref;
  1539. const task = new _worker.WorkerTask("test save");
  1540. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1541. const annotationStorage = new Map();
  1542. annotationStorage.set(annotation.data.id, {
  1543. value: "hello world",
  1544. rotation: 90
  1545. });
  1546. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  1547. expect(data.length).toEqual(2);
  1548. const [oldData, newData] = data;
  1549. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1550. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  1551. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  1552. 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) /MK << /R 90>>>>\nendobj\n");
  1553. expect(newData.data).toEqual("2 0 obj\n<< /Length 74 /Subtype /Form /Resources " + "<< /Font << /Helv 314 0 R>>>> /BBox [0 0 32 10] /Matrix [0 1 -1 0 32 0]>> stream\n" + "/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm 2 3.04 Td (hello world) Tj " + "ET Q EMC\nendstream\nendobj\n");
  1554. });
  1555. it("should get field object for usage in JS sandbox", async function () {
  1556. const textWidgetRef = _primitives.Ref.get(123, 0);
  1557. const xDictRef = _primitives.Ref.get(141, 0);
  1558. const dDictRef = _primitives.Ref.get(262, 0);
  1559. const next0Ref = _primitives.Ref.get(314, 0);
  1560. const next1Ref = _primitives.Ref.get(271, 0);
  1561. const next2Ref = _primitives.Ref.get(577, 0);
  1562. const next00Ref = _primitives.Ref.get(413, 0);
  1563. const xDict = new _primitives.Dict();
  1564. const dDict = new _primitives.Dict();
  1565. const next0Dict = new _primitives.Dict();
  1566. const next1Dict = new _primitives.Dict();
  1567. const next2Dict = new _primitives.Dict();
  1568. const next00Dict = new _primitives.Dict();
  1569. const xref = new _test_utils.XRefMock([{
  1570. ref: textWidgetRef,
  1571. data: textWidgetDict
  1572. }, {
  1573. ref: xDictRef,
  1574. data: xDict
  1575. }, {
  1576. ref: dDictRef,
  1577. data: dDict
  1578. }, {
  1579. ref: next0Ref,
  1580. data: next0Dict
  1581. }, {
  1582. ref: next00Ref,
  1583. data: next00Dict
  1584. }, {
  1585. ref: next1Ref,
  1586. data: next1Dict
  1587. }, {
  1588. ref: next2Ref,
  1589. data: next2Dict
  1590. }]);
  1591. const JS = _primitives.Name.get("JavaScript");
  1592. const additionalActionsDict = new _primitives.Dict();
  1593. const eDict = new _primitives.Dict();
  1594. eDict.set("JS", "hello()");
  1595. eDict.set("S", JS);
  1596. additionalActionsDict.set("E", eDict);
  1597. xDict.set("JS", "world()");
  1598. xDict.set("S", JS);
  1599. xDict.set("Next", [next0Ref, next1Ref, next2Ref, xDictRef]);
  1600. next0Dict.set("JS", "olleh()");
  1601. next0Dict.set("S", JS);
  1602. next0Dict.set("Next", next00Ref);
  1603. next00Dict.set("JS", "foo()");
  1604. next00Dict.set("S", JS);
  1605. next00Dict.set("Next", next0Ref);
  1606. next1Dict.set("JS", "dlrow()");
  1607. next1Dict.set("S", JS);
  1608. next1Dict.set("Next", xDictRef);
  1609. next2Dict.set("JS", "oof()");
  1610. next2Dict.set("S", JS);
  1611. dDict.set("JS", "bar()");
  1612. dDict.set("S", JS);
  1613. dDict.set("Next", dDictRef);
  1614. additionalActionsDict.set("D", dDictRef);
  1615. additionalActionsDict.set("X", xDictRef);
  1616. textWidgetDict.set("AA", additionalActionsDict);
  1617. partialEvaluator.xref = xref;
  1618. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1619. const fieldObject = await annotation.getFieldObject();
  1620. const actions = fieldObject.actions;
  1621. expect(actions["Mouse Enter"]).toEqual(["hello()"]);
  1622. expect(actions["Mouse Exit"]).toEqual(["world()", "olleh()", "foo()", "dlrow()", "oof()"]);
  1623. expect(actions["Mouse Down"]).toEqual(["bar()"]);
  1624. });
  1625. it("should save Japanese text", async function () {
  1626. textWidgetDict.get("DR").get("Font").set("Goth", gothRefObj.ref);
  1627. textWidgetDict.set("DA", "/Goth 5 Tf");
  1628. const textWidgetRef = _primitives.Ref.get(123, 0);
  1629. const xref = new _test_utils.XRefMock([{
  1630. ref: textWidgetRef,
  1631. data: textWidgetDict
  1632. }, gothRefObj]);
  1633. partialEvaluator.xref = xref;
  1634. const task = new _worker.WorkerTask("test save");
  1635. const annotation = await _annotation.AnnotationFactory.create(xref, textWidgetRef, pdfManagerMock, idFactoryMock);
  1636. const annotationStorage = new Map();
  1637. annotationStorage.set(annotation.data.id, {
  1638. value: "こんにちは世界の"
  1639. });
  1640. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  1641. const utf16String = "\x30\x53\x30\x93\x30\x6b\x30\x61\x30\x6f\x4e\x16\x75\x4c\x30\x6e";
  1642. expect(data.length).toEqual(2);
  1643. const [oldData, newData] = data;
  1644. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1645. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  1646. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  1647. 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`);
  1648. expect(newData.data).toEqual("2 0 obj\n<< /Length 76 /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 2 Td (${utf16String}) Tj ` + "ET Q EMC\nendstream\nendobj\n");
  1649. });
  1650. });
  1651. describe("ButtonWidgetAnnotation", function () {
  1652. let buttonWidgetDict;
  1653. beforeEach(function () {
  1654. buttonWidgetDict = new _primitives.Dict();
  1655. buttonWidgetDict.set("Type", _primitives.Name.get("Annot"));
  1656. buttonWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  1657. buttonWidgetDict.set("FT", _primitives.Name.get("Btn"));
  1658. });
  1659. afterEach(function () {
  1660. buttonWidgetDict = null;
  1661. });
  1662. it("should handle checkboxes with export value", async function () {
  1663. buttonWidgetDict.set("V", _primitives.Name.get("Checked"));
  1664. buttonWidgetDict.set("DV", _primitives.Name.get("Off"));
  1665. const appearanceStatesDict = new _primitives.Dict();
  1666. const normalAppearanceDict = new _primitives.Dict();
  1667. normalAppearanceDict.set("Off", 0);
  1668. normalAppearanceDict.set("Checked", 1);
  1669. appearanceStatesDict.set("N", normalAppearanceDict);
  1670. buttonWidgetDict.set("AP", appearanceStatesDict);
  1671. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1672. const xref = new _test_utils.XRefMock([{
  1673. ref: buttonWidgetRef,
  1674. data: buttonWidgetDict
  1675. }]);
  1676. const {
  1677. data
  1678. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1679. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1680. expect(data.checkBox).toEqual(true);
  1681. expect(data.fieldValue).toEqual("Checked");
  1682. expect(data.defaultFieldValue).toEqual("Off");
  1683. expect(data.radioButton).toEqual(false);
  1684. expect(data.exportValue).toEqual("Checked");
  1685. });
  1686. it("should handle checkboxes without export value", async function () {
  1687. buttonWidgetDict.set("V", _primitives.Name.get("Checked"));
  1688. buttonWidgetDict.set("DV", _primitives.Name.get("Off"));
  1689. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1690. const xref = new _test_utils.XRefMock([{
  1691. ref: buttonWidgetRef,
  1692. data: buttonWidgetDict
  1693. }]);
  1694. const {
  1695. data
  1696. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1697. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1698. expect(data.checkBox).toEqual(true);
  1699. expect(data.fieldValue).toEqual("Checked");
  1700. expect(data.defaultFieldValue).toEqual("Off");
  1701. expect(data.radioButton).toEqual(false);
  1702. });
  1703. it("should handle checkboxes without /Off appearance", async function () {
  1704. buttonWidgetDict.set("V", _primitives.Name.get("Checked"));
  1705. buttonWidgetDict.set("DV", _primitives.Name.get("Off"));
  1706. const appearanceStatesDict = new _primitives.Dict();
  1707. const normalAppearanceDict = new _primitives.Dict();
  1708. normalAppearanceDict.set("Checked", 1);
  1709. appearanceStatesDict.set("N", normalAppearanceDict);
  1710. buttonWidgetDict.set("AP", appearanceStatesDict);
  1711. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1712. const xref = new _test_utils.XRefMock([{
  1713. ref: buttonWidgetRef,
  1714. data: buttonWidgetDict
  1715. }]);
  1716. const {
  1717. data
  1718. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1719. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1720. expect(data.checkBox).toEqual(true);
  1721. expect(data.fieldValue).toEqual("Checked");
  1722. expect(data.defaultFieldValue).toEqual("Off");
  1723. expect(data.radioButton).toEqual(false);
  1724. expect(data.exportValue).toEqual("Checked");
  1725. });
  1726. it("should render checkbox with fallback font for printing", async function () {
  1727. const appearanceStatesDict = new _primitives.Dict();
  1728. const normalAppearanceDict = new _primitives.Dict();
  1729. const checkedAppearanceDict = new _primitives.Dict();
  1730. const uncheckedAppearanceDict = new _primitives.Dict();
  1731. const checkedStream = new _stream.StringStream("/ 12 Tf (4) Tj");
  1732. checkedStream.dict = checkedAppearanceDict;
  1733. const uncheckedStream = new _stream.StringStream("");
  1734. uncheckedStream.dict = uncheckedAppearanceDict;
  1735. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1736. checkedAppearanceDict.set("FormType", 1);
  1737. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1738. normalAppearanceDict.set("Checked", checkedStream);
  1739. normalAppearanceDict.set("Off", uncheckedStream);
  1740. appearanceStatesDict.set("N", normalAppearanceDict);
  1741. buttonWidgetDict.set("AP", appearanceStatesDict);
  1742. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1743. const xref = new _test_utils.XRefMock([{
  1744. ref: buttonWidgetRef,
  1745. data: buttonWidgetDict
  1746. }]);
  1747. const task = new _worker.WorkerTask("test print");
  1748. const checkboxEvaluator = partialEvaluator.clone({
  1749. ignoreErrors: true
  1750. });
  1751. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1752. const annotationStorage = new Map();
  1753. annotationStorage.set(annotation.data.id, {
  1754. value: true
  1755. });
  1756. const {
  1757. opList
  1758. } = await annotation.getOperatorList(checkboxEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  1759. expect(opList.argsArray.length).toEqual(5);
  1760. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.dependency, _util.OPS.setFont, _util.OPS.showText, _util.OPS.endAnnotation]);
  1761. expect(opList.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  1762. expect(opList.argsArray[3][0][0].unicode).toEqual("4");
  1763. });
  1764. it("should render checkboxes for printing", async function () {
  1765. const appearanceStatesDict = new _primitives.Dict();
  1766. const normalAppearanceDict = new _primitives.Dict();
  1767. const checkedAppearanceDict = new _primitives.Dict();
  1768. const uncheckedAppearanceDict = new _primitives.Dict();
  1769. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1770. checkedStream.dict = checkedAppearanceDict;
  1771. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  1772. uncheckedStream.dict = uncheckedAppearanceDict;
  1773. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1774. checkedAppearanceDict.set("FormType", 1);
  1775. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1776. normalAppearanceDict.set("Checked", checkedStream);
  1777. normalAppearanceDict.set("Off", uncheckedStream);
  1778. appearanceStatesDict.set("N", normalAppearanceDict);
  1779. buttonWidgetDict.set("AP", appearanceStatesDict);
  1780. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1781. const xref = new _test_utils.XRefMock([{
  1782. ref: buttonWidgetRef,
  1783. data: buttonWidgetDict
  1784. }]);
  1785. const task = new _worker.WorkerTask("test print");
  1786. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1787. const annotationStorage = new Map();
  1788. annotationStorage.set(annotation.data.id, {
  1789. value: true
  1790. });
  1791. const {
  1792. opList: opList1
  1793. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  1794. expect(opList1.argsArray.length).toEqual(3);
  1795. expect(opList1.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1796. expect(opList1.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  1797. expect(opList1.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1798. annotationStorage.set(annotation.data.id, {
  1799. value: false
  1800. });
  1801. const {
  1802. opList: opList2
  1803. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  1804. expect(opList2.argsArray.length).toEqual(3);
  1805. expect(opList2.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1806. expect(opList2.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  1807. expect(opList2.argsArray[1]).toEqual(new Uint8ClampedArray([76, 51, 26]));
  1808. });
  1809. it("should render checkboxes for printing twice", async function () {
  1810. const appearanceStatesDict = new _primitives.Dict();
  1811. const normalAppearanceDict = new _primitives.Dict();
  1812. const checkedAppearanceDict = new _primitives.Dict();
  1813. const uncheckedAppearanceDict = new _primitives.Dict();
  1814. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1815. checkedStream.dict = checkedAppearanceDict;
  1816. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  1817. uncheckedStream.dict = uncheckedAppearanceDict;
  1818. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1819. checkedAppearanceDict.set("FormType", 1);
  1820. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1821. normalAppearanceDict.set("Checked", checkedStream);
  1822. normalAppearanceDict.set("Off", uncheckedStream);
  1823. appearanceStatesDict.set("N", normalAppearanceDict);
  1824. buttonWidgetDict.set("AP", appearanceStatesDict);
  1825. buttonWidgetDict.set("AS", _primitives.Name.get("Off"));
  1826. const buttonWidgetRef = _primitives.Ref.get(1249, 0);
  1827. const xref = new _test_utils.XRefMock([{
  1828. ref: buttonWidgetRef,
  1829. data: buttonWidgetDict
  1830. }]);
  1831. const task = new _worker.WorkerTask("test print");
  1832. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1833. const annotationStorage = new Map();
  1834. for (let i = 0; i < 2; i++) {
  1835. annotationStorage.set(annotation.data.id, {
  1836. value: true
  1837. });
  1838. const {
  1839. opList
  1840. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  1841. expect(opList.argsArray.length).toEqual(3);
  1842. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1843. expect(opList.argsArray[0]).toEqual(["1249R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  1844. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1845. }
  1846. });
  1847. it("should render checkboxes for printing using normal appearance", async function () {
  1848. const appearanceStatesDict = new _primitives.Dict();
  1849. const normalAppearanceDict = new _primitives.Dict();
  1850. const checkedAppearanceDict = new _primitives.Dict();
  1851. const uncheckedAppearanceDict = new _primitives.Dict();
  1852. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  1853. checkedStream.dict = checkedAppearanceDict;
  1854. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  1855. uncheckedStream.dict = uncheckedAppearanceDict;
  1856. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  1857. checkedAppearanceDict.set("FormType", 1);
  1858. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  1859. normalAppearanceDict.set("Checked", checkedStream);
  1860. normalAppearanceDict.set("Off", uncheckedStream);
  1861. appearanceStatesDict.set("N", normalAppearanceDict);
  1862. buttonWidgetDict.set("AP", appearanceStatesDict);
  1863. buttonWidgetDict.set("AS", _primitives.Name.get("Checked"));
  1864. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1865. const xref = new _test_utils.XRefMock([{
  1866. ref: buttonWidgetRef,
  1867. data: buttonWidgetDict
  1868. }]);
  1869. const task = new _worker.WorkerTask("test print");
  1870. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1871. const annotationStorage = new Map();
  1872. const {
  1873. opList
  1874. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  1875. expect(opList.argsArray.length).toEqual(3);
  1876. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  1877. expect(opList.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  1878. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  1879. });
  1880. it("should save checkboxes", async function () {
  1881. const appearanceStatesDict = new _primitives.Dict();
  1882. const normalAppearanceDict = new _primitives.Dict();
  1883. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  1884. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  1885. appearanceStatesDict.set("N", normalAppearanceDict);
  1886. buttonWidgetDict.set("AP", appearanceStatesDict);
  1887. buttonWidgetDict.set("V", _primitives.Name.get("Off"));
  1888. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  1889. const xref = new _test_utils.XRefMock([{
  1890. ref: buttonWidgetRef,
  1891. data: buttonWidgetDict
  1892. }]);
  1893. partialEvaluator.xref = xref;
  1894. const task = new _worker.WorkerTask("test save");
  1895. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1896. const annotationStorage = new Map();
  1897. annotationStorage.set(annotation.data.id, {
  1898. value: true
  1899. });
  1900. const [oldData] = await annotation.save(partialEvaluator, task, annotationStorage);
  1901. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  1902. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1903. 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");
  1904. annotationStorage.set(annotation.data.id, {
  1905. value: false
  1906. });
  1907. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  1908. expect(data).toEqual(null);
  1909. });
  1910. it("should save rotated checkboxes", async function () {
  1911. const appearanceStatesDict = new _primitives.Dict();
  1912. const normalAppearanceDict = new _primitives.Dict();
  1913. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  1914. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  1915. appearanceStatesDict.set("N", normalAppearanceDict);
  1916. buttonWidgetDict.set("AP", appearanceStatesDict);
  1917. buttonWidgetDict.set("V", _primitives.Name.get("Off"));
  1918. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  1919. const xref = new _test_utils.XRefMock([{
  1920. ref: buttonWidgetRef,
  1921. data: buttonWidgetDict
  1922. }]);
  1923. partialEvaluator.xref = xref;
  1924. const task = new _worker.WorkerTask("test save");
  1925. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1926. const annotationStorage = new Map();
  1927. annotationStorage.set(annotation.data.id, {
  1928. value: true,
  1929. rotation: 180
  1930. });
  1931. const [oldData] = await annotation.save(partialEvaluator, task, annotationStorage);
  1932. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  1933. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  1934. 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) /MK << /R 180>>>>\nendobj\n");
  1935. annotationStorage.set(annotation.data.id, {
  1936. value: false
  1937. });
  1938. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  1939. expect(data).toEqual(null);
  1940. });
  1941. it("should handle radio buttons with a field value", async function () {
  1942. const parentDict = new _primitives.Dict();
  1943. parentDict.set("V", _primitives.Name.get("1"));
  1944. const normalAppearanceStateDict = new _primitives.Dict();
  1945. normalAppearanceStateDict.set("2", null);
  1946. const appearanceStatesDict = new _primitives.Dict();
  1947. appearanceStatesDict.set("N", normalAppearanceStateDict);
  1948. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  1949. buttonWidgetDict.set("Parent", parentDict);
  1950. buttonWidgetDict.set("AP", appearanceStatesDict);
  1951. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1952. const xref = new _test_utils.XRefMock([{
  1953. ref: buttonWidgetRef,
  1954. data: buttonWidgetDict
  1955. }]);
  1956. const {
  1957. data
  1958. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1959. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1960. expect(data.checkBox).toEqual(false);
  1961. expect(data.radioButton).toEqual(true);
  1962. expect(data.fieldValue).toEqual("1");
  1963. expect(data.buttonValue).toEqual("2");
  1964. });
  1965. it("should handle radio buttons with a field value that's not an ASCII string", async function () {
  1966. const parentDict = new _primitives.Dict();
  1967. parentDict.set("V", _primitives.Name.get("\x91I=\x91\xf0\x93\xe0\x97e3"));
  1968. const normalAppearanceStateDict = new _primitives.Dict();
  1969. normalAppearanceStateDict.set("\x91I=\x91\xf0\x93\xe0\x97e3", null);
  1970. const appearanceStatesDict = new _primitives.Dict();
  1971. appearanceStatesDict.set("N", normalAppearanceStateDict);
  1972. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  1973. buttonWidgetDict.set("Parent", parentDict);
  1974. buttonWidgetDict.set("AP", appearanceStatesDict);
  1975. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1976. const xref = new _test_utils.XRefMock([{
  1977. ref: buttonWidgetRef,
  1978. data: buttonWidgetDict
  1979. }]);
  1980. const {
  1981. data
  1982. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  1983. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  1984. expect(data.checkBox).toEqual(false);
  1985. expect(data.radioButton).toEqual(true);
  1986. expect(data.fieldValue).toEqual("‚I=‚ðfiàŠe3");
  1987. expect(data.buttonValue).toEqual("‚I=‚ðfiàŠe3");
  1988. });
  1989. it("should handle radio buttons without a field value", async function () {
  1990. const normalAppearanceStateDict = new _primitives.Dict();
  1991. normalAppearanceStateDict.set("2", null);
  1992. const appearanceStatesDict = new _primitives.Dict();
  1993. appearanceStatesDict.set("N", normalAppearanceStateDict);
  1994. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  1995. buttonWidgetDict.set("AP", appearanceStatesDict);
  1996. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  1997. const xref = new _test_utils.XRefMock([{
  1998. ref: buttonWidgetRef,
  1999. data: buttonWidgetDict
  2000. }]);
  2001. const {
  2002. data
  2003. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2004. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2005. expect(data.checkBox).toEqual(false);
  2006. expect(data.radioButton).toEqual(true);
  2007. expect(data.fieldValue).toEqual(null);
  2008. expect(data.buttonValue).toEqual("2");
  2009. });
  2010. it("should render radio buttons for printing", async function () {
  2011. const appearanceStatesDict = new _primitives.Dict();
  2012. const normalAppearanceDict = new _primitives.Dict();
  2013. const checkedAppearanceDict = new _primitives.Dict();
  2014. const uncheckedAppearanceDict = new _primitives.Dict();
  2015. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  2016. checkedStream.dict = checkedAppearanceDict;
  2017. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  2018. uncheckedStream.dict = uncheckedAppearanceDict;
  2019. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  2020. checkedAppearanceDict.set("FormType", 1);
  2021. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  2022. normalAppearanceDict.set("Checked", checkedStream);
  2023. normalAppearanceDict.set("Off", uncheckedStream);
  2024. appearanceStatesDict.set("N", normalAppearanceDict);
  2025. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2026. buttonWidgetDict.set("AP", appearanceStatesDict);
  2027. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2028. const xref = new _test_utils.XRefMock([{
  2029. ref: buttonWidgetRef,
  2030. data: buttonWidgetDict
  2031. }]);
  2032. const task = new _worker.WorkerTask("test print");
  2033. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2034. const annotationStorage = new Map();
  2035. annotationStorage.set(annotation.data.id, {
  2036. value: true
  2037. });
  2038. const {
  2039. opList: opList1
  2040. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  2041. expect(opList1.argsArray.length).toEqual(3);
  2042. expect(opList1.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  2043. expect(opList1.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  2044. expect(opList1.argsArray[1]).toEqual(new Uint8ClampedArray([26, 51, 76]));
  2045. annotationStorage.set(annotation.data.id, {
  2046. value: false
  2047. });
  2048. const {
  2049. opList: opList2
  2050. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  2051. expect(opList2.argsArray.length).toEqual(3);
  2052. expect(opList2.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  2053. expect(opList2.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  2054. expect(opList2.argsArray[1]).toEqual(new Uint8ClampedArray([76, 51, 26]));
  2055. });
  2056. it("should render radio buttons for printing using normal appearance", async function () {
  2057. const appearanceStatesDict = new _primitives.Dict();
  2058. const normalAppearanceDict = new _primitives.Dict();
  2059. const checkedAppearanceDict = new _primitives.Dict();
  2060. const uncheckedAppearanceDict = new _primitives.Dict();
  2061. const checkedStream = new _stream.StringStream("0.1 0.2 0.3 rg");
  2062. checkedStream.dict = checkedAppearanceDict;
  2063. const uncheckedStream = new _stream.StringStream("0.3 0.2 0.1 rg");
  2064. uncheckedStream.dict = uncheckedAppearanceDict;
  2065. checkedAppearanceDict.set("BBox", [0, 0, 8, 8]);
  2066. checkedAppearanceDict.set("FormType", 1);
  2067. checkedAppearanceDict.set("Matrix", [1, 0, 0, 1, 0, 0]);
  2068. normalAppearanceDict.set("Checked", checkedStream);
  2069. normalAppearanceDict.set("Off", uncheckedStream);
  2070. appearanceStatesDict.set("N", normalAppearanceDict);
  2071. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2072. buttonWidgetDict.set("AP", appearanceStatesDict);
  2073. buttonWidgetDict.set("AS", _primitives.Name.get("Off"));
  2074. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2075. const xref = new _test_utils.XRefMock([{
  2076. ref: buttonWidgetRef,
  2077. data: buttonWidgetDict
  2078. }]);
  2079. const task = new _worker.WorkerTask("test print");
  2080. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2081. const annotationStorage = new Map();
  2082. const {
  2083. opList
  2084. } = await annotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, annotationStorage);
  2085. expect(opList.argsArray.length).toEqual(3);
  2086. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setFillRGBColor, _util.OPS.endAnnotation]);
  2087. expect(opList.argsArray[0]).toEqual(["124R", [0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0], false]);
  2088. expect(opList.argsArray[1]).toEqual(new Uint8ClampedArray([76, 51, 26]));
  2089. });
  2090. it("should save radio buttons", async function () {
  2091. const appearanceStatesDict = new _primitives.Dict();
  2092. const normalAppearanceDict = new _primitives.Dict();
  2093. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  2094. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  2095. appearanceStatesDict.set("N", normalAppearanceDict);
  2096. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2097. buttonWidgetDict.set("AP", appearanceStatesDict);
  2098. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  2099. const parentRef = _primitives.Ref.get(456, 0);
  2100. const parentDict = new _primitives.Dict();
  2101. parentDict.set("V", _primitives.Name.get("Off"));
  2102. parentDict.set("Kids", [buttonWidgetRef]);
  2103. buttonWidgetDict.set("Parent", parentRef);
  2104. const xref = new _test_utils.XRefMock([{
  2105. ref: buttonWidgetRef,
  2106. data: buttonWidgetDict
  2107. }, {
  2108. ref: parentRef,
  2109. data: parentDict
  2110. }]);
  2111. parentDict.xref = xref;
  2112. buttonWidgetDict.xref = xref;
  2113. partialEvaluator.xref = xref;
  2114. const task = new _worker.WorkerTask("test save");
  2115. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2116. const annotationStorage = new Map();
  2117. annotationStorage.set(annotation.data.id, {
  2118. value: true
  2119. });
  2120. let data = await annotation.save(partialEvaluator, task, annotationStorage);
  2121. expect(data.length).toEqual(2);
  2122. const [radioData, parentData] = data;
  2123. radioData.data = radioData.data.replace(/\(D:\d+\)/, "(date)");
  2124. expect(radioData.ref).toEqual(_primitives.Ref.get(123, 0));
  2125. 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");
  2126. expect(parentData.ref).toEqual(_primitives.Ref.get(456, 0));
  2127. expect(parentData.data).toEqual("456 0 obj\n<< /V /Checked /Kids [123 0 R]>>\nendobj\n");
  2128. annotationStorage.set(annotation.data.id, {
  2129. value: false
  2130. });
  2131. data = await annotation.save(partialEvaluator, task, annotationStorage);
  2132. expect(data).toEqual(null);
  2133. });
  2134. it("should save radio buttons without a field value", async function () {
  2135. const appearanceStatesDict = new _primitives.Dict();
  2136. const normalAppearanceDict = new _primitives.Dict();
  2137. normalAppearanceDict.set("Checked", _primitives.Ref.get(314, 0));
  2138. normalAppearanceDict.set("Off", _primitives.Ref.get(271, 0));
  2139. appearanceStatesDict.set("N", normalAppearanceDict);
  2140. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.RADIO);
  2141. buttonWidgetDict.set("AP", appearanceStatesDict);
  2142. const buttonWidgetRef = _primitives.Ref.get(123, 0);
  2143. const parentRef = _primitives.Ref.get(456, 0);
  2144. const parentDict = new _primitives.Dict();
  2145. parentDict.set("Kids", [buttonWidgetRef]);
  2146. buttonWidgetDict.set("Parent", parentRef);
  2147. const xref = new _test_utils.XRefMock([{
  2148. ref: buttonWidgetRef,
  2149. data: buttonWidgetDict
  2150. }, {
  2151. ref: parentRef,
  2152. data: parentDict
  2153. }]);
  2154. parentDict.xref = xref;
  2155. buttonWidgetDict.xref = xref;
  2156. partialEvaluator.xref = xref;
  2157. const task = new _worker.WorkerTask("test save");
  2158. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2159. const annotationStorage = new Map();
  2160. annotationStorage.set(annotation.data.id, {
  2161. value: true
  2162. });
  2163. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  2164. expect(data.length).toEqual(2);
  2165. const [radioData, parentData] = data;
  2166. radioData.data = radioData.data.replace(/\(D:\d+\)/, "(date)");
  2167. expect(radioData.ref).toEqual(_primitives.Ref.get(123, 0));
  2168. 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");
  2169. expect(parentData.ref).toEqual(_primitives.Ref.get(456, 0));
  2170. expect(parentData.data).toEqual("456 0 obj\n<< /Kids [123 0 R] /V /Checked>>\nendobj\n");
  2171. });
  2172. it("should save nothing", async function () {
  2173. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2174. const xref = new _test_utils.XRefMock([{
  2175. ref: buttonWidgetRef,
  2176. data: buttonWidgetDict
  2177. }]);
  2178. const task = new _worker.WorkerTask("test save");
  2179. const annotation = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2180. const annotationStorage = new Map();
  2181. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  2182. expect(data).toEqual(null);
  2183. });
  2184. it("should handle push buttons", async function () {
  2185. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2186. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2187. const actionDict = new _primitives.Dict();
  2188. actionDict.set("S", _primitives.Name.get("JavaScript"));
  2189. actionDict.set("JS", "do_something();");
  2190. buttonWidgetDict.set("A", actionDict);
  2191. const xref = new _test_utils.XRefMock([{
  2192. ref: buttonWidgetRef,
  2193. data: buttonWidgetDict
  2194. }]);
  2195. const {
  2196. data
  2197. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2198. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2199. expect(data.pushButton).toEqual(true);
  2200. expect(data.actions.Action).toEqual(["do_something();"]);
  2201. });
  2202. it("should handle push buttons that act as a tooltip only", async function () {
  2203. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2204. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2205. buttonWidgetDict.set("TU", "An alternative text");
  2206. const xref = new _test_utils.XRefMock([{
  2207. ref: buttonWidgetRef,
  2208. data: buttonWidgetDict
  2209. }]);
  2210. const {
  2211. data
  2212. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2213. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2214. expect(data.pushButton).toEqual(true);
  2215. expect(data.alternativeText).toEqual("An alternative text");
  2216. });
  2217. it("should handle URL in A dict in push buttons", async function () {
  2218. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2219. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2220. const actionDict = new _primitives.Dict();
  2221. actionDict.set("S", _primitives.Name.get("JavaScript"));
  2222. actionDict.set("JS", "app.launchURL('https://developer.mozilla.org/en-US/', true)");
  2223. buttonWidgetDict.set("A", actionDict);
  2224. const xref = new _test_utils.XRefMock([{
  2225. ref: buttonWidgetRef,
  2226. data: buttonWidgetDict
  2227. }]);
  2228. const {
  2229. data
  2230. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2231. expect(data.url).toEqual("https://developer.mozilla.org/en-US/");
  2232. });
  2233. it("should handle URL in AA dict in push buttons", async function () {
  2234. const buttonWidgetRef = _primitives.Ref.get(124, 0);
  2235. buttonWidgetDict.set("Ff", _util.AnnotationFieldFlag.PUSHBUTTON);
  2236. const dDict = new _primitives.Dict();
  2237. dDict.set("S", _primitives.Name.get("JavaScript"));
  2238. dDict.set("JS", "app.launchURL('https://developer.mozilla.org/en-US/', true)");
  2239. const actionDict = new _primitives.Dict();
  2240. actionDict.set("D", dDict);
  2241. buttonWidgetDict.set("AA", actionDict);
  2242. const xref = new _test_utils.XRefMock([{
  2243. ref: buttonWidgetRef,
  2244. data: buttonWidgetDict
  2245. }]);
  2246. const {
  2247. data
  2248. } = await _annotation.AnnotationFactory.create(xref, buttonWidgetRef, pdfManagerMock, idFactoryMock);
  2249. expect(data.url).toEqual("https://developer.mozilla.org/en-US/");
  2250. });
  2251. });
  2252. describe("ChoiceWidgetAnnotation", function () {
  2253. let choiceWidgetDict, fontRefObj;
  2254. beforeEach(function () {
  2255. choiceWidgetDict = new _primitives.Dict();
  2256. choiceWidgetDict.set("Type", _primitives.Name.get("Annot"));
  2257. choiceWidgetDict.set("Subtype", _primitives.Name.get("Widget"));
  2258. choiceWidgetDict.set("FT", _primitives.Name.get("Ch"));
  2259. const helvDict = new _primitives.Dict();
  2260. helvDict.set("BaseFont", _primitives.Name.get("Helvetica"));
  2261. helvDict.set("Type", _primitives.Name.get("Font"));
  2262. helvDict.set("Subtype", _primitives.Name.get("Type1"));
  2263. const fontRef = _primitives.Ref.get(314, 0);
  2264. fontRefObj = {
  2265. ref: fontRef,
  2266. data: helvDict
  2267. };
  2268. const resourceDict = new _primitives.Dict();
  2269. const fontDict = new _primitives.Dict();
  2270. fontDict.set("Helv", fontRef);
  2271. resourceDict.set("Font", fontDict);
  2272. choiceWidgetDict.set("DA", "/Helv 5 Tf");
  2273. choiceWidgetDict.set("DR", resourceDict);
  2274. choiceWidgetDict.set("Rect", [0, 0, 32, 10]);
  2275. });
  2276. afterEach(function () {
  2277. choiceWidgetDict = fontRefObj = null;
  2278. });
  2279. it("should handle missing option arrays", async function () {
  2280. const choiceWidgetRef = _primitives.Ref.get(122, 0);
  2281. const xref = new _test_utils.XRefMock([{
  2282. ref: choiceWidgetRef,
  2283. data: choiceWidgetDict
  2284. }]);
  2285. const {
  2286. data
  2287. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2288. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2289. expect(data.options).toEqual([]);
  2290. });
  2291. it("should handle option arrays with array elements", async function () {
  2292. const optionBarRef = _primitives.Ref.get(20, 0);
  2293. const optionBarStr = "Bar";
  2294. const optionOneRef = _primitives.Ref.get(10, 0);
  2295. const optionOneArr = ["bar_export", optionBarRef];
  2296. const options = [["foo_export", "Foo"], optionOneRef];
  2297. const expected = [{
  2298. exportValue: "foo_export",
  2299. displayValue: "Foo"
  2300. }, {
  2301. exportValue: "bar_export",
  2302. displayValue: "Bar"
  2303. }];
  2304. choiceWidgetDict.set("Opt", options);
  2305. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2306. const xref = new _test_utils.XRefMock([{
  2307. ref: choiceWidgetRef,
  2308. data: choiceWidgetDict
  2309. }, {
  2310. ref: optionBarRef,
  2311. data: optionBarStr
  2312. }, {
  2313. ref: optionOneRef,
  2314. data: optionOneArr
  2315. }]);
  2316. const {
  2317. data
  2318. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2319. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2320. expect(data.options).toEqual(expected);
  2321. });
  2322. it("should handle option arrays with string elements", async function () {
  2323. const optionBarRef = _primitives.Ref.get(10, 0);
  2324. const optionBarStr = "Bar";
  2325. const options = ["Foo", optionBarRef];
  2326. const expected = [{
  2327. exportValue: "Foo",
  2328. displayValue: "Foo"
  2329. }, {
  2330. exportValue: "Bar",
  2331. displayValue: "Bar"
  2332. }];
  2333. choiceWidgetDict.set("Opt", options);
  2334. const choiceWidgetRef = _primitives.Ref.get(981, 0);
  2335. const xref = new _test_utils.XRefMock([{
  2336. ref: choiceWidgetRef,
  2337. data: choiceWidgetDict
  2338. }, {
  2339. ref: optionBarRef,
  2340. data: optionBarStr
  2341. }]);
  2342. const {
  2343. data
  2344. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2345. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2346. expect(data.options).toEqual(expected);
  2347. });
  2348. it("should handle inherited option arrays (issue 8094)", async function () {
  2349. const options = [["Value1", "Description1"], ["Value2", "Description2"]];
  2350. const expected = [{
  2351. exportValue: "Value1",
  2352. displayValue: "Description1"
  2353. }, {
  2354. exportValue: "Value2",
  2355. displayValue: "Description2"
  2356. }];
  2357. const parentDict = new _primitives.Dict();
  2358. parentDict.set("Opt", options);
  2359. choiceWidgetDict.set("Parent", parentDict);
  2360. const choiceWidgetRef = _primitives.Ref.get(123, 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.options).toEqual(expected);
  2370. });
  2371. it("should decode form values", async function () {
  2372. const encodedString = "\xFE\xFF\x00F\x00o\x00o";
  2373. const decodedString = "Foo";
  2374. choiceWidgetDict.set("Opt", [encodedString]);
  2375. choiceWidgetDict.set("V", encodedString);
  2376. choiceWidgetDict.set("DV", _primitives.Name.get("foo"));
  2377. const choiceWidgetRef = _primitives.Ref.get(984, 0);
  2378. const xref = new _test_utils.XRefMock([{
  2379. ref: choiceWidgetRef,
  2380. data: choiceWidgetDict
  2381. }]);
  2382. const {
  2383. data
  2384. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2385. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2386. expect(data.fieldValue).toEqual([decodedString]);
  2387. expect(data.defaultFieldValue).toEqual("foo");
  2388. expect(data.options).toEqual([{
  2389. exportValue: decodedString,
  2390. displayValue: decodedString
  2391. }]);
  2392. });
  2393. it("should convert the field value to an array", async function () {
  2394. const inputs = [null, "Foo", ["Foo", "Bar"]];
  2395. const outputs = [[], ["Foo"], ["Foo", "Bar"]];
  2396. let promise = Promise.resolve();
  2397. for (let i = 0, ii = inputs.length; i < ii; i++) {
  2398. promise = promise.then(() => {
  2399. choiceWidgetDict.set("V", inputs[i]);
  2400. const choiceWidgetRef = _primitives.Ref.get(968, 0);
  2401. const xref = new _test_utils.XRefMock([{
  2402. ref: choiceWidgetRef,
  2403. data: choiceWidgetDict
  2404. }]);
  2405. return _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock).then(({
  2406. data
  2407. }) => {
  2408. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2409. expect(data.fieldValue).toEqual(outputs[i]);
  2410. });
  2411. });
  2412. }
  2413. await promise;
  2414. });
  2415. it("should handle unknown flags", async function () {
  2416. const choiceWidgetRef = _primitives.Ref.get(166, 0);
  2417. const xref = new _test_utils.XRefMock([{
  2418. ref: choiceWidgetRef,
  2419. data: choiceWidgetDict
  2420. }]);
  2421. const {
  2422. data
  2423. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2424. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2425. expect(data.readOnly).toEqual(false);
  2426. expect(data.hidden).toEqual(false);
  2427. expect(data.combo).toEqual(false);
  2428. expect(data.multiSelect).toEqual(false);
  2429. });
  2430. it("should not set invalid flags", async function () {
  2431. choiceWidgetDict.set("Ff", "readonly");
  2432. const choiceWidgetRef = _primitives.Ref.get(165, 0);
  2433. const xref = new _test_utils.XRefMock([{
  2434. ref: choiceWidgetRef,
  2435. data: choiceWidgetDict
  2436. }]);
  2437. const {
  2438. data
  2439. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2440. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2441. expect(data.readOnly).toEqual(false);
  2442. expect(data.hidden).toEqual(false);
  2443. expect(data.combo).toEqual(false);
  2444. expect(data.multiSelect).toEqual(false);
  2445. });
  2446. it("should set valid flags", async function () {
  2447. choiceWidgetDict.set("Ff", _util.AnnotationFieldFlag.READONLY + _util.AnnotationFieldFlag.COMBO + _util.AnnotationFieldFlag.MULTISELECT);
  2448. const choiceWidgetRef = _primitives.Ref.get(512, 0);
  2449. const xref = new _test_utils.XRefMock([{
  2450. ref: choiceWidgetRef,
  2451. data: choiceWidgetDict
  2452. }]);
  2453. const {
  2454. data
  2455. } = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2456. expect(data.annotationType).toEqual(_util.AnnotationType.WIDGET);
  2457. expect(data.readOnly).toEqual(true);
  2458. expect(data.hidden).toEqual(false);
  2459. expect(data.combo).toEqual(true);
  2460. expect(data.multiSelect).toEqual(true);
  2461. });
  2462. it("should render choice for printing", async function () {
  2463. const choiceWidgetRef = _primitives.Ref.get(271, 0);
  2464. const xref = new _test_utils.XRefMock([{
  2465. ref: choiceWidgetRef,
  2466. data: choiceWidgetDict
  2467. }, fontRefObj]);
  2468. const task = new _worker.WorkerTask("test print");
  2469. partialEvaluator.xref = xref;
  2470. const annotation = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2471. const annotationStorage = new Map();
  2472. annotationStorage.set(annotation.data.id, {
  2473. value: "a value"
  2474. });
  2475. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  2476. 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"));
  2477. });
  2478. it("should render choice with multiple selections but one is visible for printing", async function () {
  2479. choiceWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTISELECT);
  2480. choiceWidgetDict.set("Opt", [["A", "a"], ["B", "b"], ["C", "c"], ["D", "d"]]);
  2481. choiceWidgetDict.set("V", ["A"]);
  2482. const choiceWidgetRef = _primitives.Ref.get(271, 0);
  2483. const xref = new _test_utils.XRefMock([{
  2484. ref: choiceWidgetRef,
  2485. data: choiceWidgetDict
  2486. }, fontRefObj]);
  2487. const task = new _worker.WorkerTask("test print");
  2488. partialEvaluator.xref = xref;
  2489. const annotation = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2490. const annotationStorage = new Map();
  2491. annotationStorage.set(annotation.data.id, {
  2492. value: ["A", "C"]
  2493. });
  2494. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  2495. 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 -5.88 Td (a) Tj", "0 -6.75 Td (b) Tj", "ET Q EMC"].join("\n"));
  2496. });
  2497. it("should render choice with multiple selections for printing", async function () {
  2498. choiceWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTISELECT);
  2499. choiceWidgetDict.set("Opt", [["A", "a"], ["B", "b"], ["C", "c"], ["D", "d"]]);
  2500. choiceWidgetDict.set("V", ["A"]);
  2501. const choiceWidgetRef = _primitives.Ref.get(271, 0);
  2502. const xref = new _test_utils.XRefMock([{
  2503. ref: choiceWidgetRef,
  2504. data: choiceWidgetDict
  2505. }, fontRefObj]);
  2506. const task = new _worker.WorkerTask("test print");
  2507. partialEvaluator.xref = xref;
  2508. const annotation = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2509. const annotationStorage = new Map();
  2510. annotationStorage.set(annotation.data.id, {
  2511. value: ["B", "C"]
  2512. });
  2513. const appearance = await annotation._getAppearance(partialEvaluator, task, annotationStorage);
  2514. 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 -5.88 Td (b) Tj", "0 -6.75 Td (c) Tj", "ET Q EMC"].join("\n"));
  2515. });
  2516. it("should save rotated choice", async function () {
  2517. choiceWidgetDict.set("Opt", ["A", "B", "C"]);
  2518. choiceWidgetDict.set("V", "A");
  2519. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2520. const xref = new _test_utils.XRefMock([{
  2521. ref: choiceWidgetRef,
  2522. data: choiceWidgetDict
  2523. }, fontRefObj]);
  2524. partialEvaluator.xref = xref;
  2525. const task = new _worker.WorkerTask("test save");
  2526. const annotation = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2527. const annotationStorage = new Map();
  2528. annotationStorage.set(annotation.data.id, {
  2529. value: "C",
  2530. rotation: 270
  2531. });
  2532. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  2533. expect(data.length).toEqual(2);
  2534. const [oldData, newData] = data;
  2535. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  2536. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  2537. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  2538. 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) /MK << /R 270>>>>\nendobj\n");
  2539. expect(newData.data).toEqual(["2 0 obj", "<< /Length 170 /Subtype /Form /Resources << /Font << /Helv 314 0 R>>>> " + "/BBox [0 0 32 10] /Matrix [0 -1 1 0 0 10]>> stream", "/Tx BMC q", "1 1 10 32 re W n", "0.600006 0.756866 0.854904 rg", "1 11.75 10 6.75 re f", "BT", "/Helv 5 Tf", "1 0 0 1 0 32 Tm", "2 -5.88 Td (A) Tj", "0 -6.75 Td (B) Tj", "0 -6.75 Td (C) Tj", "ET Q EMC", "endstream", "endobj\n"].join("\n"));
  2540. });
  2541. it("should save choice", async function () {
  2542. choiceWidgetDict.set("Opt", ["A", "B", "C"]);
  2543. choiceWidgetDict.set("V", "A");
  2544. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2545. const xref = new _test_utils.XRefMock([{
  2546. ref: choiceWidgetRef,
  2547. data: choiceWidgetDict
  2548. }, fontRefObj]);
  2549. partialEvaluator.xref = xref;
  2550. const task = new _worker.WorkerTask("test save");
  2551. const annotation = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2552. const annotationStorage = new Map();
  2553. annotationStorage.set(annotation.data.id, {
  2554. value: "C"
  2555. });
  2556. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  2557. expect(data.length).toEqual(2);
  2558. const [oldData, newData] = data;
  2559. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  2560. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  2561. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  2562. 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");
  2563. expect(newData.data).toEqual(["2 0 obj", "<< /Length 133 /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 -5.88 Td (C) Tj", "ET Q EMC", "endstream", "endobj\n"].join("\n"));
  2564. });
  2565. it("should save choice with multiple selections", async function () {
  2566. choiceWidgetDict.set("Ff", _util.AnnotationFieldFlag.MULTISELECT);
  2567. choiceWidgetDict.set("Opt", [["A", "a"], ["B", "b"], ["C", "c"], ["D", "d"]]);
  2568. choiceWidgetDict.set("V", ["A"]);
  2569. const choiceWidgetRef = _primitives.Ref.get(123, 0);
  2570. const xref = new _test_utils.XRefMock([{
  2571. ref: choiceWidgetRef,
  2572. data: choiceWidgetDict
  2573. }, fontRefObj]);
  2574. const task = new _worker.WorkerTask("test save");
  2575. partialEvaluator.xref = xref;
  2576. const annotation = await _annotation.AnnotationFactory.create(xref, choiceWidgetRef, pdfManagerMock, idFactoryMock);
  2577. const annotationStorage = new Map();
  2578. annotationStorage.set(annotation.data.id, {
  2579. value: ["B", "C"]
  2580. });
  2581. const data = await annotation.save(partialEvaluator, task, annotationStorage);
  2582. expect(data.length).toEqual(2);
  2583. const [oldData, newData] = data;
  2584. expect(oldData.ref).toEqual(_primitives.Ref.get(123, 0));
  2585. expect(newData.ref).toEqual(_primitives.Ref.get(2, 0));
  2586. oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
  2587. 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");
  2588. expect(newData.data).toEqual(["2 0 obj", "<< /Length 171 /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 -5.88 Td (b) Tj", "0 -6.75 Td (c) Tj", "ET Q EMC", "endstream", "endobj\n"].join("\n"));
  2589. });
  2590. });
  2591. describe("LineAnnotation", function () {
  2592. it("should set the line coordinates", async function () {
  2593. const lineDict = new _primitives.Dict();
  2594. lineDict.set("Type", _primitives.Name.get("Annot"));
  2595. lineDict.set("Subtype", _primitives.Name.get("Line"));
  2596. lineDict.set("L", [1, 2, 3, 4]);
  2597. lineDict.set("LE", ["Square", "Circle"]);
  2598. const lineRef = _primitives.Ref.get(122, 0);
  2599. const xref = new _test_utils.XRefMock([{
  2600. ref: lineRef,
  2601. data: lineDict
  2602. }]);
  2603. const {
  2604. data
  2605. } = await _annotation.AnnotationFactory.create(xref, lineRef, pdfManagerMock, idFactoryMock);
  2606. expect(data.annotationType).toEqual(_util.AnnotationType.LINE);
  2607. expect(data.lineCoordinates).toEqual([1, 2, 3, 4]);
  2608. expect(data.lineEndings).toEqual(["None", "None"]);
  2609. });
  2610. it("should set the line endings", async function () {
  2611. const lineDict = new _primitives.Dict();
  2612. lineDict.set("Type", _primitives.Name.get("Annot"));
  2613. lineDict.set("Subtype", _primitives.Name.get("Line"));
  2614. lineDict.set("L", [1, 2, 3, 4]);
  2615. lineDict.set("LE", [_primitives.Name.get("Square"), _primitives.Name.get("Circle")]);
  2616. const lineRef = _primitives.Ref.get(122, 0);
  2617. const xref = new _test_utils.XRefMock([{
  2618. ref: lineRef,
  2619. data: lineDict
  2620. }]);
  2621. const {
  2622. data
  2623. } = await _annotation.AnnotationFactory.create(xref, lineRef, pdfManagerMock, idFactoryMock);
  2624. expect(data.annotationType).toEqual(_util.AnnotationType.LINE);
  2625. expect(data.lineCoordinates).toEqual([1, 2, 3, 4]);
  2626. expect(data.lineEndings).toEqual(["Square", "Circle"]);
  2627. });
  2628. });
  2629. describe("FileAttachmentAnnotation", function () {
  2630. it("should correctly parse a file attachment", async function () {
  2631. const fileStream = new _stream.StringStream("<<\n" + "/Type /EmbeddedFile\n" + "/Subtype /text#2Fplain\n" + ">>\n" + "stream\n" + "Test attachment" + "endstream\n");
  2632. const parser = new _parser.Parser({
  2633. lexer: new _parser.Lexer(fileStream),
  2634. xref: null,
  2635. allowStreams: true
  2636. });
  2637. const fileStreamRef = _primitives.Ref.get(18, 0);
  2638. const fileStreamDict = parser.getObj();
  2639. const embeddedFileDict = new _primitives.Dict();
  2640. embeddedFileDict.set("F", fileStreamRef);
  2641. const fileSpecRef = _primitives.Ref.get(19, 0);
  2642. const fileSpecDict = new _primitives.Dict();
  2643. fileSpecDict.set("Type", _primitives.Name.get("Filespec"));
  2644. fileSpecDict.set("Desc", "");
  2645. fileSpecDict.set("EF", embeddedFileDict);
  2646. fileSpecDict.set("UF", "Test.txt");
  2647. const fileAttachmentRef = _primitives.Ref.get(20, 0);
  2648. const fileAttachmentDict = new _primitives.Dict();
  2649. fileAttachmentDict.set("Type", _primitives.Name.get("Annot"));
  2650. fileAttachmentDict.set("Subtype", _primitives.Name.get("FileAttachment"));
  2651. fileAttachmentDict.set("FS", fileSpecRef);
  2652. fileAttachmentDict.set("T", "Topic");
  2653. fileAttachmentDict.set("Contents", "Test.txt");
  2654. const xref = new _test_utils.XRefMock([{
  2655. ref: fileStreamRef,
  2656. data: fileStreamDict
  2657. }, {
  2658. ref: fileSpecRef,
  2659. data: fileSpecDict
  2660. }, {
  2661. ref: fileAttachmentRef,
  2662. data: fileAttachmentDict
  2663. }]);
  2664. embeddedFileDict.assignXref(xref);
  2665. fileSpecDict.assignXref(xref);
  2666. fileAttachmentDict.assignXref(xref);
  2667. const {
  2668. data
  2669. } = await _annotation.AnnotationFactory.create(xref, fileAttachmentRef, pdfManagerMock, idFactoryMock);
  2670. expect(data.annotationType).toEqual(_util.AnnotationType.FILEATTACHMENT);
  2671. expect(data.file.filename).toEqual("Test.txt");
  2672. expect(data.file.content).toEqual((0, _util.stringToBytes)("Test attachment"));
  2673. });
  2674. });
  2675. describe("PopupAnnotation", function () {
  2676. it("should inherit properties from its parent", async function () {
  2677. const parentDict = new _primitives.Dict();
  2678. parentDict.set("Type", _primitives.Name.get("Annot"));
  2679. parentDict.set("Subtype", _primitives.Name.get("Text"));
  2680. parentDict.set("M", "D:20190423");
  2681. parentDict.set("C", [0, 0, 1]);
  2682. const popupDict = new _primitives.Dict();
  2683. popupDict.set("Type", _primitives.Name.get("Annot"));
  2684. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2685. popupDict.set("Parent", parentDict);
  2686. const popupRef = _primitives.Ref.get(13, 0);
  2687. const xref = new _test_utils.XRefMock([{
  2688. ref: popupRef,
  2689. data: popupDict
  2690. }]);
  2691. const {
  2692. data
  2693. } = await _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock);
  2694. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  2695. expect(data.modificationDate).toEqual("D:20190423");
  2696. expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  2697. });
  2698. it("should handle missing parent properties", async function () {
  2699. const parentDict = new _primitives.Dict();
  2700. parentDict.set("Type", _primitives.Name.get("Annot"));
  2701. parentDict.set("Subtype", _primitives.Name.get("Text"));
  2702. const popupDict = new _primitives.Dict();
  2703. popupDict.set("Type", _primitives.Name.get("Annot"));
  2704. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2705. popupDict.set("Parent", parentDict);
  2706. const popupRef = _primitives.Ref.get(13, 0);
  2707. const xref = new _test_utils.XRefMock([{
  2708. ref: popupRef,
  2709. data: popupDict
  2710. }]);
  2711. const {
  2712. data
  2713. } = await _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock);
  2714. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  2715. expect(data.modificationDate).toEqual(null);
  2716. expect(data.color).toEqual(null);
  2717. });
  2718. it("should inherit the parent flags when the Popup is not viewable, " + "but the parent is (PR 7352)", async function () {
  2719. const parentDict = new _primitives.Dict();
  2720. parentDict.set("Type", _primitives.Name.get("Annot"));
  2721. parentDict.set("Subtype", _primitives.Name.get("Text"));
  2722. parentDict.set("F", 28);
  2723. const popupDict = new _primitives.Dict();
  2724. popupDict.set("Type", _primitives.Name.get("Annot"));
  2725. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2726. popupDict.set("F", 25);
  2727. popupDict.set("Parent", parentDict);
  2728. const popupRef = _primitives.Ref.get(13, 0);
  2729. const xref = new _test_utils.XRefMock([{
  2730. ref: popupRef,
  2731. data: popupDict
  2732. }]);
  2733. const {
  2734. data,
  2735. viewable
  2736. } = await _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock);
  2737. expect(data.annotationType).toEqual(_util.AnnotationType.POPUP);
  2738. expect(data.annotationFlags).toEqual(25);
  2739. expect(viewable).toEqual(true);
  2740. });
  2741. it("should correctly inherit Contents from group-master annotation " + "if parent has ReplyType == Group", async function () {
  2742. const annotationRef = _primitives.Ref.get(819, 0);
  2743. const annotationDict = new _primitives.Dict();
  2744. annotationDict.set("Type", _primitives.Name.get("Annot"));
  2745. annotationDict.set("Subtype", _primitives.Name.get("Text"));
  2746. annotationDict.set("T", "Correct Title");
  2747. annotationDict.set("Contents", "Correct Text");
  2748. annotationDict.set("M", "D:20190423");
  2749. annotationDict.set("C", [0, 0, 1]);
  2750. const replyRef = _primitives.Ref.get(820, 0);
  2751. const replyDict = new _primitives.Dict();
  2752. replyDict.set("Type", _primitives.Name.get("Annot"));
  2753. replyDict.set("Subtype", _primitives.Name.get("Text"));
  2754. replyDict.set("IRT", annotationRef);
  2755. replyDict.set("RT", _primitives.Name.get("Group"));
  2756. replyDict.set("T", "Reply Title");
  2757. replyDict.set("Contents", "Reply Text");
  2758. replyDict.set("M", "D:20190523");
  2759. replyDict.set("C", [0.4]);
  2760. const popupRef = _primitives.Ref.get(821, 0);
  2761. const popupDict = new _primitives.Dict();
  2762. popupDict.set("Type", _primitives.Name.get("Annot"));
  2763. popupDict.set("Subtype", _primitives.Name.get("Popup"));
  2764. popupDict.set("T", "Wrong Title");
  2765. popupDict.set("Contents", "Wrong Text");
  2766. popupDict.set("Parent", replyRef);
  2767. popupDict.set("M", "D:20190623");
  2768. popupDict.set("C", [0.8]);
  2769. replyDict.set("Popup", popupRef);
  2770. const xref = new _test_utils.XRefMock([{
  2771. ref: annotationRef,
  2772. data: annotationDict
  2773. }, {
  2774. ref: replyRef,
  2775. data: replyDict
  2776. }, {
  2777. ref: popupRef,
  2778. data: popupDict
  2779. }]);
  2780. annotationDict.assignXref(xref);
  2781. popupDict.assignXref(xref);
  2782. replyDict.assignXref(xref);
  2783. const {
  2784. data
  2785. } = await _annotation.AnnotationFactory.create(xref, popupRef, pdfManagerMock, idFactoryMock);
  2786. expect(data.titleObj).toEqual({
  2787. str: "Correct Title",
  2788. dir: "ltr"
  2789. });
  2790. expect(data.contentsObj).toEqual({
  2791. str: "Correct Text",
  2792. dir: "ltr"
  2793. });
  2794. expect(data.modificationDate).toEqual("D:20190423");
  2795. expect(data.color).toEqual(new Uint8ClampedArray([0, 0, 255]));
  2796. });
  2797. });
  2798. describe("FreeTextAnnotation", () => {
  2799. it("should create a new FreeText annotation", async () => {
  2800. partialEvaluator.xref = new _test_utils.XRefMock();
  2801. const task = new _worker.WorkerTask("test FreeText creation");
  2802. const data = await _annotation.AnnotationFactory.saveNewAnnotations(partialEvaluator, task, [{
  2803. annotationType: _util.AnnotationEditorType.FREETEXT,
  2804. rect: [12, 34, 56, 78],
  2805. rotation: 0,
  2806. fontSize: 10,
  2807. color: [0, 0, 0],
  2808. value: "Hello PDF.js World!"
  2809. }]);
  2810. const base = data.annotations[0].data.replace(/\(D:\d+\)/, "(date)");
  2811. expect(base).toEqual("2 0 obj\n" + "<< /Type /Annot /Subtype /FreeText /CreationDate (date) " + "/Rect [12 34 56 78] /DA (/Helv 10 Tf 0 g) /Contents (Hello PDF.js World!) " + "/F 4 /Border [0 0 0] /Rotate 0 /AP << /N 3 0 R>>>>\n" + "endobj\n");
  2812. const font = data.dependencies[0].data;
  2813. expect(font).toEqual("1 0 obj\n" + "<< /BaseFont /Helvetica /Type /Font /Subtype /Type1 /Encoding " + "/WinAnsiEncoding>>\n" + "endobj\n");
  2814. const appearance = data.dependencies[1].data;
  2815. expect(appearance).toEqual("3 0 obj\n" + "<< /FormType 1 /Subtype /Form /Type /XObject /BBox [0 0 44 44] " + "/Length 101 /Resources << /Font << /Helv 1 0 R>>>>>> stream\n" + "q\n" + "0 0 44 44 re W n\n" + "BT\n" + "1 0 0 1 0 47.5 Tm 0 Tc 0 g\n" + "/Helv 10 Tf\n" + "0 -13.5 Td (Hello PDF.js World!) Tj\n" + "ET\n" + "Q\n" + "endstream\n" + "\n" + "endobj\n");
  2816. });
  2817. it("should render an added FreeText annotation for printing", async function () {
  2818. partialEvaluator.xref = new _test_utils.XRefMock();
  2819. const task = new _worker.WorkerTask("test FreeText printing");
  2820. const freetextAnnotation = (await _annotation.AnnotationFactory.printNewAnnotations(partialEvaluator, task, [{
  2821. annotationType: _util.AnnotationEditorType.FREETEXT,
  2822. rect: [12, 34, 56, 78],
  2823. rotation: 0,
  2824. fontSize: 10,
  2825. color: [0, 0, 0],
  2826. value: "A"
  2827. }]))[0];
  2828. const {
  2829. opList
  2830. } = await freetextAnnotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, null);
  2831. expect(opList.fnArray.length).toEqual(16);
  2832. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.save, _util.OPS.constructPath, _util.OPS.clip, _util.OPS.endPath, _util.OPS.beginText, _util.OPS.setTextMatrix, _util.OPS.setCharSpacing, _util.OPS.setFillRGBColor, _util.OPS.dependency, _util.OPS.setFont, _util.OPS.moveText, _util.OPS.showText, _util.OPS.endText, _util.OPS.restore, _util.OPS.endAnnotation]);
  2833. });
  2834. });
  2835. describe("InkAnnotation", function () {
  2836. it("should handle a single ink list", async function () {
  2837. const inkDict = new _primitives.Dict();
  2838. inkDict.set("Type", _primitives.Name.get("Annot"));
  2839. inkDict.set("Subtype", _primitives.Name.get("Ink"));
  2840. inkDict.set("InkList", [[1, 1, 1, 2, 2, 2, 3, 3]]);
  2841. const inkRef = _primitives.Ref.get(142, 0);
  2842. const xref = new _test_utils.XRefMock([{
  2843. ref: inkRef,
  2844. data: inkDict
  2845. }]);
  2846. const {
  2847. data
  2848. } = await _annotation.AnnotationFactory.create(xref, inkRef, pdfManagerMock, idFactoryMock);
  2849. expect(data.annotationType).toEqual(_util.AnnotationType.INK);
  2850. expect(data.inkLists.length).toEqual(1);
  2851. expect(data.inkLists[0]).toEqual([{
  2852. x: 1,
  2853. y: 1
  2854. }, {
  2855. x: 1,
  2856. y: 2
  2857. }, {
  2858. x: 2,
  2859. y: 2
  2860. }, {
  2861. x: 3,
  2862. y: 3
  2863. }]);
  2864. });
  2865. it("should handle multiple ink lists", async function () {
  2866. const inkDict = new _primitives.Dict();
  2867. inkDict.set("Type", _primitives.Name.get("Annot"));
  2868. inkDict.set("Subtype", _primitives.Name.get("Ink"));
  2869. inkDict.set("InkList", [[1, 1, 1, 2], [3, 3, 4, 5]]);
  2870. const inkRef = _primitives.Ref.get(143, 0);
  2871. const xref = new _test_utils.XRefMock([{
  2872. ref: inkRef,
  2873. data: inkDict
  2874. }]);
  2875. const {
  2876. data
  2877. } = await _annotation.AnnotationFactory.create(xref, inkRef, pdfManagerMock, idFactoryMock);
  2878. expect(data.annotationType).toEqual(_util.AnnotationType.INK);
  2879. expect(data.inkLists.length).toEqual(2);
  2880. expect(data.inkLists[0]).toEqual([{
  2881. x: 1,
  2882. y: 1
  2883. }, {
  2884. x: 1,
  2885. y: 2
  2886. }]);
  2887. expect(data.inkLists[1]).toEqual([{
  2888. x: 3,
  2889. y: 3
  2890. }, {
  2891. x: 4,
  2892. y: 5
  2893. }]);
  2894. });
  2895. it("should create a new Ink annotation", async function () {
  2896. partialEvaluator.xref = new _test_utils.XRefMock();
  2897. const task = new _worker.WorkerTask("test Ink creation");
  2898. const data = await _annotation.AnnotationFactory.saveNewAnnotations(partialEvaluator, task, [{
  2899. annotationType: _util.AnnotationEditorType.INK,
  2900. rect: [12, 34, 56, 78],
  2901. rotation: 0,
  2902. thickness: 1,
  2903. opacity: 1,
  2904. color: [0, 0, 0],
  2905. paths: [{
  2906. bezier: [10, 11, 12, 13, 14, 15, 16, 17, 22, 23, 24, 25, 26, 27],
  2907. points: [1, 2, 3, 4, 5, 6, 7, 8]
  2908. }, {
  2909. bezier: [910, 911, 912, 913, 914, 915, 916, 917, 922, 923, 924, 925, 926, 927],
  2910. points: [91, 92, 93, 94, 95, 96, 97, 98]
  2911. }]
  2912. }]);
  2913. const base = data.annotations[0].data.replace(/\(D:\d+\)/, "(date)");
  2914. expect(base).toEqual("1 0 obj\n" + "<< /Type /Annot /Subtype /Ink /CreationDate (date) /Rect [12 34 56 78] " + "/InkList [[1 2 3 4 5 6 7 8] [91 92 93 94 95 96 97 98]] /F 4 /Border [0 0 0] " + "/Rotate 0 /AP << /N 2 0 R>>>>\n" + "endobj\n");
  2915. const appearance = data.dependencies[0].data;
  2916. expect(appearance).toEqual("2 0 obj\n" + "<< /FormType 1 /Subtype /Form /Type /XObject /BBox [0 0 44 44] /Length 129>> stream\n" + "1 w 1 J 1 j\n" + "0 G\n" + "10 11 m\n" + "12 13 14 15 16 17 c\n" + "22 23 24 25 26 27 c\n" + "S\n" + "910 911 m\n" + "912 913 914 915 916 917 c\n" + "922 923 924 925 926 927 c\n" + "S\n" + "endstream\n" + "\n" + "endobj\n");
  2917. });
  2918. it("should create a new Ink annotation with some transparency", async function () {
  2919. partialEvaluator.xref = new _test_utils.XRefMock();
  2920. const task = new _worker.WorkerTask("test Ink creation");
  2921. const data = await _annotation.AnnotationFactory.saveNewAnnotations(partialEvaluator, task, [{
  2922. annotationType: _util.AnnotationEditorType.INK,
  2923. rect: [12, 34, 56, 78],
  2924. rotation: 0,
  2925. thickness: 1,
  2926. opacity: 0.12,
  2927. color: [0, 0, 0],
  2928. paths: [{
  2929. bezier: [10, 11, 12, 13, 14, 15, 16, 17, 22, 23, 24, 25, 26, 27],
  2930. points: [1, 2, 3, 4, 5, 6, 7, 8]
  2931. }, {
  2932. bezier: [910, 911, 912, 913, 914, 915, 916, 917, 922, 923, 924, 925, 926, 927],
  2933. points: [91, 92, 93, 94, 95, 96, 97, 98]
  2934. }]
  2935. }]);
  2936. const base = data.annotations[0].data.replace(/\(D:\d+\)/, "(date)");
  2937. expect(base).toEqual("1 0 obj\n" + "<< /Type /Annot /Subtype /Ink /CreationDate (date) /Rect [12 34 56 78] " + "/InkList [[1 2 3 4 5 6 7 8] [91 92 93 94 95 96 97 98]] /F 4 /Border [0 0 0] " + "/Rotate 0 /AP << /N 2 0 R>>>>\n" + "endobj\n");
  2938. const appearance = data.dependencies[0].data;
  2939. expect(appearance).toEqual("2 0 obj\n" + "<< /FormType 1 /Subtype /Form /Type /XObject /BBox [0 0 44 44] /Length 136 /Resources " + "<< /ExtGState << /R0 << /CA 0.12 /Type /ExtGState>>>>>>>> stream\n" + "1 w 1 J 1 j\n" + "0 G\n" + "/R0 gs\n" + "10 11 m\n" + "12 13 14 15 16 17 c\n" + "22 23 24 25 26 27 c\n" + "S\n" + "910 911 m\n" + "912 913 914 915 916 917 c\n" + "922 923 924 925 926 927 c\n" + "S\n" + "endstream\n" + "\n" + "endobj\n");
  2940. });
  2941. it("should render an added Ink annotation for printing", async function () {
  2942. partialEvaluator.xref = new _test_utils.XRefMock();
  2943. const task = new _worker.WorkerTask("test Ink printing");
  2944. const inkAnnotation = (await _annotation.AnnotationFactory.printNewAnnotations(partialEvaluator, task, [{
  2945. annotationType: _util.AnnotationEditorType.INK,
  2946. rect: [12, 34, 56, 78],
  2947. rotation: 0,
  2948. thickness: 3,
  2949. opacity: 1,
  2950. color: [0, 255, 0],
  2951. paths: [{
  2952. bezier: [1, 2, 3, 4, 5, 6, 7, 8],
  2953. points: [1, 2, 3, 4, 5, 6, 7, 8]
  2954. }]
  2955. }]))[0];
  2956. const {
  2957. opList
  2958. } = await inkAnnotation.getOperatorList(partialEvaluator, task, _util.RenderingIntentFlag.PRINT, false, null);
  2959. expect(opList.argsArray.length).toEqual(8);
  2960. expect(opList.fnArray).toEqual([_util.OPS.beginAnnotation, _util.OPS.setLineWidth, _util.OPS.setLineCap, _util.OPS.setLineJoin, _util.OPS.setStrokeRGBColor, _util.OPS.constructPath, _util.OPS.stroke, _util.OPS.endAnnotation]);
  2961. expect(opList.argsArray[1]).toEqual([3]);
  2962. expect(opList.argsArray[2]).toEqual([1]);
  2963. expect(opList.argsArray[3]).toEqual([1]);
  2964. expect(opList.argsArray[4]).toEqual(new Uint8ClampedArray([0, 255, 0]));
  2965. expect(opList.argsArray[5][0]).toEqual([_util.OPS.moveTo, _util.OPS.curveTo]);
  2966. expect(opList.argsArray[5][1]).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
  2967. expect(opList.argsArray[5][2]).toEqual([1, 1, 2, 2]);
  2968. });
  2969. });
  2970. describe("HightlightAnnotation", function () {
  2971. it("should set quadpoints to null if not defined", async function () {
  2972. const highlightDict = new _primitives.Dict();
  2973. highlightDict.set("Type", _primitives.Name.get("Annot"));
  2974. highlightDict.set("Subtype", _primitives.Name.get("Highlight"));
  2975. const highlightRef = _primitives.Ref.get(121, 0);
  2976. const xref = new _test_utils.XRefMock([{
  2977. ref: highlightRef,
  2978. data: highlightDict
  2979. }]);
  2980. const {
  2981. data
  2982. } = await _annotation.AnnotationFactory.create(xref, highlightRef, pdfManagerMock, idFactoryMock);
  2983. expect(data.annotationType).toEqual(_util.AnnotationType.HIGHLIGHT);
  2984. expect(data.quadPoints).toEqual(null);
  2985. });
  2986. it("should set quadpoints if defined", async function () {
  2987. const highlightDict = new _primitives.Dict();
  2988. highlightDict.set("Type", _primitives.Name.get("Annot"));
  2989. highlightDict.set("Subtype", _primitives.Name.get("Highlight"));
  2990. highlightDict.set("Rect", [10, 10, 20, 20]);
  2991. highlightDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  2992. const highlightRef = _primitives.Ref.get(121, 0);
  2993. const xref = new _test_utils.XRefMock([{
  2994. ref: highlightRef,
  2995. data: highlightDict
  2996. }]);
  2997. const {
  2998. data
  2999. } = await _annotation.AnnotationFactory.create(xref, highlightRef, pdfManagerMock, idFactoryMock);
  3000. expect(data.annotationType).toEqual(_util.AnnotationType.HIGHLIGHT);
  3001. expect(data.quadPoints).toEqual([[{
  3002. x: 10,
  3003. y: 20
  3004. }, {
  3005. x: 20,
  3006. y: 20
  3007. }, {
  3008. x: 10,
  3009. y: 10
  3010. }, {
  3011. x: 20,
  3012. y: 10
  3013. }]]);
  3014. });
  3015. it("should set quadpoints to null when empty", async function () {
  3016. const highlightDict = new _primitives.Dict();
  3017. highlightDict.set("Type", _primitives.Name.get("Annot"));
  3018. highlightDict.set("Subtype", _primitives.Name.get("Highlight"));
  3019. highlightDict.set("Rect", [10, 10, 20, 20]);
  3020. highlightDict.set("QuadPoints", []);
  3021. const highlightRef = _primitives.Ref.get(121, 0);
  3022. const xref = new _test_utils.XRefMock([{
  3023. ref: highlightRef,
  3024. data: highlightDict
  3025. }]);
  3026. const {
  3027. data
  3028. } = await _annotation.AnnotationFactory.create(xref, highlightRef, pdfManagerMock, idFactoryMock);
  3029. expect(data.annotationType).toEqual(_util.AnnotationType.HIGHLIGHT);
  3030. expect(data.quadPoints).toEqual(null);
  3031. });
  3032. });
  3033. describe("UnderlineAnnotation", function () {
  3034. it("should set quadpoints to null if not defined", async function () {
  3035. const underlineDict = new _primitives.Dict();
  3036. underlineDict.set("Type", _primitives.Name.get("Annot"));
  3037. underlineDict.set("Subtype", _primitives.Name.get("Underline"));
  3038. const underlineRef = _primitives.Ref.get(121, 0);
  3039. const xref = new _test_utils.XRefMock([{
  3040. ref: underlineRef,
  3041. data: underlineDict
  3042. }]);
  3043. const {
  3044. data
  3045. } = await _annotation.AnnotationFactory.create(xref, underlineRef, pdfManagerMock, idFactoryMock);
  3046. expect(data.annotationType).toEqual(_util.AnnotationType.UNDERLINE);
  3047. expect(data.quadPoints).toEqual(null);
  3048. });
  3049. it("should set quadpoints if defined", async function () {
  3050. const underlineDict = new _primitives.Dict();
  3051. underlineDict.set("Type", _primitives.Name.get("Annot"));
  3052. underlineDict.set("Subtype", _primitives.Name.get("Underline"));
  3053. underlineDict.set("Rect", [10, 10, 20, 20]);
  3054. underlineDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  3055. const underlineRef = _primitives.Ref.get(121, 0);
  3056. const xref = new _test_utils.XRefMock([{
  3057. ref: underlineRef,
  3058. data: underlineDict
  3059. }]);
  3060. const {
  3061. data
  3062. } = await _annotation.AnnotationFactory.create(xref, underlineRef, pdfManagerMock, idFactoryMock);
  3063. expect(data.annotationType).toEqual(_util.AnnotationType.UNDERLINE);
  3064. expect(data.quadPoints).toEqual([[{
  3065. x: 10,
  3066. y: 20
  3067. }, {
  3068. x: 20,
  3069. y: 20
  3070. }, {
  3071. x: 10,
  3072. y: 10
  3073. }, {
  3074. x: 20,
  3075. y: 10
  3076. }]]);
  3077. });
  3078. });
  3079. describe("SquigglyAnnotation", function () {
  3080. it("should set quadpoints to null if not defined", async function () {
  3081. const squigglyDict = new _primitives.Dict();
  3082. squigglyDict.set("Type", _primitives.Name.get("Annot"));
  3083. squigglyDict.set("Subtype", _primitives.Name.get("Squiggly"));
  3084. const squigglyRef = _primitives.Ref.get(121, 0);
  3085. const xref = new _test_utils.XRefMock([{
  3086. ref: squigglyRef,
  3087. data: squigglyDict
  3088. }]);
  3089. const {
  3090. data
  3091. } = await _annotation.AnnotationFactory.create(xref, squigglyRef, pdfManagerMock, idFactoryMock);
  3092. expect(data.annotationType).toEqual(_util.AnnotationType.SQUIGGLY);
  3093. expect(data.quadPoints).toEqual(null);
  3094. });
  3095. it("should set quadpoints if defined", async function () {
  3096. const squigglyDict = new _primitives.Dict();
  3097. squigglyDict.set("Type", _primitives.Name.get("Annot"));
  3098. squigglyDict.set("Subtype", _primitives.Name.get("Squiggly"));
  3099. squigglyDict.set("Rect", [10, 10, 20, 20]);
  3100. squigglyDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  3101. const squigglyRef = _primitives.Ref.get(121, 0);
  3102. const xref = new _test_utils.XRefMock([{
  3103. ref: squigglyRef,
  3104. data: squigglyDict
  3105. }]);
  3106. const {
  3107. data
  3108. } = await _annotation.AnnotationFactory.create(xref, squigglyRef, pdfManagerMock, idFactoryMock);
  3109. expect(data.annotationType).toEqual(_util.AnnotationType.SQUIGGLY);
  3110. expect(data.quadPoints).toEqual([[{
  3111. x: 10,
  3112. y: 20
  3113. }, {
  3114. x: 20,
  3115. y: 20
  3116. }, {
  3117. x: 10,
  3118. y: 10
  3119. }, {
  3120. x: 20,
  3121. y: 10
  3122. }]]);
  3123. });
  3124. });
  3125. describe("StrikeOutAnnotation", function () {
  3126. it("should set quadpoints to null if not defined", async function () {
  3127. const strikeOutDict = new _primitives.Dict();
  3128. strikeOutDict.set("Type", _primitives.Name.get("Annot"));
  3129. strikeOutDict.set("Subtype", _primitives.Name.get("StrikeOut"));
  3130. const strikeOutRef = _primitives.Ref.get(121, 0);
  3131. const xref = new _test_utils.XRefMock([{
  3132. ref: strikeOutRef,
  3133. data: strikeOutDict
  3134. }]);
  3135. const {
  3136. data
  3137. } = await _annotation.AnnotationFactory.create(xref, strikeOutRef, pdfManagerMock, idFactoryMock);
  3138. expect(data.annotationType).toEqual(_util.AnnotationType.STRIKEOUT);
  3139. expect(data.quadPoints).toEqual(null);
  3140. });
  3141. it("should set quadpoints if defined", async function () {
  3142. const strikeOutDict = new _primitives.Dict();
  3143. strikeOutDict.set("Type", _primitives.Name.get("Annot"));
  3144. strikeOutDict.set("Subtype", _primitives.Name.get("StrikeOut"));
  3145. strikeOutDict.set("Rect", [10, 10, 20, 20]);
  3146. strikeOutDict.set("QuadPoints", [10, 20, 20, 20, 10, 10, 20, 10]);
  3147. const strikeOutRef = _primitives.Ref.get(121, 0);
  3148. const xref = new _test_utils.XRefMock([{
  3149. ref: strikeOutRef,
  3150. data: strikeOutDict
  3151. }]);
  3152. const {
  3153. data
  3154. } = await _annotation.AnnotationFactory.create(xref, strikeOutRef, pdfManagerMock, idFactoryMock);
  3155. expect(data.annotationType).toEqual(_util.AnnotationType.STRIKEOUT);
  3156. expect(data.quadPoints).toEqual([[{
  3157. x: 10,
  3158. y: 20
  3159. }, {
  3160. x: 20,
  3161. y: 20
  3162. }, {
  3163. x: 10,
  3164. y: 10
  3165. }, {
  3166. x: 20,
  3167. y: 10
  3168. }]]);
  3169. });
  3170. });
  3171. });