annotation_spec.js 129 KB

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