annotation_spec.js 129 KB

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