annotation_spec.js 148 KB

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