annotation_spec.js 147 KB

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