2
0

annotation_spec.js 130 KB

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