annotation_spec.js 133 KB

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