annotation_spec.js 103 KB

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