annotation_spec.js 148 KB

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