pdf.mjs 675 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2024 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. /******/ // The require scope
  23. /******/ var __webpack_require__ = {};
  24. /******/
  25. /************************************************************************/
  26. /******/ /* webpack/runtime/define property getters */
  27. /******/ (() => {
  28. /******/ // define getter functions for harmony exports
  29. /******/ __webpack_require__.d = (exports, definition) => {
  30. /******/ for(var key in definition) {
  31. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  32. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  33. /******/ }
  34. /******/ }
  35. /******/ };
  36. /******/ })();
  37. /******/
  38. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  39. /******/ (() => {
  40. /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
  41. /******/ })();
  42. /******/
  43. /************************************************************************/
  44. var __webpack_exports__ = globalThis.pdfjsLib = {};
  45. // EXPORTS
  46. __webpack_require__.d(__webpack_exports__, {
  47. AbortException: () => (/* reexport */ AbortException),
  48. AnnotationEditorLayer: () => (/* reexport */ AnnotationEditorLayer),
  49. AnnotationEditorParamsType: () => (/* reexport */ AnnotationEditorParamsType),
  50. AnnotationEditorType: () => (/* reexport */ AnnotationEditorType),
  51. AnnotationEditorUIManager: () => (/* reexport */ AnnotationEditorUIManager),
  52. AnnotationLayer: () => (/* reexport */ AnnotationLayer),
  53. AnnotationMode: () => (/* reexport */ AnnotationMode),
  54. AnnotationType: () => (/* reexport */ AnnotationType),
  55. ColorPicker: () => (/* reexport */ ColorPicker),
  56. DOMSVGFactory: () => (/* reexport */ DOMSVGFactory),
  57. DrawLayer: () => (/* reexport */ DrawLayer),
  58. FeatureTest: () => (/* reexport */ util_FeatureTest),
  59. GlobalWorkerOptions: () => (/* reexport */ GlobalWorkerOptions),
  60. ImageKind: () => (/* reexport */ util_ImageKind),
  61. InvalidPDFException: () => (/* reexport */ InvalidPDFException),
  62. MathClamp: () => (/* reexport */ MathClamp),
  63. OPS: () => (/* reexport */ OPS),
  64. OutputScale: () => (/* reexport */ OutputScale),
  65. PDFDataRangeTransport: () => (/* reexport */ PDFDataRangeTransport),
  66. PDFDateString: () => (/* reexport */ PDFDateString),
  67. PDFWorker: () => (/* reexport */ PDFWorker),
  68. PasswordResponses: () => (/* reexport */ PasswordResponses),
  69. PermissionFlag: () => (/* reexport */ PermissionFlag),
  70. PixelsPerInch: () => (/* reexport */ PixelsPerInch),
  71. RenderingCancelledException: () => (/* reexport */ RenderingCancelledException),
  72. ResponseException: () => (/* reexport */ ResponseException),
  73. SignatureExtractor: () => (/* reexport */ SignatureExtractor),
  74. SupportedImageMimeTypes: () => (/* reexport */ SupportedImageMimeTypes),
  75. TextLayer: () => (/* reexport */ TextLayer),
  76. TouchManager: () => (/* reexport */ TouchManager),
  77. Util: () => (/* reexport */ Util),
  78. VerbosityLevel: () => (/* reexport */ VerbosityLevel),
  79. XfaLayer: () => (/* reexport */ XfaLayer),
  80. build: () => (/* reexport */ build),
  81. createValidAbsoluteUrl: () => (/* reexport */ createValidAbsoluteUrl),
  82. fetchData: () => (/* reexport */ fetchData),
  83. getDocument: () => (/* reexport */ getDocument),
  84. getFilenameFromUrl: () => (/* reexport */ getFilenameFromUrl),
  85. getPdfFilenameFromUrl: () => (/* reexport */ getPdfFilenameFromUrl),
  86. getUuid: () => (/* reexport */ getUuid),
  87. getXfaPageViewport: () => (/* reexport */ getXfaPageViewport),
  88. isDataScheme: () => (/* reexport */ isDataScheme),
  89. isPdfFile: () => (/* reexport */ isPdfFile),
  90. isValidExplicitDest: () => (/* reexport */ isValidExplicitDest),
  91. noContextMenu: () => (/* reexport */ noContextMenu),
  92. normalizeUnicode: () => (/* reexport */ normalizeUnicode),
  93. setLayerDimensions: () => (/* reexport */ setLayerDimensions),
  94. shadow: () => (/* reexport */ shadow),
  95. stopEvent: () => (/* reexport */ stopEvent),
  96. version: () => (/* reexport */ version)
  97. });
  98. ;// ./src/shared/util.js
  99. const isNodeJS = typeof process === "object" && process + "" === "[object process]" && !process.versions.nw && !(process.versions.electron && process.type && process.type !== "browser");
  100. const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  101. const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
  102. const LINE_FACTOR = 1.35;
  103. const LINE_DESCENT_FACTOR = 0.35;
  104. const BASELINE_FACTOR = LINE_DESCENT_FACTOR / LINE_FACTOR;
  105. const RenderingIntentFlag = {
  106. ANY: 0x01,
  107. DISPLAY: 0x02,
  108. PRINT: 0x04,
  109. SAVE: 0x08,
  110. ANNOTATIONS_FORMS: 0x10,
  111. ANNOTATIONS_STORAGE: 0x20,
  112. ANNOTATIONS_DISABLE: 0x40,
  113. IS_EDITING: 0x80,
  114. OPLIST: 0x100
  115. };
  116. const AnnotationMode = {
  117. DISABLE: 0,
  118. ENABLE: 1,
  119. ENABLE_FORMS: 2,
  120. ENABLE_STORAGE: 3
  121. };
  122. const AnnotationEditorPrefix = "pdfjs_internal_editor_";
  123. const AnnotationEditorType = {
  124. DISABLE: -1,
  125. NONE: 0,
  126. FREETEXT: 3,
  127. HIGHLIGHT: 9,
  128. STAMP: 13,
  129. INK: 15,
  130. SIGNATURE: 101
  131. };
  132. const AnnotationEditorParamsType = {
  133. RESIZE: 1,
  134. CREATE: 2,
  135. FREETEXT_SIZE: 11,
  136. FREETEXT_COLOR: 12,
  137. FREETEXT_OPACITY: 13,
  138. INK_COLOR: 21,
  139. INK_THICKNESS: 22,
  140. INK_OPACITY: 23,
  141. HIGHLIGHT_COLOR: 31,
  142. HIGHLIGHT_DEFAULT_COLOR: 32,
  143. HIGHLIGHT_THICKNESS: 33,
  144. HIGHLIGHT_FREE: 34,
  145. HIGHLIGHT_SHOW_ALL: 35,
  146. DRAW_STEP: 41
  147. };
  148. const PermissionFlag = {
  149. PRINT: 0x04,
  150. MODIFY_CONTENTS: 0x08,
  151. COPY: 0x10,
  152. MODIFY_ANNOTATIONS: 0x20,
  153. FILL_INTERACTIVE_FORMS: 0x100,
  154. COPY_FOR_ACCESSIBILITY: 0x200,
  155. ASSEMBLE: 0x400,
  156. PRINT_HIGH_QUALITY: 0x800
  157. };
  158. const TextRenderingMode = {
  159. FILL: 0,
  160. STROKE: 1,
  161. FILL_STROKE: 2,
  162. INVISIBLE: 3,
  163. FILL_ADD_TO_PATH: 4,
  164. STROKE_ADD_TO_PATH: 5,
  165. FILL_STROKE_ADD_TO_PATH: 6,
  166. ADD_TO_PATH: 7,
  167. FILL_STROKE_MASK: 3,
  168. ADD_TO_PATH_FLAG: 4
  169. };
  170. const util_ImageKind = {
  171. GRAYSCALE_1BPP: 1,
  172. RGB_24BPP: 2,
  173. RGBA_32BPP: 3
  174. };
  175. const AnnotationType = {
  176. TEXT: 1,
  177. LINK: 2,
  178. FREETEXT: 3,
  179. LINE: 4,
  180. SQUARE: 5,
  181. CIRCLE: 6,
  182. POLYGON: 7,
  183. POLYLINE: 8,
  184. HIGHLIGHT: 9,
  185. UNDERLINE: 10,
  186. SQUIGGLY: 11,
  187. STRIKEOUT: 12,
  188. STAMP: 13,
  189. CARET: 14,
  190. INK: 15,
  191. POPUP: 16,
  192. FILEATTACHMENT: 17,
  193. SOUND: 18,
  194. MOVIE: 19,
  195. WIDGET: 20,
  196. SCREEN: 21,
  197. PRINTERMARK: 22,
  198. TRAPNET: 23,
  199. WATERMARK: 24,
  200. THREED: 25,
  201. REDACT: 26
  202. };
  203. const AnnotationReplyType = {
  204. GROUP: "Group",
  205. REPLY: "R"
  206. };
  207. const AnnotationFlag = {
  208. INVISIBLE: 0x01,
  209. HIDDEN: 0x02,
  210. PRINT: 0x04,
  211. NOZOOM: 0x08,
  212. NOROTATE: 0x10,
  213. NOVIEW: 0x20,
  214. READONLY: 0x40,
  215. LOCKED: 0x80,
  216. TOGGLENOVIEW: 0x100,
  217. LOCKEDCONTENTS: 0x200
  218. };
  219. const AnnotationFieldFlag = {
  220. READONLY: 0x0000001,
  221. REQUIRED: 0x0000002,
  222. NOEXPORT: 0x0000004,
  223. MULTILINE: 0x0001000,
  224. PASSWORD: 0x0002000,
  225. NOTOGGLETOOFF: 0x0004000,
  226. RADIO: 0x0008000,
  227. PUSHBUTTON: 0x0010000,
  228. COMBO: 0x0020000,
  229. EDIT: 0x0040000,
  230. SORT: 0x0080000,
  231. FILESELECT: 0x0100000,
  232. MULTISELECT: 0x0200000,
  233. DONOTSPELLCHECK: 0x0400000,
  234. DONOTSCROLL: 0x0800000,
  235. COMB: 0x1000000,
  236. RICHTEXT: 0x2000000,
  237. RADIOSINUNISON: 0x2000000,
  238. COMMITONSELCHANGE: 0x4000000
  239. };
  240. const AnnotationBorderStyleType = {
  241. SOLID: 1,
  242. DASHED: 2,
  243. BEVELED: 3,
  244. INSET: 4,
  245. UNDERLINE: 5
  246. };
  247. const AnnotationActionEventType = {
  248. E: "Mouse Enter",
  249. X: "Mouse Exit",
  250. D: "Mouse Down",
  251. U: "Mouse Up",
  252. Fo: "Focus",
  253. Bl: "Blur",
  254. PO: "PageOpen",
  255. PC: "PageClose",
  256. PV: "PageVisible",
  257. PI: "PageInvisible",
  258. K: "Keystroke",
  259. F: "Format",
  260. V: "Validate",
  261. C: "Calculate"
  262. };
  263. const DocumentActionEventType = {
  264. WC: "WillClose",
  265. WS: "WillSave",
  266. DS: "DidSave",
  267. WP: "WillPrint",
  268. DP: "DidPrint"
  269. };
  270. const PageActionEventType = {
  271. O: "PageOpen",
  272. C: "PageClose"
  273. };
  274. const VerbosityLevel = {
  275. ERRORS: 0,
  276. WARNINGS: 1,
  277. INFOS: 5
  278. };
  279. const OPS = {
  280. dependency: 1,
  281. setLineWidth: 2,
  282. setLineCap: 3,
  283. setLineJoin: 4,
  284. setMiterLimit: 5,
  285. setDash: 6,
  286. setRenderingIntent: 7,
  287. setFlatness: 8,
  288. setGState: 9,
  289. save: 10,
  290. restore: 11,
  291. transform: 12,
  292. moveTo: 13,
  293. lineTo: 14,
  294. curveTo: 15,
  295. curveTo2: 16,
  296. curveTo3: 17,
  297. closePath: 18,
  298. rectangle: 19,
  299. stroke: 20,
  300. closeStroke: 21,
  301. fill: 22,
  302. eoFill: 23,
  303. fillStroke: 24,
  304. eoFillStroke: 25,
  305. closeFillStroke: 26,
  306. closeEOFillStroke: 27,
  307. endPath: 28,
  308. clip: 29,
  309. eoClip: 30,
  310. beginText: 31,
  311. endText: 32,
  312. setCharSpacing: 33,
  313. setWordSpacing: 34,
  314. setHScale: 35,
  315. setLeading: 36,
  316. setFont: 37,
  317. setTextRenderingMode: 38,
  318. setTextRise: 39,
  319. moveText: 40,
  320. setLeadingMoveText: 41,
  321. setTextMatrix: 42,
  322. nextLine: 43,
  323. showText: 44,
  324. showSpacedText: 45,
  325. nextLineShowText: 46,
  326. nextLineSetSpacingShowText: 47,
  327. setCharWidth: 48,
  328. setCharWidthAndBounds: 49,
  329. setStrokeColorSpace: 50,
  330. setFillColorSpace: 51,
  331. setStrokeColor: 52,
  332. setStrokeColorN: 53,
  333. setFillColor: 54,
  334. setFillColorN: 55,
  335. setStrokeGray: 56,
  336. setFillGray: 57,
  337. setStrokeRGBColor: 58,
  338. setFillRGBColor: 59,
  339. setStrokeCMYKColor: 60,
  340. setFillCMYKColor: 61,
  341. shadingFill: 62,
  342. beginInlineImage: 63,
  343. beginImageData: 64,
  344. endInlineImage: 65,
  345. paintXObject: 66,
  346. markPoint: 67,
  347. markPointProps: 68,
  348. beginMarkedContent: 69,
  349. beginMarkedContentProps: 70,
  350. endMarkedContent: 71,
  351. beginCompat: 72,
  352. endCompat: 73,
  353. paintFormXObjectBegin: 74,
  354. paintFormXObjectEnd: 75,
  355. beginGroup: 76,
  356. endGroup: 77,
  357. beginAnnotation: 80,
  358. endAnnotation: 81,
  359. paintImageMaskXObject: 83,
  360. paintImageMaskXObjectGroup: 84,
  361. paintImageXObject: 85,
  362. paintInlineImageXObject: 86,
  363. paintInlineImageXObjectGroup: 87,
  364. paintImageXObjectRepeat: 88,
  365. paintImageMaskXObjectRepeat: 89,
  366. paintSolidColorImageMask: 90,
  367. constructPath: 91,
  368. setStrokeTransparent: 92,
  369. setFillTransparent: 93
  370. };
  371. const DrawOPS = {
  372. moveTo: 0,
  373. lineTo: 1,
  374. curveTo: 2,
  375. closePath: 3
  376. };
  377. const PasswordResponses = {
  378. NEED_PASSWORD: 1,
  379. INCORRECT_PASSWORD: 2
  380. };
  381. let verbosity = VerbosityLevel.WARNINGS;
  382. function setVerbosityLevel(level) {
  383. if (Number.isInteger(level)) {
  384. verbosity = level;
  385. }
  386. }
  387. function getVerbosityLevel() {
  388. return verbosity;
  389. }
  390. function info(msg) {
  391. if (verbosity >= VerbosityLevel.INFOS) {
  392. console.log(`Info: ${msg}`);
  393. }
  394. }
  395. function warn(msg) {
  396. if (verbosity >= VerbosityLevel.WARNINGS) {
  397. console.log(`Warning: ${msg}`);
  398. }
  399. }
  400. function unreachable(msg) {
  401. throw new Error(msg);
  402. }
  403. function assert(cond, msg) {
  404. if (!cond) {
  405. unreachable(msg);
  406. }
  407. }
  408. function _isValidProtocol(url) {
  409. switch (url?.protocol) {
  410. case "http:":
  411. case "https:":
  412. case "ftp:":
  413. case "mailto:":
  414. case "tel:":
  415. return true;
  416. default:
  417. return false;
  418. }
  419. }
  420. function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
  421. if (!url) {
  422. return null;
  423. }
  424. if (options && typeof url === "string") {
  425. if (options.addDefaultProtocol && url.startsWith("www.")) {
  426. const dots = url.match(/\./g);
  427. if (dots?.length >= 2) {
  428. url = `http://${url}`;
  429. }
  430. }
  431. if (options.tryConvertEncoding) {
  432. try {
  433. url = stringToUTF8String(url);
  434. } catch {}
  435. }
  436. }
  437. const absoluteUrl = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
  438. return _isValidProtocol(absoluteUrl) ? absoluteUrl : null;
  439. }
  440. function shadow(obj, prop, value, nonSerializable = false) {
  441. Object.defineProperty(obj, prop, {
  442. value,
  443. enumerable: !nonSerializable,
  444. configurable: true,
  445. writable: false
  446. });
  447. return value;
  448. }
  449. const BaseException = function BaseExceptionClosure() {
  450. function BaseException(message, name) {
  451. this.message = message;
  452. this.name = name;
  453. }
  454. BaseException.prototype = new Error();
  455. BaseException.constructor = BaseException;
  456. return BaseException;
  457. }();
  458. class PasswordException extends BaseException {
  459. constructor(msg, code) {
  460. super(msg, "PasswordException");
  461. this.code = code;
  462. }
  463. }
  464. class UnknownErrorException extends BaseException {
  465. constructor(msg, details) {
  466. super(msg, "UnknownErrorException");
  467. this.details = details;
  468. }
  469. }
  470. class InvalidPDFException extends BaseException {
  471. constructor(msg) {
  472. super(msg, "InvalidPDFException");
  473. }
  474. }
  475. class ResponseException extends BaseException {
  476. constructor(msg, status, missing) {
  477. super(msg, "ResponseException");
  478. this.status = status;
  479. this.missing = missing;
  480. }
  481. }
  482. class FormatError extends BaseException {
  483. constructor(msg) {
  484. super(msg, "FormatError");
  485. }
  486. }
  487. class AbortException extends BaseException {
  488. constructor(msg) {
  489. super(msg, "AbortException");
  490. }
  491. }
  492. function bytesToString(bytes) {
  493. if (typeof bytes !== "object" || bytes?.length === undefined) {
  494. unreachable("Invalid argument for bytesToString");
  495. }
  496. const length = bytes.length;
  497. const MAX_ARGUMENT_COUNT = 8192;
  498. if (length < MAX_ARGUMENT_COUNT) {
  499. return String.fromCharCode.apply(null, bytes);
  500. }
  501. const strBuf = [];
  502. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  503. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  504. const chunk = bytes.subarray(i, chunkEnd);
  505. strBuf.push(String.fromCharCode.apply(null, chunk));
  506. }
  507. return strBuf.join("");
  508. }
  509. function stringToBytes(str) {
  510. if (typeof str !== "string") {
  511. unreachable("Invalid argument for stringToBytes");
  512. }
  513. const length = str.length;
  514. const bytes = new Uint8Array(length);
  515. for (let i = 0; i < length; ++i) {
  516. bytes[i] = str.charCodeAt(i) & 0xff;
  517. }
  518. return bytes;
  519. }
  520. function string32(value) {
  521. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  522. }
  523. function objectSize(obj) {
  524. return Object.keys(obj).length;
  525. }
  526. function objectFromMap(map) {
  527. const obj = Object.create(null);
  528. for (const [key, value] of map) {
  529. obj[key] = value;
  530. }
  531. return obj;
  532. }
  533. function isLittleEndian() {
  534. const buffer8 = new Uint8Array(4);
  535. buffer8[0] = 1;
  536. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  537. return view32[0] === 1;
  538. }
  539. function isEvalSupported() {
  540. try {
  541. new Function("");
  542. return true;
  543. } catch {
  544. return false;
  545. }
  546. }
  547. class util_FeatureTest {
  548. static get isLittleEndian() {
  549. return shadow(this, "isLittleEndian", isLittleEndian());
  550. }
  551. static get isEvalSupported() {
  552. return shadow(this, "isEvalSupported", isEvalSupported());
  553. }
  554. static get isOffscreenCanvasSupported() {
  555. return shadow(this, "isOffscreenCanvasSupported", typeof OffscreenCanvas !== "undefined");
  556. }
  557. static get isImageDecoderSupported() {
  558. return shadow(this, "isImageDecoderSupported", typeof ImageDecoder !== "undefined");
  559. }
  560. static get platform() {
  561. if (typeof navigator !== "undefined" && typeof navigator?.platform === "string" && typeof navigator?.userAgent === "string") {
  562. const {
  563. platform,
  564. userAgent
  565. } = navigator;
  566. return shadow(this, "platform", {
  567. isAndroid: userAgent.includes("Android"),
  568. isLinux: platform.includes("Linux"),
  569. isMac: platform.includes("Mac"),
  570. isWindows: platform.includes("Win"),
  571. isFirefox: userAgent.includes("Firefox")
  572. });
  573. }
  574. return shadow(this, "platform", {
  575. isAndroid: false,
  576. isLinux: false,
  577. isMac: false,
  578. isWindows: false,
  579. isFirefox: false
  580. });
  581. }
  582. static get isCSSRoundSupported() {
  583. return shadow(this, "isCSSRoundSupported", globalThis.CSS?.supports?.("width: round(1.5px, 1px)"));
  584. }
  585. }
  586. const hexNumbers = Array.from(Array(256).keys(), n => n.toString(16).padStart(2, "0"));
  587. class Util {
  588. static makeHexColor(r, g, b) {
  589. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  590. }
  591. static transform(m1, m2) {
  592. return [m1[0] * m2[0] + m1[2] * m2[1], m1[1] * m2[0] + m1[3] * m2[1], m1[0] * m2[2] + m1[2] * m2[3], m1[1] * m2[2] + m1[3] * m2[3], m1[0] * m2[4] + m1[2] * m2[5] + m1[4], m1[1] * m2[4] + m1[3] * m2[5] + m1[5]];
  593. }
  594. static applyTransform(p, m) {
  595. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  596. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  597. return [xt, yt];
  598. }
  599. static applyInverseTransform(p, m) {
  600. const d = m[0] * m[3] - m[1] * m[2];
  601. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  602. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  603. return [xt, yt];
  604. }
  605. static getAxialAlignedBoundingBox(r, m) {
  606. const p1 = this.applyTransform(r, m);
  607. const p2 = this.applyTransform(r.slice(2, 4), m);
  608. const p3 = this.applyTransform([r[0], r[3]], m);
  609. const p4 = this.applyTransform([r[2], r[1]], m);
  610. return [Math.min(p1[0], p2[0], p3[0], p4[0]), Math.min(p1[1], p2[1], p3[1], p4[1]), Math.max(p1[0], p2[0], p3[0], p4[0]), Math.max(p1[1], p2[1], p3[1], p4[1])];
  611. }
  612. static inverseTransform(m) {
  613. const d = m[0] * m[3] - m[1] * m[2];
  614. return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d, (m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
  615. }
  616. static singularValueDecompose2dScale(m) {
  617. const transpose = [m[0], m[2], m[1], m[3]];
  618. const a = m[0] * transpose[0] + m[1] * transpose[2];
  619. const b = m[0] * transpose[1] + m[1] * transpose[3];
  620. const c = m[2] * transpose[0] + m[3] * transpose[2];
  621. const d = m[2] * transpose[1] + m[3] * transpose[3];
  622. const first = (a + d) / 2;
  623. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  624. const sx = first + second || 1;
  625. const sy = first - second || 1;
  626. return [Math.sqrt(sx), Math.sqrt(sy)];
  627. }
  628. static normalizeRect(rect) {
  629. const r = rect.slice(0);
  630. if (rect[0] > rect[2]) {
  631. r[0] = rect[2];
  632. r[2] = rect[0];
  633. }
  634. if (rect[1] > rect[3]) {
  635. r[1] = rect[3];
  636. r[3] = rect[1];
  637. }
  638. return r;
  639. }
  640. static intersect(rect1, rect2) {
  641. const xLow = Math.max(Math.min(rect1[0], rect1[2]), Math.min(rect2[0], rect2[2]));
  642. const xHigh = Math.min(Math.max(rect1[0], rect1[2]), Math.max(rect2[0], rect2[2]));
  643. if (xLow > xHigh) {
  644. return null;
  645. }
  646. const yLow = Math.max(Math.min(rect1[1], rect1[3]), Math.min(rect2[1], rect2[3]));
  647. const yHigh = Math.min(Math.max(rect1[1], rect1[3]), Math.max(rect2[1], rect2[3]));
  648. if (yLow > yHigh) {
  649. return null;
  650. }
  651. return [xLow, yLow, xHigh, yHigh];
  652. }
  653. static pointBoundingBox(x, y, minMax) {
  654. minMax[0] = Math.min(minMax[0], x);
  655. minMax[1] = Math.min(minMax[1], y);
  656. minMax[2] = Math.max(minMax[2], x);
  657. minMax[3] = Math.max(minMax[3], y);
  658. }
  659. static rectBoundingBox(x0, y0, x1, y1, minMax) {
  660. minMax[0] = Math.min(minMax[0], x0, x1);
  661. minMax[1] = Math.min(minMax[1], y0, y1);
  662. minMax[2] = Math.max(minMax[2], x0, x1);
  663. minMax[3] = Math.max(minMax[3], y0, y1);
  664. }
  665. static #getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, t, minMax) {
  666. if (t <= 0 || t >= 1) {
  667. return;
  668. }
  669. const mt = 1 - t;
  670. const tt = t * t;
  671. const ttt = tt * t;
  672. const x = mt * (mt * (mt * x0 + 3 * t * x1) + 3 * tt * x2) + ttt * x3;
  673. const y = mt * (mt * (mt * y0 + 3 * t * y1) + 3 * tt * y2) + ttt * y3;
  674. minMax[0] = Math.min(minMax[0], x);
  675. minMax[1] = Math.min(minMax[1], y);
  676. minMax[2] = Math.max(minMax[2], x);
  677. minMax[3] = Math.max(minMax[3], y);
  678. }
  679. static #getExtremum(x0, x1, x2, x3, y0, y1, y2, y3, a, b, c, minMax) {
  680. if (Math.abs(a) < 1e-12) {
  681. if (Math.abs(b) >= 1e-12) {
  682. this.#getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, -c / b, minMax);
  683. }
  684. return;
  685. }
  686. const delta = b ** 2 - 4 * c * a;
  687. if (delta < 0) {
  688. return;
  689. }
  690. const sqrtDelta = Math.sqrt(delta);
  691. const a2 = 2 * a;
  692. this.#getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, (-b + sqrtDelta) / a2, minMax);
  693. this.#getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, (-b - sqrtDelta) / a2, minMax);
  694. }
  695. static bezierBoundingBox(x0, y0, x1, y1, x2, y2, x3, y3, minMax) {
  696. minMax[0] = Math.min(minMax[0], x0, x3);
  697. minMax[1] = Math.min(minMax[1], y0, y3);
  698. minMax[2] = Math.max(minMax[2], x0, x3);
  699. minMax[3] = Math.max(minMax[3], y0, y3);
  700. this.#getExtremum(x0, x1, x2, x3, y0, y1, y2, y3, 3 * (-x0 + 3 * (x1 - x2) + x3), 6 * (x0 - 2 * x1 + x2), 3 * (x1 - x0), minMax);
  701. this.#getExtremum(x0, x1, x2, x3, y0, y1, y2, y3, 3 * (-y0 + 3 * (y1 - y2) + y3), 6 * (y0 - 2 * y1 + y2), 3 * (y1 - y0), minMax);
  702. }
  703. }
  704. const PDFStringTranslateTable = (/* unused pure expression or super */ null && ([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2d8, 0x2c7, 0x2c6, 0x2d9, 0x2dd, 0x2db, 0x2da, 0x2dc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x192, 0x2044, 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018, 0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x141, 0x152, 0x160, 0x178, 0x17d, 0x131, 0x142, 0x153, 0x161, 0x17e, 0, 0x20ac]));
  705. function stringToPDFString(str) {
  706. if (str[0] >= "\xEF") {
  707. let encoding;
  708. if (str[0] === "\xFE" && str[1] === "\xFF") {
  709. encoding = "utf-16be";
  710. if (str.length % 2 === 1) {
  711. str = str.slice(0, -1);
  712. }
  713. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  714. encoding = "utf-16le";
  715. if (str.length % 2 === 1) {
  716. str = str.slice(0, -1);
  717. }
  718. } else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
  719. encoding = "utf-8";
  720. }
  721. if (encoding) {
  722. try {
  723. const decoder = new TextDecoder(encoding, {
  724. fatal: true
  725. });
  726. const buffer = stringToBytes(str);
  727. const decoded = decoder.decode(buffer);
  728. if (!decoded.includes("\x1b")) {
  729. return decoded;
  730. }
  731. return decoded.replaceAll(/\x1b[^\x1b]*(?:\x1b|$)/g, "");
  732. } catch (ex) {
  733. warn(`stringToPDFString: "${ex}".`);
  734. }
  735. }
  736. }
  737. const strBuf = [];
  738. for (let i = 0, ii = str.length; i < ii; i++) {
  739. const charCode = str.charCodeAt(i);
  740. if (charCode === 0x1b) {
  741. while (++i < ii && str.charCodeAt(i) !== 0x1b) {}
  742. continue;
  743. }
  744. const code = PDFStringTranslateTable[charCode];
  745. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  746. }
  747. return strBuf.join("");
  748. }
  749. function stringToUTF8String(str) {
  750. return decodeURIComponent(escape(str));
  751. }
  752. function utf8StringToString(str) {
  753. return unescape(encodeURIComponent(str));
  754. }
  755. function isArrayEqual(arr1, arr2) {
  756. if (arr1.length !== arr2.length) {
  757. return false;
  758. }
  759. for (let i = 0, ii = arr1.length; i < ii; i++) {
  760. if (arr1[i] !== arr2[i]) {
  761. return false;
  762. }
  763. }
  764. return true;
  765. }
  766. function getModificationDate(date = new Date()) {
  767. const buffer = [date.getUTCFullYear().toString(), (date.getUTCMonth() + 1).toString().padStart(2, "0"), date.getUTCDate().toString().padStart(2, "0"), date.getUTCHours().toString().padStart(2, "0"), date.getUTCMinutes().toString().padStart(2, "0"), date.getUTCSeconds().toString().padStart(2, "0")];
  768. return buffer.join("");
  769. }
  770. let NormalizeRegex = null;
  771. let NormalizationMap = null;
  772. function normalizeUnicode(str) {
  773. if (!NormalizeRegex) {
  774. NormalizeRegex = /([\u00a0\u00b5\u037e\u0eb3\u2000-\u200a\u202f\u2126\ufb00-\ufb04\ufb06\ufb20-\ufb36\ufb38-\ufb3c\ufb3e\ufb40-\ufb41\ufb43-\ufb44\ufb46-\ufba1\ufba4-\ufba9\ufbae-\ufbb1\ufbd3-\ufbdc\ufbde-\ufbe7\ufbea-\ufbf8\ufbfc-\ufbfd\ufc00-\ufc5d\ufc64-\ufcf1\ufcf5-\ufd3d\ufd88\ufdf4\ufdfa-\ufdfb\ufe71\ufe77\ufe79\ufe7b\ufe7d]+)|(\ufb05+)/gu;
  775. NormalizationMap = new Map([["ſt", "ſt"]]);
  776. }
  777. return str.replaceAll(NormalizeRegex, (_, p1, p2) => p1 ? p1.normalize("NFKC") : NormalizationMap.get(p2));
  778. }
  779. function getUuid() {
  780. if (typeof crypto.randomUUID === "function") {
  781. return crypto.randomUUID();
  782. }
  783. const buf = new Uint8Array(32);
  784. crypto.getRandomValues(buf);
  785. return bytesToString(buf);
  786. }
  787. const AnnotationPrefix = "pdfjs_internal_id_";
  788. function _isValidExplicitDest(validRef, validName, dest) {
  789. if (!Array.isArray(dest) || dest.length < 2) {
  790. return false;
  791. }
  792. const [page, zoom, ...args] = dest;
  793. if (!validRef(page) && !Number.isInteger(page)) {
  794. return false;
  795. }
  796. if (!validName(zoom)) {
  797. return false;
  798. }
  799. const argsLen = args.length;
  800. let allowNull = true;
  801. switch (zoom.name) {
  802. case "XYZ":
  803. if (argsLen < 2 || argsLen > 3) {
  804. return false;
  805. }
  806. break;
  807. case "Fit":
  808. case "FitB":
  809. return argsLen === 0;
  810. case "FitH":
  811. case "FitBH":
  812. case "FitV":
  813. case "FitBV":
  814. if (argsLen > 1) {
  815. return false;
  816. }
  817. break;
  818. case "FitR":
  819. if (argsLen !== 4) {
  820. return false;
  821. }
  822. allowNull = false;
  823. break;
  824. default:
  825. return false;
  826. }
  827. for (const arg of args) {
  828. if (typeof arg === "number" || allowNull && arg === null) {
  829. continue;
  830. }
  831. return false;
  832. }
  833. return true;
  834. }
  835. function MathClamp(v, min, max) {
  836. return Math.min(Math.max(v, min), max);
  837. }
  838. function toHexUtil(arr) {
  839. if (Uint8Array.prototype.toHex) {
  840. return arr.toHex();
  841. }
  842. return Array.from(arr, num => hexNumbers[num]).join("");
  843. }
  844. function toBase64Util(arr) {
  845. if (Uint8Array.prototype.toBase64) {
  846. return arr.toBase64();
  847. }
  848. return btoa(bytesToString(arr));
  849. }
  850. function fromBase64Util(str) {
  851. if (Uint8Array.fromBase64) {
  852. return Uint8Array.fromBase64(str);
  853. }
  854. return stringToBytes(atob(str));
  855. }
  856. if (typeof Promise.try !== "function") {
  857. Promise.try = function (fn, ...args) {
  858. return new Promise(resolve => {
  859. resolve(fn(...args));
  860. });
  861. };
  862. }
  863. if (typeof Math.sumPrecise !== "function") {
  864. Math.sumPrecise = function (numbers) {
  865. return numbers.reduce((a, b) => a + b, 0);
  866. };
  867. }
  868. ;// ./src/display/display_utils.js
  869. const SVG_NS = "http://www.w3.org/2000/svg";
  870. class PixelsPerInch {
  871. static CSS = 96.0;
  872. static PDF = 72.0;
  873. static PDF_TO_CSS_UNITS = this.CSS / this.PDF;
  874. }
  875. async function fetchData(url, type = "text") {
  876. if (isValidFetchUrl(url, document.baseURI)) {
  877. const response = await fetch(url);
  878. if (!response.ok) {
  879. throw new Error(response.statusText);
  880. }
  881. switch (type) {
  882. case "arraybuffer":
  883. return response.arrayBuffer();
  884. case "blob":
  885. return response.blob();
  886. case "json":
  887. return response.json();
  888. }
  889. return response.text();
  890. }
  891. return new Promise((resolve, reject) => {
  892. const request = new XMLHttpRequest();
  893. request.open("GET", url, true);
  894. request.responseType = type;
  895. request.onreadystatechange = () => {
  896. if (request.readyState !== XMLHttpRequest.DONE) {
  897. return;
  898. }
  899. if (request.status === 200 || request.status === 0) {
  900. switch (type) {
  901. case "arraybuffer":
  902. case "blob":
  903. case "json":
  904. resolve(request.response);
  905. return;
  906. }
  907. resolve(request.responseText);
  908. return;
  909. }
  910. reject(new Error(request.statusText));
  911. };
  912. request.send(null);
  913. });
  914. }
  915. class PageViewport {
  916. constructor({
  917. viewBox,
  918. userUnit,
  919. scale,
  920. rotation,
  921. offsetX = 0,
  922. offsetY = 0,
  923. dontFlip = false
  924. }) {
  925. this.viewBox = viewBox;
  926. this.userUnit = userUnit;
  927. this.scale = scale;
  928. this.rotation = rotation;
  929. this.offsetX = offsetX;
  930. this.offsetY = offsetY;
  931. scale *= userUnit;
  932. const centerX = (viewBox[2] + viewBox[0]) / 2;
  933. const centerY = (viewBox[3] + viewBox[1]) / 2;
  934. let rotateA, rotateB, rotateC, rotateD;
  935. rotation %= 360;
  936. if (rotation < 0) {
  937. rotation += 360;
  938. }
  939. switch (rotation) {
  940. case 180:
  941. rotateA = -1;
  942. rotateB = 0;
  943. rotateC = 0;
  944. rotateD = 1;
  945. break;
  946. case 90:
  947. rotateA = 0;
  948. rotateB = 1;
  949. rotateC = 1;
  950. rotateD = 0;
  951. break;
  952. case 270:
  953. rotateA = 0;
  954. rotateB = -1;
  955. rotateC = -1;
  956. rotateD = 0;
  957. break;
  958. case 0:
  959. rotateA = 1;
  960. rotateB = 0;
  961. rotateC = 0;
  962. rotateD = -1;
  963. break;
  964. default:
  965. throw new Error("PageViewport: Invalid rotation, must be a multiple of 90 degrees.");
  966. }
  967. if (dontFlip) {
  968. rotateC = -rotateC;
  969. rotateD = -rotateD;
  970. }
  971. let offsetCanvasX, offsetCanvasY;
  972. let width, height;
  973. if (rotateA === 0) {
  974. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  975. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  976. width = (viewBox[3] - viewBox[1]) * scale;
  977. height = (viewBox[2] - viewBox[0]) * scale;
  978. } else {
  979. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  980. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  981. width = (viewBox[2] - viewBox[0]) * scale;
  982. height = (viewBox[3] - viewBox[1]) * scale;
  983. }
  984. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  985. this.width = width;
  986. this.height = height;
  987. }
  988. get rawDims() {
  989. const dims = this.viewBox;
  990. return shadow(this, "rawDims", {
  991. pageWidth: dims[2] - dims[0],
  992. pageHeight: dims[3] - dims[1],
  993. pageX: dims[0],
  994. pageY: dims[1]
  995. });
  996. }
  997. clone({
  998. scale = this.scale,
  999. rotation = this.rotation,
  1000. offsetX = this.offsetX,
  1001. offsetY = this.offsetY,
  1002. dontFlip = false
  1003. } = {}) {
  1004. return new PageViewport({
  1005. viewBox: this.viewBox.slice(),
  1006. userUnit: this.userUnit,
  1007. scale,
  1008. rotation,
  1009. offsetX,
  1010. offsetY,
  1011. dontFlip
  1012. });
  1013. }
  1014. convertToViewportPoint(x, y) {
  1015. return Util.applyTransform([x, y], this.transform);
  1016. }
  1017. convertToViewportRectangle(rect) {
  1018. const topLeft = Util.applyTransform([rect[0], rect[1]], this.transform);
  1019. const bottomRight = Util.applyTransform([rect[2], rect[3]], this.transform);
  1020. return [topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]];
  1021. }
  1022. convertToPdfPoint(x, y) {
  1023. return Util.applyInverseTransform([x, y], this.transform);
  1024. }
  1025. }
  1026. class RenderingCancelledException extends BaseException {
  1027. constructor(msg, extraDelay = 0) {
  1028. super(msg, "RenderingCancelledException");
  1029. this.extraDelay = extraDelay;
  1030. }
  1031. }
  1032. function isDataScheme(url) {
  1033. const ii = url.length;
  1034. let i = 0;
  1035. while (i < ii && url[i].trim() === "") {
  1036. i++;
  1037. }
  1038. return url.substring(i, i + 5).toLowerCase() === "data:";
  1039. }
  1040. function isPdfFile(filename) {
  1041. return typeof filename === "string" && /\.pdf$/i.test(filename);
  1042. }
  1043. function getFilenameFromUrl(url) {
  1044. [url] = url.split(/[#?]/, 1);
  1045. return url.substring(url.lastIndexOf("/") + 1);
  1046. }
  1047. function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
  1048. if (typeof url !== "string") {
  1049. return defaultFilename;
  1050. }
  1051. if (isDataScheme(url)) {
  1052. warn('getPdfFilenameFromUrl: ignore "data:"-URL for performance reasons.');
  1053. return defaultFilename;
  1054. }
  1055. const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  1056. const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  1057. const splitURI = reURI.exec(url);
  1058. let suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]);
  1059. if (suggestedFilename) {
  1060. suggestedFilename = suggestedFilename[0];
  1061. if (suggestedFilename.includes("%")) {
  1062. try {
  1063. suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
  1064. } catch {}
  1065. }
  1066. }
  1067. return suggestedFilename || defaultFilename;
  1068. }
  1069. class StatTimer {
  1070. started = Object.create(null);
  1071. times = [];
  1072. time(name) {
  1073. if (name in this.started) {
  1074. warn(`Timer is already running for ${name}`);
  1075. }
  1076. this.started[name] = Date.now();
  1077. }
  1078. timeEnd(name) {
  1079. if (!(name in this.started)) {
  1080. warn(`Timer has not been started for ${name}`);
  1081. }
  1082. this.times.push({
  1083. name,
  1084. start: this.started[name],
  1085. end: Date.now()
  1086. });
  1087. delete this.started[name];
  1088. }
  1089. toString() {
  1090. const outBuf = [];
  1091. let longest = 0;
  1092. for (const {
  1093. name
  1094. } of this.times) {
  1095. longest = Math.max(name.length, longest);
  1096. }
  1097. for (const {
  1098. name,
  1099. start,
  1100. end
  1101. } of this.times) {
  1102. outBuf.push(`${name.padEnd(longest)} ${end - start}ms\n`);
  1103. }
  1104. return outBuf.join("");
  1105. }
  1106. }
  1107. function isValidFetchUrl(url, baseUrl) {
  1108. const res = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
  1109. return res?.protocol === "http:" || res?.protocol === "https:";
  1110. }
  1111. function noContextMenu(e) {
  1112. e.preventDefault();
  1113. }
  1114. function stopEvent(e) {
  1115. e.preventDefault();
  1116. e.stopPropagation();
  1117. }
  1118. function deprecated(details) {
  1119. console.log("Deprecated API usage: " + details);
  1120. }
  1121. class PDFDateString {
  1122. static #regex;
  1123. static toDateObject(input) {
  1124. if (!input || typeof input !== "string") {
  1125. return null;
  1126. }
  1127. this.#regex ||= new RegExp("^D:" + "(\\d{4})" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "([Z|+|-])?" + "(\\d{2})?" + "'?" + "(\\d{2})?" + "'?");
  1128. const matches = this.#regex.exec(input);
  1129. if (!matches) {
  1130. return null;
  1131. }
  1132. const year = parseInt(matches[1], 10);
  1133. let month = parseInt(matches[2], 10);
  1134. month = month >= 1 && month <= 12 ? month - 1 : 0;
  1135. let day = parseInt(matches[3], 10);
  1136. day = day >= 1 && day <= 31 ? day : 1;
  1137. let hour = parseInt(matches[4], 10);
  1138. hour = hour >= 0 && hour <= 23 ? hour : 0;
  1139. let minute = parseInt(matches[5], 10);
  1140. minute = minute >= 0 && minute <= 59 ? minute : 0;
  1141. let second = parseInt(matches[6], 10);
  1142. second = second >= 0 && second <= 59 ? second : 0;
  1143. const universalTimeRelation = matches[7] || "Z";
  1144. let offsetHour = parseInt(matches[8], 10);
  1145. offsetHour = offsetHour >= 0 && offsetHour <= 23 ? offsetHour : 0;
  1146. let offsetMinute = parseInt(matches[9], 10) || 0;
  1147. offsetMinute = offsetMinute >= 0 && offsetMinute <= 59 ? offsetMinute : 0;
  1148. if (universalTimeRelation === "-") {
  1149. hour += offsetHour;
  1150. minute += offsetMinute;
  1151. } else if (universalTimeRelation === "+") {
  1152. hour -= offsetHour;
  1153. minute -= offsetMinute;
  1154. }
  1155. return new Date(Date.UTC(year, month, day, hour, minute, second));
  1156. }
  1157. }
  1158. function getXfaPageViewport(xfaPage, {
  1159. scale = 1,
  1160. rotation = 0
  1161. }) {
  1162. const {
  1163. width,
  1164. height
  1165. } = xfaPage.attributes.style;
  1166. const viewBox = [0, 0, parseInt(width), parseInt(height)];
  1167. return new PageViewport({
  1168. viewBox,
  1169. userUnit: 1,
  1170. scale,
  1171. rotation
  1172. });
  1173. }
  1174. function getRGB(color) {
  1175. if (color.startsWith("#")) {
  1176. const colorRGB = parseInt(color.slice(1), 16);
  1177. return [(colorRGB & 0xff0000) >> 16, (colorRGB & 0x00ff00) >> 8, colorRGB & 0x0000ff];
  1178. }
  1179. if (color.startsWith("rgb(")) {
  1180. return color.slice(4, -1).split(",").map(x => parseInt(x));
  1181. }
  1182. if (color.startsWith("rgba(")) {
  1183. return color.slice(5, -1).split(",").map(x => parseInt(x)).slice(0, 3);
  1184. }
  1185. warn(`Not a valid color format: "${color}"`);
  1186. return [0, 0, 0];
  1187. }
  1188. function getColorValues(colors) {
  1189. const span = document.createElement("span");
  1190. span.style.visibility = "hidden";
  1191. document.body.append(span);
  1192. for (const name of colors.keys()) {
  1193. span.style.color = name;
  1194. const computedColor = window.getComputedStyle(span).color;
  1195. colors.set(name, getRGB(computedColor));
  1196. }
  1197. span.remove();
  1198. }
  1199. function getCurrentTransform(ctx) {
  1200. const {
  1201. a,
  1202. b,
  1203. c,
  1204. d,
  1205. e,
  1206. f
  1207. } = ctx.getTransform();
  1208. return [a, b, c, d, e, f];
  1209. }
  1210. function getCurrentTransformInverse(ctx) {
  1211. const {
  1212. a,
  1213. b,
  1214. c,
  1215. d,
  1216. e,
  1217. f
  1218. } = ctx.getTransform().invertSelf();
  1219. return [a, b, c, d, e, f];
  1220. }
  1221. function setLayerDimensions(div, viewport, mustFlip = false, mustRotate = true) {
  1222. if (viewport instanceof PageViewport) {
  1223. const {
  1224. pageWidth,
  1225. pageHeight
  1226. } = viewport.rawDims;
  1227. const {
  1228. style
  1229. } = div;
  1230. const useRound = util_FeatureTest.isCSSRoundSupported;
  1231. const w = `var(--total-scale-factor) * ${pageWidth}px`,
  1232. h = `var(--total-scale-factor) * ${pageHeight}px`;
  1233. const widthStr = useRound ? `round(down, ${w}, var(--scale-round-x))` : `calc(${w})`,
  1234. heightStr = useRound ? `round(down, ${h}, var(--scale-round-y))` : `calc(${h})`;
  1235. if (!mustFlip || viewport.rotation % 180 === 0) {
  1236. style.width = widthStr;
  1237. style.height = heightStr;
  1238. } else {
  1239. style.width = heightStr;
  1240. style.height = widthStr;
  1241. }
  1242. }
  1243. if (mustRotate) {
  1244. div.setAttribute("data-main-rotation", viewport.rotation);
  1245. }
  1246. }
  1247. class OutputScale {
  1248. constructor() {
  1249. const {
  1250. pixelRatio
  1251. } = OutputScale;
  1252. this.sx = pixelRatio;
  1253. this.sy = pixelRatio;
  1254. }
  1255. get scaled() {
  1256. return this.sx !== 1 || this.sy !== 1;
  1257. }
  1258. get symmetric() {
  1259. return this.sx === this.sy;
  1260. }
  1261. limitCanvas(width, height, maxPixels, maxDim) {
  1262. let maxAreaScale = Infinity,
  1263. maxWidthScale = Infinity,
  1264. maxHeightScale = Infinity;
  1265. if (maxPixels > 0) {
  1266. maxAreaScale = Math.sqrt(maxPixels / (width * height));
  1267. }
  1268. if (maxDim !== -1) {
  1269. maxWidthScale = maxDim / width;
  1270. maxHeightScale = maxDim / height;
  1271. }
  1272. const maxScale = Math.min(maxAreaScale, maxWidthScale, maxHeightScale);
  1273. if (this.sx > maxScale || this.sy > maxScale) {
  1274. this.sx = maxScale;
  1275. this.sy = maxScale;
  1276. return true;
  1277. }
  1278. return false;
  1279. }
  1280. static get pixelRatio() {
  1281. return globalThis.devicePixelRatio || 1;
  1282. }
  1283. }
  1284. const SupportedImageMimeTypes = ["image/apng", "image/avif", "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml", "image/webp", "image/x-icon"];
  1285. ;// ./src/display/editor/toolbar.js
  1286. class EditorToolbar {
  1287. #toolbar = null;
  1288. #colorPicker = null;
  1289. #editor;
  1290. #buttons = null;
  1291. #altText = null;
  1292. #signatureDescriptionButton = null;
  1293. static #l10nRemove = null;
  1294. constructor(editor) {
  1295. this.#editor = editor;
  1296. EditorToolbar.#l10nRemove ||= Object.freeze({
  1297. freetext: "pdfjs-editor-remove-freetext-button",
  1298. highlight: "pdfjs-editor-remove-highlight-button",
  1299. ink: "pdfjs-editor-remove-ink-button",
  1300. stamp: "pdfjs-editor-remove-stamp-button",
  1301. signature: "pdfjs-editor-remove-signature-button"
  1302. });
  1303. }
  1304. render() {
  1305. const editToolbar = this.#toolbar = document.createElement("div");
  1306. editToolbar.classList.add("editToolbar", "hidden");
  1307. editToolbar.setAttribute("role", "toolbar");
  1308. const signal = this.#editor._uiManager._signal;
  1309. editToolbar.addEventListener("contextmenu", noContextMenu, {
  1310. signal
  1311. });
  1312. editToolbar.addEventListener("pointerdown", EditorToolbar.#pointerDown, {
  1313. signal
  1314. });
  1315. const buttons = this.#buttons = document.createElement("div");
  1316. buttons.className = "buttons";
  1317. editToolbar.append(buttons);
  1318. const position = this.#editor.toolbarPosition;
  1319. if (position) {
  1320. const {
  1321. style
  1322. } = editToolbar;
  1323. const x = this.#editor._uiManager.direction === "ltr" ? 1 - position[0] : position[0];
  1324. style.insetInlineEnd = `${100 * x}%`;
  1325. style.top = `calc(${100 * position[1]}% + var(--editor-toolbar-vert-offset))`;
  1326. }
  1327. this.#addDeleteButton();
  1328. return editToolbar;
  1329. }
  1330. get div() {
  1331. return this.#toolbar;
  1332. }
  1333. static #pointerDown(e) {
  1334. e.stopPropagation();
  1335. }
  1336. #focusIn(e) {
  1337. this.#editor._focusEventsAllowed = false;
  1338. stopEvent(e);
  1339. }
  1340. #focusOut(e) {
  1341. this.#editor._focusEventsAllowed = true;
  1342. stopEvent(e);
  1343. }
  1344. #addListenersToElement(element) {
  1345. const signal = this.#editor._uiManager._signal;
  1346. element.addEventListener("focusin", this.#focusIn.bind(this), {
  1347. capture: true,
  1348. signal
  1349. });
  1350. element.addEventListener("focusout", this.#focusOut.bind(this), {
  1351. capture: true,
  1352. signal
  1353. });
  1354. element.addEventListener("contextmenu", noContextMenu, {
  1355. signal
  1356. });
  1357. }
  1358. hide() {
  1359. this.#toolbar.classList.add("hidden");
  1360. this.#colorPicker?.hideDropdown();
  1361. }
  1362. show() {
  1363. this.#toolbar.classList.remove("hidden");
  1364. this.#altText?.shown();
  1365. }
  1366. #addDeleteButton() {
  1367. const {
  1368. editorType,
  1369. _uiManager
  1370. } = this.#editor;
  1371. const button = document.createElement("button");
  1372. button.className = "delete";
  1373. button.tabIndex = 0;
  1374. button.setAttribute("data-l10n-id", EditorToolbar.#l10nRemove[editorType]);
  1375. this.#addListenersToElement(button);
  1376. button.addEventListener("click", e => {
  1377. _uiManager.delete();
  1378. }, {
  1379. signal: _uiManager._signal
  1380. });
  1381. this.#buttons.append(button);
  1382. }
  1383. get #divider() {
  1384. const divider = document.createElement("div");
  1385. divider.className = "divider";
  1386. return divider;
  1387. }
  1388. async addAltText(altText) {
  1389. const button = await altText.render();
  1390. this.#addListenersToElement(button);
  1391. this.#buttons.prepend(button, this.#divider);
  1392. this.#altText = altText;
  1393. }
  1394. addColorPicker(colorPicker) {
  1395. this.#colorPicker = colorPicker;
  1396. const button = colorPicker.renderButton();
  1397. this.#addListenersToElement(button);
  1398. this.#buttons.prepend(button, this.#divider);
  1399. }
  1400. async addEditSignatureButton(signatureManager) {
  1401. const button = this.#signatureDescriptionButton = await signatureManager.renderEditButton(this.#editor);
  1402. this.#addListenersToElement(button);
  1403. this.#buttons.prepend(button, this.#divider);
  1404. }
  1405. updateEditSignatureButton(description) {
  1406. if (this.#signatureDescriptionButton) {
  1407. this.#signatureDescriptionButton.title = description;
  1408. }
  1409. }
  1410. remove() {
  1411. this.#toolbar.remove();
  1412. this.#colorPicker?.destroy();
  1413. this.#colorPicker = null;
  1414. }
  1415. }
  1416. class HighlightToolbar {
  1417. #buttons = null;
  1418. #toolbar = null;
  1419. #uiManager;
  1420. constructor(uiManager) {
  1421. this.#uiManager = uiManager;
  1422. }
  1423. #render() {
  1424. const editToolbar = this.#toolbar = document.createElement("div");
  1425. editToolbar.className = "editToolbar";
  1426. editToolbar.setAttribute("role", "toolbar");
  1427. editToolbar.addEventListener("contextmenu", noContextMenu, {
  1428. signal: this.#uiManager._signal
  1429. });
  1430. const buttons = this.#buttons = document.createElement("div");
  1431. buttons.className = "buttons";
  1432. editToolbar.append(buttons);
  1433. this.#addHighlightButton();
  1434. return editToolbar;
  1435. }
  1436. #getLastPoint(boxes, isLTR) {
  1437. let lastY = 0;
  1438. let lastX = 0;
  1439. for (const box of boxes) {
  1440. const y = box.y + box.height;
  1441. if (y < lastY) {
  1442. continue;
  1443. }
  1444. const x = box.x + (isLTR ? box.width : 0);
  1445. if (y > lastY) {
  1446. lastX = x;
  1447. lastY = y;
  1448. continue;
  1449. }
  1450. if (isLTR) {
  1451. if (x > lastX) {
  1452. lastX = x;
  1453. }
  1454. } else if (x < lastX) {
  1455. lastX = x;
  1456. }
  1457. }
  1458. return [isLTR ? 1 - lastX : lastX, lastY];
  1459. }
  1460. show(parent, boxes, isLTR) {
  1461. const [x, y] = this.#getLastPoint(boxes, isLTR);
  1462. const {
  1463. style
  1464. } = this.#toolbar ||= this.#render();
  1465. parent.append(this.#toolbar);
  1466. style.insetInlineEnd = `${100 * x}%`;
  1467. style.top = `calc(${100 * y}% + var(--editor-toolbar-vert-offset))`;
  1468. }
  1469. hide() {
  1470. this.#toolbar.remove();
  1471. }
  1472. #addHighlightButton() {
  1473. const button = document.createElement("button");
  1474. button.className = "highlightButton";
  1475. button.tabIndex = 0;
  1476. button.setAttribute("data-l10n-id", `pdfjs-highlight-floating-button1`);
  1477. const span = document.createElement("span");
  1478. button.append(span);
  1479. span.className = "visuallyHidden";
  1480. span.setAttribute("data-l10n-id", "pdfjs-highlight-floating-button-label");
  1481. const signal = this.#uiManager._signal;
  1482. button.addEventListener("contextmenu", noContextMenu, {
  1483. signal
  1484. });
  1485. button.addEventListener("click", () => {
  1486. this.#uiManager.highlightSelection("floating_button");
  1487. }, {
  1488. signal
  1489. });
  1490. this.#buttons.append(button);
  1491. }
  1492. }
  1493. ;// ./src/display/editor/tools.js
  1494. function bindEvents(obj, element, names) {
  1495. for (const name of names) {
  1496. element.addEventListener(name, obj[name].bind(obj));
  1497. }
  1498. }
  1499. class IdManager {
  1500. #id = 0;
  1501. get id() {
  1502. return `${AnnotationEditorPrefix}${this.#id++}`;
  1503. }
  1504. }
  1505. class ImageManager {
  1506. #baseId = getUuid();
  1507. #id = 0;
  1508. #cache = null;
  1509. static get _isSVGFittingCanvas() {
  1510. const svg = `data:image/svg+xml;charset=UTF-8,<svg viewBox="0 0 1 1" width="1" height="1" xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1" style="fill:red;"/></svg>`;
  1511. const canvas = new OffscreenCanvas(1, 3);
  1512. const ctx = canvas.getContext("2d", {
  1513. willReadFrequently: true
  1514. });
  1515. const image = new Image();
  1516. image.src = svg;
  1517. const promise = image.decode().then(() => {
  1518. ctx.drawImage(image, 0, 0, 1, 1, 0, 0, 1, 3);
  1519. return new Uint32Array(ctx.getImageData(0, 0, 1, 1).data.buffer)[0] === 0;
  1520. });
  1521. return shadow(this, "_isSVGFittingCanvas", promise);
  1522. }
  1523. async #get(key, rawData) {
  1524. this.#cache ||= new Map();
  1525. let data = this.#cache.get(key);
  1526. if (data === null) {
  1527. return null;
  1528. }
  1529. if (data?.bitmap) {
  1530. data.refCounter += 1;
  1531. return data;
  1532. }
  1533. try {
  1534. data ||= {
  1535. bitmap: null,
  1536. id: `image_${this.#baseId}_${this.#id++}`,
  1537. refCounter: 0,
  1538. isSvg: false
  1539. };
  1540. let image;
  1541. if (typeof rawData === "string") {
  1542. data.url = rawData;
  1543. image = await fetchData(rawData, "blob");
  1544. } else if (rawData instanceof File) {
  1545. image = data.file = rawData;
  1546. } else if (rawData instanceof Blob) {
  1547. image = rawData;
  1548. }
  1549. if (image.type === "image/svg+xml") {
  1550. const mustRemoveAspectRatioPromise = ImageManager._isSVGFittingCanvas;
  1551. const fileReader = new FileReader();
  1552. const imageElement = new Image();
  1553. const imagePromise = new Promise((resolve, reject) => {
  1554. imageElement.onload = () => {
  1555. data.bitmap = imageElement;
  1556. data.isSvg = true;
  1557. resolve();
  1558. };
  1559. fileReader.onload = async () => {
  1560. const url = data.svgUrl = fileReader.result;
  1561. imageElement.src = (await mustRemoveAspectRatioPromise) ? `${url}#svgView(preserveAspectRatio(none))` : url;
  1562. };
  1563. imageElement.onerror = fileReader.onerror = reject;
  1564. });
  1565. fileReader.readAsDataURL(image);
  1566. await imagePromise;
  1567. } else {
  1568. data.bitmap = await createImageBitmap(image);
  1569. }
  1570. data.refCounter = 1;
  1571. } catch (e) {
  1572. warn(e);
  1573. data = null;
  1574. }
  1575. this.#cache.set(key, data);
  1576. if (data) {
  1577. this.#cache.set(data.id, data);
  1578. }
  1579. return data;
  1580. }
  1581. async getFromFile(file) {
  1582. const {
  1583. lastModified,
  1584. name,
  1585. size,
  1586. type
  1587. } = file;
  1588. return this.#get(`${lastModified}_${name}_${size}_${type}`, file);
  1589. }
  1590. async getFromUrl(url) {
  1591. return this.#get(url, url);
  1592. }
  1593. async getFromBlob(id, blobPromise) {
  1594. const blob = await blobPromise;
  1595. return this.#get(id, blob);
  1596. }
  1597. async getFromId(id) {
  1598. this.#cache ||= new Map();
  1599. const data = this.#cache.get(id);
  1600. if (!data) {
  1601. return null;
  1602. }
  1603. if (data.bitmap) {
  1604. data.refCounter += 1;
  1605. return data;
  1606. }
  1607. if (data.file) {
  1608. return this.getFromFile(data.file);
  1609. }
  1610. if (data.blobPromise) {
  1611. const {
  1612. blobPromise
  1613. } = data;
  1614. delete data.blobPromise;
  1615. return this.getFromBlob(data.id, blobPromise);
  1616. }
  1617. return this.getFromUrl(data.url);
  1618. }
  1619. getFromCanvas(id, canvas) {
  1620. this.#cache ||= new Map();
  1621. let data = this.#cache.get(id);
  1622. if (data?.bitmap) {
  1623. data.refCounter += 1;
  1624. return data;
  1625. }
  1626. const offscreen = new OffscreenCanvas(canvas.width, canvas.height);
  1627. const ctx = offscreen.getContext("2d");
  1628. ctx.drawImage(canvas, 0, 0);
  1629. data = {
  1630. bitmap: offscreen.transferToImageBitmap(),
  1631. id: `image_${this.#baseId}_${this.#id++}`,
  1632. refCounter: 1,
  1633. isSvg: false
  1634. };
  1635. this.#cache.set(id, data);
  1636. this.#cache.set(data.id, data);
  1637. return data;
  1638. }
  1639. getSvgUrl(id) {
  1640. const data = this.#cache.get(id);
  1641. if (!data?.isSvg) {
  1642. return null;
  1643. }
  1644. return data.svgUrl;
  1645. }
  1646. deleteId(id) {
  1647. this.#cache ||= new Map();
  1648. const data = this.#cache.get(id);
  1649. if (!data) {
  1650. return;
  1651. }
  1652. data.refCounter -= 1;
  1653. if (data.refCounter !== 0) {
  1654. return;
  1655. }
  1656. const {
  1657. bitmap
  1658. } = data;
  1659. if (!data.url && !data.file) {
  1660. const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
  1661. const ctx = canvas.getContext("bitmaprenderer");
  1662. ctx.transferFromImageBitmap(bitmap);
  1663. data.blobPromise = canvas.convertToBlob();
  1664. }
  1665. bitmap.close?.();
  1666. data.bitmap = null;
  1667. }
  1668. isValidId(id) {
  1669. return id.startsWith(`image_${this.#baseId}_`);
  1670. }
  1671. }
  1672. class CommandManager {
  1673. #commands = [];
  1674. #locked = false;
  1675. #maxSize;
  1676. #position = -1;
  1677. constructor(maxSize = 128) {
  1678. this.#maxSize = maxSize;
  1679. }
  1680. add({
  1681. cmd,
  1682. undo,
  1683. post,
  1684. mustExec,
  1685. type = NaN,
  1686. overwriteIfSameType = false,
  1687. keepUndo = false
  1688. }) {
  1689. if (mustExec) {
  1690. cmd();
  1691. }
  1692. if (this.#locked) {
  1693. return;
  1694. }
  1695. const save = {
  1696. cmd,
  1697. undo,
  1698. post,
  1699. type
  1700. };
  1701. if (this.#position === -1) {
  1702. if (this.#commands.length > 0) {
  1703. this.#commands.length = 0;
  1704. }
  1705. this.#position = 0;
  1706. this.#commands.push(save);
  1707. return;
  1708. }
  1709. if (overwriteIfSameType && this.#commands[this.#position].type === type) {
  1710. if (keepUndo) {
  1711. save.undo = this.#commands[this.#position].undo;
  1712. }
  1713. this.#commands[this.#position] = save;
  1714. return;
  1715. }
  1716. const next = this.#position + 1;
  1717. if (next === this.#maxSize) {
  1718. this.#commands.splice(0, 1);
  1719. } else {
  1720. this.#position = next;
  1721. if (next < this.#commands.length) {
  1722. this.#commands.splice(next);
  1723. }
  1724. }
  1725. this.#commands.push(save);
  1726. }
  1727. undo() {
  1728. if (this.#position === -1) {
  1729. return;
  1730. }
  1731. this.#locked = true;
  1732. const {
  1733. undo,
  1734. post
  1735. } = this.#commands[this.#position];
  1736. undo();
  1737. post?.();
  1738. this.#locked = false;
  1739. this.#position -= 1;
  1740. }
  1741. redo() {
  1742. if (this.#position < this.#commands.length - 1) {
  1743. this.#position += 1;
  1744. this.#locked = true;
  1745. const {
  1746. cmd,
  1747. post
  1748. } = this.#commands[this.#position];
  1749. cmd();
  1750. post?.();
  1751. this.#locked = false;
  1752. }
  1753. }
  1754. hasSomethingToUndo() {
  1755. return this.#position !== -1;
  1756. }
  1757. hasSomethingToRedo() {
  1758. return this.#position < this.#commands.length - 1;
  1759. }
  1760. cleanType(type) {
  1761. if (this.#position === -1) {
  1762. return;
  1763. }
  1764. for (let i = this.#position; i >= 0; i--) {
  1765. if (this.#commands[i].type !== type) {
  1766. this.#commands.splice(i + 1, this.#position - i);
  1767. this.#position = i;
  1768. return;
  1769. }
  1770. }
  1771. this.#commands.length = 0;
  1772. this.#position = -1;
  1773. }
  1774. destroy() {
  1775. this.#commands = null;
  1776. }
  1777. }
  1778. class KeyboardManager {
  1779. constructor(callbacks) {
  1780. this.buffer = [];
  1781. this.callbacks = new Map();
  1782. this.allKeys = new Set();
  1783. const {
  1784. isMac
  1785. } = util_FeatureTest.platform;
  1786. for (const [keys, callback, options = {}] of callbacks) {
  1787. for (const key of keys) {
  1788. const isMacKey = key.startsWith("mac+");
  1789. if (isMac && isMacKey) {
  1790. this.callbacks.set(key.slice(4), {
  1791. callback,
  1792. options
  1793. });
  1794. this.allKeys.add(key.split("+").at(-1));
  1795. } else if (!isMac && !isMacKey) {
  1796. this.callbacks.set(key, {
  1797. callback,
  1798. options
  1799. });
  1800. this.allKeys.add(key.split("+").at(-1));
  1801. }
  1802. }
  1803. }
  1804. }
  1805. #serialize(event) {
  1806. if (event.altKey) {
  1807. this.buffer.push("alt");
  1808. }
  1809. if (event.ctrlKey) {
  1810. this.buffer.push("ctrl");
  1811. }
  1812. if (event.metaKey) {
  1813. this.buffer.push("meta");
  1814. }
  1815. if (event.shiftKey) {
  1816. this.buffer.push("shift");
  1817. }
  1818. this.buffer.push(event.key);
  1819. const str = this.buffer.join("+");
  1820. this.buffer.length = 0;
  1821. return str;
  1822. }
  1823. exec(self, event) {
  1824. if (!this.allKeys.has(event.key)) {
  1825. return;
  1826. }
  1827. const info = this.callbacks.get(this.#serialize(event));
  1828. if (!info) {
  1829. return;
  1830. }
  1831. const {
  1832. callback,
  1833. options: {
  1834. bubbles = false,
  1835. args = [],
  1836. checker = null
  1837. }
  1838. } = info;
  1839. if (checker && !checker(self, event)) {
  1840. return;
  1841. }
  1842. callback.bind(self, ...args, event)();
  1843. if (!bubbles) {
  1844. stopEvent(event);
  1845. }
  1846. }
  1847. }
  1848. class ColorManager {
  1849. static _colorsMapping = new Map([["CanvasText", [0, 0, 0]], ["Canvas", [255, 255, 255]]]);
  1850. get _colors() {
  1851. const colors = new Map([["CanvasText", null], ["Canvas", null]]);
  1852. getColorValues(colors);
  1853. return shadow(this, "_colors", colors);
  1854. }
  1855. convert(color) {
  1856. const rgb = getRGB(color);
  1857. if (!window.matchMedia("(forced-colors: active)").matches) {
  1858. return rgb;
  1859. }
  1860. for (const [name, RGB] of this._colors) {
  1861. if (RGB.every((x, i) => x === rgb[i])) {
  1862. return ColorManager._colorsMapping.get(name);
  1863. }
  1864. }
  1865. return rgb;
  1866. }
  1867. getHexCode(name) {
  1868. const rgb = this._colors.get(name);
  1869. if (!rgb) {
  1870. return name;
  1871. }
  1872. return Util.makeHexColor(...rgb);
  1873. }
  1874. }
  1875. class AnnotationEditorUIManager {
  1876. #abortController = new AbortController();
  1877. #activeEditor = null;
  1878. #allEditors = new Map();
  1879. #allLayers = new Map();
  1880. #altTextManager = null;
  1881. #annotationStorage = null;
  1882. #changedExistingAnnotations = null;
  1883. #commandManager = new CommandManager();
  1884. #copyPasteAC = null;
  1885. #currentDrawingSession = null;
  1886. #currentPageIndex = 0;
  1887. #deletedAnnotationsElementIds = new Set();
  1888. #draggingEditors = null;
  1889. #editorTypes = null;
  1890. #editorsToRescale = new Set();
  1891. _editorUndoBar = null;
  1892. #enableHighlightFloatingButton = false;
  1893. #enableUpdatedAddImage = false;
  1894. #enableNewAltTextWhenAddingImage = false;
  1895. #filterFactory = null;
  1896. #focusMainContainerTimeoutId = null;
  1897. #focusManagerAC = null;
  1898. #highlightColors = null;
  1899. #highlightWhenShiftUp = false;
  1900. #highlightToolbar = null;
  1901. #idManager = new IdManager();
  1902. #isEnabled = false;
  1903. #isWaiting = false;
  1904. #keyboardManagerAC = null;
  1905. #lastActiveElement = null;
  1906. #mainHighlightColorPicker = null;
  1907. #missingCanvases = null;
  1908. #mlManager = null;
  1909. #mode = AnnotationEditorType.NONE;
  1910. #selectedEditors = new Set();
  1911. #selectedTextNode = null;
  1912. #signatureManager = null;
  1913. #pageColors = null;
  1914. #showAllStates = null;
  1915. #previousStates = {
  1916. isEditing: false,
  1917. isEmpty: true,
  1918. hasSomethingToUndo: false,
  1919. hasSomethingToRedo: false,
  1920. hasSelectedEditor: false,
  1921. hasSelectedText: false
  1922. };
  1923. #translation = [0, 0];
  1924. #translationTimeoutId = null;
  1925. #container = null;
  1926. #viewer = null;
  1927. #updateModeCapability = null;
  1928. static TRANSLATE_SMALL = 1;
  1929. static TRANSLATE_BIG = 10;
  1930. static get _keyboardManager() {
  1931. const proto = AnnotationEditorUIManager.prototype;
  1932. const arrowChecker = self => self.#container.contains(document.activeElement) && document.activeElement.tagName !== "BUTTON" && self.hasSomethingToControl();
  1933. const textInputChecker = (_self, {
  1934. target: el
  1935. }) => {
  1936. if (el instanceof HTMLInputElement) {
  1937. const {
  1938. type
  1939. } = el;
  1940. return type !== "text" && type !== "number";
  1941. }
  1942. return true;
  1943. };
  1944. const small = this.TRANSLATE_SMALL;
  1945. const big = this.TRANSLATE_BIG;
  1946. return shadow(this, "_keyboardManager", new KeyboardManager([[["ctrl+a", "mac+meta+a"], proto.selectAll, {
  1947. checker: textInputChecker
  1948. }], [["ctrl+z", "mac+meta+z"], proto.undo, {
  1949. checker: textInputChecker
  1950. }], [["ctrl+y", "ctrl+shift+z", "mac+meta+shift+z", "ctrl+shift+Z", "mac+meta+shift+Z"], proto.redo, {
  1951. checker: textInputChecker
  1952. }], [["Backspace", "alt+Backspace", "ctrl+Backspace", "shift+Backspace", "mac+Backspace", "mac+alt+Backspace", "mac+ctrl+Backspace", "Delete", "ctrl+Delete", "shift+Delete", "mac+Delete"], proto.delete, {
  1953. checker: textInputChecker
  1954. }], [["Enter", "mac+Enter"], proto.addNewEditorFromKeyboard, {
  1955. checker: (self, {
  1956. target: el
  1957. }) => !(el instanceof HTMLButtonElement) && self.#container.contains(el) && !self.isEnterHandled
  1958. }], [[" ", "mac+ "], proto.addNewEditorFromKeyboard, {
  1959. checker: (self, {
  1960. target: el
  1961. }) => !(el instanceof HTMLButtonElement) && self.#container.contains(document.activeElement)
  1962. }], [["Escape", "mac+Escape"], proto.unselectAll], [["ArrowLeft", "mac+ArrowLeft"], proto.translateSelectedEditors, {
  1963. args: [-small, 0],
  1964. checker: arrowChecker
  1965. }], [["ctrl+ArrowLeft", "mac+shift+ArrowLeft"], proto.translateSelectedEditors, {
  1966. args: [-big, 0],
  1967. checker: arrowChecker
  1968. }], [["ArrowRight", "mac+ArrowRight"], proto.translateSelectedEditors, {
  1969. args: [small, 0],
  1970. checker: arrowChecker
  1971. }], [["ctrl+ArrowRight", "mac+shift+ArrowRight"], proto.translateSelectedEditors, {
  1972. args: [big, 0],
  1973. checker: arrowChecker
  1974. }], [["ArrowUp", "mac+ArrowUp"], proto.translateSelectedEditors, {
  1975. args: [0, -small],
  1976. checker: arrowChecker
  1977. }], [["ctrl+ArrowUp", "mac+shift+ArrowUp"], proto.translateSelectedEditors, {
  1978. args: [0, -big],
  1979. checker: arrowChecker
  1980. }], [["ArrowDown", "mac+ArrowDown"], proto.translateSelectedEditors, {
  1981. args: [0, small],
  1982. checker: arrowChecker
  1983. }], [["ctrl+ArrowDown", "mac+shift+ArrowDown"], proto.translateSelectedEditors, {
  1984. args: [0, big],
  1985. checker: arrowChecker
  1986. }]]));
  1987. }
  1988. constructor(container, viewer, altTextManager, signatureManager, eventBus, pdfDocument, pageColors, highlightColors, enableHighlightFloatingButton, enableUpdatedAddImage, enableNewAltTextWhenAddingImage, mlManager, editorUndoBar, supportsPinchToZoom) {
  1989. const signal = this._signal = this.#abortController.signal;
  1990. this.#container = container;
  1991. this.#viewer = viewer;
  1992. this.#altTextManager = altTextManager;
  1993. this.#signatureManager = signatureManager;
  1994. this._eventBus = eventBus;
  1995. eventBus._on("editingaction", this.onEditingAction.bind(this), {
  1996. signal
  1997. });
  1998. eventBus._on("pagechanging", this.onPageChanging.bind(this), {
  1999. signal
  2000. });
  2001. eventBus._on("scalechanging", this.onScaleChanging.bind(this), {
  2002. signal
  2003. });
  2004. eventBus._on("rotationchanging", this.onRotationChanging.bind(this), {
  2005. signal
  2006. });
  2007. eventBus._on("setpreference", this.onSetPreference.bind(this), {
  2008. signal
  2009. });
  2010. eventBus._on("switchannotationeditorparams", evt => this.updateParams(evt.type, evt.value), {
  2011. signal
  2012. });
  2013. this.#addSelectionListener();
  2014. this.#addDragAndDropListeners();
  2015. this.#addKeyboardManager();
  2016. this.#annotationStorage = pdfDocument.annotationStorage;
  2017. this.#filterFactory = pdfDocument.filterFactory;
  2018. this.#pageColors = pageColors;
  2019. this.#highlightColors = highlightColors || null;
  2020. this.#enableHighlightFloatingButton = enableHighlightFloatingButton;
  2021. this.#enableUpdatedAddImage = enableUpdatedAddImage;
  2022. this.#enableNewAltTextWhenAddingImage = enableNewAltTextWhenAddingImage;
  2023. this.#mlManager = mlManager || null;
  2024. this.viewParameters = {
  2025. realScale: PixelsPerInch.PDF_TO_CSS_UNITS,
  2026. rotation: 0
  2027. };
  2028. this.isShiftKeyDown = false;
  2029. this._editorUndoBar = editorUndoBar || null;
  2030. this._supportsPinchToZoom = supportsPinchToZoom !== false;
  2031. }
  2032. destroy() {
  2033. this.#updateModeCapability?.resolve();
  2034. this.#updateModeCapability = null;
  2035. this.#abortController?.abort();
  2036. this.#abortController = null;
  2037. this._signal = null;
  2038. for (const layer of this.#allLayers.values()) {
  2039. layer.destroy();
  2040. }
  2041. this.#allLayers.clear();
  2042. this.#allEditors.clear();
  2043. this.#editorsToRescale.clear();
  2044. this.#missingCanvases?.clear();
  2045. this.#activeEditor = null;
  2046. this.#selectedEditors.clear();
  2047. this.#commandManager.destroy();
  2048. this.#altTextManager?.destroy();
  2049. this.#signatureManager?.destroy();
  2050. this.#highlightToolbar?.hide();
  2051. this.#highlightToolbar = null;
  2052. this.#mainHighlightColorPicker?.destroy();
  2053. this.#mainHighlightColorPicker = null;
  2054. if (this.#focusMainContainerTimeoutId) {
  2055. clearTimeout(this.#focusMainContainerTimeoutId);
  2056. this.#focusMainContainerTimeoutId = null;
  2057. }
  2058. if (this.#translationTimeoutId) {
  2059. clearTimeout(this.#translationTimeoutId);
  2060. this.#translationTimeoutId = null;
  2061. }
  2062. this._editorUndoBar?.destroy();
  2063. }
  2064. combinedSignal(ac) {
  2065. return AbortSignal.any([this._signal, ac.signal]);
  2066. }
  2067. get mlManager() {
  2068. return this.#mlManager;
  2069. }
  2070. get useNewAltTextFlow() {
  2071. return this.#enableUpdatedAddImage;
  2072. }
  2073. get useNewAltTextWhenAddingImage() {
  2074. return this.#enableNewAltTextWhenAddingImage;
  2075. }
  2076. get hcmFilter() {
  2077. return shadow(this, "hcmFilter", this.#pageColors ? this.#filterFactory.addHCMFilter(this.#pageColors.foreground, this.#pageColors.background) : "none");
  2078. }
  2079. get direction() {
  2080. return shadow(this, "direction", getComputedStyle(this.#container).direction);
  2081. }
  2082. get highlightColors() {
  2083. return shadow(this, "highlightColors", this.#highlightColors ? new Map(this.#highlightColors.split(",").map(pair => pair.split("=").map(x => x.trim()))) : null);
  2084. }
  2085. get highlightColorNames() {
  2086. return shadow(this, "highlightColorNames", this.highlightColors ? new Map(Array.from(this.highlightColors, e => e.reverse())) : null);
  2087. }
  2088. setCurrentDrawingSession(layer) {
  2089. if (layer) {
  2090. this.unselectAll();
  2091. this.disableUserSelect(true);
  2092. } else {
  2093. this.disableUserSelect(false);
  2094. }
  2095. this.#currentDrawingSession = layer;
  2096. }
  2097. setMainHighlightColorPicker(colorPicker) {
  2098. this.#mainHighlightColorPicker = colorPicker;
  2099. }
  2100. editAltText(editor, firstTime = false) {
  2101. this.#altTextManager?.editAltText(this, editor, firstTime);
  2102. }
  2103. getSignature(editor) {
  2104. this.#signatureManager?.getSignature({
  2105. uiManager: this,
  2106. editor
  2107. });
  2108. }
  2109. get signatureManager() {
  2110. return this.#signatureManager;
  2111. }
  2112. switchToMode(mode, callback) {
  2113. this._eventBus.on("annotationeditormodechanged", callback, {
  2114. once: true,
  2115. signal: this._signal
  2116. });
  2117. this._eventBus.dispatch("showannotationeditorui", {
  2118. source: this,
  2119. mode
  2120. });
  2121. }
  2122. setPreference(name, value) {
  2123. this._eventBus.dispatch("setpreference", {
  2124. source: this,
  2125. name,
  2126. value
  2127. });
  2128. }
  2129. onSetPreference({
  2130. name,
  2131. value
  2132. }) {
  2133. switch (name) {
  2134. case "enableNewAltTextWhenAddingImage":
  2135. this.#enableNewAltTextWhenAddingImage = value;
  2136. break;
  2137. }
  2138. }
  2139. onPageChanging({
  2140. pageNumber
  2141. }) {
  2142. this.#currentPageIndex = pageNumber - 1;
  2143. }
  2144. focusMainContainer() {
  2145. this.#container.focus();
  2146. }
  2147. findParent(x, y) {
  2148. for (const layer of this.#allLayers.values()) {
  2149. const {
  2150. x: layerX,
  2151. y: layerY,
  2152. width,
  2153. height
  2154. } = layer.div.getBoundingClientRect();
  2155. if (x >= layerX && x <= layerX + width && y >= layerY && y <= layerY + height) {
  2156. return layer;
  2157. }
  2158. }
  2159. return null;
  2160. }
  2161. disableUserSelect(value = false) {
  2162. this.#viewer.classList.toggle("noUserSelect", value);
  2163. }
  2164. addShouldRescale(editor) {
  2165. this.#editorsToRescale.add(editor);
  2166. }
  2167. removeShouldRescale(editor) {
  2168. this.#editorsToRescale.delete(editor);
  2169. }
  2170. onScaleChanging({
  2171. scale
  2172. }) {
  2173. this.commitOrRemove();
  2174. this.viewParameters.realScale = scale * PixelsPerInch.PDF_TO_CSS_UNITS;
  2175. for (const editor of this.#editorsToRescale) {
  2176. editor.onScaleChanging();
  2177. }
  2178. this.#currentDrawingSession?.onScaleChanging();
  2179. }
  2180. onRotationChanging({
  2181. pagesRotation
  2182. }) {
  2183. this.commitOrRemove();
  2184. this.viewParameters.rotation = pagesRotation;
  2185. }
  2186. #getAnchorElementForSelection({
  2187. anchorNode
  2188. }) {
  2189. return anchorNode.nodeType === Node.TEXT_NODE ? anchorNode.parentElement : anchorNode;
  2190. }
  2191. #getLayerForTextLayer(textLayer) {
  2192. const {
  2193. currentLayer
  2194. } = this;
  2195. if (currentLayer.hasTextLayer(textLayer)) {
  2196. return currentLayer;
  2197. }
  2198. for (const layer of this.#allLayers.values()) {
  2199. if (layer.hasTextLayer(textLayer)) {
  2200. return layer;
  2201. }
  2202. }
  2203. return null;
  2204. }
  2205. highlightSelection(methodOfCreation = "") {
  2206. const selection = document.getSelection();
  2207. if (!selection || selection.isCollapsed) {
  2208. return;
  2209. }
  2210. const {
  2211. anchorNode,
  2212. anchorOffset,
  2213. focusNode,
  2214. focusOffset
  2215. } = selection;
  2216. const text = selection.toString();
  2217. const anchorElement = this.#getAnchorElementForSelection(selection);
  2218. const textLayer = anchorElement.closest(".textLayer");
  2219. const boxes = this.getSelectionBoxes(textLayer);
  2220. if (!boxes) {
  2221. return;
  2222. }
  2223. selection.empty();
  2224. const layer = this.#getLayerForTextLayer(textLayer);
  2225. const isNoneMode = this.#mode === AnnotationEditorType.NONE;
  2226. const callback = () => {
  2227. layer?.createAndAddNewEditor({
  2228. x: 0,
  2229. y: 0
  2230. }, false, {
  2231. methodOfCreation,
  2232. boxes,
  2233. anchorNode,
  2234. anchorOffset,
  2235. focusNode,
  2236. focusOffset,
  2237. text
  2238. });
  2239. if (isNoneMode) {
  2240. this.showAllEditors("highlight", true, true);
  2241. }
  2242. };
  2243. if (isNoneMode) {
  2244. this.switchToMode(AnnotationEditorType.HIGHLIGHT, callback);
  2245. return;
  2246. }
  2247. callback();
  2248. }
  2249. #displayHighlightToolbar() {
  2250. const selection = document.getSelection();
  2251. if (!selection || selection.isCollapsed) {
  2252. return;
  2253. }
  2254. const anchorElement = this.#getAnchorElementForSelection(selection);
  2255. const textLayer = anchorElement.closest(".textLayer");
  2256. const boxes = this.getSelectionBoxes(textLayer);
  2257. if (!boxes) {
  2258. return;
  2259. }
  2260. this.#highlightToolbar ||= new HighlightToolbar(this);
  2261. this.#highlightToolbar.show(textLayer, boxes, this.direction === "ltr");
  2262. }
  2263. addToAnnotationStorage(editor) {
  2264. if (!editor.isEmpty() && this.#annotationStorage && !this.#annotationStorage.has(editor.id)) {
  2265. this.#annotationStorage.setValue(editor.id, editor);
  2266. }
  2267. }
  2268. #selectionChange() {
  2269. const selection = document.getSelection();
  2270. if (!selection || selection.isCollapsed) {
  2271. if (this.#selectedTextNode) {
  2272. this.#highlightToolbar?.hide();
  2273. this.#selectedTextNode = null;
  2274. this.#dispatchUpdateStates({
  2275. hasSelectedText: false
  2276. });
  2277. }
  2278. return;
  2279. }
  2280. const {
  2281. anchorNode
  2282. } = selection;
  2283. if (anchorNode === this.#selectedTextNode) {
  2284. return;
  2285. }
  2286. const anchorElement = this.#getAnchorElementForSelection(selection);
  2287. const textLayer = anchorElement.closest(".textLayer");
  2288. if (!textLayer) {
  2289. if (this.#selectedTextNode) {
  2290. this.#highlightToolbar?.hide();
  2291. this.#selectedTextNode = null;
  2292. this.#dispatchUpdateStates({
  2293. hasSelectedText: false
  2294. });
  2295. }
  2296. return;
  2297. }
  2298. this.#highlightToolbar?.hide();
  2299. this.#selectedTextNode = anchorNode;
  2300. this.#dispatchUpdateStates({
  2301. hasSelectedText: true
  2302. });
  2303. if (this.#mode !== AnnotationEditorType.HIGHLIGHT && this.#mode !== AnnotationEditorType.NONE) {
  2304. return;
  2305. }
  2306. if (this.#mode === AnnotationEditorType.HIGHLIGHT) {
  2307. this.showAllEditors("highlight", true, true);
  2308. }
  2309. this.#highlightWhenShiftUp = this.isShiftKeyDown;
  2310. if (!this.isShiftKeyDown) {
  2311. const activeLayer = this.#mode === AnnotationEditorType.HIGHLIGHT ? this.#getLayerForTextLayer(textLayer) : null;
  2312. activeLayer?.toggleDrawing();
  2313. const ac = new AbortController();
  2314. const signal = this.combinedSignal(ac);
  2315. const pointerup = e => {
  2316. if (e.type === "pointerup" && e.button !== 0) {
  2317. return;
  2318. }
  2319. ac.abort();
  2320. activeLayer?.toggleDrawing(true);
  2321. if (e.type === "pointerup") {
  2322. this.#onSelectEnd("main_toolbar");
  2323. }
  2324. };
  2325. window.addEventListener("pointerup", pointerup, {
  2326. signal
  2327. });
  2328. window.addEventListener("blur", pointerup, {
  2329. signal
  2330. });
  2331. }
  2332. }
  2333. #onSelectEnd(methodOfCreation = "") {
  2334. if (this.#mode === AnnotationEditorType.HIGHLIGHT) {
  2335. this.highlightSelection(methodOfCreation);
  2336. } else if (this.#enableHighlightFloatingButton) {
  2337. this.#displayHighlightToolbar();
  2338. }
  2339. }
  2340. #addSelectionListener() {
  2341. document.addEventListener("selectionchange", this.#selectionChange.bind(this), {
  2342. signal: this._signal
  2343. });
  2344. }
  2345. #addFocusManager() {
  2346. if (this.#focusManagerAC) {
  2347. return;
  2348. }
  2349. this.#focusManagerAC = new AbortController();
  2350. const signal = this.combinedSignal(this.#focusManagerAC);
  2351. window.addEventListener("focus", this.focus.bind(this), {
  2352. signal
  2353. });
  2354. window.addEventListener("blur", this.blur.bind(this), {
  2355. signal
  2356. });
  2357. }
  2358. #removeFocusManager() {
  2359. this.#focusManagerAC?.abort();
  2360. this.#focusManagerAC = null;
  2361. }
  2362. blur() {
  2363. this.isShiftKeyDown = false;
  2364. if (this.#highlightWhenShiftUp) {
  2365. this.#highlightWhenShiftUp = false;
  2366. this.#onSelectEnd("main_toolbar");
  2367. }
  2368. if (!this.hasSelection) {
  2369. return;
  2370. }
  2371. const {
  2372. activeElement
  2373. } = document;
  2374. for (const editor of this.#selectedEditors) {
  2375. if (editor.div.contains(activeElement)) {
  2376. this.#lastActiveElement = [editor, activeElement];
  2377. editor._focusEventsAllowed = false;
  2378. break;
  2379. }
  2380. }
  2381. }
  2382. focus() {
  2383. if (!this.#lastActiveElement) {
  2384. return;
  2385. }
  2386. const [lastEditor, lastActiveElement] = this.#lastActiveElement;
  2387. this.#lastActiveElement = null;
  2388. lastActiveElement.addEventListener("focusin", () => {
  2389. lastEditor._focusEventsAllowed = true;
  2390. }, {
  2391. once: true,
  2392. signal: this._signal
  2393. });
  2394. lastActiveElement.focus();
  2395. }
  2396. #addKeyboardManager() {
  2397. if (this.#keyboardManagerAC) {
  2398. return;
  2399. }
  2400. this.#keyboardManagerAC = new AbortController();
  2401. const signal = this.combinedSignal(this.#keyboardManagerAC);
  2402. window.addEventListener("keydown", this.keydown.bind(this), {
  2403. signal
  2404. });
  2405. window.addEventListener("keyup", this.keyup.bind(this), {
  2406. signal
  2407. });
  2408. }
  2409. #removeKeyboardManager() {
  2410. this.#keyboardManagerAC?.abort();
  2411. this.#keyboardManagerAC = null;
  2412. }
  2413. #addCopyPasteListeners() {
  2414. if (this.#copyPasteAC) {
  2415. return;
  2416. }
  2417. this.#copyPasteAC = new AbortController();
  2418. const signal = this.combinedSignal(this.#copyPasteAC);
  2419. document.addEventListener("copy", this.copy.bind(this), {
  2420. signal
  2421. });
  2422. document.addEventListener("cut", this.cut.bind(this), {
  2423. signal
  2424. });
  2425. document.addEventListener("paste", this.paste.bind(this), {
  2426. signal
  2427. });
  2428. }
  2429. #removeCopyPasteListeners() {
  2430. this.#copyPasteAC?.abort();
  2431. this.#copyPasteAC = null;
  2432. }
  2433. #addDragAndDropListeners() {
  2434. const signal = this._signal;
  2435. document.addEventListener("dragover", this.dragOver.bind(this), {
  2436. signal
  2437. });
  2438. document.addEventListener("drop", this.drop.bind(this), {
  2439. signal
  2440. });
  2441. }
  2442. addEditListeners() {
  2443. this.#addKeyboardManager();
  2444. this.#addCopyPasteListeners();
  2445. }
  2446. removeEditListeners() {
  2447. this.#removeKeyboardManager();
  2448. this.#removeCopyPasteListeners();
  2449. }
  2450. dragOver(event) {
  2451. for (const {
  2452. type
  2453. } of event.dataTransfer.items) {
  2454. for (const editorType of this.#editorTypes) {
  2455. if (editorType.isHandlingMimeForPasting(type)) {
  2456. event.dataTransfer.dropEffect = "copy";
  2457. event.preventDefault();
  2458. return;
  2459. }
  2460. }
  2461. }
  2462. }
  2463. drop(event) {
  2464. for (const item of event.dataTransfer.items) {
  2465. for (const editorType of this.#editorTypes) {
  2466. if (editorType.isHandlingMimeForPasting(item.type)) {
  2467. editorType.paste(item, this.currentLayer);
  2468. event.preventDefault();
  2469. return;
  2470. }
  2471. }
  2472. }
  2473. }
  2474. copy(event) {
  2475. event.preventDefault();
  2476. this.#activeEditor?.commitOrRemove();
  2477. if (!this.hasSelection) {
  2478. return;
  2479. }
  2480. const editors = [];
  2481. for (const editor of this.#selectedEditors) {
  2482. const serialized = editor.serialize(true);
  2483. if (serialized) {
  2484. editors.push(serialized);
  2485. }
  2486. }
  2487. if (editors.length === 0) {
  2488. return;
  2489. }
  2490. event.clipboardData.setData("application/pdfjs", JSON.stringify(editors));
  2491. }
  2492. cut(event) {
  2493. this.copy(event);
  2494. this.delete();
  2495. }
  2496. async paste(event) {
  2497. event.preventDefault();
  2498. const {
  2499. clipboardData
  2500. } = event;
  2501. for (const item of clipboardData.items) {
  2502. for (const editorType of this.#editorTypes) {
  2503. if (editorType.isHandlingMimeForPasting(item.type)) {
  2504. editorType.paste(item, this.currentLayer);
  2505. return;
  2506. }
  2507. }
  2508. }
  2509. let data = clipboardData.getData("application/pdfjs");
  2510. if (!data) {
  2511. return;
  2512. }
  2513. try {
  2514. data = JSON.parse(data);
  2515. } catch (ex) {
  2516. warn(`paste: "${ex.message}".`);
  2517. return;
  2518. }
  2519. if (!Array.isArray(data)) {
  2520. return;
  2521. }
  2522. this.unselectAll();
  2523. const layer = this.currentLayer;
  2524. try {
  2525. const newEditors = [];
  2526. for (const editor of data) {
  2527. const deserializedEditor = await layer.deserialize(editor);
  2528. if (!deserializedEditor) {
  2529. return;
  2530. }
  2531. newEditors.push(deserializedEditor);
  2532. }
  2533. const cmd = () => {
  2534. for (const editor of newEditors) {
  2535. this.#addEditorToLayer(editor);
  2536. }
  2537. this.#selectEditors(newEditors);
  2538. };
  2539. const undo = () => {
  2540. for (const editor of newEditors) {
  2541. editor.remove();
  2542. }
  2543. };
  2544. this.addCommands({
  2545. cmd,
  2546. undo,
  2547. mustExec: true
  2548. });
  2549. } catch (ex) {
  2550. warn(`paste: "${ex.message}".`);
  2551. }
  2552. }
  2553. keydown(event) {
  2554. if (!this.isShiftKeyDown && event.key === "Shift") {
  2555. this.isShiftKeyDown = true;
  2556. }
  2557. if (this.#mode !== AnnotationEditorType.NONE && !this.isEditorHandlingKeyboard) {
  2558. AnnotationEditorUIManager._keyboardManager.exec(this, event);
  2559. }
  2560. }
  2561. keyup(event) {
  2562. if (this.isShiftKeyDown && event.key === "Shift") {
  2563. this.isShiftKeyDown = false;
  2564. if (this.#highlightWhenShiftUp) {
  2565. this.#highlightWhenShiftUp = false;
  2566. this.#onSelectEnd("main_toolbar");
  2567. }
  2568. }
  2569. }
  2570. onEditingAction({
  2571. name
  2572. }) {
  2573. switch (name) {
  2574. case "undo":
  2575. case "redo":
  2576. case "delete":
  2577. case "selectAll":
  2578. this[name]();
  2579. break;
  2580. case "highlightSelection":
  2581. this.highlightSelection("context_menu");
  2582. break;
  2583. }
  2584. }
  2585. #dispatchUpdateStates(details) {
  2586. const hasChanged = Object.entries(details).some(([key, value]) => this.#previousStates[key] !== value);
  2587. if (hasChanged) {
  2588. this._eventBus.dispatch("annotationeditorstateschanged", {
  2589. source: this,
  2590. details: Object.assign(this.#previousStates, details)
  2591. });
  2592. if (this.#mode === AnnotationEditorType.HIGHLIGHT && details.hasSelectedEditor === false) {
  2593. this.#dispatchUpdateUI([[AnnotationEditorParamsType.HIGHLIGHT_FREE, true]]);
  2594. }
  2595. }
  2596. }
  2597. #dispatchUpdateUI(details) {
  2598. this._eventBus.dispatch("annotationeditorparamschanged", {
  2599. source: this,
  2600. details
  2601. });
  2602. }
  2603. setEditingState(isEditing) {
  2604. if (isEditing) {
  2605. this.#addFocusManager();
  2606. this.#addCopyPasteListeners();
  2607. this.#dispatchUpdateStates({
  2608. isEditing: this.#mode !== AnnotationEditorType.NONE,
  2609. isEmpty: this.#isEmpty(),
  2610. hasSomethingToUndo: this.#commandManager.hasSomethingToUndo(),
  2611. hasSomethingToRedo: this.#commandManager.hasSomethingToRedo(),
  2612. hasSelectedEditor: false
  2613. });
  2614. } else {
  2615. this.#removeFocusManager();
  2616. this.#removeCopyPasteListeners();
  2617. this.#dispatchUpdateStates({
  2618. isEditing: false
  2619. });
  2620. this.disableUserSelect(false);
  2621. }
  2622. }
  2623. registerEditorTypes(types) {
  2624. if (this.#editorTypes) {
  2625. return;
  2626. }
  2627. this.#editorTypes = types;
  2628. for (const editorType of this.#editorTypes) {
  2629. this.#dispatchUpdateUI(editorType.defaultPropertiesToUpdate);
  2630. }
  2631. }
  2632. getId() {
  2633. return this.#idManager.id;
  2634. }
  2635. get currentLayer() {
  2636. return this.#allLayers.get(this.#currentPageIndex);
  2637. }
  2638. getLayer(pageIndex) {
  2639. return this.#allLayers.get(pageIndex);
  2640. }
  2641. get currentPageIndex() {
  2642. return this.#currentPageIndex;
  2643. }
  2644. addLayer(layer) {
  2645. this.#allLayers.set(layer.pageIndex, layer);
  2646. if (this.#isEnabled) {
  2647. layer.enable();
  2648. } else {
  2649. layer.disable();
  2650. }
  2651. }
  2652. removeLayer(layer) {
  2653. this.#allLayers.delete(layer.pageIndex);
  2654. }
  2655. async updateMode(mode, editId = null, isFromKeyboard = false) {
  2656. if (this.#mode === mode) {
  2657. return;
  2658. }
  2659. if (this.#updateModeCapability) {
  2660. await this.#updateModeCapability.promise;
  2661. if (!this.#updateModeCapability) {
  2662. return;
  2663. }
  2664. }
  2665. this.#updateModeCapability = Promise.withResolvers();
  2666. this.#mode = mode;
  2667. if (mode === AnnotationEditorType.NONE) {
  2668. this.setEditingState(false);
  2669. this.#disableAll();
  2670. this._editorUndoBar?.hide();
  2671. this.#updateModeCapability.resolve();
  2672. return;
  2673. }
  2674. if (mode === AnnotationEditorType.SIGNATURE) {
  2675. await this.#signatureManager?.loadSignatures();
  2676. }
  2677. this.setEditingState(true);
  2678. await this.#enableAll();
  2679. this.unselectAll();
  2680. for (const layer of this.#allLayers.values()) {
  2681. layer.updateMode(mode);
  2682. }
  2683. if (!editId) {
  2684. if (isFromKeyboard) {
  2685. this.addNewEditorFromKeyboard();
  2686. }
  2687. this.#updateModeCapability.resolve();
  2688. return;
  2689. }
  2690. for (const editor of this.#allEditors.values()) {
  2691. if (editor.annotationElementId === editId) {
  2692. this.setSelected(editor);
  2693. editor.enterInEditMode();
  2694. } else {
  2695. editor.unselect();
  2696. }
  2697. }
  2698. this.#updateModeCapability.resolve();
  2699. }
  2700. addNewEditorFromKeyboard() {
  2701. if (this.currentLayer.canCreateNewEmptyEditor()) {
  2702. this.currentLayer.addNewEditor();
  2703. }
  2704. }
  2705. updateToolbar(mode) {
  2706. if (mode === this.#mode) {
  2707. return;
  2708. }
  2709. this._eventBus.dispatch("switchannotationeditormode", {
  2710. source: this,
  2711. mode
  2712. });
  2713. }
  2714. updateParams(type, value) {
  2715. if (!this.#editorTypes) {
  2716. return;
  2717. }
  2718. switch (type) {
  2719. case AnnotationEditorParamsType.CREATE:
  2720. this.currentLayer.addNewEditor(value);
  2721. return;
  2722. case AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR:
  2723. this.#mainHighlightColorPicker?.updateColor(value);
  2724. break;
  2725. case AnnotationEditorParamsType.HIGHLIGHT_SHOW_ALL:
  2726. this._eventBus.dispatch("reporttelemetry", {
  2727. source: this,
  2728. details: {
  2729. type: "editing",
  2730. data: {
  2731. type: "highlight",
  2732. action: "toggle_visibility"
  2733. }
  2734. }
  2735. });
  2736. (this.#showAllStates ||= new Map()).set(type, value);
  2737. this.showAllEditors("highlight", value);
  2738. break;
  2739. }
  2740. for (const editor of this.#selectedEditors) {
  2741. editor.updateParams(type, value);
  2742. }
  2743. for (const editorType of this.#editorTypes) {
  2744. editorType.updateDefaultParams(type, value);
  2745. }
  2746. }
  2747. showAllEditors(type, visible, updateButton = false) {
  2748. for (const editor of this.#allEditors.values()) {
  2749. if (editor.editorType === type) {
  2750. editor.show(visible);
  2751. }
  2752. }
  2753. const state = this.#showAllStates?.get(AnnotationEditorParamsType.HIGHLIGHT_SHOW_ALL) ?? true;
  2754. if (state !== visible) {
  2755. this.#dispatchUpdateUI([[AnnotationEditorParamsType.HIGHLIGHT_SHOW_ALL, visible]]);
  2756. }
  2757. }
  2758. enableWaiting(mustWait = false) {
  2759. if (this.#isWaiting === mustWait) {
  2760. return;
  2761. }
  2762. this.#isWaiting = mustWait;
  2763. for (const layer of this.#allLayers.values()) {
  2764. if (mustWait) {
  2765. layer.disableClick();
  2766. } else {
  2767. layer.enableClick();
  2768. }
  2769. layer.div.classList.toggle("waiting", mustWait);
  2770. }
  2771. }
  2772. async #enableAll() {
  2773. if (!this.#isEnabled) {
  2774. this.#isEnabled = true;
  2775. const promises = [];
  2776. for (const layer of this.#allLayers.values()) {
  2777. promises.push(layer.enable());
  2778. }
  2779. await Promise.all(promises);
  2780. for (const editor of this.#allEditors.values()) {
  2781. editor.enable();
  2782. }
  2783. }
  2784. }
  2785. #disableAll() {
  2786. this.unselectAll();
  2787. if (this.#isEnabled) {
  2788. this.#isEnabled = false;
  2789. for (const layer of this.#allLayers.values()) {
  2790. layer.disable();
  2791. }
  2792. for (const editor of this.#allEditors.values()) {
  2793. editor.disable();
  2794. }
  2795. }
  2796. }
  2797. getEditors(pageIndex) {
  2798. const editors = [];
  2799. for (const editor of this.#allEditors.values()) {
  2800. if (editor.pageIndex === pageIndex) {
  2801. editors.push(editor);
  2802. }
  2803. }
  2804. return editors;
  2805. }
  2806. getEditor(id) {
  2807. return this.#allEditors.get(id);
  2808. }
  2809. addEditor(editor) {
  2810. this.#allEditors.set(editor.id, editor);
  2811. }
  2812. removeEditor(editor) {
  2813. if (editor.div.contains(document.activeElement)) {
  2814. if (this.#focusMainContainerTimeoutId) {
  2815. clearTimeout(this.#focusMainContainerTimeoutId);
  2816. }
  2817. this.#focusMainContainerTimeoutId = setTimeout(() => {
  2818. this.focusMainContainer();
  2819. this.#focusMainContainerTimeoutId = null;
  2820. }, 0);
  2821. }
  2822. this.#allEditors.delete(editor.id);
  2823. if (editor.annotationElementId) {
  2824. this.#missingCanvases?.delete(editor.annotationElementId);
  2825. }
  2826. this.unselect(editor);
  2827. if (!editor.annotationElementId || !this.#deletedAnnotationsElementIds.has(editor.annotationElementId)) {
  2828. this.#annotationStorage?.remove(editor.id);
  2829. }
  2830. }
  2831. addDeletedAnnotationElement(editor) {
  2832. this.#deletedAnnotationsElementIds.add(editor.annotationElementId);
  2833. this.addChangedExistingAnnotation(editor);
  2834. editor.deleted = true;
  2835. }
  2836. isDeletedAnnotationElement(annotationElementId) {
  2837. return this.#deletedAnnotationsElementIds.has(annotationElementId);
  2838. }
  2839. removeDeletedAnnotationElement(editor) {
  2840. this.#deletedAnnotationsElementIds.delete(editor.annotationElementId);
  2841. this.removeChangedExistingAnnotation(editor);
  2842. editor.deleted = false;
  2843. }
  2844. #addEditorToLayer(editor) {
  2845. const layer = this.#allLayers.get(editor.pageIndex);
  2846. if (layer) {
  2847. layer.addOrRebuild(editor);
  2848. } else {
  2849. this.addEditor(editor);
  2850. this.addToAnnotationStorage(editor);
  2851. }
  2852. }
  2853. setActiveEditor(editor) {
  2854. if (this.#activeEditor === editor) {
  2855. return;
  2856. }
  2857. this.#activeEditor = editor;
  2858. if (editor) {
  2859. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2860. }
  2861. }
  2862. get #lastSelectedEditor() {
  2863. let ed = null;
  2864. for (ed of this.#selectedEditors) {}
  2865. return ed;
  2866. }
  2867. updateUI(editor) {
  2868. if (this.#lastSelectedEditor === editor) {
  2869. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2870. }
  2871. }
  2872. updateUIForDefaultProperties(editorType) {
  2873. this.#dispatchUpdateUI(editorType.defaultPropertiesToUpdate);
  2874. }
  2875. toggleSelected(editor) {
  2876. if (this.#selectedEditors.has(editor)) {
  2877. this.#selectedEditors.delete(editor);
  2878. editor.unselect();
  2879. this.#dispatchUpdateStates({
  2880. hasSelectedEditor: this.hasSelection
  2881. });
  2882. return;
  2883. }
  2884. this.#selectedEditors.add(editor);
  2885. editor.select();
  2886. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2887. this.#dispatchUpdateStates({
  2888. hasSelectedEditor: true
  2889. });
  2890. }
  2891. setSelected(editor) {
  2892. this.#currentDrawingSession?.commitOrRemove();
  2893. for (const ed of this.#selectedEditors) {
  2894. if (ed !== editor) {
  2895. ed.unselect();
  2896. }
  2897. }
  2898. this.#selectedEditors.clear();
  2899. this.#selectedEditors.add(editor);
  2900. editor.select();
  2901. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2902. this.#dispatchUpdateStates({
  2903. hasSelectedEditor: true
  2904. });
  2905. }
  2906. isSelected(editor) {
  2907. return this.#selectedEditors.has(editor);
  2908. }
  2909. get firstSelectedEditor() {
  2910. return this.#selectedEditors.values().next().value;
  2911. }
  2912. unselect(editor) {
  2913. editor.unselect();
  2914. this.#selectedEditors.delete(editor);
  2915. this.#dispatchUpdateStates({
  2916. hasSelectedEditor: this.hasSelection
  2917. });
  2918. }
  2919. get hasSelection() {
  2920. return this.#selectedEditors.size !== 0;
  2921. }
  2922. get isEnterHandled() {
  2923. return this.#selectedEditors.size === 1 && this.firstSelectedEditor.isEnterHandled;
  2924. }
  2925. undo() {
  2926. this.#commandManager.undo();
  2927. this.#dispatchUpdateStates({
  2928. hasSomethingToUndo: this.#commandManager.hasSomethingToUndo(),
  2929. hasSomethingToRedo: true,
  2930. isEmpty: this.#isEmpty()
  2931. });
  2932. this._editorUndoBar?.hide();
  2933. }
  2934. redo() {
  2935. this.#commandManager.redo();
  2936. this.#dispatchUpdateStates({
  2937. hasSomethingToUndo: true,
  2938. hasSomethingToRedo: this.#commandManager.hasSomethingToRedo(),
  2939. isEmpty: this.#isEmpty()
  2940. });
  2941. }
  2942. addCommands(params) {
  2943. this.#commandManager.add(params);
  2944. this.#dispatchUpdateStates({
  2945. hasSomethingToUndo: true,
  2946. hasSomethingToRedo: false,
  2947. isEmpty: this.#isEmpty()
  2948. });
  2949. }
  2950. cleanUndoStack(type) {
  2951. this.#commandManager.cleanType(type);
  2952. }
  2953. #isEmpty() {
  2954. if (this.#allEditors.size === 0) {
  2955. return true;
  2956. }
  2957. if (this.#allEditors.size === 1) {
  2958. for (const editor of this.#allEditors.values()) {
  2959. return editor.isEmpty();
  2960. }
  2961. }
  2962. return false;
  2963. }
  2964. delete() {
  2965. this.commitOrRemove();
  2966. const drawingEditor = this.currentLayer?.endDrawingSession(true);
  2967. if (!this.hasSelection && !drawingEditor) {
  2968. return;
  2969. }
  2970. const editors = drawingEditor ? [drawingEditor] : [...this.#selectedEditors];
  2971. const cmd = () => {
  2972. this._editorUndoBar?.show(undo, editors.length === 1 ? editors[0].editorType : editors.length);
  2973. for (const editor of editors) {
  2974. editor.remove();
  2975. }
  2976. };
  2977. const undo = () => {
  2978. for (const editor of editors) {
  2979. this.#addEditorToLayer(editor);
  2980. }
  2981. };
  2982. this.addCommands({
  2983. cmd,
  2984. undo,
  2985. mustExec: true
  2986. });
  2987. }
  2988. commitOrRemove() {
  2989. this.#activeEditor?.commitOrRemove();
  2990. }
  2991. hasSomethingToControl() {
  2992. return this.#activeEditor || this.hasSelection;
  2993. }
  2994. #selectEditors(editors) {
  2995. for (const editor of this.#selectedEditors) {
  2996. editor.unselect();
  2997. }
  2998. this.#selectedEditors.clear();
  2999. for (const editor of editors) {
  3000. if (editor.isEmpty()) {
  3001. continue;
  3002. }
  3003. this.#selectedEditors.add(editor);
  3004. editor.select();
  3005. }
  3006. this.#dispatchUpdateStates({
  3007. hasSelectedEditor: this.hasSelection
  3008. });
  3009. }
  3010. selectAll() {
  3011. for (const editor of this.#selectedEditors) {
  3012. editor.commit();
  3013. }
  3014. this.#selectEditors(this.#allEditors.values());
  3015. }
  3016. unselectAll() {
  3017. if (this.#activeEditor) {
  3018. this.#activeEditor.commitOrRemove();
  3019. if (this.#mode !== AnnotationEditorType.NONE) {
  3020. return;
  3021. }
  3022. }
  3023. if (this.#currentDrawingSession?.commitOrRemove()) {
  3024. return;
  3025. }
  3026. if (!this.hasSelection) {
  3027. return;
  3028. }
  3029. for (const editor of this.#selectedEditors) {
  3030. editor.unselect();
  3031. }
  3032. this.#selectedEditors.clear();
  3033. this.#dispatchUpdateStates({
  3034. hasSelectedEditor: false
  3035. });
  3036. }
  3037. translateSelectedEditors(x, y, noCommit = false) {
  3038. if (!noCommit) {
  3039. this.commitOrRemove();
  3040. }
  3041. if (!this.hasSelection) {
  3042. return;
  3043. }
  3044. this.#translation[0] += x;
  3045. this.#translation[1] += y;
  3046. const [totalX, totalY] = this.#translation;
  3047. const editors = [...this.#selectedEditors];
  3048. const TIME_TO_WAIT = 1000;
  3049. if (this.#translationTimeoutId) {
  3050. clearTimeout(this.#translationTimeoutId);
  3051. }
  3052. this.#translationTimeoutId = setTimeout(() => {
  3053. this.#translationTimeoutId = null;
  3054. this.#translation[0] = this.#translation[1] = 0;
  3055. this.addCommands({
  3056. cmd: () => {
  3057. for (const editor of editors) {
  3058. if (this.#allEditors.has(editor.id)) {
  3059. editor.translateInPage(totalX, totalY);
  3060. editor.translationDone();
  3061. }
  3062. }
  3063. },
  3064. undo: () => {
  3065. for (const editor of editors) {
  3066. if (this.#allEditors.has(editor.id)) {
  3067. editor.translateInPage(-totalX, -totalY);
  3068. editor.translationDone();
  3069. }
  3070. }
  3071. },
  3072. mustExec: false
  3073. });
  3074. }, TIME_TO_WAIT);
  3075. for (const editor of editors) {
  3076. editor.translateInPage(x, y);
  3077. editor.translationDone();
  3078. }
  3079. }
  3080. setUpDragSession() {
  3081. if (!this.hasSelection) {
  3082. return;
  3083. }
  3084. this.disableUserSelect(true);
  3085. this.#draggingEditors = new Map();
  3086. for (const editor of this.#selectedEditors) {
  3087. this.#draggingEditors.set(editor, {
  3088. savedX: editor.x,
  3089. savedY: editor.y,
  3090. savedPageIndex: editor.pageIndex,
  3091. newX: 0,
  3092. newY: 0,
  3093. newPageIndex: -1
  3094. });
  3095. }
  3096. }
  3097. endDragSession() {
  3098. if (!this.#draggingEditors) {
  3099. return false;
  3100. }
  3101. this.disableUserSelect(false);
  3102. const map = this.#draggingEditors;
  3103. this.#draggingEditors = null;
  3104. let mustBeAddedInUndoStack = false;
  3105. for (const [{
  3106. x,
  3107. y,
  3108. pageIndex
  3109. }, value] of map) {
  3110. value.newX = x;
  3111. value.newY = y;
  3112. value.newPageIndex = pageIndex;
  3113. mustBeAddedInUndoStack ||= x !== value.savedX || y !== value.savedY || pageIndex !== value.savedPageIndex;
  3114. }
  3115. if (!mustBeAddedInUndoStack) {
  3116. return false;
  3117. }
  3118. const move = (editor, x, y, pageIndex) => {
  3119. if (this.#allEditors.has(editor.id)) {
  3120. const parent = this.#allLayers.get(pageIndex);
  3121. if (parent) {
  3122. editor._setParentAndPosition(parent, x, y);
  3123. } else {
  3124. editor.pageIndex = pageIndex;
  3125. editor.x = x;
  3126. editor.y = y;
  3127. }
  3128. }
  3129. };
  3130. this.addCommands({
  3131. cmd: () => {
  3132. for (const [editor, {
  3133. newX,
  3134. newY,
  3135. newPageIndex
  3136. }] of map) {
  3137. move(editor, newX, newY, newPageIndex);
  3138. }
  3139. },
  3140. undo: () => {
  3141. for (const [editor, {
  3142. savedX,
  3143. savedY,
  3144. savedPageIndex
  3145. }] of map) {
  3146. move(editor, savedX, savedY, savedPageIndex);
  3147. }
  3148. },
  3149. mustExec: true
  3150. });
  3151. return true;
  3152. }
  3153. dragSelectedEditors(tx, ty) {
  3154. if (!this.#draggingEditors) {
  3155. return;
  3156. }
  3157. for (const editor of this.#draggingEditors.keys()) {
  3158. editor.drag(tx, ty);
  3159. }
  3160. }
  3161. rebuild(editor) {
  3162. if (editor.parent === null) {
  3163. const parent = this.getLayer(editor.pageIndex);
  3164. if (parent) {
  3165. parent.changeParent(editor);
  3166. parent.addOrRebuild(editor);
  3167. } else {
  3168. this.addEditor(editor);
  3169. this.addToAnnotationStorage(editor);
  3170. editor.rebuild();
  3171. }
  3172. } else {
  3173. editor.parent.addOrRebuild(editor);
  3174. }
  3175. }
  3176. get isEditorHandlingKeyboard() {
  3177. return this.getActive()?.shouldGetKeyboardEvents() || this.#selectedEditors.size === 1 && this.firstSelectedEditor.shouldGetKeyboardEvents();
  3178. }
  3179. isActive(editor) {
  3180. return this.#activeEditor === editor;
  3181. }
  3182. getActive() {
  3183. return this.#activeEditor;
  3184. }
  3185. getMode() {
  3186. return this.#mode;
  3187. }
  3188. get imageManager() {
  3189. return shadow(this, "imageManager", new ImageManager());
  3190. }
  3191. getSelectionBoxes(textLayer) {
  3192. if (!textLayer) {
  3193. return null;
  3194. }
  3195. const selection = document.getSelection();
  3196. for (let i = 0, ii = selection.rangeCount; i < ii; i++) {
  3197. if (!textLayer.contains(selection.getRangeAt(i).commonAncestorContainer)) {
  3198. return null;
  3199. }
  3200. }
  3201. const {
  3202. x: layerX,
  3203. y: layerY,
  3204. width: parentWidth,
  3205. height: parentHeight
  3206. } = textLayer.getBoundingClientRect();
  3207. let rotator;
  3208. switch (textLayer.getAttribute("data-main-rotation")) {
  3209. case "90":
  3210. rotator = (x, y, w, h) => ({
  3211. x: (y - layerY) / parentHeight,
  3212. y: 1 - (x + w - layerX) / parentWidth,
  3213. width: h / parentHeight,
  3214. height: w / parentWidth
  3215. });
  3216. break;
  3217. case "180":
  3218. rotator = (x, y, w, h) => ({
  3219. x: 1 - (x + w - layerX) / parentWidth,
  3220. y: 1 - (y + h - layerY) / parentHeight,
  3221. width: w / parentWidth,
  3222. height: h / parentHeight
  3223. });
  3224. break;
  3225. case "270":
  3226. rotator = (x, y, w, h) => ({
  3227. x: 1 - (y + h - layerY) / parentHeight,
  3228. y: (x - layerX) / parentWidth,
  3229. width: h / parentHeight,
  3230. height: w / parentWidth
  3231. });
  3232. break;
  3233. default:
  3234. rotator = (x, y, w, h) => ({
  3235. x: (x - layerX) / parentWidth,
  3236. y: (y - layerY) / parentHeight,
  3237. width: w / parentWidth,
  3238. height: h / parentHeight
  3239. });
  3240. break;
  3241. }
  3242. const boxes = [];
  3243. for (let i = 0, ii = selection.rangeCount; i < ii; i++) {
  3244. const range = selection.getRangeAt(i);
  3245. if (range.collapsed) {
  3246. continue;
  3247. }
  3248. for (const {
  3249. x,
  3250. y,
  3251. width,
  3252. height
  3253. } of range.getClientRects()) {
  3254. if (width === 0 || height === 0) {
  3255. continue;
  3256. }
  3257. boxes.push(rotator(x, y, width, height));
  3258. }
  3259. }
  3260. return boxes.length === 0 ? null : boxes;
  3261. }
  3262. addChangedExistingAnnotation({
  3263. annotationElementId,
  3264. id
  3265. }) {
  3266. (this.#changedExistingAnnotations ||= new Map()).set(annotationElementId, id);
  3267. }
  3268. removeChangedExistingAnnotation({
  3269. annotationElementId
  3270. }) {
  3271. this.#changedExistingAnnotations?.delete(annotationElementId);
  3272. }
  3273. renderAnnotationElement(annotation) {
  3274. const editorId = this.#changedExistingAnnotations?.get(annotation.data.id);
  3275. if (!editorId) {
  3276. return;
  3277. }
  3278. const editor = this.#annotationStorage.getRawValue(editorId);
  3279. if (!editor) {
  3280. return;
  3281. }
  3282. if (this.#mode === AnnotationEditorType.NONE && !editor.hasBeenModified) {
  3283. return;
  3284. }
  3285. editor.renderAnnotationElement(annotation);
  3286. }
  3287. setMissingCanvas(annotationId, annotationElementId, canvas) {
  3288. const editor = this.#missingCanvases?.get(annotationId);
  3289. if (!editor) {
  3290. return;
  3291. }
  3292. editor.setCanvas(annotationElementId, canvas);
  3293. this.#missingCanvases.delete(annotationId);
  3294. }
  3295. addMissingCanvas(annotationId, editor) {
  3296. (this.#missingCanvases ||= new Map()).set(annotationId, editor);
  3297. }
  3298. }
  3299. ;// ./src/display/editor/alt_text.js
  3300. class AltText {
  3301. #altText = null;
  3302. #altTextDecorative = false;
  3303. #altTextButton = null;
  3304. #altTextButtonLabel = null;
  3305. #altTextTooltip = null;
  3306. #altTextTooltipTimeout = null;
  3307. #altTextWasFromKeyBoard = false;
  3308. #badge = null;
  3309. #editor = null;
  3310. #guessedText = null;
  3311. #textWithDisclaimer = null;
  3312. #useNewAltTextFlow = false;
  3313. static #l10nNewButton = null;
  3314. static _l10n = null;
  3315. constructor(editor) {
  3316. this.#editor = editor;
  3317. this.#useNewAltTextFlow = editor._uiManager.useNewAltTextFlow;
  3318. AltText.#l10nNewButton ||= Object.freeze({
  3319. added: "pdfjs-editor-new-alt-text-added-button",
  3320. "added-label": "pdfjs-editor-new-alt-text-added-button-label",
  3321. missing: "pdfjs-editor-new-alt-text-missing-button",
  3322. "missing-label": "pdfjs-editor-new-alt-text-missing-button-label",
  3323. review: "pdfjs-editor-new-alt-text-to-review-button",
  3324. "review-label": "pdfjs-editor-new-alt-text-to-review-button-label"
  3325. });
  3326. }
  3327. static initialize(l10n) {
  3328. AltText._l10n ??= l10n;
  3329. }
  3330. async render() {
  3331. const altText = this.#altTextButton = document.createElement("button");
  3332. altText.className = "altText";
  3333. altText.tabIndex = "0";
  3334. const label = this.#altTextButtonLabel = document.createElement("span");
  3335. altText.append(label);
  3336. if (this.#useNewAltTextFlow) {
  3337. altText.classList.add("new");
  3338. altText.setAttribute("data-l10n-id", AltText.#l10nNewButton.missing);
  3339. label.setAttribute("data-l10n-id", AltText.#l10nNewButton["missing-label"]);
  3340. } else {
  3341. altText.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-button");
  3342. label.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-button-label");
  3343. }
  3344. const signal = this.#editor._uiManager._signal;
  3345. altText.addEventListener("contextmenu", noContextMenu, {
  3346. signal
  3347. });
  3348. altText.addEventListener("pointerdown", event => event.stopPropagation(), {
  3349. signal
  3350. });
  3351. const onClick = event => {
  3352. event.preventDefault();
  3353. this.#editor._uiManager.editAltText(this.#editor);
  3354. if (this.#useNewAltTextFlow) {
  3355. this.#editor._reportTelemetry({
  3356. action: "pdfjs.image.alt_text.image_status_label_clicked",
  3357. data: {
  3358. label: this.#label
  3359. }
  3360. });
  3361. }
  3362. };
  3363. altText.addEventListener("click", onClick, {
  3364. capture: true,
  3365. signal
  3366. });
  3367. altText.addEventListener("keydown", event => {
  3368. if (event.target === altText && event.key === "Enter") {
  3369. this.#altTextWasFromKeyBoard = true;
  3370. onClick(event);
  3371. }
  3372. }, {
  3373. signal
  3374. });
  3375. await this.#setState();
  3376. return altText;
  3377. }
  3378. get #label() {
  3379. return this.#altText && "added" || this.#altText === null && this.guessedText && "review" || "missing";
  3380. }
  3381. finish() {
  3382. if (!this.#altTextButton) {
  3383. return;
  3384. }
  3385. this.#altTextButton.focus({
  3386. focusVisible: this.#altTextWasFromKeyBoard
  3387. });
  3388. this.#altTextWasFromKeyBoard = false;
  3389. }
  3390. isEmpty() {
  3391. if (this.#useNewAltTextFlow) {
  3392. return this.#altText === null;
  3393. }
  3394. return !this.#altText && !this.#altTextDecorative;
  3395. }
  3396. hasData() {
  3397. if (this.#useNewAltTextFlow) {
  3398. return this.#altText !== null || !!this.#guessedText;
  3399. }
  3400. return this.isEmpty();
  3401. }
  3402. get guessedText() {
  3403. return this.#guessedText;
  3404. }
  3405. async setGuessedText(guessedText) {
  3406. if (this.#altText !== null) {
  3407. return;
  3408. }
  3409. this.#guessedText = guessedText;
  3410. this.#textWithDisclaimer = await AltText._l10n.get("pdfjs-editor-new-alt-text-generated-alt-text-with-disclaimer", {
  3411. generatedAltText: guessedText
  3412. });
  3413. this.#setState();
  3414. }
  3415. toggleAltTextBadge(visibility = false) {
  3416. if (!this.#useNewAltTextFlow || this.#altText) {
  3417. this.#badge?.remove();
  3418. this.#badge = null;
  3419. return;
  3420. }
  3421. if (!this.#badge) {
  3422. const badge = this.#badge = document.createElement("div");
  3423. badge.className = "noAltTextBadge";
  3424. this.#editor.div.append(badge);
  3425. }
  3426. this.#badge.classList.toggle("hidden", !visibility);
  3427. }
  3428. serialize(isForCopying) {
  3429. let altText = this.#altText;
  3430. if (!isForCopying && this.#guessedText === altText) {
  3431. altText = this.#textWithDisclaimer;
  3432. }
  3433. return {
  3434. altText,
  3435. decorative: this.#altTextDecorative,
  3436. guessedText: this.#guessedText,
  3437. textWithDisclaimer: this.#textWithDisclaimer
  3438. };
  3439. }
  3440. get data() {
  3441. return {
  3442. altText: this.#altText,
  3443. decorative: this.#altTextDecorative
  3444. };
  3445. }
  3446. set data({
  3447. altText,
  3448. decorative,
  3449. guessedText,
  3450. textWithDisclaimer,
  3451. cancel = false
  3452. }) {
  3453. if (guessedText) {
  3454. this.#guessedText = guessedText;
  3455. this.#textWithDisclaimer = textWithDisclaimer;
  3456. }
  3457. if (this.#altText === altText && this.#altTextDecorative === decorative) {
  3458. return;
  3459. }
  3460. if (!cancel) {
  3461. this.#altText = altText;
  3462. this.#altTextDecorative = decorative;
  3463. }
  3464. this.#setState();
  3465. }
  3466. toggle(enabled = false) {
  3467. if (!this.#altTextButton) {
  3468. return;
  3469. }
  3470. if (!enabled && this.#altTextTooltipTimeout) {
  3471. clearTimeout(this.#altTextTooltipTimeout);
  3472. this.#altTextTooltipTimeout = null;
  3473. }
  3474. this.#altTextButton.disabled = !enabled;
  3475. }
  3476. shown() {
  3477. this.#editor._reportTelemetry({
  3478. action: "pdfjs.image.alt_text.image_status_label_displayed",
  3479. data: {
  3480. label: this.#label
  3481. }
  3482. });
  3483. }
  3484. destroy() {
  3485. this.#altTextButton?.remove();
  3486. this.#altTextButton = null;
  3487. this.#altTextButtonLabel = null;
  3488. this.#altTextTooltip = null;
  3489. this.#badge?.remove();
  3490. this.#badge = null;
  3491. }
  3492. async #setState() {
  3493. const button = this.#altTextButton;
  3494. if (!button) {
  3495. return;
  3496. }
  3497. if (this.#useNewAltTextFlow) {
  3498. button.classList.toggle("done", !!this.#altText);
  3499. button.setAttribute("data-l10n-id", AltText.#l10nNewButton[this.#label]);
  3500. this.#altTextButtonLabel?.setAttribute("data-l10n-id", AltText.#l10nNewButton[`${this.#label}-label`]);
  3501. if (!this.#altText) {
  3502. this.#altTextTooltip?.remove();
  3503. return;
  3504. }
  3505. } else {
  3506. if (!this.#altText && !this.#altTextDecorative) {
  3507. button.classList.remove("done");
  3508. this.#altTextTooltip?.remove();
  3509. return;
  3510. }
  3511. button.classList.add("done");
  3512. button.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-edit-button");
  3513. }
  3514. let tooltip = this.#altTextTooltip;
  3515. if (!tooltip) {
  3516. this.#altTextTooltip = tooltip = document.createElement("span");
  3517. tooltip.className = "tooltip";
  3518. tooltip.setAttribute("role", "tooltip");
  3519. tooltip.id = `alt-text-tooltip-${this.#editor.id}`;
  3520. const DELAY_TO_SHOW_TOOLTIP = 100;
  3521. const signal = this.#editor._uiManager._signal;
  3522. signal.addEventListener("abort", () => {
  3523. clearTimeout(this.#altTextTooltipTimeout);
  3524. this.#altTextTooltipTimeout = null;
  3525. }, {
  3526. once: true
  3527. });
  3528. button.addEventListener("mouseenter", () => {
  3529. this.#altTextTooltipTimeout = setTimeout(() => {
  3530. this.#altTextTooltipTimeout = null;
  3531. this.#altTextTooltip.classList.add("show");
  3532. this.#editor._reportTelemetry({
  3533. action: "alt_text_tooltip"
  3534. });
  3535. }, DELAY_TO_SHOW_TOOLTIP);
  3536. }, {
  3537. signal
  3538. });
  3539. button.addEventListener("mouseleave", () => {
  3540. if (this.#altTextTooltipTimeout) {
  3541. clearTimeout(this.#altTextTooltipTimeout);
  3542. this.#altTextTooltipTimeout = null;
  3543. }
  3544. this.#altTextTooltip?.classList.remove("show");
  3545. }, {
  3546. signal
  3547. });
  3548. }
  3549. if (this.#altTextDecorative) {
  3550. tooltip.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-decorative-tooltip");
  3551. } else {
  3552. tooltip.removeAttribute("data-l10n-id");
  3553. tooltip.textContent = this.#altText;
  3554. }
  3555. if (!tooltip.parentNode) {
  3556. button.append(tooltip);
  3557. }
  3558. const element = this.#editor.getElementForAltText();
  3559. element?.setAttribute("aria-describedby", tooltip.id);
  3560. }
  3561. }
  3562. ;// ./src/display/touch_manager.js
  3563. class TouchManager {
  3564. #container;
  3565. #isPinching = false;
  3566. #isPinchingStopped = null;
  3567. #isPinchingDisabled;
  3568. #onPinchStart;
  3569. #onPinching;
  3570. #onPinchEnd;
  3571. #pointerDownAC = null;
  3572. #signal;
  3573. #touchInfo = null;
  3574. #touchManagerAC;
  3575. #touchMoveAC = null;
  3576. constructor({
  3577. container,
  3578. isPinchingDisabled = null,
  3579. isPinchingStopped = null,
  3580. onPinchStart = null,
  3581. onPinching = null,
  3582. onPinchEnd = null,
  3583. signal
  3584. }) {
  3585. this.#container = container;
  3586. this.#isPinchingStopped = isPinchingStopped;
  3587. this.#isPinchingDisabled = isPinchingDisabled;
  3588. this.#onPinchStart = onPinchStart;
  3589. this.#onPinching = onPinching;
  3590. this.#onPinchEnd = onPinchEnd;
  3591. this.#touchManagerAC = new AbortController();
  3592. this.#signal = AbortSignal.any([signal, this.#touchManagerAC.signal]);
  3593. container.addEventListener("touchstart", this.#onTouchStart.bind(this), {
  3594. passive: false,
  3595. signal: this.#signal
  3596. });
  3597. }
  3598. get MIN_TOUCH_DISTANCE_TO_PINCH() {
  3599. return 35 / OutputScale.pixelRatio;
  3600. }
  3601. #onTouchStart(evt) {
  3602. if (this.#isPinchingDisabled?.()) {
  3603. return;
  3604. }
  3605. if (evt.touches.length === 1) {
  3606. if (this.#pointerDownAC) {
  3607. return;
  3608. }
  3609. const pointerDownAC = this.#pointerDownAC = new AbortController();
  3610. const signal = AbortSignal.any([this.#signal, pointerDownAC.signal]);
  3611. const container = this.#container;
  3612. const opts = {
  3613. capture: true,
  3614. signal,
  3615. passive: false
  3616. };
  3617. const cancelPointerDown = e => {
  3618. if (e.pointerType === "touch") {
  3619. this.#pointerDownAC?.abort();
  3620. this.#pointerDownAC = null;
  3621. }
  3622. };
  3623. container.addEventListener("pointerdown", e => {
  3624. if (e.pointerType === "touch") {
  3625. stopEvent(e);
  3626. cancelPointerDown(e);
  3627. }
  3628. }, opts);
  3629. container.addEventListener("pointerup", cancelPointerDown, opts);
  3630. container.addEventListener("pointercancel", cancelPointerDown, opts);
  3631. return;
  3632. }
  3633. if (!this.#touchMoveAC) {
  3634. this.#touchMoveAC = new AbortController();
  3635. const signal = AbortSignal.any([this.#signal, this.#touchMoveAC.signal]);
  3636. const container = this.#container;
  3637. const opt = {
  3638. signal,
  3639. capture: false,
  3640. passive: false
  3641. };
  3642. container.addEventListener("touchmove", this.#onTouchMove.bind(this), opt);
  3643. const onTouchEnd = this.#onTouchEnd.bind(this);
  3644. container.addEventListener("touchend", onTouchEnd, opt);
  3645. container.addEventListener("touchcancel", onTouchEnd, opt);
  3646. opt.capture = true;
  3647. container.addEventListener("pointerdown", stopEvent, opt);
  3648. container.addEventListener("pointermove", stopEvent, opt);
  3649. container.addEventListener("pointercancel", stopEvent, opt);
  3650. container.addEventListener("pointerup", stopEvent, opt);
  3651. this.#onPinchStart?.();
  3652. }
  3653. stopEvent(evt);
  3654. if (evt.touches.length !== 2 || this.#isPinchingStopped?.()) {
  3655. this.#touchInfo = null;
  3656. return;
  3657. }
  3658. let [touch0, touch1] = evt.touches;
  3659. if (touch0.identifier > touch1.identifier) {
  3660. [touch0, touch1] = [touch1, touch0];
  3661. }
  3662. this.#touchInfo = {
  3663. touch0X: touch0.screenX,
  3664. touch0Y: touch0.screenY,
  3665. touch1X: touch1.screenX,
  3666. touch1Y: touch1.screenY
  3667. };
  3668. }
  3669. #onTouchMove(evt) {
  3670. if (!this.#touchInfo || evt.touches.length !== 2) {
  3671. return;
  3672. }
  3673. stopEvent(evt);
  3674. let [touch0, touch1] = evt.touches;
  3675. if (touch0.identifier > touch1.identifier) {
  3676. [touch0, touch1] = [touch1, touch0];
  3677. }
  3678. const {
  3679. screenX: screen0X,
  3680. screenY: screen0Y
  3681. } = touch0;
  3682. const {
  3683. screenX: screen1X,
  3684. screenY: screen1Y
  3685. } = touch1;
  3686. const touchInfo = this.#touchInfo;
  3687. const {
  3688. touch0X: pTouch0X,
  3689. touch0Y: pTouch0Y,
  3690. touch1X: pTouch1X,
  3691. touch1Y: pTouch1Y
  3692. } = touchInfo;
  3693. const prevGapX = pTouch1X - pTouch0X;
  3694. const prevGapY = pTouch1Y - pTouch0Y;
  3695. const currGapX = screen1X - screen0X;
  3696. const currGapY = screen1Y - screen0Y;
  3697. const distance = Math.hypot(currGapX, currGapY) || 1;
  3698. const pDistance = Math.hypot(prevGapX, prevGapY) || 1;
  3699. if (!this.#isPinching && Math.abs(pDistance - distance) <= TouchManager.MIN_TOUCH_DISTANCE_TO_PINCH) {
  3700. return;
  3701. }
  3702. touchInfo.touch0X = screen0X;
  3703. touchInfo.touch0Y = screen0Y;
  3704. touchInfo.touch1X = screen1X;
  3705. touchInfo.touch1Y = screen1Y;
  3706. if (!this.#isPinching) {
  3707. this.#isPinching = true;
  3708. return;
  3709. }
  3710. const origin = [(screen0X + screen1X) / 2, (screen0Y + screen1Y) / 2];
  3711. this.#onPinching?.(origin, pDistance, distance);
  3712. }
  3713. #onTouchEnd(evt) {
  3714. if (evt.touches.length >= 2) {
  3715. return;
  3716. }
  3717. this.#touchMoveAC.abort();
  3718. this.#touchMoveAC = null;
  3719. this.#onPinchEnd?.();
  3720. if (!this.#touchInfo) {
  3721. return;
  3722. }
  3723. stopEvent(evt);
  3724. this.#touchInfo = null;
  3725. this.#isPinching = false;
  3726. }
  3727. destroy() {
  3728. this.#touchManagerAC?.abort();
  3729. this.#touchManagerAC = null;
  3730. this.#pointerDownAC?.abort();
  3731. this.#pointerDownAC = null;
  3732. }
  3733. }
  3734. ;// ./src/display/editor/editor.js
  3735. class AnnotationEditor {
  3736. #accessibilityData = null;
  3737. #allResizerDivs = null;
  3738. #altText = null;
  3739. #disabled = false;
  3740. #dragPointerId = null;
  3741. #dragPointerType = "";
  3742. #keepAspectRatio = false;
  3743. #resizersDiv = null;
  3744. #lastPointerCoords = null;
  3745. #savedDimensions = null;
  3746. #focusAC = null;
  3747. #focusedResizerName = "";
  3748. #hasBeenClicked = false;
  3749. #initialRect = null;
  3750. #isEditing = false;
  3751. #isInEditMode = false;
  3752. #isResizerEnabledForKeyboard = false;
  3753. #moveInDOMTimeout = null;
  3754. #prevDragX = 0;
  3755. #prevDragY = 0;
  3756. #telemetryTimeouts = null;
  3757. #touchManager = null;
  3758. _isCopy = false;
  3759. _editToolbar = null;
  3760. _initialOptions = Object.create(null);
  3761. _initialData = null;
  3762. _isVisible = true;
  3763. _uiManager = null;
  3764. _focusEventsAllowed = true;
  3765. static _l10n = null;
  3766. static _l10nResizer = null;
  3767. #isDraggable = false;
  3768. #zIndex = AnnotationEditor._zIndex++;
  3769. static _borderLineWidth = -1;
  3770. static _colorManager = new ColorManager();
  3771. static _zIndex = 1;
  3772. static _telemetryTimeout = 1000;
  3773. static get _resizerKeyboardManager() {
  3774. const resize = AnnotationEditor.prototype._resizeWithKeyboard;
  3775. const small = AnnotationEditorUIManager.TRANSLATE_SMALL;
  3776. const big = AnnotationEditorUIManager.TRANSLATE_BIG;
  3777. return shadow(this, "_resizerKeyboardManager", new KeyboardManager([[["ArrowLeft", "mac+ArrowLeft"], resize, {
  3778. args: [-small, 0]
  3779. }], [["ctrl+ArrowLeft", "mac+shift+ArrowLeft"], resize, {
  3780. args: [-big, 0]
  3781. }], [["ArrowRight", "mac+ArrowRight"], resize, {
  3782. args: [small, 0]
  3783. }], [["ctrl+ArrowRight", "mac+shift+ArrowRight"], resize, {
  3784. args: [big, 0]
  3785. }], [["ArrowUp", "mac+ArrowUp"], resize, {
  3786. args: [0, -small]
  3787. }], [["ctrl+ArrowUp", "mac+shift+ArrowUp"], resize, {
  3788. args: [0, -big]
  3789. }], [["ArrowDown", "mac+ArrowDown"], resize, {
  3790. args: [0, small]
  3791. }], [["ctrl+ArrowDown", "mac+shift+ArrowDown"], resize, {
  3792. args: [0, big]
  3793. }], [["Escape", "mac+Escape"], AnnotationEditor.prototype._stopResizingWithKeyboard]]));
  3794. }
  3795. constructor(parameters) {
  3796. this.parent = parameters.parent;
  3797. this.id = parameters.id;
  3798. this.width = this.height = null;
  3799. this.pageIndex = parameters.parent.pageIndex;
  3800. this.name = parameters.name;
  3801. this.div = null;
  3802. this._uiManager = parameters.uiManager;
  3803. this.annotationElementId = null;
  3804. this._willKeepAspectRatio = false;
  3805. this._initialOptions.isCentered = parameters.isCentered;
  3806. this._structTreeParentId = null;
  3807. const {
  3808. rotation,
  3809. rawDims: {
  3810. pageWidth,
  3811. pageHeight,
  3812. pageX,
  3813. pageY
  3814. }
  3815. } = this.parent.viewport;
  3816. this.rotation = rotation;
  3817. this.pageRotation = (360 + rotation - this._uiManager.viewParameters.rotation) % 360;
  3818. this.pageDimensions = [pageWidth, pageHeight];
  3819. this.pageTranslation = [pageX, pageY];
  3820. const [width, height] = this.parentDimensions;
  3821. this.x = parameters.x / width;
  3822. this.y = parameters.y / height;
  3823. this.isAttachedToDOM = false;
  3824. this.deleted = false;
  3825. }
  3826. get editorType() {
  3827. return Object.getPrototypeOf(this).constructor._type;
  3828. }
  3829. static get isDrawer() {
  3830. return false;
  3831. }
  3832. static get _defaultLineColor() {
  3833. return shadow(this, "_defaultLineColor", this._colorManager.getHexCode("CanvasText"));
  3834. }
  3835. static deleteAnnotationElement(editor) {
  3836. const fakeEditor = new FakeEditor({
  3837. id: editor.parent.getNextId(),
  3838. parent: editor.parent,
  3839. uiManager: editor._uiManager
  3840. });
  3841. fakeEditor.annotationElementId = editor.annotationElementId;
  3842. fakeEditor.deleted = true;
  3843. fakeEditor._uiManager.addToAnnotationStorage(fakeEditor);
  3844. }
  3845. static initialize(l10n, _uiManager) {
  3846. AnnotationEditor._l10n ??= l10n;
  3847. AnnotationEditor._l10nResizer ||= Object.freeze({
  3848. topLeft: "pdfjs-editor-resizer-top-left",
  3849. topMiddle: "pdfjs-editor-resizer-top-middle",
  3850. topRight: "pdfjs-editor-resizer-top-right",
  3851. middleRight: "pdfjs-editor-resizer-middle-right",
  3852. bottomRight: "pdfjs-editor-resizer-bottom-right",
  3853. bottomMiddle: "pdfjs-editor-resizer-bottom-middle",
  3854. bottomLeft: "pdfjs-editor-resizer-bottom-left",
  3855. middleLeft: "pdfjs-editor-resizer-middle-left"
  3856. });
  3857. if (AnnotationEditor._borderLineWidth !== -1) {
  3858. return;
  3859. }
  3860. const style = getComputedStyle(document.documentElement);
  3861. AnnotationEditor._borderLineWidth = parseFloat(style.getPropertyValue("--outline-width")) || 0;
  3862. }
  3863. static updateDefaultParams(_type, _value) {}
  3864. static get defaultPropertiesToUpdate() {
  3865. return [];
  3866. }
  3867. static isHandlingMimeForPasting(mime) {
  3868. return false;
  3869. }
  3870. static paste(item, parent) {
  3871. unreachable("Not implemented");
  3872. }
  3873. get propertiesToUpdate() {
  3874. return [];
  3875. }
  3876. get _isDraggable() {
  3877. return this.#isDraggable;
  3878. }
  3879. set _isDraggable(value) {
  3880. this.#isDraggable = value;
  3881. this.div?.classList.toggle("draggable", value);
  3882. }
  3883. get isEnterHandled() {
  3884. return true;
  3885. }
  3886. center() {
  3887. const [pageWidth, pageHeight] = this.pageDimensions;
  3888. switch (this.parentRotation) {
  3889. case 90:
  3890. this.x -= this.height * pageHeight / (pageWidth * 2);
  3891. this.y += this.width * pageWidth / (pageHeight * 2);
  3892. break;
  3893. case 180:
  3894. this.x += this.width / 2;
  3895. this.y += this.height / 2;
  3896. break;
  3897. case 270:
  3898. this.x += this.height * pageHeight / (pageWidth * 2);
  3899. this.y -= this.width * pageWidth / (pageHeight * 2);
  3900. break;
  3901. default:
  3902. this.x -= this.width / 2;
  3903. this.y -= this.height / 2;
  3904. break;
  3905. }
  3906. this.fixAndSetPosition();
  3907. }
  3908. addCommands(params) {
  3909. this._uiManager.addCommands(params);
  3910. }
  3911. get currentLayer() {
  3912. return this._uiManager.currentLayer;
  3913. }
  3914. setInBackground() {
  3915. this.div.style.zIndex = 0;
  3916. }
  3917. setInForeground() {
  3918. this.div.style.zIndex = this.#zIndex;
  3919. }
  3920. setParent(parent) {
  3921. if (parent !== null) {
  3922. this.pageIndex = parent.pageIndex;
  3923. this.pageDimensions = parent.pageDimensions;
  3924. } else {
  3925. this.#stopResizing();
  3926. }
  3927. this.parent = parent;
  3928. }
  3929. focusin(event) {
  3930. if (!this._focusEventsAllowed) {
  3931. return;
  3932. }
  3933. if (!this.#hasBeenClicked) {
  3934. this.parent.setSelected(this);
  3935. } else {
  3936. this.#hasBeenClicked = false;
  3937. }
  3938. }
  3939. focusout(event) {
  3940. if (!this._focusEventsAllowed) {
  3941. return;
  3942. }
  3943. if (!this.isAttachedToDOM) {
  3944. return;
  3945. }
  3946. const target = event.relatedTarget;
  3947. if (target?.closest(`#${this.id}`)) {
  3948. return;
  3949. }
  3950. event.preventDefault();
  3951. if (!this.parent?.isMultipleSelection) {
  3952. this.commitOrRemove();
  3953. }
  3954. }
  3955. commitOrRemove() {
  3956. if (this.isEmpty()) {
  3957. this.remove();
  3958. } else {
  3959. this.commit();
  3960. }
  3961. }
  3962. commit() {
  3963. this.addToAnnotationStorage();
  3964. }
  3965. addToAnnotationStorage() {
  3966. this._uiManager.addToAnnotationStorage(this);
  3967. }
  3968. setAt(x, y, tx, ty) {
  3969. const [width, height] = this.parentDimensions;
  3970. [tx, ty] = this.screenToPageTranslation(tx, ty);
  3971. this.x = (x + tx) / width;
  3972. this.y = (y + ty) / height;
  3973. this.fixAndSetPosition();
  3974. }
  3975. _moveAfterPaste(baseX, baseY) {
  3976. const [parentWidth, parentHeight] = this.parentDimensions;
  3977. this.setAt(baseX * parentWidth, baseY * parentHeight, this.width * parentWidth, this.height * parentHeight);
  3978. this._onTranslated();
  3979. }
  3980. #translate([width, height], x, y) {
  3981. [x, y] = this.screenToPageTranslation(x, y);
  3982. this.x += x / width;
  3983. this.y += y / height;
  3984. this._onTranslating(this.x, this.y);
  3985. this.fixAndSetPosition();
  3986. }
  3987. translate(x, y) {
  3988. this.#translate(this.parentDimensions, x, y);
  3989. }
  3990. translateInPage(x, y) {
  3991. this.#initialRect ||= [this.x, this.y, this.width, this.height];
  3992. this.#translate(this.pageDimensions, x, y);
  3993. this.div.scrollIntoView({
  3994. block: "nearest"
  3995. });
  3996. }
  3997. translationDone() {
  3998. this._onTranslated(this.x, this.y);
  3999. }
  4000. drag(tx, ty) {
  4001. this.#initialRect ||= [this.x, this.y, this.width, this.height];
  4002. const {
  4003. div,
  4004. parentDimensions: [parentWidth, parentHeight]
  4005. } = this;
  4006. this.x += tx / parentWidth;
  4007. this.y += ty / parentHeight;
  4008. if (this.parent && (this.x < 0 || this.x > 1 || this.y < 0 || this.y > 1)) {
  4009. const {
  4010. x,
  4011. y
  4012. } = this.div.getBoundingClientRect();
  4013. if (this.parent.findNewParent(this, x, y)) {
  4014. this.x -= Math.floor(this.x);
  4015. this.y -= Math.floor(this.y);
  4016. }
  4017. }
  4018. let {
  4019. x,
  4020. y
  4021. } = this;
  4022. const [bx, by] = this.getBaseTranslation();
  4023. x += bx;
  4024. y += by;
  4025. const {
  4026. style
  4027. } = div;
  4028. style.left = `${(100 * x).toFixed(2)}%`;
  4029. style.top = `${(100 * y).toFixed(2)}%`;
  4030. this._onTranslating(x, y);
  4031. div.scrollIntoView({
  4032. block: "nearest"
  4033. });
  4034. }
  4035. _onTranslating(x, y) {}
  4036. _onTranslated(x, y) {}
  4037. get _hasBeenMoved() {
  4038. return !!this.#initialRect && (this.#initialRect[0] !== this.x || this.#initialRect[1] !== this.y);
  4039. }
  4040. get _hasBeenResized() {
  4041. return !!this.#initialRect && (this.#initialRect[2] !== this.width || this.#initialRect[3] !== this.height);
  4042. }
  4043. getBaseTranslation() {
  4044. const [parentWidth, parentHeight] = this.parentDimensions;
  4045. const {
  4046. _borderLineWidth
  4047. } = AnnotationEditor;
  4048. const x = _borderLineWidth / parentWidth;
  4049. const y = _borderLineWidth / parentHeight;
  4050. switch (this.rotation) {
  4051. case 90:
  4052. return [-x, y];
  4053. case 180:
  4054. return [x, y];
  4055. case 270:
  4056. return [x, -y];
  4057. default:
  4058. return [-x, -y];
  4059. }
  4060. }
  4061. get _mustFixPosition() {
  4062. return true;
  4063. }
  4064. fixAndSetPosition(rotation = this.rotation) {
  4065. const {
  4066. div: {
  4067. style
  4068. },
  4069. pageDimensions: [pageWidth, pageHeight]
  4070. } = this;
  4071. let {
  4072. x,
  4073. y,
  4074. width,
  4075. height
  4076. } = this;
  4077. width *= pageWidth;
  4078. height *= pageHeight;
  4079. x *= pageWidth;
  4080. y *= pageHeight;
  4081. if (this._mustFixPosition) {
  4082. switch (rotation) {
  4083. case 0:
  4084. x = MathClamp(x, 0, pageWidth - width);
  4085. y = MathClamp(y, 0, pageHeight - height);
  4086. break;
  4087. case 90:
  4088. x = MathClamp(x, 0, pageWidth - height);
  4089. y = MathClamp(y, width, pageHeight);
  4090. break;
  4091. case 180:
  4092. x = MathClamp(x, width, pageWidth);
  4093. y = MathClamp(y, height, pageHeight);
  4094. break;
  4095. case 270:
  4096. x = MathClamp(x, height, pageWidth);
  4097. y = MathClamp(y, 0, pageHeight - width);
  4098. break;
  4099. }
  4100. }
  4101. this.x = x /= pageWidth;
  4102. this.y = y /= pageHeight;
  4103. const [bx, by] = this.getBaseTranslation();
  4104. x += bx;
  4105. y += by;
  4106. style.left = `${(100 * x).toFixed(2)}%`;
  4107. style.top = `${(100 * y).toFixed(2)}%`;
  4108. this.moveInDOM();
  4109. }
  4110. static #rotatePoint(x, y, angle) {
  4111. switch (angle) {
  4112. case 90:
  4113. return [y, -x];
  4114. case 180:
  4115. return [-x, -y];
  4116. case 270:
  4117. return [-y, x];
  4118. default:
  4119. return [x, y];
  4120. }
  4121. }
  4122. screenToPageTranslation(x, y) {
  4123. return AnnotationEditor.#rotatePoint(x, y, this.parentRotation);
  4124. }
  4125. pageTranslationToScreen(x, y) {
  4126. return AnnotationEditor.#rotatePoint(x, y, 360 - this.parentRotation);
  4127. }
  4128. #getRotationMatrix(rotation) {
  4129. switch (rotation) {
  4130. case 90:
  4131. {
  4132. const [pageWidth, pageHeight] = this.pageDimensions;
  4133. return [0, -pageWidth / pageHeight, pageHeight / pageWidth, 0];
  4134. }
  4135. case 180:
  4136. return [-1, 0, 0, -1];
  4137. case 270:
  4138. {
  4139. const [pageWidth, pageHeight] = this.pageDimensions;
  4140. return [0, pageWidth / pageHeight, -pageHeight / pageWidth, 0];
  4141. }
  4142. default:
  4143. return [1, 0, 0, 1];
  4144. }
  4145. }
  4146. get parentScale() {
  4147. return this._uiManager.viewParameters.realScale;
  4148. }
  4149. get parentRotation() {
  4150. return (this._uiManager.viewParameters.rotation + this.pageRotation) % 360;
  4151. }
  4152. get parentDimensions() {
  4153. const {
  4154. parentScale,
  4155. pageDimensions: [pageWidth, pageHeight]
  4156. } = this;
  4157. return [pageWidth * parentScale, pageHeight * parentScale];
  4158. }
  4159. setDims(width, height) {
  4160. const [parentWidth, parentHeight] = this.parentDimensions;
  4161. const {
  4162. style
  4163. } = this.div;
  4164. style.width = `${(100 * width / parentWidth).toFixed(2)}%`;
  4165. if (!this.#keepAspectRatio) {
  4166. style.height = `${(100 * height / parentHeight).toFixed(2)}%`;
  4167. }
  4168. }
  4169. fixDims() {
  4170. const {
  4171. style
  4172. } = this.div;
  4173. const {
  4174. height,
  4175. width
  4176. } = style;
  4177. const widthPercent = width.endsWith("%");
  4178. const heightPercent = !this.#keepAspectRatio && height.endsWith("%");
  4179. if (widthPercent && heightPercent) {
  4180. return;
  4181. }
  4182. const [parentWidth, parentHeight] = this.parentDimensions;
  4183. if (!widthPercent) {
  4184. style.width = `${(100 * parseFloat(width) / parentWidth).toFixed(2)}%`;
  4185. }
  4186. if (!this.#keepAspectRatio && !heightPercent) {
  4187. style.height = `${(100 * parseFloat(height) / parentHeight).toFixed(2)}%`;
  4188. }
  4189. }
  4190. getInitialTranslation() {
  4191. return [0, 0];
  4192. }
  4193. #createResizers() {
  4194. if (this.#resizersDiv) {
  4195. return;
  4196. }
  4197. this.#resizersDiv = document.createElement("div");
  4198. this.#resizersDiv.classList.add("resizers");
  4199. const classes = this._willKeepAspectRatio ? ["topLeft", "topRight", "bottomRight", "bottomLeft"] : ["topLeft", "topMiddle", "topRight", "middleRight", "bottomRight", "bottomMiddle", "bottomLeft", "middleLeft"];
  4200. const signal = this._uiManager._signal;
  4201. for (const name of classes) {
  4202. const div = document.createElement("div");
  4203. this.#resizersDiv.append(div);
  4204. div.classList.add("resizer", name);
  4205. div.setAttribute("data-resizer-name", name);
  4206. div.addEventListener("pointerdown", this.#resizerPointerdown.bind(this, name), {
  4207. signal
  4208. });
  4209. div.addEventListener("contextmenu", noContextMenu, {
  4210. signal
  4211. });
  4212. div.tabIndex = -1;
  4213. }
  4214. this.div.prepend(this.#resizersDiv);
  4215. }
  4216. #resizerPointerdown(name, event) {
  4217. event.preventDefault();
  4218. const {
  4219. isMac
  4220. } = util_FeatureTest.platform;
  4221. if (event.button !== 0 || event.ctrlKey && isMac) {
  4222. return;
  4223. }
  4224. this.#altText?.toggle(false);
  4225. const savedDraggable = this._isDraggable;
  4226. this._isDraggable = false;
  4227. this.#lastPointerCoords = [event.screenX, event.screenY];
  4228. const ac = new AbortController();
  4229. const signal = this._uiManager.combinedSignal(ac);
  4230. this.parent.togglePointerEvents(false);
  4231. window.addEventListener("pointermove", this.#resizerPointermove.bind(this, name), {
  4232. passive: true,
  4233. capture: true,
  4234. signal
  4235. });
  4236. window.addEventListener("touchmove", stopEvent, {
  4237. passive: false,
  4238. signal
  4239. });
  4240. window.addEventListener("contextmenu", noContextMenu, {
  4241. signal
  4242. });
  4243. this.#savedDimensions = {
  4244. savedX: this.x,
  4245. savedY: this.y,
  4246. savedWidth: this.width,
  4247. savedHeight: this.height
  4248. };
  4249. const savedParentCursor = this.parent.div.style.cursor;
  4250. const savedCursor = this.div.style.cursor;
  4251. this.div.style.cursor = this.parent.div.style.cursor = window.getComputedStyle(event.target).cursor;
  4252. const pointerUpCallback = () => {
  4253. ac.abort();
  4254. this.parent.togglePointerEvents(true);
  4255. this.#altText?.toggle(true);
  4256. this._isDraggable = savedDraggable;
  4257. this.parent.div.style.cursor = savedParentCursor;
  4258. this.div.style.cursor = savedCursor;
  4259. this.#addResizeToUndoStack();
  4260. };
  4261. window.addEventListener("pointerup", pointerUpCallback, {
  4262. signal
  4263. });
  4264. window.addEventListener("blur", pointerUpCallback, {
  4265. signal
  4266. });
  4267. }
  4268. #resize(x, y, width, height) {
  4269. this.width = width;
  4270. this.height = height;
  4271. this.x = x;
  4272. this.y = y;
  4273. const [parentWidth, parentHeight] = this.parentDimensions;
  4274. this.setDims(parentWidth * width, parentHeight * height);
  4275. this.fixAndSetPosition();
  4276. this._onResized();
  4277. }
  4278. _onResized() {}
  4279. #addResizeToUndoStack() {
  4280. if (!this.#savedDimensions) {
  4281. return;
  4282. }
  4283. const {
  4284. savedX,
  4285. savedY,
  4286. savedWidth,
  4287. savedHeight
  4288. } = this.#savedDimensions;
  4289. this.#savedDimensions = null;
  4290. const newX = this.x;
  4291. const newY = this.y;
  4292. const newWidth = this.width;
  4293. const newHeight = this.height;
  4294. if (newX === savedX && newY === savedY && newWidth === savedWidth && newHeight === savedHeight) {
  4295. return;
  4296. }
  4297. this.addCommands({
  4298. cmd: this.#resize.bind(this, newX, newY, newWidth, newHeight),
  4299. undo: this.#resize.bind(this, savedX, savedY, savedWidth, savedHeight),
  4300. mustExec: true
  4301. });
  4302. }
  4303. static _round(x) {
  4304. return Math.round(x * 10000) / 10000;
  4305. }
  4306. #resizerPointermove(name, event) {
  4307. const [parentWidth, parentHeight] = this.parentDimensions;
  4308. const savedX = this.x;
  4309. const savedY = this.y;
  4310. const savedWidth = this.width;
  4311. const savedHeight = this.height;
  4312. const minWidth = AnnotationEditor.MIN_SIZE / parentWidth;
  4313. const minHeight = AnnotationEditor.MIN_SIZE / parentHeight;
  4314. const rotationMatrix = this.#getRotationMatrix(this.rotation);
  4315. const transf = (x, y) => [rotationMatrix[0] * x + rotationMatrix[2] * y, rotationMatrix[1] * x + rotationMatrix[3] * y];
  4316. const invRotationMatrix = this.#getRotationMatrix(360 - this.rotation);
  4317. const invTransf = (x, y) => [invRotationMatrix[0] * x + invRotationMatrix[2] * y, invRotationMatrix[1] * x + invRotationMatrix[3] * y];
  4318. let getPoint;
  4319. let getOpposite;
  4320. let isDiagonal = false;
  4321. let isHorizontal = false;
  4322. switch (name) {
  4323. case "topLeft":
  4324. isDiagonal = true;
  4325. getPoint = (w, h) => [0, 0];
  4326. getOpposite = (w, h) => [w, h];
  4327. break;
  4328. case "topMiddle":
  4329. getPoint = (w, h) => [w / 2, 0];
  4330. getOpposite = (w, h) => [w / 2, h];
  4331. break;
  4332. case "topRight":
  4333. isDiagonal = true;
  4334. getPoint = (w, h) => [w, 0];
  4335. getOpposite = (w, h) => [0, h];
  4336. break;
  4337. case "middleRight":
  4338. isHorizontal = true;
  4339. getPoint = (w, h) => [w, h / 2];
  4340. getOpposite = (w, h) => [0, h / 2];
  4341. break;
  4342. case "bottomRight":
  4343. isDiagonal = true;
  4344. getPoint = (w, h) => [w, h];
  4345. getOpposite = (w, h) => [0, 0];
  4346. break;
  4347. case "bottomMiddle":
  4348. getPoint = (w, h) => [w / 2, h];
  4349. getOpposite = (w, h) => [w / 2, 0];
  4350. break;
  4351. case "bottomLeft":
  4352. isDiagonal = true;
  4353. getPoint = (w, h) => [0, h];
  4354. getOpposite = (w, h) => [w, 0];
  4355. break;
  4356. case "middleLeft":
  4357. isHorizontal = true;
  4358. getPoint = (w, h) => [0, h / 2];
  4359. getOpposite = (w, h) => [w, h / 2];
  4360. break;
  4361. }
  4362. const point = getPoint(savedWidth, savedHeight);
  4363. const oppositePoint = getOpposite(savedWidth, savedHeight);
  4364. let transfOppositePoint = transf(...oppositePoint);
  4365. const oppositeX = AnnotationEditor._round(savedX + transfOppositePoint[0]);
  4366. const oppositeY = AnnotationEditor._round(savedY + transfOppositePoint[1]);
  4367. let ratioX = 1;
  4368. let ratioY = 1;
  4369. let deltaX, deltaY;
  4370. if (!event.fromKeyboard) {
  4371. const {
  4372. screenX,
  4373. screenY
  4374. } = event;
  4375. const [lastScreenX, lastScreenY] = this.#lastPointerCoords;
  4376. [deltaX, deltaY] = this.screenToPageTranslation(screenX - lastScreenX, screenY - lastScreenY);
  4377. this.#lastPointerCoords[0] = screenX;
  4378. this.#lastPointerCoords[1] = screenY;
  4379. } else {
  4380. ({
  4381. deltaX,
  4382. deltaY
  4383. } = event);
  4384. }
  4385. [deltaX, deltaY] = invTransf(deltaX / parentWidth, deltaY / parentHeight);
  4386. if (isDiagonal) {
  4387. const oldDiag = Math.hypot(savedWidth, savedHeight);
  4388. ratioX = ratioY = Math.max(Math.min(Math.hypot(oppositePoint[0] - point[0] - deltaX, oppositePoint[1] - point[1] - deltaY) / oldDiag, 1 / savedWidth, 1 / savedHeight), minWidth / savedWidth, minHeight / savedHeight);
  4389. } else if (isHorizontal) {
  4390. ratioX = MathClamp(Math.abs(oppositePoint[0] - point[0] - deltaX), minWidth, 1) / savedWidth;
  4391. } else {
  4392. ratioY = MathClamp(Math.abs(oppositePoint[1] - point[1] - deltaY), minHeight, 1) / savedHeight;
  4393. }
  4394. const newWidth = AnnotationEditor._round(savedWidth * ratioX);
  4395. const newHeight = AnnotationEditor._round(savedHeight * ratioY);
  4396. transfOppositePoint = transf(...getOpposite(newWidth, newHeight));
  4397. const newX = oppositeX - transfOppositePoint[0];
  4398. const newY = oppositeY - transfOppositePoint[1];
  4399. this.#initialRect ||= [this.x, this.y, this.width, this.height];
  4400. this.width = newWidth;
  4401. this.height = newHeight;
  4402. this.x = newX;
  4403. this.y = newY;
  4404. this.setDims(parentWidth * newWidth, parentHeight * newHeight);
  4405. this.fixAndSetPosition();
  4406. this._onResizing();
  4407. }
  4408. _onResizing() {}
  4409. altTextFinish() {
  4410. this.#altText?.finish();
  4411. }
  4412. async addEditToolbar() {
  4413. if (this._editToolbar || this.#isInEditMode) {
  4414. return this._editToolbar;
  4415. }
  4416. this._editToolbar = new EditorToolbar(this);
  4417. this.div.append(this._editToolbar.render());
  4418. if (this.#altText) {
  4419. await this._editToolbar.addAltText(this.#altText);
  4420. }
  4421. return this._editToolbar;
  4422. }
  4423. removeEditToolbar() {
  4424. if (!this._editToolbar) {
  4425. return;
  4426. }
  4427. this._editToolbar.remove();
  4428. this._editToolbar = null;
  4429. this.#altText?.destroy();
  4430. }
  4431. addContainer(container) {
  4432. const editToolbarDiv = this._editToolbar?.div;
  4433. if (editToolbarDiv) {
  4434. editToolbarDiv.before(container);
  4435. } else {
  4436. this.div.append(container);
  4437. }
  4438. }
  4439. getClientDimensions() {
  4440. return this.div.getBoundingClientRect();
  4441. }
  4442. async addAltTextButton() {
  4443. if (this.#altText) {
  4444. return;
  4445. }
  4446. AltText.initialize(AnnotationEditor._l10n);
  4447. this.#altText = new AltText(this);
  4448. if (this.#accessibilityData) {
  4449. this.#altText.data = this.#accessibilityData;
  4450. this.#accessibilityData = null;
  4451. }
  4452. await this.addEditToolbar();
  4453. }
  4454. get altTextData() {
  4455. return this.#altText?.data;
  4456. }
  4457. set altTextData(data) {
  4458. if (!this.#altText) {
  4459. return;
  4460. }
  4461. this.#altText.data = data;
  4462. }
  4463. get guessedAltText() {
  4464. return this.#altText?.guessedText;
  4465. }
  4466. async setGuessedAltText(text) {
  4467. await this.#altText?.setGuessedText(text);
  4468. }
  4469. serializeAltText(isForCopying) {
  4470. return this.#altText?.serialize(isForCopying);
  4471. }
  4472. hasAltText() {
  4473. return !!this.#altText && !this.#altText.isEmpty();
  4474. }
  4475. hasAltTextData() {
  4476. return this.#altText?.hasData() ?? false;
  4477. }
  4478. render() {
  4479. const div = this.div = document.createElement("div");
  4480. div.setAttribute("data-editor-rotation", (360 - this.rotation) % 360);
  4481. div.className = this.name;
  4482. div.setAttribute("id", this.id);
  4483. div.tabIndex = this.#disabled ? -1 : 0;
  4484. div.setAttribute("role", "application");
  4485. if (this.defaultL10nId) {
  4486. div.setAttribute("data-l10n-id", this.defaultL10nId);
  4487. }
  4488. if (!this._isVisible) {
  4489. div.classList.add("hidden");
  4490. }
  4491. this.setInForeground();
  4492. this.#addFocusListeners();
  4493. const [parentWidth, parentHeight] = this.parentDimensions;
  4494. if (this.parentRotation % 180 !== 0) {
  4495. div.style.maxWidth = `${(100 * parentHeight / parentWidth).toFixed(2)}%`;
  4496. div.style.maxHeight = `${(100 * parentWidth / parentHeight).toFixed(2)}%`;
  4497. }
  4498. const [tx, ty] = this.getInitialTranslation();
  4499. this.translate(tx, ty);
  4500. bindEvents(this, div, ["keydown", "pointerdown"]);
  4501. if (this.isResizable && this._uiManager._supportsPinchToZoom) {
  4502. this.#touchManager ||= new TouchManager({
  4503. container: div,
  4504. isPinchingDisabled: () => !this.isSelected,
  4505. onPinchStart: this.#touchPinchStartCallback.bind(this),
  4506. onPinching: this.#touchPinchCallback.bind(this),
  4507. onPinchEnd: this.#touchPinchEndCallback.bind(this),
  4508. signal: this._uiManager._signal
  4509. });
  4510. }
  4511. this._uiManager._editorUndoBar?.hide();
  4512. return div;
  4513. }
  4514. #touchPinchStartCallback() {
  4515. this.#savedDimensions = {
  4516. savedX: this.x,
  4517. savedY: this.y,
  4518. savedWidth: this.width,
  4519. savedHeight: this.height
  4520. };
  4521. this.#altText?.toggle(false);
  4522. this.parent.togglePointerEvents(false);
  4523. }
  4524. #touchPinchCallback(_origin, prevDistance, distance) {
  4525. const slowDownFactor = 0.7;
  4526. let factor = slowDownFactor * (distance / prevDistance) + 1 - slowDownFactor;
  4527. if (factor === 1) {
  4528. return;
  4529. }
  4530. const rotationMatrix = this.#getRotationMatrix(this.rotation);
  4531. const transf = (x, y) => [rotationMatrix[0] * x + rotationMatrix[2] * y, rotationMatrix[1] * x + rotationMatrix[3] * y];
  4532. const [parentWidth, parentHeight] = this.parentDimensions;
  4533. const savedX = this.x;
  4534. const savedY = this.y;
  4535. const savedWidth = this.width;
  4536. const savedHeight = this.height;
  4537. const minWidth = AnnotationEditor.MIN_SIZE / parentWidth;
  4538. const minHeight = AnnotationEditor.MIN_SIZE / parentHeight;
  4539. factor = Math.max(Math.min(factor, 1 / savedWidth, 1 / savedHeight), minWidth / savedWidth, minHeight / savedHeight);
  4540. const newWidth = AnnotationEditor._round(savedWidth * factor);
  4541. const newHeight = AnnotationEditor._round(savedHeight * factor);
  4542. if (newWidth === savedWidth && newHeight === savedHeight) {
  4543. return;
  4544. }
  4545. this.#initialRect ||= [savedX, savedY, savedWidth, savedHeight];
  4546. const transfCenterPoint = transf(savedWidth / 2, savedHeight / 2);
  4547. const centerX = AnnotationEditor._round(savedX + transfCenterPoint[0]);
  4548. const centerY = AnnotationEditor._round(savedY + transfCenterPoint[1]);
  4549. const newTransfCenterPoint = transf(newWidth / 2, newHeight / 2);
  4550. this.x = centerX - newTransfCenterPoint[0];
  4551. this.y = centerY - newTransfCenterPoint[1];
  4552. this.width = newWidth;
  4553. this.height = newHeight;
  4554. this.setDims(parentWidth * newWidth, parentHeight * newHeight);
  4555. this.fixAndSetPosition();
  4556. this._onResizing();
  4557. }
  4558. #touchPinchEndCallback() {
  4559. this.#altText?.toggle(true);
  4560. this.parent.togglePointerEvents(true);
  4561. this.#addResizeToUndoStack();
  4562. }
  4563. pointerdown(event) {
  4564. const {
  4565. isMac
  4566. } = util_FeatureTest.platform;
  4567. if (event.button !== 0 || event.ctrlKey && isMac) {
  4568. event.preventDefault();
  4569. return;
  4570. }
  4571. this.#hasBeenClicked = true;
  4572. if (this._isDraggable) {
  4573. this.#setUpDragSession(event);
  4574. return;
  4575. }
  4576. this.#selectOnPointerEvent(event);
  4577. }
  4578. get isSelected() {
  4579. return this._uiManager.isSelected(this);
  4580. }
  4581. #selectOnPointerEvent(event) {
  4582. const {
  4583. isMac
  4584. } = util_FeatureTest.platform;
  4585. if (event.ctrlKey && !isMac || event.shiftKey || event.metaKey && isMac) {
  4586. this.parent.toggleSelected(this);
  4587. } else {
  4588. this.parent.setSelected(this);
  4589. }
  4590. }
  4591. #setUpDragSession(event) {
  4592. const {
  4593. isSelected
  4594. } = this;
  4595. this._uiManager.setUpDragSession();
  4596. let hasDraggingStarted = false;
  4597. const ac = new AbortController();
  4598. const signal = this._uiManager.combinedSignal(ac);
  4599. const opts = {
  4600. capture: true,
  4601. passive: false,
  4602. signal
  4603. };
  4604. const cancelDrag = e => {
  4605. ac.abort();
  4606. this.#dragPointerId = null;
  4607. this.#hasBeenClicked = false;
  4608. if (!this._uiManager.endDragSession()) {
  4609. this.#selectOnPointerEvent(e);
  4610. }
  4611. if (hasDraggingStarted) {
  4612. this._onStopDragging();
  4613. }
  4614. };
  4615. if (isSelected) {
  4616. this.#prevDragX = event.clientX;
  4617. this.#prevDragY = event.clientY;
  4618. this.#dragPointerId = event.pointerId;
  4619. this.#dragPointerType = event.pointerType;
  4620. window.addEventListener("pointermove", e => {
  4621. if (!hasDraggingStarted) {
  4622. hasDraggingStarted = true;
  4623. this._onStartDragging();
  4624. }
  4625. const {
  4626. clientX: x,
  4627. clientY: y,
  4628. pointerId
  4629. } = e;
  4630. if (pointerId !== this.#dragPointerId) {
  4631. stopEvent(e);
  4632. return;
  4633. }
  4634. const [tx, ty] = this.screenToPageTranslation(x - this.#prevDragX, y - this.#prevDragY);
  4635. this.#prevDragX = x;
  4636. this.#prevDragY = y;
  4637. this._uiManager.dragSelectedEditors(tx, ty);
  4638. }, opts);
  4639. window.addEventListener("touchmove", stopEvent, opts);
  4640. window.addEventListener("pointerdown", e => {
  4641. if (e.pointerType === this.#dragPointerType) {
  4642. if (this.#touchManager || e.isPrimary) {
  4643. cancelDrag(e);
  4644. }
  4645. }
  4646. stopEvent(e);
  4647. }, opts);
  4648. }
  4649. const pointerUpCallback = e => {
  4650. if (!this.#dragPointerId || this.#dragPointerId === e.pointerId) {
  4651. cancelDrag(e);
  4652. return;
  4653. }
  4654. stopEvent(e);
  4655. };
  4656. window.addEventListener("pointerup", pointerUpCallback, {
  4657. signal
  4658. });
  4659. window.addEventListener("blur", pointerUpCallback, {
  4660. signal
  4661. });
  4662. }
  4663. _onStartDragging() {}
  4664. _onStopDragging() {}
  4665. moveInDOM() {
  4666. if (this.#moveInDOMTimeout) {
  4667. clearTimeout(this.#moveInDOMTimeout);
  4668. }
  4669. this.#moveInDOMTimeout = setTimeout(() => {
  4670. this.#moveInDOMTimeout = null;
  4671. this.parent?.moveEditorInDOM(this);
  4672. }, 0);
  4673. }
  4674. _setParentAndPosition(parent, x, y) {
  4675. parent.changeParent(this);
  4676. this.x = x;
  4677. this.y = y;
  4678. this.fixAndSetPosition();
  4679. this._onTranslated();
  4680. }
  4681. getRect(tx, ty, rotation = this.rotation) {
  4682. const scale = this.parentScale;
  4683. const [pageWidth, pageHeight] = this.pageDimensions;
  4684. const [pageX, pageY] = this.pageTranslation;
  4685. const shiftX = tx / scale;
  4686. const shiftY = ty / scale;
  4687. const x = this.x * pageWidth;
  4688. const y = this.y * pageHeight;
  4689. const width = this.width * pageWidth;
  4690. const height = this.height * pageHeight;
  4691. switch (rotation) {
  4692. case 0:
  4693. return [x + shiftX + pageX, pageHeight - y - shiftY - height + pageY, x + shiftX + width + pageX, pageHeight - y - shiftY + pageY];
  4694. case 90:
  4695. return [x + shiftY + pageX, pageHeight - y + shiftX + pageY, x + shiftY + height + pageX, pageHeight - y + shiftX + width + pageY];
  4696. case 180:
  4697. return [x - shiftX - width + pageX, pageHeight - y + shiftY + pageY, x - shiftX + pageX, pageHeight - y + shiftY + height + pageY];
  4698. case 270:
  4699. return [x - shiftY - height + pageX, pageHeight - y - shiftX - width + pageY, x - shiftY + pageX, pageHeight - y - shiftX + pageY];
  4700. default:
  4701. throw new Error("Invalid rotation");
  4702. }
  4703. }
  4704. getRectInCurrentCoords(rect, pageHeight) {
  4705. const [x1, y1, x2, y2] = rect;
  4706. const width = x2 - x1;
  4707. const height = y2 - y1;
  4708. switch (this.rotation) {
  4709. case 0:
  4710. return [x1, pageHeight - y2, width, height];
  4711. case 90:
  4712. return [x1, pageHeight - y1, height, width];
  4713. case 180:
  4714. return [x2, pageHeight - y1, width, height];
  4715. case 270:
  4716. return [x2, pageHeight - y2, height, width];
  4717. default:
  4718. throw new Error("Invalid rotation");
  4719. }
  4720. }
  4721. onceAdded(focus) {}
  4722. isEmpty() {
  4723. return false;
  4724. }
  4725. enableEditMode() {
  4726. this.#isInEditMode = true;
  4727. }
  4728. disableEditMode() {
  4729. this.#isInEditMode = false;
  4730. }
  4731. isInEditMode() {
  4732. return this.#isInEditMode;
  4733. }
  4734. shouldGetKeyboardEvents() {
  4735. return this.#isResizerEnabledForKeyboard;
  4736. }
  4737. needsToBeRebuilt() {
  4738. return this.div && !this.isAttachedToDOM;
  4739. }
  4740. get isOnScreen() {
  4741. const {
  4742. top,
  4743. left,
  4744. bottom,
  4745. right
  4746. } = this.getClientDimensions();
  4747. const {
  4748. innerHeight,
  4749. innerWidth
  4750. } = window;
  4751. return left < innerWidth && right > 0 && top < innerHeight && bottom > 0;
  4752. }
  4753. #addFocusListeners() {
  4754. if (this.#focusAC || !this.div) {
  4755. return;
  4756. }
  4757. this.#focusAC = new AbortController();
  4758. const signal = this._uiManager.combinedSignal(this.#focusAC);
  4759. this.div.addEventListener("focusin", this.focusin.bind(this), {
  4760. signal
  4761. });
  4762. this.div.addEventListener("focusout", this.focusout.bind(this), {
  4763. signal
  4764. });
  4765. }
  4766. rebuild() {
  4767. this.#addFocusListeners();
  4768. }
  4769. rotate(_angle) {}
  4770. resize() {}
  4771. serializeDeleted() {
  4772. return {
  4773. id: this.annotationElementId,
  4774. deleted: true,
  4775. pageIndex: this.pageIndex,
  4776. popupRef: this._initialData?.popupRef || ""
  4777. };
  4778. }
  4779. serialize(isForCopying = false, context = null) {
  4780. unreachable("An editor must be serializable");
  4781. }
  4782. static async deserialize(data, parent, uiManager) {
  4783. const editor = new this.prototype.constructor({
  4784. parent,
  4785. id: parent.getNextId(),
  4786. uiManager
  4787. });
  4788. editor.rotation = data.rotation;
  4789. editor.#accessibilityData = data.accessibilityData;
  4790. editor._isCopy = data.isCopy || false;
  4791. const [pageWidth, pageHeight] = editor.pageDimensions;
  4792. const [x, y, width, height] = editor.getRectInCurrentCoords(data.rect, pageHeight);
  4793. editor.x = x / pageWidth;
  4794. editor.y = y / pageHeight;
  4795. editor.width = width / pageWidth;
  4796. editor.height = height / pageHeight;
  4797. return editor;
  4798. }
  4799. get hasBeenModified() {
  4800. return !!this.annotationElementId && (this.deleted || this.serialize() !== null);
  4801. }
  4802. remove() {
  4803. this.#focusAC?.abort();
  4804. this.#focusAC = null;
  4805. if (!this.isEmpty()) {
  4806. this.commit();
  4807. }
  4808. if (this.parent) {
  4809. this.parent.remove(this);
  4810. } else {
  4811. this._uiManager.removeEditor(this);
  4812. }
  4813. if (this.#moveInDOMTimeout) {
  4814. clearTimeout(this.#moveInDOMTimeout);
  4815. this.#moveInDOMTimeout = null;
  4816. }
  4817. this.#stopResizing();
  4818. this.removeEditToolbar();
  4819. if (this.#telemetryTimeouts) {
  4820. for (const timeout of this.#telemetryTimeouts.values()) {
  4821. clearTimeout(timeout);
  4822. }
  4823. this.#telemetryTimeouts = null;
  4824. }
  4825. this.parent = null;
  4826. this.#touchManager?.destroy();
  4827. this.#touchManager = null;
  4828. }
  4829. get isResizable() {
  4830. return false;
  4831. }
  4832. makeResizable() {
  4833. if (this.isResizable) {
  4834. this.#createResizers();
  4835. this.#resizersDiv.classList.remove("hidden");
  4836. }
  4837. }
  4838. get toolbarPosition() {
  4839. return null;
  4840. }
  4841. keydown(event) {
  4842. if (!this.isResizable || event.target !== this.div || event.key !== "Enter") {
  4843. return;
  4844. }
  4845. this._uiManager.setSelected(this);
  4846. this.#savedDimensions = {
  4847. savedX: this.x,
  4848. savedY: this.y,
  4849. savedWidth: this.width,
  4850. savedHeight: this.height
  4851. };
  4852. const children = this.#resizersDiv.children;
  4853. if (!this.#allResizerDivs) {
  4854. this.#allResizerDivs = Array.from(children);
  4855. const boundResizerKeydown = this.#resizerKeydown.bind(this);
  4856. const boundResizerBlur = this.#resizerBlur.bind(this);
  4857. const signal = this._uiManager._signal;
  4858. for (const div of this.#allResizerDivs) {
  4859. const name = div.getAttribute("data-resizer-name");
  4860. div.setAttribute("role", "spinbutton");
  4861. div.addEventListener("keydown", boundResizerKeydown, {
  4862. signal
  4863. });
  4864. div.addEventListener("blur", boundResizerBlur, {
  4865. signal
  4866. });
  4867. div.addEventListener("focus", this.#resizerFocus.bind(this, name), {
  4868. signal
  4869. });
  4870. div.setAttribute("data-l10n-id", AnnotationEditor._l10nResizer[name]);
  4871. }
  4872. }
  4873. const first = this.#allResizerDivs[0];
  4874. let firstPosition = 0;
  4875. for (const div of children) {
  4876. if (div === first) {
  4877. break;
  4878. }
  4879. firstPosition++;
  4880. }
  4881. const nextFirstPosition = (360 - this.rotation + this.parentRotation) % 360 / 90 * (this.#allResizerDivs.length / 4);
  4882. if (nextFirstPosition !== firstPosition) {
  4883. if (nextFirstPosition < firstPosition) {
  4884. for (let i = 0; i < firstPosition - nextFirstPosition; i++) {
  4885. this.#resizersDiv.append(this.#resizersDiv.firstChild);
  4886. }
  4887. } else if (nextFirstPosition > firstPosition) {
  4888. for (let i = 0; i < nextFirstPosition - firstPosition; i++) {
  4889. this.#resizersDiv.firstChild.before(this.#resizersDiv.lastChild);
  4890. }
  4891. }
  4892. let i = 0;
  4893. for (const child of children) {
  4894. const div = this.#allResizerDivs[i++];
  4895. const name = div.getAttribute("data-resizer-name");
  4896. child.setAttribute("data-l10n-id", AnnotationEditor._l10nResizer[name]);
  4897. }
  4898. }
  4899. this.#setResizerTabIndex(0);
  4900. this.#isResizerEnabledForKeyboard = true;
  4901. this.#resizersDiv.firstChild.focus({
  4902. focusVisible: true
  4903. });
  4904. event.preventDefault();
  4905. event.stopImmediatePropagation();
  4906. }
  4907. #resizerKeydown(event) {
  4908. AnnotationEditor._resizerKeyboardManager.exec(this, event);
  4909. }
  4910. #resizerBlur(event) {
  4911. if (this.#isResizerEnabledForKeyboard && event.relatedTarget?.parentNode !== this.#resizersDiv) {
  4912. this.#stopResizing();
  4913. }
  4914. }
  4915. #resizerFocus(name) {
  4916. this.#focusedResizerName = this.#isResizerEnabledForKeyboard ? name : "";
  4917. }
  4918. #setResizerTabIndex(value) {
  4919. if (!this.#allResizerDivs) {
  4920. return;
  4921. }
  4922. for (const div of this.#allResizerDivs) {
  4923. div.tabIndex = value;
  4924. }
  4925. }
  4926. _resizeWithKeyboard(x, y) {
  4927. if (!this.#isResizerEnabledForKeyboard) {
  4928. return;
  4929. }
  4930. this.#resizerPointermove(this.#focusedResizerName, {
  4931. deltaX: x,
  4932. deltaY: y,
  4933. fromKeyboard: true
  4934. });
  4935. }
  4936. #stopResizing() {
  4937. this.#isResizerEnabledForKeyboard = false;
  4938. this.#setResizerTabIndex(-1);
  4939. this.#addResizeToUndoStack();
  4940. }
  4941. _stopResizingWithKeyboard() {
  4942. this.#stopResizing();
  4943. this.div.focus();
  4944. }
  4945. select() {
  4946. this.makeResizable();
  4947. this.div?.classList.add("selectedEditor");
  4948. if (!this._editToolbar) {
  4949. this.addEditToolbar().then(() => {
  4950. if (this.div?.classList.contains("selectedEditor")) {
  4951. this._editToolbar?.show();
  4952. }
  4953. });
  4954. return;
  4955. }
  4956. this._editToolbar?.show();
  4957. this.#altText?.toggleAltTextBadge(false);
  4958. }
  4959. unselect() {
  4960. this.#resizersDiv?.classList.add("hidden");
  4961. this.div?.classList.remove("selectedEditor");
  4962. if (this.div?.contains(document.activeElement)) {
  4963. this._uiManager.currentLayer.div.focus({
  4964. preventScroll: true
  4965. });
  4966. }
  4967. this._editToolbar?.hide();
  4968. this.#altText?.toggleAltTextBadge(true);
  4969. }
  4970. updateParams(type, value) {}
  4971. disableEditing() {}
  4972. enableEditing() {}
  4973. enterInEditMode() {}
  4974. getElementForAltText() {
  4975. return this.div;
  4976. }
  4977. get contentDiv() {
  4978. return this.div;
  4979. }
  4980. get isEditing() {
  4981. return this.#isEditing;
  4982. }
  4983. set isEditing(value) {
  4984. this.#isEditing = value;
  4985. if (!this.parent) {
  4986. return;
  4987. }
  4988. if (value) {
  4989. this.parent.setSelected(this);
  4990. this.parent.setActiveEditor(this);
  4991. } else {
  4992. this.parent.setActiveEditor(null);
  4993. }
  4994. }
  4995. setAspectRatio(width, height) {
  4996. this.#keepAspectRatio = true;
  4997. const aspectRatio = width / height;
  4998. const {
  4999. style
  5000. } = this.div;
  5001. style.aspectRatio = aspectRatio;
  5002. style.height = "auto";
  5003. }
  5004. static get MIN_SIZE() {
  5005. return 16;
  5006. }
  5007. static canCreateNewEmptyEditor() {
  5008. return true;
  5009. }
  5010. get telemetryInitialData() {
  5011. return {
  5012. action: "added"
  5013. };
  5014. }
  5015. get telemetryFinalData() {
  5016. return null;
  5017. }
  5018. _reportTelemetry(data, mustWait = false) {
  5019. if (mustWait) {
  5020. this.#telemetryTimeouts ||= new Map();
  5021. const {
  5022. action
  5023. } = data;
  5024. let timeout = this.#telemetryTimeouts.get(action);
  5025. if (timeout) {
  5026. clearTimeout(timeout);
  5027. }
  5028. timeout = setTimeout(() => {
  5029. this._reportTelemetry(data);
  5030. this.#telemetryTimeouts.delete(action);
  5031. if (this.#telemetryTimeouts.size === 0) {
  5032. this.#telemetryTimeouts = null;
  5033. }
  5034. }, AnnotationEditor._telemetryTimeout);
  5035. this.#telemetryTimeouts.set(action, timeout);
  5036. return;
  5037. }
  5038. data.type ||= this.editorType;
  5039. this._uiManager._eventBus.dispatch("reporttelemetry", {
  5040. source: this,
  5041. details: {
  5042. type: "editing",
  5043. data
  5044. }
  5045. });
  5046. }
  5047. show(visible = this._isVisible) {
  5048. this.div.classList.toggle("hidden", !visible);
  5049. this._isVisible = visible;
  5050. }
  5051. enable() {
  5052. if (this.div) {
  5053. this.div.tabIndex = 0;
  5054. }
  5055. this.#disabled = false;
  5056. }
  5057. disable() {
  5058. if (this.div) {
  5059. this.div.tabIndex = -1;
  5060. }
  5061. this.#disabled = true;
  5062. }
  5063. renderAnnotationElement(annotation) {
  5064. let content = annotation.container.querySelector(".annotationContent");
  5065. if (!content) {
  5066. content = document.createElement("div");
  5067. content.classList.add("annotationContent", this.editorType);
  5068. annotation.container.prepend(content);
  5069. } else if (content.nodeName === "CANVAS") {
  5070. const canvas = content;
  5071. content = document.createElement("div");
  5072. content.classList.add("annotationContent", this.editorType);
  5073. canvas.before(content);
  5074. }
  5075. return content;
  5076. }
  5077. resetAnnotationElement(annotation) {
  5078. const {
  5079. firstChild
  5080. } = annotation.container;
  5081. if (firstChild?.nodeName === "DIV" && firstChild.classList.contains("annotationContent")) {
  5082. firstChild.remove();
  5083. }
  5084. }
  5085. }
  5086. class FakeEditor extends AnnotationEditor {
  5087. constructor(params) {
  5088. super(params);
  5089. this.annotationElementId = params.annotationElementId;
  5090. this.deleted = true;
  5091. }
  5092. serialize() {
  5093. return this.serializeDeleted();
  5094. }
  5095. }
  5096. ;// ./src/shared/murmurhash3.js
  5097. const SEED = 0xc3d2e1f0;
  5098. const MASK_HIGH = 0xffff0000;
  5099. const MASK_LOW = 0xffff;
  5100. class MurmurHash3_64 {
  5101. constructor(seed) {
  5102. this.h1 = seed ? seed & 0xffffffff : SEED;
  5103. this.h2 = seed ? seed & 0xffffffff : SEED;
  5104. }
  5105. update(input) {
  5106. let data, length;
  5107. if (typeof input === "string") {
  5108. data = new Uint8Array(input.length * 2);
  5109. length = 0;
  5110. for (let i = 0, ii = input.length; i < ii; i++) {
  5111. const code = input.charCodeAt(i);
  5112. if (code <= 0xff) {
  5113. data[length++] = code;
  5114. } else {
  5115. data[length++] = code >>> 8;
  5116. data[length++] = code & 0xff;
  5117. }
  5118. }
  5119. } else if (ArrayBuffer.isView(input)) {
  5120. data = input.slice();
  5121. length = data.byteLength;
  5122. } else {
  5123. throw new Error("Invalid data format, must be a string or TypedArray.");
  5124. }
  5125. const blockCounts = length >> 2;
  5126. const tailLength = length - blockCounts * 4;
  5127. const dataUint32 = new Uint32Array(data.buffer, 0, blockCounts);
  5128. let k1 = 0,
  5129. k2 = 0;
  5130. let h1 = this.h1,
  5131. h2 = this.h2;
  5132. const C1 = 0xcc9e2d51,
  5133. C2 = 0x1b873593;
  5134. const C1_LOW = C1 & MASK_LOW,
  5135. C2_LOW = C2 & MASK_LOW;
  5136. for (let i = 0; i < blockCounts; i++) {
  5137. if (i & 1) {
  5138. k1 = dataUint32[i];
  5139. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  5140. k1 = k1 << 15 | k1 >>> 17;
  5141. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  5142. h1 ^= k1;
  5143. h1 = h1 << 13 | h1 >>> 19;
  5144. h1 = h1 * 5 + 0xe6546b64;
  5145. } else {
  5146. k2 = dataUint32[i];
  5147. k2 = k2 * C1 & MASK_HIGH | k2 * C1_LOW & MASK_LOW;
  5148. k2 = k2 << 15 | k2 >>> 17;
  5149. k2 = k2 * C2 & MASK_HIGH | k2 * C2_LOW & MASK_LOW;
  5150. h2 ^= k2;
  5151. h2 = h2 << 13 | h2 >>> 19;
  5152. h2 = h2 * 5 + 0xe6546b64;
  5153. }
  5154. }
  5155. k1 = 0;
  5156. switch (tailLength) {
  5157. case 3:
  5158. k1 ^= data[blockCounts * 4 + 2] << 16;
  5159. case 2:
  5160. k1 ^= data[blockCounts * 4 + 1] << 8;
  5161. case 1:
  5162. k1 ^= data[blockCounts * 4];
  5163. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  5164. k1 = k1 << 15 | k1 >>> 17;
  5165. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  5166. if (blockCounts & 1) {
  5167. h1 ^= k1;
  5168. } else {
  5169. h2 ^= k1;
  5170. }
  5171. }
  5172. this.h1 = h1;
  5173. this.h2 = h2;
  5174. }
  5175. hexdigest() {
  5176. let h1 = this.h1,
  5177. h2 = this.h2;
  5178. h1 ^= h2 >>> 1;
  5179. h1 = h1 * 0xed558ccd & MASK_HIGH | h1 * 0x8ccd & MASK_LOW;
  5180. h2 = h2 * 0xff51afd7 & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16;
  5181. h1 ^= h2 >>> 1;
  5182. h1 = h1 * 0x1a85ec53 & MASK_HIGH | h1 * 0xec53 & MASK_LOW;
  5183. h2 = h2 * 0xc4ceb9fe & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16;
  5184. h1 ^= h2 >>> 1;
  5185. return (h1 >>> 0).toString(16).padStart(8, "0") + (h2 >>> 0).toString(16).padStart(8, "0");
  5186. }
  5187. }
  5188. ;// ./src/display/annotation_storage.js
  5189. const SerializableEmpty = Object.freeze({
  5190. map: null,
  5191. hash: "",
  5192. transfer: undefined
  5193. });
  5194. class AnnotationStorage {
  5195. #modified = false;
  5196. #modifiedIds = null;
  5197. #storage = new Map();
  5198. constructor() {
  5199. this.onSetModified = null;
  5200. this.onResetModified = null;
  5201. this.onAnnotationEditor = null;
  5202. }
  5203. getValue(key, defaultValue) {
  5204. const value = this.#storage.get(key);
  5205. if (value === undefined) {
  5206. return defaultValue;
  5207. }
  5208. return Object.assign(defaultValue, value);
  5209. }
  5210. getRawValue(key) {
  5211. return this.#storage.get(key);
  5212. }
  5213. remove(key) {
  5214. this.#storage.delete(key);
  5215. if (this.#storage.size === 0) {
  5216. this.resetModified();
  5217. }
  5218. if (typeof this.onAnnotationEditor === "function") {
  5219. for (const value of this.#storage.values()) {
  5220. if (value instanceof AnnotationEditor) {
  5221. return;
  5222. }
  5223. }
  5224. this.onAnnotationEditor(null);
  5225. }
  5226. }
  5227. setValue(key, value) {
  5228. const obj = this.#storage.get(key);
  5229. let modified = false;
  5230. if (obj !== undefined) {
  5231. for (const [entry, val] of Object.entries(value)) {
  5232. if (obj[entry] !== val) {
  5233. modified = true;
  5234. obj[entry] = val;
  5235. }
  5236. }
  5237. } else {
  5238. modified = true;
  5239. this.#storage.set(key, value);
  5240. }
  5241. if (modified) {
  5242. this.#setModified();
  5243. }
  5244. if (value instanceof AnnotationEditor && typeof this.onAnnotationEditor === "function") {
  5245. this.onAnnotationEditor(value.constructor._type);
  5246. }
  5247. }
  5248. has(key) {
  5249. return this.#storage.has(key);
  5250. }
  5251. getAll() {
  5252. return this.#storage.size > 0 ? objectFromMap(this.#storage) : null;
  5253. }
  5254. setAll(obj) {
  5255. for (const [key, val] of Object.entries(obj)) {
  5256. this.setValue(key, val);
  5257. }
  5258. }
  5259. get size() {
  5260. return this.#storage.size;
  5261. }
  5262. #setModified() {
  5263. if (!this.#modified) {
  5264. this.#modified = true;
  5265. if (typeof this.onSetModified === "function") {
  5266. this.onSetModified();
  5267. }
  5268. }
  5269. }
  5270. resetModified() {
  5271. if (this.#modified) {
  5272. this.#modified = false;
  5273. if (typeof this.onResetModified === "function") {
  5274. this.onResetModified();
  5275. }
  5276. }
  5277. }
  5278. get print() {
  5279. return new PrintAnnotationStorage(this);
  5280. }
  5281. get serializable() {
  5282. if (this.#storage.size === 0) {
  5283. return SerializableEmpty;
  5284. }
  5285. const map = new Map(),
  5286. hash = new MurmurHash3_64(),
  5287. transfer = [];
  5288. const context = Object.create(null);
  5289. let hasBitmap = false;
  5290. for (const [key, val] of this.#storage) {
  5291. const serialized = val instanceof AnnotationEditor ? val.serialize(false, context) : val;
  5292. if (serialized) {
  5293. map.set(key, serialized);
  5294. hash.update(`${key}:${JSON.stringify(serialized)}`);
  5295. hasBitmap ||= !!serialized.bitmap;
  5296. }
  5297. }
  5298. if (hasBitmap) {
  5299. for (const value of map.values()) {
  5300. if (value.bitmap) {
  5301. transfer.push(value.bitmap);
  5302. }
  5303. }
  5304. }
  5305. return map.size > 0 ? {
  5306. map,
  5307. hash: hash.hexdigest(),
  5308. transfer
  5309. } : SerializableEmpty;
  5310. }
  5311. get editorStats() {
  5312. let stats = null;
  5313. const typeToEditor = new Map();
  5314. for (const value of this.#storage.values()) {
  5315. if (!(value instanceof AnnotationEditor)) {
  5316. continue;
  5317. }
  5318. const editorStats = value.telemetryFinalData;
  5319. if (!editorStats) {
  5320. continue;
  5321. }
  5322. const {
  5323. type
  5324. } = editorStats;
  5325. if (!typeToEditor.has(type)) {
  5326. typeToEditor.set(type, Object.getPrototypeOf(value).constructor);
  5327. }
  5328. stats ||= Object.create(null);
  5329. const map = stats[type] ||= new Map();
  5330. for (const [key, val] of Object.entries(editorStats)) {
  5331. if (key === "type") {
  5332. continue;
  5333. }
  5334. let counters = map.get(key);
  5335. if (!counters) {
  5336. counters = new Map();
  5337. map.set(key, counters);
  5338. }
  5339. const count = counters.get(val) ?? 0;
  5340. counters.set(val, count + 1);
  5341. }
  5342. }
  5343. for (const [type, editor] of typeToEditor) {
  5344. stats[type] = editor.computeTelemetryFinalData(stats[type]);
  5345. }
  5346. return stats;
  5347. }
  5348. resetModifiedIds() {
  5349. this.#modifiedIds = null;
  5350. }
  5351. get modifiedIds() {
  5352. if (this.#modifiedIds) {
  5353. return this.#modifiedIds;
  5354. }
  5355. const ids = [];
  5356. for (const value of this.#storage.values()) {
  5357. if (!(value instanceof AnnotationEditor) || !value.annotationElementId || !value.serialize()) {
  5358. continue;
  5359. }
  5360. ids.push(value.annotationElementId);
  5361. }
  5362. return this.#modifiedIds = {
  5363. ids: new Set(ids),
  5364. hash: ids.join(",")
  5365. };
  5366. }
  5367. }
  5368. class PrintAnnotationStorage extends AnnotationStorage {
  5369. #serializable;
  5370. constructor(parent) {
  5371. super();
  5372. const {
  5373. map,
  5374. hash,
  5375. transfer
  5376. } = parent.serializable;
  5377. const clone = structuredClone(map, transfer ? {
  5378. transfer
  5379. } : null);
  5380. this.#serializable = {
  5381. map: clone,
  5382. hash,
  5383. transfer
  5384. };
  5385. }
  5386. get print() {
  5387. unreachable("Should not call PrintAnnotationStorage.print");
  5388. }
  5389. get serializable() {
  5390. return this.#serializable;
  5391. }
  5392. get modifiedIds() {
  5393. return shadow(this, "modifiedIds", {
  5394. ids: new Set(),
  5395. hash: ""
  5396. });
  5397. }
  5398. }
  5399. ;// ./src/display/font_loader.js
  5400. class FontLoader {
  5401. #systemFonts = new Set();
  5402. constructor({
  5403. ownerDocument = globalThis.document,
  5404. styleElement = null
  5405. }) {
  5406. this._document = ownerDocument;
  5407. this.nativeFontFaces = new Set();
  5408. this.styleElement = null;
  5409. this.loadingRequests = [];
  5410. this.loadTestFontId = 0;
  5411. }
  5412. addNativeFontFace(nativeFontFace) {
  5413. this.nativeFontFaces.add(nativeFontFace);
  5414. this._document.fonts.add(nativeFontFace);
  5415. }
  5416. removeNativeFontFace(nativeFontFace) {
  5417. this.nativeFontFaces.delete(nativeFontFace);
  5418. this._document.fonts.delete(nativeFontFace);
  5419. }
  5420. insertRule(rule) {
  5421. if (!this.styleElement) {
  5422. this.styleElement = this._document.createElement("style");
  5423. this._document.documentElement.getElementsByTagName("head")[0].append(this.styleElement);
  5424. }
  5425. const styleSheet = this.styleElement.sheet;
  5426. styleSheet.insertRule(rule, styleSheet.cssRules.length);
  5427. }
  5428. clear() {
  5429. for (const nativeFontFace of this.nativeFontFaces) {
  5430. this._document.fonts.delete(nativeFontFace);
  5431. }
  5432. this.nativeFontFaces.clear();
  5433. this.#systemFonts.clear();
  5434. if (this.styleElement) {
  5435. this.styleElement.remove();
  5436. this.styleElement = null;
  5437. }
  5438. }
  5439. async loadSystemFont({
  5440. systemFontInfo: info,
  5441. disableFontFace,
  5442. _inspectFont
  5443. }) {
  5444. if (!info || this.#systemFonts.has(info.loadedName)) {
  5445. return;
  5446. }
  5447. assert(!disableFontFace, "loadSystemFont shouldn't be called when `disableFontFace` is set.");
  5448. if (this.isFontLoadingAPISupported) {
  5449. const {
  5450. loadedName,
  5451. src,
  5452. style
  5453. } = info;
  5454. const fontFace = new FontFace(loadedName, src, style);
  5455. this.addNativeFontFace(fontFace);
  5456. try {
  5457. await fontFace.load();
  5458. this.#systemFonts.add(loadedName);
  5459. _inspectFont?.(info);
  5460. } catch {
  5461. warn(`Cannot load system font: ${info.baseFontName}, installing it could help to improve PDF rendering.`);
  5462. this.removeNativeFontFace(fontFace);
  5463. }
  5464. return;
  5465. }
  5466. unreachable("Not implemented: loadSystemFont without the Font Loading API.");
  5467. }
  5468. async bind(font) {
  5469. if (font.attached || font.missingFile && !font.systemFontInfo) {
  5470. return;
  5471. }
  5472. font.attached = true;
  5473. if (font.systemFontInfo) {
  5474. await this.loadSystemFont(font);
  5475. return;
  5476. }
  5477. if (this.isFontLoadingAPISupported) {
  5478. const nativeFontFace = font.createNativeFontFace();
  5479. if (nativeFontFace) {
  5480. this.addNativeFontFace(nativeFontFace);
  5481. try {
  5482. await nativeFontFace.loaded;
  5483. } catch (ex) {
  5484. warn(`Failed to load font '${nativeFontFace.family}': '${ex}'.`);
  5485. font.disableFontFace = true;
  5486. throw ex;
  5487. }
  5488. }
  5489. return;
  5490. }
  5491. const rule = font.createFontFaceRule();
  5492. if (rule) {
  5493. this.insertRule(rule);
  5494. if (this.isSyncFontLoadingSupported) {
  5495. return;
  5496. }
  5497. await new Promise(resolve => {
  5498. const request = this._queueLoadingCallback(resolve);
  5499. this._prepareFontLoadEvent(font, request);
  5500. });
  5501. }
  5502. }
  5503. get isFontLoadingAPISupported() {
  5504. const hasFonts = !!this._document?.fonts;
  5505. return shadow(this, "isFontLoadingAPISupported", hasFonts);
  5506. }
  5507. get isSyncFontLoadingSupported() {
  5508. return shadow(this, "isSyncFontLoadingSupported", isNodeJS || util_FeatureTest.platform.isFirefox);
  5509. }
  5510. _queueLoadingCallback(callback) {
  5511. function completeRequest() {
  5512. assert(!request.done, "completeRequest() cannot be called twice.");
  5513. request.done = true;
  5514. while (loadingRequests.length > 0 && loadingRequests[0].done) {
  5515. const otherRequest = loadingRequests.shift();
  5516. setTimeout(otherRequest.callback, 0);
  5517. }
  5518. }
  5519. const {
  5520. loadingRequests
  5521. } = this;
  5522. const request = {
  5523. done: false,
  5524. complete: completeRequest,
  5525. callback
  5526. };
  5527. loadingRequests.push(request);
  5528. return request;
  5529. }
  5530. get _loadTestFont() {
  5531. const testFont = atob("T1RUTwALAIAAAwAwQ0ZGIDHtZg4AAAOYAAAAgUZGVE1lkzZwAAAEHAAAABxHREVGABQA" + "FQAABDgAAAAeT1MvMlYNYwkAAAEgAAAAYGNtYXABDQLUAAACNAAAAUJoZWFk/xVFDQAA" + "ALwAAAA2aGhlYQdkA+oAAAD0AAAAJGhtdHgD6AAAAAAEWAAAAAZtYXhwAAJQAAAAARgA" + "AAAGbmFtZVjmdH4AAAGAAAAAsXBvc3T/hgAzAAADeAAAACAAAQAAAAEAALZRFsRfDzz1" + "AAsD6AAAAADOBOTLAAAAAM4KHDwAAAAAA+gDIQAAAAgAAgAAAAAAAAABAAADIQAAAFoD" + "6AAAAAAD6AABAAAAAAAAAAAAAAAAAAAAAQAAUAAAAgAAAAQD6AH0AAUAAAKKArwAAACM" + "AooCvAAAAeAAMQECAAACAAYJAAAAAAAAAAAAAQAAAAAAAAAAAAAAAFBmRWQAwAAuAC4D" + "IP84AFoDIQAAAAAAAQAAAAAAAAAAACAAIAABAAAADgCuAAEAAAAAAAAAAQAAAAEAAAAA" + "AAEAAQAAAAEAAAAAAAIAAQAAAAEAAAAAAAMAAQAAAAEAAAAAAAQAAQAAAAEAAAAAAAUA" + "AQAAAAEAAAAAAAYAAQAAAAMAAQQJAAAAAgABAAMAAQQJAAEAAgABAAMAAQQJAAIAAgAB" + "AAMAAQQJAAMAAgABAAMAAQQJAAQAAgABAAMAAQQJAAUAAgABAAMAAQQJAAYAAgABWABY" + "AAAAAAAAAwAAAAMAAAAcAAEAAAAAADwAAwABAAAAHAAEACAAAAAEAAQAAQAAAC7//wAA" + "AC7////TAAEAAAAAAAABBgAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAD/gwAyAAAAAQAAAAAAAAAAAAAAAAAA" + "AAABAAQEAAEBAQJYAAEBASH4DwD4GwHEAvgcA/gXBIwMAYuL+nz5tQXkD5j3CBLnEQAC" + "AQEBIVhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYAAABAQAADwACAQEEE/t3" + "Dov6fAH6fAT+fPp8+nwHDosMCvm1Cvm1DAz6fBQAAAAAAAABAAAAAMmJbzEAAAAAzgTj" + "FQAAAADOBOQpAAEAAAAAAAAADAAUAAQAAAABAAAAAgABAAAAAAAAAAAD6AAAAAAAAA==");
  5532. return shadow(this, "_loadTestFont", testFont);
  5533. }
  5534. _prepareFontLoadEvent(font, request) {
  5535. function int32(data, offset) {
  5536. return data.charCodeAt(offset) << 24 | data.charCodeAt(offset + 1) << 16 | data.charCodeAt(offset + 2) << 8 | data.charCodeAt(offset + 3) & 0xff;
  5537. }
  5538. function spliceString(s, offset, remove, insert) {
  5539. const chunk1 = s.substring(0, offset);
  5540. const chunk2 = s.substring(offset + remove);
  5541. return chunk1 + insert + chunk2;
  5542. }
  5543. let i, ii;
  5544. const canvas = this._document.createElement("canvas");
  5545. canvas.width = 1;
  5546. canvas.height = 1;
  5547. const ctx = canvas.getContext("2d");
  5548. let called = 0;
  5549. function isFontReady(name, callback) {
  5550. if (++called > 30) {
  5551. warn("Load test font never loaded.");
  5552. callback();
  5553. return;
  5554. }
  5555. ctx.font = "30px " + name;
  5556. ctx.fillText(".", 0, 20);
  5557. const imageData = ctx.getImageData(0, 0, 1, 1);
  5558. if (imageData.data[3] > 0) {
  5559. callback();
  5560. return;
  5561. }
  5562. setTimeout(isFontReady.bind(null, name, callback));
  5563. }
  5564. const loadTestFontId = `lt${Date.now()}${this.loadTestFontId++}`;
  5565. let data = this._loadTestFont;
  5566. const COMMENT_OFFSET = 976;
  5567. data = spliceString(data, COMMENT_OFFSET, loadTestFontId.length, loadTestFontId);
  5568. const CFF_CHECKSUM_OFFSET = 16;
  5569. const XXXX_VALUE = 0x58585858;
  5570. let checksum = int32(data, CFF_CHECKSUM_OFFSET);
  5571. for (i = 0, ii = loadTestFontId.length - 3; i < ii; i += 4) {
  5572. checksum = checksum - XXXX_VALUE + int32(loadTestFontId, i) | 0;
  5573. }
  5574. if (i < loadTestFontId.length) {
  5575. checksum = checksum - XXXX_VALUE + int32(loadTestFontId + "XXX", i) | 0;
  5576. }
  5577. data = spliceString(data, CFF_CHECKSUM_OFFSET, 4, string32(checksum));
  5578. const url = `url(data:font/opentype;base64,${btoa(data)});`;
  5579. const rule = `@font-face {font-family:"${loadTestFontId}";src:${url}}`;
  5580. this.insertRule(rule);
  5581. const div = this._document.createElement("div");
  5582. div.style.visibility = "hidden";
  5583. div.style.width = div.style.height = "10px";
  5584. div.style.position = "absolute";
  5585. div.style.top = div.style.left = "0px";
  5586. for (const name of [font.loadedName, loadTestFontId]) {
  5587. const span = this._document.createElement("span");
  5588. span.textContent = "Hi";
  5589. span.style.fontFamily = name;
  5590. div.append(span);
  5591. }
  5592. this._document.body.append(div);
  5593. isFontReady(loadTestFontId, () => {
  5594. div.remove();
  5595. request.complete();
  5596. });
  5597. }
  5598. }
  5599. class FontFaceObject {
  5600. constructor(translatedData, inspectFont = null) {
  5601. this.compiledGlyphs = Object.create(null);
  5602. for (const i in translatedData) {
  5603. this[i] = translatedData[i];
  5604. }
  5605. this._inspectFont = inspectFont;
  5606. }
  5607. createNativeFontFace() {
  5608. if (!this.data || this.disableFontFace) {
  5609. return null;
  5610. }
  5611. let nativeFontFace;
  5612. if (!this.cssFontInfo) {
  5613. nativeFontFace = new FontFace(this.loadedName, this.data, {});
  5614. } else {
  5615. const css = {
  5616. weight: this.cssFontInfo.fontWeight
  5617. };
  5618. if (this.cssFontInfo.italicAngle) {
  5619. css.style = `oblique ${this.cssFontInfo.italicAngle}deg`;
  5620. }
  5621. nativeFontFace = new FontFace(this.cssFontInfo.fontFamily, this.data, css);
  5622. }
  5623. this._inspectFont?.(this);
  5624. return nativeFontFace;
  5625. }
  5626. createFontFaceRule() {
  5627. if (!this.data || this.disableFontFace) {
  5628. return null;
  5629. }
  5630. const url = `url(data:${this.mimetype};base64,${toBase64Util(this.data)});`;
  5631. let rule;
  5632. if (!this.cssFontInfo) {
  5633. rule = `@font-face {font-family:"${this.loadedName}";src:${url}}`;
  5634. } else {
  5635. let css = `font-weight: ${this.cssFontInfo.fontWeight};`;
  5636. if (this.cssFontInfo.italicAngle) {
  5637. css += `font-style: oblique ${this.cssFontInfo.italicAngle}deg;`;
  5638. }
  5639. rule = `@font-face {font-family:"${this.cssFontInfo.fontFamily}";${css}src:${url}}`;
  5640. }
  5641. this._inspectFont?.(this, url);
  5642. return rule;
  5643. }
  5644. getPathGenerator(objs, character) {
  5645. if (this.compiledGlyphs[character] !== undefined) {
  5646. return this.compiledGlyphs[character];
  5647. }
  5648. const objId = this.loadedName + "_path_" + character;
  5649. let cmds;
  5650. try {
  5651. cmds = objs.get(objId);
  5652. } catch (ex) {
  5653. warn(`getPathGenerator - ignoring character: "${ex}".`);
  5654. }
  5655. const path = new Path2D(cmds || "");
  5656. if (!this.fontExtraProperties) {
  5657. objs.delete(objId);
  5658. }
  5659. return this.compiledGlyphs[character] = path;
  5660. }
  5661. }
  5662. ;// ./src/shared/message_handler.js
  5663. const CallbackKind = {
  5664. DATA: 1,
  5665. ERROR: 2
  5666. };
  5667. const StreamKind = {
  5668. CANCEL: 1,
  5669. CANCEL_COMPLETE: 2,
  5670. CLOSE: 3,
  5671. ENQUEUE: 4,
  5672. ERROR: 5,
  5673. PULL: 6,
  5674. PULL_COMPLETE: 7,
  5675. START_COMPLETE: 8
  5676. };
  5677. function onFn() {}
  5678. function wrapReason(ex) {
  5679. if (ex instanceof AbortException || ex instanceof InvalidPDFException || ex instanceof PasswordException || ex instanceof ResponseException || ex instanceof UnknownErrorException) {
  5680. return ex;
  5681. }
  5682. if (!(ex instanceof Error || typeof ex === "object" && ex !== null)) {
  5683. unreachable('wrapReason: Expected "reason" to be a (possibly cloned) Error.');
  5684. }
  5685. switch (ex.name) {
  5686. case "AbortException":
  5687. return new AbortException(ex.message);
  5688. case "InvalidPDFException":
  5689. return new InvalidPDFException(ex.message);
  5690. case "PasswordException":
  5691. return new PasswordException(ex.message, ex.code);
  5692. case "ResponseException":
  5693. return new ResponseException(ex.message, ex.status, ex.missing);
  5694. case "UnknownErrorException":
  5695. return new UnknownErrorException(ex.message, ex.details);
  5696. }
  5697. return new UnknownErrorException(ex.message, ex.toString());
  5698. }
  5699. class MessageHandler {
  5700. #messageAC = new AbortController();
  5701. constructor(sourceName, targetName, comObj) {
  5702. this.sourceName = sourceName;
  5703. this.targetName = targetName;
  5704. this.comObj = comObj;
  5705. this.callbackId = 1;
  5706. this.streamId = 1;
  5707. this.streamSinks = Object.create(null);
  5708. this.streamControllers = Object.create(null);
  5709. this.callbackCapabilities = Object.create(null);
  5710. this.actionHandler = Object.create(null);
  5711. comObj.addEventListener("message", this.#onMessage.bind(this), {
  5712. signal: this.#messageAC.signal
  5713. });
  5714. }
  5715. #onMessage({
  5716. data
  5717. }) {
  5718. if (data.targetName !== this.sourceName) {
  5719. return;
  5720. }
  5721. if (data.stream) {
  5722. this.#processStreamMessage(data);
  5723. return;
  5724. }
  5725. if (data.callback) {
  5726. const callbackId = data.callbackId;
  5727. const capability = this.callbackCapabilities[callbackId];
  5728. if (!capability) {
  5729. throw new Error(`Cannot resolve callback ${callbackId}`);
  5730. }
  5731. delete this.callbackCapabilities[callbackId];
  5732. if (data.callback === CallbackKind.DATA) {
  5733. capability.resolve(data.data);
  5734. } else if (data.callback === CallbackKind.ERROR) {
  5735. capability.reject(wrapReason(data.reason));
  5736. } else {
  5737. throw new Error("Unexpected callback case");
  5738. }
  5739. return;
  5740. }
  5741. const action = this.actionHandler[data.action];
  5742. if (!action) {
  5743. throw new Error(`Unknown action from worker: ${data.action}`);
  5744. }
  5745. if (data.callbackId) {
  5746. const sourceName = this.sourceName,
  5747. targetName = data.sourceName,
  5748. comObj = this.comObj;
  5749. Promise.try(action, data.data).then(function (result) {
  5750. comObj.postMessage({
  5751. sourceName,
  5752. targetName,
  5753. callback: CallbackKind.DATA,
  5754. callbackId: data.callbackId,
  5755. data: result
  5756. });
  5757. }, function (reason) {
  5758. comObj.postMessage({
  5759. sourceName,
  5760. targetName,
  5761. callback: CallbackKind.ERROR,
  5762. callbackId: data.callbackId,
  5763. reason: wrapReason(reason)
  5764. });
  5765. });
  5766. return;
  5767. }
  5768. if (data.streamId) {
  5769. this.#createStreamSink(data);
  5770. return;
  5771. }
  5772. action(data.data);
  5773. }
  5774. on(actionName, handler) {
  5775. const ah = this.actionHandler;
  5776. if (ah[actionName]) {
  5777. throw new Error(`There is already an actionName called "${actionName}"`);
  5778. }
  5779. ah[actionName] = handler;
  5780. }
  5781. send(actionName, data, transfers) {
  5782. this.comObj.postMessage({
  5783. sourceName: this.sourceName,
  5784. targetName: this.targetName,
  5785. action: actionName,
  5786. data
  5787. }, transfers);
  5788. }
  5789. sendWithPromise(actionName, data, transfers) {
  5790. const callbackId = this.callbackId++;
  5791. const capability = Promise.withResolvers();
  5792. this.callbackCapabilities[callbackId] = capability;
  5793. try {
  5794. this.comObj.postMessage({
  5795. sourceName: this.sourceName,
  5796. targetName: this.targetName,
  5797. action: actionName,
  5798. callbackId,
  5799. data
  5800. }, transfers);
  5801. } catch (ex) {
  5802. capability.reject(ex);
  5803. }
  5804. return capability.promise;
  5805. }
  5806. sendWithStream(actionName, data, queueingStrategy, transfers) {
  5807. const streamId = this.streamId++,
  5808. sourceName = this.sourceName,
  5809. targetName = this.targetName,
  5810. comObj = this.comObj;
  5811. return new ReadableStream({
  5812. start: controller => {
  5813. const startCapability = Promise.withResolvers();
  5814. this.streamControllers[streamId] = {
  5815. controller,
  5816. startCall: startCapability,
  5817. pullCall: null,
  5818. cancelCall: null,
  5819. isClosed: false
  5820. };
  5821. comObj.postMessage({
  5822. sourceName,
  5823. targetName,
  5824. action: actionName,
  5825. streamId,
  5826. data,
  5827. desiredSize: controller.desiredSize
  5828. }, transfers);
  5829. return startCapability.promise;
  5830. },
  5831. pull: controller => {
  5832. const pullCapability = Promise.withResolvers();
  5833. this.streamControllers[streamId].pullCall = pullCapability;
  5834. comObj.postMessage({
  5835. sourceName,
  5836. targetName,
  5837. stream: StreamKind.PULL,
  5838. streamId,
  5839. desiredSize: controller.desiredSize
  5840. });
  5841. return pullCapability.promise;
  5842. },
  5843. cancel: reason => {
  5844. assert(reason instanceof Error, "cancel must have a valid reason");
  5845. const cancelCapability = Promise.withResolvers();
  5846. this.streamControllers[streamId].cancelCall = cancelCapability;
  5847. this.streamControllers[streamId].isClosed = true;
  5848. comObj.postMessage({
  5849. sourceName,
  5850. targetName,
  5851. stream: StreamKind.CANCEL,
  5852. streamId,
  5853. reason: wrapReason(reason)
  5854. });
  5855. return cancelCapability.promise;
  5856. }
  5857. }, queueingStrategy);
  5858. }
  5859. #createStreamSink(data) {
  5860. const streamId = data.streamId,
  5861. sourceName = this.sourceName,
  5862. targetName = data.sourceName,
  5863. comObj = this.comObj;
  5864. const self = this,
  5865. action = this.actionHandler[data.action];
  5866. const streamSink = {
  5867. enqueue(chunk, size = 1, transfers) {
  5868. if (this.isCancelled) {
  5869. return;
  5870. }
  5871. const lastDesiredSize = this.desiredSize;
  5872. this.desiredSize -= size;
  5873. if (lastDesiredSize > 0 && this.desiredSize <= 0) {
  5874. this.sinkCapability = Promise.withResolvers();
  5875. this.ready = this.sinkCapability.promise;
  5876. }
  5877. comObj.postMessage({
  5878. sourceName,
  5879. targetName,
  5880. stream: StreamKind.ENQUEUE,
  5881. streamId,
  5882. chunk
  5883. }, transfers);
  5884. },
  5885. close() {
  5886. if (this.isCancelled) {
  5887. return;
  5888. }
  5889. this.isCancelled = true;
  5890. comObj.postMessage({
  5891. sourceName,
  5892. targetName,
  5893. stream: StreamKind.CLOSE,
  5894. streamId
  5895. });
  5896. delete self.streamSinks[streamId];
  5897. },
  5898. error(reason) {
  5899. assert(reason instanceof Error, "error must have a valid reason");
  5900. if (this.isCancelled) {
  5901. return;
  5902. }
  5903. this.isCancelled = true;
  5904. comObj.postMessage({
  5905. sourceName,
  5906. targetName,
  5907. stream: StreamKind.ERROR,
  5908. streamId,
  5909. reason: wrapReason(reason)
  5910. });
  5911. },
  5912. sinkCapability: Promise.withResolvers(),
  5913. onPull: null,
  5914. onCancel: null,
  5915. isCancelled: false,
  5916. desiredSize: data.desiredSize,
  5917. ready: null
  5918. };
  5919. streamSink.sinkCapability.resolve();
  5920. streamSink.ready = streamSink.sinkCapability.promise;
  5921. this.streamSinks[streamId] = streamSink;
  5922. Promise.try(action, data.data, streamSink).then(function () {
  5923. comObj.postMessage({
  5924. sourceName,
  5925. targetName,
  5926. stream: StreamKind.START_COMPLETE,
  5927. streamId,
  5928. success: true
  5929. });
  5930. }, function (reason) {
  5931. comObj.postMessage({
  5932. sourceName,
  5933. targetName,
  5934. stream: StreamKind.START_COMPLETE,
  5935. streamId,
  5936. reason: wrapReason(reason)
  5937. });
  5938. });
  5939. }
  5940. #processStreamMessage(data) {
  5941. const streamId = data.streamId,
  5942. sourceName = this.sourceName,
  5943. targetName = data.sourceName,
  5944. comObj = this.comObj;
  5945. const streamController = this.streamControllers[streamId],
  5946. streamSink = this.streamSinks[streamId];
  5947. switch (data.stream) {
  5948. case StreamKind.START_COMPLETE:
  5949. if (data.success) {
  5950. streamController.startCall.resolve();
  5951. } else {
  5952. streamController.startCall.reject(wrapReason(data.reason));
  5953. }
  5954. break;
  5955. case StreamKind.PULL_COMPLETE:
  5956. if (data.success) {
  5957. streamController.pullCall.resolve();
  5958. } else {
  5959. streamController.pullCall.reject(wrapReason(data.reason));
  5960. }
  5961. break;
  5962. case StreamKind.PULL:
  5963. if (!streamSink) {
  5964. comObj.postMessage({
  5965. sourceName,
  5966. targetName,
  5967. stream: StreamKind.PULL_COMPLETE,
  5968. streamId,
  5969. success: true
  5970. });
  5971. break;
  5972. }
  5973. if (streamSink.desiredSize <= 0 && data.desiredSize > 0) {
  5974. streamSink.sinkCapability.resolve();
  5975. }
  5976. streamSink.desiredSize = data.desiredSize;
  5977. Promise.try(streamSink.onPull || onFn).then(function () {
  5978. comObj.postMessage({
  5979. sourceName,
  5980. targetName,
  5981. stream: StreamKind.PULL_COMPLETE,
  5982. streamId,
  5983. success: true
  5984. });
  5985. }, function (reason) {
  5986. comObj.postMessage({
  5987. sourceName,
  5988. targetName,
  5989. stream: StreamKind.PULL_COMPLETE,
  5990. streamId,
  5991. reason: wrapReason(reason)
  5992. });
  5993. });
  5994. break;
  5995. case StreamKind.ENQUEUE:
  5996. assert(streamController, "enqueue should have stream controller");
  5997. if (streamController.isClosed) {
  5998. break;
  5999. }
  6000. streamController.controller.enqueue(data.chunk);
  6001. break;
  6002. case StreamKind.CLOSE:
  6003. assert(streamController, "close should have stream controller");
  6004. if (streamController.isClosed) {
  6005. break;
  6006. }
  6007. streamController.isClosed = true;
  6008. streamController.controller.close();
  6009. this.#deleteStreamController(streamController, streamId);
  6010. break;
  6011. case StreamKind.ERROR:
  6012. assert(streamController, "error should have stream controller");
  6013. streamController.controller.error(wrapReason(data.reason));
  6014. this.#deleteStreamController(streamController, streamId);
  6015. break;
  6016. case StreamKind.CANCEL_COMPLETE:
  6017. if (data.success) {
  6018. streamController.cancelCall.resolve();
  6019. } else {
  6020. streamController.cancelCall.reject(wrapReason(data.reason));
  6021. }
  6022. this.#deleteStreamController(streamController, streamId);
  6023. break;
  6024. case StreamKind.CANCEL:
  6025. if (!streamSink) {
  6026. break;
  6027. }
  6028. const dataReason = wrapReason(data.reason);
  6029. Promise.try(streamSink.onCancel || onFn, dataReason).then(function () {
  6030. comObj.postMessage({
  6031. sourceName,
  6032. targetName,
  6033. stream: StreamKind.CANCEL_COMPLETE,
  6034. streamId,
  6035. success: true
  6036. });
  6037. }, function (reason) {
  6038. comObj.postMessage({
  6039. sourceName,
  6040. targetName,
  6041. stream: StreamKind.CANCEL_COMPLETE,
  6042. streamId,
  6043. reason: wrapReason(reason)
  6044. });
  6045. });
  6046. streamSink.sinkCapability.reject(dataReason);
  6047. streamSink.isCancelled = true;
  6048. delete this.streamSinks[streamId];
  6049. break;
  6050. default:
  6051. throw new Error("Unexpected stream case");
  6052. }
  6053. }
  6054. async #deleteStreamController(streamController, streamId) {
  6055. await Promise.allSettled([streamController.startCall?.promise, streamController.pullCall?.promise, streamController.cancelCall?.promise]);
  6056. delete this.streamControllers[streamId];
  6057. }
  6058. destroy() {
  6059. this.#messageAC?.abort();
  6060. this.#messageAC = null;
  6061. }
  6062. }
  6063. ;// ./src/display/canvas_factory.js
  6064. class BaseCanvasFactory {
  6065. #enableHWA = false;
  6066. constructor({
  6067. enableHWA = false
  6068. }) {
  6069. this.#enableHWA = enableHWA;
  6070. }
  6071. create(width, height) {
  6072. if (width <= 0 || height <= 0) {
  6073. throw new Error("Invalid canvas size");
  6074. }
  6075. const canvas = this._createCanvas(width, height);
  6076. return {
  6077. canvas,
  6078. context: canvas.getContext("2d", {
  6079. willReadFrequently: !this.#enableHWA
  6080. })
  6081. };
  6082. }
  6083. reset(canvasAndContext, width, height) {
  6084. if (!canvasAndContext.canvas) {
  6085. throw new Error("Canvas is not specified");
  6086. }
  6087. if (width <= 0 || height <= 0) {
  6088. throw new Error("Invalid canvas size");
  6089. }
  6090. canvasAndContext.canvas.width = width;
  6091. canvasAndContext.canvas.height = height;
  6092. }
  6093. destroy(canvasAndContext) {
  6094. if (!canvasAndContext.canvas) {
  6095. throw new Error("Canvas is not specified");
  6096. }
  6097. canvasAndContext.canvas.width = 0;
  6098. canvasAndContext.canvas.height = 0;
  6099. canvasAndContext.canvas = null;
  6100. canvasAndContext.context = null;
  6101. }
  6102. _createCanvas(width, height) {
  6103. unreachable("Abstract method `_createCanvas` called.");
  6104. }
  6105. }
  6106. class DOMCanvasFactory extends BaseCanvasFactory {
  6107. constructor({
  6108. ownerDocument = globalThis.document,
  6109. enableHWA = false
  6110. }) {
  6111. super({
  6112. enableHWA
  6113. });
  6114. this._document = ownerDocument;
  6115. }
  6116. _createCanvas(width, height) {
  6117. const canvas = this._document.createElement("canvas");
  6118. canvas.width = width;
  6119. canvas.height = height;
  6120. return canvas;
  6121. }
  6122. }
  6123. ;// ./src/display/cmap_reader_factory.js
  6124. class BaseCMapReaderFactory {
  6125. constructor({
  6126. baseUrl = null,
  6127. isCompressed = true
  6128. }) {
  6129. this.baseUrl = baseUrl;
  6130. this.isCompressed = isCompressed;
  6131. }
  6132. async fetch({
  6133. name
  6134. }) {
  6135. if (!this.baseUrl) {
  6136. throw new Error("Ensure that the `cMapUrl` and `cMapPacked` API parameters are provided.");
  6137. }
  6138. if (!name) {
  6139. throw new Error("CMap name must be specified.");
  6140. }
  6141. const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
  6142. return this._fetch(url).then(cMapData => ({
  6143. cMapData,
  6144. isCompressed: this.isCompressed
  6145. })).catch(reason => {
  6146. throw new Error(`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`);
  6147. });
  6148. }
  6149. async _fetch(url) {
  6150. unreachable("Abstract method `_fetch` called.");
  6151. }
  6152. }
  6153. class DOMCMapReaderFactory extends BaseCMapReaderFactory {
  6154. async _fetch(url) {
  6155. const data = await fetchData(url, this.isCompressed ? "arraybuffer" : "text");
  6156. return data instanceof ArrayBuffer ? new Uint8Array(data) : stringToBytes(data);
  6157. }
  6158. }
  6159. ;// ./src/display/filter_factory.js
  6160. class BaseFilterFactory {
  6161. addFilter(maps) {
  6162. return "none";
  6163. }
  6164. addHCMFilter(fgColor, bgColor) {
  6165. return "none";
  6166. }
  6167. addAlphaFilter(map) {
  6168. return "none";
  6169. }
  6170. addLuminosityFilter(map) {
  6171. return "none";
  6172. }
  6173. addHighlightHCMFilter(filterName, fgColor, bgColor, newFgColor, newBgColor) {
  6174. return "none";
  6175. }
  6176. destroy(keepHCM = false) {}
  6177. }
  6178. class DOMFilterFactory extends BaseFilterFactory {
  6179. #baseUrl;
  6180. #_cache;
  6181. #_defs;
  6182. #docId;
  6183. #document;
  6184. #_hcmCache;
  6185. #id = 0;
  6186. constructor({
  6187. docId,
  6188. ownerDocument = globalThis.document
  6189. }) {
  6190. super();
  6191. this.#docId = docId;
  6192. this.#document = ownerDocument;
  6193. }
  6194. get #cache() {
  6195. return this.#_cache ||= new Map();
  6196. }
  6197. get #hcmCache() {
  6198. return this.#_hcmCache ||= new Map();
  6199. }
  6200. get #defs() {
  6201. if (!this.#_defs) {
  6202. const div = this.#document.createElement("div");
  6203. const {
  6204. style
  6205. } = div;
  6206. style.visibility = "hidden";
  6207. style.contain = "strict";
  6208. style.width = style.height = 0;
  6209. style.position = "absolute";
  6210. style.top = style.left = 0;
  6211. style.zIndex = -1;
  6212. const svg = this.#document.createElementNS(SVG_NS, "svg");
  6213. svg.setAttribute("width", 0);
  6214. svg.setAttribute("height", 0);
  6215. this.#_defs = this.#document.createElementNS(SVG_NS, "defs");
  6216. div.append(svg);
  6217. svg.append(this.#_defs);
  6218. this.#document.body.append(div);
  6219. }
  6220. return this.#_defs;
  6221. }
  6222. #createTables(maps) {
  6223. if (maps.length === 1) {
  6224. const mapR = maps[0];
  6225. const buffer = new Array(256);
  6226. for (let i = 0; i < 256; i++) {
  6227. buffer[i] = mapR[i] / 255;
  6228. }
  6229. const table = buffer.join(",");
  6230. return [table, table, table];
  6231. }
  6232. const [mapR, mapG, mapB] = maps;
  6233. const bufferR = new Array(256);
  6234. const bufferG = new Array(256);
  6235. const bufferB = new Array(256);
  6236. for (let i = 0; i < 256; i++) {
  6237. bufferR[i] = mapR[i] / 255;
  6238. bufferG[i] = mapG[i] / 255;
  6239. bufferB[i] = mapB[i] / 255;
  6240. }
  6241. return [bufferR.join(","), bufferG.join(","), bufferB.join(",")];
  6242. }
  6243. #createUrl(id) {
  6244. if (this.#baseUrl === undefined) {
  6245. this.#baseUrl = "";
  6246. const url = this.#document.URL;
  6247. if (url !== this.#document.baseURI) {
  6248. if (isDataScheme(url)) {
  6249. warn('#createUrl: ignore "data:"-URL for performance reasons.');
  6250. } else {
  6251. this.#baseUrl = url.split("#", 1)[0];
  6252. }
  6253. }
  6254. }
  6255. return `url(${this.#baseUrl}#${id})`;
  6256. }
  6257. addFilter(maps) {
  6258. if (!maps) {
  6259. return "none";
  6260. }
  6261. let value = this.#cache.get(maps);
  6262. if (value) {
  6263. return value;
  6264. }
  6265. const [tableR, tableG, tableB] = this.#createTables(maps);
  6266. const key = maps.length === 1 ? tableR : `${tableR}${tableG}${tableB}`;
  6267. value = this.#cache.get(key);
  6268. if (value) {
  6269. this.#cache.set(maps, value);
  6270. return value;
  6271. }
  6272. const id = `g_${this.#docId}_transfer_map_${this.#id++}`;
  6273. const url = this.#createUrl(id);
  6274. this.#cache.set(maps, url);
  6275. this.#cache.set(key, url);
  6276. const filter = this.#createFilter(id);
  6277. this.#addTransferMapConversion(tableR, tableG, tableB, filter);
  6278. return url;
  6279. }
  6280. addHCMFilter(fgColor, bgColor) {
  6281. const key = `${fgColor}-${bgColor}`;
  6282. const filterName = "base";
  6283. let info = this.#hcmCache.get(filterName);
  6284. if (info?.key === key) {
  6285. return info.url;
  6286. }
  6287. if (info) {
  6288. info.filter?.remove();
  6289. info.key = key;
  6290. info.url = "none";
  6291. info.filter = null;
  6292. } else {
  6293. info = {
  6294. key,
  6295. url: "none",
  6296. filter: null
  6297. };
  6298. this.#hcmCache.set(filterName, info);
  6299. }
  6300. if (!fgColor || !bgColor) {
  6301. return info.url;
  6302. }
  6303. const fgRGB = this.#getRGB(fgColor);
  6304. fgColor = Util.makeHexColor(...fgRGB);
  6305. const bgRGB = this.#getRGB(bgColor);
  6306. bgColor = Util.makeHexColor(...bgRGB);
  6307. this.#defs.style.color = "";
  6308. if (fgColor === "#000000" && bgColor === "#ffffff" || fgColor === bgColor) {
  6309. return info.url;
  6310. }
  6311. const map = new Array(256);
  6312. for (let i = 0; i <= 255; i++) {
  6313. const x = i / 255;
  6314. map[i] = x <= 0.03928 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4;
  6315. }
  6316. const table = map.join(",");
  6317. const id = `g_${this.#docId}_hcm_filter`;
  6318. const filter = info.filter = this.#createFilter(id);
  6319. this.#addTransferMapConversion(table, table, table, filter);
  6320. this.#addGrayConversion(filter);
  6321. const getSteps = (c, n) => {
  6322. const start = fgRGB[c] / 255;
  6323. const end = bgRGB[c] / 255;
  6324. const arr = new Array(n + 1);
  6325. for (let i = 0; i <= n; i++) {
  6326. arr[i] = start + i / n * (end - start);
  6327. }
  6328. return arr.join(",");
  6329. };
  6330. this.#addTransferMapConversion(getSteps(0, 5), getSteps(1, 5), getSteps(2, 5), filter);
  6331. info.url = this.#createUrl(id);
  6332. return info.url;
  6333. }
  6334. addAlphaFilter(map) {
  6335. let value = this.#cache.get(map);
  6336. if (value) {
  6337. return value;
  6338. }
  6339. const [tableA] = this.#createTables([map]);
  6340. const key = `alpha_${tableA}`;
  6341. value = this.#cache.get(key);
  6342. if (value) {
  6343. this.#cache.set(map, value);
  6344. return value;
  6345. }
  6346. const id = `g_${this.#docId}_alpha_map_${this.#id++}`;
  6347. const url = this.#createUrl(id);
  6348. this.#cache.set(map, url);
  6349. this.#cache.set(key, url);
  6350. const filter = this.#createFilter(id);
  6351. this.#addTransferMapAlphaConversion(tableA, filter);
  6352. return url;
  6353. }
  6354. addLuminosityFilter(map) {
  6355. let value = this.#cache.get(map || "luminosity");
  6356. if (value) {
  6357. return value;
  6358. }
  6359. let tableA, key;
  6360. if (map) {
  6361. [tableA] = this.#createTables([map]);
  6362. key = `luminosity_${tableA}`;
  6363. } else {
  6364. key = "luminosity";
  6365. }
  6366. value = this.#cache.get(key);
  6367. if (value) {
  6368. this.#cache.set(map, value);
  6369. return value;
  6370. }
  6371. const id = `g_${this.#docId}_luminosity_map_${this.#id++}`;
  6372. const url = this.#createUrl(id);
  6373. this.#cache.set(map, url);
  6374. this.#cache.set(key, url);
  6375. const filter = this.#createFilter(id);
  6376. this.#addLuminosityConversion(filter);
  6377. if (map) {
  6378. this.#addTransferMapAlphaConversion(tableA, filter);
  6379. }
  6380. return url;
  6381. }
  6382. addHighlightHCMFilter(filterName, fgColor, bgColor, newFgColor, newBgColor) {
  6383. const key = `${fgColor}-${bgColor}-${newFgColor}-${newBgColor}`;
  6384. let info = this.#hcmCache.get(filterName);
  6385. if (info?.key === key) {
  6386. return info.url;
  6387. }
  6388. if (info) {
  6389. info.filter?.remove();
  6390. info.key = key;
  6391. info.url = "none";
  6392. info.filter = null;
  6393. } else {
  6394. info = {
  6395. key,
  6396. url: "none",
  6397. filter: null
  6398. };
  6399. this.#hcmCache.set(filterName, info);
  6400. }
  6401. if (!fgColor || !bgColor) {
  6402. return info.url;
  6403. }
  6404. const [fgRGB, bgRGB] = [fgColor, bgColor].map(this.#getRGB.bind(this));
  6405. let fgGray = Math.round(0.2126 * fgRGB[0] + 0.7152 * fgRGB[1] + 0.0722 * fgRGB[2]);
  6406. let bgGray = Math.round(0.2126 * bgRGB[0] + 0.7152 * bgRGB[1] + 0.0722 * bgRGB[2]);
  6407. let [newFgRGB, newBgRGB] = [newFgColor, newBgColor].map(this.#getRGB.bind(this));
  6408. if (bgGray < fgGray) {
  6409. [fgGray, bgGray, newFgRGB, newBgRGB] = [bgGray, fgGray, newBgRGB, newFgRGB];
  6410. }
  6411. this.#defs.style.color = "";
  6412. const getSteps = (fg, bg, n) => {
  6413. const arr = new Array(256);
  6414. const step = (bgGray - fgGray) / n;
  6415. const newStart = fg / 255;
  6416. const newStep = (bg - fg) / (255 * n);
  6417. let prev = 0;
  6418. for (let i = 0; i <= n; i++) {
  6419. const k = Math.round(fgGray + i * step);
  6420. const value = newStart + i * newStep;
  6421. for (let j = prev; j <= k; j++) {
  6422. arr[j] = value;
  6423. }
  6424. prev = k + 1;
  6425. }
  6426. for (let i = prev; i < 256; i++) {
  6427. arr[i] = arr[prev - 1];
  6428. }
  6429. return arr.join(",");
  6430. };
  6431. const id = `g_${this.#docId}_hcm_${filterName}_filter`;
  6432. const filter = info.filter = this.#createFilter(id);
  6433. this.#addGrayConversion(filter);
  6434. this.#addTransferMapConversion(getSteps(newFgRGB[0], newBgRGB[0], 5), getSteps(newFgRGB[1], newBgRGB[1], 5), getSteps(newFgRGB[2], newBgRGB[2], 5), filter);
  6435. info.url = this.#createUrl(id);
  6436. return info.url;
  6437. }
  6438. destroy(keepHCM = false) {
  6439. if (keepHCM && this.#_hcmCache?.size) {
  6440. return;
  6441. }
  6442. this.#_defs?.parentNode.parentNode.remove();
  6443. this.#_defs = null;
  6444. this.#_cache?.clear();
  6445. this.#_cache = null;
  6446. this.#_hcmCache?.clear();
  6447. this.#_hcmCache = null;
  6448. this.#id = 0;
  6449. }
  6450. #addLuminosityConversion(filter) {
  6451. const feColorMatrix = this.#document.createElementNS(SVG_NS, "feColorMatrix");
  6452. feColorMatrix.setAttribute("type", "matrix");
  6453. feColorMatrix.setAttribute("values", "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3 0.59 0.11 0 0");
  6454. filter.append(feColorMatrix);
  6455. }
  6456. #addGrayConversion(filter) {
  6457. const feColorMatrix = this.#document.createElementNS(SVG_NS, "feColorMatrix");
  6458. feColorMatrix.setAttribute("type", "matrix");
  6459. feColorMatrix.setAttribute("values", "0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0.2126 0.7152 0.0722 0 0 0 0 0 1 0");
  6460. filter.append(feColorMatrix);
  6461. }
  6462. #createFilter(id) {
  6463. const filter = this.#document.createElementNS(SVG_NS, "filter");
  6464. filter.setAttribute("color-interpolation-filters", "sRGB");
  6465. filter.setAttribute("id", id);
  6466. this.#defs.append(filter);
  6467. return filter;
  6468. }
  6469. #appendFeFunc(feComponentTransfer, func, table) {
  6470. const feFunc = this.#document.createElementNS(SVG_NS, func);
  6471. feFunc.setAttribute("type", "discrete");
  6472. feFunc.setAttribute("tableValues", table);
  6473. feComponentTransfer.append(feFunc);
  6474. }
  6475. #addTransferMapConversion(rTable, gTable, bTable, filter) {
  6476. const feComponentTransfer = this.#document.createElementNS(SVG_NS, "feComponentTransfer");
  6477. filter.append(feComponentTransfer);
  6478. this.#appendFeFunc(feComponentTransfer, "feFuncR", rTable);
  6479. this.#appendFeFunc(feComponentTransfer, "feFuncG", gTable);
  6480. this.#appendFeFunc(feComponentTransfer, "feFuncB", bTable);
  6481. }
  6482. #addTransferMapAlphaConversion(aTable, filter) {
  6483. const feComponentTransfer = this.#document.createElementNS(SVG_NS, "feComponentTransfer");
  6484. filter.append(feComponentTransfer);
  6485. this.#appendFeFunc(feComponentTransfer, "feFuncA", aTable);
  6486. }
  6487. #getRGB(color) {
  6488. this.#defs.style.color = color;
  6489. return getRGB(getComputedStyle(this.#defs).getPropertyValue("color"));
  6490. }
  6491. }
  6492. ;// ./src/display/standard_fontdata_factory.js
  6493. class BaseStandardFontDataFactory {
  6494. constructor({
  6495. baseUrl = null
  6496. }) {
  6497. this.baseUrl = baseUrl;
  6498. }
  6499. async fetch({
  6500. filename
  6501. }) {
  6502. if (!this.baseUrl) {
  6503. throw new Error("Ensure that the `standardFontDataUrl` API parameter is provided.");
  6504. }
  6505. if (!filename) {
  6506. throw new Error("Font filename must be specified.");
  6507. }
  6508. const url = `${this.baseUrl}${filename}`;
  6509. return this._fetch(url).catch(reason => {
  6510. throw new Error(`Unable to load font data at: ${url}`);
  6511. });
  6512. }
  6513. async _fetch(url) {
  6514. unreachable("Abstract method `_fetch` called.");
  6515. }
  6516. }
  6517. class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
  6518. async _fetch(url) {
  6519. const data = await fetchData(url, "arraybuffer");
  6520. return new Uint8Array(data);
  6521. }
  6522. }
  6523. ;// ./src/display/wasm_factory.js
  6524. class BaseWasmFactory {
  6525. constructor({
  6526. baseUrl = null
  6527. }) {
  6528. this.baseUrl = baseUrl;
  6529. }
  6530. async fetch({
  6531. filename
  6532. }) {
  6533. if (!this.baseUrl) {
  6534. throw new Error("Ensure that the `wasmUrl` API parameter is provided.");
  6535. }
  6536. if (!filename) {
  6537. throw new Error("Wasm filename must be specified.");
  6538. }
  6539. const url = `${this.baseUrl}${filename}`;
  6540. return this._fetch(url).catch(reason => {
  6541. throw new Error(`Unable to load wasm data at: ${url}`);
  6542. });
  6543. }
  6544. async _fetch(url) {
  6545. unreachable("Abstract method `_fetch` called.");
  6546. }
  6547. }
  6548. class DOMWasmFactory extends BaseWasmFactory {
  6549. async _fetch(url) {
  6550. const data = await fetchData(url, "arraybuffer");
  6551. return new Uint8Array(data);
  6552. }
  6553. }
  6554. ;// ./src/display/node_utils.js
  6555. if (isNodeJS) {
  6556. warn("Please use the `legacy` build in Node.js environments.");
  6557. }
  6558. async function node_utils_fetchData(url) {
  6559. const fs = process.getBuiltinModule("fs");
  6560. const data = await fs.promises.readFile(url);
  6561. return new Uint8Array(data);
  6562. }
  6563. class NodeFilterFactory extends BaseFilterFactory {}
  6564. class NodeCanvasFactory extends BaseCanvasFactory {
  6565. _createCanvas(width, height) {
  6566. const require = process.getBuiltinModule("module").createRequire(import.meta.url);
  6567. const canvas = require("@napi-rs/canvas");
  6568. return canvas.createCanvas(width, height);
  6569. }
  6570. }
  6571. class NodeCMapReaderFactory extends BaseCMapReaderFactory {
  6572. async _fetch(url) {
  6573. return node_utils_fetchData(url);
  6574. }
  6575. }
  6576. class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
  6577. async _fetch(url) {
  6578. return node_utils_fetchData(url);
  6579. }
  6580. }
  6581. class NodeWasmFactory extends BaseWasmFactory {
  6582. async _fetch(url) {
  6583. return node_utils_fetchData(url);
  6584. }
  6585. }
  6586. ;// ./src/display/pattern_helper.js
  6587. const PathType = {
  6588. FILL: "Fill",
  6589. STROKE: "Stroke",
  6590. SHADING: "Shading"
  6591. };
  6592. function applyBoundingBox(ctx, bbox) {
  6593. if (!bbox) {
  6594. return;
  6595. }
  6596. const width = bbox[2] - bbox[0];
  6597. const height = bbox[3] - bbox[1];
  6598. const region = new Path2D();
  6599. region.rect(bbox[0], bbox[1], width, height);
  6600. ctx.clip(region);
  6601. }
  6602. class BaseShadingPattern {
  6603. isModifyingCurrentTransform() {
  6604. return false;
  6605. }
  6606. getPattern() {
  6607. unreachable("Abstract method `getPattern` called.");
  6608. }
  6609. }
  6610. class RadialAxialShadingPattern extends BaseShadingPattern {
  6611. constructor(IR) {
  6612. super();
  6613. this._type = IR[1];
  6614. this._bbox = IR[2];
  6615. this._colorStops = IR[3];
  6616. this._p0 = IR[4];
  6617. this._p1 = IR[5];
  6618. this._r0 = IR[6];
  6619. this._r1 = IR[7];
  6620. this.matrix = null;
  6621. }
  6622. _createGradient(ctx) {
  6623. let grad;
  6624. if (this._type === "axial") {
  6625. grad = ctx.createLinearGradient(this._p0[0], this._p0[1], this._p1[0], this._p1[1]);
  6626. } else if (this._type === "radial") {
  6627. grad = ctx.createRadialGradient(this._p0[0], this._p0[1], this._r0, this._p1[0], this._p1[1], this._r1);
  6628. }
  6629. for (const colorStop of this._colorStops) {
  6630. grad.addColorStop(colorStop[0], colorStop[1]);
  6631. }
  6632. return grad;
  6633. }
  6634. getPattern(ctx, owner, inverse, pathType) {
  6635. let pattern;
  6636. if (pathType === PathType.STROKE || pathType === PathType.FILL) {
  6637. const ownerBBox = owner.current.getClippedPathBoundingBox(pathType, getCurrentTransform(ctx)) || [0, 0, 0, 0];
  6638. const width = Math.ceil(ownerBBox[2] - ownerBBox[0]) || 1;
  6639. const height = Math.ceil(ownerBBox[3] - ownerBBox[1]) || 1;
  6640. const tmpCanvas = owner.cachedCanvases.getCanvas("pattern", width, height);
  6641. const tmpCtx = tmpCanvas.context;
  6642. tmpCtx.clearRect(0, 0, tmpCtx.canvas.width, tmpCtx.canvas.height);
  6643. tmpCtx.beginPath();
  6644. tmpCtx.rect(0, 0, tmpCtx.canvas.width, tmpCtx.canvas.height);
  6645. tmpCtx.translate(-ownerBBox[0], -ownerBBox[1]);
  6646. inverse = Util.transform(inverse, [1, 0, 0, 1, ownerBBox[0], ownerBBox[1]]);
  6647. tmpCtx.transform(...owner.baseTransform);
  6648. if (this.matrix) {
  6649. tmpCtx.transform(...this.matrix);
  6650. }
  6651. applyBoundingBox(tmpCtx, this._bbox);
  6652. tmpCtx.fillStyle = this._createGradient(tmpCtx);
  6653. tmpCtx.fill();
  6654. pattern = ctx.createPattern(tmpCanvas.canvas, "no-repeat");
  6655. const domMatrix = new DOMMatrix(inverse);
  6656. pattern.setTransform(domMatrix);
  6657. } else {
  6658. applyBoundingBox(ctx, this._bbox);
  6659. pattern = this._createGradient(ctx);
  6660. }
  6661. return pattern;
  6662. }
  6663. }
  6664. function drawTriangle(data, context, p1, p2, p3, c1, c2, c3) {
  6665. const coords = context.coords,
  6666. colors = context.colors;
  6667. const bytes = data.data,
  6668. rowSize = data.width * 4;
  6669. let tmp;
  6670. if (coords[p1 + 1] > coords[p2 + 1]) {
  6671. tmp = p1;
  6672. p1 = p2;
  6673. p2 = tmp;
  6674. tmp = c1;
  6675. c1 = c2;
  6676. c2 = tmp;
  6677. }
  6678. if (coords[p2 + 1] > coords[p3 + 1]) {
  6679. tmp = p2;
  6680. p2 = p3;
  6681. p3 = tmp;
  6682. tmp = c2;
  6683. c2 = c3;
  6684. c3 = tmp;
  6685. }
  6686. if (coords[p1 + 1] > coords[p2 + 1]) {
  6687. tmp = p1;
  6688. p1 = p2;
  6689. p2 = tmp;
  6690. tmp = c1;
  6691. c1 = c2;
  6692. c2 = tmp;
  6693. }
  6694. const x1 = (coords[p1] + context.offsetX) * context.scaleX;
  6695. const y1 = (coords[p1 + 1] + context.offsetY) * context.scaleY;
  6696. const x2 = (coords[p2] + context.offsetX) * context.scaleX;
  6697. const y2 = (coords[p2 + 1] + context.offsetY) * context.scaleY;
  6698. const x3 = (coords[p3] + context.offsetX) * context.scaleX;
  6699. const y3 = (coords[p3 + 1] + context.offsetY) * context.scaleY;
  6700. if (y1 >= y3) {
  6701. return;
  6702. }
  6703. const c1r = colors[c1],
  6704. c1g = colors[c1 + 1],
  6705. c1b = colors[c1 + 2];
  6706. const c2r = colors[c2],
  6707. c2g = colors[c2 + 1],
  6708. c2b = colors[c2 + 2];
  6709. const c3r = colors[c3],
  6710. c3g = colors[c3 + 1],
  6711. c3b = colors[c3 + 2];
  6712. const minY = Math.round(y1),
  6713. maxY = Math.round(y3);
  6714. let xa, car, cag, cab;
  6715. let xb, cbr, cbg, cbb;
  6716. for (let y = minY; y <= maxY; y++) {
  6717. if (y < y2) {
  6718. const k = y < y1 ? 0 : (y1 - y) / (y1 - y2);
  6719. xa = x1 - (x1 - x2) * k;
  6720. car = c1r - (c1r - c2r) * k;
  6721. cag = c1g - (c1g - c2g) * k;
  6722. cab = c1b - (c1b - c2b) * k;
  6723. } else {
  6724. let k;
  6725. if (y > y3) {
  6726. k = 1;
  6727. } else if (y2 === y3) {
  6728. k = 0;
  6729. } else {
  6730. k = (y2 - y) / (y2 - y3);
  6731. }
  6732. xa = x2 - (x2 - x3) * k;
  6733. car = c2r - (c2r - c3r) * k;
  6734. cag = c2g - (c2g - c3g) * k;
  6735. cab = c2b - (c2b - c3b) * k;
  6736. }
  6737. let k;
  6738. if (y < y1) {
  6739. k = 0;
  6740. } else if (y > y3) {
  6741. k = 1;
  6742. } else {
  6743. k = (y1 - y) / (y1 - y3);
  6744. }
  6745. xb = x1 - (x1 - x3) * k;
  6746. cbr = c1r - (c1r - c3r) * k;
  6747. cbg = c1g - (c1g - c3g) * k;
  6748. cbb = c1b - (c1b - c3b) * k;
  6749. const x1_ = Math.round(Math.min(xa, xb));
  6750. const x2_ = Math.round(Math.max(xa, xb));
  6751. let j = rowSize * y + x1_ * 4;
  6752. for (let x = x1_; x <= x2_; x++) {
  6753. k = (xa - x) / (xa - xb);
  6754. if (k < 0) {
  6755. k = 0;
  6756. } else if (k > 1) {
  6757. k = 1;
  6758. }
  6759. bytes[j++] = car - (car - cbr) * k | 0;
  6760. bytes[j++] = cag - (cag - cbg) * k | 0;
  6761. bytes[j++] = cab - (cab - cbb) * k | 0;
  6762. bytes[j++] = 255;
  6763. }
  6764. }
  6765. }
  6766. function drawFigure(data, figure, context) {
  6767. const ps = figure.coords;
  6768. const cs = figure.colors;
  6769. let i, ii;
  6770. switch (figure.type) {
  6771. case "lattice":
  6772. const verticesPerRow = figure.verticesPerRow;
  6773. const rows = Math.floor(ps.length / verticesPerRow) - 1;
  6774. const cols = verticesPerRow - 1;
  6775. for (i = 0; i < rows; i++) {
  6776. let q = i * verticesPerRow;
  6777. for (let j = 0; j < cols; j++, q++) {
  6778. drawTriangle(data, context, ps[q], ps[q + 1], ps[q + verticesPerRow], cs[q], cs[q + 1], cs[q + verticesPerRow]);
  6779. drawTriangle(data, context, ps[q + verticesPerRow + 1], ps[q + 1], ps[q + verticesPerRow], cs[q + verticesPerRow + 1], cs[q + 1], cs[q + verticesPerRow]);
  6780. }
  6781. }
  6782. break;
  6783. case "triangles":
  6784. for (i = 0, ii = ps.length; i < ii; i += 3) {
  6785. drawTriangle(data, context, ps[i], ps[i + 1], ps[i + 2], cs[i], cs[i + 1], cs[i + 2]);
  6786. }
  6787. break;
  6788. default:
  6789. throw new Error("illegal figure");
  6790. }
  6791. }
  6792. class MeshShadingPattern extends BaseShadingPattern {
  6793. constructor(IR) {
  6794. super();
  6795. this._coords = IR[2];
  6796. this._colors = IR[3];
  6797. this._figures = IR[4];
  6798. this._bounds = IR[5];
  6799. this._bbox = IR[6];
  6800. this._background = IR[7];
  6801. this.matrix = null;
  6802. }
  6803. _createMeshCanvas(combinedScale, backgroundColor, cachedCanvases) {
  6804. const EXPECTED_SCALE = 1.1;
  6805. const MAX_PATTERN_SIZE = 3000;
  6806. const BORDER_SIZE = 2;
  6807. const offsetX = Math.floor(this._bounds[0]);
  6808. const offsetY = Math.floor(this._bounds[1]);
  6809. const boundsWidth = Math.ceil(this._bounds[2]) - offsetX;
  6810. const boundsHeight = Math.ceil(this._bounds[3]) - offsetY;
  6811. const width = Math.min(Math.ceil(Math.abs(boundsWidth * combinedScale[0] * EXPECTED_SCALE)), MAX_PATTERN_SIZE);
  6812. const height = Math.min(Math.ceil(Math.abs(boundsHeight * combinedScale[1] * EXPECTED_SCALE)), MAX_PATTERN_SIZE);
  6813. const scaleX = boundsWidth / width;
  6814. const scaleY = boundsHeight / height;
  6815. const context = {
  6816. coords: this._coords,
  6817. colors: this._colors,
  6818. offsetX: -offsetX,
  6819. offsetY: -offsetY,
  6820. scaleX: 1 / scaleX,
  6821. scaleY: 1 / scaleY
  6822. };
  6823. const paddedWidth = width + BORDER_SIZE * 2;
  6824. const paddedHeight = height + BORDER_SIZE * 2;
  6825. const tmpCanvas = cachedCanvases.getCanvas("mesh", paddedWidth, paddedHeight);
  6826. const tmpCtx = tmpCanvas.context;
  6827. const data = tmpCtx.createImageData(width, height);
  6828. if (backgroundColor) {
  6829. const bytes = data.data;
  6830. for (let i = 0, ii = bytes.length; i < ii; i += 4) {
  6831. bytes[i] = backgroundColor[0];
  6832. bytes[i + 1] = backgroundColor[1];
  6833. bytes[i + 2] = backgroundColor[2];
  6834. bytes[i + 3] = 255;
  6835. }
  6836. }
  6837. for (const figure of this._figures) {
  6838. drawFigure(data, figure, context);
  6839. }
  6840. tmpCtx.putImageData(data, BORDER_SIZE, BORDER_SIZE);
  6841. const canvas = tmpCanvas.canvas;
  6842. return {
  6843. canvas,
  6844. offsetX: offsetX - BORDER_SIZE * scaleX,
  6845. offsetY: offsetY - BORDER_SIZE * scaleY,
  6846. scaleX,
  6847. scaleY
  6848. };
  6849. }
  6850. isModifyingCurrentTransform() {
  6851. return true;
  6852. }
  6853. getPattern(ctx, owner, inverse, pathType) {
  6854. applyBoundingBox(ctx, this._bbox);
  6855. let scale;
  6856. if (pathType === PathType.SHADING) {
  6857. scale = Util.singularValueDecompose2dScale(getCurrentTransform(ctx));
  6858. } else {
  6859. scale = Util.singularValueDecompose2dScale(owner.baseTransform);
  6860. if (this.matrix) {
  6861. const matrixScale = Util.singularValueDecompose2dScale(this.matrix);
  6862. scale = [scale[0] * matrixScale[0], scale[1] * matrixScale[1]];
  6863. }
  6864. }
  6865. const temporaryPatternCanvas = this._createMeshCanvas(scale, pathType === PathType.SHADING ? null : this._background, owner.cachedCanvases);
  6866. if (pathType !== PathType.SHADING) {
  6867. ctx.setTransform(...owner.baseTransform);
  6868. if (this.matrix) {
  6869. ctx.transform(...this.matrix);
  6870. }
  6871. }
  6872. ctx.translate(temporaryPatternCanvas.offsetX, temporaryPatternCanvas.offsetY);
  6873. ctx.scale(temporaryPatternCanvas.scaleX, temporaryPatternCanvas.scaleY);
  6874. return ctx.createPattern(temporaryPatternCanvas.canvas, "no-repeat");
  6875. }
  6876. }
  6877. class DummyShadingPattern extends BaseShadingPattern {
  6878. getPattern() {
  6879. return "hotpink";
  6880. }
  6881. }
  6882. function getShadingPattern(IR) {
  6883. switch (IR[0]) {
  6884. case "RadialAxial":
  6885. return new RadialAxialShadingPattern(IR);
  6886. case "Mesh":
  6887. return new MeshShadingPattern(IR);
  6888. case "Dummy":
  6889. return new DummyShadingPattern();
  6890. }
  6891. throw new Error(`Unknown IR type: ${IR[0]}`);
  6892. }
  6893. const PaintType = {
  6894. COLORED: 1,
  6895. UNCOLORED: 2
  6896. };
  6897. class TilingPattern {
  6898. static MAX_PATTERN_SIZE = 3000;
  6899. constructor(IR, ctx, canvasGraphicsFactory, baseTransform) {
  6900. this.color = IR[1];
  6901. this.operatorList = IR[2];
  6902. this.matrix = IR[3];
  6903. this.bbox = IR[4];
  6904. this.xstep = IR[5];
  6905. this.ystep = IR[6];
  6906. this.paintType = IR[7];
  6907. this.tilingType = IR[8];
  6908. this.ctx = ctx;
  6909. this.canvasGraphicsFactory = canvasGraphicsFactory;
  6910. this.baseTransform = baseTransform;
  6911. }
  6912. createPatternCanvas(owner) {
  6913. const {
  6914. bbox,
  6915. operatorList,
  6916. paintType,
  6917. tilingType,
  6918. color,
  6919. canvasGraphicsFactory
  6920. } = this;
  6921. let {
  6922. xstep,
  6923. ystep
  6924. } = this;
  6925. xstep = Math.abs(xstep);
  6926. ystep = Math.abs(ystep);
  6927. info("TilingType: " + tilingType);
  6928. const x0 = bbox[0],
  6929. y0 = bbox[1],
  6930. x1 = bbox[2],
  6931. y1 = bbox[3];
  6932. const width = x1 - x0;
  6933. const height = y1 - y0;
  6934. const matrixScale = Util.singularValueDecompose2dScale(this.matrix);
  6935. const curMatrixScale = Util.singularValueDecompose2dScale(this.baseTransform);
  6936. const combinedScaleX = matrixScale[0] * curMatrixScale[0];
  6937. const combinedScaleY = matrixScale[1] * curMatrixScale[1];
  6938. let canvasWidth = width,
  6939. canvasHeight = height,
  6940. redrawHorizontally = false,
  6941. redrawVertically = false;
  6942. const xScaledStep = Math.ceil(xstep * combinedScaleX);
  6943. const yScaledStep = Math.ceil(ystep * combinedScaleY);
  6944. const xScaledWidth = Math.ceil(width * combinedScaleX);
  6945. const yScaledHeight = Math.ceil(height * combinedScaleY);
  6946. if (xScaledStep >= xScaledWidth) {
  6947. canvasWidth = xstep;
  6948. } else {
  6949. redrawHorizontally = true;
  6950. }
  6951. if (yScaledStep >= yScaledHeight) {
  6952. canvasHeight = ystep;
  6953. } else {
  6954. redrawVertically = true;
  6955. }
  6956. const dimx = this.getSizeAndScale(canvasWidth, this.ctx.canvas.width, combinedScaleX);
  6957. const dimy = this.getSizeAndScale(canvasHeight, this.ctx.canvas.height, combinedScaleY);
  6958. const tmpCanvas = owner.cachedCanvases.getCanvas("pattern", dimx.size, dimy.size);
  6959. const tmpCtx = tmpCanvas.context;
  6960. const graphics = canvasGraphicsFactory.createCanvasGraphics(tmpCtx);
  6961. graphics.groupLevel = owner.groupLevel;
  6962. this.setFillAndStrokeStyleToContext(graphics, paintType, color);
  6963. tmpCtx.translate(-dimx.scale * x0, -dimy.scale * y0);
  6964. graphics.transform(dimx.scale, 0, 0, dimy.scale, 0, 0);
  6965. tmpCtx.save();
  6966. this.clipBbox(graphics, x0, y0, x1, y1);
  6967. graphics.baseTransform = getCurrentTransform(graphics.ctx);
  6968. graphics.executeOperatorList(operatorList);
  6969. graphics.endDrawing();
  6970. tmpCtx.restore();
  6971. if (redrawHorizontally || redrawVertically) {
  6972. const image = tmpCanvas.canvas;
  6973. if (redrawHorizontally) {
  6974. canvasWidth = xstep;
  6975. }
  6976. if (redrawVertically) {
  6977. canvasHeight = ystep;
  6978. }
  6979. const dimx2 = this.getSizeAndScale(canvasWidth, this.ctx.canvas.width, combinedScaleX);
  6980. const dimy2 = this.getSizeAndScale(canvasHeight, this.ctx.canvas.height, combinedScaleY);
  6981. const xSize = dimx2.size;
  6982. const ySize = dimy2.size;
  6983. const tmpCanvas2 = owner.cachedCanvases.getCanvas("pattern-workaround", xSize, ySize);
  6984. const tmpCtx2 = tmpCanvas2.context;
  6985. const ii = redrawHorizontally ? Math.floor(width / xstep) : 0;
  6986. const jj = redrawVertically ? Math.floor(height / ystep) : 0;
  6987. for (let i = 0; i <= ii; i++) {
  6988. for (let j = 0; j <= jj; j++) {
  6989. tmpCtx2.drawImage(image, xSize * i, ySize * j, xSize, ySize, 0, 0, xSize, ySize);
  6990. }
  6991. }
  6992. return {
  6993. canvas: tmpCanvas2.canvas,
  6994. scaleX: dimx2.scale,
  6995. scaleY: dimy2.scale,
  6996. offsetX: x0,
  6997. offsetY: y0
  6998. };
  6999. }
  7000. return {
  7001. canvas: tmpCanvas.canvas,
  7002. scaleX: dimx.scale,
  7003. scaleY: dimy.scale,
  7004. offsetX: x0,
  7005. offsetY: y0
  7006. };
  7007. }
  7008. getSizeAndScale(step, realOutputSize, scale) {
  7009. const maxSize = Math.max(TilingPattern.MAX_PATTERN_SIZE, realOutputSize);
  7010. let size = Math.ceil(step * scale);
  7011. if (size >= maxSize) {
  7012. size = maxSize;
  7013. } else {
  7014. scale = size / step;
  7015. }
  7016. return {
  7017. scale,
  7018. size
  7019. };
  7020. }
  7021. clipBbox(graphics, x0, y0, x1, y1) {
  7022. const bboxWidth = x1 - x0;
  7023. const bboxHeight = y1 - y0;
  7024. graphics.ctx.rect(x0, y0, bboxWidth, bboxHeight);
  7025. graphics.current.updateRectMinMax(getCurrentTransform(graphics.ctx), [x0, y0, x1, y1]);
  7026. graphics.clip();
  7027. graphics.endPath();
  7028. }
  7029. setFillAndStrokeStyleToContext(graphics, paintType, color) {
  7030. const context = graphics.ctx,
  7031. current = graphics.current;
  7032. switch (paintType) {
  7033. case PaintType.COLORED:
  7034. const ctx = this.ctx;
  7035. context.fillStyle = ctx.fillStyle;
  7036. context.strokeStyle = ctx.strokeStyle;
  7037. current.fillColor = ctx.fillStyle;
  7038. current.strokeColor = ctx.strokeStyle;
  7039. break;
  7040. case PaintType.UNCOLORED:
  7041. const cssColor = Util.makeHexColor(color[0], color[1], color[2]);
  7042. context.fillStyle = cssColor;
  7043. context.strokeStyle = cssColor;
  7044. current.fillColor = cssColor;
  7045. current.strokeColor = cssColor;
  7046. break;
  7047. default:
  7048. throw new FormatError(`Unsupported paint type: ${paintType}`);
  7049. }
  7050. }
  7051. isModifyingCurrentTransform() {
  7052. return false;
  7053. }
  7054. getPattern(ctx, owner, inverse, pathType) {
  7055. let matrix = inverse;
  7056. if (pathType !== PathType.SHADING) {
  7057. matrix = Util.transform(matrix, owner.baseTransform);
  7058. if (this.matrix) {
  7059. matrix = Util.transform(matrix, this.matrix);
  7060. }
  7061. }
  7062. const temporaryPatternCanvas = this.createPatternCanvas(owner);
  7063. let domMatrix = new DOMMatrix(matrix);
  7064. domMatrix = domMatrix.translate(temporaryPatternCanvas.offsetX, temporaryPatternCanvas.offsetY);
  7065. domMatrix = domMatrix.scale(1 / temporaryPatternCanvas.scaleX, 1 / temporaryPatternCanvas.scaleY);
  7066. const pattern = ctx.createPattern(temporaryPatternCanvas.canvas, "repeat");
  7067. pattern.setTransform(domMatrix);
  7068. return pattern;
  7069. }
  7070. }
  7071. ;// ./src/shared/image_utils.js
  7072. function convertToRGBA(params) {
  7073. switch (params.kind) {
  7074. case ImageKind.GRAYSCALE_1BPP:
  7075. return convertBlackAndWhiteToRGBA(params);
  7076. case ImageKind.RGB_24BPP:
  7077. return convertRGBToRGBA(params);
  7078. }
  7079. return null;
  7080. }
  7081. function convertBlackAndWhiteToRGBA({
  7082. src,
  7083. srcPos = 0,
  7084. dest,
  7085. width,
  7086. height,
  7087. nonBlackColor = 0xffffffff,
  7088. inverseDecode = false
  7089. }) {
  7090. const black = util_FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
  7091. const [zeroMapping, oneMapping] = inverseDecode ? [nonBlackColor, black] : [black, nonBlackColor];
  7092. const widthInSource = width >> 3;
  7093. const widthRemainder = width & 7;
  7094. const srcLength = src.length;
  7095. dest = new Uint32Array(dest.buffer);
  7096. let destPos = 0;
  7097. for (let i = 0; i < height; i++) {
  7098. for (const max = srcPos + widthInSource; srcPos < max; srcPos++) {
  7099. const elem = srcPos < srcLength ? src[srcPos] : 255;
  7100. dest[destPos++] = elem & 0b10000000 ? oneMapping : zeroMapping;
  7101. dest[destPos++] = elem & 0b1000000 ? oneMapping : zeroMapping;
  7102. dest[destPos++] = elem & 0b100000 ? oneMapping : zeroMapping;
  7103. dest[destPos++] = elem & 0b10000 ? oneMapping : zeroMapping;
  7104. dest[destPos++] = elem & 0b1000 ? oneMapping : zeroMapping;
  7105. dest[destPos++] = elem & 0b100 ? oneMapping : zeroMapping;
  7106. dest[destPos++] = elem & 0b10 ? oneMapping : zeroMapping;
  7107. dest[destPos++] = elem & 0b1 ? oneMapping : zeroMapping;
  7108. }
  7109. if (widthRemainder === 0) {
  7110. continue;
  7111. }
  7112. const elem = srcPos < srcLength ? src[srcPos++] : 255;
  7113. for (let j = 0; j < widthRemainder; j++) {
  7114. dest[destPos++] = elem & 1 << 7 - j ? oneMapping : zeroMapping;
  7115. }
  7116. }
  7117. return {
  7118. srcPos,
  7119. destPos
  7120. };
  7121. }
  7122. function convertRGBToRGBA({
  7123. src,
  7124. srcPos = 0,
  7125. dest,
  7126. destPos = 0,
  7127. width,
  7128. height
  7129. }) {
  7130. let i = 0;
  7131. const len = width * height * 3;
  7132. const len32 = len >> 2;
  7133. const src32 = new Uint32Array(src.buffer, srcPos, len32);
  7134. if (FeatureTest.isLittleEndian) {
  7135. for (; i < len32 - 2; i += 3, destPos += 4) {
  7136. const s1 = src32[i];
  7137. const s2 = src32[i + 1];
  7138. const s3 = src32[i + 2];
  7139. dest[destPos] = s1 | 0xff000000;
  7140. dest[destPos + 1] = s1 >>> 24 | s2 << 8 | 0xff000000;
  7141. dest[destPos + 2] = s2 >>> 16 | s3 << 16 | 0xff000000;
  7142. dest[destPos + 3] = s3 >>> 8 | 0xff000000;
  7143. }
  7144. for (let j = i * 4, jj = srcPos + len; j < jj; j += 3) {
  7145. dest[destPos++] = src[j] | src[j + 1] << 8 | src[j + 2] << 16 | 0xff000000;
  7146. }
  7147. } else {
  7148. for (; i < len32 - 2; i += 3, destPos += 4) {
  7149. const s1 = src32[i];
  7150. const s2 = src32[i + 1];
  7151. const s3 = src32[i + 2];
  7152. dest[destPos] = s1 | 0xff;
  7153. dest[destPos + 1] = s1 << 24 | s2 >>> 8 | 0xff;
  7154. dest[destPos + 2] = s2 << 16 | s3 >>> 16 | 0xff;
  7155. dest[destPos + 3] = s3 << 8 | 0xff;
  7156. }
  7157. for (let j = i * 4, jj = srcPos + len; j < jj; j += 3) {
  7158. dest[destPos++] = src[j] << 24 | src[j + 1] << 16 | src[j + 2] << 8 | 0xff;
  7159. }
  7160. }
  7161. return {
  7162. srcPos: srcPos + len,
  7163. destPos
  7164. };
  7165. }
  7166. function grayToRGBA(src, dest) {
  7167. if (FeatureTest.isLittleEndian) {
  7168. for (let i = 0, ii = src.length; i < ii; i++) {
  7169. dest[i] = src[i] * 0x10101 | 0xff000000;
  7170. }
  7171. } else {
  7172. for (let i = 0, ii = src.length; i < ii; i++) {
  7173. dest[i] = src[i] * 0x1010100 | 0x000000ff;
  7174. }
  7175. }
  7176. }
  7177. ;// ./src/display/canvas.js
  7178. const MIN_FONT_SIZE = 16;
  7179. const MAX_FONT_SIZE = 100;
  7180. const EXECUTION_TIME = 15;
  7181. const EXECUTION_STEPS = 10;
  7182. const MAX_SIZE_TO_COMPILE = 1000;
  7183. const FULL_CHUNK_HEIGHT = 16;
  7184. const SCALE_MATRIX = new DOMMatrix();
  7185. function mirrorContextOperations(ctx, destCtx) {
  7186. if (ctx._removeMirroring) {
  7187. throw new Error("Context is already forwarding operations.");
  7188. }
  7189. ctx.__originalSave = ctx.save;
  7190. ctx.__originalRestore = ctx.restore;
  7191. ctx.__originalRotate = ctx.rotate;
  7192. ctx.__originalScale = ctx.scale;
  7193. ctx.__originalTranslate = ctx.translate;
  7194. ctx.__originalTransform = ctx.transform;
  7195. ctx.__originalSetTransform = ctx.setTransform;
  7196. ctx.__originalResetTransform = ctx.resetTransform;
  7197. ctx.__originalClip = ctx.clip;
  7198. ctx.__originalMoveTo = ctx.moveTo;
  7199. ctx.__originalLineTo = ctx.lineTo;
  7200. ctx.__originalBezierCurveTo = ctx.bezierCurveTo;
  7201. ctx.__originalRect = ctx.rect;
  7202. ctx.__originalClosePath = ctx.closePath;
  7203. ctx.__originalBeginPath = ctx.beginPath;
  7204. ctx._removeMirroring = () => {
  7205. ctx.save = ctx.__originalSave;
  7206. ctx.restore = ctx.__originalRestore;
  7207. ctx.rotate = ctx.__originalRotate;
  7208. ctx.scale = ctx.__originalScale;
  7209. ctx.translate = ctx.__originalTranslate;
  7210. ctx.transform = ctx.__originalTransform;
  7211. ctx.setTransform = ctx.__originalSetTransform;
  7212. ctx.resetTransform = ctx.__originalResetTransform;
  7213. ctx.clip = ctx.__originalClip;
  7214. ctx.moveTo = ctx.__originalMoveTo;
  7215. ctx.lineTo = ctx.__originalLineTo;
  7216. ctx.bezierCurveTo = ctx.__originalBezierCurveTo;
  7217. ctx.rect = ctx.__originalRect;
  7218. ctx.closePath = ctx.__originalClosePath;
  7219. ctx.beginPath = ctx.__originalBeginPath;
  7220. delete ctx._removeMirroring;
  7221. };
  7222. ctx.save = function ctxSave() {
  7223. destCtx.save();
  7224. this.__originalSave();
  7225. };
  7226. ctx.restore = function ctxRestore() {
  7227. destCtx.restore();
  7228. this.__originalRestore();
  7229. };
  7230. ctx.translate = function ctxTranslate(x, y) {
  7231. destCtx.translate(x, y);
  7232. this.__originalTranslate(x, y);
  7233. };
  7234. ctx.scale = function ctxScale(x, y) {
  7235. destCtx.scale(x, y);
  7236. this.__originalScale(x, y);
  7237. };
  7238. ctx.transform = function ctxTransform(a, b, c, d, e, f) {
  7239. destCtx.transform(a, b, c, d, e, f);
  7240. this.__originalTransform(a, b, c, d, e, f);
  7241. };
  7242. ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
  7243. destCtx.setTransform(a, b, c, d, e, f);
  7244. this.__originalSetTransform(a, b, c, d, e, f);
  7245. };
  7246. ctx.resetTransform = function ctxResetTransform() {
  7247. destCtx.resetTransform();
  7248. this.__originalResetTransform();
  7249. };
  7250. ctx.rotate = function ctxRotate(angle) {
  7251. destCtx.rotate(angle);
  7252. this.__originalRotate(angle);
  7253. };
  7254. ctx.clip = function ctxRotate(rule) {
  7255. destCtx.clip(rule);
  7256. this.__originalClip(rule);
  7257. };
  7258. ctx.moveTo = function (x, y) {
  7259. destCtx.moveTo(x, y);
  7260. this.__originalMoveTo(x, y);
  7261. };
  7262. ctx.lineTo = function (x, y) {
  7263. destCtx.lineTo(x, y);
  7264. this.__originalLineTo(x, y);
  7265. };
  7266. ctx.bezierCurveTo = function (cp1x, cp1y, cp2x, cp2y, x, y) {
  7267. destCtx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  7268. this.__originalBezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  7269. };
  7270. ctx.rect = function (x, y, width, height) {
  7271. destCtx.rect(x, y, width, height);
  7272. this.__originalRect(x, y, width, height);
  7273. };
  7274. ctx.closePath = function () {
  7275. destCtx.closePath();
  7276. this.__originalClosePath();
  7277. };
  7278. ctx.beginPath = function () {
  7279. destCtx.beginPath();
  7280. this.__originalBeginPath();
  7281. };
  7282. }
  7283. class CachedCanvases {
  7284. constructor(canvasFactory) {
  7285. this.canvasFactory = canvasFactory;
  7286. this.cache = Object.create(null);
  7287. }
  7288. getCanvas(id, width, height) {
  7289. let canvasEntry;
  7290. if (this.cache[id] !== undefined) {
  7291. canvasEntry = this.cache[id];
  7292. this.canvasFactory.reset(canvasEntry, width, height);
  7293. } else {
  7294. canvasEntry = this.canvasFactory.create(width, height);
  7295. this.cache[id] = canvasEntry;
  7296. }
  7297. return canvasEntry;
  7298. }
  7299. delete(id) {
  7300. delete this.cache[id];
  7301. }
  7302. clear() {
  7303. for (const id in this.cache) {
  7304. const canvasEntry = this.cache[id];
  7305. this.canvasFactory.destroy(canvasEntry);
  7306. delete this.cache[id];
  7307. }
  7308. }
  7309. }
  7310. function drawImageAtIntegerCoords(ctx, srcImg, srcX, srcY, srcW, srcH, destX, destY, destW, destH) {
  7311. const [a, b, c, d, tx, ty] = getCurrentTransform(ctx);
  7312. if (b === 0 && c === 0) {
  7313. const tlX = destX * a + tx;
  7314. const rTlX = Math.round(tlX);
  7315. const tlY = destY * d + ty;
  7316. const rTlY = Math.round(tlY);
  7317. const brX = (destX + destW) * a + tx;
  7318. const rWidth = Math.abs(Math.round(brX) - rTlX) || 1;
  7319. const brY = (destY + destH) * d + ty;
  7320. const rHeight = Math.abs(Math.round(brY) - rTlY) || 1;
  7321. ctx.setTransform(Math.sign(a), 0, 0, Math.sign(d), rTlX, rTlY);
  7322. ctx.drawImage(srcImg, srcX, srcY, srcW, srcH, 0, 0, rWidth, rHeight);
  7323. ctx.setTransform(a, b, c, d, tx, ty);
  7324. return [rWidth, rHeight];
  7325. }
  7326. if (a === 0 && d === 0) {
  7327. const tlX = destY * c + tx;
  7328. const rTlX = Math.round(tlX);
  7329. const tlY = destX * b + ty;
  7330. const rTlY = Math.round(tlY);
  7331. const brX = (destY + destH) * c + tx;
  7332. const rWidth = Math.abs(Math.round(brX) - rTlX) || 1;
  7333. const brY = (destX + destW) * b + ty;
  7334. const rHeight = Math.abs(Math.round(brY) - rTlY) || 1;
  7335. ctx.setTransform(0, Math.sign(b), Math.sign(c), 0, rTlX, rTlY);
  7336. ctx.drawImage(srcImg, srcX, srcY, srcW, srcH, 0, 0, rHeight, rWidth);
  7337. ctx.setTransform(a, b, c, d, tx, ty);
  7338. return [rHeight, rWidth];
  7339. }
  7340. ctx.drawImage(srcImg, srcX, srcY, srcW, srcH, destX, destY, destW, destH);
  7341. const scaleX = Math.hypot(a, b);
  7342. const scaleY = Math.hypot(c, d);
  7343. return [scaleX * destW, scaleY * destH];
  7344. }
  7345. function compileType3Glyph(imgData) {
  7346. const {
  7347. width,
  7348. height
  7349. } = imgData;
  7350. if (width > MAX_SIZE_TO_COMPILE || height > MAX_SIZE_TO_COMPILE) {
  7351. return null;
  7352. }
  7353. const POINT_TO_PROCESS_LIMIT = 1000;
  7354. const POINT_TYPES = new Uint8Array([0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0]);
  7355. const width1 = width + 1;
  7356. const points = new Uint8Array(width1 * (height + 1));
  7357. let i, j, j0;
  7358. const lineSize = width + 7 & ~7;
  7359. const data = new Uint8Array(lineSize * height);
  7360. let pos = 0;
  7361. for (const elem of imgData.data) {
  7362. let mask = 128;
  7363. while (mask > 0) {
  7364. data[pos++] = elem & mask ? 0 : 255;
  7365. mask >>= 1;
  7366. }
  7367. }
  7368. let count = 0;
  7369. pos = 0;
  7370. if (data[pos] !== 0) {
  7371. points[0] = 1;
  7372. ++count;
  7373. }
  7374. for (j = 1; j < width; j++) {
  7375. if (data[pos] !== data[pos + 1]) {
  7376. points[j] = data[pos] ? 2 : 1;
  7377. ++count;
  7378. }
  7379. pos++;
  7380. }
  7381. if (data[pos] !== 0) {
  7382. points[j] = 2;
  7383. ++count;
  7384. }
  7385. for (i = 1; i < height; i++) {
  7386. pos = i * lineSize;
  7387. j0 = i * width1;
  7388. if (data[pos - lineSize] !== data[pos]) {
  7389. points[j0] = data[pos] ? 1 : 8;
  7390. ++count;
  7391. }
  7392. let sum = (data[pos] ? 4 : 0) + (data[pos - lineSize] ? 8 : 0);
  7393. for (j = 1; j < width; j++) {
  7394. sum = (sum >> 2) + (data[pos + 1] ? 4 : 0) + (data[pos - lineSize + 1] ? 8 : 0);
  7395. if (POINT_TYPES[sum]) {
  7396. points[j0 + j] = POINT_TYPES[sum];
  7397. ++count;
  7398. }
  7399. pos++;
  7400. }
  7401. if (data[pos - lineSize] !== data[pos]) {
  7402. points[j0 + j] = data[pos] ? 2 : 4;
  7403. ++count;
  7404. }
  7405. if (count > POINT_TO_PROCESS_LIMIT) {
  7406. return null;
  7407. }
  7408. }
  7409. pos = lineSize * (height - 1);
  7410. j0 = i * width1;
  7411. if (data[pos] !== 0) {
  7412. points[j0] = 8;
  7413. ++count;
  7414. }
  7415. for (j = 1; j < width; j++) {
  7416. if (data[pos] !== data[pos + 1]) {
  7417. points[j0 + j] = data[pos] ? 4 : 8;
  7418. ++count;
  7419. }
  7420. pos++;
  7421. }
  7422. if (data[pos] !== 0) {
  7423. points[j0 + j] = 4;
  7424. ++count;
  7425. }
  7426. if (count > POINT_TO_PROCESS_LIMIT) {
  7427. return null;
  7428. }
  7429. const steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
  7430. const path = new Path2D();
  7431. const {
  7432. a,
  7433. b,
  7434. c,
  7435. d,
  7436. e,
  7437. f
  7438. } = new DOMMatrix().scaleSelf(1 / width, -1 / height).translateSelf(0, -height);
  7439. for (i = 0; count && i <= height; i++) {
  7440. let p = i * width1;
  7441. const end = p + width;
  7442. while (p < end && !points[p]) {
  7443. p++;
  7444. }
  7445. if (p === end) {
  7446. continue;
  7447. }
  7448. let x = p % width1;
  7449. let y = i;
  7450. path.moveTo(a * x + c * y + e, b * x + d * y + f);
  7451. const p0 = p;
  7452. let type = points[p];
  7453. do {
  7454. const step = steps[type];
  7455. do {
  7456. p += step;
  7457. } while (!points[p]);
  7458. const pp = points[p];
  7459. if (pp !== 5 && pp !== 10) {
  7460. type = pp;
  7461. points[p] = 0;
  7462. } else {
  7463. type = pp & 0x33 * type >> 4;
  7464. points[p] &= type >> 2 | type << 2;
  7465. }
  7466. x = p % width1;
  7467. y = p / width1 | 0;
  7468. path.lineTo(a * x + c * y + e, b * x + d * y + f);
  7469. if (!points[p]) {
  7470. --count;
  7471. }
  7472. } while (p0 !== p);
  7473. --i;
  7474. }
  7475. return path;
  7476. }
  7477. class CanvasExtraState {
  7478. constructor(width, height) {
  7479. this.alphaIsShape = false;
  7480. this.fontSize = 0;
  7481. this.fontSizeScale = 1;
  7482. this.textMatrix = IDENTITY_MATRIX;
  7483. this.textMatrixScale = 1;
  7484. this.fontMatrix = FONT_IDENTITY_MATRIX;
  7485. this.leading = 0;
  7486. this.x = 0;
  7487. this.y = 0;
  7488. this.lineX = 0;
  7489. this.lineY = 0;
  7490. this.charSpacing = 0;
  7491. this.wordSpacing = 0;
  7492. this.textHScale = 1;
  7493. this.textRenderingMode = TextRenderingMode.FILL;
  7494. this.textRise = 0;
  7495. this.fillColor = "#000000";
  7496. this.strokeColor = "#000000";
  7497. this.patternFill = false;
  7498. this.patternStroke = false;
  7499. this.fillAlpha = 1;
  7500. this.strokeAlpha = 1;
  7501. this.lineWidth = 1;
  7502. this.activeSMask = null;
  7503. this.transferMaps = "none";
  7504. this.startNewPathAndClipBox([0, 0, width, height]);
  7505. }
  7506. clone() {
  7507. const clone = Object.create(this);
  7508. clone.clipBox = this.clipBox.slice();
  7509. return clone;
  7510. }
  7511. updateRectMinMax(transform, rect) {
  7512. const p1 = Util.applyTransform(rect, transform);
  7513. const p2 = Util.applyTransform(rect.slice(2), transform);
  7514. const p3 = Util.applyTransform([rect[0], rect[3]], transform);
  7515. const p4 = Util.applyTransform([rect[2], rect[1]], transform);
  7516. this.minX = Math.min(this.minX, p1[0], p2[0], p3[0], p4[0]);
  7517. this.minY = Math.min(this.minY, p1[1], p2[1], p3[1], p4[1]);
  7518. this.maxX = Math.max(this.maxX, p1[0], p2[0], p3[0], p4[0]);
  7519. this.maxY = Math.max(this.maxY, p1[1], p2[1], p3[1], p4[1]);
  7520. }
  7521. getPathBoundingBox(pathType = PathType.FILL, transform = null) {
  7522. const box = [this.minX, this.minY, this.maxX, this.maxY];
  7523. if (pathType === PathType.STROKE) {
  7524. if (!transform) {
  7525. unreachable("Stroke bounding box must include transform.");
  7526. }
  7527. const scale = Util.singularValueDecompose2dScale(transform);
  7528. const xStrokePad = scale[0] * this.lineWidth / 2;
  7529. const yStrokePad = scale[1] * this.lineWidth / 2;
  7530. box[0] -= xStrokePad;
  7531. box[1] -= yStrokePad;
  7532. box[2] += xStrokePad;
  7533. box[3] += yStrokePad;
  7534. }
  7535. return box;
  7536. }
  7537. updateClipFromPath() {
  7538. const intersect = Util.intersect(this.clipBox, this.getPathBoundingBox());
  7539. this.startNewPathAndClipBox(intersect || [0, 0, 0, 0]);
  7540. }
  7541. isEmptyClip() {
  7542. return this.minX === Infinity;
  7543. }
  7544. startNewPathAndClipBox(box) {
  7545. this.clipBox = box;
  7546. this.minX = Infinity;
  7547. this.minY = Infinity;
  7548. this.maxX = 0;
  7549. this.maxY = 0;
  7550. }
  7551. getClippedPathBoundingBox(pathType = PathType.FILL, transform = null) {
  7552. return Util.intersect(this.clipBox, this.getPathBoundingBox(pathType, transform));
  7553. }
  7554. }
  7555. function putBinaryImageData(ctx, imgData) {
  7556. if (imgData instanceof ImageData) {
  7557. ctx.putImageData(imgData, 0, 0);
  7558. return;
  7559. }
  7560. const height = imgData.height,
  7561. width = imgData.width;
  7562. const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  7563. const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  7564. const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  7565. const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  7566. let srcPos = 0,
  7567. destPos;
  7568. const src = imgData.data;
  7569. const dest = chunkImgData.data;
  7570. let i, j, thisChunkHeight, elemsInThisChunk;
  7571. if (imgData.kind === util_ImageKind.GRAYSCALE_1BPP) {
  7572. const srcLength = src.byteLength;
  7573. const dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2);
  7574. const dest32DataLength = dest32.length;
  7575. const fullSrcDiff = width + 7 >> 3;
  7576. const white = 0xffffffff;
  7577. const black = util_FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
  7578. for (i = 0; i < totalChunks; i++) {
  7579. thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  7580. destPos = 0;
  7581. for (j = 0; j < thisChunkHeight; j++) {
  7582. const srcDiff = srcLength - srcPos;
  7583. let k = 0;
  7584. const kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7;
  7585. const kEndUnrolled = kEnd & ~7;
  7586. let mask = 0;
  7587. let srcByte = 0;
  7588. for (; k < kEndUnrolled; k += 8) {
  7589. srcByte = src[srcPos++];
  7590. dest32[destPos++] = srcByte & 128 ? white : black;
  7591. dest32[destPos++] = srcByte & 64 ? white : black;
  7592. dest32[destPos++] = srcByte & 32 ? white : black;
  7593. dest32[destPos++] = srcByte & 16 ? white : black;
  7594. dest32[destPos++] = srcByte & 8 ? white : black;
  7595. dest32[destPos++] = srcByte & 4 ? white : black;
  7596. dest32[destPos++] = srcByte & 2 ? white : black;
  7597. dest32[destPos++] = srcByte & 1 ? white : black;
  7598. }
  7599. for (; k < kEnd; k++) {
  7600. if (mask === 0) {
  7601. srcByte = src[srcPos++];
  7602. mask = 128;
  7603. }
  7604. dest32[destPos++] = srcByte & mask ? white : black;
  7605. mask >>= 1;
  7606. }
  7607. }
  7608. while (destPos < dest32DataLength) {
  7609. dest32[destPos++] = 0;
  7610. }
  7611. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  7612. }
  7613. } else if (imgData.kind === util_ImageKind.RGBA_32BPP) {
  7614. j = 0;
  7615. elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
  7616. for (i = 0; i < fullChunks; i++) {
  7617. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  7618. srcPos += elemsInThisChunk;
  7619. ctx.putImageData(chunkImgData, 0, j);
  7620. j += FULL_CHUNK_HEIGHT;
  7621. }
  7622. if (i < totalChunks) {
  7623. elemsInThisChunk = width * partialChunkHeight * 4;
  7624. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  7625. ctx.putImageData(chunkImgData, 0, j);
  7626. }
  7627. } else if (imgData.kind === util_ImageKind.RGB_24BPP) {
  7628. thisChunkHeight = FULL_CHUNK_HEIGHT;
  7629. elemsInThisChunk = width * thisChunkHeight;
  7630. for (i = 0; i < totalChunks; i++) {
  7631. if (i >= fullChunks) {
  7632. thisChunkHeight = partialChunkHeight;
  7633. elemsInThisChunk = width * thisChunkHeight;
  7634. }
  7635. destPos = 0;
  7636. for (j = elemsInThisChunk; j--;) {
  7637. dest[destPos++] = src[srcPos++];
  7638. dest[destPos++] = src[srcPos++];
  7639. dest[destPos++] = src[srcPos++];
  7640. dest[destPos++] = 255;
  7641. }
  7642. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  7643. }
  7644. } else {
  7645. throw new Error(`bad image kind: ${imgData.kind}`);
  7646. }
  7647. }
  7648. function putBinaryImageMask(ctx, imgData) {
  7649. if (imgData.bitmap) {
  7650. ctx.drawImage(imgData.bitmap, 0, 0);
  7651. return;
  7652. }
  7653. const height = imgData.height,
  7654. width = imgData.width;
  7655. const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  7656. const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  7657. const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  7658. const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  7659. let srcPos = 0;
  7660. const src = imgData.data;
  7661. const dest = chunkImgData.data;
  7662. for (let i = 0; i < totalChunks; i++) {
  7663. const thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  7664. ({
  7665. srcPos
  7666. } = convertBlackAndWhiteToRGBA({
  7667. src,
  7668. srcPos,
  7669. dest,
  7670. width,
  7671. height: thisChunkHeight,
  7672. nonBlackColor: 0
  7673. }));
  7674. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  7675. }
  7676. }
  7677. function copyCtxState(sourceCtx, destCtx) {
  7678. const properties = ["strokeStyle", "fillStyle", "fillRule", "globalAlpha", "lineWidth", "lineCap", "lineJoin", "miterLimit", "globalCompositeOperation", "font", "filter"];
  7679. for (const property of properties) {
  7680. if (sourceCtx[property] !== undefined) {
  7681. destCtx[property] = sourceCtx[property];
  7682. }
  7683. }
  7684. if (sourceCtx.setLineDash !== undefined) {
  7685. destCtx.setLineDash(sourceCtx.getLineDash());
  7686. destCtx.lineDashOffset = sourceCtx.lineDashOffset;
  7687. }
  7688. }
  7689. function resetCtxToDefault(ctx) {
  7690. ctx.strokeStyle = ctx.fillStyle = "#000000";
  7691. ctx.fillRule = "nonzero";
  7692. ctx.globalAlpha = 1;
  7693. ctx.lineWidth = 1;
  7694. ctx.lineCap = "butt";
  7695. ctx.lineJoin = "miter";
  7696. ctx.miterLimit = 10;
  7697. ctx.globalCompositeOperation = "source-over";
  7698. ctx.font = "10px sans-serif";
  7699. if (ctx.setLineDash !== undefined) {
  7700. ctx.setLineDash([]);
  7701. ctx.lineDashOffset = 0;
  7702. }
  7703. if (!isNodeJS) {
  7704. const {
  7705. filter
  7706. } = ctx;
  7707. if (filter !== "none" && filter !== "") {
  7708. ctx.filter = "none";
  7709. }
  7710. }
  7711. }
  7712. function getImageSmoothingEnabled(transform, interpolate) {
  7713. if (interpolate) {
  7714. return true;
  7715. }
  7716. const scale = Util.singularValueDecompose2dScale(transform);
  7717. scale[0] = Math.fround(scale[0]);
  7718. scale[1] = Math.fround(scale[1]);
  7719. const actualScale = Math.fround(OutputScale.pixelRatio * PixelsPerInch.PDF_TO_CSS_UNITS);
  7720. return scale[0] <= actualScale && scale[1] <= actualScale;
  7721. }
  7722. const LINE_CAP_STYLES = ["butt", "round", "square"];
  7723. const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
  7724. const NORMAL_CLIP = {};
  7725. const EO_CLIP = {};
  7726. class CanvasGraphics {
  7727. constructor(canvasCtx, commonObjs, objs, canvasFactory, filterFactory, {
  7728. optionalContentConfig,
  7729. markedContentStack = null
  7730. }, annotationCanvasMap, pageColors) {
  7731. this.ctx = canvasCtx;
  7732. this.current = new CanvasExtraState(this.ctx.canvas.width, this.ctx.canvas.height);
  7733. this.stateStack = [];
  7734. this.pendingClip = null;
  7735. this.pendingEOFill = false;
  7736. this.res = null;
  7737. this.xobjs = null;
  7738. this.commonObjs = commonObjs;
  7739. this.objs = objs;
  7740. this.canvasFactory = canvasFactory;
  7741. this.filterFactory = filterFactory;
  7742. this.groupStack = [];
  7743. this.processingType3 = null;
  7744. this.baseTransform = null;
  7745. this.baseTransformStack = [];
  7746. this.groupLevel = 0;
  7747. this.smaskStack = [];
  7748. this.smaskCounter = 0;
  7749. this.tempSMask = null;
  7750. this.suspendedCtx = null;
  7751. this.contentVisible = true;
  7752. this.markedContentStack = markedContentStack || [];
  7753. this.optionalContentConfig = optionalContentConfig;
  7754. this.cachedCanvases = new CachedCanvases(this.canvasFactory);
  7755. this.cachedPatterns = new Map();
  7756. this.annotationCanvasMap = annotationCanvasMap;
  7757. this.viewportScale = 1;
  7758. this.outputScaleX = 1;
  7759. this.outputScaleY = 1;
  7760. this.pageColors = pageColors;
  7761. this._cachedScaleForStroking = [-1, 0];
  7762. this._cachedGetSinglePixelWidth = null;
  7763. this._cachedBitmapsMap = new Map();
  7764. }
  7765. getObject(data, fallback = null) {
  7766. if (typeof data === "string") {
  7767. return data.startsWith("g_") ? this.commonObjs.get(data) : this.objs.get(data);
  7768. }
  7769. return fallback;
  7770. }
  7771. beginDrawing({
  7772. transform,
  7773. viewport,
  7774. transparency = false,
  7775. background = null
  7776. }) {
  7777. const width = this.ctx.canvas.width;
  7778. const height = this.ctx.canvas.height;
  7779. const savedFillStyle = this.ctx.fillStyle;
  7780. this.ctx.fillStyle = background || "#ffffff";
  7781. this.ctx.fillRect(0, 0, width, height);
  7782. this.ctx.fillStyle = savedFillStyle;
  7783. if (transparency) {
  7784. const transparentCanvas = this.cachedCanvases.getCanvas("transparent", width, height);
  7785. this.compositeCtx = this.ctx;
  7786. this.transparentCanvas = transparentCanvas.canvas;
  7787. this.ctx = transparentCanvas.context;
  7788. this.ctx.save();
  7789. this.ctx.transform(...getCurrentTransform(this.compositeCtx));
  7790. }
  7791. this.ctx.save();
  7792. resetCtxToDefault(this.ctx);
  7793. if (transform) {
  7794. this.ctx.transform(...transform);
  7795. this.outputScaleX = transform[0];
  7796. this.outputScaleY = transform[0];
  7797. }
  7798. this.ctx.transform(...viewport.transform);
  7799. this.viewportScale = viewport.scale;
  7800. this.baseTransform = getCurrentTransform(this.ctx);
  7801. }
  7802. executeOperatorList(operatorList, executionStartIdx, continueCallback, stepper) {
  7803. const argsArray = operatorList.argsArray;
  7804. const fnArray = operatorList.fnArray;
  7805. let i = executionStartIdx || 0;
  7806. const argsArrayLen = argsArray.length;
  7807. if (argsArrayLen === i) {
  7808. return i;
  7809. }
  7810. const chunkOperations = argsArrayLen - i > EXECUTION_STEPS && typeof continueCallback === "function";
  7811. const endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
  7812. let steps = 0;
  7813. const commonObjs = this.commonObjs;
  7814. const objs = this.objs;
  7815. let fnId;
  7816. while (true) {
  7817. if (stepper !== undefined && i === stepper.nextBreakPoint) {
  7818. stepper.breakIt(i, continueCallback);
  7819. return i;
  7820. }
  7821. fnId = fnArray[i];
  7822. if (fnId !== OPS.dependency) {
  7823. this[fnId].apply(this, argsArray[i]);
  7824. } else {
  7825. for (const depObjId of argsArray[i]) {
  7826. const objsPool = depObjId.startsWith("g_") ? commonObjs : objs;
  7827. if (!objsPool.has(depObjId)) {
  7828. objsPool.get(depObjId, continueCallback);
  7829. return i;
  7830. }
  7831. }
  7832. }
  7833. i++;
  7834. if (i === argsArrayLen) {
  7835. return i;
  7836. }
  7837. if (chunkOperations && ++steps > EXECUTION_STEPS) {
  7838. if (Date.now() > endTime) {
  7839. continueCallback();
  7840. return i;
  7841. }
  7842. steps = 0;
  7843. }
  7844. }
  7845. }
  7846. #restoreInitialState() {
  7847. while (this.stateStack.length || this.inSMaskMode) {
  7848. this.restore();
  7849. }
  7850. this.current.activeSMask = null;
  7851. this.ctx.restore();
  7852. if (this.transparentCanvas) {
  7853. this.ctx = this.compositeCtx;
  7854. this.ctx.save();
  7855. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  7856. this.ctx.drawImage(this.transparentCanvas, 0, 0);
  7857. this.ctx.restore();
  7858. this.transparentCanvas = null;
  7859. }
  7860. }
  7861. endDrawing() {
  7862. this.#restoreInitialState();
  7863. this.cachedCanvases.clear();
  7864. this.cachedPatterns.clear();
  7865. for (const cache of this._cachedBitmapsMap.values()) {
  7866. for (const canvas of cache.values()) {
  7867. if (typeof HTMLCanvasElement !== "undefined" && canvas instanceof HTMLCanvasElement) {
  7868. canvas.width = canvas.height = 0;
  7869. }
  7870. }
  7871. cache.clear();
  7872. }
  7873. this._cachedBitmapsMap.clear();
  7874. this.#drawFilter();
  7875. }
  7876. #drawFilter() {
  7877. if (this.pageColors) {
  7878. const hcmFilterId = this.filterFactory.addHCMFilter(this.pageColors.foreground, this.pageColors.background);
  7879. if (hcmFilterId !== "none") {
  7880. const savedFilter = this.ctx.filter;
  7881. this.ctx.filter = hcmFilterId;
  7882. this.ctx.drawImage(this.ctx.canvas, 0, 0);
  7883. this.ctx.filter = savedFilter;
  7884. }
  7885. }
  7886. }
  7887. _scaleImage(img, inverseTransform) {
  7888. const width = img.width ?? img.displayWidth;
  7889. const height = img.height ?? img.displayHeight;
  7890. let widthScale = Math.max(Math.hypot(inverseTransform[0], inverseTransform[1]), 1);
  7891. let heightScale = Math.max(Math.hypot(inverseTransform[2], inverseTransform[3]), 1);
  7892. let paintWidth = width,
  7893. paintHeight = height;
  7894. let tmpCanvasId = "prescale1";
  7895. let tmpCanvas, tmpCtx;
  7896. while (widthScale > 2 && paintWidth > 1 || heightScale > 2 && paintHeight > 1) {
  7897. let newWidth = paintWidth,
  7898. newHeight = paintHeight;
  7899. if (widthScale > 2 && paintWidth > 1) {
  7900. newWidth = paintWidth >= 16384 ? Math.floor(paintWidth / 2) - 1 || 1 : Math.ceil(paintWidth / 2);
  7901. widthScale /= paintWidth / newWidth;
  7902. }
  7903. if (heightScale > 2 && paintHeight > 1) {
  7904. newHeight = paintHeight >= 16384 ? Math.floor(paintHeight / 2) - 1 || 1 : Math.ceil(paintHeight) / 2;
  7905. heightScale /= paintHeight / newHeight;
  7906. }
  7907. tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
  7908. tmpCtx = tmpCanvas.context;
  7909. tmpCtx.clearRect(0, 0, newWidth, newHeight);
  7910. tmpCtx.drawImage(img, 0, 0, paintWidth, paintHeight, 0, 0, newWidth, newHeight);
  7911. img = tmpCanvas.canvas;
  7912. paintWidth = newWidth;
  7913. paintHeight = newHeight;
  7914. tmpCanvasId = tmpCanvasId === "prescale1" ? "prescale2" : "prescale1";
  7915. }
  7916. return {
  7917. img,
  7918. paintWidth,
  7919. paintHeight
  7920. };
  7921. }
  7922. _createMaskCanvas(img) {
  7923. const ctx = this.ctx;
  7924. const {
  7925. width,
  7926. height
  7927. } = img;
  7928. const fillColor = this.current.fillColor;
  7929. const isPatternFill = this.current.patternFill;
  7930. const currentTransform = getCurrentTransform(ctx);
  7931. let cache, cacheKey, scaled, maskCanvas;
  7932. if ((img.bitmap || img.data) && img.count > 1) {
  7933. const mainKey = img.bitmap || img.data.buffer;
  7934. cacheKey = JSON.stringify(isPatternFill ? currentTransform : [currentTransform.slice(0, 4), fillColor]);
  7935. cache = this._cachedBitmapsMap.get(mainKey);
  7936. if (!cache) {
  7937. cache = new Map();
  7938. this._cachedBitmapsMap.set(mainKey, cache);
  7939. }
  7940. const cachedImage = cache.get(cacheKey);
  7941. if (cachedImage && !isPatternFill) {
  7942. const offsetX = Math.round(Math.min(currentTransform[0], currentTransform[2]) + currentTransform[4]);
  7943. const offsetY = Math.round(Math.min(currentTransform[1], currentTransform[3]) + currentTransform[5]);
  7944. return {
  7945. canvas: cachedImage,
  7946. offsetX,
  7947. offsetY
  7948. };
  7949. }
  7950. scaled = cachedImage;
  7951. }
  7952. if (!scaled) {
  7953. maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
  7954. putBinaryImageMask(maskCanvas.context, img);
  7955. }
  7956. let maskToCanvas = Util.transform(currentTransform, [1 / width, 0, 0, -1 / height, 0, 0]);
  7957. maskToCanvas = Util.transform(maskToCanvas, [1, 0, 0, 1, 0, -height]);
  7958. const [minX, minY, maxX, maxY] = Util.getAxialAlignedBoundingBox([0, 0, width, height], maskToCanvas);
  7959. const drawnWidth = Math.round(maxX - minX) || 1;
  7960. const drawnHeight = Math.round(maxY - minY) || 1;
  7961. const fillCanvas = this.cachedCanvases.getCanvas("fillCanvas", drawnWidth, drawnHeight);
  7962. const fillCtx = fillCanvas.context;
  7963. const offsetX = minX;
  7964. const offsetY = minY;
  7965. fillCtx.translate(-offsetX, -offsetY);
  7966. fillCtx.transform(...maskToCanvas);
  7967. if (!scaled) {
  7968. scaled = this._scaleImage(maskCanvas.canvas, getCurrentTransformInverse(fillCtx));
  7969. scaled = scaled.img;
  7970. if (cache && isPatternFill) {
  7971. cache.set(cacheKey, scaled);
  7972. }
  7973. }
  7974. fillCtx.imageSmoothingEnabled = getImageSmoothingEnabled(getCurrentTransform(fillCtx), img.interpolate);
  7975. drawImageAtIntegerCoords(fillCtx, scaled, 0, 0, scaled.width, scaled.height, 0, 0, width, height);
  7976. fillCtx.globalCompositeOperation = "source-in";
  7977. const inverse = Util.transform(getCurrentTransformInverse(fillCtx), [1, 0, 0, 1, -offsetX, -offsetY]);
  7978. fillCtx.fillStyle = isPatternFill ? fillColor.getPattern(ctx, this, inverse, PathType.FILL) : fillColor;
  7979. fillCtx.fillRect(0, 0, width, height);
  7980. if (cache && !isPatternFill) {
  7981. this.cachedCanvases.delete("fillCanvas");
  7982. cache.set(cacheKey, fillCanvas.canvas);
  7983. }
  7984. return {
  7985. canvas: fillCanvas.canvas,
  7986. offsetX: Math.round(offsetX),
  7987. offsetY: Math.round(offsetY)
  7988. };
  7989. }
  7990. setLineWidth(width) {
  7991. if (width !== this.current.lineWidth) {
  7992. this._cachedScaleForStroking[0] = -1;
  7993. }
  7994. this.current.lineWidth = width;
  7995. this.ctx.lineWidth = width;
  7996. }
  7997. setLineCap(style) {
  7998. this.ctx.lineCap = LINE_CAP_STYLES[style];
  7999. }
  8000. setLineJoin(style) {
  8001. this.ctx.lineJoin = LINE_JOIN_STYLES[style];
  8002. }
  8003. setMiterLimit(limit) {
  8004. this.ctx.miterLimit = limit;
  8005. }
  8006. setDash(dashArray, dashPhase) {
  8007. const ctx = this.ctx;
  8008. if (ctx.setLineDash !== undefined) {
  8009. ctx.setLineDash(dashArray);
  8010. ctx.lineDashOffset = dashPhase;
  8011. }
  8012. }
  8013. setRenderingIntent(intent) {}
  8014. setFlatness(flatness) {}
  8015. setGState(states) {
  8016. for (const [key, value] of states) {
  8017. switch (key) {
  8018. case "LW":
  8019. this.setLineWidth(value);
  8020. break;
  8021. case "LC":
  8022. this.setLineCap(value);
  8023. break;
  8024. case "LJ":
  8025. this.setLineJoin(value);
  8026. break;
  8027. case "ML":
  8028. this.setMiterLimit(value);
  8029. break;
  8030. case "D":
  8031. this.setDash(value[0], value[1]);
  8032. break;
  8033. case "RI":
  8034. this.setRenderingIntent(value);
  8035. break;
  8036. case "FL":
  8037. this.setFlatness(value);
  8038. break;
  8039. case "Font":
  8040. this.setFont(value[0], value[1]);
  8041. break;
  8042. case "CA":
  8043. this.current.strokeAlpha = value;
  8044. break;
  8045. case "ca":
  8046. this.ctx.globalAlpha = this.current.fillAlpha = value;
  8047. break;
  8048. case "BM":
  8049. this.ctx.globalCompositeOperation = value;
  8050. break;
  8051. case "SMask":
  8052. this.current.activeSMask = value ? this.tempSMask : null;
  8053. this.tempSMask = null;
  8054. this.checkSMaskState();
  8055. break;
  8056. case "TR":
  8057. this.ctx.filter = this.current.transferMaps = this.filterFactory.addFilter(value);
  8058. break;
  8059. }
  8060. }
  8061. }
  8062. get inSMaskMode() {
  8063. return !!this.suspendedCtx;
  8064. }
  8065. checkSMaskState() {
  8066. const inSMaskMode = this.inSMaskMode;
  8067. if (this.current.activeSMask && !inSMaskMode) {
  8068. this.beginSMaskMode();
  8069. } else if (!this.current.activeSMask && inSMaskMode) {
  8070. this.endSMaskMode();
  8071. }
  8072. }
  8073. beginSMaskMode() {
  8074. if (this.inSMaskMode) {
  8075. throw new Error("beginSMaskMode called while already in smask mode");
  8076. }
  8077. const drawnWidth = this.ctx.canvas.width;
  8078. const drawnHeight = this.ctx.canvas.height;
  8079. const cacheId = "smaskGroupAt" + this.groupLevel;
  8080. const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight);
  8081. this.suspendedCtx = this.ctx;
  8082. const ctx = this.ctx = scratchCanvas.context;
  8083. ctx.setTransform(this.suspendedCtx.getTransform());
  8084. copyCtxState(this.suspendedCtx, ctx);
  8085. mirrorContextOperations(ctx, this.suspendedCtx);
  8086. this.setGState([["BM", "source-over"]]);
  8087. }
  8088. endSMaskMode() {
  8089. if (!this.inSMaskMode) {
  8090. throw new Error("endSMaskMode called while not in smask mode");
  8091. }
  8092. this.ctx._removeMirroring();
  8093. copyCtxState(this.ctx, this.suspendedCtx);
  8094. this.ctx = this.suspendedCtx;
  8095. this.suspendedCtx = null;
  8096. }
  8097. compose(dirtyBox) {
  8098. if (!this.current.activeSMask) {
  8099. return;
  8100. }
  8101. if (!dirtyBox) {
  8102. dirtyBox = [0, 0, this.ctx.canvas.width, this.ctx.canvas.height];
  8103. } else {
  8104. dirtyBox[0] = Math.floor(dirtyBox[0]);
  8105. dirtyBox[1] = Math.floor(dirtyBox[1]);
  8106. dirtyBox[2] = Math.ceil(dirtyBox[2]);
  8107. dirtyBox[3] = Math.ceil(dirtyBox[3]);
  8108. }
  8109. const smask = this.current.activeSMask;
  8110. const suspendedCtx = this.suspendedCtx;
  8111. this.composeSMask(suspendedCtx, smask, this.ctx, dirtyBox);
  8112. this.ctx.save();
  8113. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  8114. this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  8115. this.ctx.restore();
  8116. }
  8117. composeSMask(ctx, smask, layerCtx, layerBox) {
  8118. const layerOffsetX = layerBox[0];
  8119. const layerOffsetY = layerBox[1];
  8120. const layerWidth = layerBox[2] - layerOffsetX;
  8121. const layerHeight = layerBox[3] - layerOffsetY;
  8122. if (layerWidth === 0 || layerHeight === 0) {
  8123. return;
  8124. }
  8125. this.genericComposeSMask(smask.context, layerCtx, layerWidth, layerHeight, smask.subtype, smask.backdrop, smask.transferMap, layerOffsetX, layerOffsetY, smask.offsetX, smask.offsetY);
  8126. ctx.save();
  8127. ctx.globalAlpha = 1;
  8128. ctx.globalCompositeOperation = "source-over";
  8129. ctx.setTransform(1, 0, 0, 1, 0, 0);
  8130. ctx.drawImage(layerCtx.canvas, 0, 0);
  8131. ctx.restore();
  8132. }
  8133. genericComposeSMask(maskCtx, layerCtx, width, height, subtype, backdrop, transferMap, layerOffsetX, layerOffsetY, maskOffsetX, maskOffsetY) {
  8134. let maskCanvas = maskCtx.canvas;
  8135. let maskX = layerOffsetX - maskOffsetX;
  8136. let maskY = layerOffsetY - maskOffsetY;
  8137. if (backdrop) {
  8138. const backdropRGB = Util.makeHexColor(...backdrop);
  8139. if (maskX < 0 || maskY < 0 || maskX + width > maskCanvas.width || maskY + height > maskCanvas.height) {
  8140. const canvas = this.cachedCanvases.getCanvas("maskExtension", width, height);
  8141. const ctx = canvas.context;
  8142. ctx.drawImage(maskCanvas, -maskX, -maskY);
  8143. ctx.globalCompositeOperation = "destination-atop";
  8144. ctx.fillStyle = backdropRGB;
  8145. ctx.fillRect(0, 0, width, height);
  8146. ctx.globalCompositeOperation = "source-over";
  8147. maskCanvas = canvas.canvas;
  8148. maskX = maskY = 0;
  8149. } else {
  8150. maskCtx.save();
  8151. maskCtx.globalAlpha = 1;
  8152. maskCtx.setTransform(1, 0, 0, 1, 0, 0);
  8153. const clip = new Path2D();
  8154. clip.rect(maskX, maskY, width, height);
  8155. maskCtx.clip(clip);
  8156. maskCtx.globalCompositeOperation = "destination-atop";
  8157. maskCtx.fillStyle = backdropRGB;
  8158. maskCtx.fillRect(maskX, maskY, width, height);
  8159. maskCtx.restore();
  8160. }
  8161. }
  8162. layerCtx.save();
  8163. layerCtx.globalAlpha = 1;
  8164. layerCtx.setTransform(1, 0, 0, 1, 0, 0);
  8165. if (subtype === "Alpha" && transferMap) {
  8166. layerCtx.filter = this.filterFactory.addAlphaFilter(transferMap);
  8167. } else if (subtype === "Luminosity") {
  8168. layerCtx.filter = this.filterFactory.addLuminosityFilter(transferMap);
  8169. }
  8170. const clip = new Path2D();
  8171. clip.rect(layerOffsetX, layerOffsetY, width, height);
  8172. layerCtx.clip(clip);
  8173. layerCtx.globalCompositeOperation = "destination-in";
  8174. layerCtx.drawImage(maskCanvas, maskX, maskY, width, height, layerOffsetX, layerOffsetY, width, height);
  8175. layerCtx.restore();
  8176. }
  8177. save() {
  8178. if (this.inSMaskMode) {
  8179. copyCtxState(this.ctx, this.suspendedCtx);
  8180. }
  8181. this.ctx.save();
  8182. const old = this.current;
  8183. this.stateStack.push(old);
  8184. this.current = old.clone();
  8185. }
  8186. restore() {
  8187. if (this.stateStack.length === 0) {
  8188. if (this.inSMaskMode) {
  8189. this.endSMaskMode();
  8190. }
  8191. return;
  8192. }
  8193. this.current = this.stateStack.pop();
  8194. this.ctx.restore();
  8195. if (this.inSMaskMode) {
  8196. copyCtxState(this.suspendedCtx, this.ctx);
  8197. }
  8198. this.checkSMaskState();
  8199. this.pendingClip = null;
  8200. this._cachedScaleForStroking[0] = -1;
  8201. this._cachedGetSinglePixelWidth = null;
  8202. }
  8203. transform(a, b, c, d, e, f) {
  8204. this.ctx.transform(a, b, c, d, e, f);
  8205. this._cachedScaleForStroking[0] = -1;
  8206. this._cachedGetSinglePixelWidth = null;
  8207. }
  8208. constructPath(op, data, minMax) {
  8209. let [path] = data;
  8210. if (!minMax) {
  8211. path ||= data[0] = new Path2D();
  8212. this[op](path);
  8213. return;
  8214. }
  8215. if (!(path instanceof Path2D)) {
  8216. const path2d = data[0] = new Path2D();
  8217. for (let i = 0, ii = path.length; i < ii;) {
  8218. switch (path[i++]) {
  8219. case DrawOPS.moveTo:
  8220. path2d.moveTo(path[i++], path[i++]);
  8221. break;
  8222. case DrawOPS.lineTo:
  8223. path2d.lineTo(path[i++], path[i++]);
  8224. break;
  8225. case DrawOPS.curveTo:
  8226. path2d.bezierCurveTo(path[i++], path[i++], path[i++], path[i++], path[i++], path[i++]);
  8227. break;
  8228. case DrawOPS.closePath:
  8229. path2d.closePath();
  8230. break;
  8231. default:
  8232. warn(`Unrecognized drawing path operator: ${path[i - 1]}`);
  8233. break;
  8234. }
  8235. }
  8236. path = path2d;
  8237. }
  8238. this.current.updateRectMinMax(getCurrentTransform(this.ctx), minMax);
  8239. this[op](path);
  8240. }
  8241. closePath() {
  8242. this.ctx.closePath();
  8243. }
  8244. stroke(path, consumePath = true) {
  8245. const ctx = this.ctx;
  8246. const strokeColor = this.current.strokeColor;
  8247. ctx.globalAlpha = this.current.strokeAlpha;
  8248. if (this.contentVisible) {
  8249. if (typeof strokeColor === "object" && strokeColor?.getPattern) {
  8250. const baseTransform = strokeColor.isModifyingCurrentTransform() ? ctx.getTransform() : null;
  8251. ctx.save();
  8252. ctx.strokeStyle = strokeColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.STROKE);
  8253. if (baseTransform) {
  8254. const newPath = new Path2D();
  8255. newPath.addPath(path, ctx.getTransform().invertSelf().multiplySelf(baseTransform));
  8256. path = newPath;
  8257. }
  8258. this.rescaleAndStroke(path, false);
  8259. ctx.restore();
  8260. } else {
  8261. this.rescaleAndStroke(path, true);
  8262. }
  8263. }
  8264. if (consumePath) {
  8265. this.consumePath(path, this.current.getClippedPathBoundingBox(PathType.STROKE, getCurrentTransform(this.ctx)));
  8266. }
  8267. ctx.globalAlpha = this.current.fillAlpha;
  8268. }
  8269. closeStroke(path) {
  8270. this.stroke(path);
  8271. }
  8272. fill(path, consumePath = true) {
  8273. const ctx = this.ctx;
  8274. const fillColor = this.current.fillColor;
  8275. const isPatternFill = this.current.patternFill;
  8276. let needRestore = false;
  8277. if (isPatternFill) {
  8278. const baseTransform = fillColor.isModifyingCurrentTransform() ? ctx.getTransform() : null;
  8279. ctx.save();
  8280. ctx.fillStyle = fillColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.FILL);
  8281. if (baseTransform) {
  8282. const newPath = new Path2D();
  8283. newPath.addPath(path, ctx.getTransform().invertSelf().multiplySelf(baseTransform));
  8284. path = newPath;
  8285. }
  8286. needRestore = true;
  8287. }
  8288. const intersect = this.current.getClippedPathBoundingBox();
  8289. if (this.contentVisible && intersect !== null) {
  8290. if (this.pendingEOFill) {
  8291. ctx.fill(path, "evenodd");
  8292. this.pendingEOFill = false;
  8293. } else {
  8294. ctx.fill(path);
  8295. }
  8296. }
  8297. if (needRestore) {
  8298. ctx.restore();
  8299. }
  8300. if (consumePath) {
  8301. this.consumePath(path, intersect);
  8302. }
  8303. }
  8304. eoFill(path) {
  8305. this.pendingEOFill = true;
  8306. this.fill(path);
  8307. }
  8308. fillStroke(path) {
  8309. this.fill(path, false);
  8310. this.stroke(path, false);
  8311. this.consumePath(path);
  8312. }
  8313. eoFillStroke(path) {
  8314. this.pendingEOFill = true;
  8315. this.fillStroke(path);
  8316. }
  8317. closeFillStroke(path) {
  8318. this.fillStroke(path);
  8319. }
  8320. closeEOFillStroke(path) {
  8321. this.pendingEOFill = true;
  8322. this.fillStroke(path);
  8323. }
  8324. endPath(path) {
  8325. this.consumePath(path);
  8326. }
  8327. clip() {
  8328. this.pendingClip = NORMAL_CLIP;
  8329. }
  8330. eoClip() {
  8331. this.pendingClip = EO_CLIP;
  8332. }
  8333. beginText() {
  8334. this.current.textMatrix = IDENTITY_MATRIX;
  8335. this.current.textMatrixScale = 1;
  8336. this.current.x = this.current.lineX = 0;
  8337. this.current.y = this.current.lineY = 0;
  8338. }
  8339. endText() {
  8340. const paths = this.pendingTextPaths;
  8341. const ctx = this.ctx;
  8342. if (paths === undefined) {
  8343. ctx.beginPath();
  8344. return;
  8345. }
  8346. const newPath = new Path2D();
  8347. const invTransf = ctx.getTransform().invertSelf();
  8348. for (const {
  8349. transform,
  8350. x,
  8351. y,
  8352. fontSize,
  8353. path
  8354. } of paths) {
  8355. newPath.addPath(path, new DOMMatrix(transform).preMultiplySelf(invTransf).translate(x, y).scale(fontSize, -fontSize));
  8356. }
  8357. ctx.clip(newPath);
  8358. ctx.beginPath();
  8359. delete this.pendingTextPaths;
  8360. }
  8361. setCharSpacing(spacing) {
  8362. this.current.charSpacing = spacing;
  8363. }
  8364. setWordSpacing(spacing) {
  8365. this.current.wordSpacing = spacing;
  8366. }
  8367. setHScale(scale) {
  8368. this.current.textHScale = scale / 100;
  8369. }
  8370. setLeading(leading) {
  8371. this.current.leading = -leading;
  8372. }
  8373. setFont(fontRefName, size) {
  8374. const fontObj = this.commonObjs.get(fontRefName);
  8375. const current = this.current;
  8376. if (!fontObj) {
  8377. throw new Error(`Can't find font for ${fontRefName}`);
  8378. }
  8379. current.fontMatrix = fontObj.fontMatrix || FONT_IDENTITY_MATRIX;
  8380. if (current.fontMatrix[0] === 0 || current.fontMatrix[3] === 0) {
  8381. warn("Invalid font matrix for font " + fontRefName);
  8382. }
  8383. if (size < 0) {
  8384. size = -size;
  8385. current.fontDirection = -1;
  8386. } else {
  8387. current.fontDirection = 1;
  8388. }
  8389. this.current.font = fontObj;
  8390. this.current.fontSize = size;
  8391. if (fontObj.isType3Font) {
  8392. return;
  8393. }
  8394. const name = fontObj.loadedName || "sans-serif";
  8395. const typeface = fontObj.systemFontInfo?.css || `"${name}", ${fontObj.fallbackName}`;
  8396. let bold = "normal";
  8397. if (fontObj.black) {
  8398. bold = "900";
  8399. } else if (fontObj.bold) {
  8400. bold = "bold";
  8401. }
  8402. const italic = fontObj.italic ? "italic" : "normal";
  8403. let browserFontSize = size;
  8404. if (size < MIN_FONT_SIZE) {
  8405. browserFontSize = MIN_FONT_SIZE;
  8406. } else if (size > MAX_FONT_SIZE) {
  8407. browserFontSize = MAX_FONT_SIZE;
  8408. }
  8409. this.current.fontSizeScale = size / browserFontSize;
  8410. this.ctx.font = `${italic} ${bold} ${browserFontSize}px ${typeface}`;
  8411. }
  8412. setTextRenderingMode(mode) {
  8413. this.current.textRenderingMode = mode;
  8414. }
  8415. setTextRise(rise) {
  8416. this.current.textRise = rise;
  8417. }
  8418. moveText(x, y) {
  8419. this.current.x = this.current.lineX += x;
  8420. this.current.y = this.current.lineY += y;
  8421. }
  8422. setLeadingMoveText(x, y) {
  8423. this.setLeading(-y);
  8424. this.moveText(x, y);
  8425. }
  8426. setTextMatrix(a, b, c, d, e, f) {
  8427. this.current.textMatrix = [a, b, c, d, e, f];
  8428. this.current.textMatrixScale = Math.hypot(a, b);
  8429. this.current.x = this.current.lineX = 0;
  8430. this.current.y = this.current.lineY = 0;
  8431. }
  8432. nextLine() {
  8433. this.moveText(0, this.current.leading);
  8434. }
  8435. #getScaledPath(path, currentTransform, transform) {
  8436. const newPath = new Path2D();
  8437. newPath.addPath(path, new DOMMatrix(transform).invertSelf().multiplySelf(currentTransform));
  8438. return newPath;
  8439. }
  8440. paintChar(character, x, y, patternFillTransform, patternStrokeTransform) {
  8441. const ctx = this.ctx;
  8442. const current = this.current;
  8443. const font = current.font;
  8444. const textRenderingMode = current.textRenderingMode;
  8445. const fontSize = current.fontSize / current.fontSizeScale;
  8446. const fillStrokeMode = textRenderingMode & TextRenderingMode.FILL_STROKE_MASK;
  8447. const isAddToPathSet = !!(textRenderingMode & TextRenderingMode.ADD_TO_PATH_FLAG);
  8448. const patternFill = current.patternFill && !font.missingFile;
  8449. const patternStroke = current.patternStroke && !font.missingFile;
  8450. let path;
  8451. if (font.disableFontFace || isAddToPathSet || patternFill || patternStroke) {
  8452. path = font.getPathGenerator(this.commonObjs, character);
  8453. }
  8454. if (font.disableFontFace || patternFill || patternStroke) {
  8455. ctx.save();
  8456. ctx.translate(x, y);
  8457. ctx.scale(fontSize, -fontSize);
  8458. let currentTransform;
  8459. if (fillStrokeMode === TextRenderingMode.FILL || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8460. if (patternFillTransform) {
  8461. currentTransform = ctx.getTransform();
  8462. ctx.setTransform(...patternFillTransform);
  8463. ctx.fill(this.#getScaledPath(path, currentTransform, patternFillTransform));
  8464. } else {
  8465. ctx.fill(path);
  8466. }
  8467. }
  8468. if (fillStrokeMode === TextRenderingMode.STROKE || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8469. if (patternStrokeTransform) {
  8470. currentTransform ||= ctx.getTransform();
  8471. ctx.setTransform(...patternStrokeTransform);
  8472. const {
  8473. a,
  8474. b,
  8475. c,
  8476. d
  8477. } = currentTransform;
  8478. const invPatternTransform = Util.inverseTransform(patternStrokeTransform);
  8479. const transf = Util.transform([a, b, c, d, 0, 0], invPatternTransform);
  8480. const [sx, sy] = Util.singularValueDecompose2dScale(transf);
  8481. ctx.lineWidth *= Math.max(sx, sy) / fontSize;
  8482. ctx.stroke(this.#getScaledPath(path, currentTransform, patternStrokeTransform));
  8483. } else {
  8484. ctx.lineWidth /= fontSize;
  8485. ctx.stroke(path);
  8486. }
  8487. }
  8488. ctx.restore();
  8489. } else {
  8490. if (fillStrokeMode === TextRenderingMode.FILL || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8491. ctx.fillText(character, x, y);
  8492. }
  8493. if (fillStrokeMode === TextRenderingMode.STROKE || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8494. ctx.strokeText(character, x, y);
  8495. }
  8496. }
  8497. if (isAddToPathSet) {
  8498. const paths = this.pendingTextPaths ||= [];
  8499. paths.push({
  8500. transform: getCurrentTransform(ctx),
  8501. x,
  8502. y,
  8503. fontSize,
  8504. path
  8505. });
  8506. }
  8507. }
  8508. get isFontSubpixelAAEnabled() {
  8509. const {
  8510. context: ctx
  8511. } = this.cachedCanvases.getCanvas("isFontSubpixelAAEnabled", 10, 10);
  8512. ctx.scale(1.5, 1);
  8513. ctx.fillText("I", 0, 10);
  8514. const data = ctx.getImageData(0, 0, 10, 10).data;
  8515. let enabled = false;
  8516. for (let i = 3; i < data.length; i += 4) {
  8517. if (data[i] > 0 && data[i] < 255) {
  8518. enabled = true;
  8519. break;
  8520. }
  8521. }
  8522. return shadow(this, "isFontSubpixelAAEnabled", enabled);
  8523. }
  8524. showText(glyphs) {
  8525. const current = this.current;
  8526. const font = current.font;
  8527. if (font.isType3Font) {
  8528. return this.showType3Text(glyphs);
  8529. }
  8530. const fontSize = current.fontSize;
  8531. if (fontSize === 0) {
  8532. return undefined;
  8533. }
  8534. const ctx = this.ctx;
  8535. const fontSizeScale = current.fontSizeScale;
  8536. const charSpacing = current.charSpacing;
  8537. const wordSpacing = current.wordSpacing;
  8538. const fontDirection = current.fontDirection;
  8539. const textHScale = current.textHScale * fontDirection;
  8540. const glyphsLength = glyphs.length;
  8541. const vertical = font.vertical;
  8542. const spacingDir = vertical ? 1 : -1;
  8543. const defaultVMetrics = font.defaultVMetrics;
  8544. const widthAdvanceScale = fontSize * current.fontMatrix[0];
  8545. const simpleFillText = current.textRenderingMode === TextRenderingMode.FILL && !font.disableFontFace && !current.patternFill;
  8546. ctx.save();
  8547. ctx.transform(...current.textMatrix);
  8548. ctx.translate(current.x, current.y + current.textRise);
  8549. if (fontDirection > 0) {
  8550. ctx.scale(textHScale, -1);
  8551. } else {
  8552. ctx.scale(textHScale, 1);
  8553. }
  8554. let patternFillTransform, patternStrokeTransform;
  8555. if (current.patternFill) {
  8556. ctx.save();
  8557. const pattern = current.fillColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.FILL);
  8558. patternFillTransform = getCurrentTransform(ctx);
  8559. ctx.restore();
  8560. ctx.fillStyle = pattern;
  8561. }
  8562. if (current.patternStroke) {
  8563. ctx.save();
  8564. const pattern = current.strokeColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.STROKE);
  8565. patternStrokeTransform = getCurrentTransform(ctx);
  8566. ctx.restore();
  8567. ctx.strokeStyle = pattern;
  8568. }
  8569. let lineWidth = current.lineWidth;
  8570. const scale = current.textMatrixScale;
  8571. if (scale === 0 || lineWidth === 0) {
  8572. const fillStrokeMode = current.textRenderingMode & TextRenderingMode.FILL_STROKE_MASK;
  8573. if (fillStrokeMode === TextRenderingMode.STROKE || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8574. lineWidth = this.getSinglePixelWidth();
  8575. }
  8576. } else {
  8577. lineWidth /= scale;
  8578. }
  8579. if (fontSizeScale !== 1.0) {
  8580. ctx.scale(fontSizeScale, fontSizeScale);
  8581. lineWidth /= fontSizeScale;
  8582. }
  8583. ctx.lineWidth = lineWidth;
  8584. if (font.isInvalidPDFjsFont) {
  8585. const chars = [];
  8586. let width = 0;
  8587. for (const glyph of glyphs) {
  8588. chars.push(glyph.unicode);
  8589. width += glyph.width;
  8590. }
  8591. ctx.fillText(chars.join(""), 0, 0);
  8592. current.x += width * widthAdvanceScale * textHScale;
  8593. ctx.restore();
  8594. this.compose();
  8595. return undefined;
  8596. }
  8597. let x = 0,
  8598. i;
  8599. for (i = 0; i < glyphsLength; ++i) {
  8600. const glyph = glyphs[i];
  8601. if (typeof glyph === "number") {
  8602. x += spacingDir * glyph * fontSize / 1000;
  8603. continue;
  8604. }
  8605. let restoreNeeded = false;
  8606. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  8607. const character = glyph.fontChar;
  8608. const accent = glyph.accent;
  8609. let scaledX, scaledY;
  8610. let width = glyph.width;
  8611. if (vertical) {
  8612. const vmetric = glyph.vmetric || defaultVMetrics;
  8613. const vx = -(glyph.vmetric ? vmetric[1] : width * 0.5) * widthAdvanceScale;
  8614. const vy = vmetric[2] * widthAdvanceScale;
  8615. width = vmetric ? -vmetric[0] : width;
  8616. scaledX = vx / fontSizeScale;
  8617. scaledY = (x + vy) / fontSizeScale;
  8618. } else {
  8619. scaledX = x / fontSizeScale;
  8620. scaledY = 0;
  8621. }
  8622. if (font.remeasure && width > 0) {
  8623. const measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
  8624. if (width < measuredWidth && this.isFontSubpixelAAEnabled) {
  8625. const characterScaleX = width / measuredWidth;
  8626. restoreNeeded = true;
  8627. ctx.save();
  8628. ctx.scale(characterScaleX, 1);
  8629. scaledX /= characterScaleX;
  8630. } else if (width !== measuredWidth) {
  8631. scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
  8632. }
  8633. }
  8634. if (this.contentVisible && (glyph.isInFont || font.missingFile)) {
  8635. if (simpleFillText && !accent) {
  8636. ctx.fillText(character, scaledX, scaledY);
  8637. } else {
  8638. this.paintChar(character, scaledX, scaledY, patternFillTransform, patternStrokeTransform);
  8639. if (accent) {
  8640. const scaledAccentX = scaledX + fontSize * accent.offset.x / fontSizeScale;
  8641. const scaledAccentY = scaledY - fontSize * accent.offset.y / fontSizeScale;
  8642. this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY, patternFillTransform, patternStrokeTransform);
  8643. }
  8644. }
  8645. }
  8646. const charWidth = vertical ? width * widthAdvanceScale - spacing * fontDirection : width * widthAdvanceScale + spacing * fontDirection;
  8647. x += charWidth;
  8648. if (restoreNeeded) {
  8649. ctx.restore();
  8650. }
  8651. }
  8652. if (vertical) {
  8653. current.y -= x;
  8654. } else {
  8655. current.x += x * textHScale;
  8656. }
  8657. ctx.restore();
  8658. this.compose();
  8659. return undefined;
  8660. }
  8661. showType3Text(glyphs) {
  8662. const ctx = this.ctx;
  8663. const current = this.current;
  8664. const font = current.font;
  8665. const fontSize = current.fontSize;
  8666. const fontDirection = current.fontDirection;
  8667. const spacingDir = font.vertical ? 1 : -1;
  8668. const charSpacing = current.charSpacing;
  8669. const wordSpacing = current.wordSpacing;
  8670. const textHScale = current.textHScale * fontDirection;
  8671. const fontMatrix = current.fontMatrix || FONT_IDENTITY_MATRIX;
  8672. const glyphsLength = glyphs.length;
  8673. const isTextInvisible = current.textRenderingMode === TextRenderingMode.INVISIBLE;
  8674. let i, glyph, width, spacingLength;
  8675. if (isTextInvisible || fontSize === 0) {
  8676. return;
  8677. }
  8678. this._cachedScaleForStroking[0] = -1;
  8679. this._cachedGetSinglePixelWidth = null;
  8680. ctx.save();
  8681. ctx.transform(...current.textMatrix);
  8682. ctx.translate(current.x, current.y + current.textRise);
  8683. ctx.scale(textHScale, fontDirection);
  8684. for (i = 0; i < glyphsLength; ++i) {
  8685. glyph = glyphs[i];
  8686. if (typeof glyph === "number") {
  8687. spacingLength = spacingDir * glyph * fontSize / 1000;
  8688. this.ctx.translate(spacingLength, 0);
  8689. current.x += spacingLength * textHScale;
  8690. continue;
  8691. }
  8692. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  8693. const operatorList = font.charProcOperatorList[glyph.operatorListId];
  8694. if (!operatorList) {
  8695. warn(`Type3 character "${glyph.operatorListId}" is not available.`);
  8696. } else if (this.contentVisible) {
  8697. this.processingType3 = glyph;
  8698. this.save();
  8699. ctx.scale(fontSize, fontSize);
  8700. ctx.transform(...fontMatrix);
  8701. this.executeOperatorList(operatorList);
  8702. this.restore();
  8703. }
  8704. const transformed = Util.applyTransform([glyph.width, 0], fontMatrix);
  8705. width = transformed[0] * fontSize + spacing;
  8706. ctx.translate(width, 0);
  8707. current.x += width * textHScale;
  8708. }
  8709. ctx.restore();
  8710. this.processingType3 = null;
  8711. }
  8712. setCharWidth(xWidth, yWidth) {}
  8713. setCharWidthAndBounds(xWidth, yWidth, llx, lly, urx, ury) {
  8714. this.ctx.rect(llx, lly, urx - llx, ury - lly);
  8715. this.ctx.clip();
  8716. this.endPath();
  8717. }
  8718. getColorN_Pattern(IR) {
  8719. let pattern;
  8720. if (IR[0] === "TilingPattern") {
  8721. const baseTransform = this.baseTransform || getCurrentTransform(this.ctx);
  8722. const canvasGraphicsFactory = {
  8723. createCanvasGraphics: ctx => new CanvasGraphics(ctx, this.commonObjs, this.objs, this.canvasFactory, this.filterFactory, {
  8724. optionalContentConfig: this.optionalContentConfig,
  8725. markedContentStack: this.markedContentStack
  8726. })
  8727. };
  8728. pattern = new TilingPattern(IR, this.ctx, canvasGraphicsFactory, baseTransform);
  8729. } else {
  8730. pattern = this._getPattern(IR[1], IR[2]);
  8731. }
  8732. return pattern;
  8733. }
  8734. setStrokeColorN() {
  8735. this.current.strokeColor = this.getColorN_Pattern(arguments);
  8736. this.current.patternStroke = true;
  8737. }
  8738. setFillColorN() {
  8739. this.current.fillColor = this.getColorN_Pattern(arguments);
  8740. this.current.patternFill = true;
  8741. }
  8742. setStrokeRGBColor(r, g, b) {
  8743. this.ctx.strokeStyle = this.current.strokeColor = Util.makeHexColor(r, g, b);
  8744. this.current.patternStroke = false;
  8745. }
  8746. setStrokeTransparent() {
  8747. this.ctx.strokeStyle = this.current.strokeColor = "transparent";
  8748. this.current.patternStroke = false;
  8749. }
  8750. setFillRGBColor(r, g, b) {
  8751. this.ctx.fillStyle = this.current.fillColor = Util.makeHexColor(r, g, b);
  8752. this.current.patternFill = false;
  8753. }
  8754. setFillTransparent() {
  8755. this.ctx.fillStyle = this.current.fillColor = "transparent";
  8756. this.current.patternFill = false;
  8757. }
  8758. _getPattern(objId, matrix = null) {
  8759. let pattern;
  8760. if (this.cachedPatterns.has(objId)) {
  8761. pattern = this.cachedPatterns.get(objId);
  8762. } else {
  8763. pattern = getShadingPattern(this.getObject(objId));
  8764. this.cachedPatterns.set(objId, pattern);
  8765. }
  8766. if (matrix) {
  8767. pattern.matrix = matrix;
  8768. }
  8769. return pattern;
  8770. }
  8771. shadingFill(objId) {
  8772. if (!this.contentVisible) {
  8773. return;
  8774. }
  8775. const ctx = this.ctx;
  8776. this.save();
  8777. const pattern = this._getPattern(objId);
  8778. ctx.fillStyle = pattern.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.SHADING);
  8779. const inv = getCurrentTransformInverse(ctx);
  8780. if (inv) {
  8781. const {
  8782. width,
  8783. height
  8784. } = ctx.canvas;
  8785. const [x0, y0, x1, y1] = Util.getAxialAlignedBoundingBox([0, 0, width, height], inv);
  8786. this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
  8787. } else {
  8788. this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
  8789. }
  8790. this.compose(this.current.getClippedPathBoundingBox());
  8791. this.restore();
  8792. }
  8793. beginInlineImage() {
  8794. unreachable("Should not call beginInlineImage");
  8795. }
  8796. beginImageData() {
  8797. unreachable("Should not call beginImageData");
  8798. }
  8799. paintFormXObjectBegin(matrix, bbox) {
  8800. if (!this.contentVisible) {
  8801. return;
  8802. }
  8803. this.save();
  8804. this.baseTransformStack.push(this.baseTransform);
  8805. if (matrix) {
  8806. this.transform(...matrix);
  8807. }
  8808. this.baseTransform = getCurrentTransform(this.ctx);
  8809. if (bbox) {
  8810. const width = bbox[2] - bbox[0];
  8811. const height = bbox[3] - bbox[1];
  8812. this.ctx.rect(bbox[0], bbox[1], width, height);
  8813. this.current.updateRectMinMax(getCurrentTransform(this.ctx), bbox);
  8814. this.clip();
  8815. this.endPath();
  8816. }
  8817. }
  8818. paintFormXObjectEnd() {
  8819. if (!this.contentVisible) {
  8820. return;
  8821. }
  8822. this.restore();
  8823. this.baseTransform = this.baseTransformStack.pop();
  8824. }
  8825. beginGroup(group) {
  8826. if (!this.contentVisible) {
  8827. return;
  8828. }
  8829. this.save();
  8830. if (this.inSMaskMode) {
  8831. this.endSMaskMode();
  8832. this.current.activeSMask = null;
  8833. }
  8834. const currentCtx = this.ctx;
  8835. if (!group.isolated) {
  8836. info("TODO: Support non-isolated groups.");
  8837. }
  8838. if (group.knockout) {
  8839. warn("Knockout groups not supported.");
  8840. }
  8841. const currentTransform = getCurrentTransform(currentCtx);
  8842. if (group.matrix) {
  8843. currentCtx.transform(...group.matrix);
  8844. }
  8845. if (!group.bbox) {
  8846. throw new Error("Bounding box is required.");
  8847. }
  8848. let bounds = Util.getAxialAlignedBoundingBox(group.bbox, getCurrentTransform(currentCtx));
  8849. const canvasBounds = [0, 0, currentCtx.canvas.width, currentCtx.canvas.height];
  8850. bounds = Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
  8851. const offsetX = Math.floor(bounds[0]);
  8852. const offsetY = Math.floor(bounds[1]);
  8853. const drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
  8854. const drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
  8855. this.current.startNewPathAndClipBox([0, 0, drawnWidth, drawnHeight]);
  8856. let cacheId = "groupAt" + this.groupLevel;
  8857. if (group.smask) {
  8858. cacheId += "_smask_" + this.smaskCounter++ % 2;
  8859. }
  8860. const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight);
  8861. const groupCtx = scratchCanvas.context;
  8862. groupCtx.translate(-offsetX, -offsetY);
  8863. groupCtx.transform(...currentTransform);
  8864. let clip = new Path2D();
  8865. const [x0, y0, x1, y1] = group.bbox;
  8866. clip.rect(x0, y0, x1 - x0, y1 - y0);
  8867. if (group.matrix) {
  8868. const path = new Path2D();
  8869. path.addPath(clip, new DOMMatrix(group.matrix));
  8870. clip = path;
  8871. }
  8872. groupCtx.clip(clip);
  8873. if (group.smask) {
  8874. this.smaskStack.push({
  8875. canvas: scratchCanvas.canvas,
  8876. context: groupCtx,
  8877. offsetX,
  8878. offsetY,
  8879. subtype: group.smask.subtype,
  8880. backdrop: group.smask.backdrop,
  8881. transferMap: group.smask.transferMap || null,
  8882. startTransformInverse: null
  8883. });
  8884. } else {
  8885. currentCtx.setTransform(1, 0, 0, 1, 0, 0);
  8886. currentCtx.translate(offsetX, offsetY);
  8887. currentCtx.save();
  8888. }
  8889. copyCtxState(currentCtx, groupCtx);
  8890. this.ctx = groupCtx;
  8891. this.setGState([["BM", "source-over"], ["ca", 1], ["CA", 1]]);
  8892. this.groupStack.push(currentCtx);
  8893. this.groupLevel++;
  8894. }
  8895. endGroup(group) {
  8896. if (!this.contentVisible) {
  8897. return;
  8898. }
  8899. this.groupLevel--;
  8900. const groupCtx = this.ctx;
  8901. const ctx = this.groupStack.pop();
  8902. this.ctx = ctx;
  8903. this.ctx.imageSmoothingEnabled = false;
  8904. if (group.smask) {
  8905. this.tempSMask = this.smaskStack.pop();
  8906. this.restore();
  8907. } else {
  8908. this.ctx.restore();
  8909. const currentMtx = getCurrentTransform(this.ctx);
  8910. this.restore();
  8911. this.ctx.save();
  8912. this.ctx.setTransform(...currentMtx);
  8913. const dirtyBox = Util.getAxialAlignedBoundingBox([0, 0, groupCtx.canvas.width, groupCtx.canvas.height], currentMtx);
  8914. this.ctx.drawImage(groupCtx.canvas, 0, 0);
  8915. this.ctx.restore();
  8916. this.compose(dirtyBox);
  8917. }
  8918. }
  8919. beginAnnotation(id, rect, transform, matrix, hasOwnCanvas) {
  8920. this.#restoreInitialState();
  8921. resetCtxToDefault(this.ctx);
  8922. this.ctx.save();
  8923. this.save();
  8924. if (this.baseTransform) {
  8925. this.ctx.setTransform(...this.baseTransform);
  8926. }
  8927. if (rect) {
  8928. const width = rect[2] - rect[0];
  8929. const height = rect[3] - rect[1];
  8930. if (hasOwnCanvas && this.annotationCanvasMap) {
  8931. transform = transform.slice();
  8932. transform[4] -= rect[0];
  8933. transform[5] -= rect[1];
  8934. rect = rect.slice();
  8935. rect[0] = rect[1] = 0;
  8936. rect[2] = width;
  8937. rect[3] = height;
  8938. const [scaleX, scaleY] = Util.singularValueDecompose2dScale(getCurrentTransform(this.ctx));
  8939. const {
  8940. viewportScale
  8941. } = this;
  8942. const canvasWidth = Math.ceil(width * this.outputScaleX * viewportScale);
  8943. const canvasHeight = Math.ceil(height * this.outputScaleY * viewportScale);
  8944. this.annotationCanvas = this.canvasFactory.create(canvasWidth, canvasHeight);
  8945. const {
  8946. canvas,
  8947. context
  8948. } = this.annotationCanvas;
  8949. this.annotationCanvasMap.set(id, canvas);
  8950. this.annotationCanvas.savedCtx = this.ctx;
  8951. this.ctx = context;
  8952. this.ctx.save();
  8953. this.ctx.setTransform(scaleX, 0, 0, -scaleY, 0, height * scaleY);
  8954. resetCtxToDefault(this.ctx);
  8955. } else {
  8956. resetCtxToDefault(this.ctx);
  8957. this.endPath();
  8958. this.ctx.rect(rect[0], rect[1], width, height);
  8959. this.ctx.clip();
  8960. this.ctx.beginPath();
  8961. }
  8962. }
  8963. this.current = new CanvasExtraState(this.ctx.canvas.width, this.ctx.canvas.height);
  8964. this.transform(...transform);
  8965. this.transform(...matrix);
  8966. }
  8967. endAnnotation() {
  8968. if (this.annotationCanvas) {
  8969. this.ctx.restore();
  8970. this.#drawFilter();
  8971. this.ctx = this.annotationCanvas.savedCtx;
  8972. delete this.annotationCanvas.savedCtx;
  8973. delete this.annotationCanvas;
  8974. }
  8975. }
  8976. paintImageMaskXObject(img) {
  8977. if (!this.contentVisible) {
  8978. return;
  8979. }
  8980. const count = img.count;
  8981. img = this.getObject(img.data, img);
  8982. img.count = count;
  8983. const ctx = this.ctx;
  8984. const glyph = this.processingType3;
  8985. if (glyph) {
  8986. if (glyph.compiled === undefined) {
  8987. glyph.compiled = compileType3Glyph(img);
  8988. }
  8989. if (glyph.compiled) {
  8990. ctx.fill(glyph.compiled);
  8991. return;
  8992. }
  8993. }
  8994. const mask = this._createMaskCanvas(img);
  8995. const maskCanvas = mask.canvas;
  8996. ctx.save();
  8997. ctx.setTransform(1, 0, 0, 1, 0, 0);
  8998. ctx.drawImage(maskCanvas, mask.offsetX, mask.offsetY);
  8999. ctx.restore();
  9000. this.compose();
  9001. }
  9002. paintImageMaskXObjectRepeat(img, scaleX, skewX = 0, skewY = 0, scaleY, positions) {
  9003. if (!this.contentVisible) {
  9004. return;
  9005. }
  9006. img = this.getObject(img.data, img);
  9007. const ctx = this.ctx;
  9008. ctx.save();
  9009. const currentTransform = getCurrentTransform(ctx);
  9010. ctx.transform(scaleX, skewX, skewY, scaleY, 0, 0);
  9011. const mask = this._createMaskCanvas(img);
  9012. ctx.setTransform(1, 0, 0, 1, mask.offsetX - currentTransform[4], mask.offsetY - currentTransform[5]);
  9013. for (let i = 0, ii = positions.length; i < ii; i += 2) {
  9014. const trans = Util.transform(currentTransform, [scaleX, skewX, skewY, scaleY, positions[i], positions[i + 1]]);
  9015. const [x, y] = Util.applyTransform([0, 0], trans);
  9016. ctx.drawImage(mask.canvas, x, y);
  9017. }
  9018. ctx.restore();
  9019. this.compose();
  9020. }
  9021. paintImageMaskXObjectGroup(images) {
  9022. if (!this.contentVisible) {
  9023. return;
  9024. }
  9025. const ctx = this.ctx;
  9026. const fillColor = this.current.fillColor;
  9027. const isPatternFill = this.current.patternFill;
  9028. for (const image of images) {
  9029. const {
  9030. data,
  9031. width,
  9032. height,
  9033. transform
  9034. } = image;
  9035. const maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
  9036. const maskCtx = maskCanvas.context;
  9037. maskCtx.save();
  9038. const img = this.getObject(data, image);
  9039. putBinaryImageMask(maskCtx, img);
  9040. maskCtx.globalCompositeOperation = "source-in";
  9041. maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this, getCurrentTransformInverse(ctx), PathType.FILL) : fillColor;
  9042. maskCtx.fillRect(0, 0, width, height);
  9043. maskCtx.restore();
  9044. ctx.save();
  9045. ctx.transform(...transform);
  9046. ctx.scale(1, -1);
  9047. drawImageAtIntegerCoords(ctx, maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
  9048. ctx.restore();
  9049. }
  9050. this.compose();
  9051. }
  9052. paintImageXObject(objId) {
  9053. if (!this.contentVisible) {
  9054. return;
  9055. }
  9056. const imgData = this.getObject(objId);
  9057. if (!imgData) {
  9058. warn("Dependent image isn't ready yet");
  9059. return;
  9060. }
  9061. this.paintInlineImageXObject(imgData);
  9062. }
  9063. paintImageXObjectRepeat(objId, scaleX, scaleY, positions) {
  9064. if (!this.contentVisible) {
  9065. return;
  9066. }
  9067. const imgData = this.getObject(objId);
  9068. if (!imgData) {
  9069. warn("Dependent image isn't ready yet");
  9070. return;
  9071. }
  9072. const width = imgData.width;
  9073. const height = imgData.height;
  9074. const map = [];
  9075. for (let i = 0, ii = positions.length; i < ii; i += 2) {
  9076. map.push({
  9077. transform: [scaleX, 0, 0, scaleY, positions[i], positions[i + 1]],
  9078. x: 0,
  9079. y: 0,
  9080. w: width,
  9081. h: height
  9082. });
  9083. }
  9084. this.paintInlineImageXObjectGroup(imgData, map);
  9085. }
  9086. applyTransferMapsToCanvas(ctx) {
  9087. if (this.current.transferMaps !== "none") {
  9088. ctx.filter = this.current.transferMaps;
  9089. ctx.drawImage(ctx.canvas, 0, 0);
  9090. ctx.filter = "none";
  9091. }
  9092. return ctx.canvas;
  9093. }
  9094. applyTransferMapsToBitmap(imgData) {
  9095. if (this.current.transferMaps === "none") {
  9096. return imgData.bitmap;
  9097. }
  9098. const {
  9099. bitmap,
  9100. width,
  9101. height
  9102. } = imgData;
  9103. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", width, height);
  9104. const tmpCtx = tmpCanvas.context;
  9105. tmpCtx.filter = this.current.transferMaps;
  9106. tmpCtx.drawImage(bitmap, 0, 0);
  9107. tmpCtx.filter = "none";
  9108. return tmpCanvas.canvas;
  9109. }
  9110. paintInlineImageXObject(imgData) {
  9111. if (!this.contentVisible) {
  9112. return;
  9113. }
  9114. const width = imgData.width;
  9115. const height = imgData.height;
  9116. const ctx = this.ctx;
  9117. this.save();
  9118. if (!isNodeJS) {
  9119. const {
  9120. filter
  9121. } = ctx;
  9122. if (filter !== "none" && filter !== "") {
  9123. ctx.filter = "none";
  9124. }
  9125. }
  9126. ctx.scale(1 / width, -1 / height);
  9127. let imgToPaint;
  9128. if (imgData.bitmap) {
  9129. imgToPaint = this.applyTransferMapsToBitmap(imgData);
  9130. } else if (typeof HTMLElement === "function" && imgData instanceof HTMLElement || !imgData.data) {
  9131. imgToPaint = imgData;
  9132. } else {
  9133. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", width, height);
  9134. const tmpCtx = tmpCanvas.context;
  9135. putBinaryImageData(tmpCtx, imgData);
  9136. imgToPaint = this.applyTransferMapsToCanvas(tmpCtx);
  9137. }
  9138. const scaled = this._scaleImage(imgToPaint, getCurrentTransformInverse(ctx));
  9139. ctx.imageSmoothingEnabled = getImageSmoothingEnabled(getCurrentTransform(ctx), imgData.interpolate);
  9140. drawImageAtIntegerCoords(ctx, scaled.img, 0, 0, scaled.paintWidth, scaled.paintHeight, 0, -height, width, height);
  9141. this.compose();
  9142. this.restore();
  9143. }
  9144. paintInlineImageXObjectGroup(imgData, map) {
  9145. if (!this.contentVisible) {
  9146. return;
  9147. }
  9148. const ctx = this.ctx;
  9149. let imgToPaint;
  9150. if (imgData.bitmap) {
  9151. imgToPaint = imgData.bitmap;
  9152. } else {
  9153. const w = imgData.width;
  9154. const h = imgData.height;
  9155. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", w, h);
  9156. const tmpCtx = tmpCanvas.context;
  9157. putBinaryImageData(tmpCtx, imgData);
  9158. imgToPaint = this.applyTransferMapsToCanvas(tmpCtx);
  9159. }
  9160. for (const entry of map) {
  9161. ctx.save();
  9162. ctx.transform(...entry.transform);
  9163. ctx.scale(1, -1);
  9164. drawImageAtIntegerCoords(ctx, imgToPaint, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1);
  9165. ctx.restore();
  9166. }
  9167. this.compose();
  9168. }
  9169. paintSolidColorImageMask() {
  9170. if (!this.contentVisible) {
  9171. return;
  9172. }
  9173. this.ctx.fillRect(0, 0, 1, 1);
  9174. this.compose();
  9175. }
  9176. markPoint(tag) {}
  9177. markPointProps(tag, properties) {}
  9178. beginMarkedContent(tag) {
  9179. this.markedContentStack.push({
  9180. visible: true
  9181. });
  9182. }
  9183. beginMarkedContentProps(tag, properties) {
  9184. if (tag === "OC") {
  9185. this.markedContentStack.push({
  9186. visible: this.optionalContentConfig.isVisible(properties)
  9187. });
  9188. } else {
  9189. this.markedContentStack.push({
  9190. visible: true
  9191. });
  9192. }
  9193. this.contentVisible = this.isContentVisible();
  9194. }
  9195. endMarkedContent() {
  9196. this.markedContentStack.pop();
  9197. this.contentVisible = this.isContentVisible();
  9198. }
  9199. beginCompat() {}
  9200. endCompat() {}
  9201. consumePath(path, clipBox) {
  9202. const isEmpty = this.current.isEmptyClip();
  9203. if (this.pendingClip) {
  9204. this.current.updateClipFromPath();
  9205. }
  9206. if (!this.pendingClip) {
  9207. this.compose(clipBox);
  9208. }
  9209. const ctx = this.ctx;
  9210. if (this.pendingClip) {
  9211. if (!isEmpty) {
  9212. if (this.pendingClip === EO_CLIP) {
  9213. ctx.clip(path, "evenodd");
  9214. } else {
  9215. ctx.clip(path);
  9216. }
  9217. }
  9218. this.pendingClip = null;
  9219. }
  9220. this.current.startNewPathAndClipBox(this.current.clipBox);
  9221. ctx.beginPath();
  9222. }
  9223. getSinglePixelWidth() {
  9224. if (!this._cachedGetSinglePixelWidth) {
  9225. const m = getCurrentTransform(this.ctx);
  9226. if (m[1] === 0 && m[2] === 0) {
  9227. this._cachedGetSinglePixelWidth = 1 / Math.min(Math.abs(m[0]), Math.abs(m[3]));
  9228. } else {
  9229. const absDet = Math.abs(m[0] * m[3] - m[2] * m[1]);
  9230. const normX = Math.hypot(m[0], m[2]);
  9231. const normY = Math.hypot(m[1], m[3]);
  9232. this._cachedGetSinglePixelWidth = Math.max(normX, normY) / absDet;
  9233. }
  9234. }
  9235. return this._cachedGetSinglePixelWidth;
  9236. }
  9237. getScaleForStroking() {
  9238. if (this._cachedScaleForStroking[0] === -1) {
  9239. const {
  9240. lineWidth
  9241. } = this.current;
  9242. const {
  9243. a,
  9244. b,
  9245. c,
  9246. d
  9247. } = this.ctx.getTransform();
  9248. let scaleX, scaleY;
  9249. if (b === 0 && c === 0) {
  9250. const normX = Math.abs(a);
  9251. const normY = Math.abs(d);
  9252. if (normX === normY) {
  9253. if (lineWidth === 0) {
  9254. scaleX = scaleY = 1 / normX;
  9255. } else {
  9256. const scaledLineWidth = normX * lineWidth;
  9257. scaleX = scaleY = scaledLineWidth < 1 ? 1 / scaledLineWidth : 1;
  9258. }
  9259. } else if (lineWidth === 0) {
  9260. scaleX = 1 / normX;
  9261. scaleY = 1 / normY;
  9262. } else {
  9263. const scaledXLineWidth = normX * lineWidth;
  9264. const scaledYLineWidth = normY * lineWidth;
  9265. scaleX = scaledXLineWidth < 1 ? 1 / scaledXLineWidth : 1;
  9266. scaleY = scaledYLineWidth < 1 ? 1 / scaledYLineWidth : 1;
  9267. }
  9268. } else {
  9269. const absDet = Math.abs(a * d - b * c);
  9270. const normX = Math.hypot(a, b);
  9271. const normY = Math.hypot(c, d);
  9272. if (lineWidth === 0) {
  9273. scaleX = normY / absDet;
  9274. scaleY = normX / absDet;
  9275. } else {
  9276. const baseArea = lineWidth * absDet;
  9277. scaleX = normY > baseArea ? normY / baseArea : 1;
  9278. scaleY = normX > baseArea ? normX / baseArea : 1;
  9279. }
  9280. }
  9281. this._cachedScaleForStroking[0] = scaleX;
  9282. this._cachedScaleForStroking[1] = scaleY;
  9283. }
  9284. return this._cachedScaleForStroking;
  9285. }
  9286. rescaleAndStroke(path, saveRestore) {
  9287. const {
  9288. ctx,
  9289. current: {
  9290. lineWidth
  9291. }
  9292. } = this;
  9293. const [scaleX, scaleY] = this.getScaleForStroking();
  9294. if (scaleX === scaleY) {
  9295. ctx.lineWidth = (lineWidth || 1) * scaleX;
  9296. ctx.stroke(path);
  9297. return;
  9298. }
  9299. const dashes = ctx.getLineDash();
  9300. if (saveRestore) {
  9301. ctx.save();
  9302. }
  9303. ctx.scale(scaleX, scaleY);
  9304. SCALE_MATRIX.a = 1 / scaleX;
  9305. SCALE_MATRIX.d = 1 / scaleY;
  9306. const newPath = new Path2D();
  9307. newPath.addPath(path, SCALE_MATRIX);
  9308. if (dashes.length > 0) {
  9309. const scale = Math.max(scaleX, scaleY);
  9310. ctx.setLineDash(dashes.map(x => x / scale));
  9311. ctx.lineDashOffset /= scale;
  9312. }
  9313. ctx.lineWidth = lineWidth || 1;
  9314. ctx.stroke(newPath);
  9315. if (saveRestore) {
  9316. ctx.restore();
  9317. }
  9318. }
  9319. isContentVisible() {
  9320. for (let i = this.markedContentStack.length - 1; i >= 0; i--) {
  9321. if (!this.markedContentStack[i].visible) {
  9322. return false;
  9323. }
  9324. }
  9325. return true;
  9326. }
  9327. }
  9328. for (const op in OPS) {
  9329. if (CanvasGraphics.prototype[op] !== undefined) {
  9330. CanvasGraphics.prototype[OPS[op]] = CanvasGraphics.prototype[op];
  9331. }
  9332. }
  9333. ;// ./src/display/worker_options.js
  9334. class GlobalWorkerOptions {
  9335. static #port = null;
  9336. static #src = "";
  9337. static get workerPort() {
  9338. return this.#port;
  9339. }
  9340. static set workerPort(val) {
  9341. if (!(typeof Worker !== "undefined" && val instanceof Worker) && val !== null) {
  9342. throw new Error("Invalid `workerPort` type.");
  9343. }
  9344. this.#port = val;
  9345. }
  9346. static get workerSrc() {
  9347. return this.#src;
  9348. }
  9349. static set workerSrc(val) {
  9350. if (typeof val !== "string") {
  9351. throw new Error("Invalid `workerSrc` type.");
  9352. }
  9353. this.#src = val;
  9354. }
  9355. }
  9356. ;// ./src/display/metadata.js
  9357. class Metadata {
  9358. #metadataMap;
  9359. #data;
  9360. constructor({
  9361. parsedData,
  9362. rawData
  9363. }) {
  9364. this.#metadataMap = parsedData;
  9365. this.#data = rawData;
  9366. }
  9367. getRaw() {
  9368. return this.#data;
  9369. }
  9370. get(name) {
  9371. return this.#metadataMap.get(name) ?? null;
  9372. }
  9373. getAll() {
  9374. return objectFromMap(this.#metadataMap);
  9375. }
  9376. has(name) {
  9377. return this.#metadataMap.has(name);
  9378. }
  9379. }
  9380. ;// ./src/display/optional_content_config.js
  9381. const INTERNAL = Symbol("INTERNAL");
  9382. class OptionalContentGroup {
  9383. #isDisplay = false;
  9384. #isPrint = false;
  9385. #userSet = false;
  9386. #visible = true;
  9387. constructor(renderingIntent, {
  9388. name,
  9389. intent,
  9390. usage,
  9391. rbGroups
  9392. }) {
  9393. this.#isDisplay = !!(renderingIntent & RenderingIntentFlag.DISPLAY);
  9394. this.#isPrint = !!(renderingIntent & RenderingIntentFlag.PRINT);
  9395. this.name = name;
  9396. this.intent = intent;
  9397. this.usage = usage;
  9398. this.rbGroups = rbGroups;
  9399. }
  9400. get visible() {
  9401. if (this.#userSet) {
  9402. return this.#visible;
  9403. }
  9404. if (!this.#visible) {
  9405. return false;
  9406. }
  9407. const {
  9408. print,
  9409. view
  9410. } = this.usage;
  9411. if (this.#isDisplay) {
  9412. return view?.viewState !== "OFF";
  9413. } else if (this.#isPrint) {
  9414. return print?.printState !== "OFF";
  9415. }
  9416. return true;
  9417. }
  9418. _setVisible(internal, visible, userSet = false) {
  9419. if (internal !== INTERNAL) {
  9420. unreachable("Internal method `_setVisible` called.");
  9421. }
  9422. this.#userSet = userSet;
  9423. this.#visible = visible;
  9424. }
  9425. }
  9426. class OptionalContentConfig {
  9427. #cachedGetHash = null;
  9428. #groups = new Map();
  9429. #initialHash = null;
  9430. #order = null;
  9431. constructor(data, renderingIntent = RenderingIntentFlag.DISPLAY) {
  9432. this.renderingIntent = renderingIntent;
  9433. this.name = null;
  9434. this.creator = null;
  9435. if (data === null) {
  9436. return;
  9437. }
  9438. this.name = data.name;
  9439. this.creator = data.creator;
  9440. this.#order = data.order;
  9441. for (const group of data.groups) {
  9442. this.#groups.set(group.id, new OptionalContentGroup(renderingIntent, group));
  9443. }
  9444. if (data.baseState === "OFF") {
  9445. for (const group of this.#groups.values()) {
  9446. group._setVisible(INTERNAL, false);
  9447. }
  9448. }
  9449. for (const on of data.on) {
  9450. this.#groups.get(on)._setVisible(INTERNAL, true);
  9451. }
  9452. for (const off of data.off) {
  9453. this.#groups.get(off)._setVisible(INTERNAL, false);
  9454. }
  9455. this.#initialHash = this.getHash();
  9456. }
  9457. #evaluateVisibilityExpression(array) {
  9458. const length = array.length;
  9459. if (length < 2) {
  9460. return true;
  9461. }
  9462. const operator = array[0];
  9463. for (let i = 1; i < length; i++) {
  9464. const element = array[i];
  9465. let state;
  9466. if (Array.isArray(element)) {
  9467. state = this.#evaluateVisibilityExpression(element);
  9468. } else if (this.#groups.has(element)) {
  9469. state = this.#groups.get(element).visible;
  9470. } else {
  9471. warn(`Optional content group not found: ${element}`);
  9472. return true;
  9473. }
  9474. switch (operator) {
  9475. case "And":
  9476. if (!state) {
  9477. return false;
  9478. }
  9479. break;
  9480. case "Or":
  9481. if (state) {
  9482. return true;
  9483. }
  9484. break;
  9485. case "Not":
  9486. return !state;
  9487. default:
  9488. return true;
  9489. }
  9490. }
  9491. return operator === "And";
  9492. }
  9493. isVisible(group) {
  9494. if (this.#groups.size === 0) {
  9495. return true;
  9496. }
  9497. if (!group) {
  9498. info("Optional content group not defined.");
  9499. return true;
  9500. }
  9501. if (group.type === "OCG") {
  9502. if (!this.#groups.has(group.id)) {
  9503. warn(`Optional content group not found: ${group.id}`);
  9504. return true;
  9505. }
  9506. return this.#groups.get(group.id).visible;
  9507. } else if (group.type === "OCMD") {
  9508. if (group.expression) {
  9509. return this.#evaluateVisibilityExpression(group.expression);
  9510. }
  9511. if (!group.policy || group.policy === "AnyOn") {
  9512. for (const id of group.ids) {
  9513. if (!this.#groups.has(id)) {
  9514. warn(`Optional content group not found: ${id}`);
  9515. return true;
  9516. }
  9517. if (this.#groups.get(id).visible) {
  9518. return true;
  9519. }
  9520. }
  9521. return false;
  9522. } else if (group.policy === "AllOn") {
  9523. for (const id of group.ids) {
  9524. if (!this.#groups.has(id)) {
  9525. warn(`Optional content group not found: ${id}`);
  9526. return true;
  9527. }
  9528. if (!this.#groups.get(id).visible) {
  9529. return false;
  9530. }
  9531. }
  9532. return true;
  9533. } else if (group.policy === "AnyOff") {
  9534. for (const id of group.ids) {
  9535. if (!this.#groups.has(id)) {
  9536. warn(`Optional content group not found: ${id}`);
  9537. return true;
  9538. }
  9539. if (!this.#groups.get(id).visible) {
  9540. return true;
  9541. }
  9542. }
  9543. return false;
  9544. } else if (group.policy === "AllOff") {
  9545. for (const id of group.ids) {
  9546. if (!this.#groups.has(id)) {
  9547. warn(`Optional content group not found: ${id}`);
  9548. return true;
  9549. }
  9550. if (this.#groups.get(id).visible) {
  9551. return false;
  9552. }
  9553. }
  9554. return true;
  9555. }
  9556. warn(`Unknown optional content policy ${group.policy}.`);
  9557. return true;
  9558. }
  9559. warn(`Unknown group type ${group.type}.`);
  9560. return true;
  9561. }
  9562. setVisibility(id, visible = true, preserveRB = true) {
  9563. const group = this.#groups.get(id);
  9564. if (!group) {
  9565. warn(`Optional content group not found: ${id}`);
  9566. return;
  9567. }
  9568. if (preserveRB && visible && group.rbGroups.length) {
  9569. for (const rbGroup of group.rbGroups) {
  9570. for (const otherId of rbGroup) {
  9571. if (otherId !== id) {
  9572. this.#groups.get(otherId)?._setVisible(INTERNAL, false, true);
  9573. }
  9574. }
  9575. }
  9576. }
  9577. group._setVisible(INTERNAL, !!visible, true);
  9578. this.#cachedGetHash = null;
  9579. }
  9580. setOCGState({
  9581. state,
  9582. preserveRB
  9583. }) {
  9584. let operator;
  9585. for (const elem of state) {
  9586. switch (elem) {
  9587. case "ON":
  9588. case "OFF":
  9589. case "Toggle":
  9590. operator = elem;
  9591. continue;
  9592. }
  9593. const group = this.#groups.get(elem);
  9594. if (!group) {
  9595. continue;
  9596. }
  9597. switch (operator) {
  9598. case "ON":
  9599. this.setVisibility(elem, true, preserveRB);
  9600. break;
  9601. case "OFF":
  9602. this.setVisibility(elem, false, preserveRB);
  9603. break;
  9604. case "Toggle":
  9605. this.setVisibility(elem, !group.visible, preserveRB);
  9606. break;
  9607. }
  9608. }
  9609. this.#cachedGetHash = null;
  9610. }
  9611. get hasInitialVisibility() {
  9612. return this.#initialHash === null || this.getHash() === this.#initialHash;
  9613. }
  9614. getOrder() {
  9615. if (!this.#groups.size) {
  9616. return null;
  9617. }
  9618. if (this.#order) {
  9619. return this.#order.slice();
  9620. }
  9621. return [...this.#groups.keys()];
  9622. }
  9623. getGroups() {
  9624. return this.#groups.size > 0 ? objectFromMap(this.#groups) : null;
  9625. }
  9626. getGroup(id) {
  9627. return this.#groups.get(id) || null;
  9628. }
  9629. getHash() {
  9630. if (this.#cachedGetHash !== null) {
  9631. return this.#cachedGetHash;
  9632. }
  9633. const hash = new MurmurHash3_64();
  9634. for (const [id, group] of this.#groups) {
  9635. hash.update(`${id}:${group.visible}`);
  9636. }
  9637. return this.#cachedGetHash = hash.hexdigest();
  9638. }
  9639. }
  9640. ;// ./src/display/transport_stream.js
  9641. class PDFDataTransportStream {
  9642. constructor(pdfDataRangeTransport, {
  9643. disableRange = false,
  9644. disableStream = false
  9645. }) {
  9646. assert(pdfDataRangeTransport, 'PDFDataTransportStream - missing required "pdfDataRangeTransport" argument.');
  9647. const {
  9648. length,
  9649. initialData,
  9650. progressiveDone,
  9651. contentDispositionFilename
  9652. } = pdfDataRangeTransport;
  9653. this._queuedChunks = [];
  9654. this._progressiveDone = progressiveDone;
  9655. this._contentDispositionFilename = contentDispositionFilename;
  9656. if (initialData?.length > 0) {
  9657. const buffer = initialData instanceof Uint8Array && initialData.byteLength === initialData.buffer.byteLength ? initialData.buffer : new Uint8Array(initialData).buffer;
  9658. this._queuedChunks.push(buffer);
  9659. }
  9660. this._pdfDataRangeTransport = pdfDataRangeTransport;
  9661. this._isStreamingSupported = !disableStream;
  9662. this._isRangeSupported = !disableRange;
  9663. this._contentLength = length;
  9664. this._fullRequestReader = null;
  9665. this._rangeReaders = [];
  9666. pdfDataRangeTransport.addRangeListener((begin, chunk) => {
  9667. this._onReceiveData({
  9668. begin,
  9669. chunk
  9670. });
  9671. });
  9672. pdfDataRangeTransport.addProgressListener((loaded, total) => {
  9673. this._onProgress({
  9674. loaded,
  9675. total
  9676. });
  9677. });
  9678. pdfDataRangeTransport.addProgressiveReadListener(chunk => {
  9679. this._onReceiveData({
  9680. chunk
  9681. });
  9682. });
  9683. pdfDataRangeTransport.addProgressiveDoneListener(() => {
  9684. this._onProgressiveDone();
  9685. });
  9686. pdfDataRangeTransport.transportReady();
  9687. }
  9688. _onReceiveData({
  9689. begin,
  9690. chunk
  9691. }) {
  9692. const buffer = chunk instanceof Uint8Array && chunk.byteLength === chunk.buffer.byteLength ? chunk.buffer : new Uint8Array(chunk).buffer;
  9693. if (begin === undefined) {
  9694. if (this._fullRequestReader) {
  9695. this._fullRequestReader._enqueue(buffer);
  9696. } else {
  9697. this._queuedChunks.push(buffer);
  9698. }
  9699. } else {
  9700. const found = this._rangeReaders.some(function (rangeReader) {
  9701. if (rangeReader._begin !== begin) {
  9702. return false;
  9703. }
  9704. rangeReader._enqueue(buffer);
  9705. return true;
  9706. });
  9707. assert(found, "_onReceiveData - no `PDFDataTransportStreamRangeReader` instance found.");
  9708. }
  9709. }
  9710. get _progressiveDataLength() {
  9711. return this._fullRequestReader?._loaded ?? 0;
  9712. }
  9713. _onProgress(evt) {
  9714. if (evt.total === undefined) {
  9715. this._rangeReaders[0]?.onProgress?.({
  9716. loaded: evt.loaded
  9717. });
  9718. } else {
  9719. this._fullRequestReader?.onProgress?.({
  9720. loaded: evt.loaded,
  9721. total: evt.total
  9722. });
  9723. }
  9724. }
  9725. _onProgressiveDone() {
  9726. this._fullRequestReader?.progressiveDone();
  9727. this._progressiveDone = true;
  9728. }
  9729. _removeRangeReader(reader) {
  9730. const i = this._rangeReaders.indexOf(reader);
  9731. if (i >= 0) {
  9732. this._rangeReaders.splice(i, 1);
  9733. }
  9734. }
  9735. getFullReader() {
  9736. assert(!this._fullRequestReader, "PDFDataTransportStream.getFullReader can only be called once.");
  9737. const queuedChunks = this._queuedChunks;
  9738. this._queuedChunks = null;
  9739. return new PDFDataTransportStreamReader(this, queuedChunks, this._progressiveDone, this._contentDispositionFilename);
  9740. }
  9741. getRangeReader(begin, end) {
  9742. if (end <= this._progressiveDataLength) {
  9743. return null;
  9744. }
  9745. const reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  9746. this._pdfDataRangeTransport.requestDataRange(begin, end);
  9747. this._rangeReaders.push(reader);
  9748. return reader;
  9749. }
  9750. cancelAllRequests(reason) {
  9751. this._fullRequestReader?.cancel(reason);
  9752. for (const reader of this._rangeReaders.slice(0)) {
  9753. reader.cancel(reason);
  9754. }
  9755. this._pdfDataRangeTransport.abort();
  9756. }
  9757. }
  9758. class PDFDataTransportStreamReader {
  9759. constructor(stream, queuedChunks, progressiveDone = false, contentDispositionFilename = null) {
  9760. this._stream = stream;
  9761. this._done = progressiveDone || false;
  9762. this._filename = isPdfFile(contentDispositionFilename) ? contentDispositionFilename : null;
  9763. this._queuedChunks = queuedChunks || [];
  9764. this._loaded = 0;
  9765. for (const chunk of this._queuedChunks) {
  9766. this._loaded += chunk.byteLength;
  9767. }
  9768. this._requests = [];
  9769. this._headersReady = Promise.resolve();
  9770. stream._fullRequestReader = this;
  9771. this.onProgress = null;
  9772. }
  9773. _enqueue(chunk) {
  9774. if (this._done) {
  9775. return;
  9776. }
  9777. if (this._requests.length > 0) {
  9778. const requestCapability = this._requests.shift();
  9779. requestCapability.resolve({
  9780. value: chunk,
  9781. done: false
  9782. });
  9783. } else {
  9784. this._queuedChunks.push(chunk);
  9785. }
  9786. this._loaded += chunk.byteLength;
  9787. }
  9788. get headersReady() {
  9789. return this._headersReady;
  9790. }
  9791. get filename() {
  9792. return this._filename;
  9793. }
  9794. get isRangeSupported() {
  9795. return this._stream._isRangeSupported;
  9796. }
  9797. get isStreamingSupported() {
  9798. return this._stream._isStreamingSupported;
  9799. }
  9800. get contentLength() {
  9801. return this._stream._contentLength;
  9802. }
  9803. async read() {
  9804. if (this._queuedChunks.length > 0) {
  9805. const chunk = this._queuedChunks.shift();
  9806. return {
  9807. value: chunk,
  9808. done: false
  9809. };
  9810. }
  9811. if (this._done) {
  9812. return {
  9813. value: undefined,
  9814. done: true
  9815. };
  9816. }
  9817. const requestCapability = Promise.withResolvers();
  9818. this._requests.push(requestCapability);
  9819. return requestCapability.promise;
  9820. }
  9821. cancel(reason) {
  9822. this._done = true;
  9823. for (const requestCapability of this._requests) {
  9824. requestCapability.resolve({
  9825. value: undefined,
  9826. done: true
  9827. });
  9828. }
  9829. this._requests.length = 0;
  9830. }
  9831. progressiveDone() {
  9832. if (this._done) {
  9833. return;
  9834. }
  9835. this._done = true;
  9836. }
  9837. }
  9838. class PDFDataTransportStreamRangeReader {
  9839. constructor(stream, begin, end) {
  9840. this._stream = stream;
  9841. this._begin = begin;
  9842. this._end = end;
  9843. this._queuedChunk = null;
  9844. this._requests = [];
  9845. this._done = false;
  9846. this.onProgress = null;
  9847. }
  9848. _enqueue(chunk) {
  9849. if (this._done) {
  9850. return;
  9851. }
  9852. if (this._requests.length === 0) {
  9853. this._queuedChunk = chunk;
  9854. } else {
  9855. const requestsCapability = this._requests.shift();
  9856. requestsCapability.resolve({
  9857. value: chunk,
  9858. done: false
  9859. });
  9860. for (const requestCapability of this._requests) {
  9861. requestCapability.resolve({
  9862. value: undefined,
  9863. done: true
  9864. });
  9865. }
  9866. this._requests.length = 0;
  9867. }
  9868. this._done = true;
  9869. this._stream._removeRangeReader(this);
  9870. }
  9871. get isStreamingSupported() {
  9872. return false;
  9873. }
  9874. async read() {
  9875. if (this._queuedChunk) {
  9876. const chunk = this._queuedChunk;
  9877. this._queuedChunk = null;
  9878. return {
  9879. value: chunk,
  9880. done: false
  9881. };
  9882. }
  9883. if (this._done) {
  9884. return {
  9885. value: undefined,
  9886. done: true
  9887. };
  9888. }
  9889. const requestCapability = Promise.withResolvers();
  9890. this._requests.push(requestCapability);
  9891. return requestCapability.promise;
  9892. }
  9893. cancel(reason) {
  9894. this._done = true;
  9895. for (const requestCapability of this._requests) {
  9896. requestCapability.resolve({
  9897. value: undefined,
  9898. done: true
  9899. });
  9900. }
  9901. this._requests.length = 0;
  9902. this._stream._removeRangeReader(this);
  9903. }
  9904. }
  9905. ;// ./src/display/content_disposition.js
  9906. function getFilenameFromContentDispositionHeader(contentDisposition) {
  9907. let needsEncodingFixup = true;
  9908. let tmp = toParamRegExp("filename\\*", "i").exec(contentDisposition);
  9909. if (tmp) {
  9910. tmp = tmp[1];
  9911. let filename = rfc2616unquote(tmp);
  9912. filename = unescape(filename);
  9913. filename = rfc5987decode(filename);
  9914. filename = rfc2047decode(filename);
  9915. return fixupEncoding(filename);
  9916. }
  9917. tmp = rfc2231getparam(contentDisposition);
  9918. if (tmp) {
  9919. const filename = rfc2047decode(tmp);
  9920. return fixupEncoding(filename);
  9921. }
  9922. tmp = toParamRegExp("filename", "i").exec(contentDisposition);
  9923. if (tmp) {
  9924. tmp = tmp[1];
  9925. let filename = rfc2616unquote(tmp);
  9926. filename = rfc2047decode(filename);
  9927. return fixupEncoding(filename);
  9928. }
  9929. function toParamRegExp(attributePattern, flags) {
  9930. return new RegExp("(?:^|;)\\s*" + attributePattern + "\\s*=\\s*" + "(" + '[^";\\s][^;\\s]*' + "|" + '"(?:[^"\\\\]|\\\\"?)+"?' + ")", flags);
  9931. }
  9932. function textdecode(encoding, value) {
  9933. if (encoding) {
  9934. if (!/^[\x00-\xFF]+$/.test(value)) {
  9935. return value;
  9936. }
  9937. try {
  9938. const decoder = new TextDecoder(encoding, {
  9939. fatal: true
  9940. });
  9941. const buffer = stringToBytes(value);
  9942. value = decoder.decode(buffer);
  9943. needsEncodingFixup = false;
  9944. } catch {}
  9945. }
  9946. return value;
  9947. }
  9948. function fixupEncoding(value) {
  9949. if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
  9950. value = textdecode("utf-8", value);
  9951. if (needsEncodingFixup) {
  9952. value = textdecode("iso-8859-1", value);
  9953. }
  9954. }
  9955. return value;
  9956. }
  9957. function rfc2231getparam(contentDispositionStr) {
  9958. const matches = [];
  9959. let match;
  9960. const iter = toParamRegExp("filename\\*((?!0\\d)\\d+)(\\*?)", "ig");
  9961. while ((match = iter.exec(contentDispositionStr)) !== null) {
  9962. let [, n, quot, part] = match;
  9963. n = parseInt(n, 10);
  9964. if (n in matches) {
  9965. if (n === 0) {
  9966. break;
  9967. }
  9968. continue;
  9969. }
  9970. matches[n] = [quot, part];
  9971. }
  9972. const parts = [];
  9973. for (let n = 0; n < matches.length; ++n) {
  9974. if (!(n in matches)) {
  9975. break;
  9976. }
  9977. let [quot, part] = matches[n];
  9978. part = rfc2616unquote(part);
  9979. if (quot) {
  9980. part = unescape(part);
  9981. if (n === 0) {
  9982. part = rfc5987decode(part);
  9983. }
  9984. }
  9985. parts.push(part);
  9986. }
  9987. return parts.join("");
  9988. }
  9989. function rfc2616unquote(value) {
  9990. if (value.startsWith('"')) {
  9991. const parts = value.slice(1).split('\\"');
  9992. for (let i = 0; i < parts.length; ++i) {
  9993. const quotindex = parts[i].indexOf('"');
  9994. if (quotindex !== -1) {
  9995. parts[i] = parts[i].slice(0, quotindex);
  9996. parts.length = i + 1;
  9997. }
  9998. parts[i] = parts[i].replaceAll(/\\(.)/g, "$1");
  9999. }
  10000. value = parts.join('"');
  10001. }
  10002. return value;
  10003. }
  10004. function rfc5987decode(extvalue) {
  10005. const encodingend = extvalue.indexOf("'");
  10006. if (encodingend === -1) {
  10007. return extvalue;
  10008. }
  10009. const encoding = extvalue.slice(0, encodingend);
  10010. const langvalue = extvalue.slice(encodingend + 1);
  10011. const value = langvalue.replace(/^[^']*'/, "");
  10012. return textdecode(encoding, value);
  10013. }
  10014. function rfc2047decode(value) {
  10015. if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {
  10016. return value;
  10017. }
  10018. return value.replaceAll(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (matches, charset, encoding, text) {
  10019. if (encoding === "q" || encoding === "Q") {
  10020. text = text.replaceAll("_", " ");
  10021. text = text.replaceAll(/=([0-9a-fA-F]{2})/g, function (match, hex) {
  10022. return String.fromCharCode(parseInt(hex, 16));
  10023. });
  10024. return textdecode(charset, text);
  10025. }
  10026. try {
  10027. text = atob(text);
  10028. } catch {}
  10029. return textdecode(charset, text);
  10030. });
  10031. }
  10032. return "";
  10033. }
  10034. ;// ./src/display/network_utils.js
  10035. function createHeaders(isHttp, httpHeaders) {
  10036. const headers = new Headers();
  10037. if (!isHttp || !httpHeaders || typeof httpHeaders !== "object") {
  10038. return headers;
  10039. }
  10040. for (const key in httpHeaders) {
  10041. const val = httpHeaders[key];
  10042. if (val !== undefined) {
  10043. headers.append(key, val);
  10044. }
  10045. }
  10046. return headers;
  10047. }
  10048. function getResponseOrigin(url) {
  10049. return URL.parse(url)?.origin ?? null;
  10050. }
  10051. function validateRangeRequestCapabilities({
  10052. responseHeaders,
  10053. isHttp,
  10054. rangeChunkSize,
  10055. disableRange
  10056. }) {
  10057. const returnValues = {
  10058. allowRangeRequests: false,
  10059. suggestedLength: undefined
  10060. };
  10061. const length = parseInt(responseHeaders.get("Content-Length"), 10);
  10062. if (!Number.isInteger(length)) {
  10063. return returnValues;
  10064. }
  10065. returnValues.suggestedLength = length;
  10066. if (length <= 2 * rangeChunkSize) {
  10067. return returnValues;
  10068. }
  10069. if (disableRange || !isHttp) {
  10070. return returnValues;
  10071. }
  10072. if (responseHeaders.get("Accept-Ranges") !== "bytes") {
  10073. return returnValues;
  10074. }
  10075. const contentEncoding = responseHeaders.get("Content-Encoding") || "identity";
  10076. if (contentEncoding !== "identity") {
  10077. return returnValues;
  10078. }
  10079. returnValues.allowRangeRequests = true;
  10080. return returnValues;
  10081. }
  10082. function extractFilenameFromHeader(responseHeaders) {
  10083. const contentDisposition = responseHeaders.get("Content-Disposition");
  10084. if (contentDisposition) {
  10085. let filename = getFilenameFromContentDispositionHeader(contentDisposition);
  10086. if (filename.includes("%")) {
  10087. try {
  10088. filename = decodeURIComponent(filename);
  10089. } catch {}
  10090. }
  10091. if (isPdfFile(filename)) {
  10092. return filename;
  10093. }
  10094. }
  10095. return null;
  10096. }
  10097. function createResponseError(status, url) {
  10098. return new ResponseException(`Unexpected server response (${status}) while retrieving PDF "${url}".`, status, status === 404 || status === 0 && url.startsWith("file:"));
  10099. }
  10100. function validateResponseStatus(status) {
  10101. return status === 200 || status === 206;
  10102. }
  10103. ;// ./src/display/fetch_stream.js
  10104. function createFetchOptions(headers, withCredentials, abortController) {
  10105. return {
  10106. method: "GET",
  10107. headers,
  10108. signal: abortController.signal,
  10109. mode: "cors",
  10110. credentials: withCredentials ? "include" : "same-origin",
  10111. redirect: "follow"
  10112. };
  10113. }
  10114. function getArrayBuffer(val) {
  10115. if (val instanceof Uint8Array) {
  10116. return val.buffer;
  10117. }
  10118. if (val instanceof ArrayBuffer) {
  10119. return val;
  10120. }
  10121. warn(`getArrayBuffer - unexpected data format: ${val}`);
  10122. return new Uint8Array(val).buffer;
  10123. }
  10124. class PDFFetchStream {
  10125. _responseOrigin = null;
  10126. constructor(source) {
  10127. this.source = source;
  10128. this.isHttp = /^https?:/i.test(source.url);
  10129. this.headers = createHeaders(this.isHttp, source.httpHeaders);
  10130. this._fullRequestReader = null;
  10131. this._rangeRequestReaders = [];
  10132. }
  10133. get _progressiveDataLength() {
  10134. return this._fullRequestReader?._loaded ?? 0;
  10135. }
  10136. getFullReader() {
  10137. assert(!this._fullRequestReader, "PDFFetchStream.getFullReader can only be called once.");
  10138. this._fullRequestReader = new PDFFetchStreamReader(this);
  10139. return this._fullRequestReader;
  10140. }
  10141. getRangeReader(begin, end) {
  10142. if (end <= this._progressiveDataLength) {
  10143. return null;
  10144. }
  10145. const reader = new PDFFetchStreamRangeReader(this, begin, end);
  10146. this._rangeRequestReaders.push(reader);
  10147. return reader;
  10148. }
  10149. cancelAllRequests(reason) {
  10150. this._fullRequestReader?.cancel(reason);
  10151. for (const reader of this._rangeRequestReaders.slice(0)) {
  10152. reader.cancel(reason);
  10153. }
  10154. }
  10155. }
  10156. class PDFFetchStreamReader {
  10157. constructor(stream) {
  10158. this._stream = stream;
  10159. this._reader = null;
  10160. this._loaded = 0;
  10161. this._filename = null;
  10162. const source = stream.source;
  10163. this._withCredentials = source.withCredentials || false;
  10164. this._contentLength = source.length;
  10165. this._headersCapability = Promise.withResolvers();
  10166. this._disableRange = source.disableRange || false;
  10167. this._rangeChunkSize = source.rangeChunkSize;
  10168. if (!this._rangeChunkSize && !this._disableRange) {
  10169. this._disableRange = true;
  10170. }
  10171. this._abortController = new AbortController();
  10172. this._isStreamingSupported = !source.disableStream;
  10173. this._isRangeSupported = !source.disableRange;
  10174. const headers = new Headers(stream.headers);
  10175. const url = source.url;
  10176. fetch(url, createFetchOptions(headers, this._withCredentials, this._abortController)).then(response => {
  10177. stream._responseOrigin = getResponseOrigin(response.url);
  10178. if (!validateResponseStatus(response.status)) {
  10179. throw createResponseError(response.status, url);
  10180. }
  10181. this._reader = response.body.getReader();
  10182. this._headersCapability.resolve();
  10183. const responseHeaders = response.headers;
  10184. const {
  10185. allowRangeRequests,
  10186. suggestedLength
  10187. } = validateRangeRequestCapabilities({
  10188. responseHeaders,
  10189. isHttp: stream.isHttp,
  10190. rangeChunkSize: this._rangeChunkSize,
  10191. disableRange: this._disableRange
  10192. });
  10193. this._isRangeSupported = allowRangeRequests;
  10194. this._contentLength = suggestedLength || this._contentLength;
  10195. this._filename = extractFilenameFromHeader(responseHeaders);
  10196. if (!this._isStreamingSupported && this._isRangeSupported) {
  10197. this.cancel(new AbortException("Streaming is disabled."));
  10198. }
  10199. }).catch(this._headersCapability.reject);
  10200. this.onProgress = null;
  10201. }
  10202. get headersReady() {
  10203. return this._headersCapability.promise;
  10204. }
  10205. get filename() {
  10206. return this._filename;
  10207. }
  10208. get contentLength() {
  10209. return this._contentLength;
  10210. }
  10211. get isRangeSupported() {
  10212. return this._isRangeSupported;
  10213. }
  10214. get isStreamingSupported() {
  10215. return this._isStreamingSupported;
  10216. }
  10217. async read() {
  10218. await this._headersCapability.promise;
  10219. const {
  10220. value,
  10221. done
  10222. } = await this._reader.read();
  10223. if (done) {
  10224. return {
  10225. value,
  10226. done
  10227. };
  10228. }
  10229. this._loaded += value.byteLength;
  10230. this.onProgress?.({
  10231. loaded: this._loaded,
  10232. total: this._contentLength
  10233. });
  10234. return {
  10235. value: getArrayBuffer(value),
  10236. done: false
  10237. };
  10238. }
  10239. cancel(reason) {
  10240. this._reader?.cancel(reason);
  10241. this._abortController.abort();
  10242. }
  10243. }
  10244. class PDFFetchStreamRangeReader {
  10245. constructor(stream, begin, end) {
  10246. this._stream = stream;
  10247. this._reader = null;
  10248. this._loaded = 0;
  10249. const source = stream.source;
  10250. this._withCredentials = source.withCredentials || false;
  10251. this._readCapability = Promise.withResolvers();
  10252. this._isStreamingSupported = !source.disableStream;
  10253. this._abortController = new AbortController();
  10254. const headers = new Headers(stream.headers);
  10255. headers.append("Range", `bytes=${begin}-${end - 1}`);
  10256. const url = source.url;
  10257. fetch(url, createFetchOptions(headers, this._withCredentials, this._abortController)).then(response => {
  10258. const responseOrigin = getResponseOrigin(response.url);
  10259. if (responseOrigin !== stream._responseOrigin) {
  10260. throw new Error(`Expected range response-origin "${responseOrigin}" to match "${stream._responseOrigin}".`);
  10261. }
  10262. if (!validateResponseStatus(response.status)) {
  10263. throw createResponseError(response.status, url);
  10264. }
  10265. this._readCapability.resolve();
  10266. this._reader = response.body.getReader();
  10267. }).catch(this._readCapability.reject);
  10268. this.onProgress = null;
  10269. }
  10270. get isStreamingSupported() {
  10271. return this._isStreamingSupported;
  10272. }
  10273. async read() {
  10274. await this._readCapability.promise;
  10275. const {
  10276. value,
  10277. done
  10278. } = await this._reader.read();
  10279. if (done) {
  10280. return {
  10281. value,
  10282. done
  10283. };
  10284. }
  10285. this._loaded += value.byteLength;
  10286. this.onProgress?.({
  10287. loaded: this._loaded
  10288. });
  10289. return {
  10290. value: getArrayBuffer(value),
  10291. done: false
  10292. };
  10293. }
  10294. cancel(reason) {
  10295. this._reader?.cancel(reason);
  10296. this._abortController.abort();
  10297. }
  10298. }
  10299. ;// ./src/display/network.js
  10300. const OK_RESPONSE = 200;
  10301. const PARTIAL_CONTENT_RESPONSE = 206;
  10302. function network_getArrayBuffer(xhr) {
  10303. const data = xhr.response;
  10304. if (typeof data !== "string") {
  10305. return data;
  10306. }
  10307. return stringToBytes(data).buffer;
  10308. }
  10309. class NetworkManager {
  10310. _responseOrigin = null;
  10311. constructor({
  10312. url,
  10313. httpHeaders,
  10314. withCredentials
  10315. }) {
  10316. this.url = url;
  10317. this.isHttp = /^https?:/i.test(url);
  10318. this.headers = createHeaders(this.isHttp, httpHeaders);
  10319. this.withCredentials = withCredentials || false;
  10320. this.currXhrId = 0;
  10321. this.pendingRequests = Object.create(null);
  10322. }
  10323. request(args) {
  10324. const xhr = new XMLHttpRequest();
  10325. const xhrId = this.currXhrId++;
  10326. const pendingRequest = this.pendingRequests[xhrId] = {
  10327. xhr
  10328. };
  10329. xhr.open("GET", this.url);
  10330. xhr.withCredentials = this.withCredentials;
  10331. for (const [key, val] of this.headers) {
  10332. xhr.setRequestHeader(key, val);
  10333. }
  10334. if (this.isHttp && "begin" in args && "end" in args) {
  10335. xhr.setRequestHeader("Range", `bytes=${args.begin}-${args.end - 1}`);
  10336. pendingRequest.expectedStatus = PARTIAL_CONTENT_RESPONSE;
  10337. } else {
  10338. pendingRequest.expectedStatus = OK_RESPONSE;
  10339. }
  10340. xhr.responseType = "arraybuffer";
  10341. assert(args.onError, "Expected `onError` callback to be provided.");
  10342. xhr.onerror = () => {
  10343. args.onError(xhr.status);
  10344. };
  10345. xhr.onreadystatechange = this.onStateChange.bind(this, xhrId);
  10346. xhr.onprogress = this.onProgress.bind(this, xhrId);
  10347. pendingRequest.onHeadersReceived = args.onHeadersReceived;
  10348. pendingRequest.onDone = args.onDone;
  10349. pendingRequest.onError = args.onError;
  10350. pendingRequest.onProgress = args.onProgress;
  10351. xhr.send(null);
  10352. return xhrId;
  10353. }
  10354. onProgress(xhrId, evt) {
  10355. const pendingRequest = this.pendingRequests[xhrId];
  10356. if (!pendingRequest) {
  10357. return;
  10358. }
  10359. pendingRequest.onProgress?.(evt);
  10360. }
  10361. onStateChange(xhrId, evt) {
  10362. const pendingRequest = this.pendingRequests[xhrId];
  10363. if (!pendingRequest) {
  10364. return;
  10365. }
  10366. const xhr = pendingRequest.xhr;
  10367. if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) {
  10368. pendingRequest.onHeadersReceived();
  10369. delete pendingRequest.onHeadersReceived;
  10370. }
  10371. if (xhr.readyState !== 4) {
  10372. return;
  10373. }
  10374. if (!(xhrId in this.pendingRequests)) {
  10375. return;
  10376. }
  10377. delete this.pendingRequests[xhrId];
  10378. if (xhr.status === 0 && this.isHttp) {
  10379. pendingRequest.onError(xhr.status);
  10380. return;
  10381. }
  10382. const xhrStatus = xhr.status || OK_RESPONSE;
  10383. const ok_response_on_range_request = xhrStatus === OK_RESPONSE && pendingRequest.expectedStatus === PARTIAL_CONTENT_RESPONSE;
  10384. if (!ok_response_on_range_request && xhrStatus !== pendingRequest.expectedStatus) {
  10385. pendingRequest.onError(xhr.status);
  10386. return;
  10387. }
  10388. const chunk = network_getArrayBuffer(xhr);
  10389. if (xhrStatus === PARTIAL_CONTENT_RESPONSE) {
  10390. const rangeHeader = xhr.getResponseHeader("Content-Range");
  10391. const matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader);
  10392. if (matches) {
  10393. pendingRequest.onDone({
  10394. begin: parseInt(matches[1], 10),
  10395. chunk
  10396. });
  10397. } else {
  10398. warn(`Missing or invalid "Content-Range" header.`);
  10399. pendingRequest.onError(0);
  10400. }
  10401. } else if (chunk) {
  10402. pendingRequest.onDone({
  10403. begin: 0,
  10404. chunk
  10405. });
  10406. } else {
  10407. pendingRequest.onError(xhr.status);
  10408. }
  10409. }
  10410. getRequestXhr(xhrId) {
  10411. return this.pendingRequests[xhrId].xhr;
  10412. }
  10413. isPendingRequest(xhrId) {
  10414. return xhrId in this.pendingRequests;
  10415. }
  10416. abortRequest(xhrId) {
  10417. const xhr = this.pendingRequests[xhrId].xhr;
  10418. delete this.pendingRequests[xhrId];
  10419. xhr.abort();
  10420. }
  10421. }
  10422. class PDFNetworkStream {
  10423. constructor(source) {
  10424. this._source = source;
  10425. this._manager = new NetworkManager(source);
  10426. this._rangeChunkSize = source.rangeChunkSize;
  10427. this._fullRequestReader = null;
  10428. this._rangeRequestReaders = [];
  10429. }
  10430. _onRangeRequestReaderClosed(reader) {
  10431. const i = this._rangeRequestReaders.indexOf(reader);
  10432. if (i >= 0) {
  10433. this._rangeRequestReaders.splice(i, 1);
  10434. }
  10435. }
  10436. getFullReader() {
  10437. assert(!this._fullRequestReader, "PDFNetworkStream.getFullReader can only be called once.");
  10438. this._fullRequestReader = new PDFNetworkStreamFullRequestReader(this._manager, this._source);
  10439. return this._fullRequestReader;
  10440. }
  10441. getRangeReader(begin, end) {
  10442. const reader = new PDFNetworkStreamRangeRequestReader(this._manager, begin, end);
  10443. reader.onClosed = this._onRangeRequestReaderClosed.bind(this);
  10444. this._rangeRequestReaders.push(reader);
  10445. return reader;
  10446. }
  10447. cancelAllRequests(reason) {
  10448. this._fullRequestReader?.cancel(reason);
  10449. for (const reader of this._rangeRequestReaders.slice(0)) {
  10450. reader.cancel(reason);
  10451. }
  10452. }
  10453. }
  10454. class PDFNetworkStreamFullRequestReader {
  10455. constructor(manager, source) {
  10456. this._manager = manager;
  10457. this._url = source.url;
  10458. this._fullRequestId = manager.request({
  10459. onHeadersReceived: this._onHeadersReceived.bind(this),
  10460. onDone: this._onDone.bind(this),
  10461. onError: this._onError.bind(this),
  10462. onProgress: this._onProgress.bind(this)
  10463. });
  10464. this._headersCapability = Promise.withResolvers();
  10465. this._disableRange = source.disableRange || false;
  10466. this._contentLength = source.length;
  10467. this._rangeChunkSize = source.rangeChunkSize;
  10468. if (!this._rangeChunkSize && !this._disableRange) {
  10469. this._disableRange = true;
  10470. }
  10471. this._isStreamingSupported = false;
  10472. this._isRangeSupported = false;
  10473. this._cachedChunks = [];
  10474. this._requests = [];
  10475. this._done = false;
  10476. this._storedError = undefined;
  10477. this._filename = null;
  10478. this.onProgress = null;
  10479. }
  10480. _onHeadersReceived() {
  10481. const fullRequestXhrId = this._fullRequestId;
  10482. const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
  10483. this._manager._responseOrigin = getResponseOrigin(fullRequestXhr.responseURL);
  10484. const rawResponseHeaders = fullRequestXhr.getAllResponseHeaders();
  10485. const responseHeaders = new Headers(rawResponseHeaders ? rawResponseHeaders.trimStart().replace(/[^\S ]+$/, "").split(/[\r\n]+/).map(x => {
  10486. const [key, ...val] = x.split(": ");
  10487. return [key, val.join(": ")];
  10488. }) : []);
  10489. const {
  10490. allowRangeRequests,
  10491. suggestedLength
  10492. } = validateRangeRequestCapabilities({
  10493. responseHeaders,
  10494. isHttp: this._manager.isHttp,
  10495. rangeChunkSize: this._rangeChunkSize,
  10496. disableRange: this._disableRange
  10497. });
  10498. if (allowRangeRequests) {
  10499. this._isRangeSupported = true;
  10500. }
  10501. this._contentLength = suggestedLength || this._contentLength;
  10502. this._filename = extractFilenameFromHeader(responseHeaders);
  10503. if (this._isRangeSupported) {
  10504. this._manager.abortRequest(fullRequestXhrId);
  10505. }
  10506. this._headersCapability.resolve();
  10507. }
  10508. _onDone(data) {
  10509. if (data) {
  10510. if (this._requests.length > 0) {
  10511. const requestCapability = this._requests.shift();
  10512. requestCapability.resolve({
  10513. value: data.chunk,
  10514. done: false
  10515. });
  10516. } else {
  10517. this._cachedChunks.push(data.chunk);
  10518. }
  10519. }
  10520. this._done = true;
  10521. if (this._cachedChunks.length > 0) {
  10522. return;
  10523. }
  10524. for (const requestCapability of this._requests) {
  10525. requestCapability.resolve({
  10526. value: undefined,
  10527. done: true
  10528. });
  10529. }
  10530. this._requests.length = 0;
  10531. }
  10532. _onError(status) {
  10533. this._storedError = createResponseError(status, this._url);
  10534. this._headersCapability.reject(this._storedError);
  10535. for (const requestCapability of this._requests) {
  10536. requestCapability.reject(this._storedError);
  10537. }
  10538. this._requests.length = 0;
  10539. this._cachedChunks.length = 0;
  10540. }
  10541. _onProgress(evt) {
  10542. this.onProgress?.({
  10543. loaded: evt.loaded,
  10544. total: evt.lengthComputable ? evt.total : this._contentLength
  10545. });
  10546. }
  10547. get filename() {
  10548. return this._filename;
  10549. }
  10550. get isRangeSupported() {
  10551. return this._isRangeSupported;
  10552. }
  10553. get isStreamingSupported() {
  10554. return this._isStreamingSupported;
  10555. }
  10556. get contentLength() {
  10557. return this._contentLength;
  10558. }
  10559. get headersReady() {
  10560. return this._headersCapability.promise;
  10561. }
  10562. async read() {
  10563. await this._headersCapability.promise;
  10564. if (this._storedError) {
  10565. throw this._storedError;
  10566. }
  10567. if (this._cachedChunks.length > 0) {
  10568. const chunk = this._cachedChunks.shift();
  10569. return {
  10570. value: chunk,
  10571. done: false
  10572. };
  10573. }
  10574. if (this._done) {
  10575. return {
  10576. value: undefined,
  10577. done: true
  10578. };
  10579. }
  10580. const requestCapability = Promise.withResolvers();
  10581. this._requests.push(requestCapability);
  10582. return requestCapability.promise;
  10583. }
  10584. cancel(reason) {
  10585. this._done = true;
  10586. this._headersCapability.reject(reason);
  10587. for (const requestCapability of this._requests) {
  10588. requestCapability.resolve({
  10589. value: undefined,
  10590. done: true
  10591. });
  10592. }
  10593. this._requests.length = 0;
  10594. if (this._manager.isPendingRequest(this._fullRequestId)) {
  10595. this._manager.abortRequest(this._fullRequestId);
  10596. }
  10597. this._fullRequestReader = null;
  10598. }
  10599. }
  10600. class PDFNetworkStreamRangeRequestReader {
  10601. constructor(manager, begin, end) {
  10602. this._manager = manager;
  10603. this._url = manager.url;
  10604. this._requestId = manager.request({
  10605. begin,
  10606. end,
  10607. onHeadersReceived: this._onHeadersReceived.bind(this),
  10608. onDone: this._onDone.bind(this),
  10609. onError: this._onError.bind(this),
  10610. onProgress: this._onProgress.bind(this)
  10611. });
  10612. this._requests = [];
  10613. this._queuedChunk = null;
  10614. this._done = false;
  10615. this._storedError = undefined;
  10616. this.onProgress = null;
  10617. this.onClosed = null;
  10618. }
  10619. _onHeadersReceived() {
  10620. const responseOrigin = getResponseOrigin(this._manager.getRequestXhr(this._requestId)?.responseURL);
  10621. if (responseOrigin !== this._manager._responseOrigin) {
  10622. this._storedError = new Error(`Expected range response-origin "${responseOrigin}" to match "${this._manager._responseOrigin}".`);
  10623. this._onError(0);
  10624. }
  10625. }
  10626. _close() {
  10627. this.onClosed?.(this);
  10628. }
  10629. _onDone(data) {
  10630. const chunk = data.chunk;
  10631. if (this._requests.length > 0) {
  10632. const requestCapability = this._requests.shift();
  10633. requestCapability.resolve({
  10634. value: chunk,
  10635. done: false
  10636. });
  10637. } else {
  10638. this._queuedChunk = chunk;
  10639. }
  10640. this._done = true;
  10641. for (const requestCapability of this._requests) {
  10642. requestCapability.resolve({
  10643. value: undefined,
  10644. done: true
  10645. });
  10646. }
  10647. this._requests.length = 0;
  10648. this._close();
  10649. }
  10650. _onError(status) {
  10651. this._storedError ??= createResponseError(status, this._url);
  10652. for (const requestCapability of this._requests) {
  10653. requestCapability.reject(this._storedError);
  10654. }
  10655. this._requests.length = 0;
  10656. this._queuedChunk = null;
  10657. }
  10658. _onProgress(evt) {
  10659. if (!this.isStreamingSupported) {
  10660. this.onProgress?.({
  10661. loaded: evt.loaded
  10662. });
  10663. }
  10664. }
  10665. get isStreamingSupported() {
  10666. return false;
  10667. }
  10668. async read() {
  10669. if (this._storedError) {
  10670. throw this._storedError;
  10671. }
  10672. if (this._queuedChunk !== null) {
  10673. const chunk = this._queuedChunk;
  10674. this._queuedChunk = null;
  10675. return {
  10676. value: chunk,
  10677. done: false
  10678. };
  10679. }
  10680. if (this._done) {
  10681. return {
  10682. value: undefined,
  10683. done: true
  10684. };
  10685. }
  10686. const requestCapability = Promise.withResolvers();
  10687. this._requests.push(requestCapability);
  10688. return requestCapability.promise;
  10689. }
  10690. cancel(reason) {
  10691. this._done = true;
  10692. for (const requestCapability of this._requests) {
  10693. requestCapability.resolve({
  10694. value: undefined,
  10695. done: true
  10696. });
  10697. }
  10698. this._requests.length = 0;
  10699. if (this._manager.isPendingRequest(this._requestId)) {
  10700. this._manager.abortRequest(this._requestId);
  10701. }
  10702. this._close();
  10703. }
  10704. }
  10705. ;// ./src/display/node_stream.js
  10706. const urlRegex = /^[a-z][a-z0-9\-+.]+:/i;
  10707. function parseUrlOrPath(sourceUrl) {
  10708. if (urlRegex.test(sourceUrl)) {
  10709. return new URL(sourceUrl);
  10710. }
  10711. const url = process.getBuiltinModule("url");
  10712. return new URL(url.pathToFileURL(sourceUrl));
  10713. }
  10714. class PDFNodeStream {
  10715. constructor(source) {
  10716. this.source = source;
  10717. this.url = parseUrlOrPath(source.url);
  10718. assert(this.url.protocol === "file:", "PDFNodeStream only supports file:// URLs.");
  10719. this._fullRequestReader = null;
  10720. this._rangeRequestReaders = [];
  10721. }
  10722. get _progressiveDataLength() {
  10723. return this._fullRequestReader?._loaded ?? 0;
  10724. }
  10725. getFullReader() {
  10726. assert(!this._fullRequestReader, "PDFNodeStream.getFullReader can only be called once.");
  10727. this._fullRequestReader = new PDFNodeStreamFsFullReader(this);
  10728. return this._fullRequestReader;
  10729. }
  10730. getRangeReader(start, end) {
  10731. if (end <= this._progressiveDataLength) {
  10732. return null;
  10733. }
  10734. const rangeReader = new PDFNodeStreamFsRangeReader(this, start, end);
  10735. this._rangeRequestReaders.push(rangeReader);
  10736. return rangeReader;
  10737. }
  10738. cancelAllRequests(reason) {
  10739. this._fullRequestReader?.cancel(reason);
  10740. for (const reader of this._rangeRequestReaders.slice(0)) {
  10741. reader.cancel(reason);
  10742. }
  10743. }
  10744. }
  10745. class PDFNodeStreamFsFullReader {
  10746. constructor(stream) {
  10747. this._url = stream.url;
  10748. this._done = false;
  10749. this._storedError = null;
  10750. this.onProgress = null;
  10751. const source = stream.source;
  10752. this._contentLength = source.length;
  10753. this._loaded = 0;
  10754. this._filename = null;
  10755. this._disableRange = source.disableRange || false;
  10756. this._rangeChunkSize = source.rangeChunkSize;
  10757. if (!this._rangeChunkSize && !this._disableRange) {
  10758. this._disableRange = true;
  10759. }
  10760. this._isStreamingSupported = !source.disableStream;
  10761. this._isRangeSupported = !source.disableRange;
  10762. this._readableStream = null;
  10763. this._readCapability = Promise.withResolvers();
  10764. this._headersCapability = Promise.withResolvers();
  10765. const fs = process.getBuiltinModule("fs");
  10766. fs.promises.lstat(this._url).then(stat => {
  10767. this._contentLength = stat.size;
  10768. this._setReadableStream(fs.createReadStream(this._url));
  10769. this._headersCapability.resolve();
  10770. }, error => {
  10771. if (error.code === "ENOENT") {
  10772. error = createResponseError(0, this._url.href);
  10773. }
  10774. this._storedError = error;
  10775. this._headersCapability.reject(error);
  10776. });
  10777. }
  10778. get headersReady() {
  10779. return this._headersCapability.promise;
  10780. }
  10781. get filename() {
  10782. return this._filename;
  10783. }
  10784. get contentLength() {
  10785. return this._contentLength;
  10786. }
  10787. get isRangeSupported() {
  10788. return this._isRangeSupported;
  10789. }
  10790. get isStreamingSupported() {
  10791. return this._isStreamingSupported;
  10792. }
  10793. async read() {
  10794. await this._readCapability.promise;
  10795. if (this._done) {
  10796. return {
  10797. value: undefined,
  10798. done: true
  10799. };
  10800. }
  10801. if (this._storedError) {
  10802. throw this._storedError;
  10803. }
  10804. const chunk = this._readableStream.read();
  10805. if (chunk === null) {
  10806. this._readCapability = Promise.withResolvers();
  10807. return this.read();
  10808. }
  10809. this._loaded += chunk.length;
  10810. this.onProgress?.({
  10811. loaded: this._loaded,
  10812. total: this._contentLength
  10813. });
  10814. const buffer = new Uint8Array(chunk).buffer;
  10815. return {
  10816. value: buffer,
  10817. done: false
  10818. };
  10819. }
  10820. cancel(reason) {
  10821. if (!this._readableStream) {
  10822. this._error(reason);
  10823. return;
  10824. }
  10825. this._readableStream.destroy(reason);
  10826. }
  10827. _error(reason) {
  10828. this._storedError = reason;
  10829. this._readCapability.resolve();
  10830. }
  10831. _setReadableStream(readableStream) {
  10832. this._readableStream = readableStream;
  10833. readableStream.on("readable", () => {
  10834. this._readCapability.resolve();
  10835. });
  10836. readableStream.on("end", () => {
  10837. readableStream.destroy();
  10838. this._done = true;
  10839. this._readCapability.resolve();
  10840. });
  10841. readableStream.on("error", reason => {
  10842. this._error(reason);
  10843. });
  10844. if (!this._isStreamingSupported && this._isRangeSupported) {
  10845. this._error(new AbortException("streaming is disabled"));
  10846. }
  10847. if (this._storedError) {
  10848. this._readableStream.destroy(this._storedError);
  10849. }
  10850. }
  10851. }
  10852. class PDFNodeStreamFsRangeReader {
  10853. constructor(stream, start, end) {
  10854. this._url = stream.url;
  10855. this._done = false;
  10856. this._storedError = null;
  10857. this.onProgress = null;
  10858. this._loaded = 0;
  10859. this._readableStream = null;
  10860. this._readCapability = Promise.withResolvers();
  10861. const source = stream.source;
  10862. this._isStreamingSupported = !source.disableStream;
  10863. const fs = process.getBuiltinModule("fs");
  10864. this._setReadableStream(fs.createReadStream(this._url, {
  10865. start,
  10866. end: end - 1
  10867. }));
  10868. }
  10869. get isStreamingSupported() {
  10870. return this._isStreamingSupported;
  10871. }
  10872. async read() {
  10873. await this._readCapability.promise;
  10874. if (this._done) {
  10875. return {
  10876. value: undefined,
  10877. done: true
  10878. };
  10879. }
  10880. if (this._storedError) {
  10881. throw this._storedError;
  10882. }
  10883. const chunk = this._readableStream.read();
  10884. if (chunk === null) {
  10885. this._readCapability = Promise.withResolvers();
  10886. return this.read();
  10887. }
  10888. this._loaded += chunk.length;
  10889. this.onProgress?.({
  10890. loaded: this._loaded
  10891. });
  10892. const buffer = new Uint8Array(chunk).buffer;
  10893. return {
  10894. value: buffer,
  10895. done: false
  10896. };
  10897. }
  10898. cancel(reason) {
  10899. if (!this._readableStream) {
  10900. this._error(reason);
  10901. return;
  10902. }
  10903. this._readableStream.destroy(reason);
  10904. }
  10905. _error(reason) {
  10906. this._storedError = reason;
  10907. this._readCapability.resolve();
  10908. }
  10909. _setReadableStream(readableStream) {
  10910. this._readableStream = readableStream;
  10911. readableStream.on("readable", () => {
  10912. this._readCapability.resolve();
  10913. });
  10914. readableStream.on("end", () => {
  10915. readableStream.destroy();
  10916. this._done = true;
  10917. this._readCapability.resolve();
  10918. });
  10919. readableStream.on("error", reason => {
  10920. this._error(reason);
  10921. });
  10922. if (this._storedError) {
  10923. this._readableStream.destroy(this._storedError);
  10924. }
  10925. }
  10926. }
  10927. ;// ./src/display/text_layer.js
  10928. const MAX_TEXT_DIVS_TO_RENDER = 100000;
  10929. const DEFAULT_FONT_SIZE = 30;
  10930. class TextLayer {
  10931. #capability = Promise.withResolvers();
  10932. #container = null;
  10933. #disableProcessItems = false;
  10934. #fontInspectorEnabled = !!globalThis.FontInspector?.enabled;
  10935. #lang = null;
  10936. #layoutTextParams = null;
  10937. #pageHeight = 0;
  10938. #pageWidth = 0;
  10939. #reader = null;
  10940. #rootContainer = null;
  10941. #rotation = 0;
  10942. #scale = 0;
  10943. #styleCache = Object.create(null);
  10944. #textContentItemsStr = [];
  10945. #textContentSource = null;
  10946. #textDivs = [];
  10947. #textDivProperties = new WeakMap();
  10948. #transform = null;
  10949. static #ascentCache = new Map();
  10950. static #canvasContexts = new Map();
  10951. static #canvasCtxFonts = new WeakMap();
  10952. static #minFontSize = null;
  10953. static #pendingTextLayers = new Set();
  10954. constructor({
  10955. textContentSource,
  10956. container,
  10957. viewport
  10958. }) {
  10959. if (textContentSource instanceof ReadableStream) {
  10960. this.#textContentSource = textContentSource;
  10961. } else if (typeof textContentSource === "object") {
  10962. this.#textContentSource = new ReadableStream({
  10963. start(controller) {
  10964. controller.enqueue(textContentSource);
  10965. controller.close();
  10966. }
  10967. });
  10968. } else {
  10969. throw new Error('No "textContentSource" parameter specified.');
  10970. }
  10971. this.#container = this.#rootContainer = container;
  10972. this.#scale = viewport.scale * OutputScale.pixelRatio;
  10973. this.#rotation = viewport.rotation;
  10974. this.#layoutTextParams = {
  10975. div: null,
  10976. properties: null,
  10977. ctx: null
  10978. };
  10979. const {
  10980. pageWidth,
  10981. pageHeight,
  10982. pageX,
  10983. pageY
  10984. } = viewport.rawDims;
  10985. this.#transform = [1, 0, 0, -1, -pageX, pageY + pageHeight];
  10986. this.#pageWidth = pageWidth;
  10987. this.#pageHeight = pageHeight;
  10988. TextLayer.#ensureMinFontSizeComputed();
  10989. setLayerDimensions(container, viewport);
  10990. this.#capability.promise.finally(() => {
  10991. TextLayer.#pendingTextLayers.delete(this);
  10992. this.#layoutTextParams = null;
  10993. this.#styleCache = null;
  10994. }).catch(() => {});
  10995. }
  10996. static get fontFamilyMap() {
  10997. const {
  10998. isWindows,
  10999. isFirefox
  11000. } = util_FeatureTest.platform;
  11001. return shadow(this, "fontFamilyMap", new Map([["sans-serif", `${isWindows && isFirefox ? "Calibri, " : ""}sans-serif`], ["monospace", `${isWindows && isFirefox ? "Lucida Console, " : ""}monospace`]]));
  11002. }
  11003. render() {
  11004. const pump = () => {
  11005. this.#reader.read().then(({
  11006. value,
  11007. done
  11008. }) => {
  11009. if (done) {
  11010. this.#capability.resolve();
  11011. return;
  11012. }
  11013. this.#lang ??= value.lang;
  11014. Object.assign(this.#styleCache, value.styles);
  11015. this.#processItems(value.items);
  11016. pump();
  11017. }, this.#capability.reject);
  11018. };
  11019. this.#reader = this.#textContentSource.getReader();
  11020. TextLayer.#pendingTextLayers.add(this);
  11021. pump();
  11022. return this.#capability.promise;
  11023. }
  11024. update({
  11025. viewport,
  11026. onBefore = null
  11027. }) {
  11028. const scale = viewport.scale * OutputScale.pixelRatio;
  11029. const rotation = viewport.rotation;
  11030. if (rotation !== this.#rotation) {
  11031. onBefore?.();
  11032. this.#rotation = rotation;
  11033. setLayerDimensions(this.#rootContainer, {
  11034. rotation
  11035. });
  11036. }
  11037. if (scale !== this.#scale) {
  11038. onBefore?.();
  11039. this.#scale = scale;
  11040. const params = {
  11041. div: null,
  11042. properties: null,
  11043. ctx: TextLayer.#getCtx(this.#lang)
  11044. };
  11045. for (const div of this.#textDivs) {
  11046. params.properties = this.#textDivProperties.get(div);
  11047. params.div = div;
  11048. this.#layout(params);
  11049. }
  11050. }
  11051. }
  11052. cancel() {
  11053. const abortEx = new AbortException("TextLayer task cancelled.");
  11054. this.#reader?.cancel(abortEx).catch(() => {});
  11055. this.#reader = null;
  11056. this.#capability.reject(abortEx);
  11057. }
  11058. get textDivs() {
  11059. return this.#textDivs;
  11060. }
  11061. get textContentItemsStr() {
  11062. return this.#textContentItemsStr;
  11063. }
  11064. #processItems(items) {
  11065. if (this.#disableProcessItems) {
  11066. return;
  11067. }
  11068. this.#layoutTextParams.ctx ??= TextLayer.#getCtx(this.#lang);
  11069. const textDivs = this.#textDivs,
  11070. textContentItemsStr = this.#textContentItemsStr;
  11071. for (const item of items) {
  11072. if (textDivs.length > MAX_TEXT_DIVS_TO_RENDER) {
  11073. warn("Ignoring additional textDivs for performance reasons.");
  11074. this.#disableProcessItems = true;
  11075. return;
  11076. }
  11077. if (item.str === undefined) {
  11078. if (item.type === "beginMarkedContentProps" || item.type === "beginMarkedContent") {
  11079. const parent = this.#container;
  11080. this.#container = document.createElement("span");
  11081. this.#container.classList.add("markedContent");
  11082. if (item.id !== null) {
  11083. this.#container.setAttribute("id", `${item.id}`);
  11084. }
  11085. parent.append(this.#container);
  11086. } else if (item.type === "endMarkedContent") {
  11087. this.#container = this.#container.parentNode;
  11088. }
  11089. continue;
  11090. }
  11091. textContentItemsStr.push(item.str);
  11092. this.#appendText(item);
  11093. }
  11094. }
  11095. #appendText(geom) {
  11096. const textDiv = document.createElement("span");
  11097. const textDivProperties = {
  11098. angle: 0,
  11099. canvasWidth: 0,
  11100. hasText: geom.str !== "",
  11101. hasEOL: geom.hasEOL,
  11102. fontSize: 0
  11103. };
  11104. this.#textDivs.push(textDiv);
  11105. const tx = Util.transform(this.#transform, geom.transform);
  11106. let angle = Math.atan2(tx[1], tx[0]);
  11107. const style = this.#styleCache[geom.fontName];
  11108. if (style.vertical) {
  11109. angle += Math.PI / 2;
  11110. }
  11111. let fontFamily = this.#fontInspectorEnabled && style.fontSubstitution || style.fontFamily;
  11112. fontFamily = TextLayer.fontFamilyMap.get(fontFamily) || fontFamily;
  11113. const fontHeight = Math.hypot(tx[2], tx[3]);
  11114. const fontAscent = fontHeight * TextLayer.#getAscent(fontFamily, style, this.#lang);
  11115. let left, top;
  11116. if (angle === 0) {
  11117. left = tx[4];
  11118. top = tx[5] - fontAscent;
  11119. } else {
  11120. left = tx[4] + fontAscent * Math.sin(angle);
  11121. top = tx[5] - fontAscent * Math.cos(angle);
  11122. }
  11123. const scaleFactorStr = "calc(var(--total-scale-factor) *";
  11124. const divStyle = textDiv.style;
  11125. if (this.#container === this.#rootContainer) {
  11126. divStyle.left = `${(100 * left / this.#pageWidth).toFixed(2)}%`;
  11127. divStyle.top = `${(100 * top / this.#pageHeight).toFixed(2)}%`;
  11128. } else {
  11129. divStyle.left = `${scaleFactorStr}${left.toFixed(2)}px)`;
  11130. divStyle.top = `${scaleFactorStr}${top.toFixed(2)}px)`;
  11131. }
  11132. divStyle.fontSize = `${scaleFactorStr}${(TextLayer.#minFontSize * fontHeight).toFixed(2)}px)`;
  11133. divStyle.fontFamily = fontFamily;
  11134. textDivProperties.fontSize = fontHeight;
  11135. textDiv.setAttribute("role", "presentation");
  11136. textDiv.textContent = geom.str;
  11137. textDiv.dir = geom.dir;
  11138. if (this.#fontInspectorEnabled) {
  11139. textDiv.dataset.fontName = style.fontSubstitutionLoadedName || geom.fontName;
  11140. }
  11141. if (angle !== 0) {
  11142. textDivProperties.angle = angle * (180 / Math.PI);
  11143. }
  11144. let shouldScaleText = false;
  11145. if (geom.str.length > 1) {
  11146. shouldScaleText = true;
  11147. } else if (geom.str !== " " && geom.transform[0] !== geom.transform[3]) {
  11148. const absScaleX = Math.abs(geom.transform[0]),
  11149. absScaleY = Math.abs(geom.transform[3]);
  11150. if (absScaleX !== absScaleY && Math.max(absScaleX, absScaleY) / Math.min(absScaleX, absScaleY) > 1.5) {
  11151. shouldScaleText = true;
  11152. }
  11153. }
  11154. if (shouldScaleText) {
  11155. textDivProperties.canvasWidth = style.vertical ? geom.height : geom.width;
  11156. }
  11157. this.#textDivProperties.set(textDiv, textDivProperties);
  11158. this.#layoutTextParams.div = textDiv;
  11159. this.#layoutTextParams.properties = textDivProperties;
  11160. this.#layout(this.#layoutTextParams);
  11161. if (textDivProperties.hasText) {
  11162. this.#container.append(textDiv);
  11163. }
  11164. if (textDivProperties.hasEOL) {
  11165. const br = document.createElement("br");
  11166. br.setAttribute("role", "presentation");
  11167. this.#container.append(br);
  11168. }
  11169. }
  11170. #layout(params) {
  11171. const {
  11172. div,
  11173. properties,
  11174. ctx
  11175. } = params;
  11176. const {
  11177. style
  11178. } = div;
  11179. let transform = "";
  11180. if (TextLayer.#minFontSize > 1) {
  11181. transform = `scale(${1 / TextLayer.#minFontSize})`;
  11182. }
  11183. if (properties.canvasWidth !== 0 && properties.hasText) {
  11184. const {
  11185. fontFamily
  11186. } = style;
  11187. const {
  11188. canvasWidth,
  11189. fontSize
  11190. } = properties;
  11191. TextLayer.#ensureCtxFont(ctx, fontSize * this.#scale, fontFamily);
  11192. const {
  11193. width
  11194. } = ctx.measureText(div.textContent);
  11195. if (width > 0) {
  11196. transform = `scaleX(${canvasWidth * this.#scale / width}) ${transform}`;
  11197. }
  11198. }
  11199. if (properties.angle !== 0) {
  11200. transform = `rotate(${properties.angle}deg) ${transform}`;
  11201. }
  11202. if (transform.length > 0) {
  11203. style.transform = transform;
  11204. }
  11205. }
  11206. static cleanup() {
  11207. if (this.#pendingTextLayers.size > 0) {
  11208. return;
  11209. }
  11210. this.#ascentCache.clear();
  11211. for (const {
  11212. canvas
  11213. } of this.#canvasContexts.values()) {
  11214. canvas.remove();
  11215. }
  11216. this.#canvasContexts.clear();
  11217. }
  11218. static #getCtx(lang = null) {
  11219. let ctx = this.#canvasContexts.get(lang ||= "");
  11220. if (!ctx) {
  11221. const canvas = document.createElement("canvas");
  11222. canvas.className = "hiddenCanvasElement";
  11223. canvas.lang = lang;
  11224. document.body.append(canvas);
  11225. ctx = canvas.getContext("2d", {
  11226. alpha: false,
  11227. willReadFrequently: true
  11228. });
  11229. this.#canvasContexts.set(lang, ctx);
  11230. this.#canvasCtxFonts.set(ctx, {
  11231. size: 0,
  11232. family: ""
  11233. });
  11234. }
  11235. return ctx;
  11236. }
  11237. static #ensureCtxFont(ctx, size, family) {
  11238. const cached = this.#canvasCtxFonts.get(ctx);
  11239. if (size === cached.size && family === cached.family) {
  11240. return;
  11241. }
  11242. ctx.font = `${size}px ${family}`;
  11243. cached.size = size;
  11244. cached.family = family;
  11245. }
  11246. static #ensureMinFontSizeComputed() {
  11247. if (this.#minFontSize !== null) {
  11248. return;
  11249. }
  11250. const div = document.createElement("div");
  11251. div.style.opacity = 0;
  11252. div.style.lineHeight = 1;
  11253. div.style.fontSize = "1px";
  11254. div.style.position = "absolute";
  11255. div.textContent = "X";
  11256. document.body.append(div);
  11257. this.#minFontSize = div.getBoundingClientRect().height;
  11258. div.remove();
  11259. }
  11260. static #getAscent(fontFamily, style, lang) {
  11261. const cachedAscent = this.#ascentCache.get(fontFamily);
  11262. if (cachedAscent) {
  11263. return cachedAscent;
  11264. }
  11265. const ctx = this.#getCtx(lang);
  11266. ctx.canvas.width = ctx.canvas.height = DEFAULT_FONT_SIZE;
  11267. this.#ensureCtxFont(ctx, DEFAULT_FONT_SIZE, fontFamily);
  11268. const metrics = ctx.measureText("");
  11269. const ascent = metrics.fontBoundingBoxAscent;
  11270. const descent = Math.abs(metrics.fontBoundingBoxDescent);
  11271. ctx.canvas.width = ctx.canvas.height = 0;
  11272. let ratio = 0.8;
  11273. if (ascent) {
  11274. ratio = ascent / (ascent + descent);
  11275. } else {
  11276. if (util_FeatureTest.platform.isFirefox) {
  11277. warn("Enable the `dom.textMetrics.fontBoundingBox.enabled` preference " + "in `about:config` to improve TextLayer rendering.");
  11278. }
  11279. if (style.ascent) {
  11280. ratio = style.ascent;
  11281. } else if (style.descent) {
  11282. ratio = 1 + style.descent;
  11283. }
  11284. }
  11285. this.#ascentCache.set(fontFamily, ratio);
  11286. return ratio;
  11287. }
  11288. }
  11289. ;// ./src/display/xfa_text.js
  11290. class XfaText {
  11291. static textContent(xfa) {
  11292. const items = [];
  11293. const output = {
  11294. items,
  11295. styles: Object.create(null)
  11296. };
  11297. function walk(node) {
  11298. if (!node) {
  11299. return;
  11300. }
  11301. let str = null;
  11302. const name = node.name;
  11303. if (name === "#text") {
  11304. str = node.value;
  11305. } else if (!XfaText.shouldBuildText(name)) {
  11306. return;
  11307. } else if (node?.attributes?.textContent) {
  11308. str = node.attributes.textContent;
  11309. } else if (node.value) {
  11310. str = node.value;
  11311. }
  11312. if (str !== null) {
  11313. items.push({
  11314. str
  11315. });
  11316. }
  11317. if (!node.children) {
  11318. return;
  11319. }
  11320. for (const child of node.children) {
  11321. walk(child);
  11322. }
  11323. }
  11324. walk(xfa);
  11325. return output;
  11326. }
  11327. static shouldBuildText(name) {
  11328. return !(name === "textarea" || name === "input" || name === "option" || name === "select");
  11329. }
  11330. }
  11331. ;// ./src/display/api.js
  11332. const DEFAULT_RANGE_CHUNK_SIZE = 65536;
  11333. const RENDERING_CANCELLED_TIMEOUT = 100;
  11334. function getDocument(src = {}) {
  11335. if (typeof src === "string" || src instanceof URL) {
  11336. src = {
  11337. url: src
  11338. };
  11339. } else if (src instanceof ArrayBuffer || ArrayBuffer.isView(src)) {
  11340. src = {
  11341. data: src
  11342. };
  11343. }
  11344. const task = new PDFDocumentLoadingTask();
  11345. const {
  11346. docId
  11347. } = task;
  11348. const url = src.url ? getUrlProp(src.url) : null;
  11349. const data = src.data ? getDataProp(src.data) : null;
  11350. const httpHeaders = src.httpHeaders || null;
  11351. const withCredentials = src.withCredentials === true;
  11352. const password = src.password ?? null;
  11353. const rangeTransport = src.range instanceof PDFDataRangeTransport ? src.range : null;
  11354. const rangeChunkSize = Number.isInteger(src.rangeChunkSize) && src.rangeChunkSize > 0 ? src.rangeChunkSize : DEFAULT_RANGE_CHUNK_SIZE;
  11355. let worker = src.worker instanceof PDFWorker ? src.worker : null;
  11356. const verbosity = src.verbosity;
  11357. const docBaseUrl = typeof src.docBaseUrl === "string" && !isDataScheme(src.docBaseUrl) ? src.docBaseUrl : null;
  11358. const cMapUrl = getFactoryUrlProp(src.cMapUrl);
  11359. const cMapPacked = src.cMapPacked !== false;
  11360. const CMapReaderFactory = src.CMapReaderFactory || (isNodeJS ? NodeCMapReaderFactory : DOMCMapReaderFactory);
  11361. const iccUrl = getFactoryUrlProp(src.iccUrl);
  11362. const standardFontDataUrl = getFactoryUrlProp(src.standardFontDataUrl);
  11363. const StandardFontDataFactory = src.StandardFontDataFactory || (isNodeJS ? NodeStandardFontDataFactory : DOMStandardFontDataFactory);
  11364. const wasmUrl = getFactoryUrlProp(src.wasmUrl);
  11365. const WasmFactory = src.WasmFactory || (isNodeJS ? NodeWasmFactory : DOMWasmFactory);
  11366. const ignoreErrors = src.stopAtErrors !== true;
  11367. const maxImageSize = Number.isInteger(src.maxImageSize) && src.maxImageSize > -1 ? src.maxImageSize : -1;
  11368. const isEvalSupported = src.isEvalSupported !== false;
  11369. const isOffscreenCanvasSupported = typeof src.isOffscreenCanvasSupported === "boolean" ? src.isOffscreenCanvasSupported : !isNodeJS;
  11370. const isImageDecoderSupported = typeof src.isImageDecoderSupported === "boolean" ? src.isImageDecoderSupported : !isNodeJS && (util_FeatureTest.platform.isFirefox || !globalThis.chrome);
  11371. const canvasMaxAreaInBytes = Number.isInteger(src.canvasMaxAreaInBytes) ? src.canvasMaxAreaInBytes : -1;
  11372. const disableFontFace = typeof src.disableFontFace === "boolean" ? src.disableFontFace : isNodeJS;
  11373. const fontExtraProperties = src.fontExtraProperties === true;
  11374. const enableXfa = src.enableXfa === true;
  11375. const ownerDocument = src.ownerDocument || globalThis.document;
  11376. const disableRange = src.disableRange === true;
  11377. const disableStream = src.disableStream === true;
  11378. const disableAutoFetch = src.disableAutoFetch === true;
  11379. const pdfBug = src.pdfBug === true;
  11380. const CanvasFactory = src.CanvasFactory || (isNodeJS ? NodeCanvasFactory : DOMCanvasFactory);
  11381. const FilterFactory = src.FilterFactory || (isNodeJS ? NodeFilterFactory : DOMFilterFactory);
  11382. const enableHWA = src.enableHWA === true;
  11383. const useWasm = src.useWasm !== false;
  11384. const length = rangeTransport ? rangeTransport.length : src.length ?? NaN;
  11385. const useSystemFonts = typeof src.useSystemFonts === "boolean" ? src.useSystemFonts : !isNodeJS && !disableFontFace;
  11386. const useWorkerFetch = typeof src.useWorkerFetch === "boolean" ? src.useWorkerFetch : !!(CMapReaderFactory === DOMCMapReaderFactory && StandardFontDataFactory === DOMStandardFontDataFactory && WasmFactory === DOMWasmFactory && cMapUrl && standardFontDataUrl && wasmUrl && isValidFetchUrl(cMapUrl, document.baseURI) && isValidFetchUrl(standardFontDataUrl, document.baseURI) && isValidFetchUrl(wasmUrl, document.baseURI));
  11387. const styleElement = null;
  11388. setVerbosityLevel(verbosity);
  11389. const transportFactory = {
  11390. canvasFactory: new CanvasFactory({
  11391. ownerDocument,
  11392. enableHWA
  11393. }),
  11394. filterFactory: new FilterFactory({
  11395. docId,
  11396. ownerDocument
  11397. }),
  11398. cMapReaderFactory: useWorkerFetch ? null : new CMapReaderFactory({
  11399. baseUrl: cMapUrl,
  11400. isCompressed: cMapPacked
  11401. }),
  11402. standardFontDataFactory: useWorkerFetch ? null : new StandardFontDataFactory({
  11403. baseUrl: standardFontDataUrl
  11404. }),
  11405. wasmFactory: useWorkerFetch ? null : new WasmFactory({
  11406. baseUrl: wasmUrl
  11407. })
  11408. };
  11409. if (!worker) {
  11410. const workerParams = {
  11411. verbosity,
  11412. port: GlobalWorkerOptions.workerPort
  11413. };
  11414. worker = workerParams.port ? PDFWorker.fromPort(workerParams) : new PDFWorker(workerParams);
  11415. task._worker = worker;
  11416. }
  11417. const docParams = {
  11418. docId,
  11419. apiVersion: "5.1.91",
  11420. data,
  11421. password,
  11422. disableAutoFetch,
  11423. rangeChunkSize,
  11424. length,
  11425. docBaseUrl,
  11426. enableXfa,
  11427. evaluatorOptions: {
  11428. maxImageSize,
  11429. disableFontFace,
  11430. ignoreErrors,
  11431. isEvalSupported,
  11432. isOffscreenCanvasSupported,
  11433. isImageDecoderSupported,
  11434. canvasMaxAreaInBytes,
  11435. fontExtraProperties,
  11436. useSystemFonts,
  11437. useWasm,
  11438. useWorkerFetch,
  11439. cMapUrl,
  11440. iccUrl,
  11441. standardFontDataUrl,
  11442. wasmUrl
  11443. }
  11444. };
  11445. const transportParams = {
  11446. ownerDocument,
  11447. pdfBug,
  11448. styleElement,
  11449. loadingParams: {
  11450. disableAutoFetch,
  11451. enableXfa
  11452. }
  11453. };
  11454. worker.promise.then(function () {
  11455. if (task.destroyed) {
  11456. throw new Error("Loading aborted");
  11457. }
  11458. if (worker.destroyed) {
  11459. throw new Error("Worker was destroyed");
  11460. }
  11461. const workerIdPromise = worker.messageHandler.sendWithPromise("GetDocRequest", docParams, data ? [data.buffer] : null);
  11462. let networkStream;
  11463. if (rangeTransport) {
  11464. networkStream = new PDFDataTransportStream(rangeTransport, {
  11465. disableRange,
  11466. disableStream
  11467. });
  11468. } else if (!data) {
  11469. if (!url) {
  11470. throw new Error("getDocument - no `url` parameter provided.");
  11471. }
  11472. let NetworkStream;
  11473. if (isNodeJS) {
  11474. if (isValidFetchUrl(url)) {
  11475. if (typeof fetch === "undefined" || typeof Response === "undefined" || !("body" in Response.prototype)) {
  11476. throw new Error("getDocument - the Fetch API was disabled in Node.js, see `--no-experimental-fetch`.");
  11477. }
  11478. NetworkStream = PDFFetchStream;
  11479. } else {
  11480. NetworkStream = PDFNodeStream;
  11481. }
  11482. } else {
  11483. NetworkStream = isValidFetchUrl(url) ? PDFFetchStream : PDFNetworkStream;
  11484. }
  11485. networkStream = new NetworkStream({
  11486. url,
  11487. length,
  11488. httpHeaders,
  11489. withCredentials,
  11490. rangeChunkSize,
  11491. disableRange,
  11492. disableStream
  11493. });
  11494. }
  11495. return workerIdPromise.then(workerId => {
  11496. if (task.destroyed) {
  11497. throw new Error("Loading aborted");
  11498. }
  11499. if (worker.destroyed) {
  11500. throw new Error("Worker was destroyed");
  11501. }
  11502. const messageHandler = new MessageHandler(docId, workerId, worker.port);
  11503. const transport = new WorkerTransport(messageHandler, task, networkStream, transportParams, transportFactory);
  11504. task._transport = transport;
  11505. messageHandler.send("Ready", null);
  11506. });
  11507. }).catch(task._capability.reject);
  11508. return task;
  11509. }
  11510. function getUrlProp(val) {
  11511. if (val instanceof URL) {
  11512. return val.href;
  11513. }
  11514. if (typeof val === "string") {
  11515. if (isNodeJS) {
  11516. return val;
  11517. }
  11518. const url = URL.parse(val, window.location);
  11519. if (url) {
  11520. return url.href;
  11521. }
  11522. }
  11523. throw new Error("Invalid PDF url data: " + "either string or URL-object is expected in the url property.");
  11524. }
  11525. function getDataProp(val) {
  11526. if (isNodeJS && typeof Buffer !== "undefined" && val instanceof Buffer) {
  11527. throw new Error("Please provide binary data as `Uint8Array`, rather than `Buffer`.");
  11528. }
  11529. if (val instanceof Uint8Array && val.byteLength === val.buffer.byteLength) {
  11530. return val;
  11531. }
  11532. if (typeof val === "string") {
  11533. return stringToBytes(val);
  11534. }
  11535. if (val instanceof ArrayBuffer || ArrayBuffer.isView(val) || typeof val === "object" && !isNaN(val?.length)) {
  11536. return new Uint8Array(val);
  11537. }
  11538. throw new Error("Invalid PDF binary data: either TypedArray, " + "string, or array-like object is expected in the data property.");
  11539. }
  11540. function getFactoryUrlProp(val) {
  11541. if (typeof val !== "string") {
  11542. return null;
  11543. }
  11544. if (val.endsWith("/")) {
  11545. return val;
  11546. }
  11547. throw new Error(`Invalid factory url: "${val}" must include trailing slash.`);
  11548. }
  11549. const isRefProxy = v => typeof v === "object" && Number.isInteger(v?.num) && v.num >= 0 && Number.isInteger(v?.gen) && v.gen >= 0;
  11550. const isNameProxy = v => typeof v === "object" && typeof v?.name === "string";
  11551. const isValidExplicitDest = _isValidExplicitDest.bind(null, isRefProxy, isNameProxy);
  11552. class PDFDocumentLoadingTask {
  11553. static #docId = 0;
  11554. _capability = Promise.withResolvers();
  11555. _transport = null;
  11556. _worker = null;
  11557. docId = `d${PDFDocumentLoadingTask.#docId++}`;
  11558. destroyed = false;
  11559. onPassword = null;
  11560. onProgress = null;
  11561. get promise() {
  11562. return this._capability.promise;
  11563. }
  11564. async destroy() {
  11565. this.destroyed = true;
  11566. try {
  11567. if (this._worker?.port) {
  11568. this._worker._pendingDestroy = true;
  11569. }
  11570. await this._transport?.destroy();
  11571. } catch (ex) {
  11572. if (this._worker?.port) {
  11573. delete this._worker._pendingDestroy;
  11574. }
  11575. throw ex;
  11576. }
  11577. this._transport = null;
  11578. this._worker?.destroy();
  11579. this._worker = null;
  11580. }
  11581. async getData() {
  11582. return this._transport.getData();
  11583. }
  11584. }
  11585. class PDFDataRangeTransport {
  11586. constructor(length, initialData, progressiveDone = false, contentDispositionFilename = null) {
  11587. this.length = length;
  11588. this.initialData = initialData;
  11589. this.progressiveDone = progressiveDone;
  11590. this.contentDispositionFilename = contentDispositionFilename;
  11591. this._rangeListeners = [];
  11592. this._progressListeners = [];
  11593. this._progressiveReadListeners = [];
  11594. this._progressiveDoneListeners = [];
  11595. this._readyCapability = Promise.withResolvers();
  11596. }
  11597. addRangeListener(listener) {
  11598. this._rangeListeners.push(listener);
  11599. }
  11600. addProgressListener(listener) {
  11601. this._progressListeners.push(listener);
  11602. }
  11603. addProgressiveReadListener(listener) {
  11604. this._progressiveReadListeners.push(listener);
  11605. }
  11606. addProgressiveDoneListener(listener) {
  11607. this._progressiveDoneListeners.push(listener);
  11608. }
  11609. onDataRange(begin, chunk) {
  11610. for (const listener of this._rangeListeners) {
  11611. listener(begin, chunk);
  11612. }
  11613. }
  11614. onDataProgress(loaded, total) {
  11615. this._readyCapability.promise.then(() => {
  11616. for (const listener of this._progressListeners) {
  11617. listener(loaded, total);
  11618. }
  11619. });
  11620. }
  11621. onDataProgressiveRead(chunk) {
  11622. this._readyCapability.promise.then(() => {
  11623. for (const listener of this._progressiveReadListeners) {
  11624. listener(chunk);
  11625. }
  11626. });
  11627. }
  11628. onDataProgressiveDone() {
  11629. this._readyCapability.promise.then(() => {
  11630. for (const listener of this._progressiveDoneListeners) {
  11631. listener();
  11632. }
  11633. });
  11634. }
  11635. transportReady() {
  11636. this._readyCapability.resolve();
  11637. }
  11638. requestDataRange(begin, end) {
  11639. unreachable("Abstract method PDFDataRangeTransport.requestDataRange");
  11640. }
  11641. abort() {}
  11642. }
  11643. class PDFDocumentProxy {
  11644. constructor(pdfInfo, transport) {
  11645. this._pdfInfo = pdfInfo;
  11646. this._transport = transport;
  11647. }
  11648. get annotationStorage() {
  11649. return this._transport.annotationStorage;
  11650. }
  11651. get canvasFactory() {
  11652. return this._transport.canvasFactory;
  11653. }
  11654. get filterFactory() {
  11655. return this._transport.filterFactory;
  11656. }
  11657. get numPages() {
  11658. return this._pdfInfo.numPages;
  11659. }
  11660. get fingerprints() {
  11661. return this._pdfInfo.fingerprints;
  11662. }
  11663. get isPureXfa() {
  11664. return shadow(this, "isPureXfa", !!this._transport._htmlForXfa);
  11665. }
  11666. get allXfaHtml() {
  11667. return this._transport._htmlForXfa;
  11668. }
  11669. getPage(pageNumber) {
  11670. return this._transport.getPage(pageNumber);
  11671. }
  11672. getPageIndex(ref) {
  11673. return this._transport.getPageIndex(ref);
  11674. }
  11675. getDestinations() {
  11676. return this._transport.getDestinations();
  11677. }
  11678. getDestination(id) {
  11679. return this._transport.getDestination(id);
  11680. }
  11681. getPageLabels() {
  11682. return this._transport.getPageLabels();
  11683. }
  11684. getPageLayout() {
  11685. return this._transport.getPageLayout();
  11686. }
  11687. getPageMode() {
  11688. return this._transport.getPageMode();
  11689. }
  11690. getViewerPreferences() {
  11691. return this._transport.getViewerPreferences();
  11692. }
  11693. getOpenAction() {
  11694. return this._transport.getOpenAction();
  11695. }
  11696. getAttachments() {
  11697. return this._transport.getAttachments();
  11698. }
  11699. getJSActions() {
  11700. return this._transport.getDocJSActions();
  11701. }
  11702. getOutline() {
  11703. return this._transport.getOutline();
  11704. }
  11705. getOptionalContentConfig({
  11706. intent = "display"
  11707. } = {}) {
  11708. const {
  11709. renderingIntent
  11710. } = this._transport.getRenderingIntent(intent);
  11711. return this._transport.getOptionalContentConfig(renderingIntent);
  11712. }
  11713. getPermissions() {
  11714. return this._transport.getPermissions();
  11715. }
  11716. getMetadata() {
  11717. return this._transport.getMetadata();
  11718. }
  11719. getMarkInfo() {
  11720. return this._transport.getMarkInfo();
  11721. }
  11722. getData() {
  11723. return this._transport.getData();
  11724. }
  11725. saveDocument() {
  11726. return this._transport.saveDocument();
  11727. }
  11728. getDownloadInfo() {
  11729. return this._transport.downloadInfoCapability.promise;
  11730. }
  11731. cleanup(keepLoadedFonts = false) {
  11732. return this._transport.startCleanup(keepLoadedFonts || this.isPureXfa);
  11733. }
  11734. destroy() {
  11735. return this.loadingTask.destroy();
  11736. }
  11737. cachedPageNumber(ref) {
  11738. return this._transport.cachedPageNumber(ref);
  11739. }
  11740. get loadingParams() {
  11741. return this._transport.loadingParams;
  11742. }
  11743. get loadingTask() {
  11744. return this._transport.loadingTask;
  11745. }
  11746. getFieldObjects() {
  11747. return this._transport.getFieldObjects();
  11748. }
  11749. hasJSActions() {
  11750. return this._transport.hasJSActions();
  11751. }
  11752. getCalculationOrderIds() {
  11753. return this._transport.getCalculationOrderIds();
  11754. }
  11755. }
  11756. class PDFPageProxy {
  11757. #pendingCleanup = false;
  11758. constructor(pageIndex, pageInfo, transport, pdfBug = false) {
  11759. this._pageIndex = pageIndex;
  11760. this._pageInfo = pageInfo;
  11761. this._transport = transport;
  11762. this._stats = pdfBug ? new StatTimer() : null;
  11763. this._pdfBug = pdfBug;
  11764. this.commonObjs = transport.commonObjs;
  11765. this.objs = new PDFObjects();
  11766. this._intentStates = new Map();
  11767. this.destroyed = false;
  11768. }
  11769. get pageNumber() {
  11770. return this._pageIndex + 1;
  11771. }
  11772. get rotate() {
  11773. return this._pageInfo.rotate;
  11774. }
  11775. get ref() {
  11776. return this._pageInfo.ref;
  11777. }
  11778. get userUnit() {
  11779. return this._pageInfo.userUnit;
  11780. }
  11781. get view() {
  11782. return this._pageInfo.view;
  11783. }
  11784. getViewport({
  11785. scale,
  11786. rotation = this.rotate,
  11787. offsetX = 0,
  11788. offsetY = 0,
  11789. dontFlip = false
  11790. } = {}) {
  11791. return new PageViewport({
  11792. viewBox: this.view,
  11793. userUnit: this.userUnit,
  11794. scale,
  11795. rotation,
  11796. offsetX,
  11797. offsetY,
  11798. dontFlip
  11799. });
  11800. }
  11801. getAnnotations({
  11802. intent = "display"
  11803. } = {}) {
  11804. const {
  11805. renderingIntent
  11806. } = this._transport.getRenderingIntent(intent);
  11807. return this._transport.getAnnotations(this._pageIndex, renderingIntent);
  11808. }
  11809. getJSActions() {
  11810. return this._transport.getPageJSActions(this._pageIndex);
  11811. }
  11812. get filterFactory() {
  11813. return this._transport.filterFactory;
  11814. }
  11815. get isPureXfa() {
  11816. return shadow(this, "isPureXfa", !!this._transport._htmlForXfa);
  11817. }
  11818. async getXfa() {
  11819. return this._transport._htmlForXfa?.children[this._pageIndex] || null;
  11820. }
  11821. render({
  11822. canvasContext,
  11823. viewport,
  11824. intent = "display",
  11825. annotationMode = AnnotationMode.ENABLE,
  11826. transform = null,
  11827. background = null,
  11828. optionalContentConfigPromise = null,
  11829. annotationCanvasMap = null,
  11830. pageColors = null,
  11831. printAnnotationStorage = null,
  11832. isEditing = false
  11833. }) {
  11834. this._stats?.time("Overall");
  11835. const intentArgs = this._transport.getRenderingIntent(intent, annotationMode, printAnnotationStorage, isEditing);
  11836. const {
  11837. renderingIntent,
  11838. cacheKey
  11839. } = intentArgs;
  11840. this.#pendingCleanup = false;
  11841. optionalContentConfigPromise ||= this._transport.getOptionalContentConfig(renderingIntent);
  11842. let intentState = this._intentStates.get(cacheKey);
  11843. if (!intentState) {
  11844. intentState = Object.create(null);
  11845. this._intentStates.set(cacheKey, intentState);
  11846. }
  11847. if (intentState.streamReaderCancelTimeout) {
  11848. clearTimeout(intentState.streamReaderCancelTimeout);
  11849. intentState.streamReaderCancelTimeout = null;
  11850. }
  11851. const intentPrint = !!(renderingIntent & RenderingIntentFlag.PRINT);
  11852. if (!intentState.displayReadyCapability) {
  11853. intentState.displayReadyCapability = Promise.withResolvers();
  11854. intentState.operatorList = {
  11855. fnArray: [],
  11856. argsArray: [],
  11857. lastChunk: false,
  11858. separateAnnots: null
  11859. };
  11860. this._stats?.time("Page Request");
  11861. this._pumpOperatorList(intentArgs);
  11862. }
  11863. const complete = error => {
  11864. intentState.renderTasks.delete(internalRenderTask);
  11865. if (intentPrint) {
  11866. this.#pendingCleanup = true;
  11867. }
  11868. this.#tryCleanup();
  11869. if (error) {
  11870. internalRenderTask.capability.reject(error);
  11871. this._abortOperatorList({
  11872. intentState,
  11873. reason: error instanceof Error ? error : new Error(error)
  11874. });
  11875. } else {
  11876. internalRenderTask.capability.resolve();
  11877. }
  11878. if (this._stats) {
  11879. this._stats.timeEnd("Rendering");
  11880. this._stats.timeEnd("Overall");
  11881. if (globalThis.Stats?.enabled) {
  11882. globalThis.Stats.add(this.pageNumber, this._stats);
  11883. }
  11884. }
  11885. };
  11886. const internalRenderTask = new InternalRenderTask({
  11887. callback: complete,
  11888. params: {
  11889. canvasContext,
  11890. viewport,
  11891. transform,
  11892. background
  11893. },
  11894. objs: this.objs,
  11895. commonObjs: this.commonObjs,
  11896. annotationCanvasMap,
  11897. operatorList: intentState.operatorList,
  11898. pageIndex: this._pageIndex,
  11899. canvasFactory: this._transport.canvasFactory,
  11900. filterFactory: this._transport.filterFactory,
  11901. useRequestAnimationFrame: !intentPrint,
  11902. pdfBug: this._pdfBug,
  11903. pageColors
  11904. });
  11905. (intentState.renderTasks ||= new Set()).add(internalRenderTask);
  11906. const renderTask = internalRenderTask.task;
  11907. Promise.all([intentState.displayReadyCapability.promise, optionalContentConfigPromise]).then(([transparency, optionalContentConfig]) => {
  11908. if (this.destroyed) {
  11909. complete();
  11910. return;
  11911. }
  11912. this._stats?.time("Rendering");
  11913. if (!(optionalContentConfig.renderingIntent & renderingIntent)) {
  11914. throw new Error("Must use the same `intent`-argument when calling the `PDFPageProxy.render` " + "and `PDFDocumentProxy.getOptionalContentConfig` methods.");
  11915. }
  11916. internalRenderTask.initializeGraphics({
  11917. transparency,
  11918. optionalContentConfig
  11919. });
  11920. internalRenderTask.operatorListChanged();
  11921. }).catch(complete);
  11922. return renderTask;
  11923. }
  11924. getOperatorList({
  11925. intent = "display",
  11926. annotationMode = AnnotationMode.ENABLE,
  11927. printAnnotationStorage = null,
  11928. isEditing = false
  11929. } = {}) {
  11930. function operatorListChanged() {
  11931. if (intentState.operatorList.lastChunk) {
  11932. intentState.opListReadCapability.resolve(intentState.operatorList);
  11933. intentState.renderTasks.delete(opListTask);
  11934. }
  11935. }
  11936. const intentArgs = this._transport.getRenderingIntent(intent, annotationMode, printAnnotationStorage, isEditing, true);
  11937. let intentState = this._intentStates.get(intentArgs.cacheKey);
  11938. if (!intentState) {
  11939. intentState = Object.create(null);
  11940. this._intentStates.set(intentArgs.cacheKey, intentState);
  11941. }
  11942. let opListTask;
  11943. if (!intentState.opListReadCapability) {
  11944. opListTask = Object.create(null);
  11945. opListTask.operatorListChanged = operatorListChanged;
  11946. intentState.opListReadCapability = Promise.withResolvers();
  11947. (intentState.renderTasks ||= new Set()).add(opListTask);
  11948. intentState.operatorList = {
  11949. fnArray: [],
  11950. argsArray: [],
  11951. lastChunk: false,
  11952. separateAnnots: null
  11953. };
  11954. this._stats?.time("Page Request");
  11955. this._pumpOperatorList(intentArgs);
  11956. }
  11957. return intentState.opListReadCapability.promise;
  11958. }
  11959. streamTextContent({
  11960. includeMarkedContent = false,
  11961. disableNormalization = false
  11962. } = {}) {
  11963. const TEXT_CONTENT_CHUNK_SIZE = 100;
  11964. return this._transport.messageHandler.sendWithStream("GetTextContent", {
  11965. pageIndex: this._pageIndex,
  11966. includeMarkedContent: includeMarkedContent === true,
  11967. disableNormalization: disableNormalization === true
  11968. }, {
  11969. highWaterMark: TEXT_CONTENT_CHUNK_SIZE,
  11970. size(textContent) {
  11971. return textContent.items.length;
  11972. }
  11973. });
  11974. }
  11975. getTextContent(params = {}) {
  11976. if (this._transport._htmlForXfa) {
  11977. return this.getXfa().then(xfa => XfaText.textContent(xfa));
  11978. }
  11979. const readableStream = this.streamTextContent(params);
  11980. return new Promise(function (resolve, reject) {
  11981. function pump() {
  11982. reader.read().then(function ({
  11983. value,
  11984. done
  11985. }) {
  11986. if (done) {
  11987. resolve(textContent);
  11988. return;
  11989. }
  11990. textContent.lang ??= value.lang;
  11991. Object.assign(textContent.styles, value.styles);
  11992. textContent.items.push(...value.items);
  11993. pump();
  11994. }, reject);
  11995. }
  11996. const reader = readableStream.getReader();
  11997. const textContent = {
  11998. items: [],
  11999. styles: Object.create(null),
  12000. lang: null
  12001. };
  12002. pump();
  12003. });
  12004. }
  12005. getStructTree() {
  12006. return this._transport.getStructTree(this._pageIndex);
  12007. }
  12008. _destroy() {
  12009. this.destroyed = true;
  12010. const waitOn = [];
  12011. for (const intentState of this._intentStates.values()) {
  12012. this._abortOperatorList({
  12013. intentState,
  12014. reason: new Error("Page was destroyed."),
  12015. force: true
  12016. });
  12017. if (intentState.opListReadCapability) {
  12018. continue;
  12019. }
  12020. for (const internalRenderTask of intentState.renderTasks) {
  12021. waitOn.push(internalRenderTask.completed);
  12022. internalRenderTask.cancel();
  12023. }
  12024. }
  12025. this.objs.clear();
  12026. this.#pendingCleanup = false;
  12027. return Promise.all(waitOn);
  12028. }
  12029. cleanup(resetStats = false) {
  12030. this.#pendingCleanup = true;
  12031. const success = this.#tryCleanup();
  12032. if (resetStats && success) {
  12033. this._stats &&= new StatTimer();
  12034. }
  12035. return success;
  12036. }
  12037. #tryCleanup() {
  12038. if (!this.#pendingCleanup || this.destroyed) {
  12039. return false;
  12040. }
  12041. for (const {
  12042. renderTasks,
  12043. operatorList
  12044. } of this._intentStates.values()) {
  12045. if (renderTasks.size > 0 || !operatorList.lastChunk) {
  12046. return false;
  12047. }
  12048. }
  12049. this._intentStates.clear();
  12050. this.objs.clear();
  12051. this.#pendingCleanup = false;
  12052. return true;
  12053. }
  12054. _startRenderPage(transparency, cacheKey) {
  12055. const intentState = this._intentStates.get(cacheKey);
  12056. if (!intentState) {
  12057. return;
  12058. }
  12059. this._stats?.timeEnd("Page Request");
  12060. intentState.displayReadyCapability?.resolve(transparency);
  12061. }
  12062. _renderPageChunk(operatorListChunk, intentState) {
  12063. for (let i = 0, ii = operatorListChunk.length; i < ii; i++) {
  12064. intentState.operatorList.fnArray.push(operatorListChunk.fnArray[i]);
  12065. intentState.operatorList.argsArray.push(operatorListChunk.argsArray[i]);
  12066. }
  12067. intentState.operatorList.lastChunk = operatorListChunk.lastChunk;
  12068. intentState.operatorList.separateAnnots = operatorListChunk.separateAnnots;
  12069. for (const internalRenderTask of intentState.renderTasks) {
  12070. internalRenderTask.operatorListChanged();
  12071. }
  12072. if (operatorListChunk.lastChunk) {
  12073. this.#tryCleanup();
  12074. }
  12075. }
  12076. _pumpOperatorList({
  12077. renderingIntent,
  12078. cacheKey,
  12079. annotationStorageSerializable,
  12080. modifiedIds
  12081. }) {
  12082. const {
  12083. map,
  12084. transfer
  12085. } = annotationStorageSerializable;
  12086. const readableStream = this._transport.messageHandler.sendWithStream("GetOperatorList", {
  12087. pageIndex: this._pageIndex,
  12088. intent: renderingIntent,
  12089. cacheKey,
  12090. annotationStorage: map,
  12091. modifiedIds
  12092. }, transfer);
  12093. const reader = readableStream.getReader();
  12094. const intentState = this._intentStates.get(cacheKey);
  12095. intentState.streamReader = reader;
  12096. const pump = () => {
  12097. reader.read().then(({
  12098. value,
  12099. done
  12100. }) => {
  12101. if (done) {
  12102. intentState.streamReader = null;
  12103. return;
  12104. }
  12105. if (this._transport.destroyed) {
  12106. return;
  12107. }
  12108. this._renderPageChunk(value, intentState);
  12109. pump();
  12110. }, reason => {
  12111. intentState.streamReader = null;
  12112. if (this._transport.destroyed) {
  12113. return;
  12114. }
  12115. if (intentState.operatorList) {
  12116. intentState.operatorList.lastChunk = true;
  12117. for (const internalRenderTask of intentState.renderTasks) {
  12118. internalRenderTask.operatorListChanged();
  12119. }
  12120. this.#tryCleanup();
  12121. }
  12122. if (intentState.displayReadyCapability) {
  12123. intentState.displayReadyCapability.reject(reason);
  12124. } else if (intentState.opListReadCapability) {
  12125. intentState.opListReadCapability.reject(reason);
  12126. } else {
  12127. throw reason;
  12128. }
  12129. });
  12130. };
  12131. pump();
  12132. }
  12133. _abortOperatorList({
  12134. intentState,
  12135. reason,
  12136. force = false
  12137. }) {
  12138. if (!intentState.streamReader) {
  12139. return;
  12140. }
  12141. if (intentState.streamReaderCancelTimeout) {
  12142. clearTimeout(intentState.streamReaderCancelTimeout);
  12143. intentState.streamReaderCancelTimeout = null;
  12144. }
  12145. if (!force) {
  12146. if (intentState.renderTasks.size > 0) {
  12147. return;
  12148. }
  12149. if (reason instanceof RenderingCancelledException) {
  12150. let delay = RENDERING_CANCELLED_TIMEOUT;
  12151. if (reason.extraDelay > 0 && reason.extraDelay < 1000) {
  12152. delay += reason.extraDelay;
  12153. }
  12154. intentState.streamReaderCancelTimeout = setTimeout(() => {
  12155. intentState.streamReaderCancelTimeout = null;
  12156. this._abortOperatorList({
  12157. intentState,
  12158. reason,
  12159. force: true
  12160. });
  12161. }, delay);
  12162. return;
  12163. }
  12164. }
  12165. intentState.streamReader.cancel(new AbortException(reason.message)).catch(() => {});
  12166. intentState.streamReader = null;
  12167. if (this._transport.destroyed) {
  12168. return;
  12169. }
  12170. for (const [curCacheKey, curIntentState] of this._intentStates) {
  12171. if (curIntentState === intentState) {
  12172. this._intentStates.delete(curCacheKey);
  12173. break;
  12174. }
  12175. }
  12176. this.cleanup();
  12177. }
  12178. get stats() {
  12179. return this._stats;
  12180. }
  12181. }
  12182. class LoopbackPort {
  12183. #listeners = new Map();
  12184. #deferred = Promise.resolve();
  12185. postMessage(obj, transfer) {
  12186. const event = {
  12187. data: structuredClone(obj, transfer ? {
  12188. transfer
  12189. } : null)
  12190. };
  12191. this.#deferred.then(() => {
  12192. for (const [listener] of this.#listeners) {
  12193. listener.call(this, event);
  12194. }
  12195. });
  12196. }
  12197. addEventListener(name, listener, options = null) {
  12198. let rmAbort = null;
  12199. if (options?.signal instanceof AbortSignal) {
  12200. const {
  12201. signal
  12202. } = options;
  12203. if (signal.aborted) {
  12204. warn("LoopbackPort - cannot use an `aborted` signal.");
  12205. return;
  12206. }
  12207. const onAbort = () => this.removeEventListener(name, listener);
  12208. rmAbort = () => signal.removeEventListener("abort", onAbort);
  12209. signal.addEventListener("abort", onAbort);
  12210. }
  12211. this.#listeners.set(listener, rmAbort);
  12212. }
  12213. removeEventListener(name, listener) {
  12214. const rmAbort = this.#listeners.get(listener);
  12215. rmAbort?.();
  12216. this.#listeners.delete(listener);
  12217. }
  12218. terminate() {
  12219. for (const [, rmAbort] of this.#listeners) {
  12220. rmAbort?.();
  12221. }
  12222. this.#listeners.clear();
  12223. }
  12224. }
  12225. class PDFWorker {
  12226. static #fakeWorkerId = 0;
  12227. static #isWorkerDisabled = false;
  12228. static #workerPorts;
  12229. static {
  12230. if (isNodeJS) {
  12231. this.#isWorkerDisabled = true;
  12232. GlobalWorkerOptions.workerSrc ||= "./pdf.worker.mjs";
  12233. }
  12234. this._isSameOrigin = (baseUrl, otherUrl) => {
  12235. const base = URL.parse(baseUrl);
  12236. if (!base?.origin || base.origin === "null") {
  12237. return false;
  12238. }
  12239. const other = new URL(otherUrl, base);
  12240. return base.origin === other.origin;
  12241. };
  12242. this._createCDNWrapper = url => {
  12243. const wrapper = `await import("${url}");`;
  12244. return URL.createObjectURL(new Blob([wrapper], {
  12245. type: "text/javascript"
  12246. }));
  12247. };
  12248. }
  12249. constructor({
  12250. name = null,
  12251. port = null,
  12252. verbosity = getVerbosityLevel()
  12253. } = {}) {
  12254. this.name = name;
  12255. this.destroyed = false;
  12256. this.verbosity = verbosity;
  12257. this._readyCapability = Promise.withResolvers();
  12258. this._port = null;
  12259. this._webWorker = null;
  12260. this._messageHandler = null;
  12261. if (port) {
  12262. if (PDFWorker.#workerPorts?.has(port)) {
  12263. throw new Error("Cannot use more than one PDFWorker per port.");
  12264. }
  12265. (PDFWorker.#workerPorts ||= new WeakMap()).set(port, this);
  12266. this._initializeFromPort(port);
  12267. return;
  12268. }
  12269. this._initialize();
  12270. }
  12271. get promise() {
  12272. return this._readyCapability.promise;
  12273. }
  12274. #resolve() {
  12275. this._readyCapability.resolve();
  12276. this._messageHandler.send("configure", {
  12277. verbosity: this.verbosity
  12278. });
  12279. }
  12280. get port() {
  12281. return this._port;
  12282. }
  12283. get messageHandler() {
  12284. return this._messageHandler;
  12285. }
  12286. _initializeFromPort(port) {
  12287. this._port = port;
  12288. this._messageHandler = new MessageHandler("main", "worker", port);
  12289. this._messageHandler.on("ready", function () {});
  12290. this.#resolve();
  12291. }
  12292. _initialize() {
  12293. if (PDFWorker.#isWorkerDisabled || PDFWorker.#mainThreadWorkerMessageHandler) {
  12294. this._setupFakeWorker();
  12295. return;
  12296. }
  12297. let {
  12298. workerSrc
  12299. } = PDFWorker;
  12300. try {
  12301. if (!PDFWorker._isSameOrigin(window.location, workerSrc)) {
  12302. workerSrc = PDFWorker._createCDNWrapper(new URL(workerSrc, window.location).href);
  12303. }
  12304. const worker = new Worker(workerSrc, {
  12305. type: "module"
  12306. });
  12307. const messageHandler = new MessageHandler("main", "worker", worker);
  12308. const terminateEarly = () => {
  12309. ac.abort();
  12310. messageHandler.destroy();
  12311. worker.terminate();
  12312. if (this.destroyed) {
  12313. this._readyCapability.reject(new Error("Worker was destroyed"));
  12314. } else {
  12315. this._setupFakeWorker();
  12316. }
  12317. };
  12318. const ac = new AbortController();
  12319. worker.addEventListener("error", () => {
  12320. if (!this._webWorker) {
  12321. terminateEarly();
  12322. }
  12323. }, {
  12324. signal: ac.signal
  12325. });
  12326. messageHandler.on("test", data => {
  12327. ac.abort();
  12328. if (this.destroyed || !data) {
  12329. terminateEarly();
  12330. return;
  12331. }
  12332. this._messageHandler = messageHandler;
  12333. this._port = worker;
  12334. this._webWorker = worker;
  12335. this.#resolve();
  12336. });
  12337. messageHandler.on("ready", data => {
  12338. ac.abort();
  12339. if (this.destroyed) {
  12340. terminateEarly();
  12341. return;
  12342. }
  12343. try {
  12344. sendTest();
  12345. } catch {
  12346. this._setupFakeWorker();
  12347. }
  12348. });
  12349. const sendTest = () => {
  12350. const testObj = new Uint8Array();
  12351. messageHandler.send("test", testObj, [testObj.buffer]);
  12352. };
  12353. sendTest();
  12354. return;
  12355. } catch {
  12356. info("The worker has been disabled.");
  12357. }
  12358. this._setupFakeWorker();
  12359. }
  12360. _setupFakeWorker() {
  12361. if (!PDFWorker.#isWorkerDisabled) {
  12362. warn("Setting up fake worker.");
  12363. PDFWorker.#isWorkerDisabled = true;
  12364. }
  12365. PDFWorker._setupFakeWorkerGlobal.then(WorkerMessageHandler => {
  12366. if (this.destroyed) {
  12367. this._readyCapability.reject(new Error("Worker was destroyed"));
  12368. return;
  12369. }
  12370. const port = new LoopbackPort();
  12371. this._port = port;
  12372. const id = `fake${PDFWorker.#fakeWorkerId++}`;
  12373. const workerHandler = new MessageHandler(id + "_worker", id, port);
  12374. WorkerMessageHandler.setup(workerHandler, port);
  12375. this._messageHandler = new MessageHandler(id, id + "_worker", port);
  12376. this.#resolve();
  12377. }).catch(reason => {
  12378. this._readyCapability.reject(new Error(`Setting up fake worker failed: "${reason.message}".`));
  12379. });
  12380. }
  12381. destroy() {
  12382. this.destroyed = true;
  12383. this._webWorker?.terminate();
  12384. this._webWorker = null;
  12385. PDFWorker.#workerPorts?.delete(this._port);
  12386. this._port = null;
  12387. this._messageHandler?.destroy();
  12388. this._messageHandler = null;
  12389. }
  12390. static fromPort(params) {
  12391. if (!params?.port) {
  12392. throw new Error("PDFWorker.fromPort - invalid method signature.");
  12393. }
  12394. const cachedPort = this.#workerPorts?.get(params.port);
  12395. if (cachedPort) {
  12396. if (cachedPort._pendingDestroy) {
  12397. throw new Error("PDFWorker.fromPort - the worker is being destroyed.\n" + "Please remember to await `PDFDocumentLoadingTask.destroy()`-calls.");
  12398. }
  12399. return cachedPort;
  12400. }
  12401. return new PDFWorker(params);
  12402. }
  12403. static get workerSrc() {
  12404. if (GlobalWorkerOptions.workerSrc) {
  12405. return GlobalWorkerOptions.workerSrc;
  12406. }
  12407. throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
  12408. }
  12409. static get #mainThreadWorkerMessageHandler() {
  12410. try {
  12411. return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
  12412. } catch {
  12413. return null;
  12414. }
  12415. }
  12416. static get _setupFakeWorkerGlobal() {
  12417. const loader = async () => {
  12418. if (this.#mainThreadWorkerMessageHandler) {
  12419. return this.#mainThreadWorkerMessageHandler;
  12420. }
  12421. const worker = await import(
  12422. /*webpackIgnore: true*/
  12423. /*@vite-ignore*/
  12424. this.workerSrc);
  12425. return worker.WorkerMessageHandler;
  12426. };
  12427. return shadow(this, "_setupFakeWorkerGlobal", loader());
  12428. }
  12429. }
  12430. class WorkerTransport {
  12431. #methodPromises = new Map();
  12432. #pageCache = new Map();
  12433. #pagePromises = new Map();
  12434. #pageRefCache = new Map();
  12435. #passwordCapability = null;
  12436. constructor(messageHandler, loadingTask, networkStream, params, factory) {
  12437. this.messageHandler = messageHandler;
  12438. this.loadingTask = loadingTask;
  12439. this.commonObjs = new PDFObjects();
  12440. this.fontLoader = new FontLoader({
  12441. ownerDocument: params.ownerDocument,
  12442. styleElement: params.styleElement
  12443. });
  12444. this.loadingParams = params.loadingParams;
  12445. this._params = params;
  12446. this.canvasFactory = factory.canvasFactory;
  12447. this.filterFactory = factory.filterFactory;
  12448. this.cMapReaderFactory = factory.cMapReaderFactory;
  12449. this.standardFontDataFactory = factory.standardFontDataFactory;
  12450. this.wasmFactory = factory.wasmFactory;
  12451. this.destroyed = false;
  12452. this.destroyCapability = null;
  12453. this._networkStream = networkStream;
  12454. this._fullReader = null;
  12455. this._lastProgress = null;
  12456. this.downloadInfoCapability = Promise.withResolvers();
  12457. this.setupMessageHandler();
  12458. }
  12459. #cacheSimpleMethod(name, data = null) {
  12460. const cachedPromise = this.#methodPromises.get(name);
  12461. if (cachedPromise) {
  12462. return cachedPromise;
  12463. }
  12464. const promise = this.messageHandler.sendWithPromise(name, data);
  12465. this.#methodPromises.set(name, promise);
  12466. return promise;
  12467. }
  12468. get annotationStorage() {
  12469. return shadow(this, "annotationStorage", new AnnotationStorage());
  12470. }
  12471. getRenderingIntent(intent, annotationMode = AnnotationMode.ENABLE, printAnnotationStorage = null, isEditing = false, isOpList = false) {
  12472. let renderingIntent = RenderingIntentFlag.DISPLAY;
  12473. let annotationStorageSerializable = SerializableEmpty;
  12474. switch (intent) {
  12475. case "any":
  12476. renderingIntent = RenderingIntentFlag.ANY;
  12477. break;
  12478. case "display":
  12479. break;
  12480. case "print":
  12481. renderingIntent = RenderingIntentFlag.PRINT;
  12482. break;
  12483. default:
  12484. warn(`getRenderingIntent - invalid intent: ${intent}`);
  12485. }
  12486. const annotationStorage = renderingIntent & RenderingIntentFlag.PRINT && printAnnotationStorage instanceof PrintAnnotationStorage ? printAnnotationStorage : this.annotationStorage;
  12487. switch (annotationMode) {
  12488. case AnnotationMode.DISABLE:
  12489. renderingIntent += RenderingIntentFlag.ANNOTATIONS_DISABLE;
  12490. break;
  12491. case AnnotationMode.ENABLE:
  12492. break;
  12493. case AnnotationMode.ENABLE_FORMS:
  12494. renderingIntent += RenderingIntentFlag.ANNOTATIONS_FORMS;
  12495. break;
  12496. case AnnotationMode.ENABLE_STORAGE:
  12497. renderingIntent += RenderingIntentFlag.ANNOTATIONS_STORAGE;
  12498. annotationStorageSerializable = annotationStorage.serializable;
  12499. break;
  12500. default:
  12501. warn(`getRenderingIntent - invalid annotationMode: ${annotationMode}`);
  12502. }
  12503. if (isEditing) {
  12504. renderingIntent += RenderingIntentFlag.IS_EDITING;
  12505. }
  12506. if (isOpList) {
  12507. renderingIntent += RenderingIntentFlag.OPLIST;
  12508. }
  12509. const {
  12510. ids: modifiedIds,
  12511. hash: modifiedIdsHash
  12512. } = annotationStorage.modifiedIds;
  12513. const cacheKeyBuf = [renderingIntent, annotationStorageSerializable.hash, modifiedIdsHash];
  12514. return {
  12515. renderingIntent,
  12516. cacheKey: cacheKeyBuf.join("_"),
  12517. annotationStorageSerializable,
  12518. modifiedIds
  12519. };
  12520. }
  12521. destroy() {
  12522. if (this.destroyCapability) {
  12523. return this.destroyCapability.promise;
  12524. }
  12525. this.destroyed = true;
  12526. this.destroyCapability = Promise.withResolvers();
  12527. this.#passwordCapability?.reject(new Error("Worker was destroyed during onPassword callback"));
  12528. const waitOn = [];
  12529. for (const page of this.#pageCache.values()) {
  12530. waitOn.push(page._destroy());
  12531. }
  12532. this.#pageCache.clear();
  12533. this.#pagePromises.clear();
  12534. this.#pageRefCache.clear();
  12535. if (this.hasOwnProperty("annotationStorage")) {
  12536. this.annotationStorage.resetModified();
  12537. }
  12538. const terminated = this.messageHandler.sendWithPromise("Terminate", null);
  12539. waitOn.push(terminated);
  12540. Promise.all(waitOn).then(() => {
  12541. this.commonObjs.clear();
  12542. this.fontLoader.clear();
  12543. this.#methodPromises.clear();
  12544. this.filterFactory.destroy();
  12545. TextLayer.cleanup();
  12546. this._networkStream?.cancelAllRequests(new AbortException("Worker was terminated."));
  12547. this.messageHandler?.destroy();
  12548. this.messageHandler = null;
  12549. this.destroyCapability.resolve();
  12550. }, this.destroyCapability.reject);
  12551. return this.destroyCapability.promise;
  12552. }
  12553. setupMessageHandler() {
  12554. const {
  12555. messageHandler,
  12556. loadingTask
  12557. } = this;
  12558. messageHandler.on("GetReader", (data, sink) => {
  12559. assert(this._networkStream, "GetReader - no `IPDFStream` instance available.");
  12560. this._fullReader = this._networkStream.getFullReader();
  12561. this._fullReader.onProgress = evt => {
  12562. this._lastProgress = {
  12563. loaded: evt.loaded,
  12564. total: evt.total
  12565. };
  12566. };
  12567. sink.onPull = () => {
  12568. this._fullReader.read().then(function ({
  12569. value,
  12570. done
  12571. }) {
  12572. if (done) {
  12573. sink.close();
  12574. return;
  12575. }
  12576. assert(value instanceof ArrayBuffer, "GetReader - expected an ArrayBuffer.");
  12577. sink.enqueue(new Uint8Array(value), 1, [value]);
  12578. }).catch(reason => {
  12579. sink.error(reason);
  12580. });
  12581. };
  12582. sink.onCancel = reason => {
  12583. this._fullReader.cancel(reason);
  12584. sink.ready.catch(readyReason => {
  12585. if (this.destroyed) {
  12586. return;
  12587. }
  12588. throw readyReason;
  12589. });
  12590. };
  12591. });
  12592. messageHandler.on("ReaderHeadersReady", async data => {
  12593. await this._fullReader.headersReady;
  12594. const {
  12595. isStreamingSupported,
  12596. isRangeSupported,
  12597. contentLength
  12598. } = this._fullReader;
  12599. if (!isStreamingSupported || !isRangeSupported) {
  12600. if (this._lastProgress) {
  12601. loadingTask.onProgress?.(this._lastProgress);
  12602. }
  12603. this._fullReader.onProgress = evt => {
  12604. loadingTask.onProgress?.({
  12605. loaded: evt.loaded,
  12606. total: evt.total
  12607. });
  12608. };
  12609. }
  12610. return {
  12611. isStreamingSupported,
  12612. isRangeSupported,
  12613. contentLength
  12614. };
  12615. });
  12616. messageHandler.on("GetRangeReader", (data, sink) => {
  12617. assert(this._networkStream, "GetRangeReader - no `IPDFStream` instance available.");
  12618. const rangeReader = this._networkStream.getRangeReader(data.begin, data.end);
  12619. if (!rangeReader) {
  12620. sink.close();
  12621. return;
  12622. }
  12623. sink.onPull = () => {
  12624. rangeReader.read().then(function ({
  12625. value,
  12626. done
  12627. }) {
  12628. if (done) {
  12629. sink.close();
  12630. return;
  12631. }
  12632. assert(value instanceof ArrayBuffer, "GetRangeReader - expected an ArrayBuffer.");
  12633. sink.enqueue(new Uint8Array(value), 1, [value]);
  12634. }).catch(reason => {
  12635. sink.error(reason);
  12636. });
  12637. };
  12638. sink.onCancel = reason => {
  12639. rangeReader.cancel(reason);
  12640. sink.ready.catch(readyReason => {
  12641. if (this.destroyed) {
  12642. return;
  12643. }
  12644. throw readyReason;
  12645. });
  12646. };
  12647. });
  12648. messageHandler.on("GetDoc", ({
  12649. pdfInfo
  12650. }) => {
  12651. this._numPages = pdfInfo.numPages;
  12652. this._htmlForXfa = pdfInfo.htmlForXfa;
  12653. delete pdfInfo.htmlForXfa;
  12654. loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
  12655. });
  12656. messageHandler.on("DocException", ex => {
  12657. loadingTask._capability.reject(wrapReason(ex));
  12658. });
  12659. messageHandler.on("PasswordRequest", ex => {
  12660. this.#passwordCapability = Promise.withResolvers();
  12661. try {
  12662. if (!loadingTask.onPassword) {
  12663. throw wrapReason(ex);
  12664. }
  12665. const updatePassword = password => {
  12666. if (password instanceof Error) {
  12667. this.#passwordCapability.reject(password);
  12668. } else {
  12669. this.#passwordCapability.resolve({
  12670. password
  12671. });
  12672. }
  12673. };
  12674. loadingTask.onPassword(updatePassword, ex.code);
  12675. } catch (err) {
  12676. this.#passwordCapability.reject(err);
  12677. }
  12678. return this.#passwordCapability.promise;
  12679. });
  12680. messageHandler.on("DataLoaded", data => {
  12681. loadingTask.onProgress?.({
  12682. loaded: data.length,
  12683. total: data.length
  12684. });
  12685. this.downloadInfoCapability.resolve(data);
  12686. });
  12687. messageHandler.on("StartRenderPage", data => {
  12688. if (this.destroyed) {
  12689. return;
  12690. }
  12691. const page = this.#pageCache.get(data.pageIndex);
  12692. page._startRenderPage(data.transparency, data.cacheKey);
  12693. });
  12694. messageHandler.on("commonobj", ([id, type, exportedData]) => {
  12695. if (this.destroyed) {
  12696. return null;
  12697. }
  12698. if (this.commonObjs.has(id)) {
  12699. return null;
  12700. }
  12701. switch (type) {
  12702. case "Font":
  12703. if ("error" in exportedData) {
  12704. const exportedError = exportedData.error;
  12705. warn(`Error during font loading: ${exportedError}`);
  12706. this.commonObjs.resolve(id, exportedError);
  12707. break;
  12708. }
  12709. const inspectFont = this._params.pdfBug && globalThis.FontInspector?.enabled ? (font, url) => globalThis.FontInspector.fontAdded(font, url) : null;
  12710. const font = new FontFaceObject(exportedData, inspectFont);
  12711. this.fontLoader.bind(font).catch(() => messageHandler.sendWithPromise("FontFallback", {
  12712. id
  12713. })).finally(() => {
  12714. if (!font.fontExtraProperties && font.data) {
  12715. font.data = null;
  12716. }
  12717. this.commonObjs.resolve(id, font);
  12718. });
  12719. break;
  12720. case "CopyLocalImage":
  12721. const {
  12722. imageRef
  12723. } = exportedData;
  12724. assert(imageRef, "The imageRef must be defined.");
  12725. for (const pageProxy of this.#pageCache.values()) {
  12726. for (const [, data] of pageProxy.objs) {
  12727. if (data?.ref !== imageRef) {
  12728. continue;
  12729. }
  12730. if (!data.dataLen) {
  12731. return null;
  12732. }
  12733. this.commonObjs.resolve(id, structuredClone(data));
  12734. return data.dataLen;
  12735. }
  12736. }
  12737. break;
  12738. case "FontPath":
  12739. case "Image":
  12740. case "Pattern":
  12741. this.commonObjs.resolve(id, exportedData);
  12742. break;
  12743. default:
  12744. throw new Error(`Got unknown common object type ${type}`);
  12745. }
  12746. return null;
  12747. });
  12748. messageHandler.on("obj", ([id, pageIndex, type, imageData]) => {
  12749. if (this.destroyed) {
  12750. return;
  12751. }
  12752. const pageProxy = this.#pageCache.get(pageIndex);
  12753. if (pageProxy.objs.has(id)) {
  12754. return;
  12755. }
  12756. if (pageProxy._intentStates.size === 0) {
  12757. imageData?.bitmap?.close();
  12758. return;
  12759. }
  12760. switch (type) {
  12761. case "Image":
  12762. case "Pattern":
  12763. pageProxy.objs.resolve(id, imageData);
  12764. break;
  12765. default:
  12766. throw new Error(`Got unknown object type ${type}`);
  12767. }
  12768. });
  12769. messageHandler.on("DocProgress", data => {
  12770. if (this.destroyed) {
  12771. return;
  12772. }
  12773. loadingTask.onProgress?.({
  12774. loaded: data.loaded,
  12775. total: data.total
  12776. });
  12777. });
  12778. messageHandler.on("FetchBinaryData", async data => {
  12779. if (this.destroyed) {
  12780. throw new Error("Worker was destroyed.");
  12781. }
  12782. const factory = this[data.type];
  12783. if (!factory) {
  12784. throw new Error(`${data.type} not initialized, see the \`useWorkerFetch\` parameter.`);
  12785. }
  12786. return factory.fetch(data);
  12787. });
  12788. }
  12789. getData() {
  12790. return this.messageHandler.sendWithPromise("GetData", null);
  12791. }
  12792. saveDocument() {
  12793. if (this.annotationStorage.size <= 0) {
  12794. warn("saveDocument called while `annotationStorage` is empty, " + "please use the getData-method instead.");
  12795. }
  12796. const {
  12797. map,
  12798. transfer
  12799. } = this.annotationStorage.serializable;
  12800. return this.messageHandler.sendWithPromise("SaveDocument", {
  12801. isPureXfa: !!this._htmlForXfa,
  12802. numPages: this._numPages,
  12803. annotationStorage: map,
  12804. filename: this._fullReader?.filename ?? null
  12805. }, transfer).finally(() => {
  12806. this.annotationStorage.resetModified();
  12807. });
  12808. }
  12809. getPage(pageNumber) {
  12810. if (!Number.isInteger(pageNumber) || pageNumber <= 0 || pageNumber > this._numPages) {
  12811. return Promise.reject(new Error("Invalid page request."));
  12812. }
  12813. const pageIndex = pageNumber - 1,
  12814. cachedPromise = this.#pagePromises.get(pageIndex);
  12815. if (cachedPromise) {
  12816. return cachedPromise;
  12817. }
  12818. const promise = this.messageHandler.sendWithPromise("GetPage", {
  12819. pageIndex
  12820. }).then(pageInfo => {
  12821. if (this.destroyed) {
  12822. throw new Error("Transport destroyed");
  12823. }
  12824. if (pageInfo.refStr) {
  12825. this.#pageRefCache.set(pageInfo.refStr, pageNumber);
  12826. }
  12827. const page = new PDFPageProxy(pageIndex, pageInfo, this, this._params.pdfBug);
  12828. this.#pageCache.set(pageIndex, page);
  12829. return page;
  12830. });
  12831. this.#pagePromises.set(pageIndex, promise);
  12832. return promise;
  12833. }
  12834. getPageIndex(ref) {
  12835. if (!isRefProxy(ref)) {
  12836. return Promise.reject(new Error("Invalid pageIndex request."));
  12837. }
  12838. return this.messageHandler.sendWithPromise("GetPageIndex", {
  12839. num: ref.num,
  12840. gen: ref.gen
  12841. });
  12842. }
  12843. getAnnotations(pageIndex, intent) {
  12844. return this.messageHandler.sendWithPromise("GetAnnotations", {
  12845. pageIndex,
  12846. intent
  12847. });
  12848. }
  12849. getFieldObjects() {
  12850. return this.#cacheSimpleMethod("GetFieldObjects");
  12851. }
  12852. hasJSActions() {
  12853. return this.#cacheSimpleMethod("HasJSActions");
  12854. }
  12855. getCalculationOrderIds() {
  12856. return this.messageHandler.sendWithPromise("GetCalculationOrderIds", null);
  12857. }
  12858. getDestinations() {
  12859. return this.messageHandler.sendWithPromise("GetDestinations", null);
  12860. }
  12861. getDestination(id) {
  12862. if (typeof id !== "string") {
  12863. return Promise.reject(new Error("Invalid destination request."));
  12864. }
  12865. return this.messageHandler.sendWithPromise("GetDestination", {
  12866. id
  12867. });
  12868. }
  12869. getPageLabels() {
  12870. return this.messageHandler.sendWithPromise("GetPageLabels", null);
  12871. }
  12872. getPageLayout() {
  12873. return this.messageHandler.sendWithPromise("GetPageLayout", null);
  12874. }
  12875. getPageMode() {
  12876. return this.messageHandler.sendWithPromise("GetPageMode", null);
  12877. }
  12878. getViewerPreferences() {
  12879. return this.messageHandler.sendWithPromise("GetViewerPreferences", null);
  12880. }
  12881. getOpenAction() {
  12882. return this.messageHandler.sendWithPromise("GetOpenAction", null);
  12883. }
  12884. getAttachments() {
  12885. return this.messageHandler.sendWithPromise("GetAttachments", null);
  12886. }
  12887. getDocJSActions() {
  12888. return this.#cacheSimpleMethod("GetDocJSActions");
  12889. }
  12890. getPageJSActions(pageIndex) {
  12891. return this.messageHandler.sendWithPromise("GetPageJSActions", {
  12892. pageIndex
  12893. });
  12894. }
  12895. getStructTree(pageIndex) {
  12896. return this.messageHandler.sendWithPromise("GetStructTree", {
  12897. pageIndex
  12898. });
  12899. }
  12900. getOutline() {
  12901. return this.messageHandler.sendWithPromise("GetOutline", null);
  12902. }
  12903. getOptionalContentConfig(renderingIntent) {
  12904. return this.#cacheSimpleMethod("GetOptionalContentConfig").then(data => new OptionalContentConfig(data, renderingIntent));
  12905. }
  12906. getPermissions() {
  12907. return this.messageHandler.sendWithPromise("GetPermissions", null);
  12908. }
  12909. getMetadata() {
  12910. const name = "GetMetadata",
  12911. cachedPromise = this.#methodPromises.get(name);
  12912. if (cachedPromise) {
  12913. return cachedPromise;
  12914. }
  12915. const promise = this.messageHandler.sendWithPromise(name, null).then(results => ({
  12916. info: results[0],
  12917. metadata: results[1] ? new Metadata(results[1]) : null,
  12918. contentDispositionFilename: this._fullReader?.filename ?? null,
  12919. contentLength: this._fullReader?.contentLength ?? null
  12920. }));
  12921. this.#methodPromises.set(name, promise);
  12922. return promise;
  12923. }
  12924. getMarkInfo() {
  12925. return this.messageHandler.sendWithPromise("GetMarkInfo", null);
  12926. }
  12927. async startCleanup(keepLoadedFonts = false) {
  12928. if (this.destroyed) {
  12929. return;
  12930. }
  12931. await this.messageHandler.sendWithPromise("Cleanup", null);
  12932. for (const page of this.#pageCache.values()) {
  12933. const cleanupSuccessful = page.cleanup();
  12934. if (!cleanupSuccessful) {
  12935. throw new Error(`startCleanup: Page ${page.pageNumber} is currently rendering.`);
  12936. }
  12937. }
  12938. this.commonObjs.clear();
  12939. if (!keepLoadedFonts) {
  12940. this.fontLoader.clear();
  12941. }
  12942. this.#methodPromises.clear();
  12943. this.filterFactory.destroy(true);
  12944. TextLayer.cleanup();
  12945. }
  12946. cachedPageNumber(ref) {
  12947. if (!isRefProxy(ref)) {
  12948. return null;
  12949. }
  12950. const refStr = ref.gen === 0 ? `${ref.num}R` : `${ref.num}R${ref.gen}`;
  12951. return this.#pageRefCache.get(refStr) ?? null;
  12952. }
  12953. }
  12954. const INITIAL_DATA = Symbol("INITIAL_DATA");
  12955. class PDFObjects {
  12956. #objs = Object.create(null);
  12957. #ensureObj(objId) {
  12958. return this.#objs[objId] ||= {
  12959. ...Promise.withResolvers(),
  12960. data: INITIAL_DATA
  12961. };
  12962. }
  12963. get(objId, callback = null) {
  12964. if (callback) {
  12965. const obj = this.#ensureObj(objId);
  12966. obj.promise.then(() => callback(obj.data));
  12967. return null;
  12968. }
  12969. const obj = this.#objs[objId];
  12970. if (!obj || obj.data === INITIAL_DATA) {
  12971. throw new Error(`Requesting object that isn't resolved yet ${objId}.`);
  12972. }
  12973. return obj.data;
  12974. }
  12975. has(objId) {
  12976. const obj = this.#objs[objId];
  12977. return !!obj && obj.data !== INITIAL_DATA;
  12978. }
  12979. delete(objId) {
  12980. const obj = this.#objs[objId];
  12981. if (!obj || obj.data === INITIAL_DATA) {
  12982. return false;
  12983. }
  12984. delete this.#objs[objId];
  12985. return true;
  12986. }
  12987. resolve(objId, data = null) {
  12988. const obj = this.#ensureObj(objId);
  12989. obj.data = data;
  12990. obj.resolve();
  12991. }
  12992. clear() {
  12993. for (const objId in this.#objs) {
  12994. const {
  12995. data
  12996. } = this.#objs[objId];
  12997. data?.bitmap?.close();
  12998. }
  12999. this.#objs = Object.create(null);
  13000. }
  13001. *[Symbol.iterator]() {
  13002. for (const objId in this.#objs) {
  13003. const {
  13004. data
  13005. } = this.#objs[objId];
  13006. if (data === INITIAL_DATA) {
  13007. continue;
  13008. }
  13009. yield [objId, data];
  13010. }
  13011. }
  13012. }
  13013. class RenderTask {
  13014. #internalRenderTask = null;
  13015. onContinue = null;
  13016. onError = null;
  13017. constructor(internalRenderTask) {
  13018. this.#internalRenderTask = internalRenderTask;
  13019. }
  13020. get promise() {
  13021. return this.#internalRenderTask.capability.promise;
  13022. }
  13023. cancel(extraDelay = 0) {
  13024. this.#internalRenderTask.cancel(null, extraDelay);
  13025. }
  13026. get separateAnnots() {
  13027. const {
  13028. separateAnnots
  13029. } = this.#internalRenderTask.operatorList;
  13030. if (!separateAnnots) {
  13031. return false;
  13032. }
  13033. const {
  13034. annotationCanvasMap
  13035. } = this.#internalRenderTask;
  13036. return separateAnnots.form || separateAnnots.canvas && annotationCanvasMap?.size > 0;
  13037. }
  13038. }
  13039. class InternalRenderTask {
  13040. #rAF = null;
  13041. static #canvasInUse = new WeakSet();
  13042. constructor({
  13043. callback,
  13044. params,
  13045. objs,
  13046. commonObjs,
  13047. annotationCanvasMap,
  13048. operatorList,
  13049. pageIndex,
  13050. canvasFactory,
  13051. filterFactory,
  13052. useRequestAnimationFrame = false,
  13053. pdfBug = false,
  13054. pageColors = null
  13055. }) {
  13056. this.callback = callback;
  13057. this.params = params;
  13058. this.objs = objs;
  13059. this.commonObjs = commonObjs;
  13060. this.annotationCanvasMap = annotationCanvasMap;
  13061. this.operatorListIdx = null;
  13062. this.operatorList = operatorList;
  13063. this._pageIndex = pageIndex;
  13064. this.canvasFactory = canvasFactory;
  13065. this.filterFactory = filterFactory;
  13066. this._pdfBug = pdfBug;
  13067. this.pageColors = pageColors;
  13068. this.running = false;
  13069. this.graphicsReadyCallback = null;
  13070. this.graphicsReady = false;
  13071. this._useRequestAnimationFrame = useRequestAnimationFrame === true && typeof window !== "undefined";
  13072. this.cancelled = false;
  13073. this.capability = Promise.withResolvers();
  13074. this.task = new RenderTask(this);
  13075. this._cancelBound = this.cancel.bind(this);
  13076. this._continueBound = this._continue.bind(this);
  13077. this._scheduleNextBound = this._scheduleNext.bind(this);
  13078. this._nextBound = this._next.bind(this);
  13079. this._canvas = params.canvasContext.canvas;
  13080. }
  13081. get completed() {
  13082. return this.capability.promise.catch(function () {});
  13083. }
  13084. initializeGraphics({
  13085. transparency = false,
  13086. optionalContentConfig
  13087. }) {
  13088. if (this.cancelled) {
  13089. return;
  13090. }
  13091. if (this._canvas) {
  13092. if (InternalRenderTask.#canvasInUse.has(this._canvas)) {
  13093. throw new Error("Cannot use the same canvas during multiple render() operations. " + "Use different canvas or ensure previous operations were " + "cancelled or completed.");
  13094. }
  13095. InternalRenderTask.#canvasInUse.add(this._canvas);
  13096. }
  13097. if (this._pdfBug && globalThis.StepperManager?.enabled) {
  13098. this.stepper = globalThis.StepperManager.create(this._pageIndex);
  13099. this.stepper.init(this.operatorList);
  13100. this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
  13101. }
  13102. const {
  13103. canvasContext,
  13104. viewport,
  13105. transform,
  13106. background
  13107. } = this.params;
  13108. this.gfx = new CanvasGraphics(canvasContext, this.commonObjs, this.objs, this.canvasFactory, this.filterFactory, {
  13109. optionalContentConfig
  13110. }, this.annotationCanvasMap, this.pageColors);
  13111. this.gfx.beginDrawing({
  13112. transform,
  13113. viewport,
  13114. transparency,
  13115. background
  13116. });
  13117. this.operatorListIdx = 0;
  13118. this.graphicsReady = true;
  13119. this.graphicsReadyCallback?.();
  13120. }
  13121. cancel(error = null, extraDelay = 0) {
  13122. this.running = false;
  13123. this.cancelled = true;
  13124. this.gfx?.endDrawing();
  13125. if (this.#rAF) {
  13126. window.cancelAnimationFrame(this.#rAF);
  13127. this.#rAF = null;
  13128. }
  13129. InternalRenderTask.#canvasInUse.delete(this._canvas);
  13130. error ||= new RenderingCancelledException(`Rendering cancelled, page ${this._pageIndex + 1}`, extraDelay);
  13131. this.callback(error);
  13132. this.task.onError?.(error);
  13133. }
  13134. operatorListChanged() {
  13135. if (!this.graphicsReady) {
  13136. this.graphicsReadyCallback ||= this._continueBound;
  13137. return;
  13138. }
  13139. this.stepper?.updateOperatorList(this.operatorList);
  13140. if (this.running) {
  13141. return;
  13142. }
  13143. this._continue();
  13144. }
  13145. _continue() {
  13146. this.running = true;
  13147. if (this.cancelled) {
  13148. return;
  13149. }
  13150. if (this.task.onContinue) {
  13151. this.task.onContinue(this._scheduleNextBound);
  13152. } else {
  13153. this._scheduleNext();
  13154. }
  13155. }
  13156. _scheduleNext() {
  13157. if (this._useRequestAnimationFrame) {
  13158. this.#rAF = window.requestAnimationFrame(() => {
  13159. this.#rAF = null;
  13160. this._nextBound().catch(this._cancelBound);
  13161. });
  13162. } else {
  13163. Promise.resolve().then(this._nextBound).catch(this._cancelBound);
  13164. }
  13165. }
  13166. async _next() {
  13167. if (this.cancelled) {
  13168. return;
  13169. }
  13170. this.operatorListIdx = this.gfx.executeOperatorList(this.operatorList, this.operatorListIdx, this._continueBound, this.stepper);
  13171. if (this.operatorListIdx === this.operatorList.argsArray.length) {
  13172. this.running = false;
  13173. if (this.operatorList.lastChunk) {
  13174. this.gfx.endDrawing();
  13175. InternalRenderTask.#canvasInUse.delete(this._canvas);
  13176. this.callback();
  13177. }
  13178. }
  13179. }
  13180. }
  13181. const version = "5.1.91";
  13182. const build = "45cbe8bb0";
  13183. ;// ./src/shared/scripting_utils.js
  13184. function makeColorComp(n) {
  13185. return Math.floor(Math.max(0, Math.min(1, n)) * 255).toString(16).padStart(2, "0");
  13186. }
  13187. function scaleAndClamp(x) {
  13188. return Math.max(0, Math.min(255, 255 * x));
  13189. }
  13190. class ColorConverters {
  13191. static CMYK_G([c, y, m, k]) {
  13192. return ["G", 1 - Math.min(1, 0.3 * c + 0.59 * m + 0.11 * y + k)];
  13193. }
  13194. static G_CMYK([g]) {
  13195. return ["CMYK", 0, 0, 0, 1 - g];
  13196. }
  13197. static G_RGB([g]) {
  13198. return ["RGB", g, g, g];
  13199. }
  13200. static G_rgb([g]) {
  13201. g = scaleAndClamp(g);
  13202. return [g, g, g];
  13203. }
  13204. static G_HTML([g]) {
  13205. const G = makeColorComp(g);
  13206. return `#${G}${G}${G}`;
  13207. }
  13208. static RGB_G([r, g, b]) {
  13209. return ["G", 0.3 * r + 0.59 * g + 0.11 * b];
  13210. }
  13211. static RGB_rgb(color) {
  13212. return color.map(scaleAndClamp);
  13213. }
  13214. static RGB_HTML(color) {
  13215. return `#${color.map(makeColorComp).join("")}`;
  13216. }
  13217. static T_HTML() {
  13218. return "#00000000";
  13219. }
  13220. static T_rgb() {
  13221. return [null];
  13222. }
  13223. static CMYK_RGB([c, y, m, k]) {
  13224. return ["RGB", 1 - Math.min(1, c + k), 1 - Math.min(1, m + k), 1 - Math.min(1, y + k)];
  13225. }
  13226. static CMYK_rgb([c, y, m, k]) {
  13227. return [scaleAndClamp(1 - Math.min(1, c + k)), scaleAndClamp(1 - Math.min(1, m + k)), scaleAndClamp(1 - Math.min(1, y + k))];
  13228. }
  13229. static CMYK_HTML(components) {
  13230. const rgb = this.CMYK_RGB(components).slice(1);
  13231. return this.RGB_HTML(rgb);
  13232. }
  13233. static RGB_CMYK([r, g, b]) {
  13234. const c = 1 - r;
  13235. const m = 1 - g;
  13236. const y = 1 - b;
  13237. const k = Math.min(c, m, y);
  13238. return ["CMYK", c, m, y, k];
  13239. }
  13240. }
  13241. ;// ./src/display/svg_factory.js
  13242. class BaseSVGFactory {
  13243. create(width, height, skipDimensions = false) {
  13244. if (width <= 0 || height <= 0) {
  13245. throw new Error("Invalid SVG dimensions");
  13246. }
  13247. const svg = this._createSVG("svg:svg");
  13248. svg.setAttribute("version", "1.1");
  13249. if (!skipDimensions) {
  13250. svg.setAttribute("width", `${width}px`);
  13251. svg.setAttribute("height", `${height}px`);
  13252. }
  13253. svg.setAttribute("preserveAspectRatio", "none");
  13254. svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
  13255. return svg;
  13256. }
  13257. createElement(type) {
  13258. if (typeof type !== "string") {
  13259. throw new Error("Invalid SVG element type");
  13260. }
  13261. return this._createSVG(type);
  13262. }
  13263. _createSVG(type) {
  13264. unreachable("Abstract method `_createSVG` called.");
  13265. }
  13266. }
  13267. class DOMSVGFactory extends BaseSVGFactory {
  13268. _createSVG(type) {
  13269. return document.createElementNS(SVG_NS, type);
  13270. }
  13271. }
  13272. ;// ./src/display/xfa_layer.js
  13273. class XfaLayer {
  13274. static setupStorage(html, id, element, storage, intent) {
  13275. const storedData = storage.getValue(id, {
  13276. value: null
  13277. });
  13278. switch (element.name) {
  13279. case "textarea":
  13280. if (storedData.value !== null) {
  13281. html.textContent = storedData.value;
  13282. }
  13283. if (intent === "print") {
  13284. break;
  13285. }
  13286. html.addEventListener("input", event => {
  13287. storage.setValue(id, {
  13288. value: event.target.value
  13289. });
  13290. });
  13291. break;
  13292. case "input":
  13293. if (element.attributes.type === "radio" || element.attributes.type === "checkbox") {
  13294. if (storedData.value === element.attributes.xfaOn) {
  13295. html.setAttribute("checked", true);
  13296. } else if (storedData.value === element.attributes.xfaOff) {
  13297. html.removeAttribute("checked");
  13298. }
  13299. if (intent === "print") {
  13300. break;
  13301. }
  13302. html.addEventListener("change", event => {
  13303. storage.setValue(id, {
  13304. value: event.target.checked ? event.target.getAttribute("xfaOn") : event.target.getAttribute("xfaOff")
  13305. });
  13306. });
  13307. } else {
  13308. if (storedData.value !== null) {
  13309. html.setAttribute("value", storedData.value);
  13310. }
  13311. if (intent === "print") {
  13312. break;
  13313. }
  13314. html.addEventListener("input", event => {
  13315. storage.setValue(id, {
  13316. value: event.target.value
  13317. });
  13318. });
  13319. }
  13320. break;
  13321. case "select":
  13322. if (storedData.value !== null) {
  13323. html.setAttribute("value", storedData.value);
  13324. for (const option of element.children) {
  13325. if (option.attributes.value === storedData.value) {
  13326. option.attributes.selected = true;
  13327. } else if (option.attributes.hasOwnProperty("selected")) {
  13328. delete option.attributes.selected;
  13329. }
  13330. }
  13331. }
  13332. html.addEventListener("input", event => {
  13333. const options = event.target.options;
  13334. const value = options.selectedIndex === -1 ? "" : options[options.selectedIndex].value;
  13335. storage.setValue(id, {
  13336. value
  13337. });
  13338. });
  13339. break;
  13340. }
  13341. }
  13342. static setAttributes({
  13343. html,
  13344. element,
  13345. storage = null,
  13346. intent,
  13347. linkService
  13348. }) {
  13349. const {
  13350. attributes
  13351. } = element;
  13352. const isHTMLAnchorElement = html instanceof HTMLAnchorElement;
  13353. if (attributes.type === "radio") {
  13354. attributes.name = `${attributes.name}-${intent}`;
  13355. }
  13356. for (const [key, value] of Object.entries(attributes)) {
  13357. if (value === null || value === undefined) {
  13358. continue;
  13359. }
  13360. switch (key) {
  13361. case "class":
  13362. if (value.length) {
  13363. html.setAttribute(key, value.join(" "));
  13364. }
  13365. break;
  13366. case "dataId":
  13367. break;
  13368. case "id":
  13369. html.setAttribute("data-element-id", value);
  13370. break;
  13371. case "style":
  13372. Object.assign(html.style, value);
  13373. break;
  13374. case "textContent":
  13375. html.textContent = value;
  13376. break;
  13377. default:
  13378. if (!isHTMLAnchorElement || key !== "href" && key !== "newWindow") {
  13379. html.setAttribute(key, value);
  13380. }
  13381. }
  13382. }
  13383. if (isHTMLAnchorElement) {
  13384. linkService.addLinkAttributes(html, attributes.href, attributes.newWindow);
  13385. }
  13386. if (storage && attributes.dataId) {
  13387. this.setupStorage(html, attributes.dataId, element, storage);
  13388. }
  13389. }
  13390. static render(parameters) {
  13391. const storage = parameters.annotationStorage;
  13392. const linkService = parameters.linkService;
  13393. const root = parameters.xfaHtml;
  13394. const intent = parameters.intent || "display";
  13395. const rootHtml = document.createElement(root.name);
  13396. if (root.attributes) {
  13397. this.setAttributes({
  13398. html: rootHtml,
  13399. element: root,
  13400. intent,
  13401. linkService
  13402. });
  13403. }
  13404. const isNotForRichText = intent !== "richText";
  13405. const rootDiv = parameters.div;
  13406. rootDiv.append(rootHtml);
  13407. if (parameters.viewport) {
  13408. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  13409. rootDiv.style.transform = transform;
  13410. }
  13411. if (isNotForRichText) {
  13412. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  13413. }
  13414. const textDivs = [];
  13415. if (root.children.length === 0) {
  13416. if (root.value) {
  13417. const node = document.createTextNode(root.value);
  13418. rootHtml.append(node);
  13419. if (isNotForRichText && XfaText.shouldBuildText(root.name)) {
  13420. textDivs.push(node);
  13421. }
  13422. }
  13423. return {
  13424. textDivs
  13425. };
  13426. }
  13427. const stack = [[root, -1, rootHtml]];
  13428. while (stack.length > 0) {
  13429. const [parent, i, html] = stack.at(-1);
  13430. if (i + 1 === parent.children.length) {
  13431. stack.pop();
  13432. continue;
  13433. }
  13434. const child = parent.children[++stack.at(-1)[1]];
  13435. if (child === null) {
  13436. continue;
  13437. }
  13438. const {
  13439. name
  13440. } = child;
  13441. if (name === "#text") {
  13442. const node = document.createTextNode(child.value);
  13443. textDivs.push(node);
  13444. html.append(node);
  13445. continue;
  13446. }
  13447. const childHtml = child?.attributes?.xmlns ? document.createElementNS(child.attributes.xmlns, name) : document.createElement(name);
  13448. html.append(childHtml);
  13449. if (child.attributes) {
  13450. this.setAttributes({
  13451. html: childHtml,
  13452. element: child,
  13453. storage,
  13454. intent,
  13455. linkService
  13456. });
  13457. }
  13458. if (child.children?.length > 0) {
  13459. stack.push([child, -1, childHtml]);
  13460. } else if (child.value) {
  13461. const node = document.createTextNode(child.value);
  13462. if (isNotForRichText && XfaText.shouldBuildText(name)) {
  13463. textDivs.push(node);
  13464. }
  13465. childHtml.append(node);
  13466. }
  13467. }
  13468. for (const el of rootDiv.querySelectorAll(".xfaNonInteractive input, .xfaNonInteractive textarea")) {
  13469. el.setAttribute("readOnly", true);
  13470. }
  13471. return {
  13472. textDivs
  13473. };
  13474. }
  13475. static update(parameters) {
  13476. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  13477. parameters.div.style.transform = transform;
  13478. parameters.div.hidden = false;
  13479. }
  13480. }
  13481. ;// ./src/display/annotation_layer.js
  13482. const DEFAULT_TAB_INDEX = 1000;
  13483. const annotation_layer_DEFAULT_FONT_SIZE = 9;
  13484. const GetElementsByNameSet = new WeakSet();
  13485. class AnnotationElementFactory {
  13486. static create(parameters) {
  13487. const subtype = parameters.data.annotationType;
  13488. switch (subtype) {
  13489. case AnnotationType.LINK:
  13490. return new LinkAnnotationElement(parameters);
  13491. case AnnotationType.TEXT:
  13492. return new TextAnnotationElement(parameters);
  13493. case AnnotationType.WIDGET:
  13494. const fieldType = parameters.data.fieldType;
  13495. switch (fieldType) {
  13496. case "Tx":
  13497. return new TextWidgetAnnotationElement(parameters);
  13498. case "Btn":
  13499. if (parameters.data.radioButton) {
  13500. return new RadioButtonWidgetAnnotationElement(parameters);
  13501. } else if (parameters.data.checkBox) {
  13502. return new CheckboxWidgetAnnotationElement(parameters);
  13503. }
  13504. return new PushButtonWidgetAnnotationElement(parameters);
  13505. case "Ch":
  13506. return new ChoiceWidgetAnnotationElement(parameters);
  13507. case "Sig":
  13508. return new SignatureWidgetAnnotationElement(parameters);
  13509. }
  13510. return new WidgetAnnotationElement(parameters);
  13511. case AnnotationType.POPUP:
  13512. return new PopupAnnotationElement(parameters);
  13513. case AnnotationType.FREETEXT:
  13514. return new FreeTextAnnotationElement(parameters);
  13515. case AnnotationType.LINE:
  13516. return new LineAnnotationElement(parameters);
  13517. case AnnotationType.SQUARE:
  13518. return new SquareAnnotationElement(parameters);
  13519. case AnnotationType.CIRCLE:
  13520. return new CircleAnnotationElement(parameters);
  13521. case AnnotationType.POLYLINE:
  13522. return new PolylineAnnotationElement(parameters);
  13523. case AnnotationType.CARET:
  13524. return new CaretAnnotationElement(parameters);
  13525. case AnnotationType.INK:
  13526. return new InkAnnotationElement(parameters);
  13527. case AnnotationType.POLYGON:
  13528. return new PolygonAnnotationElement(parameters);
  13529. case AnnotationType.HIGHLIGHT:
  13530. return new HighlightAnnotationElement(parameters);
  13531. case AnnotationType.UNDERLINE:
  13532. return new UnderlineAnnotationElement(parameters);
  13533. case AnnotationType.SQUIGGLY:
  13534. return new SquigglyAnnotationElement(parameters);
  13535. case AnnotationType.STRIKEOUT:
  13536. return new StrikeOutAnnotationElement(parameters);
  13537. case AnnotationType.STAMP:
  13538. return new StampAnnotationElement(parameters);
  13539. case AnnotationType.FILEATTACHMENT:
  13540. return new FileAttachmentAnnotationElement(parameters);
  13541. default:
  13542. return new AnnotationElement(parameters);
  13543. }
  13544. }
  13545. }
  13546. class AnnotationElement {
  13547. #updates = null;
  13548. #hasBorder = false;
  13549. #popupElement = null;
  13550. constructor(parameters, {
  13551. isRenderable = false,
  13552. ignoreBorder = false,
  13553. createQuadrilaterals = false
  13554. } = {}) {
  13555. this.isRenderable = isRenderable;
  13556. this.data = parameters.data;
  13557. this.layer = parameters.layer;
  13558. this.linkService = parameters.linkService;
  13559. this.downloadManager = parameters.downloadManager;
  13560. this.imageResourcesPath = parameters.imageResourcesPath;
  13561. this.renderForms = parameters.renderForms;
  13562. this.svgFactory = parameters.svgFactory;
  13563. this.annotationStorage = parameters.annotationStorage;
  13564. this.enableScripting = parameters.enableScripting;
  13565. this.hasJSActions = parameters.hasJSActions;
  13566. this._fieldObjects = parameters.fieldObjects;
  13567. this.parent = parameters.parent;
  13568. if (isRenderable) {
  13569. this.container = this._createContainer(ignoreBorder);
  13570. }
  13571. if (createQuadrilaterals) {
  13572. this._createQuadrilaterals();
  13573. }
  13574. }
  13575. static _hasPopupData({
  13576. titleObj,
  13577. contentsObj,
  13578. richText
  13579. }) {
  13580. return !!(titleObj?.str || contentsObj?.str || richText?.str);
  13581. }
  13582. get _isEditable() {
  13583. return this.data.isEditable;
  13584. }
  13585. get hasPopupData() {
  13586. return AnnotationElement._hasPopupData(this.data);
  13587. }
  13588. updateEdited(params) {
  13589. if (!this.container) {
  13590. return;
  13591. }
  13592. this.#updates ||= {
  13593. rect: this.data.rect.slice(0)
  13594. };
  13595. const {
  13596. rect
  13597. } = params;
  13598. if (rect) {
  13599. this.#setRectEdited(rect);
  13600. }
  13601. this.#popupElement?.popup.updateEdited(params);
  13602. }
  13603. resetEdited() {
  13604. if (!this.#updates) {
  13605. return;
  13606. }
  13607. this.#setRectEdited(this.#updates.rect);
  13608. this.#popupElement?.popup.resetEdited();
  13609. this.#updates = null;
  13610. }
  13611. #setRectEdited(rect) {
  13612. const {
  13613. container: {
  13614. style
  13615. },
  13616. data: {
  13617. rect: currentRect,
  13618. rotation
  13619. },
  13620. parent: {
  13621. viewport: {
  13622. rawDims: {
  13623. pageWidth,
  13624. pageHeight,
  13625. pageX,
  13626. pageY
  13627. }
  13628. }
  13629. }
  13630. } = this;
  13631. currentRect?.splice(0, 4, ...rect);
  13632. style.left = `${100 * (rect[0] - pageX) / pageWidth}%`;
  13633. style.top = `${100 * (pageHeight - rect[3] + pageY) / pageHeight}%`;
  13634. if (rotation === 0) {
  13635. style.width = `${100 * (rect[2] - rect[0]) / pageWidth}%`;
  13636. style.height = `${100 * (rect[3] - rect[1]) / pageHeight}%`;
  13637. } else {
  13638. this.setRotation(rotation);
  13639. }
  13640. }
  13641. _createContainer(ignoreBorder) {
  13642. const {
  13643. data,
  13644. parent: {
  13645. page,
  13646. viewport
  13647. }
  13648. } = this;
  13649. const container = document.createElement("section");
  13650. container.setAttribute("data-annotation-id", data.id);
  13651. if (!(this instanceof WidgetAnnotationElement)) {
  13652. container.tabIndex = DEFAULT_TAB_INDEX;
  13653. }
  13654. const {
  13655. style
  13656. } = container;
  13657. style.zIndex = this.parent.zIndex++;
  13658. if (data.alternativeText) {
  13659. container.title = data.alternativeText;
  13660. }
  13661. if (data.noRotate) {
  13662. container.classList.add("norotate");
  13663. }
  13664. if (!data.rect || this instanceof PopupAnnotationElement) {
  13665. const {
  13666. rotation
  13667. } = data;
  13668. if (!data.hasOwnCanvas && rotation !== 0) {
  13669. this.setRotation(rotation, container);
  13670. }
  13671. return container;
  13672. }
  13673. const {
  13674. width,
  13675. height
  13676. } = this;
  13677. if (!ignoreBorder && data.borderStyle.width > 0) {
  13678. style.borderWidth = `${data.borderStyle.width}px`;
  13679. const horizontalRadius = data.borderStyle.horizontalCornerRadius;
  13680. const verticalRadius = data.borderStyle.verticalCornerRadius;
  13681. if (horizontalRadius > 0 || verticalRadius > 0) {
  13682. const radius = `calc(${horizontalRadius}px * var(--total-scale-factor)) / calc(${verticalRadius}px * var(--total-scale-factor))`;
  13683. style.borderRadius = radius;
  13684. } else if (this instanceof RadioButtonWidgetAnnotationElement) {
  13685. const radius = `calc(${width}px * var(--total-scale-factor)) / calc(${height}px * var(--total-scale-factor))`;
  13686. style.borderRadius = radius;
  13687. }
  13688. switch (data.borderStyle.style) {
  13689. case AnnotationBorderStyleType.SOLID:
  13690. style.borderStyle = "solid";
  13691. break;
  13692. case AnnotationBorderStyleType.DASHED:
  13693. style.borderStyle = "dashed";
  13694. break;
  13695. case AnnotationBorderStyleType.BEVELED:
  13696. warn("Unimplemented border style: beveled");
  13697. break;
  13698. case AnnotationBorderStyleType.INSET:
  13699. warn("Unimplemented border style: inset");
  13700. break;
  13701. case AnnotationBorderStyleType.UNDERLINE:
  13702. style.borderBottomStyle = "solid";
  13703. break;
  13704. default:
  13705. break;
  13706. }
  13707. const borderColor = data.borderColor || null;
  13708. if (borderColor) {
  13709. this.#hasBorder = true;
  13710. style.borderColor = Util.makeHexColor(borderColor[0] | 0, borderColor[1] | 0, borderColor[2] | 0);
  13711. } else {
  13712. style.borderWidth = 0;
  13713. }
  13714. }
  13715. const rect = Util.normalizeRect([data.rect[0], page.view[3] - data.rect[1] + page.view[1], data.rect[2], page.view[3] - data.rect[3] + page.view[1]]);
  13716. const {
  13717. pageWidth,
  13718. pageHeight,
  13719. pageX,
  13720. pageY
  13721. } = viewport.rawDims;
  13722. style.left = `${100 * (rect[0] - pageX) / pageWidth}%`;
  13723. style.top = `${100 * (rect[1] - pageY) / pageHeight}%`;
  13724. const {
  13725. rotation
  13726. } = data;
  13727. if (data.hasOwnCanvas || rotation === 0) {
  13728. style.width = `${100 * width / pageWidth}%`;
  13729. style.height = `${100 * height / pageHeight}%`;
  13730. } else {
  13731. this.setRotation(rotation, container);
  13732. }
  13733. return container;
  13734. }
  13735. setRotation(angle, container = this.container) {
  13736. if (!this.data.rect) {
  13737. return;
  13738. }
  13739. const {
  13740. pageWidth,
  13741. pageHeight
  13742. } = this.parent.viewport.rawDims;
  13743. let {
  13744. width,
  13745. height
  13746. } = this;
  13747. if (angle % 180 !== 0) {
  13748. [width, height] = [height, width];
  13749. }
  13750. container.style.width = `${100 * width / pageWidth}%`;
  13751. container.style.height = `${100 * height / pageHeight}%`;
  13752. container.setAttribute("data-main-rotation", (360 - angle) % 360);
  13753. }
  13754. get _commonActions() {
  13755. const setColor = (jsName, styleName, event) => {
  13756. const color = event.detail[jsName];
  13757. const colorType = color[0];
  13758. const colorArray = color.slice(1);
  13759. event.target.style[styleName] = ColorConverters[`${colorType}_HTML`](colorArray);
  13760. this.annotationStorage.setValue(this.data.id, {
  13761. [styleName]: ColorConverters[`${colorType}_rgb`](colorArray)
  13762. });
  13763. };
  13764. return shadow(this, "_commonActions", {
  13765. display: event => {
  13766. const {
  13767. display
  13768. } = event.detail;
  13769. const hidden = display % 2 === 1;
  13770. this.container.style.visibility = hidden ? "hidden" : "visible";
  13771. this.annotationStorage.setValue(this.data.id, {
  13772. noView: hidden,
  13773. noPrint: display === 1 || display === 2
  13774. });
  13775. },
  13776. print: event => {
  13777. this.annotationStorage.setValue(this.data.id, {
  13778. noPrint: !event.detail.print
  13779. });
  13780. },
  13781. hidden: event => {
  13782. const {
  13783. hidden
  13784. } = event.detail;
  13785. this.container.style.visibility = hidden ? "hidden" : "visible";
  13786. this.annotationStorage.setValue(this.data.id, {
  13787. noPrint: hidden,
  13788. noView: hidden
  13789. });
  13790. },
  13791. focus: event => {
  13792. setTimeout(() => event.target.focus({
  13793. preventScroll: false
  13794. }), 0);
  13795. },
  13796. userName: event => {
  13797. event.target.title = event.detail.userName;
  13798. },
  13799. readonly: event => {
  13800. event.target.disabled = event.detail.readonly;
  13801. },
  13802. required: event => {
  13803. this._setRequired(event.target, event.detail.required);
  13804. },
  13805. bgColor: event => {
  13806. setColor("bgColor", "backgroundColor", event);
  13807. },
  13808. fillColor: event => {
  13809. setColor("fillColor", "backgroundColor", event);
  13810. },
  13811. fgColor: event => {
  13812. setColor("fgColor", "color", event);
  13813. },
  13814. textColor: event => {
  13815. setColor("textColor", "color", event);
  13816. },
  13817. borderColor: event => {
  13818. setColor("borderColor", "borderColor", event);
  13819. },
  13820. strokeColor: event => {
  13821. setColor("strokeColor", "borderColor", event);
  13822. },
  13823. rotation: event => {
  13824. const angle = event.detail.rotation;
  13825. this.setRotation(angle);
  13826. this.annotationStorage.setValue(this.data.id, {
  13827. rotation: angle
  13828. });
  13829. }
  13830. });
  13831. }
  13832. _dispatchEventFromSandbox(actions, jsEvent) {
  13833. const commonActions = this._commonActions;
  13834. for (const name of Object.keys(jsEvent.detail)) {
  13835. const action = actions[name] || commonActions[name];
  13836. action?.(jsEvent);
  13837. }
  13838. }
  13839. _setDefaultPropertiesFromJS(element) {
  13840. if (!this.enableScripting) {
  13841. return;
  13842. }
  13843. const storedData = this.annotationStorage.getRawValue(this.data.id);
  13844. if (!storedData) {
  13845. return;
  13846. }
  13847. const commonActions = this._commonActions;
  13848. for (const [actionName, detail] of Object.entries(storedData)) {
  13849. const action = commonActions[actionName];
  13850. if (action) {
  13851. const eventProxy = {
  13852. detail: {
  13853. [actionName]: detail
  13854. },
  13855. target: element
  13856. };
  13857. action(eventProxy);
  13858. delete storedData[actionName];
  13859. }
  13860. }
  13861. }
  13862. _createQuadrilaterals() {
  13863. if (!this.container) {
  13864. return;
  13865. }
  13866. const {
  13867. quadPoints
  13868. } = this.data;
  13869. if (!quadPoints) {
  13870. return;
  13871. }
  13872. const [rectBlX, rectBlY, rectTrX, rectTrY] = this.data.rect.map(x => Math.fround(x));
  13873. if (quadPoints.length === 8) {
  13874. const [trX, trY, blX, blY] = quadPoints.subarray(2, 6);
  13875. if (rectTrX === trX && rectTrY === trY && rectBlX === blX && rectBlY === blY) {
  13876. return;
  13877. }
  13878. }
  13879. const {
  13880. style
  13881. } = this.container;
  13882. let svgBuffer;
  13883. if (this.#hasBorder) {
  13884. const {
  13885. borderColor,
  13886. borderWidth
  13887. } = style;
  13888. style.borderWidth = 0;
  13889. svgBuffer = ["url('data:image/svg+xml;utf8,", `<svg xmlns="http://www.w3.org/2000/svg"`, ` preserveAspectRatio="none" viewBox="0 0 1 1">`, `<g fill="transparent" stroke="${borderColor}" stroke-width="${borderWidth}">`];
  13890. this.container.classList.add("hasBorder");
  13891. }
  13892. const width = rectTrX - rectBlX;
  13893. const height = rectTrY - rectBlY;
  13894. const {
  13895. svgFactory
  13896. } = this;
  13897. const svg = svgFactory.createElement("svg");
  13898. svg.classList.add("quadrilateralsContainer");
  13899. svg.setAttribute("width", 0);
  13900. svg.setAttribute("height", 0);
  13901. const defs = svgFactory.createElement("defs");
  13902. svg.append(defs);
  13903. const clipPath = svgFactory.createElement("clipPath");
  13904. const id = `clippath_${this.data.id}`;
  13905. clipPath.setAttribute("id", id);
  13906. clipPath.setAttribute("clipPathUnits", "objectBoundingBox");
  13907. defs.append(clipPath);
  13908. for (let i = 2, ii = quadPoints.length; i < ii; i += 8) {
  13909. const trX = quadPoints[i];
  13910. const trY = quadPoints[i + 1];
  13911. const blX = quadPoints[i + 2];
  13912. const blY = quadPoints[i + 3];
  13913. const rect = svgFactory.createElement("rect");
  13914. const x = (blX - rectBlX) / width;
  13915. const y = (rectTrY - trY) / height;
  13916. const rectWidth = (trX - blX) / width;
  13917. const rectHeight = (trY - blY) / height;
  13918. rect.setAttribute("x", x);
  13919. rect.setAttribute("y", y);
  13920. rect.setAttribute("width", rectWidth);
  13921. rect.setAttribute("height", rectHeight);
  13922. clipPath.append(rect);
  13923. svgBuffer?.push(`<rect vector-effect="non-scaling-stroke" x="${x}" y="${y}" width="${rectWidth}" height="${rectHeight}"/>`);
  13924. }
  13925. if (this.#hasBorder) {
  13926. svgBuffer.push(`</g></svg>')`);
  13927. style.backgroundImage = svgBuffer.join("");
  13928. }
  13929. this.container.append(svg);
  13930. this.container.style.clipPath = `url(#${id})`;
  13931. }
  13932. _createPopup() {
  13933. const {
  13934. data
  13935. } = this;
  13936. const popup = this.#popupElement = new PopupAnnotationElement({
  13937. data: {
  13938. color: data.color,
  13939. titleObj: data.titleObj,
  13940. modificationDate: data.modificationDate,
  13941. contentsObj: data.contentsObj,
  13942. richText: data.richText,
  13943. parentRect: data.rect,
  13944. borderStyle: 0,
  13945. id: `popup_${data.id}`,
  13946. rotation: data.rotation
  13947. },
  13948. parent: this.parent,
  13949. elements: [this]
  13950. });
  13951. this.parent.div.append(popup.render());
  13952. }
  13953. render() {
  13954. unreachable("Abstract method `AnnotationElement.render` called");
  13955. }
  13956. _getElementsByName(name, skipId = null) {
  13957. const fields = [];
  13958. if (this._fieldObjects) {
  13959. const fieldObj = this._fieldObjects[name];
  13960. if (fieldObj) {
  13961. for (const {
  13962. page,
  13963. id,
  13964. exportValues
  13965. } of fieldObj) {
  13966. if (page === -1) {
  13967. continue;
  13968. }
  13969. if (id === skipId) {
  13970. continue;
  13971. }
  13972. const exportValue = typeof exportValues === "string" ? exportValues : null;
  13973. const domElement = document.querySelector(`[data-element-id="${id}"]`);
  13974. if (domElement && !GetElementsByNameSet.has(domElement)) {
  13975. warn(`_getElementsByName - element not allowed: ${id}`);
  13976. continue;
  13977. }
  13978. fields.push({
  13979. id,
  13980. exportValue,
  13981. domElement
  13982. });
  13983. }
  13984. }
  13985. return fields;
  13986. }
  13987. for (const domElement of document.getElementsByName(name)) {
  13988. const {
  13989. exportValue
  13990. } = domElement;
  13991. const id = domElement.getAttribute("data-element-id");
  13992. if (id === skipId) {
  13993. continue;
  13994. }
  13995. if (!GetElementsByNameSet.has(domElement)) {
  13996. continue;
  13997. }
  13998. fields.push({
  13999. id,
  14000. exportValue,
  14001. domElement
  14002. });
  14003. }
  14004. return fields;
  14005. }
  14006. show() {
  14007. if (this.container) {
  14008. this.container.hidden = false;
  14009. }
  14010. this.popup?.maybeShow();
  14011. }
  14012. hide() {
  14013. if (this.container) {
  14014. this.container.hidden = true;
  14015. }
  14016. this.popup?.forceHide();
  14017. }
  14018. getElementsToTriggerPopup() {
  14019. return this.container;
  14020. }
  14021. addHighlightArea() {
  14022. const triggers = this.getElementsToTriggerPopup();
  14023. if (Array.isArray(triggers)) {
  14024. for (const element of triggers) {
  14025. element.classList.add("highlightArea");
  14026. }
  14027. } else {
  14028. triggers.classList.add("highlightArea");
  14029. }
  14030. }
  14031. _editOnDoubleClick() {
  14032. if (!this._isEditable) {
  14033. return;
  14034. }
  14035. const {
  14036. annotationEditorType: mode,
  14037. data: {
  14038. id: editId
  14039. }
  14040. } = this;
  14041. this.container.addEventListener("dblclick", () => {
  14042. this.linkService.eventBus?.dispatch("switchannotationeditormode", {
  14043. source: this,
  14044. mode,
  14045. editId
  14046. });
  14047. });
  14048. }
  14049. get width() {
  14050. return this.data.rect[2] - this.data.rect[0];
  14051. }
  14052. get height() {
  14053. return this.data.rect[3] - this.data.rect[1];
  14054. }
  14055. }
  14056. class LinkAnnotationElement extends AnnotationElement {
  14057. constructor(parameters, options = null) {
  14058. super(parameters, {
  14059. isRenderable: true,
  14060. ignoreBorder: !!options?.ignoreBorder,
  14061. createQuadrilaterals: true
  14062. });
  14063. this.isTooltipOnly = parameters.data.isTooltipOnly;
  14064. }
  14065. render() {
  14066. const {
  14067. data,
  14068. linkService
  14069. } = this;
  14070. const link = document.createElement("a");
  14071. link.setAttribute("data-element-id", data.id);
  14072. let isBound = false;
  14073. if (data.url) {
  14074. linkService.addLinkAttributes(link, data.url, data.newWindow);
  14075. isBound = true;
  14076. } else if (data.action) {
  14077. this._bindNamedAction(link, data.action);
  14078. isBound = true;
  14079. } else if (data.attachment) {
  14080. this.#bindAttachment(link, data.attachment, data.attachmentDest);
  14081. isBound = true;
  14082. } else if (data.setOCGState) {
  14083. this.#bindSetOCGState(link, data.setOCGState);
  14084. isBound = true;
  14085. } else if (data.dest) {
  14086. this._bindLink(link, data.dest);
  14087. isBound = true;
  14088. } else {
  14089. if (data.actions && (data.actions.Action || data.actions["Mouse Up"] || data.actions["Mouse Down"]) && this.enableScripting && this.hasJSActions) {
  14090. this._bindJSAction(link, data);
  14091. isBound = true;
  14092. }
  14093. if (data.resetForm) {
  14094. this._bindResetFormAction(link, data.resetForm);
  14095. isBound = true;
  14096. } else if (this.isTooltipOnly && !isBound) {
  14097. this._bindLink(link, "");
  14098. isBound = true;
  14099. }
  14100. }
  14101. this.container.classList.add("linkAnnotation");
  14102. if (isBound) {
  14103. this.container.append(link);
  14104. }
  14105. return this.container;
  14106. }
  14107. #setInternalLink() {
  14108. this.container.setAttribute("data-internal-link", "");
  14109. }
  14110. _bindLink(link, destination) {
  14111. link.href = this.linkService.getDestinationHash(destination);
  14112. link.onclick = () => {
  14113. if (destination) {
  14114. this.linkService.goToDestination(destination);
  14115. }
  14116. return false;
  14117. };
  14118. if (destination || destination === "") {
  14119. this.#setInternalLink();
  14120. }
  14121. }
  14122. _bindNamedAction(link, action) {
  14123. link.href = this.linkService.getAnchorUrl("");
  14124. link.onclick = () => {
  14125. this.linkService.executeNamedAction(action);
  14126. return false;
  14127. };
  14128. this.#setInternalLink();
  14129. }
  14130. #bindAttachment(link, attachment, dest = null) {
  14131. link.href = this.linkService.getAnchorUrl("");
  14132. if (attachment.description) {
  14133. link.title = attachment.description;
  14134. }
  14135. link.onclick = () => {
  14136. this.downloadManager?.openOrDownloadData(attachment.content, attachment.filename, dest);
  14137. return false;
  14138. };
  14139. this.#setInternalLink();
  14140. }
  14141. #bindSetOCGState(link, action) {
  14142. link.href = this.linkService.getAnchorUrl("");
  14143. link.onclick = () => {
  14144. this.linkService.executeSetOCGState(action);
  14145. return false;
  14146. };
  14147. this.#setInternalLink();
  14148. }
  14149. _bindJSAction(link, data) {
  14150. link.href = this.linkService.getAnchorUrl("");
  14151. const map = new Map([["Action", "onclick"], ["Mouse Up", "onmouseup"], ["Mouse Down", "onmousedown"]]);
  14152. for (const name of Object.keys(data.actions)) {
  14153. const jsName = map.get(name);
  14154. if (!jsName) {
  14155. continue;
  14156. }
  14157. link[jsName] = () => {
  14158. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14159. source: this,
  14160. detail: {
  14161. id: data.id,
  14162. name
  14163. }
  14164. });
  14165. return false;
  14166. };
  14167. }
  14168. if (!link.onclick) {
  14169. link.onclick = () => false;
  14170. }
  14171. this.#setInternalLink();
  14172. }
  14173. _bindResetFormAction(link, resetForm) {
  14174. const otherClickAction = link.onclick;
  14175. if (!otherClickAction) {
  14176. link.href = this.linkService.getAnchorUrl("");
  14177. }
  14178. this.#setInternalLink();
  14179. if (!this._fieldObjects) {
  14180. warn(`_bindResetFormAction - "resetForm" action not supported, ` + "ensure that the `fieldObjects` parameter is provided.");
  14181. if (!otherClickAction) {
  14182. link.onclick = () => false;
  14183. }
  14184. return;
  14185. }
  14186. link.onclick = () => {
  14187. otherClickAction?.();
  14188. const {
  14189. fields: resetFormFields,
  14190. refs: resetFormRefs,
  14191. include
  14192. } = resetForm;
  14193. const allFields = [];
  14194. if (resetFormFields.length !== 0 || resetFormRefs.length !== 0) {
  14195. const fieldIds = new Set(resetFormRefs);
  14196. for (const fieldName of resetFormFields) {
  14197. const fields = this._fieldObjects[fieldName] || [];
  14198. for (const {
  14199. id
  14200. } of fields) {
  14201. fieldIds.add(id);
  14202. }
  14203. }
  14204. for (const fields of Object.values(this._fieldObjects)) {
  14205. for (const field of fields) {
  14206. if (fieldIds.has(field.id) === include) {
  14207. allFields.push(field);
  14208. }
  14209. }
  14210. }
  14211. } else {
  14212. for (const fields of Object.values(this._fieldObjects)) {
  14213. allFields.push(...fields);
  14214. }
  14215. }
  14216. const storage = this.annotationStorage;
  14217. const allIds = [];
  14218. for (const field of allFields) {
  14219. const {
  14220. id
  14221. } = field;
  14222. allIds.push(id);
  14223. switch (field.type) {
  14224. case "text":
  14225. {
  14226. const value = field.defaultValue || "";
  14227. storage.setValue(id, {
  14228. value
  14229. });
  14230. break;
  14231. }
  14232. case "checkbox":
  14233. case "radiobutton":
  14234. {
  14235. const value = field.defaultValue === field.exportValues;
  14236. storage.setValue(id, {
  14237. value
  14238. });
  14239. break;
  14240. }
  14241. case "combobox":
  14242. case "listbox":
  14243. {
  14244. const value = field.defaultValue || "";
  14245. storage.setValue(id, {
  14246. value
  14247. });
  14248. break;
  14249. }
  14250. default:
  14251. continue;
  14252. }
  14253. const domElement = document.querySelector(`[data-element-id="${id}"]`);
  14254. if (!domElement) {
  14255. continue;
  14256. } else if (!GetElementsByNameSet.has(domElement)) {
  14257. warn(`_bindResetFormAction - element not allowed: ${id}`);
  14258. continue;
  14259. }
  14260. domElement.dispatchEvent(new Event("resetform"));
  14261. }
  14262. if (this.enableScripting) {
  14263. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14264. source: this,
  14265. detail: {
  14266. id: "app",
  14267. ids: allIds,
  14268. name: "ResetForm"
  14269. }
  14270. });
  14271. }
  14272. return false;
  14273. };
  14274. }
  14275. }
  14276. class TextAnnotationElement extends AnnotationElement {
  14277. constructor(parameters) {
  14278. super(parameters, {
  14279. isRenderable: true
  14280. });
  14281. }
  14282. render() {
  14283. this.container.classList.add("textAnnotation");
  14284. const image = document.createElement("img");
  14285. image.src = this.imageResourcesPath + "annotation-" + this.data.name.toLowerCase() + ".svg";
  14286. image.setAttribute("data-l10n-id", "pdfjs-text-annotation-type");
  14287. image.setAttribute("data-l10n-args", JSON.stringify({
  14288. type: this.data.name
  14289. }));
  14290. if (!this.data.popupRef && this.hasPopupData) {
  14291. this._createPopup();
  14292. }
  14293. this.container.append(image);
  14294. return this.container;
  14295. }
  14296. }
  14297. class WidgetAnnotationElement extends AnnotationElement {
  14298. render() {
  14299. return this.container;
  14300. }
  14301. showElementAndHideCanvas(element) {
  14302. if (this.data.hasOwnCanvas) {
  14303. if (element.previousSibling?.nodeName === "CANVAS") {
  14304. element.previousSibling.hidden = true;
  14305. }
  14306. element.hidden = false;
  14307. }
  14308. }
  14309. _getKeyModifier(event) {
  14310. return util_FeatureTest.platform.isMac ? event.metaKey : event.ctrlKey;
  14311. }
  14312. _setEventListener(element, elementData, baseName, eventName, valueGetter) {
  14313. if (baseName.includes("mouse")) {
  14314. element.addEventListener(baseName, event => {
  14315. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14316. source: this,
  14317. detail: {
  14318. id: this.data.id,
  14319. name: eventName,
  14320. value: valueGetter(event),
  14321. shift: event.shiftKey,
  14322. modifier: this._getKeyModifier(event)
  14323. }
  14324. });
  14325. });
  14326. } else {
  14327. element.addEventListener(baseName, event => {
  14328. if (baseName === "blur") {
  14329. if (!elementData.focused || !event.relatedTarget) {
  14330. return;
  14331. }
  14332. elementData.focused = false;
  14333. } else if (baseName === "focus") {
  14334. if (elementData.focused) {
  14335. return;
  14336. }
  14337. elementData.focused = true;
  14338. }
  14339. if (!valueGetter) {
  14340. return;
  14341. }
  14342. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14343. source: this,
  14344. detail: {
  14345. id: this.data.id,
  14346. name: eventName,
  14347. value: valueGetter(event)
  14348. }
  14349. });
  14350. });
  14351. }
  14352. }
  14353. _setEventListeners(element, elementData, names, getter) {
  14354. for (const [baseName, eventName] of names) {
  14355. if (eventName === "Action" || this.data.actions?.[eventName]) {
  14356. if (eventName === "Focus" || eventName === "Blur") {
  14357. elementData ||= {
  14358. focused: false
  14359. };
  14360. }
  14361. this._setEventListener(element, elementData, baseName, eventName, getter);
  14362. if (eventName === "Focus" && !this.data.actions?.Blur) {
  14363. this._setEventListener(element, elementData, "blur", "Blur", null);
  14364. } else if (eventName === "Blur" && !this.data.actions?.Focus) {
  14365. this._setEventListener(element, elementData, "focus", "Focus", null);
  14366. }
  14367. }
  14368. }
  14369. }
  14370. _setBackgroundColor(element) {
  14371. const color = this.data.backgroundColor || null;
  14372. element.style.backgroundColor = color === null ? "transparent" : Util.makeHexColor(color[0], color[1], color[2]);
  14373. }
  14374. _setTextStyle(element) {
  14375. const TEXT_ALIGNMENT = ["left", "center", "right"];
  14376. const {
  14377. fontColor
  14378. } = this.data.defaultAppearanceData;
  14379. const fontSize = this.data.defaultAppearanceData.fontSize || annotation_layer_DEFAULT_FONT_SIZE;
  14380. const style = element.style;
  14381. let computedFontSize;
  14382. const BORDER_SIZE = 2;
  14383. const roundToOneDecimal = x => Math.round(10 * x) / 10;
  14384. if (this.data.multiLine) {
  14385. const height = Math.abs(this.data.rect[3] - this.data.rect[1] - BORDER_SIZE);
  14386. const numberOfLines = Math.round(height / (LINE_FACTOR * fontSize)) || 1;
  14387. const lineHeight = height / numberOfLines;
  14388. computedFontSize = Math.min(fontSize, roundToOneDecimal(lineHeight / LINE_FACTOR));
  14389. } else {
  14390. const height = Math.abs(this.data.rect[3] - this.data.rect[1] - BORDER_SIZE);
  14391. computedFontSize = Math.min(fontSize, roundToOneDecimal(height / LINE_FACTOR));
  14392. }
  14393. style.fontSize = `calc(${computedFontSize}px * var(--total-scale-factor))`;
  14394. style.color = Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
  14395. if (this.data.textAlignment !== null) {
  14396. style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  14397. }
  14398. }
  14399. _setRequired(element, isRequired) {
  14400. if (isRequired) {
  14401. element.setAttribute("required", true);
  14402. } else {
  14403. element.removeAttribute("required");
  14404. }
  14405. element.setAttribute("aria-required", isRequired);
  14406. }
  14407. }
  14408. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  14409. constructor(parameters) {
  14410. const isRenderable = parameters.renderForms || parameters.data.hasOwnCanvas || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  14411. super(parameters, {
  14412. isRenderable
  14413. });
  14414. }
  14415. setPropertyOnSiblings(base, key, value, keyInStorage) {
  14416. const storage = this.annotationStorage;
  14417. for (const element of this._getElementsByName(base.name, base.id)) {
  14418. if (element.domElement) {
  14419. element.domElement[key] = value;
  14420. }
  14421. storage.setValue(element.id, {
  14422. [keyInStorage]: value
  14423. });
  14424. }
  14425. }
  14426. render() {
  14427. const storage = this.annotationStorage;
  14428. const id = this.data.id;
  14429. this.container.classList.add("textWidgetAnnotation");
  14430. let element = null;
  14431. if (this.renderForms) {
  14432. const storedData = storage.getValue(id, {
  14433. value: this.data.fieldValue
  14434. });
  14435. let textContent = storedData.value || "";
  14436. const maxLen = storage.getValue(id, {
  14437. charLimit: this.data.maxLen
  14438. }).charLimit;
  14439. if (maxLen && textContent.length > maxLen) {
  14440. textContent = textContent.slice(0, maxLen);
  14441. }
  14442. let fieldFormattedValues = storedData.formattedValue || this.data.textContent?.join("\n") || null;
  14443. if (fieldFormattedValues && this.data.comb) {
  14444. fieldFormattedValues = fieldFormattedValues.replaceAll(/\s+/g, "");
  14445. }
  14446. const elementData = {
  14447. userValue: textContent,
  14448. formattedValue: fieldFormattedValues,
  14449. lastCommittedValue: null,
  14450. commitKey: 1,
  14451. focused: false
  14452. };
  14453. if (this.data.multiLine) {
  14454. element = document.createElement("textarea");
  14455. element.textContent = fieldFormattedValues ?? textContent;
  14456. if (this.data.doNotScroll) {
  14457. element.style.overflowY = "hidden";
  14458. }
  14459. } else {
  14460. element = document.createElement("input");
  14461. element.type = this.data.password ? "password" : "text";
  14462. element.setAttribute("value", fieldFormattedValues ?? textContent);
  14463. if (this.data.doNotScroll) {
  14464. element.style.overflowX = "hidden";
  14465. }
  14466. }
  14467. if (this.data.hasOwnCanvas) {
  14468. element.hidden = true;
  14469. }
  14470. GetElementsByNameSet.add(element);
  14471. element.setAttribute("data-element-id", id);
  14472. element.disabled = this.data.readOnly;
  14473. element.name = this.data.fieldName;
  14474. element.tabIndex = DEFAULT_TAB_INDEX;
  14475. this._setRequired(element, this.data.required);
  14476. if (maxLen) {
  14477. element.maxLength = maxLen;
  14478. }
  14479. element.addEventListener("input", event => {
  14480. storage.setValue(id, {
  14481. value: event.target.value
  14482. });
  14483. this.setPropertyOnSiblings(element, "value", event.target.value, "value");
  14484. elementData.formattedValue = null;
  14485. });
  14486. element.addEventListener("resetform", event => {
  14487. const defaultValue = this.data.defaultFieldValue ?? "";
  14488. element.value = elementData.userValue = defaultValue;
  14489. elementData.formattedValue = null;
  14490. });
  14491. let blurListener = event => {
  14492. const {
  14493. formattedValue
  14494. } = elementData;
  14495. if (formattedValue !== null && formattedValue !== undefined) {
  14496. event.target.value = formattedValue;
  14497. }
  14498. event.target.scrollLeft = 0;
  14499. };
  14500. if (this.enableScripting && this.hasJSActions) {
  14501. element.addEventListener("focus", event => {
  14502. if (elementData.focused) {
  14503. return;
  14504. }
  14505. const {
  14506. target
  14507. } = event;
  14508. if (elementData.userValue) {
  14509. target.value = elementData.userValue;
  14510. }
  14511. elementData.lastCommittedValue = target.value;
  14512. elementData.commitKey = 1;
  14513. if (!this.data.actions?.Focus) {
  14514. elementData.focused = true;
  14515. }
  14516. });
  14517. element.addEventListener("updatefromsandbox", jsEvent => {
  14518. this.showElementAndHideCanvas(jsEvent.target);
  14519. const actions = {
  14520. value(event) {
  14521. elementData.userValue = event.detail.value ?? "";
  14522. storage.setValue(id, {
  14523. value: elementData.userValue.toString()
  14524. });
  14525. event.target.value = elementData.userValue;
  14526. },
  14527. formattedValue(event) {
  14528. const {
  14529. formattedValue
  14530. } = event.detail;
  14531. elementData.formattedValue = formattedValue;
  14532. if (formattedValue !== null && formattedValue !== undefined && event.target !== document.activeElement) {
  14533. event.target.value = formattedValue;
  14534. }
  14535. storage.setValue(id, {
  14536. formattedValue
  14537. });
  14538. },
  14539. selRange(event) {
  14540. event.target.setSelectionRange(...event.detail.selRange);
  14541. },
  14542. charLimit: event => {
  14543. const {
  14544. charLimit
  14545. } = event.detail;
  14546. const {
  14547. target
  14548. } = event;
  14549. if (charLimit === 0) {
  14550. target.removeAttribute("maxLength");
  14551. return;
  14552. }
  14553. target.setAttribute("maxLength", charLimit);
  14554. let value = elementData.userValue;
  14555. if (!value || value.length <= charLimit) {
  14556. return;
  14557. }
  14558. value = value.slice(0, charLimit);
  14559. target.value = elementData.userValue = value;
  14560. storage.setValue(id, {
  14561. value
  14562. });
  14563. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14564. source: this,
  14565. detail: {
  14566. id,
  14567. name: "Keystroke",
  14568. value,
  14569. willCommit: true,
  14570. commitKey: 1,
  14571. selStart: target.selectionStart,
  14572. selEnd: target.selectionEnd
  14573. }
  14574. });
  14575. }
  14576. };
  14577. this._dispatchEventFromSandbox(actions, jsEvent);
  14578. });
  14579. element.addEventListener("keydown", event => {
  14580. elementData.commitKey = 1;
  14581. let commitKey = -1;
  14582. if (event.key === "Escape") {
  14583. commitKey = 0;
  14584. } else if (event.key === "Enter" && !this.data.multiLine) {
  14585. commitKey = 2;
  14586. } else if (event.key === "Tab") {
  14587. elementData.commitKey = 3;
  14588. }
  14589. if (commitKey === -1) {
  14590. return;
  14591. }
  14592. const {
  14593. value
  14594. } = event.target;
  14595. if (elementData.lastCommittedValue === value) {
  14596. return;
  14597. }
  14598. elementData.lastCommittedValue = value;
  14599. elementData.userValue = value;
  14600. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14601. source: this,
  14602. detail: {
  14603. id,
  14604. name: "Keystroke",
  14605. value,
  14606. willCommit: true,
  14607. commitKey,
  14608. selStart: event.target.selectionStart,
  14609. selEnd: event.target.selectionEnd
  14610. }
  14611. });
  14612. });
  14613. const _blurListener = blurListener;
  14614. blurListener = null;
  14615. element.addEventListener("blur", event => {
  14616. if (!elementData.focused || !event.relatedTarget) {
  14617. return;
  14618. }
  14619. if (!this.data.actions?.Blur) {
  14620. elementData.focused = false;
  14621. }
  14622. const {
  14623. value
  14624. } = event.target;
  14625. elementData.userValue = value;
  14626. if (elementData.lastCommittedValue !== value) {
  14627. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14628. source: this,
  14629. detail: {
  14630. id,
  14631. name: "Keystroke",
  14632. value,
  14633. willCommit: true,
  14634. commitKey: elementData.commitKey,
  14635. selStart: event.target.selectionStart,
  14636. selEnd: event.target.selectionEnd
  14637. }
  14638. });
  14639. }
  14640. _blurListener(event);
  14641. });
  14642. if (this.data.actions?.Keystroke) {
  14643. element.addEventListener("beforeinput", event => {
  14644. elementData.lastCommittedValue = null;
  14645. const {
  14646. data,
  14647. target
  14648. } = event;
  14649. const {
  14650. value,
  14651. selectionStart,
  14652. selectionEnd
  14653. } = target;
  14654. let selStart = selectionStart,
  14655. selEnd = selectionEnd;
  14656. switch (event.inputType) {
  14657. case "deleteWordBackward":
  14658. {
  14659. const match = value.substring(0, selectionStart).match(/\w*[^\w]*$/);
  14660. if (match) {
  14661. selStart -= match[0].length;
  14662. }
  14663. break;
  14664. }
  14665. case "deleteWordForward":
  14666. {
  14667. const match = value.substring(selectionStart).match(/^[^\w]*\w*/);
  14668. if (match) {
  14669. selEnd += match[0].length;
  14670. }
  14671. break;
  14672. }
  14673. case "deleteContentBackward":
  14674. if (selectionStart === selectionEnd) {
  14675. selStart -= 1;
  14676. }
  14677. break;
  14678. case "deleteContentForward":
  14679. if (selectionStart === selectionEnd) {
  14680. selEnd += 1;
  14681. }
  14682. break;
  14683. }
  14684. event.preventDefault();
  14685. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14686. source: this,
  14687. detail: {
  14688. id,
  14689. name: "Keystroke",
  14690. value,
  14691. change: data || "",
  14692. willCommit: false,
  14693. selStart,
  14694. selEnd
  14695. }
  14696. });
  14697. });
  14698. }
  14699. this._setEventListeners(element, elementData, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.value);
  14700. }
  14701. if (blurListener) {
  14702. element.addEventListener("blur", blurListener);
  14703. }
  14704. if (this.data.comb) {
  14705. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  14706. const combWidth = fieldWidth / maxLen;
  14707. element.classList.add("comb");
  14708. element.style.letterSpacing = `calc(${combWidth}px * var(--total-scale-factor) - 1ch)`;
  14709. }
  14710. } else {
  14711. element = document.createElement("div");
  14712. element.textContent = this.data.fieldValue;
  14713. element.style.verticalAlign = "middle";
  14714. element.style.display = "table-cell";
  14715. if (this.data.hasOwnCanvas) {
  14716. element.hidden = true;
  14717. }
  14718. }
  14719. this._setTextStyle(element);
  14720. this._setBackgroundColor(element);
  14721. this._setDefaultPropertiesFromJS(element);
  14722. this.container.append(element);
  14723. return this.container;
  14724. }
  14725. }
  14726. class SignatureWidgetAnnotationElement extends WidgetAnnotationElement {
  14727. constructor(parameters) {
  14728. super(parameters, {
  14729. isRenderable: !!parameters.data.hasOwnCanvas
  14730. });
  14731. }
  14732. }
  14733. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  14734. constructor(parameters) {
  14735. super(parameters, {
  14736. isRenderable: parameters.renderForms
  14737. });
  14738. }
  14739. render() {
  14740. const storage = this.annotationStorage;
  14741. const data = this.data;
  14742. const id = data.id;
  14743. let value = storage.getValue(id, {
  14744. value: data.exportValue === data.fieldValue
  14745. }).value;
  14746. if (typeof value === "string") {
  14747. value = value !== "Off";
  14748. storage.setValue(id, {
  14749. value
  14750. });
  14751. }
  14752. this.container.classList.add("buttonWidgetAnnotation", "checkBox");
  14753. const element = document.createElement("input");
  14754. GetElementsByNameSet.add(element);
  14755. element.setAttribute("data-element-id", id);
  14756. element.disabled = data.readOnly;
  14757. this._setRequired(element, this.data.required);
  14758. element.type = "checkbox";
  14759. element.name = data.fieldName;
  14760. if (value) {
  14761. element.setAttribute("checked", true);
  14762. }
  14763. element.setAttribute("exportValue", data.exportValue);
  14764. element.tabIndex = DEFAULT_TAB_INDEX;
  14765. element.addEventListener("change", event => {
  14766. const {
  14767. name,
  14768. checked
  14769. } = event.target;
  14770. for (const checkbox of this._getElementsByName(name, id)) {
  14771. const curChecked = checked && checkbox.exportValue === data.exportValue;
  14772. if (checkbox.domElement) {
  14773. checkbox.domElement.checked = curChecked;
  14774. }
  14775. storage.setValue(checkbox.id, {
  14776. value: curChecked
  14777. });
  14778. }
  14779. storage.setValue(id, {
  14780. value: checked
  14781. });
  14782. });
  14783. element.addEventListener("resetform", event => {
  14784. const defaultValue = data.defaultFieldValue || "Off";
  14785. event.target.checked = defaultValue === data.exportValue;
  14786. });
  14787. if (this.enableScripting && this.hasJSActions) {
  14788. element.addEventListener("updatefromsandbox", jsEvent => {
  14789. const actions = {
  14790. value(event) {
  14791. event.target.checked = event.detail.value !== "Off";
  14792. storage.setValue(id, {
  14793. value: event.target.checked
  14794. });
  14795. }
  14796. };
  14797. this._dispatchEventFromSandbox(actions, jsEvent);
  14798. });
  14799. this._setEventListeners(element, null, [["change", "Validate"], ["change", "Action"], ["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  14800. }
  14801. this._setBackgroundColor(element);
  14802. this._setDefaultPropertiesFromJS(element);
  14803. this.container.append(element);
  14804. return this.container;
  14805. }
  14806. }
  14807. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  14808. constructor(parameters) {
  14809. super(parameters, {
  14810. isRenderable: parameters.renderForms
  14811. });
  14812. }
  14813. render() {
  14814. this.container.classList.add("buttonWidgetAnnotation", "radioButton");
  14815. const storage = this.annotationStorage;
  14816. const data = this.data;
  14817. const id = data.id;
  14818. let value = storage.getValue(id, {
  14819. value: data.fieldValue === data.buttonValue
  14820. }).value;
  14821. if (typeof value === "string") {
  14822. value = value !== data.buttonValue;
  14823. storage.setValue(id, {
  14824. value
  14825. });
  14826. }
  14827. if (value) {
  14828. for (const radio of this._getElementsByName(data.fieldName, id)) {
  14829. storage.setValue(radio.id, {
  14830. value: false
  14831. });
  14832. }
  14833. }
  14834. const element = document.createElement("input");
  14835. GetElementsByNameSet.add(element);
  14836. element.setAttribute("data-element-id", id);
  14837. element.disabled = data.readOnly;
  14838. this._setRequired(element, this.data.required);
  14839. element.type = "radio";
  14840. element.name = data.fieldName;
  14841. if (value) {
  14842. element.setAttribute("checked", true);
  14843. }
  14844. element.tabIndex = DEFAULT_TAB_INDEX;
  14845. element.addEventListener("change", event => {
  14846. const {
  14847. name,
  14848. checked
  14849. } = event.target;
  14850. for (const radio of this._getElementsByName(name, id)) {
  14851. storage.setValue(radio.id, {
  14852. value: false
  14853. });
  14854. }
  14855. storage.setValue(id, {
  14856. value: checked
  14857. });
  14858. });
  14859. element.addEventListener("resetform", event => {
  14860. const defaultValue = data.defaultFieldValue;
  14861. event.target.checked = defaultValue !== null && defaultValue !== undefined && defaultValue === data.buttonValue;
  14862. });
  14863. if (this.enableScripting && this.hasJSActions) {
  14864. const pdfButtonValue = data.buttonValue;
  14865. element.addEventListener("updatefromsandbox", jsEvent => {
  14866. const actions = {
  14867. value: event => {
  14868. const checked = pdfButtonValue === event.detail.value;
  14869. for (const radio of this._getElementsByName(event.target.name)) {
  14870. const curChecked = checked && radio.id === id;
  14871. if (radio.domElement) {
  14872. radio.domElement.checked = curChecked;
  14873. }
  14874. storage.setValue(radio.id, {
  14875. value: curChecked
  14876. });
  14877. }
  14878. }
  14879. };
  14880. this._dispatchEventFromSandbox(actions, jsEvent);
  14881. });
  14882. this._setEventListeners(element, null, [["change", "Validate"], ["change", "Action"], ["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.checked);
  14883. }
  14884. this._setBackgroundColor(element);
  14885. this._setDefaultPropertiesFromJS(element);
  14886. this.container.append(element);
  14887. return this.container;
  14888. }
  14889. }
  14890. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  14891. constructor(parameters) {
  14892. super(parameters, {
  14893. ignoreBorder: parameters.data.hasAppearance
  14894. });
  14895. }
  14896. render() {
  14897. const container = super.render();
  14898. container.classList.add("buttonWidgetAnnotation", "pushButton");
  14899. const linkElement = container.lastChild;
  14900. if (this.enableScripting && this.hasJSActions && linkElement) {
  14901. this._setDefaultPropertiesFromJS(linkElement);
  14902. linkElement.addEventListener("updatefromsandbox", jsEvent => {
  14903. this._dispatchEventFromSandbox({}, jsEvent);
  14904. });
  14905. }
  14906. return container;
  14907. }
  14908. }
  14909. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  14910. constructor(parameters) {
  14911. super(parameters, {
  14912. isRenderable: parameters.renderForms
  14913. });
  14914. }
  14915. render() {
  14916. this.container.classList.add("choiceWidgetAnnotation");
  14917. const storage = this.annotationStorage;
  14918. const id = this.data.id;
  14919. const storedData = storage.getValue(id, {
  14920. value: this.data.fieldValue
  14921. });
  14922. const selectElement = document.createElement("select");
  14923. GetElementsByNameSet.add(selectElement);
  14924. selectElement.setAttribute("data-element-id", id);
  14925. selectElement.disabled = this.data.readOnly;
  14926. this._setRequired(selectElement, this.data.required);
  14927. selectElement.name = this.data.fieldName;
  14928. selectElement.tabIndex = DEFAULT_TAB_INDEX;
  14929. let addAnEmptyEntry = this.data.combo && this.data.options.length > 0;
  14930. if (!this.data.combo) {
  14931. selectElement.size = this.data.options.length;
  14932. if (this.data.multiSelect) {
  14933. selectElement.multiple = true;
  14934. }
  14935. }
  14936. selectElement.addEventListener("resetform", event => {
  14937. const defaultValue = this.data.defaultFieldValue;
  14938. for (const option of selectElement.options) {
  14939. option.selected = option.value === defaultValue;
  14940. }
  14941. });
  14942. for (const option of this.data.options) {
  14943. const optionElement = document.createElement("option");
  14944. optionElement.textContent = option.displayValue;
  14945. optionElement.value = option.exportValue;
  14946. if (storedData.value.includes(option.exportValue)) {
  14947. optionElement.setAttribute("selected", true);
  14948. addAnEmptyEntry = false;
  14949. }
  14950. selectElement.append(optionElement);
  14951. }
  14952. let removeEmptyEntry = null;
  14953. if (addAnEmptyEntry) {
  14954. const noneOptionElement = document.createElement("option");
  14955. noneOptionElement.value = " ";
  14956. noneOptionElement.setAttribute("hidden", true);
  14957. noneOptionElement.setAttribute("selected", true);
  14958. selectElement.prepend(noneOptionElement);
  14959. removeEmptyEntry = () => {
  14960. noneOptionElement.remove();
  14961. selectElement.removeEventListener("input", removeEmptyEntry);
  14962. removeEmptyEntry = null;
  14963. };
  14964. selectElement.addEventListener("input", removeEmptyEntry);
  14965. }
  14966. const getValue = isExport => {
  14967. const name = isExport ? "value" : "textContent";
  14968. const {
  14969. options,
  14970. multiple
  14971. } = selectElement;
  14972. if (!multiple) {
  14973. return options.selectedIndex === -1 ? null : options[options.selectedIndex][name];
  14974. }
  14975. return Array.prototype.filter.call(options, option => option.selected).map(option => option[name]);
  14976. };
  14977. let selectedValues = getValue(false);
  14978. const getItems = event => {
  14979. const options = event.target.options;
  14980. return Array.prototype.map.call(options, option => ({
  14981. displayValue: option.textContent,
  14982. exportValue: option.value
  14983. }));
  14984. };
  14985. if (this.enableScripting && this.hasJSActions) {
  14986. selectElement.addEventListener("updatefromsandbox", jsEvent => {
  14987. const actions = {
  14988. value(event) {
  14989. removeEmptyEntry?.();
  14990. const value = event.detail.value;
  14991. const values = new Set(Array.isArray(value) ? value : [value]);
  14992. for (const option of selectElement.options) {
  14993. option.selected = values.has(option.value);
  14994. }
  14995. storage.setValue(id, {
  14996. value: getValue(true)
  14997. });
  14998. selectedValues = getValue(false);
  14999. },
  15000. multipleSelection(event) {
  15001. selectElement.multiple = true;
  15002. },
  15003. remove(event) {
  15004. const options = selectElement.options;
  15005. const index = event.detail.remove;
  15006. options[index].selected = false;
  15007. selectElement.remove(index);
  15008. if (options.length > 0) {
  15009. const i = Array.prototype.findIndex.call(options, option => option.selected);
  15010. if (i === -1) {
  15011. options[0].selected = true;
  15012. }
  15013. }
  15014. storage.setValue(id, {
  15015. value: getValue(true),
  15016. items: getItems(event)
  15017. });
  15018. selectedValues = getValue(false);
  15019. },
  15020. clear(event) {
  15021. while (selectElement.length !== 0) {
  15022. selectElement.remove(0);
  15023. }
  15024. storage.setValue(id, {
  15025. value: null,
  15026. items: []
  15027. });
  15028. selectedValues = getValue(false);
  15029. },
  15030. insert(event) {
  15031. const {
  15032. index,
  15033. displayValue,
  15034. exportValue
  15035. } = event.detail.insert;
  15036. const selectChild = selectElement.children[index];
  15037. const optionElement = document.createElement("option");
  15038. optionElement.textContent = displayValue;
  15039. optionElement.value = exportValue;
  15040. if (selectChild) {
  15041. selectChild.before(optionElement);
  15042. } else {
  15043. selectElement.append(optionElement);
  15044. }
  15045. storage.setValue(id, {
  15046. value: getValue(true),
  15047. items: getItems(event)
  15048. });
  15049. selectedValues = getValue(false);
  15050. },
  15051. items(event) {
  15052. const {
  15053. items
  15054. } = event.detail;
  15055. while (selectElement.length !== 0) {
  15056. selectElement.remove(0);
  15057. }
  15058. for (const item of items) {
  15059. const {
  15060. displayValue,
  15061. exportValue
  15062. } = item;
  15063. const optionElement = document.createElement("option");
  15064. optionElement.textContent = displayValue;
  15065. optionElement.value = exportValue;
  15066. selectElement.append(optionElement);
  15067. }
  15068. if (selectElement.options.length > 0) {
  15069. selectElement.options[0].selected = true;
  15070. }
  15071. storage.setValue(id, {
  15072. value: getValue(true),
  15073. items: getItems(event)
  15074. });
  15075. selectedValues = getValue(false);
  15076. },
  15077. indices(event) {
  15078. const indices = new Set(event.detail.indices);
  15079. for (const option of event.target.options) {
  15080. option.selected = indices.has(option.index);
  15081. }
  15082. storage.setValue(id, {
  15083. value: getValue(true)
  15084. });
  15085. selectedValues = getValue(false);
  15086. },
  15087. editable(event) {
  15088. event.target.disabled = !event.detail.editable;
  15089. }
  15090. };
  15091. this._dispatchEventFromSandbox(actions, jsEvent);
  15092. });
  15093. selectElement.addEventListener("input", event => {
  15094. const exportValue = getValue(true);
  15095. const change = getValue(false);
  15096. storage.setValue(id, {
  15097. value: exportValue
  15098. });
  15099. event.preventDefault();
  15100. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  15101. source: this,
  15102. detail: {
  15103. id,
  15104. name: "Keystroke",
  15105. value: selectedValues,
  15106. change,
  15107. changeEx: exportValue,
  15108. willCommit: false,
  15109. commitKey: 1,
  15110. keyDown: false
  15111. }
  15112. });
  15113. });
  15114. this._setEventListeners(selectElement, null, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"], ["input", "Action"], ["input", "Validate"]], event => event.target.value);
  15115. } else {
  15116. selectElement.addEventListener("input", function (event) {
  15117. storage.setValue(id, {
  15118. value: getValue(true)
  15119. });
  15120. });
  15121. }
  15122. if (this.data.combo) {
  15123. this._setTextStyle(selectElement);
  15124. } else {}
  15125. this._setBackgroundColor(selectElement);
  15126. this._setDefaultPropertiesFromJS(selectElement);
  15127. this.container.append(selectElement);
  15128. return this.container;
  15129. }
  15130. }
  15131. class PopupAnnotationElement extends AnnotationElement {
  15132. constructor(parameters) {
  15133. const {
  15134. data,
  15135. elements
  15136. } = parameters;
  15137. super(parameters, {
  15138. isRenderable: AnnotationElement._hasPopupData(data)
  15139. });
  15140. this.elements = elements;
  15141. this.popup = null;
  15142. }
  15143. render() {
  15144. this.container.classList.add("popupAnnotation");
  15145. const popup = this.popup = new PopupElement({
  15146. container: this.container,
  15147. color: this.data.color,
  15148. titleObj: this.data.titleObj,
  15149. modificationDate: this.data.modificationDate,
  15150. contentsObj: this.data.contentsObj,
  15151. richText: this.data.richText,
  15152. rect: this.data.rect,
  15153. parentRect: this.data.parentRect || null,
  15154. parent: this.parent,
  15155. elements: this.elements,
  15156. open: this.data.open
  15157. });
  15158. const elementIds = [];
  15159. for (const element of this.elements) {
  15160. element.popup = popup;
  15161. element.container.ariaHasPopup = "dialog";
  15162. elementIds.push(element.data.id);
  15163. element.addHighlightArea();
  15164. }
  15165. this.container.setAttribute("aria-controls", elementIds.map(id => `${AnnotationPrefix}${id}`).join(","));
  15166. return this.container;
  15167. }
  15168. }
  15169. class PopupElement {
  15170. #boundKeyDown = this.#keyDown.bind(this);
  15171. #boundHide = this.#hide.bind(this);
  15172. #boundShow = this.#show.bind(this);
  15173. #boundToggle = this.#toggle.bind(this);
  15174. #color = null;
  15175. #container = null;
  15176. #contentsObj = null;
  15177. #dateObj = null;
  15178. #elements = null;
  15179. #parent = null;
  15180. #parentRect = null;
  15181. #pinned = false;
  15182. #popup = null;
  15183. #position = null;
  15184. #rect = null;
  15185. #richText = null;
  15186. #titleObj = null;
  15187. #updates = null;
  15188. #wasVisible = false;
  15189. constructor({
  15190. container,
  15191. color,
  15192. elements,
  15193. titleObj,
  15194. modificationDate,
  15195. contentsObj,
  15196. richText,
  15197. parent,
  15198. rect,
  15199. parentRect,
  15200. open
  15201. }) {
  15202. this.#container = container;
  15203. this.#titleObj = titleObj;
  15204. this.#contentsObj = contentsObj;
  15205. this.#richText = richText;
  15206. this.#parent = parent;
  15207. this.#color = color;
  15208. this.#rect = rect;
  15209. this.#parentRect = parentRect;
  15210. this.#elements = elements;
  15211. this.#dateObj = PDFDateString.toDateObject(modificationDate);
  15212. this.trigger = elements.flatMap(e => e.getElementsToTriggerPopup());
  15213. for (const element of this.trigger) {
  15214. element.addEventListener("click", this.#boundToggle);
  15215. element.addEventListener("mouseenter", this.#boundShow);
  15216. element.addEventListener("mouseleave", this.#boundHide);
  15217. element.classList.add("popupTriggerArea");
  15218. }
  15219. for (const element of elements) {
  15220. element.container?.addEventListener("keydown", this.#boundKeyDown);
  15221. }
  15222. this.#container.hidden = true;
  15223. if (open) {
  15224. this.#toggle();
  15225. }
  15226. }
  15227. render() {
  15228. if (this.#popup) {
  15229. return;
  15230. }
  15231. const popup = this.#popup = document.createElement("div");
  15232. popup.className = "popup";
  15233. if (this.#color) {
  15234. const baseColor = popup.style.outlineColor = Util.makeHexColor(...this.#color);
  15235. popup.style.backgroundColor = `color-mix(in srgb, ${baseColor} 30%, white)`;
  15236. }
  15237. const header = document.createElement("span");
  15238. header.className = "header";
  15239. const title = document.createElement("h1");
  15240. header.append(title);
  15241. ({
  15242. dir: title.dir,
  15243. str: title.textContent
  15244. } = this.#titleObj);
  15245. popup.append(header);
  15246. if (this.#dateObj) {
  15247. const modificationDate = document.createElement("span");
  15248. modificationDate.classList.add("popupDate");
  15249. modificationDate.setAttribute("data-l10n-id", "pdfjs-annotation-date-time-string");
  15250. modificationDate.setAttribute("data-l10n-args", JSON.stringify({
  15251. dateObj: this.#dateObj.valueOf()
  15252. }));
  15253. header.append(modificationDate);
  15254. }
  15255. const html = this.#html;
  15256. if (html) {
  15257. XfaLayer.render({
  15258. xfaHtml: html,
  15259. intent: "richText",
  15260. div: popup
  15261. });
  15262. popup.lastChild.classList.add("richText", "popupContent");
  15263. } else {
  15264. const contents = this._formatContents(this.#contentsObj);
  15265. popup.append(contents);
  15266. }
  15267. this.#container.append(popup);
  15268. }
  15269. get #html() {
  15270. const richText = this.#richText;
  15271. const contentsObj = this.#contentsObj;
  15272. if (richText?.str && (!contentsObj?.str || contentsObj.str === richText.str)) {
  15273. return this.#richText.html || null;
  15274. }
  15275. return null;
  15276. }
  15277. get #fontSize() {
  15278. return this.#html?.attributes?.style?.fontSize || 0;
  15279. }
  15280. get #fontColor() {
  15281. return this.#html?.attributes?.style?.color || null;
  15282. }
  15283. #makePopupContent(text) {
  15284. const popupLines = [];
  15285. const popupContent = {
  15286. str: text,
  15287. html: {
  15288. name: "div",
  15289. attributes: {
  15290. dir: "auto"
  15291. },
  15292. children: [{
  15293. name: "p",
  15294. children: popupLines
  15295. }]
  15296. }
  15297. };
  15298. const lineAttributes = {
  15299. style: {
  15300. color: this.#fontColor,
  15301. fontSize: this.#fontSize ? `calc(${this.#fontSize}px * var(--total-scale-factor))` : ""
  15302. }
  15303. };
  15304. for (const line of text.split("\n")) {
  15305. popupLines.push({
  15306. name: "span",
  15307. value: line,
  15308. attributes: lineAttributes
  15309. });
  15310. }
  15311. return popupContent;
  15312. }
  15313. _formatContents({
  15314. str,
  15315. dir
  15316. }) {
  15317. const p = document.createElement("p");
  15318. p.classList.add("popupContent");
  15319. p.dir = dir;
  15320. const lines = str.split(/(?:\r\n?|\n)/);
  15321. for (let i = 0, ii = lines.length; i < ii; ++i) {
  15322. const line = lines[i];
  15323. p.append(document.createTextNode(line));
  15324. if (i < ii - 1) {
  15325. p.append(document.createElement("br"));
  15326. }
  15327. }
  15328. return p;
  15329. }
  15330. #keyDown(event) {
  15331. if (event.altKey || event.shiftKey || event.ctrlKey || event.metaKey) {
  15332. return;
  15333. }
  15334. if (event.key === "Enter" || event.key === "Escape" && this.#pinned) {
  15335. this.#toggle();
  15336. }
  15337. }
  15338. updateEdited({
  15339. rect,
  15340. popupContent
  15341. }) {
  15342. this.#updates ||= {
  15343. contentsObj: this.#contentsObj,
  15344. richText: this.#richText
  15345. };
  15346. if (rect) {
  15347. this.#position = null;
  15348. }
  15349. if (popupContent) {
  15350. this.#richText = this.#makePopupContent(popupContent);
  15351. this.#contentsObj = null;
  15352. }
  15353. this.#popup?.remove();
  15354. this.#popup = null;
  15355. }
  15356. resetEdited() {
  15357. if (!this.#updates) {
  15358. return;
  15359. }
  15360. ({
  15361. contentsObj: this.#contentsObj,
  15362. richText: this.#richText
  15363. } = this.#updates);
  15364. this.#updates = null;
  15365. this.#popup?.remove();
  15366. this.#popup = null;
  15367. this.#position = null;
  15368. }
  15369. #setPosition() {
  15370. if (this.#position !== null) {
  15371. return;
  15372. }
  15373. const {
  15374. page: {
  15375. view
  15376. },
  15377. viewport: {
  15378. rawDims: {
  15379. pageWidth,
  15380. pageHeight,
  15381. pageX,
  15382. pageY
  15383. }
  15384. }
  15385. } = this.#parent;
  15386. let useParentRect = !!this.#parentRect;
  15387. let rect = useParentRect ? this.#parentRect : this.#rect;
  15388. for (const element of this.#elements) {
  15389. if (!rect || Util.intersect(element.data.rect, rect) !== null) {
  15390. rect = element.data.rect;
  15391. useParentRect = true;
  15392. break;
  15393. }
  15394. }
  15395. const normalizedRect = Util.normalizeRect([rect[0], view[3] - rect[1] + view[1], rect[2], view[3] - rect[3] + view[1]]);
  15396. const HORIZONTAL_SPACE_AFTER_ANNOTATION = 5;
  15397. const parentWidth = useParentRect ? rect[2] - rect[0] + HORIZONTAL_SPACE_AFTER_ANNOTATION : 0;
  15398. const popupLeft = normalizedRect[0] + parentWidth;
  15399. const popupTop = normalizedRect[1];
  15400. this.#position = [100 * (popupLeft - pageX) / pageWidth, 100 * (popupTop - pageY) / pageHeight];
  15401. const {
  15402. style
  15403. } = this.#container;
  15404. style.left = `${this.#position[0]}%`;
  15405. style.top = `${this.#position[1]}%`;
  15406. }
  15407. #toggle() {
  15408. this.#pinned = !this.#pinned;
  15409. if (this.#pinned) {
  15410. this.#show();
  15411. this.#container.addEventListener("click", this.#boundToggle);
  15412. this.#container.addEventListener("keydown", this.#boundKeyDown);
  15413. } else {
  15414. this.#hide();
  15415. this.#container.removeEventListener("click", this.#boundToggle);
  15416. this.#container.removeEventListener("keydown", this.#boundKeyDown);
  15417. }
  15418. }
  15419. #show() {
  15420. if (!this.#popup) {
  15421. this.render();
  15422. }
  15423. if (!this.isVisible) {
  15424. this.#setPosition();
  15425. this.#container.hidden = false;
  15426. this.#container.style.zIndex = parseInt(this.#container.style.zIndex) + 1000;
  15427. } else if (this.#pinned) {
  15428. this.#container.classList.add("focused");
  15429. }
  15430. }
  15431. #hide() {
  15432. this.#container.classList.remove("focused");
  15433. if (this.#pinned || !this.isVisible) {
  15434. return;
  15435. }
  15436. this.#container.hidden = true;
  15437. this.#container.style.zIndex = parseInt(this.#container.style.zIndex) - 1000;
  15438. }
  15439. forceHide() {
  15440. this.#wasVisible = this.isVisible;
  15441. if (!this.#wasVisible) {
  15442. return;
  15443. }
  15444. this.#container.hidden = true;
  15445. }
  15446. maybeShow() {
  15447. if (!this.#wasVisible) {
  15448. return;
  15449. }
  15450. if (!this.#popup) {
  15451. this.#show();
  15452. }
  15453. this.#wasVisible = false;
  15454. this.#container.hidden = false;
  15455. }
  15456. get isVisible() {
  15457. return this.#container.hidden === false;
  15458. }
  15459. }
  15460. class FreeTextAnnotationElement extends AnnotationElement {
  15461. constructor(parameters) {
  15462. super(parameters, {
  15463. isRenderable: true,
  15464. ignoreBorder: true
  15465. });
  15466. this.textContent = parameters.data.textContent;
  15467. this.textPosition = parameters.data.textPosition;
  15468. this.annotationEditorType = AnnotationEditorType.FREETEXT;
  15469. }
  15470. render() {
  15471. this.container.classList.add("freeTextAnnotation");
  15472. if (this.textContent) {
  15473. const content = document.createElement("div");
  15474. content.classList.add("annotationTextContent");
  15475. content.setAttribute("role", "comment");
  15476. for (const line of this.textContent) {
  15477. const lineSpan = document.createElement("span");
  15478. lineSpan.textContent = line;
  15479. content.append(lineSpan);
  15480. }
  15481. this.container.append(content);
  15482. }
  15483. if (!this.data.popupRef && this.hasPopupData) {
  15484. this._createPopup();
  15485. }
  15486. this._editOnDoubleClick();
  15487. return this.container;
  15488. }
  15489. }
  15490. class LineAnnotationElement extends AnnotationElement {
  15491. #line = null;
  15492. constructor(parameters) {
  15493. super(parameters, {
  15494. isRenderable: true,
  15495. ignoreBorder: true
  15496. });
  15497. }
  15498. render() {
  15499. this.container.classList.add("lineAnnotation");
  15500. const {
  15501. data,
  15502. width,
  15503. height
  15504. } = this;
  15505. const svg = this.svgFactory.create(width, height, true);
  15506. const line = this.#line = this.svgFactory.createElement("svg:line");
  15507. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  15508. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  15509. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  15510. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  15511. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  15512. line.setAttribute("stroke", "transparent");
  15513. line.setAttribute("fill", "transparent");
  15514. svg.append(line);
  15515. this.container.append(svg);
  15516. if (!data.popupRef && this.hasPopupData) {
  15517. this._createPopup();
  15518. }
  15519. return this.container;
  15520. }
  15521. getElementsToTriggerPopup() {
  15522. return this.#line;
  15523. }
  15524. addHighlightArea() {
  15525. this.container.classList.add("highlightArea");
  15526. }
  15527. }
  15528. class SquareAnnotationElement extends AnnotationElement {
  15529. #square = null;
  15530. constructor(parameters) {
  15531. super(parameters, {
  15532. isRenderable: true,
  15533. ignoreBorder: true
  15534. });
  15535. }
  15536. render() {
  15537. this.container.classList.add("squareAnnotation");
  15538. const {
  15539. data,
  15540. width,
  15541. height
  15542. } = this;
  15543. const svg = this.svgFactory.create(width, height, true);
  15544. const borderWidth = data.borderStyle.width;
  15545. const square = this.#square = this.svgFactory.createElement("svg:rect");
  15546. square.setAttribute("x", borderWidth / 2);
  15547. square.setAttribute("y", borderWidth / 2);
  15548. square.setAttribute("width", width - borderWidth);
  15549. square.setAttribute("height", height - borderWidth);
  15550. square.setAttribute("stroke-width", borderWidth || 1);
  15551. square.setAttribute("stroke", "transparent");
  15552. square.setAttribute("fill", "transparent");
  15553. svg.append(square);
  15554. this.container.append(svg);
  15555. if (!data.popupRef && this.hasPopupData) {
  15556. this._createPopup();
  15557. }
  15558. return this.container;
  15559. }
  15560. getElementsToTriggerPopup() {
  15561. return this.#square;
  15562. }
  15563. addHighlightArea() {
  15564. this.container.classList.add("highlightArea");
  15565. }
  15566. }
  15567. class CircleAnnotationElement extends AnnotationElement {
  15568. #circle = null;
  15569. constructor(parameters) {
  15570. super(parameters, {
  15571. isRenderable: true,
  15572. ignoreBorder: true
  15573. });
  15574. }
  15575. render() {
  15576. this.container.classList.add("circleAnnotation");
  15577. const {
  15578. data,
  15579. width,
  15580. height
  15581. } = this;
  15582. const svg = this.svgFactory.create(width, height, true);
  15583. const borderWidth = data.borderStyle.width;
  15584. const circle = this.#circle = this.svgFactory.createElement("svg:ellipse");
  15585. circle.setAttribute("cx", width / 2);
  15586. circle.setAttribute("cy", height / 2);
  15587. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  15588. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  15589. circle.setAttribute("stroke-width", borderWidth || 1);
  15590. circle.setAttribute("stroke", "transparent");
  15591. circle.setAttribute("fill", "transparent");
  15592. svg.append(circle);
  15593. this.container.append(svg);
  15594. if (!data.popupRef && this.hasPopupData) {
  15595. this._createPopup();
  15596. }
  15597. return this.container;
  15598. }
  15599. getElementsToTriggerPopup() {
  15600. return this.#circle;
  15601. }
  15602. addHighlightArea() {
  15603. this.container.classList.add("highlightArea");
  15604. }
  15605. }
  15606. class PolylineAnnotationElement extends AnnotationElement {
  15607. #polyline = null;
  15608. constructor(parameters) {
  15609. super(parameters, {
  15610. isRenderable: true,
  15611. ignoreBorder: true
  15612. });
  15613. this.containerClassName = "polylineAnnotation";
  15614. this.svgElementName = "svg:polyline";
  15615. }
  15616. render() {
  15617. this.container.classList.add(this.containerClassName);
  15618. const {
  15619. data: {
  15620. rect,
  15621. vertices,
  15622. borderStyle,
  15623. popupRef
  15624. },
  15625. width,
  15626. height
  15627. } = this;
  15628. if (!vertices) {
  15629. return this.container;
  15630. }
  15631. const svg = this.svgFactory.create(width, height, true);
  15632. let points = [];
  15633. for (let i = 0, ii = vertices.length; i < ii; i += 2) {
  15634. const x = vertices[i] - rect[0];
  15635. const y = rect[3] - vertices[i + 1];
  15636. points.push(`${x},${y}`);
  15637. }
  15638. points = points.join(" ");
  15639. const polyline = this.#polyline = this.svgFactory.createElement(this.svgElementName);
  15640. polyline.setAttribute("points", points);
  15641. polyline.setAttribute("stroke-width", borderStyle.width || 1);
  15642. polyline.setAttribute("stroke", "transparent");
  15643. polyline.setAttribute("fill", "transparent");
  15644. svg.append(polyline);
  15645. this.container.append(svg);
  15646. if (!popupRef && this.hasPopupData) {
  15647. this._createPopup();
  15648. }
  15649. return this.container;
  15650. }
  15651. getElementsToTriggerPopup() {
  15652. return this.#polyline;
  15653. }
  15654. addHighlightArea() {
  15655. this.container.classList.add("highlightArea");
  15656. }
  15657. }
  15658. class PolygonAnnotationElement extends PolylineAnnotationElement {
  15659. constructor(parameters) {
  15660. super(parameters);
  15661. this.containerClassName = "polygonAnnotation";
  15662. this.svgElementName = "svg:polygon";
  15663. }
  15664. }
  15665. class CaretAnnotationElement extends AnnotationElement {
  15666. constructor(parameters) {
  15667. super(parameters, {
  15668. isRenderable: true,
  15669. ignoreBorder: true
  15670. });
  15671. }
  15672. render() {
  15673. this.container.classList.add("caretAnnotation");
  15674. if (!this.data.popupRef && this.hasPopupData) {
  15675. this._createPopup();
  15676. }
  15677. return this.container;
  15678. }
  15679. }
  15680. class InkAnnotationElement extends AnnotationElement {
  15681. #polylinesGroupElement = null;
  15682. #polylines = [];
  15683. constructor(parameters) {
  15684. super(parameters, {
  15685. isRenderable: true,
  15686. ignoreBorder: true
  15687. });
  15688. this.containerClassName = "inkAnnotation";
  15689. this.svgElementName = "svg:polyline";
  15690. this.annotationEditorType = this.data.it === "InkHighlight" ? AnnotationEditorType.HIGHLIGHT : AnnotationEditorType.INK;
  15691. }
  15692. #getTransform(rotation, rect) {
  15693. switch (rotation) {
  15694. case 90:
  15695. return {
  15696. transform: `rotate(90) translate(${-rect[0]},${rect[1]}) scale(1,-1)`,
  15697. width: rect[3] - rect[1],
  15698. height: rect[2] - rect[0]
  15699. };
  15700. case 180:
  15701. return {
  15702. transform: `rotate(180) translate(${-rect[2]},${rect[1]}) scale(1,-1)`,
  15703. width: rect[2] - rect[0],
  15704. height: rect[3] - rect[1]
  15705. };
  15706. case 270:
  15707. return {
  15708. transform: `rotate(270) translate(${-rect[2]},${rect[3]}) scale(1,-1)`,
  15709. width: rect[3] - rect[1],
  15710. height: rect[2] - rect[0]
  15711. };
  15712. default:
  15713. return {
  15714. transform: `translate(${-rect[0]},${rect[3]}) scale(1,-1)`,
  15715. width: rect[2] - rect[0],
  15716. height: rect[3] - rect[1]
  15717. };
  15718. }
  15719. }
  15720. render() {
  15721. this.container.classList.add(this.containerClassName);
  15722. const {
  15723. data: {
  15724. rect,
  15725. rotation,
  15726. inkLists,
  15727. borderStyle,
  15728. popupRef
  15729. }
  15730. } = this;
  15731. const {
  15732. transform,
  15733. width,
  15734. height
  15735. } = this.#getTransform(rotation, rect);
  15736. const svg = this.svgFactory.create(width, height, true);
  15737. const g = this.#polylinesGroupElement = this.svgFactory.createElement("svg:g");
  15738. svg.append(g);
  15739. g.setAttribute("stroke-width", borderStyle.width || 1);
  15740. g.setAttribute("stroke-linecap", "round");
  15741. g.setAttribute("stroke-linejoin", "round");
  15742. g.setAttribute("stroke-miterlimit", 10);
  15743. g.setAttribute("stroke", "transparent");
  15744. g.setAttribute("fill", "transparent");
  15745. g.setAttribute("transform", transform);
  15746. for (let i = 0, ii = inkLists.length; i < ii; i++) {
  15747. const polyline = this.svgFactory.createElement(this.svgElementName);
  15748. this.#polylines.push(polyline);
  15749. polyline.setAttribute("points", inkLists[i].join(","));
  15750. g.append(polyline);
  15751. }
  15752. if (!popupRef && this.hasPopupData) {
  15753. this._createPopup();
  15754. }
  15755. this.container.append(svg);
  15756. this._editOnDoubleClick();
  15757. return this.container;
  15758. }
  15759. updateEdited(params) {
  15760. super.updateEdited(params);
  15761. const {
  15762. thickness,
  15763. points,
  15764. rect
  15765. } = params;
  15766. const g = this.#polylinesGroupElement;
  15767. if (thickness >= 0) {
  15768. g.setAttribute("stroke-width", thickness || 1);
  15769. }
  15770. if (points) {
  15771. for (let i = 0, ii = this.#polylines.length; i < ii; i++) {
  15772. this.#polylines[i].setAttribute("points", points[i].join(","));
  15773. }
  15774. }
  15775. if (rect) {
  15776. const {
  15777. transform,
  15778. width,
  15779. height
  15780. } = this.#getTransform(this.data.rotation, rect);
  15781. const root = g.parentElement;
  15782. root.setAttribute("viewBox", `0 0 ${width} ${height}`);
  15783. g.setAttribute("transform", transform);
  15784. }
  15785. }
  15786. getElementsToTriggerPopup() {
  15787. return this.#polylines;
  15788. }
  15789. addHighlightArea() {
  15790. this.container.classList.add("highlightArea");
  15791. }
  15792. }
  15793. class HighlightAnnotationElement extends AnnotationElement {
  15794. constructor(parameters) {
  15795. super(parameters, {
  15796. isRenderable: true,
  15797. ignoreBorder: true,
  15798. createQuadrilaterals: true
  15799. });
  15800. this.annotationEditorType = AnnotationEditorType.HIGHLIGHT;
  15801. }
  15802. render() {
  15803. if (!this.data.popupRef && this.hasPopupData) {
  15804. this._createPopup();
  15805. }
  15806. this.container.classList.add("highlightAnnotation");
  15807. this._editOnDoubleClick();
  15808. return this.container;
  15809. }
  15810. }
  15811. class UnderlineAnnotationElement extends AnnotationElement {
  15812. constructor(parameters) {
  15813. super(parameters, {
  15814. isRenderable: true,
  15815. ignoreBorder: true,
  15816. createQuadrilaterals: true
  15817. });
  15818. }
  15819. render() {
  15820. if (!this.data.popupRef && this.hasPopupData) {
  15821. this._createPopup();
  15822. }
  15823. this.container.classList.add("underlineAnnotation");
  15824. return this.container;
  15825. }
  15826. }
  15827. class SquigglyAnnotationElement extends AnnotationElement {
  15828. constructor(parameters) {
  15829. super(parameters, {
  15830. isRenderable: true,
  15831. ignoreBorder: true,
  15832. createQuadrilaterals: true
  15833. });
  15834. }
  15835. render() {
  15836. if (!this.data.popupRef && this.hasPopupData) {
  15837. this._createPopup();
  15838. }
  15839. this.container.classList.add("squigglyAnnotation");
  15840. return this.container;
  15841. }
  15842. }
  15843. class StrikeOutAnnotationElement extends AnnotationElement {
  15844. constructor(parameters) {
  15845. super(parameters, {
  15846. isRenderable: true,
  15847. ignoreBorder: true,
  15848. createQuadrilaterals: true
  15849. });
  15850. }
  15851. render() {
  15852. if (!this.data.popupRef && this.hasPopupData) {
  15853. this._createPopup();
  15854. }
  15855. this.container.classList.add("strikeoutAnnotation");
  15856. return this.container;
  15857. }
  15858. }
  15859. class StampAnnotationElement extends AnnotationElement {
  15860. constructor(parameters) {
  15861. super(parameters, {
  15862. isRenderable: true,
  15863. ignoreBorder: true
  15864. });
  15865. this.annotationEditorType = AnnotationEditorType.STAMP;
  15866. }
  15867. render() {
  15868. this.container.classList.add("stampAnnotation");
  15869. this.container.setAttribute("role", "img");
  15870. if (!this.data.popupRef && this.hasPopupData) {
  15871. this._createPopup();
  15872. }
  15873. this._editOnDoubleClick();
  15874. return this.container;
  15875. }
  15876. }
  15877. class FileAttachmentAnnotationElement extends AnnotationElement {
  15878. #trigger = null;
  15879. constructor(parameters) {
  15880. super(parameters, {
  15881. isRenderable: true
  15882. });
  15883. const {
  15884. file
  15885. } = this.data;
  15886. this.filename = file.filename;
  15887. this.content = file.content;
  15888. this.linkService.eventBus?.dispatch("fileattachmentannotation", {
  15889. source: this,
  15890. ...file
  15891. });
  15892. }
  15893. render() {
  15894. this.container.classList.add("fileAttachmentAnnotation");
  15895. const {
  15896. container,
  15897. data
  15898. } = this;
  15899. let trigger;
  15900. if (data.hasAppearance || data.fillAlpha === 0) {
  15901. trigger = document.createElement("div");
  15902. } else {
  15903. trigger = document.createElement("img");
  15904. trigger.src = `${this.imageResourcesPath}annotation-${/paperclip/i.test(data.name) ? "paperclip" : "pushpin"}.svg`;
  15905. if (data.fillAlpha && data.fillAlpha < 1) {
  15906. trigger.style = `filter: opacity(${Math.round(data.fillAlpha * 100)}%);`;
  15907. }
  15908. }
  15909. trigger.addEventListener("dblclick", this.#download.bind(this));
  15910. this.#trigger = trigger;
  15911. const {
  15912. isMac
  15913. } = util_FeatureTest.platform;
  15914. container.addEventListener("keydown", evt => {
  15915. if (evt.key === "Enter" && (isMac ? evt.metaKey : evt.ctrlKey)) {
  15916. this.#download();
  15917. }
  15918. });
  15919. if (!data.popupRef && this.hasPopupData) {
  15920. this._createPopup();
  15921. } else {
  15922. trigger.classList.add("popupTriggerArea");
  15923. }
  15924. container.append(trigger);
  15925. return container;
  15926. }
  15927. getElementsToTriggerPopup() {
  15928. return this.#trigger;
  15929. }
  15930. addHighlightArea() {
  15931. this.container.classList.add("highlightArea");
  15932. }
  15933. #download() {
  15934. this.downloadManager?.openOrDownloadData(this.content, this.filename);
  15935. }
  15936. }
  15937. class AnnotationLayer {
  15938. #accessibilityManager = null;
  15939. #annotationCanvasMap = null;
  15940. #editableAnnotations = new Map();
  15941. #structTreeLayer = null;
  15942. constructor({
  15943. div,
  15944. accessibilityManager,
  15945. annotationCanvasMap,
  15946. annotationEditorUIManager,
  15947. page,
  15948. viewport,
  15949. structTreeLayer
  15950. }) {
  15951. this.div = div;
  15952. this.#accessibilityManager = accessibilityManager;
  15953. this.#annotationCanvasMap = annotationCanvasMap;
  15954. this.#structTreeLayer = structTreeLayer || null;
  15955. this.page = page;
  15956. this.viewport = viewport;
  15957. this.zIndex = 0;
  15958. this._annotationEditorUIManager = annotationEditorUIManager;
  15959. }
  15960. hasEditableAnnotations() {
  15961. return this.#editableAnnotations.size > 0;
  15962. }
  15963. async #appendElement(element, id) {
  15964. const contentElement = element.firstChild || element;
  15965. const annotationId = contentElement.id = `${AnnotationPrefix}${id}`;
  15966. const ariaAttributes = await this.#structTreeLayer?.getAriaAttributes(annotationId);
  15967. if (ariaAttributes) {
  15968. for (const [key, value] of ariaAttributes) {
  15969. contentElement.setAttribute(key, value);
  15970. }
  15971. }
  15972. this.div.append(element);
  15973. this.#accessibilityManager?.moveElementInDOM(this.div, element, contentElement, false);
  15974. }
  15975. async render(params) {
  15976. const {
  15977. annotations
  15978. } = params;
  15979. const layer = this.div;
  15980. setLayerDimensions(layer, this.viewport);
  15981. const popupToElements = new Map();
  15982. const elementParams = {
  15983. data: null,
  15984. layer,
  15985. linkService: params.linkService,
  15986. downloadManager: params.downloadManager,
  15987. imageResourcesPath: params.imageResourcesPath || "",
  15988. renderForms: params.renderForms !== false,
  15989. svgFactory: new DOMSVGFactory(),
  15990. annotationStorage: params.annotationStorage || new AnnotationStorage(),
  15991. enableScripting: params.enableScripting === true,
  15992. hasJSActions: params.hasJSActions,
  15993. fieldObjects: params.fieldObjects,
  15994. parent: this,
  15995. elements: null
  15996. };
  15997. for (const data of annotations) {
  15998. if (data.noHTML) {
  15999. continue;
  16000. }
  16001. const isPopupAnnotation = data.annotationType === AnnotationType.POPUP;
  16002. if (!isPopupAnnotation) {
  16003. if (data.rect[2] === data.rect[0] || data.rect[3] === data.rect[1]) {
  16004. continue;
  16005. }
  16006. } else {
  16007. const elements = popupToElements.get(data.id);
  16008. if (!elements) {
  16009. continue;
  16010. }
  16011. elementParams.elements = elements;
  16012. }
  16013. elementParams.data = data;
  16014. const element = AnnotationElementFactory.create(elementParams);
  16015. if (!element.isRenderable) {
  16016. continue;
  16017. }
  16018. if (!isPopupAnnotation && data.popupRef) {
  16019. const elements = popupToElements.get(data.popupRef);
  16020. if (!elements) {
  16021. popupToElements.set(data.popupRef, [element]);
  16022. } else {
  16023. elements.push(element);
  16024. }
  16025. }
  16026. const rendered = element.render();
  16027. if (data.hidden) {
  16028. rendered.style.visibility = "hidden";
  16029. }
  16030. await this.#appendElement(rendered, data.id);
  16031. if (element._isEditable) {
  16032. this.#editableAnnotations.set(element.data.id, element);
  16033. this._annotationEditorUIManager?.renderAnnotationElement(element);
  16034. }
  16035. }
  16036. this.#setAnnotationCanvasMap();
  16037. }
  16038. async addLinkAnnotations(annotations, linkService) {
  16039. const elementParams = {
  16040. data: null,
  16041. layer: this.div,
  16042. linkService,
  16043. svgFactory: new DOMSVGFactory(),
  16044. parent: this
  16045. };
  16046. for (const data of annotations) {
  16047. data.borderStyle ||= AnnotationLayer._defaultBorderStyle;
  16048. elementParams.data = data;
  16049. const element = AnnotationElementFactory.create(elementParams);
  16050. if (!element.isRenderable) {
  16051. continue;
  16052. }
  16053. const rendered = element.render();
  16054. await this.#appendElement(rendered, data.id);
  16055. }
  16056. }
  16057. update({
  16058. viewport
  16059. }) {
  16060. const layer = this.div;
  16061. this.viewport = viewport;
  16062. setLayerDimensions(layer, {
  16063. rotation: viewport.rotation
  16064. });
  16065. this.#setAnnotationCanvasMap();
  16066. layer.hidden = false;
  16067. }
  16068. #setAnnotationCanvasMap() {
  16069. if (!this.#annotationCanvasMap) {
  16070. return;
  16071. }
  16072. const layer = this.div;
  16073. for (const [id, canvas] of this.#annotationCanvasMap) {
  16074. const element = layer.querySelector(`[data-annotation-id="${id}"]`);
  16075. if (!element) {
  16076. continue;
  16077. }
  16078. canvas.className = "annotationContent";
  16079. const {
  16080. firstChild
  16081. } = element;
  16082. if (!firstChild) {
  16083. element.append(canvas);
  16084. } else if (firstChild.nodeName === "CANVAS") {
  16085. firstChild.replaceWith(canvas);
  16086. } else if (!firstChild.classList.contains("annotationContent")) {
  16087. firstChild.before(canvas);
  16088. } else {
  16089. firstChild.after(canvas);
  16090. }
  16091. const editableAnnotation = this.#editableAnnotations.get(id);
  16092. if (!editableAnnotation) {
  16093. continue;
  16094. }
  16095. if (editableAnnotation._hasNoCanvas) {
  16096. this._annotationEditorUIManager?.setMissingCanvas(id, element.id, canvas);
  16097. editableAnnotation._hasNoCanvas = false;
  16098. } else {
  16099. editableAnnotation.canvas = canvas;
  16100. }
  16101. }
  16102. this.#annotationCanvasMap.clear();
  16103. }
  16104. getEditableAnnotations() {
  16105. return Array.from(this.#editableAnnotations.values());
  16106. }
  16107. getEditableAnnotation(id) {
  16108. return this.#editableAnnotations.get(id);
  16109. }
  16110. static get _defaultBorderStyle() {
  16111. return shadow(this, "_defaultBorderStyle", Object.freeze({
  16112. width: 1,
  16113. rawWidth: 1,
  16114. style: AnnotationBorderStyleType.SOLID,
  16115. dashArray: [3],
  16116. horizontalCornerRadius: 0,
  16117. verticalCornerRadius: 0
  16118. }));
  16119. }
  16120. }
  16121. ;// ./src/display/editor/freetext.js
  16122. const EOL_PATTERN = /\r\n?|\n/g;
  16123. class FreeTextEditor extends AnnotationEditor {
  16124. #color;
  16125. #content = "";
  16126. #editorDivId = `${this.id}-editor`;
  16127. #editModeAC = null;
  16128. #fontSize;
  16129. static _freeTextDefaultContent = "";
  16130. static _internalPadding = 0;
  16131. static _defaultColor = null;
  16132. static _defaultFontSize = 10;
  16133. static get _keyboardManager() {
  16134. const proto = FreeTextEditor.prototype;
  16135. const arrowChecker = self => self.isEmpty();
  16136. const small = AnnotationEditorUIManager.TRANSLATE_SMALL;
  16137. const big = AnnotationEditorUIManager.TRANSLATE_BIG;
  16138. return shadow(this, "_keyboardManager", new KeyboardManager([[["ctrl+s", "mac+meta+s", "ctrl+p", "mac+meta+p"], proto.commitOrRemove, {
  16139. bubbles: true
  16140. }], [["ctrl+Enter", "mac+meta+Enter", "Escape", "mac+Escape"], proto.commitOrRemove], [["ArrowLeft", "mac+ArrowLeft"], proto._translateEmpty, {
  16141. args: [-small, 0],
  16142. checker: arrowChecker
  16143. }], [["ctrl+ArrowLeft", "mac+shift+ArrowLeft"], proto._translateEmpty, {
  16144. args: [-big, 0],
  16145. checker: arrowChecker
  16146. }], [["ArrowRight", "mac+ArrowRight"], proto._translateEmpty, {
  16147. args: [small, 0],
  16148. checker: arrowChecker
  16149. }], [["ctrl+ArrowRight", "mac+shift+ArrowRight"], proto._translateEmpty, {
  16150. args: [big, 0],
  16151. checker: arrowChecker
  16152. }], [["ArrowUp", "mac+ArrowUp"], proto._translateEmpty, {
  16153. args: [0, -small],
  16154. checker: arrowChecker
  16155. }], [["ctrl+ArrowUp", "mac+shift+ArrowUp"], proto._translateEmpty, {
  16156. args: [0, -big],
  16157. checker: arrowChecker
  16158. }], [["ArrowDown", "mac+ArrowDown"], proto._translateEmpty, {
  16159. args: [0, small],
  16160. checker: arrowChecker
  16161. }], [["ctrl+ArrowDown", "mac+shift+ArrowDown"], proto._translateEmpty, {
  16162. args: [0, big],
  16163. checker: arrowChecker
  16164. }]]));
  16165. }
  16166. static _type = "freetext";
  16167. static _editorType = AnnotationEditorType.FREETEXT;
  16168. constructor(params) {
  16169. super({
  16170. ...params,
  16171. name: "freeTextEditor"
  16172. });
  16173. this.#color = params.color || FreeTextEditor._defaultColor || AnnotationEditor._defaultLineColor;
  16174. this.#fontSize = params.fontSize || FreeTextEditor._defaultFontSize;
  16175. }
  16176. static initialize(l10n, uiManager) {
  16177. AnnotationEditor.initialize(l10n, uiManager);
  16178. const style = getComputedStyle(document.documentElement);
  16179. this._internalPadding = parseFloat(style.getPropertyValue("--freetext-padding"));
  16180. }
  16181. static updateDefaultParams(type, value) {
  16182. switch (type) {
  16183. case AnnotationEditorParamsType.FREETEXT_SIZE:
  16184. FreeTextEditor._defaultFontSize = value;
  16185. break;
  16186. case AnnotationEditorParamsType.FREETEXT_COLOR:
  16187. FreeTextEditor._defaultColor = value;
  16188. break;
  16189. }
  16190. }
  16191. updateParams(type, value) {
  16192. switch (type) {
  16193. case AnnotationEditorParamsType.FREETEXT_SIZE:
  16194. this.#updateFontSize(value);
  16195. break;
  16196. case AnnotationEditorParamsType.FREETEXT_COLOR:
  16197. this.#updateColor(value);
  16198. break;
  16199. }
  16200. }
  16201. static get defaultPropertiesToUpdate() {
  16202. return [[AnnotationEditorParamsType.FREETEXT_SIZE, FreeTextEditor._defaultFontSize], [AnnotationEditorParamsType.FREETEXT_COLOR, FreeTextEditor._defaultColor || AnnotationEditor._defaultLineColor]];
  16203. }
  16204. get propertiesToUpdate() {
  16205. return [[AnnotationEditorParamsType.FREETEXT_SIZE, this.#fontSize], [AnnotationEditorParamsType.FREETEXT_COLOR, this.#color]];
  16206. }
  16207. #updateFontSize(fontSize) {
  16208. const setFontsize = size => {
  16209. this.editorDiv.style.fontSize = `calc(${size}px * var(--total-scale-factor))`;
  16210. this.translate(0, -(size - this.#fontSize) * this.parentScale);
  16211. this.#fontSize = size;
  16212. this.#setEditorDimensions();
  16213. };
  16214. const savedFontsize = this.#fontSize;
  16215. this.addCommands({
  16216. cmd: setFontsize.bind(this, fontSize),
  16217. undo: setFontsize.bind(this, savedFontsize),
  16218. post: this._uiManager.updateUI.bind(this._uiManager, this),
  16219. mustExec: true,
  16220. type: AnnotationEditorParamsType.FREETEXT_SIZE,
  16221. overwriteIfSameType: true,
  16222. keepUndo: true
  16223. });
  16224. }
  16225. #updateColor(color) {
  16226. const setColor = col => {
  16227. this.#color = this.editorDiv.style.color = col;
  16228. };
  16229. const savedColor = this.#color;
  16230. this.addCommands({
  16231. cmd: setColor.bind(this, color),
  16232. undo: setColor.bind(this, savedColor),
  16233. post: this._uiManager.updateUI.bind(this._uiManager, this),
  16234. mustExec: true,
  16235. type: AnnotationEditorParamsType.FREETEXT_COLOR,
  16236. overwriteIfSameType: true,
  16237. keepUndo: true
  16238. });
  16239. }
  16240. _translateEmpty(x, y) {
  16241. this._uiManager.translateSelectedEditors(x, y, true);
  16242. }
  16243. getInitialTranslation() {
  16244. const scale = this.parentScale;
  16245. return [-FreeTextEditor._internalPadding * scale, -(FreeTextEditor._internalPadding + this.#fontSize) * scale];
  16246. }
  16247. rebuild() {
  16248. if (!this.parent) {
  16249. return;
  16250. }
  16251. super.rebuild();
  16252. if (this.div === null) {
  16253. return;
  16254. }
  16255. if (!this.isAttachedToDOM) {
  16256. this.parent.add(this);
  16257. }
  16258. }
  16259. enableEditMode() {
  16260. if (this.isInEditMode()) {
  16261. return;
  16262. }
  16263. this.parent.setEditingState(false);
  16264. this.parent.updateToolbar(AnnotationEditorType.FREETEXT);
  16265. super.enableEditMode();
  16266. this.overlayDiv.classList.remove("enabled");
  16267. this.editorDiv.contentEditable = true;
  16268. this._isDraggable = false;
  16269. this.div.removeAttribute("aria-activedescendant");
  16270. this.#editModeAC = new AbortController();
  16271. const signal = this._uiManager.combinedSignal(this.#editModeAC);
  16272. this.editorDiv.addEventListener("keydown", this.editorDivKeydown.bind(this), {
  16273. signal
  16274. });
  16275. this.editorDiv.addEventListener("focus", this.editorDivFocus.bind(this), {
  16276. signal
  16277. });
  16278. this.editorDiv.addEventListener("blur", this.editorDivBlur.bind(this), {
  16279. signal
  16280. });
  16281. this.editorDiv.addEventListener("input", this.editorDivInput.bind(this), {
  16282. signal
  16283. });
  16284. this.editorDiv.addEventListener("paste", this.editorDivPaste.bind(this), {
  16285. signal
  16286. });
  16287. }
  16288. disableEditMode() {
  16289. if (!this.isInEditMode()) {
  16290. return;
  16291. }
  16292. this.parent.setEditingState(true);
  16293. super.disableEditMode();
  16294. this.overlayDiv.classList.add("enabled");
  16295. this.editorDiv.contentEditable = false;
  16296. this.div.setAttribute("aria-activedescendant", this.#editorDivId);
  16297. this._isDraggable = true;
  16298. this.#editModeAC?.abort();
  16299. this.#editModeAC = null;
  16300. this.div.focus({
  16301. preventScroll: true
  16302. });
  16303. this.isEditing = false;
  16304. this.parent.div.classList.add("freetextEditing");
  16305. }
  16306. focusin(event) {
  16307. if (!this._focusEventsAllowed) {
  16308. return;
  16309. }
  16310. super.focusin(event);
  16311. if (event.target !== this.editorDiv) {
  16312. this.editorDiv.focus();
  16313. }
  16314. }
  16315. onceAdded(focus) {
  16316. if (this.width) {
  16317. return;
  16318. }
  16319. this.enableEditMode();
  16320. if (focus) {
  16321. this.editorDiv.focus();
  16322. }
  16323. if (this._initialOptions?.isCentered) {
  16324. this.center();
  16325. }
  16326. this._initialOptions = null;
  16327. }
  16328. isEmpty() {
  16329. return !this.editorDiv || this.editorDiv.innerText.trim() === "";
  16330. }
  16331. remove() {
  16332. this.isEditing = false;
  16333. if (this.parent) {
  16334. this.parent.setEditingState(true);
  16335. this.parent.div.classList.add("freetextEditing");
  16336. }
  16337. super.remove();
  16338. }
  16339. #extractText() {
  16340. const buffer = [];
  16341. this.editorDiv.normalize();
  16342. let prevChild = null;
  16343. for (const child of this.editorDiv.childNodes) {
  16344. if (prevChild?.nodeType === Node.TEXT_NODE && child.nodeName === "BR") {
  16345. continue;
  16346. }
  16347. buffer.push(FreeTextEditor.#getNodeContent(child));
  16348. prevChild = child;
  16349. }
  16350. return buffer.join("\n");
  16351. }
  16352. #setEditorDimensions() {
  16353. const [parentWidth, parentHeight] = this.parentDimensions;
  16354. let rect;
  16355. if (this.isAttachedToDOM) {
  16356. rect = this.div.getBoundingClientRect();
  16357. } else {
  16358. const {
  16359. currentLayer,
  16360. div
  16361. } = this;
  16362. const savedDisplay = div.style.display;
  16363. const savedVisibility = div.classList.contains("hidden");
  16364. div.classList.remove("hidden");
  16365. div.style.display = "hidden";
  16366. currentLayer.div.append(this.div);
  16367. rect = div.getBoundingClientRect();
  16368. div.remove();
  16369. div.style.display = savedDisplay;
  16370. div.classList.toggle("hidden", savedVisibility);
  16371. }
  16372. if (this.rotation % 180 === this.parentRotation % 180) {
  16373. this.width = rect.width / parentWidth;
  16374. this.height = rect.height / parentHeight;
  16375. } else {
  16376. this.width = rect.height / parentWidth;
  16377. this.height = rect.width / parentHeight;
  16378. }
  16379. this.fixAndSetPosition();
  16380. }
  16381. commit() {
  16382. if (!this.isInEditMode()) {
  16383. return;
  16384. }
  16385. super.commit();
  16386. this.disableEditMode();
  16387. const savedText = this.#content;
  16388. const newText = this.#content = this.#extractText().trimEnd();
  16389. if (savedText === newText) {
  16390. return;
  16391. }
  16392. const setText = text => {
  16393. this.#content = text;
  16394. if (!text) {
  16395. this.remove();
  16396. return;
  16397. }
  16398. this.#setContent();
  16399. this._uiManager.rebuild(this);
  16400. this.#setEditorDimensions();
  16401. };
  16402. this.addCommands({
  16403. cmd: () => {
  16404. setText(newText);
  16405. },
  16406. undo: () => {
  16407. setText(savedText);
  16408. },
  16409. mustExec: false
  16410. });
  16411. this.#setEditorDimensions();
  16412. }
  16413. shouldGetKeyboardEvents() {
  16414. return this.isInEditMode();
  16415. }
  16416. enterInEditMode() {
  16417. this.enableEditMode();
  16418. this.editorDiv.focus();
  16419. }
  16420. dblclick(event) {
  16421. this.enterInEditMode();
  16422. }
  16423. keydown(event) {
  16424. if (event.target === this.div && event.key === "Enter") {
  16425. this.enterInEditMode();
  16426. event.preventDefault();
  16427. }
  16428. }
  16429. editorDivKeydown(event) {
  16430. FreeTextEditor._keyboardManager.exec(this, event);
  16431. }
  16432. editorDivFocus(event) {
  16433. this.isEditing = true;
  16434. }
  16435. editorDivBlur(event) {
  16436. this.isEditing = false;
  16437. }
  16438. editorDivInput(event) {
  16439. this.parent.div.classList.toggle("freetextEditing", this.isEmpty());
  16440. }
  16441. disableEditing() {
  16442. this.editorDiv.setAttribute("role", "comment");
  16443. this.editorDiv.removeAttribute("aria-multiline");
  16444. }
  16445. enableEditing() {
  16446. this.editorDiv.setAttribute("role", "textbox");
  16447. this.editorDiv.setAttribute("aria-multiline", true);
  16448. }
  16449. render() {
  16450. if (this.div) {
  16451. return this.div;
  16452. }
  16453. let baseX, baseY;
  16454. if (this._isCopy || this.annotationElementId) {
  16455. baseX = this.x;
  16456. baseY = this.y;
  16457. }
  16458. super.render();
  16459. this.editorDiv = document.createElement("div");
  16460. this.editorDiv.className = "internal";
  16461. this.editorDiv.setAttribute("id", this.#editorDivId);
  16462. this.editorDiv.setAttribute("data-l10n-id", "pdfjs-free-text2");
  16463. this.editorDiv.setAttribute("data-l10n-attrs", "default-content");
  16464. this.enableEditing();
  16465. this.editorDiv.contentEditable = true;
  16466. const {
  16467. style
  16468. } = this.editorDiv;
  16469. style.fontSize = `calc(${this.#fontSize}px * var(--total-scale-factor))`;
  16470. style.color = this.#color;
  16471. this.div.append(this.editorDiv);
  16472. this.overlayDiv = document.createElement("div");
  16473. this.overlayDiv.classList.add("overlay", "enabled");
  16474. this.div.append(this.overlayDiv);
  16475. bindEvents(this, this.div, ["dblclick", "keydown"]);
  16476. if (this._isCopy || this.annotationElementId) {
  16477. const [parentWidth, parentHeight] = this.parentDimensions;
  16478. if (this.annotationElementId) {
  16479. const {
  16480. position
  16481. } = this._initialData;
  16482. let [tx, ty] = this.getInitialTranslation();
  16483. [tx, ty] = this.pageTranslationToScreen(tx, ty);
  16484. const [pageWidth, pageHeight] = this.pageDimensions;
  16485. const [pageX, pageY] = this.pageTranslation;
  16486. let posX, posY;
  16487. switch (this.rotation) {
  16488. case 0:
  16489. posX = baseX + (position[0] - pageX) / pageWidth;
  16490. posY = baseY + this.height - (position[1] - pageY) / pageHeight;
  16491. break;
  16492. case 90:
  16493. posX = baseX + (position[0] - pageX) / pageWidth;
  16494. posY = baseY - (position[1] - pageY) / pageHeight;
  16495. [tx, ty] = [ty, -tx];
  16496. break;
  16497. case 180:
  16498. posX = baseX - this.width + (position[0] - pageX) / pageWidth;
  16499. posY = baseY - (position[1] - pageY) / pageHeight;
  16500. [tx, ty] = [-tx, -ty];
  16501. break;
  16502. case 270:
  16503. posX = baseX + (position[0] - pageX - this.height * pageHeight) / pageWidth;
  16504. posY = baseY + (position[1] - pageY - this.width * pageWidth) / pageHeight;
  16505. [tx, ty] = [-ty, tx];
  16506. break;
  16507. }
  16508. this.setAt(posX * parentWidth, posY * parentHeight, tx, ty);
  16509. } else {
  16510. this._moveAfterPaste(baseX, baseY);
  16511. }
  16512. this.#setContent();
  16513. this._isDraggable = true;
  16514. this.editorDiv.contentEditable = false;
  16515. } else {
  16516. this._isDraggable = false;
  16517. this.editorDiv.contentEditable = true;
  16518. }
  16519. return this.div;
  16520. }
  16521. static #getNodeContent(node) {
  16522. return (node.nodeType === Node.TEXT_NODE ? node.nodeValue : node.innerText).replaceAll(EOL_PATTERN, "");
  16523. }
  16524. editorDivPaste(event) {
  16525. const clipboardData = event.clipboardData || window.clipboardData;
  16526. const {
  16527. types
  16528. } = clipboardData;
  16529. if (types.length === 1 && types[0] === "text/plain") {
  16530. return;
  16531. }
  16532. event.preventDefault();
  16533. const paste = FreeTextEditor.#deserializeContent(clipboardData.getData("text") || "").replaceAll(EOL_PATTERN, "\n");
  16534. if (!paste) {
  16535. return;
  16536. }
  16537. const selection = window.getSelection();
  16538. if (!selection.rangeCount) {
  16539. return;
  16540. }
  16541. this.editorDiv.normalize();
  16542. selection.deleteFromDocument();
  16543. const range = selection.getRangeAt(0);
  16544. if (!paste.includes("\n")) {
  16545. range.insertNode(document.createTextNode(paste));
  16546. this.editorDiv.normalize();
  16547. selection.collapseToStart();
  16548. return;
  16549. }
  16550. const {
  16551. startContainer,
  16552. startOffset
  16553. } = range;
  16554. const bufferBefore = [];
  16555. const bufferAfter = [];
  16556. if (startContainer.nodeType === Node.TEXT_NODE) {
  16557. const parent = startContainer.parentElement;
  16558. bufferAfter.push(startContainer.nodeValue.slice(startOffset).replaceAll(EOL_PATTERN, ""));
  16559. if (parent !== this.editorDiv) {
  16560. let buffer = bufferBefore;
  16561. for (const child of this.editorDiv.childNodes) {
  16562. if (child === parent) {
  16563. buffer = bufferAfter;
  16564. continue;
  16565. }
  16566. buffer.push(FreeTextEditor.#getNodeContent(child));
  16567. }
  16568. }
  16569. bufferBefore.push(startContainer.nodeValue.slice(0, startOffset).replaceAll(EOL_PATTERN, ""));
  16570. } else if (startContainer === this.editorDiv) {
  16571. let buffer = bufferBefore;
  16572. let i = 0;
  16573. for (const child of this.editorDiv.childNodes) {
  16574. if (i++ === startOffset) {
  16575. buffer = bufferAfter;
  16576. }
  16577. buffer.push(FreeTextEditor.#getNodeContent(child));
  16578. }
  16579. }
  16580. this.#content = `${bufferBefore.join("\n")}${paste}${bufferAfter.join("\n")}`;
  16581. this.#setContent();
  16582. const newRange = new Range();
  16583. let beforeLength = Math.sumPrecise(bufferBefore.map(line => line.length));
  16584. for (const {
  16585. firstChild
  16586. } of this.editorDiv.childNodes) {
  16587. if (firstChild.nodeType === Node.TEXT_NODE) {
  16588. const length = firstChild.nodeValue.length;
  16589. if (beforeLength <= length) {
  16590. newRange.setStart(firstChild, beforeLength);
  16591. newRange.setEnd(firstChild, beforeLength);
  16592. break;
  16593. }
  16594. beforeLength -= length;
  16595. }
  16596. }
  16597. selection.removeAllRanges();
  16598. selection.addRange(newRange);
  16599. }
  16600. #setContent() {
  16601. this.editorDiv.replaceChildren();
  16602. if (!this.#content) {
  16603. return;
  16604. }
  16605. for (const line of this.#content.split("\n")) {
  16606. const div = document.createElement("div");
  16607. div.append(line ? document.createTextNode(line) : document.createElement("br"));
  16608. this.editorDiv.append(div);
  16609. }
  16610. }
  16611. #serializeContent() {
  16612. return this.#content.replaceAll("\xa0", " ");
  16613. }
  16614. static #deserializeContent(content) {
  16615. return content.replaceAll(" ", "\xa0");
  16616. }
  16617. get contentDiv() {
  16618. return this.editorDiv;
  16619. }
  16620. static async deserialize(data, parent, uiManager) {
  16621. let initialData = null;
  16622. if (data instanceof FreeTextAnnotationElement) {
  16623. const {
  16624. data: {
  16625. defaultAppearanceData: {
  16626. fontSize,
  16627. fontColor
  16628. },
  16629. rect,
  16630. rotation,
  16631. id,
  16632. popupRef
  16633. },
  16634. textContent,
  16635. textPosition,
  16636. parent: {
  16637. page: {
  16638. pageNumber
  16639. }
  16640. }
  16641. } = data;
  16642. if (!textContent || textContent.length === 0) {
  16643. return null;
  16644. }
  16645. initialData = data = {
  16646. annotationType: AnnotationEditorType.FREETEXT,
  16647. color: Array.from(fontColor),
  16648. fontSize,
  16649. value: textContent.join("\n"),
  16650. position: textPosition,
  16651. pageIndex: pageNumber - 1,
  16652. rect: rect.slice(0),
  16653. rotation,
  16654. id,
  16655. deleted: false,
  16656. popupRef
  16657. };
  16658. }
  16659. const editor = await super.deserialize(data, parent, uiManager);
  16660. editor.#fontSize = data.fontSize;
  16661. editor.#color = Util.makeHexColor(...data.color);
  16662. editor.#content = FreeTextEditor.#deserializeContent(data.value);
  16663. editor.annotationElementId = data.id || null;
  16664. editor._initialData = initialData;
  16665. return editor;
  16666. }
  16667. serialize(isForCopying = false) {
  16668. if (this.isEmpty()) {
  16669. return null;
  16670. }
  16671. if (this.deleted) {
  16672. return this.serializeDeleted();
  16673. }
  16674. const padding = FreeTextEditor._internalPadding * this.parentScale;
  16675. const rect = this.getRect(padding, padding);
  16676. const color = AnnotationEditor._colorManager.convert(this.isAttachedToDOM ? getComputedStyle(this.editorDiv).color : this.#color);
  16677. const serialized = {
  16678. annotationType: AnnotationEditorType.FREETEXT,
  16679. color,
  16680. fontSize: this.#fontSize,
  16681. value: this.#serializeContent(),
  16682. pageIndex: this.pageIndex,
  16683. rect,
  16684. rotation: this.rotation,
  16685. structTreeParentId: this._structTreeParentId
  16686. };
  16687. if (isForCopying) {
  16688. serialized.isCopy = true;
  16689. return serialized;
  16690. }
  16691. if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
  16692. return null;
  16693. }
  16694. serialized.id = this.annotationElementId;
  16695. return serialized;
  16696. }
  16697. #hasElementChanged(serialized) {
  16698. const {
  16699. value,
  16700. fontSize,
  16701. color,
  16702. pageIndex
  16703. } = this._initialData;
  16704. return this._hasBeenMoved || serialized.value !== value || serialized.fontSize !== fontSize || serialized.color.some((c, i) => c !== color[i]) || serialized.pageIndex !== pageIndex;
  16705. }
  16706. renderAnnotationElement(annotation) {
  16707. const content = super.renderAnnotationElement(annotation);
  16708. if (this.deleted) {
  16709. return content;
  16710. }
  16711. const {
  16712. style
  16713. } = content;
  16714. style.fontSize = `calc(${this.#fontSize}px * var(--total-scale-factor))`;
  16715. style.color = this.#color;
  16716. content.replaceChildren();
  16717. for (const line of this.#content.split("\n")) {
  16718. const div = document.createElement("div");
  16719. div.append(line ? document.createTextNode(line) : document.createElement("br"));
  16720. content.append(div);
  16721. }
  16722. const padding = FreeTextEditor._internalPadding * this.parentScale;
  16723. annotation.updateEdited({
  16724. rect: this.getRect(padding, padding),
  16725. popupContent: this.#content
  16726. });
  16727. return content;
  16728. }
  16729. resetAnnotationElement(annotation) {
  16730. super.resetAnnotationElement(annotation);
  16731. annotation.resetEdited();
  16732. }
  16733. }
  16734. ;// ./src/display/editor/drawers/outline.js
  16735. class Outline {
  16736. static PRECISION = 1e-4;
  16737. toSVGPath() {
  16738. unreachable("Abstract method `toSVGPath` must be implemented.");
  16739. }
  16740. get box() {
  16741. unreachable("Abstract getter `box` must be implemented.");
  16742. }
  16743. serialize(_bbox, _rotation) {
  16744. unreachable("Abstract method `serialize` must be implemented.");
  16745. }
  16746. static _rescale(src, tx, ty, sx, sy, dest) {
  16747. dest ||= new Float32Array(src.length);
  16748. for (let i = 0, ii = src.length; i < ii; i += 2) {
  16749. dest[i] = tx + src[i] * sx;
  16750. dest[i + 1] = ty + src[i + 1] * sy;
  16751. }
  16752. return dest;
  16753. }
  16754. static _rescaleAndSwap(src, tx, ty, sx, sy, dest) {
  16755. dest ||= new Float32Array(src.length);
  16756. for (let i = 0, ii = src.length; i < ii; i += 2) {
  16757. dest[i] = tx + src[i + 1] * sx;
  16758. dest[i + 1] = ty + src[i] * sy;
  16759. }
  16760. return dest;
  16761. }
  16762. static _translate(src, tx, ty, dest) {
  16763. dest ||= new Float32Array(src.length);
  16764. for (let i = 0, ii = src.length; i < ii; i += 2) {
  16765. dest[i] = tx + src[i];
  16766. dest[i + 1] = ty + src[i + 1];
  16767. }
  16768. return dest;
  16769. }
  16770. static svgRound(x) {
  16771. return Math.round(x * 10000);
  16772. }
  16773. static _normalizePoint(x, y, parentWidth, parentHeight, rotation) {
  16774. switch (rotation) {
  16775. case 90:
  16776. return [1 - y / parentWidth, x / parentHeight];
  16777. case 180:
  16778. return [1 - x / parentWidth, 1 - y / parentHeight];
  16779. case 270:
  16780. return [y / parentWidth, 1 - x / parentHeight];
  16781. default:
  16782. return [x / parentWidth, y / parentHeight];
  16783. }
  16784. }
  16785. static _normalizePagePoint(x, y, rotation) {
  16786. switch (rotation) {
  16787. case 90:
  16788. return [1 - y, x];
  16789. case 180:
  16790. return [1 - x, 1 - y];
  16791. case 270:
  16792. return [y, 1 - x];
  16793. default:
  16794. return [x, y];
  16795. }
  16796. }
  16797. static createBezierPoints(x1, y1, x2, y2, x3, y3) {
  16798. return [(x1 + 5 * x2) / 6, (y1 + 5 * y2) / 6, (5 * x2 + x3) / 6, (5 * y2 + y3) / 6, (x2 + x3) / 2, (y2 + y3) / 2];
  16799. }
  16800. }
  16801. ;// ./src/display/editor/drawers/freedraw.js
  16802. class FreeDrawOutliner {
  16803. #box;
  16804. #bottom = [];
  16805. #innerMargin;
  16806. #isLTR;
  16807. #top = [];
  16808. #last = new Float32Array(18);
  16809. #lastX;
  16810. #lastY;
  16811. #min;
  16812. #min_dist;
  16813. #scaleFactor;
  16814. #thickness;
  16815. #points = [];
  16816. static #MIN_DIST = 8;
  16817. static #MIN_DIFF = 2;
  16818. static #MIN = FreeDrawOutliner.#MIN_DIST + FreeDrawOutliner.#MIN_DIFF;
  16819. constructor({
  16820. x,
  16821. y
  16822. }, box, scaleFactor, thickness, isLTR, innerMargin = 0) {
  16823. this.#box = box;
  16824. this.#thickness = thickness * scaleFactor;
  16825. this.#isLTR = isLTR;
  16826. this.#last.set([NaN, NaN, NaN, NaN, x, y], 6);
  16827. this.#innerMargin = innerMargin;
  16828. this.#min_dist = FreeDrawOutliner.#MIN_DIST * scaleFactor;
  16829. this.#min = FreeDrawOutliner.#MIN * scaleFactor;
  16830. this.#scaleFactor = scaleFactor;
  16831. this.#points.push(x, y);
  16832. }
  16833. isEmpty() {
  16834. return isNaN(this.#last[8]);
  16835. }
  16836. #getLastCoords() {
  16837. const lastTop = this.#last.subarray(4, 6);
  16838. const lastBottom = this.#last.subarray(16, 18);
  16839. const [x, y, width, height] = this.#box;
  16840. return [(this.#lastX + (lastTop[0] - lastBottom[0]) / 2 - x) / width, (this.#lastY + (lastTop[1] - lastBottom[1]) / 2 - y) / height, (this.#lastX + (lastBottom[0] - lastTop[0]) / 2 - x) / width, (this.#lastY + (lastBottom[1] - lastTop[1]) / 2 - y) / height];
  16841. }
  16842. add({
  16843. x,
  16844. y
  16845. }) {
  16846. this.#lastX = x;
  16847. this.#lastY = y;
  16848. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16849. let [x1, y1, x2, y2] = this.#last.subarray(8, 12);
  16850. const diffX = x - x2;
  16851. const diffY = y - y2;
  16852. const d = Math.hypot(diffX, diffY);
  16853. if (d < this.#min) {
  16854. return false;
  16855. }
  16856. const diffD = d - this.#min_dist;
  16857. const K = diffD / d;
  16858. const shiftX = K * diffX;
  16859. const shiftY = K * diffY;
  16860. let x0 = x1;
  16861. let y0 = y1;
  16862. x1 = x2;
  16863. y1 = y2;
  16864. x2 += shiftX;
  16865. y2 += shiftY;
  16866. this.#points?.push(x, y);
  16867. const nX = -shiftY / diffD;
  16868. const nY = shiftX / diffD;
  16869. const thX = nX * this.#thickness;
  16870. const thY = nY * this.#thickness;
  16871. this.#last.set(this.#last.subarray(2, 8), 0);
  16872. this.#last.set([x2 + thX, y2 + thY], 4);
  16873. this.#last.set(this.#last.subarray(14, 18), 12);
  16874. this.#last.set([x2 - thX, y2 - thY], 16);
  16875. if (isNaN(this.#last[6])) {
  16876. if (this.#top.length === 0) {
  16877. this.#last.set([x1 + thX, y1 + thY], 2);
  16878. this.#top.push(NaN, NaN, NaN, NaN, (x1 + thX - layerX) / layerWidth, (y1 + thY - layerY) / layerHeight);
  16879. this.#last.set([x1 - thX, y1 - thY], 14);
  16880. this.#bottom.push(NaN, NaN, NaN, NaN, (x1 - thX - layerX) / layerWidth, (y1 - thY - layerY) / layerHeight);
  16881. }
  16882. this.#last.set([x0, y0, x1, y1, x2, y2], 6);
  16883. return !this.isEmpty();
  16884. }
  16885. this.#last.set([x0, y0, x1, y1, x2, y2], 6);
  16886. const angle = Math.abs(Math.atan2(y0 - y1, x0 - x1) - Math.atan2(shiftY, shiftX));
  16887. if (angle < Math.PI / 2) {
  16888. [x1, y1, x2, y2] = this.#last.subarray(2, 6);
  16889. this.#top.push(NaN, NaN, NaN, NaN, ((x1 + x2) / 2 - layerX) / layerWidth, ((y1 + y2) / 2 - layerY) / layerHeight);
  16890. [x1, y1, x0, y0] = this.#last.subarray(14, 18);
  16891. this.#bottom.push(NaN, NaN, NaN, NaN, ((x0 + x1) / 2 - layerX) / layerWidth, ((y0 + y1) / 2 - layerY) / layerHeight);
  16892. return true;
  16893. }
  16894. [x0, y0, x1, y1, x2, y2] = this.#last.subarray(0, 6);
  16895. this.#top.push(((x0 + 5 * x1) / 6 - layerX) / layerWidth, ((y0 + 5 * y1) / 6 - layerY) / layerHeight, ((5 * x1 + x2) / 6 - layerX) / layerWidth, ((5 * y1 + y2) / 6 - layerY) / layerHeight, ((x1 + x2) / 2 - layerX) / layerWidth, ((y1 + y2) / 2 - layerY) / layerHeight);
  16896. [x2, y2, x1, y1, x0, y0] = this.#last.subarray(12, 18);
  16897. this.#bottom.push(((x0 + 5 * x1) / 6 - layerX) / layerWidth, ((y0 + 5 * y1) / 6 - layerY) / layerHeight, ((5 * x1 + x2) / 6 - layerX) / layerWidth, ((5 * y1 + y2) / 6 - layerY) / layerHeight, ((x1 + x2) / 2 - layerX) / layerWidth, ((y1 + y2) / 2 - layerY) / layerHeight);
  16898. return true;
  16899. }
  16900. toSVGPath() {
  16901. if (this.isEmpty()) {
  16902. return "";
  16903. }
  16904. const top = this.#top;
  16905. const bottom = this.#bottom;
  16906. if (isNaN(this.#last[6]) && !this.isEmpty()) {
  16907. return this.#toSVGPathTwoPoints();
  16908. }
  16909. const buffer = [];
  16910. buffer.push(`M${top[4]} ${top[5]}`);
  16911. for (let i = 6; i < top.length; i += 6) {
  16912. if (isNaN(top[i])) {
  16913. buffer.push(`L${top[i + 4]} ${top[i + 5]}`);
  16914. } else {
  16915. buffer.push(`C${top[i]} ${top[i + 1]} ${top[i + 2]} ${top[i + 3]} ${top[i + 4]} ${top[i + 5]}`);
  16916. }
  16917. }
  16918. this.#toSVGPathEnd(buffer);
  16919. for (let i = bottom.length - 6; i >= 6; i -= 6) {
  16920. if (isNaN(bottom[i])) {
  16921. buffer.push(`L${bottom[i + 4]} ${bottom[i + 5]}`);
  16922. } else {
  16923. buffer.push(`C${bottom[i]} ${bottom[i + 1]} ${bottom[i + 2]} ${bottom[i + 3]} ${bottom[i + 4]} ${bottom[i + 5]}`);
  16924. }
  16925. }
  16926. this.#toSVGPathStart(buffer);
  16927. return buffer.join(" ");
  16928. }
  16929. #toSVGPathTwoPoints() {
  16930. const [x, y, width, height] = this.#box;
  16931. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16932. return `M${(this.#last[2] - x) / width} ${(this.#last[3] - y) / height} L${(this.#last[4] - x) / width} ${(this.#last[5] - y) / height} L${lastTopX} ${lastTopY} L${lastBottomX} ${lastBottomY} L${(this.#last[16] - x) / width} ${(this.#last[17] - y) / height} L${(this.#last[14] - x) / width} ${(this.#last[15] - y) / height} Z`;
  16933. }
  16934. #toSVGPathStart(buffer) {
  16935. const bottom = this.#bottom;
  16936. buffer.push(`L${bottom[4]} ${bottom[5]} Z`);
  16937. }
  16938. #toSVGPathEnd(buffer) {
  16939. const [x, y, width, height] = this.#box;
  16940. const lastTop = this.#last.subarray(4, 6);
  16941. const lastBottom = this.#last.subarray(16, 18);
  16942. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16943. buffer.push(`L${(lastTop[0] - x) / width} ${(lastTop[1] - y) / height} L${lastTopX} ${lastTopY} L${lastBottomX} ${lastBottomY} L${(lastBottom[0] - x) / width} ${(lastBottom[1] - y) / height}`);
  16944. }
  16945. newFreeDrawOutline(outline, points, box, scaleFactor, innerMargin, isLTR) {
  16946. return new FreeDrawOutline(outline, points, box, scaleFactor, innerMargin, isLTR);
  16947. }
  16948. getOutlines() {
  16949. const top = this.#top;
  16950. const bottom = this.#bottom;
  16951. const last = this.#last;
  16952. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16953. const points = new Float32Array((this.#points?.length ?? 0) + 2);
  16954. for (let i = 0, ii = points.length - 2; i < ii; i += 2) {
  16955. points[i] = (this.#points[i] - layerX) / layerWidth;
  16956. points[i + 1] = (this.#points[i + 1] - layerY) / layerHeight;
  16957. }
  16958. points[points.length - 2] = (this.#lastX - layerX) / layerWidth;
  16959. points[points.length - 1] = (this.#lastY - layerY) / layerHeight;
  16960. if (isNaN(last[6]) && !this.isEmpty()) {
  16961. return this.#getOutlineTwoPoints(points);
  16962. }
  16963. const outline = new Float32Array(this.#top.length + 24 + this.#bottom.length);
  16964. let N = top.length;
  16965. for (let i = 0; i < N; i += 2) {
  16966. if (isNaN(top[i])) {
  16967. outline[i] = outline[i + 1] = NaN;
  16968. continue;
  16969. }
  16970. outline[i] = top[i];
  16971. outline[i + 1] = top[i + 1];
  16972. }
  16973. N = this.#getOutlineEnd(outline, N);
  16974. for (let i = bottom.length - 6; i >= 6; i -= 6) {
  16975. for (let j = 0; j < 6; j += 2) {
  16976. if (isNaN(bottom[i + j])) {
  16977. outline[N] = outline[N + 1] = NaN;
  16978. N += 2;
  16979. continue;
  16980. }
  16981. outline[N] = bottom[i + j];
  16982. outline[N + 1] = bottom[i + j + 1];
  16983. N += 2;
  16984. }
  16985. }
  16986. this.#getOutlineStart(outline, N);
  16987. return this.newFreeDrawOutline(outline, points, this.#box, this.#scaleFactor, this.#innerMargin, this.#isLTR);
  16988. }
  16989. #getOutlineTwoPoints(points) {
  16990. const last = this.#last;
  16991. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16992. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16993. const outline = new Float32Array(36);
  16994. outline.set([NaN, NaN, NaN, NaN, (last[2] - layerX) / layerWidth, (last[3] - layerY) / layerHeight, NaN, NaN, NaN, NaN, (last[4] - layerX) / layerWidth, (last[5] - layerY) / layerHeight, NaN, NaN, NaN, NaN, lastTopX, lastTopY, NaN, NaN, NaN, NaN, lastBottomX, lastBottomY, NaN, NaN, NaN, NaN, (last[16] - layerX) / layerWidth, (last[17] - layerY) / layerHeight, NaN, NaN, NaN, NaN, (last[14] - layerX) / layerWidth, (last[15] - layerY) / layerHeight], 0);
  16995. return this.newFreeDrawOutline(outline, points, this.#box, this.#scaleFactor, this.#innerMargin, this.#isLTR);
  16996. }
  16997. #getOutlineStart(outline, pos) {
  16998. const bottom = this.#bottom;
  16999. outline.set([NaN, NaN, NaN, NaN, bottom[4], bottom[5]], pos);
  17000. return pos += 6;
  17001. }
  17002. #getOutlineEnd(outline, pos) {
  17003. const lastTop = this.#last.subarray(4, 6);
  17004. const lastBottom = this.#last.subarray(16, 18);
  17005. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  17006. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  17007. outline.set([NaN, NaN, NaN, NaN, (lastTop[0] - layerX) / layerWidth, (lastTop[1] - layerY) / layerHeight, NaN, NaN, NaN, NaN, lastTopX, lastTopY, NaN, NaN, NaN, NaN, lastBottomX, lastBottomY, NaN, NaN, NaN, NaN, (lastBottom[0] - layerX) / layerWidth, (lastBottom[1] - layerY) / layerHeight], pos);
  17008. return pos += 24;
  17009. }
  17010. }
  17011. class FreeDrawOutline extends Outline {
  17012. #box;
  17013. #bbox = new Float32Array(4);
  17014. #innerMargin;
  17015. #isLTR;
  17016. #points;
  17017. #scaleFactor;
  17018. #outline;
  17019. constructor(outline, points, box, scaleFactor, innerMargin, isLTR) {
  17020. super();
  17021. this.#outline = outline;
  17022. this.#points = points;
  17023. this.#box = box;
  17024. this.#scaleFactor = scaleFactor;
  17025. this.#innerMargin = innerMargin;
  17026. this.#isLTR = isLTR;
  17027. this.lastPoint = [NaN, NaN];
  17028. this.#computeMinMax(isLTR);
  17029. const [x, y, width, height] = this.#bbox;
  17030. for (let i = 0, ii = outline.length; i < ii; i += 2) {
  17031. outline[i] = (outline[i] - x) / width;
  17032. outline[i + 1] = (outline[i + 1] - y) / height;
  17033. }
  17034. for (let i = 0, ii = points.length; i < ii; i += 2) {
  17035. points[i] = (points[i] - x) / width;
  17036. points[i + 1] = (points[i + 1] - y) / height;
  17037. }
  17038. }
  17039. toSVGPath() {
  17040. const buffer = [`M${this.#outline[4]} ${this.#outline[5]}`];
  17041. for (let i = 6, ii = this.#outline.length; i < ii; i += 6) {
  17042. if (isNaN(this.#outline[i])) {
  17043. buffer.push(`L${this.#outline[i + 4]} ${this.#outline[i + 5]}`);
  17044. continue;
  17045. }
  17046. buffer.push(`C${this.#outline[i]} ${this.#outline[i + 1]} ${this.#outline[i + 2]} ${this.#outline[i + 3]} ${this.#outline[i + 4]} ${this.#outline[i + 5]}`);
  17047. }
  17048. buffer.push("Z");
  17049. return buffer.join(" ");
  17050. }
  17051. serialize([blX, blY, trX, trY], rotation) {
  17052. const width = trX - blX;
  17053. const height = trY - blY;
  17054. let outline;
  17055. let points;
  17056. switch (rotation) {
  17057. case 0:
  17058. outline = Outline._rescale(this.#outline, blX, trY, width, -height);
  17059. points = Outline._rescale(this.#points, blX, trY, width, -height);
  17060. break;
  17061. case 90:
  17062. outline = Outline._rescaleAndSwap(this.#outline, blX, blY, width, height);
  17063. points = Outline._rescaleAndSwap(this.#points, blX, blY, width, height);
  17064. break;
  17065. case 180:
  17066. outline = Outline._rescale(this.#outline, trX, blY, -width, height);
  17067. points = Outline._rescale(this.#points, trX, blY, -width, height);
  17068. break;
  17069. case 270:
  17070. outline = Outline._rescaleAndSwap(this.#outline, trX, trY, -width, -height);
  17071. points = Outline._rescaleAndSwap(this.#points, trX, trY, -width, -height);
  17072. break;
  17073. }
  17074. return {
  17075. outline: Array.from(outline),
  17076. points: [Array.from(points)]
  17077. };
  17078. }
  17079. #computeMinMax(isLTR) {
  17080. const outline = this.#outline;
  17081. let lastX = outline[4];
  17082. let lastY = outline[5];
  17083. const minMax = [lastX, lastY, lastX, lastY];
  17084. let lastPointX = lastX;
  17085. let lastPointY = lastY;
  17086. const ltrCallback = isLTR ? Math.max : Math.min;
  17087. for (let i = 6, ii = outline.length; i < ii; i += 6) {
  17088. const x = outline[i + 4],
  17089. y = outline[i + 5];
  17090. if (isNaN(outline[i])) {
  17091. Util.pointBoundingBox(x, y, minMax);
  17092. if (lastPointY < y) {
  17093. lastPointX = x;
  17094. lastPointY = y;
  17095. } else if (lastPointY === y) {
  17096. lastPointX = ltrCallback(lastPointX, x);
  17097. }
  17098. } else {
  17099. const bbox = [Infinity, Infinity, -Infinity, -Infinity];
  17100. Util.bezierBoundingBox(lastX, lastY, ...outline.slice(i, i + 6), bbox);
  17101. Util.rectBoundingBox(...bbox, minMax);
  17102. if (lastPointY < bbox[3]) {
  17103. lastPointX = bbox[2];
  17104. lastPointY = bbox[3];
  17105. } else if (lastPointY === bbox[3]) {
  17106. lastPointX = ltrCallback(lastPointX, bbox[2]);
  17107. }
  17108. }
  17109. lastX = x;
  17110. lastY = y;
  17111. }
  17112. const bbox = this.#bbox;
  17113. bbox[0] = minMax[0] - this.#innerMargin;
  17114. bbox[1] = minMax[1] - this.#innerMargin;
  17115. bbox[2] = minMax[2] - minMax[0] + 2 * this.#innerMargin;
  17116. bbox[3] = minMax[3] - minMax[1] + 2 * this.#innerMargin;
  17117. this.lastPoint = [lastPointX, lastPointY];
  17118. }
  17119. get box() {
  17120. return this.#bbox;
  17121. }
  17122. newOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin = 0) {
  17123. return new FreeDrawOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin);
  17124. }
  17125. getNewOutline(thickness, innerMargin) {
  17126. const [x, y, width, height] = this.#bbox;
  17127. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  17128. const sx = width * layerWidth;
  17129. const sy = height * layerHeight;
  17130. const tx = x * layerWidth + layerX;
  17131. const ty = y * layerHeight + layerY;
  17132. const outliner = this.newOutliner({
  17133. x: this.#points[0] * sx + tx,
  17134. y: this.#points[1] * sy + ty
  17135. }, this.#box, this.#scaleFactor, thickness, this.#isLTR, innerMargin ?? this.#innerMargin);
  17136. for (let i = 2; i < this.#points.length; i += 2) {
  17137. outliner.add({
  17138. x: this.#points[i] * sx + tx,
  17139. y: this.#points[i + 1] * sy + ty
  17140. });
  17141. }
  17142. return outliner.getOutlines();
  17143. }
  17144. }
  17145. ;// ./src/display/editor/drawers/highlight.js
  17146. class HighlightOutliner {
  17147. #box;
  17148. #lastPoint;
  17149. #verticalEdges = [];
  17150. #intervals = [];
  17151. constructor(boxes, borderWidth = 0, innerMargin = 0, isLTR = true) {
  17152. const minMax = [Infinity, Infinity, -Infinity, -Infinity];
  17153. const NUMBER_OF_DIGITS = 4;
  17154. const EPSILON = 10 ** -NUMBER_OF_DIGITS;
  17155. for (const {
  17156. x,
  17157. y,
  17158. width,
  17159. height
  17160. } of boxes) {
  17161. const x1 = Math.floor((x - borderWidth) / EPSILON) * EPSILON;
  17162. const x2 = Math.ceil((x + width + borderWidth) / EPSILON) * EPSILON;
  17163. const y1 = Math.floor((y - borderWidth) / EPSILON) * EPSILON;
  17164. const y2 = Math.ceil((y + height + borderWidth) / EPSILON) * EPSILON;
  17165. const left = [x1, y1, y2, true];
  17166. const right = [x2, y1, y2, false];
  17167. this.#verticalEdges.push(left, right);
  17168. Util.rectBoundingBox(x1, y1, x2, y2, minMax);
  17169. }
  17170. const bboxWidth = minMax[2] - minMax[0] + 2 * innerMargin;
  17171. const bboxHeight = minMax[3] - minMax[1] + 2 * innerMargin;
  17172. const shiftedMinX = minMax[0] - innerMargin;
  17173. const shiftedMinY = minMax[1] - innerMargin;
  17174. const lastEdge = this.#verticalEdges.at(isLTR ? -1 : -2);
  17175. const lastPoint = [lastEdge[0], lastEdge[2]];
  17176. for (const edge of this.#verticalEdges) {
  17177. const [x, y1, y2] = edge;
  17178. edge[0] = (x - shiftedMinX) / bboxWidth;
  17179. edge[1] = (y1 - shiftedMinY) / bboxHeight;
  17180. edge[2] = (y2 - shiftedMinY) / bboxHeight;
  17181. }
  17182. this.#box = new Float32Array([shiftedMinX, shiftedMinY, bboxWidth, bboxHeight]);
  17183. this.#lastPoint = lastPoint;
  17184. }
  17185. getOutlines() {
  17186. this.#verticalEdges.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
  17187. const outlineVerticalEdges = [];
  17188. for (const edge of this.#verticalEdges) {
  17189. if (edge[3]) {
  17190. outlineVerticalEdges.push(...this.#breakEdge(edge));
  17191. this.#insert(edge);
  17192. } else {
  17193. this.#remove(edge);
  17194. outlineVerticalEdges.push(...this.#breakEdge(edge));
  17195. }
  17196. }
  17197. return this.#getOutlines(outlineVerticalEdges);
  17198. }
  17199. #getOutlines(outlineVerticalEdges) {
  17200. const edges = [];
  17201. const allEdges = new Set();
  17202. for (const edge of outlineVerticalEdges) {
  17203. const [x, y1, y2] = edge;
  17204. edges.push([x, y1, edge], [x, y2, edge]);
  17205. }
  17206. edges.sort((a, b) => a[1] - b[1] || a[0] - b[0]);
  17207. for (let i = 0, ii = edges.length; i < ii; i += 2) {
  17208. const edge1 = edges[i][2];
  17209. const edge2 = edges[i + 1][2];
  17210. edge1.push(edge2);
  17211. edge2.push(edge1);
  17212. allEdges.add(edge1);
  17213. allEdges.add(edge2);
  17214. }
  17215. const outlines = [];
  17216. let outline;
  17217. while (allEdges.size > 0) {
  17218. const edge = allEdges.values().next().value;
  17219. let [x, y1, y2, edge1, edge2] = edge;
  17220. allEdges.delete(edge);
  17221. let lastPointX = x;
  17222. let lastPointY = y1;
  17223. outline = [x, y2];
  17224. outlines.push(outline);
  17225. while (true) {
  17226. let e;
  17227. if (allEdges.has(edge1)) {
  17228. e = edge1;
  17229. } else if (allEdges.has(edge2)) {
  17230. e = edge2;
  17231. } else {
  17232. break;
  17233. }
  17234. allEdges.delete(e);
  17235. [x, y1, y2, edge1, edge2] = e;
  17236. if (lastPointX !== x) {
  17237. outline.push(lastPointX, lastPointY, x, lastPointY === y1 ? y1 : y2);
  17238. lastPointX = x;
  17239. }
  17240. lastPointY = lastPointY === y1 ? y2 : y1;
  17241. }
  17242. outline.push(lastPointX, lastPointY);
  17243. }
  17244. return new HighlightOutline(outlines, this.#box, this.#lastPoint);
  17245. }
  17246. #binarySearch(y) {
  17247. const array = this.#intervals;
  17248. let start = 0;
  17249. let end = array.length - 1;
  17250. while (start <= end) {
  17251. const middle = start + end >> 1;
  17252. const y1 = array[middle][0];
  17253. if (y1 === y) {
  17254. return middle;
  17255. }
  17256. if (y1 < y) {
  17257. start = middle + 1;
  17258. } else {
  17259. end = middle - 1;
  17260. }
  17261. }
  17262. return end + 1;
  17263. }
  17264. #insert([, y1, y2]) {
  17265. const index = this.#binarySearch(y1);
  17266. this.#intervals.splice(index, 0, [y1, y2]);
  17267. }
  17268. #remove([, y1, y2]) {
  17269. const index = this.#binarySearch(y1);
  17270. for (let i = index; i < this.#intervals.length; i++) {
  17271. const [start, end] = this.#intervals[i];
  17272. if (start !== y1) {
  17273. break;
  17274. }
  17275. if (start === y1 && end === y2) {
  17276. this.#intervals.splice(i, 1);
  17277. return;
  17278. }
  17279. }
  17280. for (let i = index - 1; i >= 0; i--) {
  17281. const [start, end] = this.#intervals[i];
  17282. if (start !== y1) {
  17283. break;
  17284. }
  17285. if (start === y1 && end === y2) {
  17286. this.#intervals.splice(i, 1);
  17287. return;
  17288. }
  17289. }
  17290. }
  17291. #breakEdge(edge) {
  17292. const [x, y1, y2] = edge;
  17293. const results = [[x, y1, y2]];
  17294. const index = this.#binarySearch(y2);
  17295. for (let i = 0; i < index; i++) {
  17296. const [start, end] = this.#intervals[i];
  17297. for (let j = 0, jj = results.length; j < jj; j++) {
  17298. const [, y3, y4] = results[j];
  17299. if (end <= y3 || y4 <= start) {
  17300. continue;
  17301. }
  17302. if (y3 >= start) {
  17303. if (y4 > end) {
  17304. results[j][1] = end;
  17305. } else {
  17306. if (jj === 1) {
  17307. return [];
  17308. }
  17309. results.splice(j, 1);
  17310. j--;
  17311. jj--;
  17312. }
  17313. continue;
  17314. }
  17315. results[j][2] = start;
  17316. if (y4 > end) {
  17317. results.push([x, end, y4]);
  17318. }
  17319. }
  17320. }
  17321. return results;
  17322. }
  17323. }
  17324. class HighlightOutline extends Outline {
  17325. #box;
  17326. #outlines;
  17327. constructor(outlines, box, lastPoint) {
  17328. super();
  17329. this.#outlines = outlines;
  17330. this.#box = box;
  17331. this.lastPoint = lastPoint;
  17332. }
  17333. toSVGPath() {
  17334. const buffer = [];
  17335. for (const polygon of this.#outlines) {
  17336. let [prevX, prevY] = polygon;
  17337. buffer.push(`M${prevX} ${prevY}`);
  17338. for (let i = 2; i < polygon.length; i += 2) {
  17339. const x = polygon[i];
  17340. const y = polygon[i + 1];
  17341. if (x === prevX) {
  17342. buffer.push(`V${y}`);
  17343. prevY = y;
  17344. } else if (y === prevY) {
  17345. buffer.push(`H${x}`);
  17346. prevX = x;
  17347. }
  17348. }
  17349. buffer.push("Z");
  17350. }
  17351. return buffer.join(" ");
  17352. }
  17353. serialize([blX, blY, trX, trY], _rotation) {
  17354. const outlines = [];
  17355. const width = trX - blX;
  17356. const height = trY - blY;
  17357. for (const outline of this.#outlines) {
  17358. const points = new Array(outline.length);
  17359. for (let i = 0; i < outline.length; i += 2) {
  17360. points[i] = blX + outline[i] * width;
  17361. points[i + 1] = trY - outline[i + 1] * height;
  17362. }
  17363. outlines.push(points);
  17364. }
  17365. return outlines;
  17366. }
  17367. get box() {
  17368. return this.#box;
  17369. }
  17370. get classNamesForOutlining() {
  17371. return ["highlightOutline"];
  17372. }
  17373. }
  17374. class FreeHighlightOutliner extends FreeDrawOutliner {
  17375. newFreeDrawOutline(outline, points, box, scaleFactor, innerMargin, isLTR) {
  17376. return new FreeHighlightOutline(outline, points, box, scaleFactor, innerMargin, isLTR);
  17377. }
  17378. }
  17379. class FreeHighlightOutline extends FreeDrawOutline {
  17380. newOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin = 0) {
  17381. return new FreeHighlightOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin);
  17382. }
  17383. }
  17384. ;// ./src/display/editor/color_picker.js
  17385. class ColorPicker {
  17386. #button = null;
  17387. #buttonSwatch = null;
  17388. #defaultColor;
  17389. #dropdown = null;
  17390. #dropdownWasFromKeyboard = false;
  17391. #isMainColorPicker = false;
  17392. #editor = null;
  17393. #eventBus;
  17394. #openDropdownAC = null;
  17395. #uiManager = null;
  17396. #type;
  17397. static #l10nColor = null;
  17398. static get _keyboardManager() {
  17399. return shadow(this, "_keyboardManager", new KeyboardManager([[["Escape", "mac+Escape"], ColorPicker.prototype._hideDropdownFromKeyboard], [[" ", "mac+ "], ColorPicker.prototype._colorSelectFromKeyboard], [["ArrowDown", "ArrowRight", "mac+ArrowDown", "mac+ArrowRight"], ColorPicker.prototype._moveToNext], [["ArrowUp", "ArrowLeft", "mac+ArrowUp", "mac+ArrowLeft"], ColorPicker.prototype._moveToPrevious], [["Home", "mac+Home"], ColorPicker.prototype._moveToBeginning], [["End", "mac+End"], ColorPicker.prototype._moveToEnd]]));
  17400. }
  17401. constructor({
  17402. editor = null,
  17403. uiManager = null
  17404. }) {
  17405. if (editor) {
  17406. this.#isMainColorPicker = false;
  17407. this.#type = AnnotationEditorParamsType.HIGHLIGHT_COLOR;
  17408. this.#editor = editor;
  17409. } else {
  17410. this.#isMainColorPicker = true;
  17411. this.#type = AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR;
  17412. }
  17413. this.#uiManager = editor?._uiManager || uiManager;
  17414. this.#eventBus = this.#uiManager._eventBus;
  17415. this.#defaultColor = editor?.color || this.#uiManager?.highlightColors.values().next().value || "#FFFF98";
  17416. ColorPicker.#l10nColor ||= Object.freeze({
  17417. blue: "pdfjs-editor-colorpicker-blue",
  17418. green: "pdfjs-editor-colorpicker-green",
  17419. pink: "pdfjs-editor-colorpicker-pink",
  17420. red: "pdfjs-editor-colorpicker-red",
  17421. yellow: "pdfjs-editor-colorpicker-yellow"
  17422. });
  17423. }
  17424. renderButton() {
  17425. const button = this.#button = document.createElement("button");
  17426. button.className = "colorPicker";
  17427. button.tabIndex = "0";
  17428. button.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-button");
  17429. button.setAttribute("aria-haspopup", true);
  17430. const signal = this.#uiManager._signal;
  17431. button.addEventListener("click", this.#openDropdown.bind(this), {
  17432. signal
  17433. });
  17434. button.addEventListener("keydown", this.#keyDown.bind(this), {
  17435. signal
  17436. });
  17437. const swatch = this.#buttonSwatch = document.createElement("span");
  17438. swatch.className = "swatch";
  17439. swatch.setAttribute("aria-hidden", true);
  17440. swatch.style.backgroundColor = this.#defaultColor;
  17441. button.append(swatch);
  17442. return button;
  17443. }
  17444. renderMainDropdown() {
  17445. const dropdown = this.#dropdown = this.#getDropdownRoot();
  17446. dropdown.setAttribute("aria-orientation", "horizontal");
  17447. dropdown.setAttribute("aria-labelledby", "highlightColorPickerLabel");
  17448. return dropdown;
  17449. }
  17450. #getDropdownRoot() {
  17451. const div = document.createElement("div");
  17452. const signal = this.#uiManager._signal;
  17453. div.addEventListener("contextmenu", noContextMenu, {
  17454. signal
  17455. });
  17456. div.className = "dropdown";
  17457. div.role = "listbox";
  17458. div.setAttribute("aria-multiselectable", false);
  17459. div.setAttribute("aria-orientation", "vertical");
  17460. div.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-dropdown");
  17461. for (const [name, color] of this.#uiManager.highlightColors) {
  17462. const button = document.createElement("button");
  17463. button.tabIndex = "0";
  17464. button.role = "option";
  17465. button.setAttribute("data-color", color);
  17466. button.title = name;
  17467. button.setAttribute("data-l10n-id", ColorPicker.#l10nColor[name]);
  17468. const swatch = document.createElement("span");
  17469. button.append(swatch);
  17470. swatch.className = "swatch";
  17471. swatch.style.backgroundColor = color;
  17472. button.setAttribute("aria-selected", color === this.#defaultColor);
  17473. button.addEventListener("click", this.#colorSelect.bind(this, color), {
  17474. signal
  17475. });
  17476. div.append(button);
  17477. }
  17478. div.addEventListener("keydown", this.#keyDown.bind(this), {
  17479. signal
  17480. });
  17481. return div;
  17482. }
  17483. #colorSelect(color, event) {
  17484. event.stopPropagation();
  17485. this.#eventBus.dispatch("switchannotationeditorparams", {
  17486. source: this,
  17487. type: this.#type,
  17488. value: color
  17489. });
  17490. }
  17491. _colorSelectFromKeyboard(event) {
  17492. if (event.target === this.#button) {
  17493. this.#openDropdown(event);
  17494. return;
  17495. }
  17496. const color = event.target.getAttribute("data-color");
  17497. if (!color) {
  17498. return;
  17499. }
  17500. this.#colorSelect(color, event);
  17501. }
  17502. _moveToNext(event) {
  17503. if (!this.#isDropdownVisible) {
  17504. this.#openDropdown(event);
  17505. return;
  17506. }
  17507. if (event.target === this.#button) {
  17508. this.#dropdown.firstChild?.focus();
  17509. return;
  17510. }
  17511. event.target.nextSibling?.focus();
  17512. }
  17513. _moveToPrevious(event) {
  17514. if (event.target === this.#dropdown?.firstChild || event.target === this.#button) {
  17515. if (this.#isDropdownVisible) {
  17516. this._hideDropdownFromKeyboard();
  17517. }
  17518. return;
  17519. }
  17520. if (!this.#isDropdownVisible) {
  17521. this.#openDropdown(event);
  17522. }
  17523. event.target.previousSibling?.focus();
  17524. }
  17525. _moveToBeginning(event) {
  17526. if (!this.#isDropdownVisible) {
  17527. this.#openDropdown(event);
  17528. return;
  17529. }
  17530. this.#dropdown.firstChild?.focus();
  17531. }
  17532. _moveToEnd(event) {
  17533. if (!this.#isDropdownVisible) {
  17534. this.#openDropdown(event);
  17535. return;
  17536. }
  17537. this.#dropdown.lastChild?.focus();
  17538. }
  17539. #keyDown(event) {
  17540. ColorPicker._keyboardManager.exec(this, event);
  17541. }
  17542. #openDropdown(event) {
  17543. if (this.#isDropdownVisible) {
  17544. this.hideDropdown();
  17545. return;
  17546. }
  17547. this.#dropdownWasFromKeyboard = event.detail === 0;
  17548. if (!this.#openDropdownAC) {
  17549. this.#openDropdownAC = new AbortController();
  17550. window.addEventListener("pointerdown", this.#pointerDown.bind(this), {
  17551. signal: this.#uiManager.combinedSignal(this.#openDropdownAC)
  17552. });
  17553. }
  17554. if (this.#dropdown) {
  17555. this.#dropdown.classList.remove("hidden");
  17556. return;
  17557. }
  17558. const root = this.#dropdown = this.#getDropdownRoot();
  17559. this.#button.append(root);
  17560. }
  17561. #pointerDown(event) {
  17562. if (this.#dropdown?.contains(event.target)) {
  17563. return;
  17564. }
  17565. this.hideDropdown();
  17566. }
  17567. hideDropdown() {
  17568. this.#dropdown?.classList.add("hidden");
  17569. this.#openDropdownAC?.abort();
  17570. this.#openDropdownAC = null;
  17571. }
  17572. get #isDropdownVisible() {
  17573. return this.#dropdown && !this.#dropdown.classList.contains("hidden");
  17574. }
  17575. _hideDropdownFromKeyboard() {
  17576. if (this.#isMainColorPicker) {
  17577. return;
  17578. }
  17579. if (!this.#isDropdownVisible) {
  17580. this.#editor?.unselect();
  17581. return;
  17582. }
  17583. this.hideDropdown();
  17584. this.#button.focus({
  17585. preventScroll: true,
  17586. focusVisible: this.#dropdownWasFromKeyboard
  17587. });
  17588. }
  17589. updateColor(color) {
  17590. if (this.#buttonSwatch) {
  17591. this.#buttonSwatch.style.backgroundColor = color;
  17592. }
  17593. if (!this.#dropdown) {
  17594. return;
  17595. }
  17596. const i = this.#uiManager.highlightColors.values();
  17597. for (const child of this.#dropdown.children) {
  17598. child.setAttribute("aria-selected", i.next().value === color);
  17599. }
  17600. }
  17601. destroy() {
  17602. this.#button?.remove();
  17603. this.#button = null;
  17604. this.#buttonSwatch = null;
  17605. this.#dropdown?.remove();
  17606. this.#dropdown = null;
  17607. }
  17608. }
  17609. ;// ./src/display/editor/highlight.js
  17610. class HighlightEditor extends AnnotationEditor {
  17611. #anchorNode = null;
  17612. #anchorOffset = 0;
  17613. #boxes;
  17614. #clipPathId = null;
  17615. #colorPicker = null;
  17616. #focusOutlines = null;
  17617. #focusNode = null;
  17618. #focusOffset = 0;
  17619. #highlightDiv = null;
  17620. #highlightOutlines = null;
  17621. #id = null;
  17622. #isFreeHighlight = false;
  17623. #lastPoint = null;
  17624. #opacity;
  17625. #outlineId = null;
  17626. #text = "";
  17627. #thickness;
  17628. #methodOfCreation = "";
  17629. static _defaultColor = null;
  17630. static _defaultOpacity = 1;
  17631. static _defaultThickness = 12;
  17632. static _type = "highlight";
  17633. static _editorType = AnnotationEditorType.HIGHLIGHT;
  17634. static _freeHighlightId = -1;
  17635. static _freeHighlight = null;
  17636. static _freeHighlightClipId = "";
  17637. static get _keyboardManager() {
  17638. const proto = HighlightEditor.prototype;
  17639. return shadow(this, "_keyboardManager", new KeyboardManager([[["ArrowLeft", "mac+ArrowLeft"], proto._moveCaret, {
  17640. args: [0]
  17641. }], [["ArrowRight", "mac+ArrowRight"], proto._moveCaret, {
  17642. args: [1]
  17643. }], [["ArrowUp", "mac+ArrowUp"], proto._moveCaret, {
  17644. args: [2]
  17645. }], [["ArrowDown", "mac+ArrowDown"], proto._moveCaret, {
  17646. args: [3]
  17647. }]]));
  17648. }
  17649. constructor(params) {
  17650. super({
  17651. ...params,
  17652. name: "highlightEditor"
  17653. });
  17654. this.color = params.color || HighlightEditor._defaultColor;
  17655. this.#thickness = params.thickness || HighlightEditor._defaultThickness;
  17656. this.#opacity = params.opacity || HighlightEditor._defaultOpacity;
  17657. this.#boxes = params.boxes || null;
  17658. this.#methodOfCreation = params.methodOfCreation || "";
  17659. this.#text = params.text || "";
  17660. this._isDraggable = false;
  17661. this.defaultL10nId = "pdfjs-editor-highlight-editor";
  17662. if (params.highlightId > -1) {
  17663. this.#isFreeHighlight = true;
  17664. this.#createFreeOutlines(params);
  17665. this.#addToDrawLayer();
  17666. } else if (this.#boxes) {
  17667. this.#anchorNode = params.anchorNode;
  17668. this.#anchorOffset = params.anchorOffset;
  17669. this.#focusNode = params.focusNode;
  17670. this.#focusOffset = params.focusOffset;
  17671. this.#createOutlines();
  17672. this.#addToDrawLayer();
  17673. this.rotate(this.rotation);
  17674. }
  17675. }
  17676. get telemetryInitialData() {
  17677. return {
  17678. action: "added",
  17679. type: this.#isFreeHighlight ? "free_highlight" : "highlight",
  17680. color: this._uiManager.highlightColorNames.get(this.color),
  17681. thickness: this.#thickness,
  17682. methodOfCreation: this.#methodOfCreation
  17683. };
  17684. }
  17685. get telemetryFinalData() {
  17686. return {
  17687. type: "highlight",
  17688. color: this._uiManager.highlightColorNames.get(this.color)
  17689. };
  17690. }
  17691. static computeTelemetryFinalData(data) {
  17692. return {
  17693. numberOfColors: data.get("color").size
  17694. };
  17695. }
  17696. #createOutlines() {
  17697. const outliner = new HighlightOutliner(this.#boxes, 0.001);
  17698. this.#highlightOutlines = outliner.getOutlines();
  17699. [this.x, this.y, this.width, this.height] = this.#highlightOutlines.box;
  17700. const outlinerForOutline = new HighlightOutliner(this.#boxes, 0.0025, 0.001, this._uiManager.direction === "ltr");
  17701. this.#focusOutlines = outlinerForOutline.getOutlines();
  17702. const {
  17703. lastPoint
  17704. } = this.#focusOutlines;
  17705. this.#lastPoint = [(lastPoint[0] - this.x) / this.width, (lastPoint[1] - this.y) / this.height];
  17706. }
  17707. #createFreeOutlines({
  17708. highlightOutlines,
  17709. highlightId,
  17710. clipPathId
  17711. }) {
  17712. this.#highlightOutlines = highlightOutlines;
  17713. const extraThickness = 1.5;
  17714. this.#focusOutlines = highlightOutlines.getNewOutline(this.#thickness / 2 + extraThickness, 0.0025);
  17715. if (highlightId >= 0) {
  17716. this.#id = highlightId;
  17717. this.#clipPathId = clipPathId;
  17718. this.parent.drawLayer.finalizeDraw(highlightId, {
  17719. bbox: highlightOutlines.box,
  17720. path: {
  17721. d: highlightOutlines.toSVGPath()
  17722. }
  17723. });
  17724. this.#outlineId = this.parent.drawLayer.drawOutline({
  17725. rootClass: {
  17726. highlightOutline: true,
  17727. free: true
  17728. },
  17729. bbox: this.#focusOutlines.box,
  17730. path: {
  17731. d: this.#focusOutlines.toSVGPath()
  17732. }
  17733. }, true);
  17734. } else if (this.parent) {
  17735. const angle = this.parent.viewport.rotation;
  17736. this.parent.drawLayer.updateProperties(this.#id, {
  17737. bbox: HighlightEditor.#rotateBbox(this.#highlightOutlines.box, (angle - this.rotation + 360) % 360),
  17738. path: {
  17739. d: highlightOutlines.toSVGPath()
  17740. }
  17741. });
  17742. this.parent.drawLayer.updateProperties(this.#outlineId, {
  17743. bbox: HighlightEditor.#rotateBbox(this.#focusOutlines.box, angle),
  17744. path: {
  17745. d: this.#focusOutlines.toSVGPath()
  17746. }
  17747. });
  17748. }
  17749. const [x, y, width, height] = highlightOutlines.box;
  17750. switch (this.rotation) {
  17751. case 0:
  17752. this.x = x;
  17753. this.y = y;
  17754. this.width = width;
  17755. this.height = height;
  17756. break;
  17757. case 90:
  17758. {
  17759. const [pageWidth, pageHeight] = this.parentDimensions;
  17760. this.x = y;
  17761. this.y = 1 - x;
  17762. this.width = width * pageHeight / pageWidth;
  17763. this.height = height * pageWidth / pageHeight;
  17764. break;
  17765. }
  17766. case 180:
  17767. this.x = 1 - x;
  17768. this.y = 1 - y;
  17769. this.width = width;
  17770. this.height = height;
  17771. break;
  17772. case 270:
  17773. {
  17774. const [pageWidth, pageHeight] = this.parentDimensions;
  17775. this.x = 1 - y;
  17776. this.y = x;
  17777. this.width = width * pageHeight / pageWidth;
  17778. this.height = height * pageWidth / pageHeight;
  17779. break;
  17780. }
  17781. }
  17782. const {
  17783. lastPoint
  17784. } = this.#focusOutlines;
  17785. this.#lastPoint = [(lastPoint[0] - x) / width, (lastPoint[1] - y) / height];
  17786. }
  17787. static initialize(l10n, uiManager) {
  17788. AnnotationEditor.initialize(l10n, uiManager);
  17789. HighlightEditor._defaultColor ||= uiManager.highlightColors?.values().next().value || "#fff066";
  17790. }
  17791. static updateDefaultParams(type, value) {
  17792. switch (type) {
  17793. case AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR:
  17794. HighlightEditor._defaultColor = value;
  17795. break;
  17796. case AnnotationEditorParamsType.HIGHLIGHT_THICKNESS:
  17797. HighlightEditor._defaultThickness = value;
  17798. break;
  17799. }
  17800. }
  17801. translateInPage(x, y) {}
  17802. get toolbarPosition() {
  17803. return this.#lastPoint;
  17804. }
  17805. updateParams(type, value) {
  17806. switch (type) {
  17807. case AnnotationEditorParamsType.HIGHLIGHT_COLOR:
  17808. this.#updateColor(value);
  17809. break;
  17810. case AnnotationEditorParamsType.HIGHLIGHT_THICKNESS:
  17811. this.#updateThickness(value);
  17812. break;
  17813. }
  17814. }
  17815. static get defaultPropertiesToUpdate() {
  17816. return [[AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR, HighlightEditor._defaultColor], [AnnotationEditorParamsType.HIGHLIGHT_THICKNESS, HighlightEditor._defaultThickness]];
  17817. }
  17818. get propertiesToUpdate() {
  17819. return [[AnnotationEditorParamsType.HIGHLIGHT_COLOR, this.color || HighlightEditor._defaultColor], [AnnotationEditorParamsType.HIGHLIGHT_THICKNESS, this.#thickness || HighlightEditor._defaultThickness], [AnnotationEditorParamsType.HIGHLIGHT_FREE, this.#isFreeHighlight]];
  17820. }
  17821. #updateColor(color) {
  17822. const setColorAndOpacity = (col, opa) => {
  17823. this.color = col;
  17824. this.#opacity = opa;
  17825. this.parent?.drawLayer.updateProperties(this.#id, {
  17826. root: {
  17827. fill: col,
  17828. "fill-opacity": opa
  17829. }
  17830. });
  17831. this.#colorPicker?.updateColor(col);
  17832. };
  17833. const savedColor = this.color;
  17834. const savedOpacity = this.#opacity;
  17835. this.addCommands({
  17836. cmd: setColorAndOpacity.bind(this, color, HighlightEditor._defaultOpacity),
  17837. undo: setColorAndOpacity.bind(this, savedColor, savedOpacity),
  17838. post: this._uiManager.updateUI.bind(this._uiManager, this),
  17839. mustExec: true,
  17840. type: AnnotationEditorParamsType.HIGHLIGHT_COLOR,
  17841. overwriteIfSameType: true,
  17842. keepUndo: true
  17843. });
  17844. this._reportTelemetry({
  17845. action: "color_changed",
  17846. color: this._uiManager.highlightColorNames.get(color)
  17847. }, true);
  17848. }
  17849. #updateThickness(thickness) {
  17850. const savedThickness = this.#thickness;
  17851. const setThickness = th => {
  17852. this.#thickness = th;
  17853. this.#changeThickness(th);
  17854. };
  17855. this.addCommands({
  17856. cmd: setThickness.bind(this, thickness),
  17857. undo: setThickness.bind(this, savedThickness),
  17858. post: this._uiManager.updateUI.bind(this._uiManager, this),
  17859. mustExec: true,
  17860. type: AnnotationEditorParamsType.INK_THICKNESS,
  17861. overwriteIfSameType: true,
  17862. keepUndo: true
  17863. });
  17864. this._reportTelemetry({
  17865. action: "thickness_changed",
  17866. thickness
  17867. }, true);
  17868. }
  17869. async addEditToolbar() {
  17870. const toolbar = await super.addEditToolbar();
  17871. if (!toolbar) {
  17872. return null;
  17873. }
  17874. if (this._uiManager.highlightColors) {
  17875. this.#colorPicker = new ColorPicker({
  17876. editor: this
  17877. });
  17878. toolbar.addColorPicker(this.#colorPicker);
  17879. }
  17880. return toolbar;
  17881. }
  17882. disableEditing() {
  17883. super.disableEditing();
  17884. this.div.classList.toggle("disabled", true);
  17885. }
  17886. enableEditing() {
  17887. super.enableEditing();
  17888. this.div.classList.toggle("disabled", false);
  17889. }
  17890. fixAndSetPosition() {
  17891. return super.fixAndSetPosition(this.#getRotation());
  17892. }
  17893. getBaseTranslation() {
  17894. return [0, 0];
  17895. }
  17896. getRect(tx, ty) {
  17897. return super.getRect(tx, ty, this.#getRotation());
  17898. }
  17899. onceAdded(focus) {
  17900. if (!this.annotationElementId) {
  17901. this.parent.addUndoableEditor(this);
  17902. }
  17903. if (focus) {
  17904. this.div.focus();
  17905. }
  17906. }
  17907. remove() {
  17908. this.#cleanDrawLayer();
  17909. this._reportTelemetry({
  17910. action: "deleted"
  17911. });
  17912. super.remove();
  17913. }
  17914. rebuild() {
  17915. if (!this.parent) {
  17916. return;
  17917. }
  17918. super.rebuild();
  17919. if (this.div === null) {
  17920. return;
  17921. }
  17922. this.#addToDrawLayer();
  17923. if (!this.isAttachedToDOM) {
  17924. this.parent.add(this);
  17925. }
  17926. }
  17927. setParent(parent) {
  17928. let mustBeSelected = false;
  17929. if (this.parent && !parent) {
  17930. this.#cleanDrawLayer();
  17931. } else if (parent) {
  17932. this.#addToDrawLayer(parent);
  17933. mustBeSelected = !this.parent && this.div?.classList.contains("selectedEditor");
  17934. }
  17935. super.setParent(parent);
  17936. this.show(this._isVisible);
  17937. if (mustBeSelected) {
  17938. this.select();
  17939. }
  17940. }
  17941. #changeThickness(thickness) {
  17942. if (!this.#isFreeHighlight) {
  17943. return;
  17944. }
  17945. this.#createFreeOutlines({
  17946. highlightOutlines: this.#highlightOutlines.getNewOutline(thickness / 2)
  17947. });
  17948. this.fixAndSetPosition();
  17949. const [parentWidth, parentHeight] = this.parentDimensions;
  17950. this.setDims(this.width * parentWidth, this.height * parentHeight);
  17951. }
  17952. #cleanDrawLayer() {
  17953. if (this.#id === null || !this.parent) {
  17954. return;
  17955. }
  17956. this.parent.drawLayer.remove(this.#id);
  17957. this.#id = null;
  17958. this.parent.drawLayer.remove(this.#outlineId);
  17959. this.#outlineId = null;
  17960. }
  17961. #addToDrawLayer(parent = this.parent) {
  17962. if (this.#id !== null) {
  17963. return;
  17964. }
  17965. ({
  17966. id: this.#id,
  17967. clipPathId: this.#clipPathId
  17968. } = parent.drawLayer.draw({
  17969. bbox: this.#highlightOutlines.box,
  17970. root: {
  17971. viewBox: "0 0 1 1",
  17972. fill: this.color,
  17973. "fill-opacity": this.#opacity
  17974. },
  17975. rootClass: {
  17976. highlight: true,
  17977. free: this.#isFreeHighlight
  17978. },
  17979. path: {
  17980. d: this.#highlightOutlines.toSVGPath()
  17981. }
  17982. }, false, true));
  17983. this.#outlineId = parent.drawLayer.drawOutline({
  17984. rootClass: {
  17985. highlightOutline: true,
  17986. free: this.#isFreeHighlight
  17987. },
  17988. bbox: this.#focusOutlines.box,
  17989. path: {
  17990. d: this.#focusOutlines.toSVGPath()
  17991. }
  17992. }, this.#isFreeHighlight);
  17993. if (this.#highlightDiv) {
  17994. this.#highlightDiv.style.clipPath = this.#clipPathId;
  17995. }
  17996. }
  17997. static #rotateBbox([x, y, width, height], angle) {
  17998. switch (angle) {
  17999. case 90:
  18000. return [1 - y - height, x, height, width];
  18001. case 180:
  18002. return [1 - x - width, 1 - y - height, width, height];
  18003. case 270:
  18004. return [y, 1 - x - width, height, width];
  18005. }
  18006. return [x, y, width, height];
  18007. }
  18008. rotate(angle) {
  18009. const {
  18010. drawLayer
  18011. } = this.parent;
  18012. let box;
  18013. if (this.#isFreeHighlight) {
  18014. angle = (angle - this.rotation + 360) % 360;
  18015. box = HighlightEditor.#rotateBbox(this.#highlightOutlines.box, angle);
  18016. } else {
  18017. box = HighlightEditor.#rotateBbox([this.x, this.y, this.width, this.height], angle);
  18018. }
  18019. drawLayer.updateProperties(this.#id, {
  18020. bbox: box,
  18021. root: {
  18022. "data-main-rotation": angle
  18023. }
  18024. });
  18025. drawLayer.updateProperties(this.#outlineId, {
  18026. bbox: HighlightEditor.#rotateBbox(this.#focusOutlines.box, angle),
  18027. root: {
  18028. "data-main-rotation": angle
  18029. }
  18030. });
  18031. }
  18032. render() {
  18033. if (this.div) {
  18034. return this.div;
  18035. }
  18036. const div = super.render();
  18037. if (this.#text) {
  18038. div.setAttribute("aria-label", this.#text);
  18039. div.setAttribute("role", "mark");
  18040. }
  18041. if (this.#isFreeHighlight) {
  18042. div.classList.add("free");
  18043. } else {
  18044. this.div.addEventListener("keydown", this.#keydown.bind(this), {
  18045. signal: this._uiManager._signal
  18046. });
  18047. }
  18048. const highlightDiv = this.#highlightDiv = document.createElement("div");
  18049. div.append(highlightDiv);
  18050. highlightDiv.setAttribute("aria-hidden", "true");
  18051. highlightDiv.className = "internal";
  18052. highlightDiv.style.clipPath = this.#clipPathId;
  18053. const [parentWidth, parentHeight] = this.parentDimensions;
  18054. this.setDims(this.width * parentWidth, this.height * parentHeight);
  18055. bindEvents(this, this.#highlightDiv, ["pointerover", "pointerleave"]);
  18056. this.enableEditing();
  18057. return div;
  18058. }
  18059. pointerover() {
  18060. if (!this.isSelected) {
  18061. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  18062. rootClass: {
  18063. hovered: true
  18064. }
  18065. });
  18066. }
  18067. }
  18068. pointerleave() {
  18069. if (!this.isSelected) {
  18070. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  18071. rootClass: {
  18072. hovered: false
  18073. }
  18074. });
  18075. }
  18076. }
  18077. #keydown(event) {
  18078. HighlightEditor._keyboardManager.exec(this, event);
  18079. }
  18080. _moveCaret(direction) {
  18081. this.parent.unselect(this);
  18082. switch (direction) {
  18083. case 0:
  18084. case 2:
  18085. this.#setCaret(true);
  18086. break;
  18087. case 1:
  18088. case 3:
  18089. this.#setCaret(false);
  18090. break;
  18091. }
  18092. }
  18093. #setCaret(start) {
  18094. if (!this.#anchorNode) {
  18095. return;
  18096. }
  18097. const selection = window.getSelection();
  18098. if (start) {
  18099. selection.setPosition(this.#anchorNode, this.#anchorOffset);
  18100. } else {
  18101. selection.setPosition(this.#focusNode, this.#focusOffset);
  18102. }
  18103. }
  18104. select() {
  18105. super.select();
  18106. if (!this.#outlineId) {
  18107. return;
  18108. }
  18109. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  18110. rootClass: {
  18111. hovered: false,
  18112. selected: true
  18113. }
  18114. });
  18115. }
  18116. unselect() {
  18117. super.unselect();
  18118. if (!this.#outlineId) {
  18119. return;
  18120. }
  18121. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  18122. rootClass: {
  18123. selected: false
  18124. }
  18125. });
  18126. if (!this.#isFreeHighlight) {
  18127. this.#setCaret(false);
  18128. }
  18129. }
  18130. get _mustFixPosition() {
  18131. return !this.#isFreeHighlight;
  18132. }
  18133. show(visible = this._isVisible) {
  18134. super.show(visible);
  18135. if (this.parent) {
  18136. this.parent.drawLayer.updateProperties(this.#id, {
  18137. rootClass: {
  18138. hidden: !visible
  18139. }
  18140. });
  18141. this.parent.drawLayer.updateProperties(this.#outlineId, {
  18142. rootClass: {
  18143. hidden: !visible
  18144. }
  18145. });
  18146. }
  18147. }
  18148. #getRotation() {
  18149. return this.#isFreeHighlight ? this.rotation : 0;
  18150. }
  18151. #serializeBoxes() {
  18152. if (this.#isFreeHighlight) {
  18153. return null;
  18154. }
  18155. const [pageWidth, pageHeight] = this.pageDimensions;
  18156. const [pageX, pageY] = this.pageTranslation;
  18157. const boxes = this.#boxes;
  18158. const quadPoints = new Float32Array(boxes.length * 8);
  18159. let i = 0;
  18160. for (const {
  18161. x,
  18162. y,
  18163. width,
  18164. height
  18165. } of boxes) {
  18166. const sx = x * pageWidth + pageX;
  18167. const sy = (1 - y) * pageHeight + pageY;
  18168. quadPoints[i] = quadPoints[i + 4] = sx;
  18169. quadPoints[i + 1] = quadPoints[i + 3] = sy;
  18170. quadPoints[i + 2] = quadPoints[i + 6] = sx + width * pageWidth;
  18171. quadPoints[i + 5] = quadPoints[i + 7] = sy - height * pageHeight;
  18172. i += 8;
  18173. }
  18174. return quadPoints;
  18175. }
  18176. #serializeOutlines(rect) {
  18177. return this.#highlightOutlines.serialize(rect, this.#getRotation());
  18178. }
  18179. static startHighlighting(parent, isLTR, {
  18180. target: textLayer,
  18181. x,
  18182. y
  18183. }) {
  18184. const {
  18185. x: layerX,
  18186. y: layerY,
  18187. width: parentWidth,
  18188. height: parentHeight
  18189. } = textLayer.getBoundingClientRect();
  18190. const ac = new AbortController();
  18191. const signal = parent.combinedSignal(ac);
  18192. const pointerUpCallback = e => {
  18193. ac.abort();
  18194. this.#endHighlight(parent, e);
  18195. };
  18196. window.addEventListener("blur", pointerUpCallback, {
  18197. signal
  18198. });
  18199. window.addEventListener("pointerup", pointerUpCallback, {
  18200. signal
  18201. });
  18202. window.addEventListener("pointerdown", stopEvent, {
  18203. capture: true,
  18204. passive: false,
  18205. signal
  18206. });
  18207. window.addEventListener("contextmenu", noContextMenu, {
  18208. signal
  18209. });
  18210. textLayer.addEventListener("pointermove", this.#highlightMove.bind(this, parent), {
  18211. signal
  18212. });
  18213. this._freeHighlight = new FreeHighlightOutliner({
  18214. x,
  18215. y
  18216. }, [layerX, layerY, parentWidth, parentHeight], parent.scale, this._defaultThickness / 2, isLTR, 0.001);
  18217. ({
  18218. id: this._freeHighlightId,
  18219. clipPathId: this._freeHighlightClipId
  18220. } = parent.drawLayer.draw({
  18221. bbox: [0, 0, 1, 1],
  18222. root: {
  18223. viewBox: "0 0 1 1",
  18224. fill: this._defaultColor,
  18225. "fill-opacity": this._defaultOpacity
  18226. },
  18227. rootClass: {
  18228. highlight: true,
  18229. free: true
  18230. },
  18231. path: {
  18232. d: this._freeHighlight.toSVGPath()
  18233. }
  18234. }, true, true));
  18235. }
  18236. static #highlightMove(parent, event) {
  18237. if (this._freeHighlight.add(event)) {
  18238. parent.drawLayer.updateProperties(this._freeHighlightId, {
  18239. path: {
  18240. d: this._freeHighlight.toSVGPath()
  18241. }
  18242. });
  18243. }
  18244. }
  18245. static #endHighlight(parent, event) {
  18246. if (!this._freeHighlight.isEmpty()) {
  18247. parent.createAndAddNewEditor(event, false, {
  18248. highlightId: this._freeHighlightId,
  18249. highlightOutlines: this._freeHighlight.getOutlines(),
  18250. clipPathId: this._freeHighlightClipId,
  18251. methodOfCreation: "main_toolbar"
  18252. });
  18253. } else {
  18254. parent.drawLayer.remove(this._freeHighlightId);
  18255. }
  18256. this._freeHighlightId = -1;
  18257. this._freeHighlight = null;
  18258. this._freeHighlightClipId = "";
  18259. }
  18260. static async deserialize(data, parent, uiManager) {
  18261. let initialData = null;
  18262. if (data instanceof HighlightAnnotationElement) {
  18263. const {
  18264. data: {
  18265. quadPoints,
  18266. rect,
  18267. rotation,
  18268. id,
  18269. color,
  18270. opacity,
  18271. popupRef
  18272. },
  18273. parent: {
  18274. page: {
  18275. pageNumber
  18276. }
  18277. }
  18278. } = data;
  18279. initialData = data = {
  18280. annotationType: AnnotationEditorType.HIGHLIGHT,
  18281. color: Array.from(color),
  18282. opacity,
  18283. quadPoints,
  18284. boxes: null,
  18285. pageIndex: pageNumber - 1,
  18286. rect: rect.slice(0),
  18287. rotation,
  18288. id,
  18289. deleted: false,
  18290. popupRef
  18291. };
  18292. } else if (data instanceof InkAnnotationElement) {
  18293. const {
  18294. data: {
  18295. inkLists,
  18296. rect,
  18297. rotation,
  18298. id,
  18299. color,
  18300. borderStyle: {
  18301. rawWidth: thickness
  18302. },
  18303. popupRef
  18304. },
  18305. parent: {
  18306. page: {
  18307. pageNumber
  18308. }
  18309. }
  18310. } = data;
  18311. initialData = data = {
  18312. annotationType: AnnotationEditorType.HIGHLIGHT,
  18313. color: Array.from(color),
  18314. thickness,
  18315. inkLists,
  18316. boxes: null,
  18317. pageIndex: pageNumber - 1,
  18318. rect: rect.slice(0),
  18319. rotation,
  18320. id,
  18321. deleted: false,
  18322. popupRef
  18323. };
  18324. }
  18325. const {
  18326. color,
  18327. quadPoints,
  18328. inkLists,
  18329. opacity
  18330. } = data;
  18331. const editor = await super.deserialize(data, parent, uiManager);
  18332. editor.color = Util.makeHexColor(...color);
  18333. editor.#opacity = opacity || 1;
  18334. if (inkLists) {
  18335. editor.#thickness = data.thickness;
  18336. }
  18337. editor.annotationElementId = data.id || null;
  18338. editor._initialData = initialData;
  18339. const [pageWidth, pageHeight] = editor.pageDimensions;
  18340. const [pageX, pageY] = editor.pageTranslation;
  18341. if (quadPoints) {
  18342. const boxes = editor.#boxes = [];
  18343. for (let i = 0; i < quadPoints.length; i += 8) {
  18344. boxes.push({
  18345. x: (quadPoints[i] - pageX) / pageWidth,
  18346. y: 1 - (quadPoints[i + 1] - pageY) / pageHeight,
  18347. width: (quadPoints[i + 2] - quadPoints[i]) / pageWidth,
  18348. height: (quadPoints[i + 1] - quadPoints[i + 5]) / pageHeight
  18349. });
  18350. }
  18351. editor.#createOutlines();
  18352. editor.#addToDrawLayer();
  18353. editor.rotate(editor.rotation);
  18354. } else if (inkLists) {
  18355. editor.#isFreeHighlight = true;
  18356. const points = inkLists[0];
  18357. const point = {
  18358. x: points[0] - pageX,
  18359. y: pageHeight - (points[1] - pageY)
  18360. };
  18361. const outliner = new FreeHighlightOutliner(point, [0, 0, pageWidth, pageHeight], 1, editor.#thickness / 2, true, 0.001);
  18362. for (let i = 0, ii = points.length; i < ii; i += 2) {
  18363. point.x = points[i] - pageX;
  18364. point.y = pageHeight - (points[i + 1] - pageY);
  18365. outliner.add(point);
  18366. }
  18367. const {
  18368. id,
  18369. clipPathId
  18370. } = parent.drawLayer.draw({
  18371. bbox: [0, 0, 1, 1],
  18372. root: {
  18373. viewBox: "0 0 1 1",
  18374. fill: editor.color,
  18375. "fill-opacity": editor._defaultOpacity
  18376. },
  18377. rootClass: {
  18378. highlight: true,
  18379. free: true
  18380. },
  18381. path: {
  18382. d: outliner.toSVGPath()
  18383. }
  18384. }, true, true);
  18385. editor.#createFreeOutlines({
  18386. highlightOutlines: outliner.getOutlines(),
  18387. highlightId: id,
  18388. clipPathId
  18389. });
  18390. editor.#addToDrawLayer();
  18391. editor.rotate(editor.parentRotation);
  18392. }
  18393. return editor;
  18394. }
  18395. serialize(isForCopying = false) {
  18396. if (this.isEmpty() || isForCopying) {
  18397. return null;
  18398. }
  18399. if (this.deleted) {
  18400. return this.serializeDeleted();
  18401. }
  18402. const rect = this.getRect(0, 0);
  18403. const color = AnnotationEditor._colorManager.convert(this.color);
  18404. const serialized = {
  18405. annotationType: AnnotationEditorType.HIGHLIGHT,
  18406. color,
  18407. opacity: this.#opacity,
  18408. thickness: this.#thickness,
  18409. quadPoints: this.#serializeBoxes(),
  18410. outlines: this.#serializeOutlines(rect),
  18411. pageIndex: this.pageIndex,
  18412. rect,
  18413. rotation: this.#getRotation(),
  18414. structTreeParentId: this._structTreeParentId
  18415. };
  18416. if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
  18417. return null;
  18418. }
  18419. serialized.id = this.annotationElementId;
  18420. return serialized;
  18421. }
  18422. #hasElementChanged(serialized) {
  18423. const {
  18424. color
  18425. } = this._initialData;
  18426. return serialized.color.some((c, i) => c !== color[i]);
  18427. }
  18428. renderAnnotationElement(annotation) {
  18429. annotation.updateEdited({
  18430. rect: this.getRect(0, 0)
  18431. });
  18432. return null;
  18433. }
  18434. static canCreateNewEmptyEditor() {
  18435. return false;
  18436. }
  18437. }
  18438. ;// ./src/display/editor/draw.js
  18439. class DrawingOptions {
  18440. #svgProperties = Object.create(null);
  18441. updateProperty(name, value) {
  18442. this[name] = value;
  18443. this.updateSVGProperty(name, value);
  18444. }
  18445. updateProperties(properties) {
  18446. if (!properties) {
  18447. return;
  18448. }
  18449. for (const [name, value] of Object.entries(properties)) {
  18450. if (!name.startsWith("_")) {
  18451. this.updateProperty(name, value);
  18452. }
  18453. }
  18454. }
  18455. updateSVGProperty(name, value) {
  18456. this.#svgProperties[name] = value;
  18457. }
  18458. toSVGProperties() {
  18459. const root = this.#svgProperties;
  18460. this.#svgProperties = Object.create(null);
  18461. return {
  18462. root
  18463. };
  18464. }
  18465. reset() {
  18466. this.#svgProperties = Object.create(null);
  18467. }
  18468. updateAll(options = this) {
  18469. this.updateProperties(options);
  18470. }
  18471. clone() {
  18472. unreachable("Not implemented");
  18473. }
  18474. }
  18475. class DrawingEditor extends AnnotationEditor {
  18476. #drawOutlines = null;
  18477. #mustBeCommitted;
  18478. _drawId = null;
  18479. static _currentDrawId = -1;
  18480. static _currentParent = null;
  18481. static #currentDraw = null;
  18482. static #currentDrawingAC = null;
  18483. static #currentDrawingOptions = null;
  18484. static #currentPointerId = NaN;
  18485. static #currentPointerType = null;
  18486. static #currentPointerIds = null;
  18487. static #currentMoveTimestamp = NaN;
  18488. static _INNER_MARGIN = 3;
  18489. constructor(params) {
  18490. super(params);
  18491. this.#mustBeCommitted = params.mustBeCommitted || false;
  18492. this._addOutlines(params);
  18493. }
  18494. _addOutlines(params) {
  18495. if (params.drawOutlines) {
  18496. this.#createDrawOutlines(params);
  18497. this.#addToDrawLayer();
  18498. }
  18499. }
  18500. #createDrawOutlines({
  18501. drawOutlines,
  18502. drawId,
  18503. drawingOptions
  18504. }) {
  18505. this.#drawOutlines = drawOutlines;
  18506. this._drawingOptions ||= drawingOptions;
  18507. if (drawId >= 0) {
  18508. this._drawId = drawId;
  18509. this.parent.drawLayer.finalizeDraw(drawId, drawOutlines.defaultProperties);
  18510. } else {
  18511. this._drawId = this.#createDrawing(drawOutlines, this.parent);
  18512. }
  18513. this.#updateBbox(drawOutlines.box);
  18514. }
  18515. #createDrawing(drawOutlines, parent) {
  18516. const {
  18517. id
  18518. } = parent.drawLayer.draw(DrawingEditor._mergeSVGProperties(this._drawingOptions.toSVGProperties(), drawOutlines.defaultSVGProperties), false, false);
  18519. return id;
  18520. }
  18521. static _mergeSVGProperties(p1, p2) {
  18522. const p1Keys = new Set(Object.keys(p1));
  18523. for (const [key, value] of Object.entries(p2)) {
  18524. if (p1Keys.has(key)) {
  18525. Object.assign(p1[key], value);
  18526. } else {
  18527. p1[key] = value;
  18528. }
  18529. }
  18530. return p1;
  18531. }
  18532. static getDefaultDrawingOptions(_options) {
  18533. unreachable("Not implemented");
  18534. }
  18535. static get typesMap() {
  18536. unreachable("Not implemented");
  18537. }
  18538. static get isDrawer() {
  18539. return true;
  18540. }
  18541. static get supportMultipleDrawings() {
  18542. return false;
  18543. }
  18544. static updateDefaultParams(type, value) {
  18545. const propertyName = this.typesMap.get(type);
  18546. if (propertyName) {
  18547. this._defaultDrawingOptions.updateProperty(propertyName, value);
  18548. }
  18549. if (this._currentParent) {
  18550. DrawingEditor.#currentDraw.updateProperty(propertyName, value);
  18551. this._currentParent.drawLayer.updateProperties(this._currentDrawId, this._defaultDrawingOptions.toSVGProperties());
  18552. }
  18553. }
  18554. updateParams(type, value) {
  18555. const propertyName = this.constructor.typesMap.get(type);
  18556. if (propertyName) {
  18557. this._updateProperty(type, propertyName, value);
  18558. }
  18559. }
  18560. static get defaultPropertiesToUpdate() {
  18561. const properties = [];
  18562. const options = this._defaultDrawingOptions;
  18563. for (const [type, name] of this.typesMap) {
  18564. properties.push([type, options[name]]);
  18565. }
  18566. return properties;
  18567. }
  18568. get propertiesToUpdate() {
  18569. const properties = [];
  18570. const {
  18571. _drawingOptions
  18572. } = this;
  18573. for (const [type, name] of this.constructor.typesMap) {
  18574. properties.push([type, _drawingOptions[name]]);
  18575. }
  18576. return properties;
  18577. }
  18578. _updateProperty(type, name, value) {
  18579. const options = this._drawingOptions;
  18580. const savedValue = options[name];
  18581. const setter = val => {
  18582. options.updateProperty(name, val);
  18583. const bbox = this.#drawOutlines.updateProperty(name, val);
  18584. if (bbox) {
  18585. this.#updateBbox(bbox);
  18586. }
  18587. this.parent?.drawLayer.updateProperties(this._drawId, options.toSVGProperties());
  18588. };
  18589. this.addCommands({
  18590. cmd: setter.bind(this, value),
  18591. undo: setter.bind(this, savedValue),
  18592. post: this._uiManager.updateUI.bind(this._uiManager, this),
  18593. mustExec: true,
  18594. type,
  18595. overwriteIfSameType: true,
  18596. keepUndo: true
  18597. });
  18598. }
  18599. _onResizing() {
  18600. this.parent?.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties(this.#drawOutlines.getPathResizingSVGProperties(this.#convertToDrawSpace()), {
  18601. bbox: this.#rotateBox()
  18602. }));
  18603. }
  18604. _onResized() {
  18605. this.parent?.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties(this.#drawOutlines.getPathResizedSVGProperties(this.#convertToDrawSpace()), {
  18606. bbox: this.#rotateBox()
  18607. }));
  18608. }
  18609. _onTranslating(_x, _y) {
  18610. this.parent?.drawLayer.updateProperties(this._drawId, {
  18611. bbox: this.#rotateBox()
  18612. });
  18613. }
  18614. _onTranslated() {
  18615. this.parent?.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties(this.#drawOutlines.getPathTranslatedSVGProperties(this.#convertToDrawSpace(), this.parentDimensions), {
  18616. bbox: this.#rotateBox()
  18617. }));
  18618. }
  18619. _onStartDragging() {
  18620. this.parent?.drawLayer.updateProperties(this._drawId, {
  18621. rootClass: {
  18622. moving: true
  18623. }
  18624. });
  18625. }
  18626. _onStopDragging() {
  18627. this.parent?.drawLayer.updateProperties(this._drawId, {
  18628. rootClass: {
  18629. moving: false
  18630. }
  18631. });
  18632. }
  18633. commit() {
  18634. super.commit();
  18635. this.disableEditMode();
  18636. this.disableEditing();
  18637. }
  18638. disableEditing() {
  18639. super.disableEditing();
  18640. this.div.classList.toggle("disabled", true);
  18641. }
  18642. enableEditing() {
  18643. super.enableEditing();
  18644. this.div.classList.toggle("disabled", false);
  18645. }
  18646. getBaseTranslation() {
  18647. return [0, 0];
  18648. }
  18649. get isResizable() {
  18650. return true;
  18651. }
  18652. onceAdded(focus) {
  18653. if (!this.annotationElementId) {
  18654. this.parent.addUndoableEditor(this);
  18655. }
  18656. this._isDraggable = true;
  18657. if (this.#mustBeCommitted) {
  18658. this.#mustBeCommitted = false;
  18659. this.commit();
  18660. this.parent.setSelected(this);
  18661. if (focus && this.isOnScreen) {
  18662. this.div.focus();
  18663. }
  18664. }
  18665. }
  18666. remove() {
  18667. this.#cleanDrawLayer();
  18668. super.remove();
  18669. }
  18670. rebuild() {
  18671. if (!this.parent) {
  18672. return;
  18673. }
  18674. super.rebuild();
  18675. if (this.div === null) {
  18676. return;
  18677. }
  18678. this.#addToDrawLayer();
  18679. this.#updateBbox(this.#drawOutlines.box);
  18680. if (!this.isAttachedToDOM) {
  18681. this.parent.add(this);
  18682. }
  18683. }
  18684. setParent(parent) {
  18685. let mustBeSelected = false;
  18686. if (this.parent && !parent) {
  18687. this._uiManager.removeShouldRescale(this);
  18688. this.#cleanDrawLayer();
  18689. } else if (parent) {
  18690. this._uiManager.addShouldRescale(this);
  18691. this.#addToDrawLayer(parent);
  18692. mustBeSelected = !this.parent && this.div?.classList.contains("selectedEditor");
  18693. }
  18694. super.setParent(parent);
  18695. if (mustBeSelected) {
  18696. this.select();
  18697. }
  18698. }
  18699. #cleanDrawLayer() {
  18700. if (this._drawId === null || !this.parent) {
  18701. return;
  18702. }
  18703. this.parent.drawLayer.remove(this._drawId);
  18704. this._drawId = null;
  18705. this._drawingOptions.reset();
  18706. }
  18707. #addToDrawLayer(parent = this.parent) {
  18708. if (this._drawId !== null && this.parent === parent) {
  18709. return;
  18710. }
  18711. if (this._drawId !== null) {
  18712. this.parent.drawLayer.updateParent(this._drawId, parent.drawLayer);
  18713. return;
  18714. }
  18715. this._drawingOptions.updateAll();
  18716. this._drawId = this.#createDrawing(this.#drawOutlines, parent);
  18717. }
  18718. #convertToParentSpace([x, y, width, height]) {
  18719. const {
  18720. parentDimensions: [pW, pH],
  18721. rotation
  18722. } = this;
  18723. switch (rotation) {
  18724. case 90:
  18725. return [y, 1 - x, width * (pH / pW), height * (pW / pH)];
  18726. case 180:
  18727. return [1 - x, 1 - y, width, height];
  18728. case 270:
  18729. return [1 - y, x, width * (pH / pW), height * (pW / pH)];
  18730. default:
  18731. return [x, y, width, height];
  18732. }
  18733. }
  18734. #convertToDrawSpace() {
  18735. const {
  18736. x,
  18737. y,
  18738. width,
  18739. height,
  18740. parentDimensions: [pW, pH],
  18741. rotation
  18742. } = this;
  18743. switch (rotation) {
  18744. case 90:
  18745. return [1 - y, x, width * (pW / pH), height * (pH / pW)];
  18746. case 180:
  18747. return [1 - x, 1 - y, width, height];
  18748. case 270:
  18749. return [y, 1 - x, width * (pW / pH), height * (pH / pW)];
  18750. default:
  18751. return [x, y, width, height];
  18752. }
  18753. }
  18754. #updateBbox(bbox) {
  18755. [this.x, this.y, this.width, this.height] = this.#convertToParentSpace(bbox);
  18756. if (this.div) {
  18757. this.fixAndSetPosition();
  18758. const [parentWidth, parentHeight] = this.parentDimensions;
  18759. this.setDims(this.width * parentWidth, this.height * parentHeight);
  18760. }
  18761. this._onResized();
  18762. }
  18763. #rotateBox() {
  18764. const {
  18765. x,
  18766. y,
  18767. width,
  18768. height,
  18769. rotation,
  18770. parentRotation,
  18771. parentDimensions: [pW, pH]
  18772. } = this;
  18773. switch ((rotation * 4 + parentRotation) / 90) {
  18774. case 1:
  18775. return [1 - y - height, x, height, width];
  18776. case 2:
  18777. return [1 - x - width, 1 - y - height, width, height];
  18778. case 3:
  18779. return [y, 1 - x - width, height, width];
  18780. case 4:
  18781. return [x, y - width * (pW / pH), height * (pH / pW), width * (pW / pH)];
  18782. case 5:
  18783. return [1 - y, x, width * (pW / pH), height * (pH / pW)];
  18784. case 6:
  18785. return [1 - x - height * (pH / pW), 1 - y, height * (pH / pW), width * (pW / pH)];
  18786. case 7:
  18787. return [y - width * (pW / pH), 1 - x - height * (pH / pW), width * (pW / pH), height * (pH / pW)];
  18788. case 8:
  18789. return [x - width, y - height, width, height];
  18790. case 9:
  18791. return [1 - y, x - width, height, width];
  18792. case 10:
  18793. return [1 - x, 1 - y, width, height];
  18794. case 11:
  18795. return [y - height, 1 - x, height, width];
  18796. case 12:
  18797. return [x - height * (pH / pW), y, height * (pH / pW), width * (pW / pH)];
  18798. case 13:
  18799. return [1 - y - width * (pW / pH), x - height * (pH / pW), width * (pW / pH), height * (pH / pW)];
  18800. case 14:
  18801. return [1 - x, 1 - y - width * (pW / pH), height * (pH / pW), width * (pW / pH)];
  18802. case 15:
  18803. return [y, 1 - x, width * (pW / pH), height * (pH / pW)];
  18804. default:
  18805. return [x, y, width, height];
  18806. }
  18807. }
  18808. rotate() {
  18809. if (!this.parent) {
  18810. return;
  18811. }
  18812. this.parent.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties({
  18813. bbox: this.#rotateBox()
  18814. }, this.#drawOutlines.updateRotation((this.parentRotation - this.rotation + 360) % 360)));
  18815. }
  18816. onScaleChanging() {
  18817. if (!this.parent) {
  18818. return;
  18819. }
  18820. this.#updateBbox(this.#drawOutlines.updateParentDimensions(this.parentDimensions, this.parent.scale));
  18821. }
  18822. static onScaleChangingWhenDrawing() {}
  18823. render() {
  18824. if (this.div) {
  18825. return this.div;
  18826. }
  18827. let baseX, baseY;
  18828. if (this._isCopy) {
  18829. baseX = this.x;
  18830. baseY = this.y;
  18831. }
  18832. const div = super.render();
  18833. div.classList.add("draw");
  18834. const drawDiv = document.createElement("div");
  18835. div.append(drawDiv);
  18836. drawDiv.setAttribute("aria-hidden", "true");
  18837. drawDiv.className = "internal";
  18838. const [parentWidth, parentHeight] = this.parentDimensions;
  18839. this.setDims(this.width * parentWidth, this.height * parentHeight);
  18840. this._uiManager.addShouldRescale(this);
  18841. this.disableEditing();
  18842. if (this._isCopy) {
  18843. this._moveAfterPaste(baseX, baseY);
  18844. }
  18845. return div;
  18846. }
  18847. static createDrawerInstance(_x, _y, _parentWidth, _parentHeight, _rotation) {
  18848. unreachable("Not implemented");
  18849. }
  18850. static startDrawing(parent, uiManager, _isLTR, event) {
  18851. const {
  18852. target,
  18853. offsetX: x,
  18854. offsetY: y,
  18855. pointerId,
  18856. pointerType
  18857. } = event;
  18858. if (DrawingEditor.#currentPointerType && DrawingEditor.#currentPointerType !== pointerType) {
  18859. return;
  18860. }
  18861. const {
  18862. viewport: {
  18863. rotation
  18864. }
  18865. } = parent;
  18866. const {
  18867. width: parentWidth,
  18868. height: parentHeight
  18869. } = target.getBoundingClientRect();
  18870. const ac = DrawingEditor.#currentDrawingAC = new AbortController();
  18871. const signal = parent.combinedSignal(ac);
  18872. DrawingEditor.#currentPointerId ||= pointerId;
  18873. DrawingEditor.#currentPointerType ??= pointerType;
  18874. window.addEventListener("pointerup", e => {
  18875. if (DrawingEditor.#currentPointerId === e.pointerId) {
  18876. this._endDraw(e);
  18877. } else {
  18878. DrawingEditor.#currentPointerIds?.delete(e.pointerId);
  18879. }
  18880. }, {
  18881. signal
  18882. });
  18883. window.addEventListener("pointercancel", e => {
  18884. if (DrawingEditor.#currentPointerId === e.pointerId) {
  18885. this._currentParent.endDrawingSession();
  18886. } else {
  18887. DrawingEditor.#currentPointerIds?.delete(e.pointerId);
  18888. }
  18889. }, {
  18890. signal
  18891. });
  18892. window.addEventListener("pointerdown", e => {
  18893. if (DrawingEditor.#currentPointerType !== e.pointerType) {
  18894. return;
  18895. }
  18896. (DrawingEditor.#currentPointerIds ||= new Set()).add(e.pointerId);
  18897. if (DrawingEditor.#currentDraw.isCancellable()) {
  18898. DrawingEditor.#currentDraw.removeLastElement();
  18899. if (DrawingEditor.#currentDraw.isEmpty()) {
  18900. this._currentParent.endDrawingSession(true);
  18901. } else {
  18902. this._endDraw(null);
  18903. }
  18904. }
  18905. }, {
  18906. capture: true,
  18907. passive: false,
  18908. signal
  18909. });
  18910. window.addEventListener("contextmenu", noContextMenu, {
  18911. signal
  18912. });
  18913. target.addEventListener("pointermove", this._drawMove.bind(this), {
  18914. signal
  18915. });
  18916. target.addEventListener("touchmove", e => {
  18917. if (e.timeStamp === DrawingEditor.#currentMoveTimestamp) {
  18918. stopEvent(e);
  18919. }
  18920. }, {
  18921. signal
  18922. });
  18923. parent.toggleDrawing();
  18924. uiManager._editorUndoBar?.hide();
  18925. if (DrawingEditor.#currentDraw) {
  18926. parent.drawLayer.updateProperties(this._currentDrawId, DrawingEditor.#currentDraw.startNew(x, y, parentWidth, parentHeight, rotation));
  18927. return;
  18928. }
  18929. uiManager.updateUIForDefaultProperties(this);
  18930. DrawingEditor.#currentDraw = this.createDrawerInstance(x, y, parentWidth, parentHeight, rotation);
  18931. DrawingEditor.#currentDrawingOptions = this.getDefaultDrawingOptions();
  18932. this._currentParent = parent;
  18933. ({
  18934. id: this._currentDrawId
  18935. } = parent.drawLayer.draw(this._mergeSVGProperties(DrawingEditor.#currentDrawingOptions.toSVGProperties(), DrawingEditor.#currentDraw.defaultSVGProperties), true, false));
  18936. }
  18937. static _drawMove(event) {
  18938. DrawingEditor.#currentMoveTimestamp = -1;
  18939. if (!DrawingEditor.#currentDraw) {
  18940. return;
  18941. }
  18942. const {
  18943. offsetX,
  18944. offsetY,
  18945. pointerId
  18946. } = event;
  18947. if (DrawingEditor.#currentPointerId !== pointerId) {
  18948. return;
  18949. }
  18950. if (DrawingEditor.#currentPointerIds?.size >= 1) {
  18951. this._endDraw(event);
  18952. return;
  18953. }
  18954. this._currentParent.drawLayer.updateProperties(this._currentDrawId, DrawingEditor.#currentDraw.add(offsetX, offsetY));
  18955. DrawingEditor.#currentMoveTimestamp = event.timeStamp;
  18956. stopEvent(event);
  18957. }
  18958. static _cleanup(all) {
  18959. if (all) {
  18960. this._currentDrawId = -1;
  18961. this._currentParent = null;
  18962. DrawingEditor.#currentDraw = null;
  18963. DrawingEditor.#currentDrawingOptions = null;
  18964. DrawingEditor.#currentPointerType = null;
  18965. DrawingEditor.#currentMoveTimestamp = NaN;
  18966. }
  18967. if (DrawingEditor.#currentDrawingAC) {
  18968. DrawingEditor.#currentDrawingAC.abort();
  18969. DrawingEditor.#currentDrawingAC = null;
  18970. DrawingEditor.#currentPointerId = NaN;
  18971. DrawingEditor.#currentPointerIds = null;
  18972. }
  18973. }
  18974. static _endDraw(event) {
  18975. const parent = this._currentParent;
  18976. if (!parent) {
  18977. return;
  18978. }
  18979. parent.toggleDrawing(true);
  18980. this._cleanup(false);
  18981. if (event?.target === parent.div) {
  18982. parent.drawLayer.updateProperties(this._currentDrawId, DrawingEditor.#currentDraw.end(event.offsetX, event.offsetY));
  18983. }
  18984. if (this.supportMultipleDrawings) {
  18985. const draw = DrawingEditor.#currentDraw;
  18986. const drawId = this._currentDrawId;
  18987. const lastElement = draw.getLastElement();
  18988. parent.addCommands({
  18989. cmd: () => {
  18990. parent.drawLayer.updateProperties(drawId, draw.setLastElement(lastElement));
  18991. },
  18992. undo: () => {
  18993. parent.drawLayer.updateProperties(drawId, draw.removeLastElement());
  18994. },
  18995. mustExec: false,
  18996. type: AnnotationEditorParamsType.DRAW_STEP
  18997. });
  18998. return;
  18999. }
  19000. this.endDrawing(false);
  19001. }
  19002. static endDrawing(isAborted) {
  19003. const parent = this._currentParent;
  19004. if (!parent) {
  19005. return null;
  19006. }
  19007. parent.toggleDrawing(true);
  19008. parent.cleanUndoStack(AnnotationEditorParamsType.DRAW_STEP);
  19009. if (!DrawingEditor.#currentDraw.isEmpty()) {
  19010. const {
  19011. pageDimensions: [pageWidth, pageHeight],
  19012. scale
  19013. } = parent;
  19014. const editor = parent.createAndAddNewEditor({
  19015. offsetX: 0,
  19016. offsetY: 0
  19017. }, false, {
  19018. drawId: this._currentDrawId,
  19019. drawOutlines: DrawingEditor.#currentDraw.getOutlines(pageWidth * scale, pageHeight * scale, scale, this._INNER_MARGIN),
  19020. drawingOptions: DrawingEditor.#currentDrawingOptions,
  19021. mustBeCommitted: !isAborted
  19022. });
  19023. this._cleanup(true);
  19024. return editor;
  19025. }
  19026. parent.drawLayer.remove(this._currentDrawId);
  19027. this._cleanup(true);
  19028. return null;
  19029. }
  19030. createDrawingOptions(_data) {}
  19031. static deserializeDraw(_pageX, _pageY, _pageWidth, _pageHeight, _innerWidth, _data) {
  19032. unreachable("Not implemented");
  19033. }
  19034. static async deserialize(data, parent, uiManager) {
  19035. const {
  19036. rawDims: {
  19037. pageWidth,
  19038. pageHeight,
  19039. pageX,
  19040. pageY
  19041. }
  19042. } = parent.viewport;
  19043. const drawOutlines = this.deserializeDraw(pageX, pageY, pageWidth, pageHeight, this._INNER_MARGIN, data);
  19044. const editor = await super.deserialize(data, parent, uiManager);
  19045. editor.createDrawingOptions(data);
  19046. editor.#createDrawOutlines({
  19047. drawOutlines
  19048. });
  19049. editor.#addToDrawLayer();
  19050. editor.onScaleChanging();
  19051. editor.rotate();
  19052. return editor;
  19053. }
  19054. serializeDraw(isForCopying) {
  19055. const [pageX, pageY] = this.pageTranslation;
  19056. const [pageWidth, pageHeight] = this.pageDimensions;
  19057. return this.#drawOutlines.serialize([pageX, pageY, pageWidth, pageHeight], isForCopying);
  19058. }
  19059. renderAnnotationElement(annotation) {
  19060. annotation.updateEdited({
  19061. rect: this.getRect(0, 0)
  19062. });
  19063. return null;
  19064. }
  19065. static canCreateNewEmptyEditor() {
  19066. return false;
  19067. }
  19068. }
  19069. ;// ./src/display/editor/drawers/inkdraw.js
  19070. class InkDrawOutliner {
  19071. #last = new Float64Array(6);
  19072. #line;
  19073. #lines;
  19074. #rotation;
  19075. #thickness;
  19076. #points;
  19077. #lastSVGPath = "";
  19078. #lastIndex = 0;
  19079. #outlines = new InkDrawOutline();
  19080. #parentWidth;
  19081. #parentHeight;
  19082. constructor(x, y, parentWidth, parentHeight, rotation, thickness) {
  19083. this.#parentWidth = parentWidth;
  19084. this.#parentHeight = parentHeight;
  19085. this.#rotation = rotation;
  19086. this.#thickness = thickness;
  19087. [x, y] = this.#normalizePoint(x, y);
  19088. const line = this.#line = [NaN, NaN, NaN, NaN, x, y];
  19089. this.#points = [x, y];
  19090. this.#lines = [{
  19091. line,
  19092. points: this.#points
  19093. }];
  19094. this.#last.set(line, 0);
  19095. }
  19096. updateProperty(name, value) {
  19097. if (name === "stroke-width") {
  19098. this.#thickness = value;
  19099. }
  19100. }
  19101. #normalizePoint(x, y) {
  19102. return Outline._normalizePoint(x, y, this.#parentWidth, this.#parentHeight, this.#rotation);
  19103. }
  19104. isEmpty() {
  19105. return !this.#lines || this.#lines.length === 0;
  19106. }
  19107. isCancellable() {
  19108. return this.#points.length <= 10;
  19109. }
  19110. add(x, y) {
  19111. [x, y] = this.#normalizePoint(x, y);
  19112. const [x1, y1, x2, y2] = this.#last.subarray(2, 6);
  19113. const diffX = x - x2;
  19114. const diffY = y - y2;
  19115. const d = Math.hypot(this.#parentWidth * diffX, this.#parentHeight * diffY);
  19116. if (d <= 2) {
  19117. return null;
  19118. }
  19119. this.#points.push(x, y);
  19120. if (isNaN(x1)) {
  19121. this.#last.set([x2, y2, x, y], 2);
  19122. this.#line.push(NaN, NaN, NaN, NaN, x, y);
  19123. return {
  19124. path: {
  19125. d: this.toSVGPath()
  19126. }
  19127. };
  19128. }
  19129. if (isNaN(this.#last[0])) {
  19130. this.#line.splice(6, 6);
  19131. }
  19132. this.#last.set([x1, y1, x2, y2, x, y], 0);
  19133. this.#line.push(...Outline.createBezierPoints(x1, y1, x2, y2, x, y));
  19134. return {
  19135. path: {
  19136. d: this.toSVGPath()
  19137. }
  19138. };
  19139. }
  19140. end(x, y) {
  19141. const change = this.add(x, y);
  19142. if (change) {
  19143. return change;
  19144. }
  19145. if (this.#points.length === 2) {
  19146. return {
  19147. path: {
  19148. d: this.toSVGPath()
  19149. }
  19150. };
  19151. }
  19152. return null;
  19153. }
  19154. startNew(x, y, parentWidth, parentHeight, rotation) {
  19155. this.#parentWidth = parentWidth;
  19156. this.#parentHeight = parentHeight;
  19157. this.#rotation = rotation;
  19158. [x, y] = this.#normalizePoint(x, y);
  19159. const line = this.#line = [NaN, NaN, NaN, NaN, x, y];
  19160. this.#points = [x, y];
  19161. const last = this.#lines.at(-1);
  19162. if (last) {
  19163. last.line = new Float32Array(last.line);
  19164. last.points = new Float32Array(last.points);
  19165. }
  19166. this.#lines.push({
  19167. line,
  19168. points: this.#points
  19169. });
  19170. this.#last.set(line, 0);
  19171. this.#lastIndex = 0;
  19172. this.toSVGPath();
  19173. return null;
  19174. }
  19175. getLastElement() {
  19176. return this.#lines.at(-1);
  19177. }
  19178. setLastElement(element) {
  19179. if (!this.#lines) {
  19180. return this.#outlines.setLastElement(element);
  19181. }
  19182. this.#lines.push(element);
  19183. this.#line = element.line;
  19184. this.#points = element.points;
  19185. this.#lastIndex = 0;
  19186. return {
  19187. path: {
  19188. d: this.toSVGPath()
  19189. }
  19190. };
  19191. }
  19192. removeLastElement() {
  19193. if (!this.#lines) {
  19194. return this.#outlines.removeLastElement();
  19195. }
  19196. this.#lines.pop();
  19197. this.#lastSVGPath = "";
  19198. for (let i = 0, ii = this.#lines.length; i < ii; i++) {
  19199. const {
  19200. line,
  19201. points
  19202. } = this.#lines[i];
  19203. this.#line = line;
  19204. this.#points = points;
  19205. this.#lastIndex = 0;
  19206. this.toSVGPath();
  19207. }
  19208. return {
  19209. path: {
  19210. d: this.#lastSVGPath
  19211. }
  19212. };
  19213. }
  19214. toSVGPath() {
  19215. const firstX = Outline.svgRound(this.#line[4]);
  19216. const firstY = Outline.svgRound(this.#line[5]);
  19217. if (this.#points.length === 2) {
  19218. this.#lastSVGPath = `${this.#lastSVGPath} M ${firstX} ${firstY} Z`;
  19219. return this.#lastSVGPath;
  19220. }
  19221. if (this.#points.length <= 6) {
  19222. const i = this.#lastSVGPath.lastIndexOf("M");
  19223. this.#lastSVGPath = `${this.#lastSVGPath.slice(0, i)} M ${firstX} ${firstY}`;
  19224. this.#lastIndex = 6;
  19225. }
  19226. if (this.#points.length === 4) {
  19227. const secondX = Outline.svgRound(this.#line[10]);
  19228. const secondY = Outline.svgRound(this.#line[11]);
  19229. this.#lastSVGPath = `${this.#lastSVGPath} L ${secondX} ${secondY}`;
  19230. this.#lastIndex = 12;
  19231. return this.#lastSVGPath;
  19232. }
  19233. const buffer = [];
  19234. if (this.#lastIndex === 0) {
  19235. buffer.push(`M ${firstX} ${firstY}`);
  19236. this.#lastIndex = 6;
  19237. }
  19238. for (let i = this.#lastIndex, ii = this.#line.length; i < ii; i += 6) {
  19239. const [c1x, c1y, c2x, c2y, x, y] = this.#line.slice(i, i + 6).map(Outline.svgRound);
  19240. buffer.push(`C${c1x} ${c1y} ${c2x} ${c2y} ${x} ${y}`);
  19241. }
  19242. this.#lastSVGPath += buffer.join(" ");
  19243. this.#lastIndex = this.#line.length;
  19244. return this.#lastSVGPath;
  19245. }
  19246. getOutlines(parentWidth, parentHeight, scale, innerMargin) {
  19247. const last = this.#lines.at(-1);
  19248. last.line = new Float32Array(last.line);
  19249. last.points = new Float32Array(last.points);
  19250. this.#outlines.build(this.#lines, parentWidth, parentHeight, scale, this.#rotation, this.#thickness, innerMargin);
  19251. this.#last = null;
  19252. this.#line = null;
  19253. this.#lines = null;
  19254. this.#lastSVGPath = null;
  19255. return this.#outlines;
  19256. }
  19257. get defaultSVGProperties() {
  19258. return {
  19259. root: {
  19260. viewBox: "0 0 10000 10000"
  19261. },
  19262. rootClass: {
  19263. draw: true
  19264. },
  19265. bbox: [0, 0, 1, 1]
  19266. };
  19267. }
  19268. }
  19269. class InkDrawOutline extends Outline {
  19270. #bbox;
  19271. #currentRotation = 0;
  19272. #innerMargin;
  19273. #lines;
  19274. #parentWidth;
  19275. #parentHeight;
  19276. #parentScale;
  19277. #rotation;
  19278. #thickness;
  19279. build(lines, parentWidth, parentHeight, parentScale, rotation, thickness, innerMargin) {
  19280. this.#parentWidth = parentWidth;
  19281. this.#parentHeight = parentHeight;
  19282. this.#parentScale = parentScale;
  19283. this.#rotation = rotation;
  19284. this.#thickness = thickness;
  19285. this.#innerMargin = innerMargin ?? 0;
  19286. this.#lines = lines;
  19287. this.#computeBbox();
  19288. }
  19289. get thickness() {
  19290. return this.#thickness;
  19291. }
  19292. setLastElement(element) {
  19293. this.#lines.push(element);
  19294. return {
  19295. path: {
  19296. d: this.toSVGPath()
  19297. }
  19298. };
  19299. }
  19300. removeLastElement() {
  19301. this.#lines.pop();
  19302. return {
  19303. path: {
  19304. d: this.toSVGPath()
  19305. }
  19306. };
  19307. }
  19308. toSVGPath() {
  19309. const buffer = [];
  19310. for (const {
  19311. line
  19312. } of this.#lines) {
  19313. buffer.push(`M${Outline.svgRound(line[4])} ${Outline.svgRound(line[5])}`);
  19314. if (line.length === 6) {
  19315. buffer.push("Z");
  19316. continue;
  19317. }
  19318. if (line.length === 12 && isNaN(line[6])) {
  19319. buffer.push(`L${Outline.svgRound(line[10])} ${Outline.svgRound(line[11])}`);
  19320. continue;
  19321. }
  19322. for (let i = 6, ii = line.length; i < ii; i += 6) {
  19323. const [c1x, c1y, c2x, c2y, x, y] = line.subarray(i, i + 6).map(Outline.svgRound);
  19324. buffer.push(`C${c1x} ${c1y} ${c2x} ${c2y} ${x} ${y}`);
  19325. }
  19326. }
  19327. return buffer.join("");
  19328. }
  19329. serialize([pageX, pageY, pageWidth, pageHeight], isForCopying) {
  19330. const serializedLines = [];
  19331. const serializedPoints = [];
  19332. const [x, y, width, height] = this.#getBBoxWithNoMargin();
  19333. let tx, ty, sx, sy, x1, y1, x2, y2, rescaleFn;
  19334. switch (this.#rotation) {
  19335. case 0:
  19336. rescaleFn = Outline._rescale;
  19337. tx = pageX;
  19338. ty = pageY + pageHeight;
  19339. sx = pageWidth;
  19340. sy = -pageHeight;
  19341. x1 = pageX + x * pageWidth;
  19342. y1 = pageY + (1 - y - height) * pageHeight;
  19343. x2 = pageX + (x + width) * pageWidth;
  19344. y2 = pageY + (1 - y) * pageHeight;
  19345. break;
  19346. case 90:
  19347. rescaleFn = Outline._rescaleAndSwap;
  19348. tx = pageX;
  19349. ty = pageY;
  19350. sx = pageWidth;
  19351. sy = pageHeight;
  19352. x1 = pageX + y * pageWidth;
  19353. y1 = pageY + x * pageHeight;
  19354. x2 = pageX + (y + height) * pageWidth;
  19355. y2 = pageY + (x + width) * pageHeight;
  19356. break;
  19357. case 180:
  19358. rescaleFn = Outline._rescale;
  19359. tx = pageX + pageWidth;
  19360. ty = pageY;
  19361. sx = -pageWidth;
  19362. sy = pageHeight;
  19363. x1 = pageX + (1 - x - width) * pageWidth;
  19364. y1 = pageY + y * pageHeight;
  19365. x2 = pageX + (1 - x) * pageWidth;
  19366. y2 = pageY + (y + height) * pageHeight;
  19367. break;
  19368. case 270:
  19369. rescaleFn = Outline._rescaleAndSwap;
  19370. tx = pageX + pageWidth;
  19371. ty = pageY + pageHeight;
  19372. sx = -pageWidth;
  19373. sy = -pageHeight;
  19374. x1 = pageX + (1 - y - height) * pageWidth;
  19375. y1 = pageY + (1 - x - width) * pageHeight;
  19376. x2 = pageX + (1 - y) * pageWidth;
  19377. y2 = pageY + (1 - x) * pageHeight;
  19378. break;
  19379. }
  19380. for (const {
  19381. line,
  19382. points
  19383. } of this.#lines) {
  19384. serializedLines.push(rescaleFn(line, tx, ty, sx, sy, isForCopying ? new Array(line.length) : null));
  19385. serializedPoints.push(rescaleFn(points, tx, ty, sx, sy, isForCopying ? new Array(points.length) : null));
  19386. }
  19387. return {
  19388. lines: serializedLines,
  19389. points: serializedPoints,
  19390. rect: [x1, y1, x2, y2]
  19391. };
  19392. }
  19393. static deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, {
  19394. paths: {
  19395. lines,
  19396. points
  19397. },
  19398. rotation,
  19399. thickness
  19400. }) {
  19401. const newLines = [];
  19402. let tx, ty, sx, sy, rescaleFn;
  19403. switch (rotation) {
  19404. case 0:
  19405. rescaleFn = Outline._rescale;
  19406. tx = -pageX / pageWidth;
  19407. ty = pageY / pageHeight + 1;
  19408. sx = 1 / pageWidth;
  19409. sy = -1 / pageHeight;
  19410. break;
  19411. case 90:
  19412. rescaleFn = Outline._rescaleAndSwap;
  19413. tx = -pageY / pageHeight;
  19414. ty = -pageX / pageWidth;
  19415. sx = 1 / pageHeight;
  19416. sy = 1 / pageWidth;
  19417. break;
  19418. case 180:
  19419. rescaleFn = Outline._rescale;
  19420. tx = pageX / pageWidth + 1;
  19421. ty = -pageY / pageHeight;
  19422. sx = -1 / pageWidth;
  19423. sy = 1 / pageHeight;
  19424. break;
  19425. case 270:
  19426. rescaleFn = Outline._rescaleAndSwap;
  19427. tx = pageY / pageHeight + 1;
  19428. ty = pageX / pageWidth + 1;
  19429. sx = -1 / pageHeight;
  19430. sy = -1 / pageWidth;
  19431. break;
  19432. }
  19433. if (!lines) {
  19434. lines = [];
  19435. for (const point of points) {
  19436. const len = point.length;
  19437. if (len === 2) {
  19438. lines.push(new Float32Array([NaN, NaN, NaN, NaN, point[0], point[1]]));
  19439. continue;
  19440. }
  19441. if (len === 4) {
  19442. lines.push(new Float32Array([NaN, NaN, NaN, NaN, point[0], point[1], NaN, NaN, NaN, NaN, point[2], point[3]]));
  19443. continue;
  19444. }
  19445. const line = new Float32Array(3 * (len - 2));
  19446. lines.push(line);
  19447. let [x1, y1, x2, y2] = point.subarray(0, 4);
  19448. line.set([NaN, NaN, NaN, NaN, x1, y1], 0);
  19449. for (let i = 4; i < len; i += 2) {
  19450. const x = point[i];
  19451. const y = point[i + 1];
  19452. line.set(Outline.createBezierPoints(x1, y1, x2, y2, x, y), (i - 2) * 3);
  19453. [x1, y1, x2, y2] = [x2, y2, x, y];
  19454. }
  19455. }
  19456. }
  19457. for (let i = 0, ii = lines.length; i < ii; i++) {
  19458. newLines.push({
  19459. line: rescaleFn(lines[i].map(x => x ?? NaN), tx, ty, sx, sy),
  19460. points: rescaleFn(points[i].map(x => x ?? NaN), tx, ty, sx, sy)
  19461. });
  19462. }
  19463. const outlines = new this.prototype.constructor();
  19464. outlines.build(newLines, pageWidth, pageHeight, 1, rotation, thickness, innerMargin);
  19465. return outlines;
  19466. }
  19467. #getMarginComponents(thickness = this.#thickness) {
  19468. const margin = this.#innerMargin + thickness / 2 * this.#parentScale;
  19469. return this.#rotation % 180 === 0 ? [margin / this.#parentWidth, margin / this.#parentHeight] : [margin / this.#parentHeight, margin / this.#parentWidth];
  19470. }
  19471. #getBBoxWithNoMargin() {
  19472. const [x, y, width, height] = this.#bbox;
  19473. const [marginX, marginY] = this.#getMarginComponents(0);
  19474. return [x + marginX, y + marginY, width - 2 * marginX, height - 2 * marginY];
  19475. }
  19476. #computeBbox() {
  19477. const bbox = this.#bbox = new Float32Array([Infinity, Infinity, -Infinity, -Infinity]);
  19478. for (const {
  19479. line
  19480. } of this.#lines) {
  19481. if (line.length <= 12) {
  19482. for (let i = 4, ii = line.length; i < ii; i += 6) {
  19483. Util.pointBoundingBox(line[i], line[i + 1], bbox);
  19484. }
  19485. continue;
  19486. }
  19487. let lastX = line[4],
  19488. lastY = line[5];
  19489. for (let i = 6, ii = line.length; i < ii; i += 6) {
  19490. const [c1x, c1y, c2x, c2y, x, y] = line.subarray(i, i + 6);
  19491. Util.bezierBoundingBox(lastX, lastY, c1x, c1y, c2x, c2y, x, y, bbox);
  19492. lastX = x;
  19493. lastY = y;
  19494. }
  19495. }
  19496. const [marginX, marginY] = this.#getMarginComponents();
  19497. bbox[0] = MathClamp(bbox[0] - marginX, 0, 1);
  19498. bbox[1] = MathClamp(bbox[1] - marginY, 0, 1);
  19499. bbox[2] = MathClamp(bbox[2] + marginX, 0, 1);
  19500. bbox[3] = MathClamp(bbox[3] + marginY, 0, 1);
  19501. bbox[2] -= bbox[0];
  19502. bbox[3] -= bbox[1];
  19503. }
  19504. get box() {
  19505. return this.#bbox;
  19506. }
  19507. updateProperty(name, value) {
  19508. if (name === "stroke-width") {
  19509. return this.#updateThickness(value);
  19510. }
  19511. return null;
  19512. }
  19513. #updateThickness(thickness) {
  19514. const [oldMarginX, oldMarginY] = this.#getMarginComponents();
  19515. this.#thickness = thickness;
  19516. const [newMarginX, newMarginY] = this.#getMarginComponents();
  19517. const [diffMarginX, diffMarginY] = [newMarginX - oldMarginX, newMarginY - oldMarginY];
  19518. const bbox = this.#bbox;
  19519. bbox[0] -= diffMarginX;
  19520. bbox[1] -= diffMarginY;
  19521. bbox[2] += 2 * diffMarginX;
  19522. bbox[3] += 2 * diffMarginY;
  19523. return bbox;
  19524. }
  19525. updateParentDimensions([width, height], scale) {
  19526. const [oldMarginX, oldMarginY] = this.#getMarginComponents();
  19527. this.#parentWidth = width;
  19528. this.#parentHeight = height;
  19529. this.#parentScale = scale;
  19530. const [newMarginX, newMarginY] = this.#getMarginComponents();
  19531. const diffMarginX = newMarginX - oldMarginX;
  19532. const diffMarginY = newMarginY - oldMarginY;
  19533. const bbox = this.#bbox;
  19534. bbox[0] -= diffMarginX;
  19535. bbox[1] -= diffMarginY;
  19536. bbox[2] += 2 * diffMarginX;
  19537. bbox[3] += 2 * diffMarginY;
  19538. return bbox;
  19539. }
  19540. updateRotation(rotation) {
  19541. this.#currentRotation = rotation;
  19542. return {
  19543. path: {
  19544. transform: this.rotationTransform
  19545. }
  19546. };
  19547. }
  19548. get viewBox() {
  19549. return this.#bbox.map(Outline.svgRound).join(" ");
  19550. }
  19551. get defaultProperties() {
  19552. const [x, y] = this.#bbox;
  19553. return {
  19554. root: {
  19555. viewBox: this.viewBox
  19556. },
  19557. path: {
  19558. "transform-origin": `${Outline.svgRound(x)} ${Outline.svgRound(y)}`
  19559. }
  19560. };
  19561. }
  19562. get rotationTransform() {
  19563. const [,, width, height] = this.#bbox;
  19564. let a = 0,
  19565. b = 0,
  19566. c = 0,
  19567. d = 0,
  19568. e = 0,
  19569. f = 0;
  19570. switch (this.#currentRotation) {
  19571. case 90:
  19572. b = height / width;
  19573. c = -width / height;
  19574. e = width;
  19575. break;
  19576. case 180:
  19577. a = -1;
  19578. d = -1;
  19579. e = width;
  19580. f = height;
  19581. break;
  19582. case 270:
  19583. b = -height / width;
  19584. c = width / height;
  19585. f = height;
  19586. break;
  19587. default:
  19588. return "";
  19589. }
  19590. return `matrix(${a} ${b} ${c} ${d} ${Outline.svgRound(e)} ${Outline.svgRound(f)})`;
  19591. }
  19592. getPathResizingSVGProperties([newX, newY, newWidth, newHeight]) {
  19593. const [marginX, marginY] = this.#getMarginComponents();
  19594. const [x, y, width, height] = this.#bbox;
  19595. if (Math.abs(width - marginX) <= Outline.PRECISION || Math.abs(height - marginY) <= Outline.PRECISION) {
  19596. const tx = newX + newWidth / 2 - (x + width / 2);
  19597. const ty = newY + newHeight / 2 - (y + height / 2);
  19598. return {
  19599. path: {
  19600. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`,
  19601. transform: `${this.rotationTransform} translate(${tx} ${ty})`
  19602. }
  19603. };
  19604. }
  19605. const s1x = (newWidth - 2 * marginX) / (width - 2 * marginX);
  19606. const s1y = (newHeight - 2 * marginY) / (height - 2 * marginY);
  19607. const s2x = width / newWidth;
  19608. const s2y = height / newHeight;
  19609. return {
  19610. path: {
  19611. "transform-origin": `${Outline.svgRound(x)} ${Outline.svgRound(y)}`,
  19612. transform: `${this.rotationTransform} scale(${s2x} ${s2y}) ` + `translate(${Outline.svgRound(marginX)} ${Outline.svgRound(marginY)}) scale(${s1x} ${s1y}) ` + `translate(${Outline.svgRound(-marginX)} ${Outline.svgRound(-marginY)})`
  19613. }
  19614. };
  19615. }
  19616. getPathResizedSVGProperties([newX, newY, newWidth, newHeight]) {
  19617. const [marginX, marginY] = this.#getMarginComponents();
  19618. const bbox = this.#bbox;
  19619. const [x, y, width, height] = bbox;
  19620. bbox[0] = newX;
  19621. bbox[1] = newY;
  19622. bbox[2] = newWidth;
  19623. bbox[3] = newHeight;
  19624. if (Math.abs(width - marginX) <= Outline.PRECISION || Math.abs(height - marginY) <= Outline.PRECISION) {
  19625. const tx = newX + newWidth / 2 - (x + width / 2);
  19626. const ty = newY + newHeight / 2 - (y + height / 2);
  19627. for (const {
  19628. line,
  19629. points
  19630. } of this.#lines) {
  19631. Outline._translate(line, tx, ty, line);
  19632. Outline._translate(points, tx, ty, points);
  19633. }
  19634. return {
  19635. root: {
  19636. viewBox: this.viewBox
  19637. },
  19638. path: {
  19639. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`,
  19640. transform: this.rotationTransform || null,
  19641. d: this.toSVGPath()
  19642. }
  19643. };
  19644. }
  19645. const s1x = (newWidth - 2 * marginX) / (width - 2 * marginX);
  19646. const s1y = (newHeight - 2 * marginY) / (height - 2 * marginY);
  19647. const tx = -s1x * (x + marginX) + newX + marginX;
  19648. const ty = -s1y * (y + marginY) + newY + marginY;
  19649. if (s1x !== 1 || s1y !== 1 || tx !== 0 || ty !== 0) {
  19650. for (const {
  19651. line,
  19652. points
  19653. } of this.#lines) {
  19654. Outline._rescale(line, tx, ty, s1x, s1y, line);
  19655. Outline._rescale(points, tx, ty, s1x, s1y, points);
  19656. }
  19657. }
  19658. return {
  19659. root: {
  19660. viewBox: this.viewBox
  19661. },
  19662. path: {
  19663. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`,
  19664. transform: this.rotationTransform || null,
  19665. d: this.toSVGPath()
  19666. }
  19667. };
  19668. }
  19669. getPathTranslatedSVGProperties([newX, newY], parentDimensions) {
  19670. const [newParentWidth, newParentHeight] = parentDimensions;
  19671. const bbox = this.#bbox;
  19672. const tx = newX - bbox[0];
  19673. const ty = newY - bbox[1];
  19674. if (this.#parentWidth === newParentWidth && this.#parentHeight === newParentHeight) {
  19675. for (const {
  19676. line,
  19677. points
  19678. } of this.#lines) {
  19679. Outline._translate(line, tx, ty, line);
  19680. Outline._translate(points, tx, ty, points);
  19681. }
  19682. } else {
  19683. const sx = this.#parentWidth / newParentWidth;
  19684. const sy = this.#parentHeight / newParentHeight;
  19685. this.#parentWidth = newParentWidth;
  19686. this.#parentHeight = newParentHeight;
  19687. for (const {
  19688. line,
  19689. points
  19690. } of this.#lines) {
  19691. Outline._rescale(line, tx, ty, sx, sy, line);
  19692. Outline._rescale(points, tx, ty, sx, sy, points);
  19693. }
  19694. bbox[2] *= sx;
  19695. bbox[3] *= sy;
  19696. }
  19697. bbox[0] = newX;
  19698. bbox[1] = newY;
  19699. return {
  19700. root: {
  19701. viewBox: this.viewBox
  19702. },
  19703. path: {
  19704. d: this.toSVGPath(),
  19705. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`
  19706. }
  19707. };
  19708. }
  19709. get defaultSVGProperties() {
  19710. const bbox = this.#bbox;
  19711. return {
  19712. root: {
  19713. viewBox: this.viewBox
  19714. },
  19715. rootClass: {
  19716. draw: true
  19717. },
  19718. path: {
  19719. d: this.toSVGPath(),
  19720. "transform-origin": `${Outline.svgRound(bbox[0])} ${Outline.svgRound(bbox[1])}`,
  19721. transform: this.rotationTransform || null
  19722. },
  19723. bbox
  19724. };
  19725. }
  19726. }
  19727. ;// ./src/display/editor/ink.js
  19728. class InkDrawingOptions extends DrawingOptions {
  19729. constructor(viewerParameters) {
  19730. super();
  19731. this._viewParameters = viewerParameters;
  19732. super.updateProperties({
  19733. fill: "none",
  19734. stroke: AnnotationEditor._defaultLineColor,
  19735. "stroke-opacity": 1,
  19736. "stroke-width": 1,
  19737. "stroke-linecap": "round",
  19738. "stroke-linejoin": "round",
  19739. "stroke-miterlimit": 10
  19740. });
  19741. }
  19742. updateSVGProperty(name, value) {
  19743. if (name === "stroke-width") {
  19744. value ??= this["stroke-width"];
  19745. value *= this._viewParameters.realScale;
  19746. }
  19747. super.updateSVGProperty(name, value);
  19748. }
  19749. clone() {
  19750. const clone = new InkDrawingOptions(this._viewParameters);
  19751. clone.updateAll(this);
  19752. return clone;
  19753. }
  19754. }
  19755. class InkEditor extends DrawingEditor {
  19756. static _type = "ink";
  19757. static _editorType = AnnotationEditorType.INK;
  19758. static _defaultDrawingOptions = null;
  19759. constructor(params) {
  19760. super({
  19761. ...params,
  19762. name: "inkEditor"
  19763. });
  19764. this._willKeepAspectRatio = true;
  19765. this.defaultL10nId = "pdfjs-editor-ink-editor";
  19766. }
  19767. static initialize(l10n, uiManager) {
  19768. AnnotationEditor.initialize(l10n, uiManager);
  19769. this._defaultDrawingOptions = new InkDrawingOptions(uiManager.viewParameters);
  19770. }
  19771. static getDefaultDrawingOptions(options) {
  19772. const clone = this._defaultDrawingOptions.clone();
  19773. clone.updateProperties(options);
  19774. return clone;
  19775. }
  19776. static get supportMultipleDrawings() {
  19777. return true;
  19778. }
  19779. static get typesMap() {
  19780. return shadow(this, "typesMap", new Map([[AnnotationEditorParamsType.INK_THICKNESS, "stroke-width"], [AnnotationEditorParamsType.INK_COLOR, "stroke"], [AnnotationEditorParamsType.INK_OPACITY, "stroke-opacity"]]));
  19781. }
  19782. static createDrawerInstance(x, y, parentWidth, parentHeight, rotation) {
  19783. return new InkDrawOutliner(x, y, parentWidth, parentHeight, rotation, this._defaultDrawingOptions["stroke-width"]);
  19784. }
  19785. static deserializeDraw(pageX, pageY, pageWidth, pageHeight, innerMargin, data) {
  19786. return InkDrawOutline.deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, data);
  19787. }
  19788. static async deserialize(data, parent, uiManager) {
  19789. let initialData = null;
  19790. if (data instanceof InkAnnotationElement) {
  19791. const {
  19792. data: {
  19793. inkLists,
  19794. rect,
  19795. rotation,
  19796. id,
  19797. color,
  19798. opacity,
  19799. borderStyle: {
  19800. rawWidth: thickness
  19801. },
  19802. popupRef
  19803. },
  19804. parent: {
  19805. page: {
  19806. pageNumber
  19807. }
  19808. }
  19809. } = data;
  19810. initialData = data = {
  19811. annotationType: AnnotationEditorType.INK,
  19812. color: Array.from(color),
  19813. thickness,
  19814. opacity,
  19815. paths: {
  19816. points: inkLists
  19817. },
  19818. boxes: null,
  19819. pageIndex: pageNumber - 1,
  19820. rect: rect.slice(0),
  19821. rotation,
  19822. id,
  19823. deleted: false,
  19824. popupRef
  19825. };
  19826. }
  19827. const editor = await super.deserialize(data, parent, uiManager);
  19828. editor.annotationElementId = data.id || null;
  19829. editor._initialData = initialData;
  19830. return editor;
  19831. }
  19832. onScaleChanging() {
  19833. if (!this.parent) {
  19834. return;
  19835. }
  19836. super.onScaleChanging();
  19837. const {
  19838. _drawId,
  19839. _drawingOptions,
  19840. parent
  19841. } = this;
  19842. _drawingOptions.updateSVGProperty("stroke-width");
  19843. parent.drawLayer.updateProperties(_drawId, _drawingOptions.toSVGProperties());
  19844. }
  19845. static onScaleChangingWhenDrawing() {
  19846. const parent = this._currentParent;
  19847. if (!parent) {
  19848. return;
  19849. }
  19850. super.onScaleChangingWhenDrawing();
  19851. this._defaultDrawingOptions.updateSVGProperty("stroke-width");
  19852. parent.drawLayer.updateProperties(this._currentDrawId, this._defaultDrawingOptions.toSVGProperties());
  19853. }
  19854. createDrawingOptions({
  19855. color,
  19856. thickness,
  19857. opacity
  19858. }) {
  19859. this._drawingOptions = InkEditor.getDefaultDrawingOptions({
  19860. stroke: Util.makeHexColor(...color),
  19861. "stroke-width": thickness,
  19862. "stroke-opacity": opacity
  19863. });
  19864. }
  19865. serialize(isForCopying = false) {
  19866. if (this.isEmpty()) {
  19867. return null;
  19868. }
  19869. if (this.deleted) {
  19870. return this.serializeDeleted();
  19871. }
  19872. const {
  19873. lines,
  19874. points,
  19875. rect
  19876. } = this.serializeDraw(isForCopying);
  19877. const {
  19878. _drawingOptions: {
  19879. stroke,
  19880. "stroke-opacity": opacity,
  19881. "stroke-width": thickness
  19882. }
  19883. } = this;
  19884. const serialized = {
  19885. annotationType: AnnotationEditorType.INK,
  19886. color: AnnotationEditor._colorManager.convert(stroke),
  19887. opacity,
  19888. thickness,
  19889. paths: {
  19890. lines,
  19891. points
  19892. },
  19893. pageIndex: this.pageIndex,
  19894. rect,
  19895. rotation: this.rotation,
  19896. structTreeParentId: this._structTreeParentId
  19897. };
  19898. if (isForCopying) {
  19899. serialized.isCopy = true;
  19900. return serialized;
  19901. }
  19902. if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
  19903. return null;
  19904. }
  19905. serialized.id = this.annotationElementId;
  19906. return serialized;
  19907. }
  19908. #hasElementChanged(serialized) {
  19909. const {
  19910. color,
  19911. thickness,
  19912. opacity,
  19913. pageIndex
  19914. } = this._initialData;
  19915. return this._hasBeenMoved || this._hasBeenResized || serialized.color.some((c, i) => c !== color[i]) || serialized.thickness !== thickness || serialized.opacity !== opacity || serialized.pageIndex !== pageIndex;
  19916. }
  19917. renderAnnotationElement(annotation) {
  19918. const {
  19919. points,
  19920. rect
  19921. } = this.serializeDraw(false);
  19922. annotation.updateEdited({
  19923. rect,
  19924. thickness: this._drawingOptions["stroke-width"],
  19925. points
  19926. });
  19927. return null;
  19928. }
  19929. }
  19930. ;// ./src/display/editor/drawers/contour.js
  19931. class ContourDrawOutline extends InkDrawOutline {
  19932. toSVGPath() {
  19933. let path = super.toSVGPath();
  19934. if (!path.endsWith("Z")) {
  19935. path += "Z";
  19936. }
  19937. return path;
  19938. }
  19939. }
  19940. ;// ./src/display/editor/drawers/signaturedraw.js
  19941. const BASE_HEADER_LENGTH = 8;
  19942. const POINTS_PROPERTIES_NUMBER = 3;
  19943. class SignatureExtractor {
  19944. static #PARAMETERS = {
  19945. maxDim: 512,
  19946. sigmaSFactor: 0.02,
  19947. sigmaR: 25,
  19948. kernelSize: 16
  19949. };
  19950. static #neighborIndexToId(i0, j0, i, j) {
  19951. i -= i0;
  19952. j -= j0;
  19953. if (i === 0) {
  19954. return j > 0 ? 0 : 4;
  19955. }
  19956. if (i === 1) {
  19957. return j + 6;
  19958. }
  19959. return 2 - j;
  19960. }
  19961. static #neighborIdToIndex = new Int32Array([0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1, 1, 0, 1, 1]);
  19962. static #clockwiseNonZero(buf, width, i0, j0, i, j, offset) {
  19963. const id = this.#neighborIndexToId(i0, j0, i, j);
  19964. for (let k = 0; k < 8; k++) {
  19965. const kk = (-k + id - offset + 16) % 8;
  19966. const shiftI = this.#neighborIdToIndex[2 * kk];
  19967. const shiftJ = this.#neighborIdToIndex[2 * kk + 1];
  19968. if (buf[(i0 + shiftI) * width + (j0 + shiftJ)] !== 0) {
  19969. return kk;
  19970. }
  19971. }
  19972. return -1;
  19973. }
  19974. static #counterClockwiseNonZero(buf, width, i0, j0, i, j, offset) {
  19975. const id = this.#neighborIndexToId(i0, j0, i, j);
  19976. for (let k = 0; k < 8; k++) {
  19977. const kk = (k + id + offset + 16) % 8;
  19978. const shiftI = this.#neighborIdToIndex[2 * kk];
  19979. const shiftJ = this.#neighborIdToIndex[2 * kk + 1];
  19980. if (buf[(i0 + shiftI) * width + (j0 + shiftJ)] !== 0) {
  19981. return kk;
  19982. }
  19983. }
  19984. return -1;
  19985. }
  19986. static #findContours(buf, width, height, threshold) {
  19987. const N = buf.length;
  19988. const types = new Int32Array(N);
  19989. for (let i = 0; i < N; i++) {
  19990. types[i] = buf[i] <= threshold ? 1 : 0;
  19991. }
  19992. for (let i = 1; i < height - 1; i++) {
  19993. types[i * width] = types[i * width + width - 1] = 0;
  19994. }
  19995. for (let i = 0; i < width; i++) {
  19996. types[i] = types[width * height - 1 - i] = 0;
  19997. }
  19998. let nbd = 1;
  19999. let lnbd;
  20000. const contours = [];
  20001. for (let i = 1; i < height - 1; i++) {
  20002. lnbd = 1;
  20003. for (let j = 1; j < width - 1; j++) {
  20004. const ij = i * width + j;
  20005. const pix = types[ij];
  20006. if (pix === 0) {
  20007. continue;
  20008. }
  20009. let i2 = i;
  20010. let j2 = j;
  20011. if (pix === 1 && types[ij - 1] === 0) {
  20012. nbd += 1;
  20013. j2 -= 1;
  20014. } else if (pix >= 1 && types[ij + 1] === 0) {
  20015. nbd += 1;
  20016. j2 += 1;
  20017. if (pix > 1) {
  20018. lnbd = pix;
  20019. }
  20020. } else {
  20021. if (pix !== 1) {
  20022. lnbd = Math.abs(pix);
  20023. }
  20024. continue;
  20025. }
  20026. const points = [j, i];
  20027. const isHole = j2 === j + 1;
  20028. const contour = {
  20029. isHole,
  20030. points,
  20031. id: nbd,
  20032. parent: 0
  20033. };
  20034. contours.push(contour);
  20035. let contour0;
  20036. for (const c of contours) {
  20037. if (c.id === lnbd) {
  20038. contour0 = c;
  20039. break;
  20040. }
  20041. }
  20042. if (!contour0) {
  20043. contour.parent = isHole ? lnbd : 0;
  20044. } else if (contour0.isHole) {
  20045. contour.parent = isHole ? contour0.parent : lnbd;
  20046. } else {
  20047. contour.parent = isHole ? lnbd : contour0.parent;
  20048. }
  20049. const k = this.#clockwiseNonZero(types, width, i, j, i2, j2, 0);
  20050. if (k === -1) {
  20051. types[ij] = -nbd;
  20052. if (types[ij] !== 1) {
  20053. lnbd = Math.abs(types[ij]);
  20054. }
  20055. continue;
  20056. }
  20057. let shiftI = this.#neighborIdToIndex[2 * k];
  20058. let shiftJ = this.#neighborIdToIndex[2 * k + 1];
  20059. const i1 = i + shiftI;
  20060. const j1 = j + shiftJ;
  20061. i2 = i1;
  20062. j2 = j1;
  20063. let i3 = i;
  20064. let j3 = j;
  20065. while (true) {
  20066. const kk = this.#counterClockwiseNonZero(types, width, i3, j3, i2, j2, 1);
  20067. shiftI = this.#neighborIdToIndex[2 * kk];
  20068. shiftJ = this.#neighborIdToIndex[2 * kk + 1];
  20069. const i4 = i3 + shiftI;
  20070. const j4 = j3 + shiftJ;
  20071. points.push(j4, i4);
  20072. const ij3 = i3 * width + j3;
  20073. if (types[ij3 + 1] === 0) {
  20074. types[ij3] = -nbd;
  20075. } else if (types[ij3] === 1) {
  20076. types[ij3] = nbd;
  20077. }
  20078. if (i4 === i && j4 === j && i3 === i1 && j3 === j1) {
  20079. if (types[ij] !== 1) {
  20080. lnbd = Math.abs(types[ij]);
  20081. }
  20082. break;
  20083. } else {
  20084. i2 = i3;
  20085. j2 = j3;
  20086. i3 = i4;
  20087. j3 = j4;
  20088. }
  20089. }
  20090. }
  20091. }
  20092. return contours;
  20093. }
  20094. static #douglasPeuckerHelper(points, start, end, output) {
  20095. if (end - start <= 4) {
  20096. for (let i = start; i < end - 2; i += 2) {
  20097. output.push(points[i], points[i + 1]);
  20098. }
  20099. return;
  20100. }
  20101. const ax = points[start];
  20102. const ay = points[start + 1];
  20103. const abx = points[end - 4] - ax;
  20104. const aby = points[end - 3] - ay;
  20105. const dist = Math.hypot(abx, aby);
  20106. const nabx = abx / dist;
  20107. const naby = aby / dist;
  20108. const aa = nabx * ay - naby * ax;
  20109. const m = aby / abx;
  20110. const invS = 1 / dist;
  20111. const phi = Math.atan(m);
  20112. const cosPhi = Math.cos(phi);
  20113. const sinPhi = Math.sin(phi);
  20114. const tmax = invS * (Math.abs(cosPhi) + Math.abs(sinPhi));
  20115. const poly = invS * (1 - tmax + tmax ** 2);
  20116. const partialPhi = Math.max(Math.atan(Math.abs(sinPhi + cosPhi) * poly), Math.atan(Math.abs(sinPhi - cosPhi) * poly));
  20117. let dmax = 0;
  20118. let index = start;
  20119. for (let i = start + 2; i < end - 2; i += 2) {
  20120. const d = Math.abs(aa - nabx * points[i + 1] + naby * points[i]);
  20121. if (d > dmax) {
  20122. index = i;
  20123. dmax = d;
  20124. }
  20125. }
  20126. if (dmax > (dist * partialPhi) ** 2) {
  20127. this.#douglasPeuckerHelper(points, start, index + 2, output);
  20128. this.#douglasPeuckerHelper(points, index, end, output);
  20129. } else {
  20130. output.push(ax, ay);
  20131. }
  20132. }
  20133. static #douglasPeucker(points) {
  20134. const output = [];
  20135. const len = points.length;
  20136. this.#douglasPeuckerHelper(points, 0, len, output);
  20137. output.push(points[len - 2], points[len - 1]);
  20138. return output.length <= 4 ? null : output;
  20139. }
  20140. static #bilateralFilter(buf, width, height, sigmaS, sigmaR, kernelSize) {
  20141. const kernel = new Float32Array(kernelSize ** 2);
  20142. const sigmaS2 = -2 * sigmaS ** 2;
  20143. const halfSize = kernelSize >> 1;
  20144. for (let i = 0; i < kernelSize; i++) {
  20145. const x = (i - halfSize) ** 2;
  20146. for (let j = 0; j < kernelSize; j++) {
  20147. kernel[i * kernelSize + j] = Math.exp((x + (j - halfSize) ** 2) / sigmaS2);
  20148. }
  20149. }
  20150. const rangeValues = new Float32Array(256);
  20151. const sigmaR2 = -2 * sigmaR ** 2;
  20152. for (let i = 0; i < 256; i++) {
  20153. rangeValues[i] = Math.exp(i ** 2 / sigmaR2);
  20154. }
  20155. const N = buf.length;
  20156. const out = new Uint8Array(N);
  20157. const histogram = new Uint32Array(256);
  20158. for (let i = 0; i < height; i++) {
  20159. for (let j = 0; j < width; j++) {
  20160. const ij = i * width + j;
  20161. const center = buf[ij];
  20162. let sum = 0;
  20163. let norm = 0;
  20164. for (let k = 0; k < kernelSize; k++) {
  20165. const y = i + k - halfSize;
  20166. if (y < 0 || y >= height) {
  20167. continue;
  20168. }
  20169. for (let l = 0; l < kernelSize; l++) {
  20170. const x = j + l - halfSize;
  20171. if (x < 0 || x >= width) {
  20172. continue;
  20173. }
  20174. const neighbour = buf[y * width + x];
  20175. const w = kernel[k * kernelSize + l] * rangeValues[Math.abs(neighbour - center)];
  20176. sum += neighbour * w;
  20177. norm += w;
  20178. }
  20179. }
  20180. const pix = out[ij] = Math.round(sum / norm);
  20181. histogram[pix]++;
  20182. }
  20183. }
  20184. return [out, histogram];
  20185. }
  20186. static #getHistogram(buf) {
  20187. const histogram = new Uint32Array(256);
  20188. for (const g of buf) {
  20189. histogram[g]++;
  20190. }
  20191. return histogram;
  20192. }
  20193. static #toUint8(buf) {
  20194. const N = buf.length;
  20195. const out = new Uint8ClampedArray(N >> 2);
  20196. let max = -Infinity;
  20197. let min = Infinity;
  20198. for (let i = 0, ii = out.length; i < ii; i++) {
  20199. const A = buf[(i << 2) + 3];
  20200. if (A === 0) {
  20201. max = out[i] = 0xff;
  20202. continue;
  20203. }
  20204. const pix = out[i] = buf[i << 2];
  20205. if (pix > max) {
  20206. max = pix;
  20207. }
  20208. if (pix < min) {
  20209. min = pix;
  20210. }
  20211. }
  20212. const ratio = 255 / (max - min);
  20213. for (let i = 0; i < N; i++) {
  20214. out[i] = (out[i] - min) * ratio;
  20215. }
  20216. return out;
  20217. }
  20218. static #guessThreshold(histogram) {
  20219. let i;
  20220. let M = -Infinity;
  20221. let L = -Infinity;
  20222. const min = histogram.findIndex(v => v !== 0);
  20223. let pos = min;
  20224. let spos = min;
  20225. for (i = min; i < 256; i++) {
  20226. const v = histogram[i];
  20227. if (v > M) {
  20228. if (i - pos > L) {
  20229. L = i - pos;
  20230. spos = i - 1;
  20231. }
  20232. M = v;
  20233. pos = i;
  20234. }
  20235. }
  20236. for (i = spos - 1; i >= 0; i--) {
  20237. if (histogram[i] > histogram[i + 1]) {
  20238. break;
  20239. }
  20240. }
  20241. return i;
  20242. }
  20243. static #getGrayPixels(bitmap) {
  20244. const originalBitmap = bitmap;
  20245. const {
  20246. width,
  20247. height
  20248. } = bitmap;
  20249. const {
  20250. maxDim
  20251. } = this.#PARAMETERS;
  20252. let newWidth = width;
  20253. let newHeight = height;
  20254. if (width > maxDim || height > maxDim) {
  20255. let prevWidth = width;
  20256. let prevHeight = height;
  20257. let steps = Math.log2(Math.max(width, height) / maxDim);
  20258. const isteps = Math.floor(steps);
  20259. steps = steps === isteps ? isteps - 1 : isteps;
  20260. for (let i = 0; i < steps; i++) {
  20261. newWidth = prevWidth;
  20262. newHeight = prevHeight;
  20263. if (newWidth > maxDim) {
  20264. newWidth = Math.ceil(newWidth / 2);
  20265. }
  20266. if (newHeight > maxDim) {
  20267. newHeight = Math.ceil(newHeight / 2);
  20268. }
  20269. const offscreen = new OffscreenCanvas(newWidth, newHeight);
  20270. const ctx = offscreen.getContext("2d");
  20271. ctx.drawImage(bitmap, 0, 0, prevWidth, prevHeight, 0, 0, newWidth, newHeight);
  20272. prevWidth = newWidth;
  20273. prevHeight = newHeight;
  20274. if (bitmap !== originalBitmap) {
  20275. bitmap.close();
  20276. }
  20277. bitmap = offscreen.transferToImageBitmap();
  20278. }
  20279. const ratio = Math.min(maxDim / newWidth, maxDim / newHeight);
  20280. newWidth = Math.round(newWidth * ratio);
  20281. newHeight = Math.round(newHeight * ratio);
  20282. }
  20283. const offscreen = new OffscreenCanvas(newWidth, newHeight);
  20284. const ctx = offscreen.getContext("2d", {
  20285. willReadFrequently: true
  20286. });
  20287. ctx.filter = "grayscale(1)";
  20288. ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, newWidth, newHeight);
  20289. const grayImage = ctx.getImageData(0, 0, newWidth, newHeight).data;
  20290. const uint8Buf = this.#toUint8(grayImage);
  20291. return [uint8Buf, newWidth, newHeight];
  20292. }
  20293. static extractContoursFromText(text, {
  20294. fontFamily,
  20295. fontStyle,
  20296. fontWeight
  20297. }, pageWidth, pageHeight, rotation, innerMargin) {
  20298. let canvas = new OffscreenCanvas(1, 1);
  20299. let ctx = canvas.getContext("2d", {
  20300. alpha: false
  20301. });
  20302. const fontSize = 200;
  20303. const font = ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
  20304. const {
  20305. actualBoundingBoxLeft,
  20306. actualBoundingBoxRight,
  20307. actualBoundingBoxAscent,
  20308. actualBoundingBoxDescent,
  20309. fontBoundingBoxAscent,
  20310. fontBoundingBoxDescent,
  20311. width
  20312. } = ctx.measureText(text);
  20313. const SCALE = 1.5;
  20314. const canvasWidth = Math.ceil(Math.max(Math.abs(actualBoundingBoxLeft) + Math.abs(actualBoundingBoxRight) || 0, width) * SCALE);
  20315. const canvasHeight = Math.ceil(Math.max(Math.abs(actualBoundingBoxAscent) + Math.abs(actualBoundingBoxDescent) || fontSize, Math.abs(fontBoundingBoxAscent) + Math.abs(fontBoundingBoxDescent) || fontSize) * SCALE);
  20316. canvas = new OffscreenCanvas(canvasWidth, canvasHeight);
  20317. ctx = canvas.getContext("2d", {
  20318. alpha: true,
  20319. willReadFrequently: true
  20320. });
  20321. ctx.font = font;
  20322. ctx.filter = "grayscale(1)";
  20323. ctx.fillStyle = "white";
  20324. ctx.fillRect(0, 0, canvasWidth, canvasHeight);
  20325. ctx.fillStyle = "black";
  20326. ctx.fillText(text, canvasWidth * (SCALE - 1) / 2, canvasHeight * (3 - SCALE) / 2);
  20327. const uint8Buf = this.#toUint8(ctx.getImageData(0, 0, canvasWidth, canvasHeight).data);
  20328. const histogram = this.#getHistogram(uint8Buf);
  20329. const threshold = this.#guessThreshold(histogram);
  20330. const contourList = this.#findContours(uint8Buf, canvasWidth, canvasHeight, threshold);
  20331. return this.processDrawnLines({
  20332. lines: {
  20333. curves: contourList,
  20334. width: canvasWidth,
  20335. height: canvasHeight
  20336. },
  20337. pageWidth,
  20338. pageHeight,
  20339. rotation,
  20340. innerMargin,
  20341. mustSmooth: true,
  20342. areContours: true
  20343. });
  20344. }
  20345. static process(bitmap, pageWidth, pageHeight, rotation, innerMargin) {
  20346. const [uint8Buf, width, height] = this.#getGrayPixels(bitmap);
  20347. const [buffer, histogram] = this.#bilateralFilter(uint8Buf, width, height, Math.hypot(width, height) * this.#PARAMETERS.sigmaSFactor, this.#PARAMETERS.sigmaR, this.#PARAMETERS.kernelSize);
  20348. const threshold = this.#guessThreshold(histogram);
  20349. const contourList = this.#findContours(buffer, width, height, threshold);
  20350. return this.processDrawnLines({
  20351. lines: {
  20352. curves: contourList,
  20353. width,
  20354. height
  20355. },
  20356. pageWidth,
  20357. pageHeight,
  20358. rotation,
  20359. innerMargin,
  20360. mustSmooth: true,
  20361. areContours: true
  20362. });
  20363. }
  20364. static processDrawnLines({
  20365. lines,
  20366. pageWidth,
  20367. pageHeight,
  20368. rotation,
  20369. innerMargin,
  20370. mustSmooth,
  20371. areContours
  20372. }) {
  20373. if (rotation % 180 !== 0) {
  20374. [pageWidth, pageHeight] = [pageHeight, pageWidth];
  20375. }
  20376. const {
  20377. curves,
  20378. width,
  20379. height
  20380. } = lines;
  20381. const thickness = lines.thickness ?? 0;
  20382. const linesAndPoints = [];
  20383. const ratio = Math.min(pageWidth / width, pageHeight / height);
  20384. const xScale = ratio / pageWidth;
  20385. const yScale = ratio / pageHeight;
  20386. const newCurves = [];
  20387. for (const {
  20388. points
  20389. } of curves) {
  20390. const reducedPoints = mustSmooth ? this.#douglasPeucker(points) : points;
  20391. if (!reducedPoints) {
  20392. continue;
  20393. }
  20394. newCurves.push(reducedPoints);
  20395. const len = reducedPoints.length;
  20396. const newPoints = new Float32Array(len);
  20397. const line = new Float32Array(3 * (len === 2 ? 2 : len - 2));
  20398. linesAndPoints.push({
  20399. line,
  20400. points: newPoints
  20401. });
  20402. if (len === 2) {
  20403. newPoints[0] = reducedPoints[0] * xScale;
  20404. newPoints[1] = reducedPoints[1] * yScale;
  20405. line.set([NaN, NaN, NaN, NaN, newPoints[0], newPoints[1]], 0);
  20406. continue;
  20407. }
  20408. let [x1, y1, x2, y2] = reducedPoints;
  20409. x1 *= xScale;
  20410. y1 *= yScale;
  20411. x2 *= xScale;
  20412. y2 *= yScale;
  20413. newPoints.set([x1, y1, x2, y2], 0);
  20414. line.set([NaN, NaN, NaN, NaN, x1, y1], 0);
  20415. for (let i = 4; i < len; i += 2) {
  20416. const x = newPoints[i] = reducedPoints[i] * xScale;
  20417. const y = newPoints[i + 1] = reducedPoints[i + 1] * yScale;
  20418. line.set(Outline.createBezierPoints(x1, y1, x2, y2, x, y), (i - 2) * 3);
  20419. [x1, y1, x2, y2] = [x2, y2, x, y];
  20420. }
  20421. }
  20422. if (linesAndPoints.length === 0) {
  20423. return null;
  20424. }
  20425. const outline = areContours ? new ContourDrawOutline() : new InkDrawOutline();
  20426. outline.build(linesAndPoints, pageWidth, pageHeight, 1, rotation, areContours ? 0 : thickness, innerMargin);
  20427. return {
  20428. outline,
  20429. newCurves,
  20430. areContours,
  20431. thickness,
  20432. width,
  20433. height
  20434. };
  20435. }
  20436. static async compressSignature({
  20437. outlines,
  20438. areContours,
  20439. thickness,
  20440. width,
  20441. height
  20442. }) {
  20443. let minDiff = Infinity;
  20444. let maxDiff = -Infinity;
  20445. let outlinesLength = 0;
  20446. for (const points of outlines) {
  20447. outlinesLength += points.length;
  20448. for (let i = 2, ii = points.length; i < ii; i++) {
  20449. const dx = points[i] - points[i - 2];
  20450. minDiff = Math.min(minDiff, dx);
  20451. maxDiff = Math.max(maxDiff, dx);
  20452. }
  20453. }
  20454. let bufferType;
  20455. if (minDiff >= -128 && maxDiff <= 127) {
  20456. bufferType = Int8Array;
  20457. } else if (minDiff >= -32768 && maxDiff <= 32767) {
  20458. bufferType = Int16Array;
  20459. } else {
  20460. bufferType = Int32Array;
  20461. }
  20462. const len = outlines.length;
  20463. const headerLength = BASE_HEADER_LENGTH + POINTS_PROPERTIES_NUMBER * len;
  20464. const header = new Uint32Array(headerLength);
  20465. let offset = 0;
  20466. header[offset++] = headerLength * Uint32Array.BYTES_PER_ELEMENT + (outlinesLength - 2 * len) * bufferType.BYTES_PER_ELEMENT;
  20467. header[offset++] = 0;
  20468. header[offset++] = width;
  20469. header[offset++] = height;
  20470. header[offset++] = areContours ? 0 : 1;
  20471. header[offset++] = Math.max(0, Math.floor(thickness ?? 0));
  20472. header[offset++] = len;
  20473. header[offset++] = bufferType.BYTES_PER_ELEMENT;
  20474. for (const points of outlines) {
  20475. header[offset++] = points.length - 2;
  20476. header[offset++] = points[0];
  20477. header[offset++] = points[1];
  20478. }
  20479. const cs = new CompressionStream("deflate-raw");
  20480. const writer = cs.writable.getWriter();
  20481. await writer.ready;
  20482. writer.write(header);
  20483. const BufferCtor = bufferType.prototype.constructor;
  20484. for (const points of outlines) {
  20485. const diffs = new BufferCtor(points.length - 2);
  20486. for (let i = 2, ii = points.length; i < ii; i++) {
  20487. diffs[i - 2] = points[i] - points[i - 2];
  20488. }
  20489. writer.write(diffs);
  20490. }
  20491. writer.close();
  20492. const buf = await new Response(cs.readable).arrayBuffer();
  20493. const bytes = new Uint8Array(buf);
  20494. return toBase64Util(bytes);
  20495. }
  20496. static async decompressSignature(signatureData) {
  20497. try {
  20498. const bytes = fromBase64Util(signatureData);
  20499. const {
  20500. readable,
  20501. writable
  20502. } = new DecompressionStream("deflate-raw");
  20503. const writer = writable.getWriter();
  20504. await writer.ready;
  20505. writer.write(bytes).then(async () => {
  20506. await writer.ready;
  20507. await writer.close();
  20508. }).catch(() => {});
  20509. let data = null;
  20510. let offset = 0;
  20511. for await (const chunk of readable) {
  20512. data ||= new Uint8Array(new Uint32Array(chunk.buffer, 0, 4)[0]);
  20513. data.set(chunk, offset);
  20514. offset += chunk.length;
  20515. }
  20516. const header = new Uint32Array(data.buffer, 0, data.length >> 2);
  20517. const version = header[1];
  20518. if (version !== 0) {
  20519. throw new Error(`Invalid version: ${version}`);
  20520. }
  20521. const width = header[2];
  20522. const height = header[3];
  20523. const areContours = header[4] === 0;
  20524. const thickness = header[5];
  20525. const numberOfDrawings = header[6];
  20526. const bufferType = header[7];
  20527. const outlines = [];
  20528. const diffsOffset = (BASE_HEADER_LENGTH + POINTS_PROPERTIES_NUMBER * numberOfDrawings) * Uint32Array.BYTES_PER_ELEMENT;
  20529. let diffs;
  20530. switch (bufferType) {
  20531. case Int8Array.BYTES_PER_ELEMENT:
  20532. diffs = new Int8Array(data.buffer, diffsOffset);
  20533. break;
  20534. case Int16Array.BYTES_PER_ELEMENT:
  20535. diffs = new Int16Array(data.buffer, diffsOffset);
  20536. break;
  20537. case Int32Array.BYTES_PER_ELEMENT:
  20538. diffs = new Int32Array(data.buffer, diffsOffset);
  20539. break;
  20540. }
  20541. offset = 0;
  20542. for (let i = 0; i < numberOfDrawings; i++) {
  20543. const len = header[POINTS_PROPERTIES_NUMBER * i + BASE_HEADER_LENGTH];
  20544. const points = new Float32Array(len + 2);
  20545. outlines.push(points);
  20546. for (let j = 0; j < POINTS_PROPERTIES_NUMBER - 1; j++) {
  20547. points[j] = header[POINTS_PROPERTIES_NUMBER * i + BASE_HEADER_LENGTH + j + 1];
  20548. }
  20549. for (let j = 0; j < len; j++) {
  20550. points[j + 2] = points[j] + diffs[offset++];
  20551. }
  20552. }
  20553. return {
  20554. areContours,
  20555. thickness,
  20556. outlines,
  20557. width,
  20558. height
  20559. };
  20560. } catch (e) {
  20561. warn(`decompressSignature: ${e}`);
  20562. return null;
  20563. }
  20564. }
  20565. }
  20566. ;// ./src/display/editor/signature.js
  20567. class SignatureOptions extends DrawingOptions {
  20568. constructor() {
  20569. super();
  20570. super.updateProperties({
  20571. fill: "CanvasText",
  20572. "stroke-width": 0
  20573. });
  20574. }
  20575. clone() {
  20576. const clone = new SignatureOptions();
  20577. clone.updateAll(this);
  20578. return clone;
  20579. }
  20580. }
  20581. class DrawnSignatureOptions extends InkDrawingOptions {
  20582. constructor(viewerParameters) {
  20583. super(viewerParameters);
  20584. super.updateProperties({
  20585. stroke: "CanvasText",
  20586. "stroke-width": 1
  20587. });
  20588. }
  20589. clone() {
  20590. const clone = new DrawnSignatureOptions(this._viewParameters);
  20591. clone.updateAll(this);
  20592. return clone;
  20593. }
  20594. }
  20595. class SignatureEditor extends DrawingEditor {
  20596. #isExtracted = false;
  20597. #description = null;
  20598. #signatureData = null;
  20599. #signatureUUID = null;
  20600. static _type = "signature";
  20601. static _editorType = AnnotationEditorType.SIGNATURE;
  20602. static _defaultDrawingOptions = null;
  20603. constructor(params) {
  20604. super({
  20605. ...params,
  20606. mustBeCommitted: true,
  20607. name: "signatureEditor"
  20608. });
  20609. this._willKeepAspectRatio = true;
  20610. this.#signatureData = params.signatureData || null;
  20611. this.#description = null;
  20612. this.defaultL10nId = "pdfjs-editor-signature-editor1";
  20613. }
  20614. static initialize(l10n, uiManager) {
  20615. AnnotationEditor.initialize(l10n, uiManager);
  20616. this._defaultDrawingOptions = new SignatureOptions();
  20617. this._defaultDrawnSignatureOptions = new DrawnSignatureOptions(uiManager.viewParameters);
  20618. }
  20619. static getDefaultDrawingOptions(options) {
  20620. const clone = this._defaultDrawingOptions.clone();
  20621. clone.updateProperties(options);
  20622. return clone;
  20623. }
  20624. static get supportMultipleDrawings() {
  20625. return false;
  20626. }
  20627. static get typesMap() {
  20628. return shadow(this, "typesMap", new Map());
  20629. }
  20630. static get isDrawer() {
  20631. return false;
  20632. }
  20633. get telemetryFinalData() {
  20634. return {
  20635. type: "signature",
  20636. hasDescription: !!this.#description
  20637. };
  20638. }
  20639. static computeTelemetryFinalData(data) {
  20640. const hasDescriptionStats = data.get("hasDescription");
  20641. return {
  20642. hasAltText: hasDescriptionStats.get(true) ?? 0,
  20643. hasNoAltText: hasDescriptionStats.get(false) ?? 0
  20644. };
  20645. }
  20646. get isResizable() {
  20647. return true;
  20648. }
  20649. onScaleChanging() {
  20650. if (this._drawId === null) {
  20651. return;
  20652. }
  20653. super.onScaleChanging();
  20654. }
  20655. render() {
  20656. if (this.div) {
  20657. return this.div;
  20658. }
  20659. let baseX, baseY;
  20660. const {
  20661. _isCopy
  20662. } = this;
  20663. if (_isCopy) {
  20664. this._isCopy = false;
  20665. baseX = this.x;
  20666. baseY = this.y;
  20667. }
  20668. super.render();
  20669. if (this._drawId === null) {
  20670. if (this.#signatureData) {
  20671. const {
  20672. lines,
  20673. mustSmooth,
  20674. areContours,
  20675. description,
  20676. uuid,
  20677. heightInPage
  20678. } = this.#signatureData;
  20679. const {
  20680. rawDims: {
  20681. pageWidth,
  20682. pageHeight
  20683. },
  20684. rotation
  20685. } = this.parent.viewport;
  20686. const outline = SignatureExtractor.processDrawnLines({
  20687. lines,
  20688. pageWidth,
  20689. pageHeight,
  20690. rotation,
  20691. innerMargin: SignatureEditor._INNER_MARGIN,
  20692. mustSmooth,
  20693. areContours
  20694. });
  20695. this.addSignature(outline, heightInPage, description, uuid);
  20696. } else {
  20697. this.div.setAttribute("data-l10n-args", JSON.stringify({
  20698. description: ""
  20699. }));
  20700. this.div.hidden = true;
  20701. this._uiManager.getSignature(this);
  20702. }
  20703. }
  20704. if (_isCopy) {
  20705. this._isCopy = true;
  20706. this._moveAfterPaste(baseX, baseY);
  20707. }
  20708. return this.div;
  20709. }
  20710. setUuid(uuid) {
  20711. this.#signatureUUID = uuid;
  20712. this.addEditToolbar();
  20713. }
  20714. getUuid() {
  20715. return this.#signatureUUID;
  20716. }
  20717. get description() {
  20718. return this.#description;
  20719. }
  20720. set description(description) {
  20721. this.#description = description;
  20722. super.addEditToolbar().then(toolbar => {
  20723. toolbar?.updateEditSignatureButton(description);
  20724. });
  20725. }
  20726. getSignaturePreview() {
  20727. const {
  20728. newCurves,
  20729. areContours,
  20730. thickness,
  20731. width,
  20732. height
  20733. } = this.#signatureData;
  20734. const maxDim = Math.max(width, height);
  20735. const outlineData = SignatureExtractor.processDrawnLines({
  20736. lines: {
  20737. curves: newCurves.map(points => ({
  20738. points
  20739. })),
  20740. thickness,
  20741. width,
  20742. height
  20743. },
  20744. pageWidth: maxDim,
  20745. pageHeight: maxDim,
  20746. rotation: 0,
  20747. innerMargin: 0,
  20748. mustSmooth: false,
  20749. areContours
  20750. });
  20751. return {
  20752. areContours,
  20753. outline: outlineData.outline
  20754. };
  20755. }
  20756. async addEditToolbar() {
  20757. const toolbar = await super.addEditToolbar();
  20758. if (!toolbar) {
  20759. return null;
  20760. }
  20761. if (this._uiManager.signatureManager && this.#description !== null) {
  20762. await toolbar.addEditSignatureButton(this._uiManager.signatureManager, this.#signatureUUID, this.#description);
  20763. toolbar.show();
  20764. }
  20765. return toolbar;
  20766. }
  20767. addSignature(data, heightInPage, description, uuid) {
  20768. const {
  20769. x: savedX,
  20770. y: savedY
  20771. } = this;
  20772. const {
  20773. outline
  20774. } = this.#signatureData = data;
  20775. this.#isExtracted = outline instanceof ContourDrawOutline;
  20776. this.#description = description;
  20777. this.div.setAttribute("data-l10n-args", JSON.stringify({
  20778. description
  20779. }));
  20780. let drawingOptions;
  20781. if (this.#isExtracted) {
  20782. drawingOptions = SignatureEditor.getDefaultDrawingOptions();
  20783. } else {
  20784. drawingOptions = SignatureEditor._defaultDrawnSignatureOptions.clone();
  20785. drawingOptions.updateProperties({
  20786. "stroke-width": outline.thickness
  20787. });
  20788. }
  20789. this._addOutlines({
  20790. drawOutlines: outline,
  20791. drawingOptions
  20792. });
  20793. const [parentWidth, parentHeight] = this.parentDimensions;
  20794. const [, pageHeight] = this.pageDimensions;
  20795. let newHeight = heightInPage / pageHeight;
  20796. newHeight = newHeight >= 1 ? 0.5 : newHeight;
  20797. this.width *= newHeight / this.height;
  20798. if (this.width >= 1) {
  20799. newHeight *= 0.9 / this.width;
  20800. this.width = 0.9;
  20801. }
  20802. this.height = newHeight;
  20803. this.setDims(parentWidth * this.width, parentHeight * this.height);
  20804. this.x = savedX;
  20805. this.y = savedY;
  20806. this.center();
  20807. this._onResized();
  20808. this.onScaleChanging();
  20809. this.rotate();
  20810. this._uiManager.addToAnnotationStorage(this);
  20811. this.setUuid(uuid);
  20812. this._reportTelemetry({
  20813. action: "pdfjs.signature.inserted",
  20814. data: {
  20815. hasBeenSaved: !!uuid,
  20816. hasDescription: !!description
  20817. }
  20818. });
  20819. this.div.hidden = false;
  20820. }
  20821. getFromImage(bitmap) {
  20822. const {
  20823. rawDims: {
  20824. pageWidth,
  20825. pageHeight
  20826. },
  20827. rotation
  20828. } = this.parent.viewport;
  20829. return SignatureExtractor.process(bitmap, pageWidth, pageHeight, rotation, SignatureEditor._INNER_MARGIN);
  20830. }
  20831. getFromText(text, fontInfo) {
  20832. const {
  20833. rawDims: {
  20834. pageWidth,
  20835. pageHeight
  20836. },
  20837. rotation
  20838. } = this.parent.viewport;
  20839. return SignatureExtractor.extractContoursFromText(text, fontInfo, pageWidth, pageHeight, rotation, SignatureEditor._INNER_MARGIN);
  20840. }
  20841. getDrawnSignature(curves) {
  20842. const {
  20843. rawDims: {
  20844. pageWidth,
  20845. pageHeight
  20846. },
  20847. rotation
  20848. } = this.parent.viewport;
  20849. return SignatureExtractor.processDrawnLines({
  20850. lines: curves,
  20851. pageWidth,
  20852. pageHeight,
  20853. rotation,
  20854. innerMargin: SignatureEditor._INNER_MARGIN,
  20855. mustSmooth: false,
  20856. areContours: false
  20857. });
  20858. }
  20859. createDrawingOptions({
  20860. areContours,
  20861. thickness
  20862. }) {
  20863. if (areContours) {
  20864. this._drawingOptions = SignatureEditor.getDefaultDrawingOptions();
  20865. } else {
  20866. this._drawingOptions = SignatureEditor._defaultDrawnSignatureOptions.clone();
  20867. this._drawingOptions.updateProperties({
  20868. "stroke-width": thickness
  20869. });
  20870. }
  20871. }
  20872. serialize(isForCopying = false) {
  20873. if (this.isEmpty()) {
  20874. return null;
  20875. }
  20876. const {
  20877. lines,
  20878. points,
  20879. rect
  20880. } = this.serializeDraw(isForCopying);
  20881. const {
  20882. _drawingOptions: {
  20883. "stroke-width": thickness
  20884. }
  20885. } = this;
  20886. const serialized = {
  20887. annotationType: AnnotationEditorType.SIGNATURE,
  20888. isSignature: true,
  20889. areContours: this.#isExtracted,
  20890. color: [0, 0, 0],
  20891. thickness: this.#isExtracted ? 0 : thickness,
  20892. pageIndex: this.pageIndex,
  20893. rect,
  20894. rotation: this.rotation,
  20895. structTreeParentId: this._structTreeParentId
  20896. };
  20897. if (isForCopying) {
  20898. serialized.paths = {
  20899. lines,
  20900. points
  20901. };
  20902. serialized.uuid = this.#signatureUUID;
  20903. serialized.isCopy = true;
  20904. } else {
  20905. serialized.lines = lines;
  20906. }
  20907. if (this.#description) {
  20908. serialized.accessibilityData = {
  20909. type: "Figure",
  20910. alt: this.#description
  20911. };
  20912. }
  20913. return serialized;
  20914. }
  20915. static deserializeDraw(pageX, pageY, pageWidth, pageHeight, innerMargin, data) {
  20916. if (data.areContours) {
  20917. return ContourDrawOutline.deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, data);
  20918. }
  20919. return InkDrawOutline.deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, data);
  20920. }
  20921. static async deserialize(data, parent, uiManager) {
  20922. const editor = await super.deserialize(data, parent, uiManager);
  20923. editor.#isExtracted = data.areContours;
  20924. editor.#description = data.accessibilityData?.alt || "";
  20925. editor.#signatureUUID = data.uuid;
  20926. return editor;
  20927. }
  20928. }
  20929. ;// ./src/display/editor/stamp.js
  20930. class StampEditor extends AnnotationEditor {
  20931. #bitmap = null;
  20932. #bitmapId = null;
  20933. #bitmapPromise = null;
  20934. #bitmapUrl = null;
  20935. #bitmapFile = null;
  20936. #bitmapFileName = "";
  20937. #canvas = null;
  20938. #missingCanvas = false;
  20939. #resizeTimeoutId = null;
  20940. #isSvg = false;
  20941. #hasBeenAddedInUndoStack = false;
  20942. static _type = "stamp";
  20943. static _editorType = AnnotationEditorType.STAMP;
  20944. constructor(params) {
  20945. super({
  20946. ...params,
  20947. name: "stampEditor"
  20948. });
  20949. this.#bitmapUrl = params.bitmapUrl;
  20950. this.#bitmapFile = params.bitmapFile;
  20951. this.defaultL10nId = "pdfjs-editor-stamp-editor";
  20952. }
  20953. static initialize(l10n, uiManager) {
  20954. AnnotationEditor.initialize(l10n, uiManager);
  20955. }
  20956. static isHandlingMimeForPasting(mime) {
  20957. return SupportedImageMimeTypes.includes(mime);
  20958. }
  20959. static paste(item, parent) {
  20960. parent.pasteEditor(AnnotationEditorType.STAMP, {
  20961. bitmapFile: item.getAsFile()
  20962. });
  20963. }
  20964. altTextFinish() {
  20965. if (this._uiManager.useNewAltTextFlow) {
  20966. this.div.hidden = false;
  20967. }
  20968. super.altTextFinish();
  20969. }
  20970. get telemetryFinalData() {
  20971. return {
  20972. type: "stamp",
  20973. hasAltText: !!this.altTextData?.altText
  20974. };
  20975. }
  20976. static computeTelemetryFinalData(data) {
  20977. const hasAltTextStats = data.get("hasAltText");
  20978. return {
  20979. hasAltText: hasAltTextStats.get(true) ?? 0,
  20980. hasNoAltText: hasAltTextStats.get(false) ?? 0
  20981. };
  20982. }
  20983. #getBitmapFetched(data, fromId = false) {
  20984. if (!data) {
  20985. this.remove();
  20986. return;
  20987. }
  20988. this.#bitmap = data.bitmap;
  20989. if (!fromId) {
  20990. this.#bitmapId = data.id;
  20991. this.#isSvg = data.isSvg;
  20992. }
  20993. if (data.file) {
  20994. this.#bitmapFileName = data.file.name;
  20995. }
  20996. this.#createCanvas();
  20997. }
  20998. #getBitmapDone() {
  20999. this.#bitmapPromise = null;
  21000. this._uiManager.enableWaiting(false);
  21001. if (!this.#canvas) {
  21002. return;
  21003. }
  21004. if (this._uiManager.useNewAltTextWhenAddingImage && this._uiManager.useNewAltTextFlow && this.#bitmap) {
  21005. this._editToolbar.hide();
  21006. this._uiManager.editAltText(this, true);
  21007. return;
  21008. }
  21009. if (!this._uiManager.useNewAltTextWhenAddingImage && this._uiManager.useNewAltTextFlow && this.#bitmap) {
  21010. this._reportTelemetry({
  21011. action: "pdfjs.image.image_added",
  21012. data: {
  21013. alt_text_modal: false,
  21014. alt_text_type: "empty"
  21015. }
  21016. });
  21017. try {
  21018. this.mlGuessAltText();
  21019. } catch {}
  21020. }
  21021. this.div.focus();
  21022. }
  21023. async mlGuessAltText(imageData = null, updateAltTextData = true) {
  21024. if (this.hasAltTextData()) {
  21025. return null;
  21026. }
  21027. const {
  21028. mlManager
  21029. } = this._uiManager;
  21030. if (!mlManager) {
  21031. throw new Error("No ML.");
  21032. }
  21033. if (!(await mlManager.isEnabledFor("altText"))) {
  21034. throw new Error("ML isn't enabled for alt text.");
  21035. }
  21036. const {
  21037. data,
  21038. width,
  21039. height
  21040. } = imageData || this.copyCanvas(null, null, true).imageData;
  21041. const response = await mlManager.guess({
  21042. name: "altText",
  21043. request: {
  21044. data,
  21045. width,
  21046. height,
  21047. channels: data.length / (width * height)
  21048. }
  21049. });
  21050. if (!response) {
  21051. throw new Error("No response from the AI service.");
  21052. }
  21053. if (response.error) {
  21054. throw new Error("Error from the AI service.");
  21055. }
  21056. if (response.cancel) {
  21057. return null;
  21058. }
  21059. if (!response.output) {
  21060. throw new Error("No valid response from the AI service.");
  21061. }
  21062. const altText = response.output;
  21063. await this.setGuessedAltText(altText);
  21064. if (updateAltTextData && !this.hasAltTextData()) {
  21065. this.altTextData = {
  21066. alt: altText,
  21067. decorative: false
  21068. };
  21069. }
  21070. return altText;
  21071. }
  21072. #getBitmap() {
  21073. if (this.#bitmapId) {
  21074. this._uiManager.enableWaiting(true);
  21075. this._uiManager.imageManager.getFromId(this.#bitmapId).then(data => this.#getBitmapFetched(data, true)).finally(() => this.#getBitmapDone());
  21076. return;
  21077. }
  21078. if (this.#bitmapUrl) {
  21079. const url = this.#bitmapUrl;
  21080. this.#bitmapUrl = null;
  21081. this._uiManager.enableWaiting(true);
  21082. this.#bitmapPromise = this._uiManager.imageManager.getFromUrl(url).then(data => this.#getBitmapFetched(data)).finally(() => this.#getBitmapDone());
  21083. return;
  21084. }
  21085. if (this.#bitmapFile) {
  21086. const file = this.#bitmapFile;
  21087. this.#bitmapFile = null;
  21088. this._uiManager.enableWaiting(true);
  21089. this.#bitmapPromise = this._uiManager.imageManager.getFromFile(file).then(data => this.#getBitmapFetched(data)).finally(() => this.#getBitmapDone());
  21090. return;
  21091. }
  21092. const input = document.createElement("input");
  21093. input.type = "file";
  21094. input.accept = SupportedImageMimeTypes.join(",");
  21095. const signal = this._uiManager._signal;
  21096. this.#bitmapPromise = new Promise(resolve => {
  21097. input.addEventListener("change", async () => {
  21098. if (!input.files || input.files.length === 0) {
  21099. this.remove();
  21100. } else {
  21101. this._uiManager.enableWaiting(true);
  21102. const data = await this._uiManager.imageManager.getFromFile(input.files[0]);
  21103. this._reportTelemetry({
  21104. action: "pdfjs.image.image_selected",
  21105. data: {
  21106. alt_text_modal: this._uiManager.useNewAltTextFlow
  21107. }
  21108. });
  21109. this.#getBitmapFetched(data);
  21110. }
  21111. resolve();
  21112. }, {
  21113. signal
  21114. });
  21115. input.addEventListener("cancel", () => {
  21116. this.remove();
  21117. resolve();
  21118. }, {
  21119. signal
  21120. });
  21121. }).finally(() => this.#getBitmapDone());
  21122. input.click();
  21123. }
  21124. remove() {
  21125. if (this.#bitmapId) {
  21126. this.#bitmap = null;
  21127. this._uiManager.imageManager.deleteId(this.#bitmapId);
  21128. this.#canvas?.remove();
  21129. this.#canvas = null;
  21130. if (this.#resizeTimeoutId) {
  21131. clearTimeout(this.#resizeTimeoutId);
  21132. this.#resizeTimeoutId = null;
  21133. }
  21134. }
  21135. super.remove();
  21136. }
  21137. rebuild() {
  21138. if (!this.parent) {
  21139. if (this.#bitmapId) {
  21140. this.#getBitmap();
  21141. }
  21142. return;
  21143. }
  21144. super.rebuild();
  21145. if (this.div === null) {
  21146. return;
  21147. }
  21148. if (this.#bitmapId && this.#canvas === null) {
  21149. this.#getBitmap();
  21150. }
  21151. if (!this.isAttachedToDOM) {
  21152. this.parent.add(this);
  21153. }
  21154. }
  21155. onceAdded(focus) {
  21156. this._isDraggable = true;
  21157. if (focus) {
  21158. this.div.focus();
  21159. }
  21160. }
  21161. isEmpty() {
  21162. return !(this.#bitmapPromise || this.#bitmap || this.#bitmapUrl || this.#bitmapFile || this.#bitmapId || this.#missingCanvas);
  21163. }
  21164. get isResizable() {
  21165. return true;
  21166. }
  21167. render() {
  21168. if (this.div) {
  21169. return this.div;
  21170. }
  21171. let baseX, baseY;
  21172. if (this._isCopy) {
  21173. baseX = this.x;
  21174. baseY = this.y;
  21175. }
  21176. super.render();
  21177. this.div.hidden = true;
  21178. this.addAltTextButton();
  21179. if (!this.#missingCanvas) {
  21180. if (this.#bitmap) {
  21181. this.#createCanvas();
  21182. } else {
  21183. this.#getBitmap();
  21184. }
  21185. }
  21186. if (this._isCopy) {
  21187. this._moveAfterPaste(baseX, baseY);
  21188. }
  21189. this._uiManager.addShouldRescale(this);
  21190. return this.div;
  21191. }
  21192. setCanvas(annotationElementId, canvas) {
  21193. const {
  21194. id: bitmapId,
  21195. bitmap
  21196. } = this._uiManager.imageManager.getFromCanvas(annotationElementId, canvas);
  21197. canvas.remove();
  21198. if (bitmapId && this._uiManager.imageManager.isValidId(bitmapId)) {
  21199. this.#bitmapId = bitmapId;
  21200. if (bitmap) {
  21201. this.#bitmap = bitmap;
  21202. }
  21203. this.#missingCanvas = false;
  21204. this.#createCanvas();
  21205. }
  21206. }
  21207. _onResized() {
  21208. this.onScaleChanging();
  21209. }
  21210. onScaleChanging() {
  21211. if (!this.parent) {
  21212. return;
  21213. }
  21214. if (this.#resizeTimeoutId !== null) {
  21215. clearTimeout(this.#resizeTimeoutId);
  21216. }
  21217. const TIME_TO_WAIT = 200;
  21218. this.#resizeTimeoutId = setTimeout(() => {
  21219. this.#resizeTimeoutId = null;
  21220. this.#drawBitmap();
  21221. }, TIME_TO_WAIT);
  21222. }
  21223. #createCanvas() {
  21224. const {
  21225. div
  21226. } = this;
  21227. let {
  21228. width,
  21229. height
  21230. } = this.#bitmap;
  21231. const [pageWidth, pageHeight] = this.pageDimensions;
  21232. const MAX_RATIO = 0.75;
  21233. if (this.width) {
  21234. width = this.width * pageWidth;
  21235. height = this.height * pageHeight;
  21236. } else if (width > MAX_RATIO * pageWidth || height > MAX_RATIO * pageHeight) {
  21237. const factor = Math.min(MAX_RATIO * pageWidth / width, MAX_RATIO * pageHeight / height);
  21238. width *= factor;
  21239. height *= factor;
  21240. }
  21241. const [parentWidth, parentHeight] = this.parentDimensions;
  21242. this.setDims(width * parentWidth / pageWidth, height * parentHeight / pageHeight);
  21243. this._uiManager.enableWaiting(false);
  21244. const canvas = this.#canvas = document.createElement("canvas");
  21245. canvas.setAttribute("role", "img");
  21246. this.addContainer(canvas);
  21247. this.width = width / pageWidth;
  21248. this.height = height / pageHeight;
  21249. if (this._initialOptions?.isCentered) {
  21250. this.center();
  21251. } else {
  21252. this.fixAndSetPosition();
  21253. }
  21254. this._initialOptions = null;
  21255. if (!this._uiManager.useNewAltTextWhenAddingImage || !this._uiManager.useNewAltTextFlow || this.annotationElementId) {
  21256. div.hidden = false;
  21257. }
  21258. this.#drawBitmap();
  21259. if (!this.#hasBeenAddedInUndoStack) {
  21260. this.parent.addUndoableEditor(this);
  21261. this.#hasBeenAddedInUndoStack = true;
  21262. }
  21263. this._reportTelemetry({
  21264. action: "inserted_image"
  21265. });
  21266. if (this.#bitmapFileName) {
  21267. this.div.setAttribute("aria-description", this.#bitmapFileName);
  21268. }
  21269. }
  21270. copyCanvas(maxDataDimension, maxPreviewDimension, createImageData = false) {
  21271. if (!maxDataDimension) {
  21272. maxDataDimension = 224;
  21273. }
  21274. const {
  21275. width: bitmapWidth,
  21276. height: bitmapHeight
  21277. } = this.#bitmap;
  21278. const outputScale = new OutputScale();
  21279. let bitmap = this.#bitmap;
  21280. let width = bitmapWidth,
  21281. height = bitmapHeight;
  21282. let canvas = null;
  21283. if (maxPreviewDimension) {
  21284. if (bitmapWidth > maxPreviewDimension || bitmapHeight > maxPreviewDimension) {
  21285. const ratio = Math.min(maxPreviewDimension / bitmapWidth, maxPreviewDimension / bitmapHeight);
  21286. width = Math.floor(bitmapWidth * ratio);
  21287. height = Math.floor(bitmapHeight * ratio);
  21288. }
  21289. canvas = document.createElement("canvas");
  21290. const scaledWidth = canvas.width = Math.ceil(width * outputScale.sx);
  21291. const scaledHeight = canvas.height = Math.ceil(height * outputScale.sy);
  21292. if (!this.#isSvg) {
  21293. bitmap = this.#scaleBitmap(scaledWidth, scaledHeight);
  21294. }
  21295. const ctx = canvas.getContext("2d");
  21296. ctx.filter = this._uiManager.hcmFilter;
  21297. let white = "white",
  21298. black = "#cfcfd8";
  21299. if (this._uiManager.hcmFilter !== "none") {
  21300. black = "black";
  21301. } else if (window.matchMedia?.("(prefers-color-scheme: dark)").matches) {
  21302. white = "#8f8f9d";
  21303. black = "#42414d";
  21304. }
  21305. const boxDim = 15;
  21306. const boxDimWidth = boxDim * outputScale.sx;
  21307. const boxDimHeight = boxDim * outputScale.sy;
  21308. const pattern = new OffscreenCanvas(boxDimWidth * 2, boxDimHeight * 2);
  21309. const patternCtx = pattern.getContext("2d");
  21310. patternCtx.fillStyle = white;
  21311. patternCtx.fillRect(0, 0, boxDimWidth * 2, boxDimHeight * 2);
  21312. patternCtx.fillStyle = black;
  21313. patternCtx.fillRect(0, 0, boxDimWidth, boxDimHeight);
  21314. patternCtx.fillRect(boxDimWidth, boxDimHeight, boxDimWidth, boxDimHeight);
  21315. ctx.fillStyle = ctx.createPattern(pattern, "repeat");
  21316. ctx.fillRect(0, 0, scaledWidth, scaledHeight);
  21317. ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, scaledWidth, scaledHeight);
  21318. }
  21319. let imageData = null;
  21320. if (createImageData) {
  21321. let dataWidth, dataHeight;
  21322. if (outputScale.symmetric && bitmap.width < maxDataDimension && bitmap.height < maxDataDimension) {
  21323. dataWidth = bitmap.width;
  21324. dataHeight = bitmap.height;
  21325. } else {
  21326. bitmap = this.#bitmap;
  21327. if (bitmapWidth > maxDataDimension || bitmapHeight > maxDataDimension) {
  21328. const ratio = Math.min(maxDataDimension / bitmapWidth, maxDataDimension / bitmapHeight);
  21329. dataWidth = Math.floor(bitmapWidth * ratio);
  21330. dataHeight = Math.floor(bitmapHeight * ratio);
  21331. if (!this.#isSvg) {
  21332. bitmap = this.#scaleBitmap(dataWidth, dataHeight);
  21333. }
  21334. }
  21335. }
  21336. const offscreen = new OffscreenCanvas(dataWidth, dataHeight);
  21337. const offscreenCtx = offscreen.getContext("2d", {
  21338. willReadFrequently: true
  21339. });
  21340. offscreenCtx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, dataWidth, dataHeight);
  21341. imageData = {
  21342. width: dataWidth,
  21343. height: dataHeight,
  21344. data: offscreenCtx.getImageData(0, 0, dataWidth, dataHeight).data
  21345. };
  21346. }
  21347. return {
  21348. canvas,
  21349. width,
  21350. height,
  21351. imageData
  21352. };
  21353. }
  21354. #scaleBitmap(width, height) {
  21355. const {
  21356. width: bitmapWidth,
  21357. height: bitmapHeight
  21358. } = this.#bitmap;
  21359. let newWidth = bitmapWidth;
  21360. let newHeight = bitmapHeight;
  21361. let bitmap = this.#bitmap;
  21362. while (newWidth > 2 * width || newHeight > 2 * height) {
  21363. const prevWidth = newWidth;
  21364. const prevHeight = newHeight;
  21365. if (newWidth > 2 * width) {
  21366. newWidth = newWidth >= 16384 ? Math.floor(newWidth / 2) - 1 : Math.ceil(newWidth / 2);
  21367. }
  21368. if (newHeight > 2 * height) {
  21369. newHeight = newHeight >= 16384 ? Math.floor(newHeight / 2) - 1 : Math.ceil(newHeight / 2);
  21370. }
  21371. const offscreen = new OffscreenCanvas(newWidth, newHeight);
  21372. const ctx = offscreen.getContext("2d");
  21373. ctx.drawImage(bitmap, 0, 0, prevWidth, prevHeight, 0, 0, newWidth, newHeight);
  21374. bitmap = offscreen.transferToImageBitmap();
  21375. }
  21376. return bitmap;
  21377. }
  21378. #drawBitmap() {
  21379. const [parentWidth, parentHeight] = this.parentDimensions;
  21380. const {
  21381. width,
  21382. height
  21383. } = this;
  21384. const outputScale = new OutputScale();
  21385. const scaledWidth = Math.ceil(width * parentWidth * outputScale.sx);
  21386. const scaledHeight = Math.ceil(height * parentHeight * outputScale.sy);
  21387. const canvas = this.#canvas;
  21388. if (!canvas || canvas.width === scaledWidth && canvas.height === scaledHeight) {
  21389. return;
  21390. }
  21391. canvas.width = scaledWidth;
  21392. canvas.height = scaledHeight;
  21393. const bitmap = this.#isSvg ? this.#bitmap : this.#scaleBitmap(scaledWidth, scaledHeight);
  21394. const ctx = canvas.getContext("2d");
  21395. ctx.filter = this._uiManager.hcmFilter;
  21396. ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, scaledWidth, scaledHeight);
  21397. }
  21398. #serializeBitmap(toUrl) {
  21399. if (toUrl) {
  21400. if (this.#isSvg) {
  21401. const url = this._uiManager.imageManager.getSvgUrl(this.#bitmapId);
  21402. if (url) {
  21403. return url;
  21404. }
  21405. }
  21406. const canvas = document.createElement("canvas");
  21407. ({
  21408. width: canvas.width,
  21409. height: canvas.height
  21410. } = this.#bitmap);
  21411. const ctx = canvas.getContext("2d");
  21412. ctx.drawImage(this.#bitmap, 0, 0);
  21413. return canvas.toDataURL();
  21414. }
  21415. if (this.#isSvg) {
  21416. const [pageWidth, pageHeight] = this.pageDimensions;
  21417. const width = Math.round(this.width * pageWidth * PixelsPerInch.PDF_TO_CSS_UNITS);
  21418. const height = Math.round(this.height * pageHeight * PixelsPerInch.PDF_TO_CSS_UNITS);
  21419. const offscreen = new OffscreenCanvas(width, height);
  21420. const ctx = offscreen.getContext("2d");
  21421. ctx.drawImage(this.#bitmap, 0, 0, this.#bitmap.width, this.#bitmap.height, 0, 0, width, height);
  21422. return offscreen.transferToImageBitmap();
  21423. }
  21424. return structuredClone(this.#bitmap);
  21425. }
  21426. static async deserialize(data, parent, uiManager) {
  21427. let initialData = null;
  21428. let missingCanvas = false;
  21429. if (data instanceof StampAnnotationElement) {
  21430. const {
  21431. data: {
  21432. rect,
  21433. rotation,
  21434. id,
  21435. structParent,
  21436. popupRef
  21437. },
  21438. container,
  21439. parent: {
  21440. page: {
  21441. pageNumber
  21442. }
  21443. },
  21444. canvas
  21445. } = data;
  21446. let bitmapId, bitmap;
  21447. if (canvas) {
  21448. delete data.canvas;
  21449. ({
  21450. id: bitmapId,
  21451. bitmap
  21452. } = uiManager.imageManager.getFromCanvas(container.id, canvas));
  21453. canvas.remove();
  21454. } else {
  21455. missingCanvas = true;
  21456. data._hasNoCanvas = true;
  21457. }
  21458. const altText = (await parent._structTree.getAriaAttributes(`${AnnotationPrefix}${id}`))?.get("aria-label") || "";
  21459. initialData = data = {
  21460. annotationType: AnnotationEditorType.STAMP,
  21461. bitmapId,
  21462. bitmap,
  21463. pageIndex: pageNumber - 1,
  21464. rect: rect.slice(0),
  21465. rotation,
  21466. id,
  21467. deleted: false,
  21468. accessibilityData: {
  21469. decorative: false,
  21470. altText
  21471. },
  21472. isSvg: false,
  21473. structParent,
  21474. popupRef
  21475. };
  21476. }
  21477. const editor = await super.deserialize(data, parent, uiManager);
  21478. const {
  21479. rect,
  21480. bitmap,
  21481. bitmapUrl,
  21482. bitmapId,
  21483. isSvg,
  21484. accessibilityData
  21485. } = data;
  21486. if (missingCanvas) {
  21487. uiManager.addMissingCanvas(data.id, editor);
  21488. editor.#missingCanvas = true;
  21489. } else if (bitmapId && uiManager.imageManager.isValidId(bitmapId)) {
  21490. editor.#bitmapId = bitmapId;
  21491. if (bitmap) {
  21492. editor.#bitmap = bitmap;
  21493. }
  21494. } else {
  21495. editor.#bitmapUrl = bitmapUrl;
  21496. }
  21497. editor.#isSvg = isSvg;
  21498. const [parentWidth, parentHeight] = editor.pageDimensions;
  21499. editor.width = (rect[2] - rect[0]) / parentWidth;
  21500. editor.height = (rect[3] - rect[1]) / parentHeight;
  21501. editor.annotationElementId = data.id || null;
  21502. if (accessibilityData) {
  21503. editor.altTextData = accessibilityData;
  21504. }
  21505. editor._initialData = initialData;
  21506. editor.#hasBeenAddedInUndoStack = !!initialData;
  21507. return editor;
  21508. }
  21509. serialize(isForCopying = false, context = null) {
  21510. if (this.isEmpty()) {
  21511. return null;
  21512. }
  21513. if (this.deleted) {
  21514. return this.serializeDeleted();
  21515. }
  21516. const serialized = {
  21517. annotationType: AnnotationEditorType.STAMP,
  21518. bitmapId: this.#bitmapId,
  21519. pageIndex: this.pageIndex,
  21520. rect: this.getRect(0, 0),
  21521. rotation: this.rotation,
  21522. isSvg: this.#isSvg,
  21523. structTreeParentId: this._structTreeParentId
  21524. };
  21525. if (isForCopying) {
  21526. serialized.bitmapUrl = this.#serializeBitmap(true);
  21527. serialized.accessibilityData = this.serializeAltText(true);
  21528. serialized.isCopy = true;
  21529. return serialized;
  21530. }
  21531. const {
  21532. decorative,
  21533. altText
  21534. } = this.serializeAltText(false);
  21535. if (!decorative && altText) {
  21536. serialized.accessibilityData = {
  21537. type: "Figure",
  21538. alt: altText
  21539. };
  21540. }
  21541. if (this.annotationElementId) {
  21542. const changes = this.#hasElementChanged(serialized);
  21543. if (changes.isSame) {
  21544. return null;
  21545. }
  21546. if (changes.isSameAltText) {
  21547. delete serialized.accessibilityData;
  21548. } else {
  21549. serialized.accessibilityData.structParent = this._initialData.structParent ?? -1;
  21550. }
  21551. }
  21552. serialized.id = this.annotationElementId;
  21553. if (context === null) {
  21554. return serialized;
  21555. }
  21556. context.stamps ||= new Map();
  21557. const area = this.#isSvg ? (serialized.rect[2] - serialized.rect[0]) * (serialized.rect[3] - serialized.rect[1]) : null;
  21558. if (!context.stamps.has(this.#bitmapId)) {
  21559. context.stamps.set(this.#bitmapId, {
  21560. area,
  21561. serialized
  21562. });
  21563. serialized.bitmap = this.#serializeBitmap(false);
  21564. } else if (this.#isSvg) {
  21565. const prevData = context.stamps.get(this.#bitmapId);
  21566. if (area > prevData.area) {
  21567. prevData.area = area;
  21568. prevData.serialized.bitmap.close();
  21569. prevData.serialized.bitmap = this.#serializeBitmap(false);
  21570. }
  21571. }
  21572. return serialized;
  21573. }
  21574. #hasElementChanged(serialized) {
  21575. const {
  21576. pageIndex,
  21577. accessibilityData: {
  21578. altText
  21579. }
  21580. } = this._initialData;
  21581. const isSamePageIndex = serialized.pageIndex === pageIndex;
  21582. const isSameAltText = (serialized.accessibilityData?.alt || "") === altText;
  21583. return {
  21584. isSame: !this._hasBeenMoved && !this._hasBeenResized && isSamePageIndex && isSameAltText,
  21585. isSameAltText
  21586. };
  21587. }
  21588. renderAnnotationElement(annotation) {
  21589. annotation.updateEdited({
  21590. rect: this.getRect(0, 0)
  21591. });
  21592. return null;
  21593. }
  21594. }
  21595. ;// ./src/display/editor/annotation_editor_layer.js
  21596. class AnnotationEditorLayer {
  21597. #accessibilityManager;
  21598. #allowClick = false;
  21599. #annotationLayer = null;
  21600. #clickAC = null;
  21601. #editorFocusTimeoutId = null;
  21602. #editors = new Map();
  21603. #hadPointerDown = false;
  21604. #isDisabling = false;
  21605. #isEnabling = false;
  21606. #drawingAC = null;
  21607. #focusedElement = null;
  21608. #textLayer = null;
  21609. #textSelectionAC = null;
  21610. #uiManager;
  21611. static _initialized = false;
  21612. static #editorTypes = new Map([FreeTextEditor, InkEditor, StampEditor, HighlightEditor, SignatureEditor].map(type => [type._editorType, type]));
  21613. constructor({
  21614. uiManager,
  21615. pageIndex,
  21616. div,
  21617. structTreeLayer,
  21618. accessibilityManager,
  21619. annotationLayer,
  21620. drawLayer,
  21621. textLayer,
  21622. viewport,
  21623. l10n
  21624. }) {
  21625. const editorTypes = [...AnnotationEditorLayer.#editorTypes.values()];
  21626. if (!AnnotationEditorLayer._initialized) {
  21627. AnnotationEditorLayer._initialized = true;
  21628. for (const editorType of editorTypes) {
  21629. editorType.initialize(l10n, uiManager);
  21630. }
  21631. }
  21632. uiManager.registerEditorTypes(editorTypes);
  21633. this.#uiManager = uiManager;
  21634. this.pageIndex = pageIndex;
  21635. this.div = div;
  21636. this.#accessibilityManager = accessibilityManager;
  21637. this.#annotationLayer = annotationLayer;
  21638. this.viewport = viewport;
  21639. this.#textLayer = textLayer;
  21640. this.drawLayer = drawLayer;
  21641. this._structTree = structTreeLayer;
  21642. this.#uiManager.addLayer(this);
  21643. }
  21644. get isEmpty() {
  21645. return this.#editors.size === 0;
  21646. }
  21647. get isInvisible() {
  21648. return this.isEmpty && this.#uiManager.getMode() === AnnotationEditorType.NONE;
  21649. }
  21650. updateToolbar(mode) {
  21651. this.#uiManager.updateToolbar(mode);
  21652. }
  21653. updateMode(mode = this.#uiManager.getMode()) {
  21654. this.#cleanup();
  21655. switch (mode) {
  21656. case AnnotationEditorType.NONE:
  21657. this.disableTextSelection();
  21658. this.togglePointerEvents(false);
  21659. this.toggleAnnotationLayerPointerEvents(true);
  21660. this.disableClick();
  21661. return;
  21662. case AnnotationEditorType.INK:
  21663. this.disableTextSelection();
  21664. this.togglePointerEvents(true);
  21665. this.enableClick();
  21666. break;
  21667. case AnnotationEditorType.HIGHLIGHT:
  21668. this.enableTextSelection();
  21669. this.togglePointerEvents(false);
  21670. this.disableClick();
  21671. break;
  21672. default:
  21673. this.disableTextSelection();
  21674. this.togglePointerEvents(true);
  21675. this.enableClick();
  21676. }
  21677. this.toggleAnnotationLayerPointerEvents(false);
  21678. const {
  21679. classList
  21680. } = this.div;
  21681. for (const editorType of AnnotationEditorLayer.#editorTypes.values()) {
  21682. classList.toggle(`${editorType._type}Editing`, mode === editorType._editorType);
  21683. }
  21684. this.div.hidden = false;
  21685. }
  21686. hasTextLayer(textLayer) {
  21687. return textLayer === this.#textLayer?.div;
  21688. }
  21689. setEditingState(isEditing) {
  21690. this.#uiManager.setEditingState(isEditing);
  21691. }
  21692. addCommands(params) {
  21693. this.#uiManager.addCommands(params);
  21694. }
  21695. cleanUndoStack(type) {
  21696. this.#uiManager.cleanUndoStack(type);
  21697. }
  21698. toggleDrawing(enabled = false) {
  21699. this.div.classList.toggle("drawing", !enabled);
  21700. }
  21701. togglePointerEvents(enabled = false) {
  21702. this.div.classList.toggle("disabled", !enabled);
  21703. }
  21704. toggleAnnotationLayerPointerEvents(enabled = false) {
  21705. this.#annotationLayer?.div.classList.toggle("disabled", !enabled);
  21706. }
  21707. async enable() {
  21708. this.#isEnabling = true;
  21709. this.div.tabIndex = 0;
  21710. this.togglePointerEvents(true);
  21711. const annotationElementIds = new Set();
  21712. for (const editor of this.#editors.values()) {
  21713. editor.enableEditing();
  21714. editor.show(true);
  21715. if (editor.annotationElementId) {
  21716. this.#uiManager.removeChangedExistingAnnotation(editor);
  21717. annotationElementIds.add(editor.annotationElementId);
  21718. }
  21719. }
  21720. if (!this.#annotationLayer) {
  21721. this.#isEnabling = false;
  21722. return;
  21723. }
  21724. const editables = this.#annotationLayer.getEditableAnnotations();
  21725. for (const editable of editables) {
  21726. editable.hide();
  21727. if (this.#uiManager.isDeletedAnnotationElement(editable.data.id)) {
  21728. continue;
  21729. }
  21730. if (annotationElementIds.has(editable.data.id)) {
  21731. continue;
  21732. }
  21733. const editor = await this.deserialize(editable);
  21734. if (!editor) {
  21735. continue;
  21736. }
  21737. this.addOrRebuild(editor);
  21738. editor.enableEditing();
  21739. }
  21740. this.#isEnabling = false;
  21741. }
  21742. disable() {
  21743. this.#isDisabling = true;
  21744. this.div.tabIndex = -1;
  21745. this.togglePointerEvents(false);
  21746. const changedAnnotations = new Map();
  21747. const resetAnnotations = new Map();
  21748. for (const editor of this.#editors.values()) {
  21749. editor.disableEditing();
  21750. if (!editor.annotationElementId) {
  21751. continue;
  21752. }
  21753. if (editor.serialize() !== null) {
  21754. changedAnnotations.set(editor.annotationElementId, editor);
  21755. continue;
  21756. } else {
  21757. resetAnnotations.set(editor.annotationElementId, editor);
  21758. }
  21759. this.getEditableAnnotation(editor.annotationElementId)?.show();
  21760. editor.remove();
  21761. }
  21762. if (this.#annotationLayer) {
  21763. const editables = this.#annotationLayer.getEditableAnnotations();
  21764. for (const editable of editables) {
  21765. const {
  21766. id
  21767. } = editable.data;
  21768. if (this.#uiManager.isDeletedAnnotationElement(id)) {
  21769. continue;
  21770. }
  21771. let editor = resetAnnotations.get(id);
  21772. if (editor) {
  21773. editor.resetAnnotationElement(editable);
  21774. editor.show(false);
  21775. editable.show();
  21776. continue;
  21777. }
  21778. editor = changedAnnotations.get(id);
  21779. if (editor) {
  21780. this.#uiManager.addChangedExistingAnnotation(editor);
  21781. if (editor.renderAnnotationElement(editable)) {
  21782. editor.show(false);
  21783. }
  21784. }
  21785. editable.show();
  21786. }
  21787. }
  21788. this.#cleanup();
  21789. if (this.isEmpty) {
  21790. this.div.hidden = true;
  21791. }
  21792. const {
  21793. classList
  21794. } = this.div;
  21795. for (const editorType of AnnotationEditorLayer.#editorTypes.values()) {
  21796. classList.remove(`${editorType._type}Editing`);
  21797. }
  21798. this.disableTextSelection();
  21799. this.toggleAnnotationLayerPointerEvents(true);
  21800. this.#isDisabling = false;
  21801. }
  21802. getEditableAnnotation(id) {
  21803. return this.#annotationLayer?.getEditableAnnotation(id) || null;
  21804. }
  21805. setActiveEditor(editor) {
  21806. const currentActive = this.#uiManager.getActive();
  21807. if (currentActive === editor) {
  21808. return;
  21809. }
  21810. this.#uiManager.setActiveEditor(editor);
  21811. }
  21812. enableTextSelection() {
  21813. this.div.tabIndex = -1;
  21814. if (this.#textLayer?.div && !this.#textSelectionAC) {
  21815. this.#textSelectionAC = new AbortController();
  21816. const signal = this.#uiManager.combinedSignal(this.#textSelectionAC);
  21817. this.#textLayer.div.addEventListener("pointerdown", this.#textLayerPointerDown.bind(this), {
  21818. signal
  21819. });
  21820. this.#textLayer.div.classList.add("highlighting");
  21821. }
  21822. }
  21823. disableTextSelection() {
  21824. this.div.tabIndex = 0;
  21825. if (this.#textLayer?.div && this.#textSelectionAC) {
  21826. this.#textSelectionAC.abort();
  21827. this.#textSelectionAC = null;
  21828. this.#textLayer.div.classList.remove("highlighting");
  21829. }
  21830. }
  21831. #textLayerPointerDown(event) {
  21832. this.#uiManager.unselectAll();
  21833. const {
  21834. target
  21835. } = event;
  21836. if (target === this.#textLayer.div || (target.getAttribute("role") === "img" || target.classList.contains("endOfContent")) && this.#textLayer.div.contains(target)) {
  21837. const {
  21838. isMac
  21839. } = util_FeatureTest.platform;
  21840. if (event.button !== 0 || event.ctrlKey && isMac) {
  21841. return;
  21842. }
  21843. this.#uiManager.showAllEditors("highlight", true, true);
  21844. this.#textLayer.div.classList.add("free");
  21845. this.toggleDrawing();
  21846. HighlightEditor.startHighlighting(this, this.#uiManager.direction === "ltr", {
  21847. target: this.#textLayer.div,
  21848. x: event.x,
  21849. y: event.y
  21850. });
  21851. this.#textLayer.div.addEventListener("pointerup", () => {
  21852. this.#textLayer.div.classList.remove("free");
  21853. this.toggleDrawing(true);
  21854. }, {
  21855. once: true,
  21856. signal: this.#uiManager._signal
  21857. });
  21858. event.preventDefault();
  21859. }
  21860. }
  21861. enableClick() {
  21862. if (this.#clickAC) {
  21863. return;
  21864. }
  21865. this.#clickAC = new AbortController();
  21866. const signal = this.#uiManager.combinedSignal(this.#clickAC);
  21867. this.div.addEventListener("pointerdown", this.pointerdown.bind(this), {
  21868. signal
  21869. });
  21870. const pointerup = this.pointerup.bind(this);
  21871. this.div.addEventListener("pointerup", pointerup, {
  21872. signal
  21873. });
  21874. this.div.addEventListener("pointercancel", pointerup, {
  21875. signal
  21876. });
  21877. }
  21878. disableClick() {
  21879. this.#clickAC?.abort();
  21880. this.#clickAC = null;
  21881. }
  21882. attach(editor) {
  21883. this.#editors.set(editor.id, editor);
  21884. const {
  21885. annotationElementId
  21886. } = editor;
  21887. if (annotationElementId && this.#uiManager.isDeletedAnnotationElement(annotationElementId)) {
  21888. this.#uiManager.removeDeletedAnnotationElement(editor);
  21889. }
  21890. }
  21891. detach(editor) {
  21892. this.#editors.delete(editor.id);
  21893. this.#accessibilityManager?.removePointerInTextLayer(editor.contentDiv);
  21894. if (!this.#isDisabling && editor.annotationElementId) {
  21895. this.#uiManager.addDeletedAnnotationElement(editor);
  21896. }
  21897. }
  21898. remove(editor) {
  21899. this.detach(editor);
  21900. this.#uiManager.removeEditor(editor);
  21901. editor.div.remove();
  21902. editor.isAttachedToDOM = false;
  21903. }
  21904. changeParent(editor) {
  21905. if (editor.parent === this) {
  21906. return;
  21907. }
  21908. if (editor.parent && editor.annotationElementId) {
  21909. this.#uiManager.addDeletedAnnotationElement(editor.annotationElementId);
  21910. AnnotationEditor.deleteAnnotationElement(editor);
  21911. editor.annotationElementId = null;
  21912. }
  21913. this.attach(editor);
  21914. editor.parent?.detach(editor);
  21915. editor.setParent(this);
  21916. if (editor.div && editor.isAttachedToDOM) {
  21917. editor.div.remove();
  21918. this.div.append(editor.div);
  21919. }
  21920. }
  21921. add(editor) {
  21922. if (editor.parent === this && editor.isAttachedToDOM) {
  21923. return;
  21924. }
  21925. this.changeParent(editor);
  21926. this.#uiManager.addEditor(editor);
  21927. this.attach(editor);
  21928. if (!editor.isAttachedToDOM) {
  21929. const div = editor.render();
  21930. this.div.append(div);
  21931. editor.isAttachedToDOM = true;
  21932. }
  21933. editor.fixAndSetPosition();
  21934. editor.onceAdded(!this.#isEnabling);
  21935. this.#uiManager.addToAnnotationStorage(editor);
  21936. editor._reportTelemetry(editor.telemetryInitialData);
  21937. }
  21938. moveEditorInDOM(editor) {
  21939. if (!editor.isAttachedToDOM) {
  21940. return;
  21941. }
  21942. const {
  21943. activeElement
  21944. } = document;
  21945. if (editor.div.contains(activeElement) && !this.#editorFocusTimeoutId) {
  21946. editor._focusEventsAllowed = false;
  21947. this.#editorFocusTimeoutId = setTimeout(() => {
  21948. this.#editorFocusTimeoutId = null;
  21949. if (!editor.div.contains(document.activeElement)) {
  21950. editor.div.addEventListener("focusin", () => {
  21951. editor._focusEventsAllowed = true;
  21952. }, {
  21953. once: true,
  21954. signal: this.#uiManager._signal
  21955. });
  21956. activeElement.focus();
  21957. } else {
  21958. editor._focusEventsAllowed = true;
  21959. }
  21960. }, 0);
  21961. }
  21962. editor._structTreeParentId = this.#accessibilityManager?.moveElementInDOM(this.div, editor.div, editor.contentDiv, true);
  21963. }
  21964. addOrRebuild(editor) {
  21965. if (editor.needsToBeRebuilt()) {
  21966. editor.parent ||= this;
  21967. editor.rebuild();
  21968. editor.show();
  21969. } else {
  21970. this.add(editor);
  21971. }
  21972. }
  21973. addUndoableEditor(editor) {
  21974. const cmd = () => editor._uiManager.rebuild(editor);
  21975. const undo = () => {
  21976. editor.remove();
  21977. };
  21978. this.addCommands({
  21979. cmd,
  21980. undo,
  21981. mustExec: false
  21982. });
  21983. }
  21984. getNextId() {
  21985. return this.#uiManager.getId();
  21986. }
  21987. get #currentEditorType() {
  21988. return AnnotationEditorLayer.#editorTypes.get(this.#uiManager.getMode());
  21989. }
  21990. combinedSignal(ac) {
  21991. return this.#uiManager.combinedSignal(ac);
  21992. }
  21993. #createNewEditor(params) {
  21994. const editorType = this.#currentEditorType;
  21995. return editorType ? new editorType.prototype.constructor(params) : null;
  21996. }
  21997. canCreateNewEmptyEditor() {
  21998. return this.#currentEditorType?.canCreateNewEmptyEditor();
  21999. }
  22000. async pasteEditor(mode, params) {
  22001. this.#uiManager.updateToolbar(mode);
  22002. await this.#uiManager.updateMode(mode);
  22003. const {
  22004. offsetX,
  22005. offsetY
  22006. } = this.#getCenterPoint();
  22007. const id = this.getNextId();
  22008. const editor = this.#createNewEditor({
  22009. parent: this,
  22010. id,
  22011. x: offsetX,
  22012. y: offsetY,
  22013. uiManager: this.#uiManager,
  22014. isCentered: true,
  22015. ...params
  22016. });
  22017. if (editor) {
  22018. this.add(editor);
  22019. }
  22020. }
  22021. async deserialize(data) {
  22022. return (await AnnotationEditorLayer.#editorTypes.get(data.annotationType ?? data.annotationEditorType)?.deserialize(data, this, this.#uiManager)) || null;
  22023. }
  22024. createAndAddNewEditor(event, isCentered, data = {}) {
  22025. const id = this.getNextId();
  22026. const editor = this.#createNewEditor({
  22027. parent: this,
  22028. id,
  22029. x: event.offsetX,
  22030. y: event.offsetY,
  22031. uiManager: this.#uiManager,
  22032. isCentered,
  22033. ...data
  22034. });
  22035. if (editor) {
  22036. this.add(editor);
  22037. }
  22038. return editor;
  22039. }
  22040. #getCenterPoint() {
  22041. const {
  22042. x,
  22043. y,
  22044. width,
  22045. height
  22046. } = this.div.getBoundingClientRect();
  22047. const tlX = Math.max(0, x);
  22048. const tlY = Math.max(0, y);
  22049. const brX = Math.min(window.innerWidth, x + width);
  22050. const brY = Math.min(window.innerHeight, y + height);
  22051. const centerX = (tlX + brX) / 2 - x;
  22052. const centerY = (tlY + brY) / 2 - y;
  22053. const [offsetX, offsetY] = this.viewport.rotation % 180 === 0 ? [centerX, centerY] : [centerY, centerX];
  22054. return {
  22055. offsetX,
  22056. offsetY
  22057. };
  22058. }
  22059. addNewEditor(data = {}) {
  22060. this.createAndAddNewEditor(this.#getCenterPoint(), true, data);
  22061. }
  22062. setSelected(editor) {
  22063. this.#uiManager.setSelected(editor);
  22064. }
  22065. toggleSelected(editor) {
  22066. this.#uiManager.toggleSelected(editor);
  22067. }
  22068. unselect(editor) {
  22069. this.#uiManager.unselect(editor);
  22070. }
  22071. pointerup(event) {
  22072. const {
  22073. isMac
  22074. } = util_FeatureTest.platform;
  22075. if (event.button !== 0 || event.ctrlKey && isMac) {
  22076. return;
  22077. }
  22078. if (event.target !== this.div) {
  22079. return;
  22080. }
  22081. if (!this.#hadPointerDown) {
  22082. return;
  22083. }
  22084. this.#hadPointerDown = false;
  22085. if (this.#currentEditorType?.isDrawer && this.#currentEditorType.supportMultipleDrawings) {
  22086. return;
  22087. }
  22088. if (!this.#allowClick) {
  22089. this.#allowClick = true;
  22090. return;
  22091. }
  22092. const currentMode = this.#uiManager.getMode();
  22093. if (currentMode === AnnotationEditorType.STAMP || currentMode === AnnotationEditorType.SIGNATURE) {
  22094. this.#uiManager.unselectAll();
  22095. return;
  22096. }
  22097. this.createAndAddNewEditor(event, false);
  22098. }
  22099. pointerdown(event) {
  22100. if (this.#uiManager.getMode() === AnnotationEditorType.HIGHLIGHT) {
  22101. this.enableTextSelection();
  22102. }
  22103. if (this.#hadPointerDown) {
  22104. this.#hadPointerDown = false;
  22105. return;
  22106. }
  22107. const {
  22108. isMac
  22109. } = util_FeatureTest.platform;
  22110. if (event.button !== 0 || event.ctrlKey && isMac) {
  22111. return;
  22112. }
  22113. if (event.target !== this.div) {
  22114. return;
  22115. }
  22116. this.#hadPointerDown = true;
  22117. if (this.#currentEditorType?.isDrawer) {
  22118. this.startDrawingSession(event);
  22119. return;
  22120. }
  22121. const editor = this.#uiManager.getActive();
  22122. this.#allowClick = !editor || editor.isEmpty();
  22123. }
  22124. startDrawingSession(event) {
  22125. this.div.focus({
  22126. preventScroll: true
  22127. });
  22128. if (this.#drawingAC) {
  22129. this.#currentEditorType.startDrawing(this, this.#uiManager, false, event);
  22130. return;
  22131. }
  22132. this.#uiManager.setCurrentDrawingSession(this);
  22133. this.#drawingAC = new AbortController();
  22134. const signal = this.#uiManager.combinedSignal(this.#drawingAC);
  22135. this.div.addEventListener("blur", ({
  22136. relatedTarget
  22137. }) => {
  22138. if (relatedTarget && !this.div.contains(relatedTarget)) {
  22139. this.#focusedElement = null;
  22140. this.commitOrRemove();
  22141. }
  22142. }, {
  22143. signal
  22144. });
  22145. this.#currentEditorType.startDrawing(this, this.#uiManager, false, event);
  22146. }
  22147. pause(on) {
  22148. if (on) {
  22149. const {
  22150. activeElement
  22151. } = document;
  22152. if (this.div.contains(activeElement)) {
  22153. this.#focusedElement = activeElement;
  22154. }
  22155. return;
  22156. }
  22157. if (this.#focusedElement) {
  22158. setTimeout(() => {
  22159. this.#focusedElement?.focus();
  22160. this.#focusedElement = null;
  22161. }, 0);
  22162. }
  22163. }
  22164. endDrawingSession(isAborted = false) {
  22165. if (!this.#drawingAC) {
  22166. return null;
  22167. }
  22168. this.#uiManager.setCurrentDrawingSession(null);
  22169. this.#drawingAC.abort();
  22170. this.#drawingAC = null;
  22171. this.#focusedElement = null;
  22172. return this.#currentEditorType.endDrawing(isAborted);
  22173. }
  22174. findNewParent(editor, x, y) {
  22175. const layer = this.#uiManager.findParent(x, y);
  22176. if (layer === null || layer === this) {
  22177. return false;
  22178. }
  22179. layer.changeParent(editor);
  22180. return true;
  22181. }
  22182. commitOrRemove() {
  22183. if (this.#drawingAC) {
  22184. this.endDrawingSession();
  22185. return true;
  22186. }
  22187. return false;
  22188. }
  22189. onScaleChanging() {
  22190. if (!this.#drawingAC) {
  22191. return;
  22192. }
  22193. this.#currentEditorType.onScaleChangingWhenDrawing(this);
  22194. }
  22195. destroy() {
  22196. this.commitOrRemove();
  22197. if (this.#uiManager.getActive()?.parent === this) {
  22198. this.#uiManager.commitOrRemove();
  22199. this.#uiManager.setActiveEditor(null);
  22200. }
  22201. if (this.#editorFocusTimeoutId) {
  22202. clearTimeout(this.#editorFocusTimeoutId);
  22203. this.#editorFocusTimeoutId = null;
  22204. }
  22205. for (const editor of this.#editors.values()) {
  22206. this.#accessibilityManager?.removePointerInTextLayer(editor.contentDiv);
  22207. editor.setParent(null);
  22208. editor.isAttachedToDOM = false;
  22209. editor.div.remove();
  22210. }
  22211. this.div = null;
  22212. this.#editors.clear();
  22213. this.#uiManager.removeLayer(this);
  22214. }
  22215. #cleanup() {
  22216. for (const editor of this.#editors.values()) {
  22217. if (editor.isEmpty()) {
  22218. editor.remove();
  22219. }
  22220. }
  22221. }
  22222. render({
  22223. viewport
  22224. }) {
  22225. this.viewport = viewport;
  22226. setLayerDimensions(this.div, viewport);
  22227. for (const editor of this.#uiManager.getEditors(this.pageIndex)) {
  22228. this.add(editor);
  22229. editor.rebuild();
  22230. }
  22231. this.updateMode();
  22232. }
  22233. update({
  22234. viewport
  22235. }) {
  22236. this.#uiManager.commitOrRemove();
  22237. this.#cleanup();
  22238. const oldRotation = this.viewport.rotation;
  22239. const rotation = viewport.rotation;
  22240. this.viewport = viewport;
  22241. setLayerDimensions(this.div, {
  22242. rotation
  22243. });
  22244. if (oldRotation !== rotation) {
  22245. for (const editor of this.#editors.values()) {
  22246. editor.rotate(rotation);
  22247. }
  22248. }
  22249. }
  22250. get pageDimensions() {
  22251. const {
  22252. pageWidth,
  22253. pageHeight
  22254. } = this.viewport.rawDims;
  22255. return [pageWidth, pageHeight];
  22256. }
  22257. get scale() {
  22258. return this.#uiManager.viewParameters.realScale;
  22259. }
  22260. }
  22261. ;// ./src/display/draw_layer.js
  22262. class DrawLayer {
  22263. #parent = null;
  22264. #mapping = new Map();
  22265. #toUpdate = new Map();
  22266. static #id = 0;
  22267. constructor({
  22268. pageIndex
  22269. }) {
  22270. this.pageIndex = pageIndex;
  22271. }
  22272. setParent(parent) {
  22273. if (!this.#parent) {
  22274. this.#parent = parent;
  22275. return;
  22276. }
  22277. if (this.#parent !== parent) {
  22278. if (this.#mapping.size > 0) {
  22279. for (const root of this.#mapping.values()) {
  22280. root.remove();
  22281. parent.append(root);
  22282. }
  22283. }
  22284. this.#parent = parent;
  22285. }
  22286. }
  22287. static get _svgFactory() {
  22288. return shadow(this, "_svgFactory", new DOMSVGFactory());
  22289. }
  22290. static #setBox(element, [x, y, width, height]) {
  22291. const {
  22292. style
  22293. } = element;
  22294. style.top = `${100 * y}%`;
  22295. style.left = `${100 * x}%`;
  22296. style.width = `${100 * width}%`;
  22297. style.height = `${100 * height}%`;
  22298. }
  22299. #createSVG() {
  22300. const svg = DrawLayer._svgFactory.create(1, 1, true);
  22301. this.#parent.append(svg);
  22302. svg.setAttribute("aria-hidden", true);
  22303. return svg;
  22304. }
  22305. #createClipPath(defs, pathId) {
  22306. const clipPath = DrawLayer._svgFactory.createElement("clipPath");
  22307. defs.append(clipPath);
  22308. const clipPathId = `clip_${pathId}`;
  22309. clipPath.setAttribute("id", clipPathId);
  22310. clipPath.setAttribute("clipPathUnits", "objectBoundingBox");
  22311. const clipPathUse = DrawLayer._svgFactory.createElement("use");
  22312. clipPath.append(clipPathUse);
  22313. clipPathUse.setAttribute("href", `#${pathId}`);
  22314. clipPathUse.classList.add("clip");
  22315. return clipPathId;
  22316. }
  22317. #updateProperties(element, properties) {
  22318. for (const [key, value] of Object.entries(properties)) {
  22319. if (value === null) {
  22320. element.removeAttribute(key);
  22321. } else {
  22322. element.setAttribute(key, value);
  22323. }
  22324. }
  22325. }
  22326. draw(properties, isPathUpdatable = false, hasClip = false) {
  22327. const id = DrawLayer.#id++;
  22328. const root = this.#createSVG();
  22329. const defs = DrawLayer._svgFactory.createElement("defs");
  22330. root.append(defs);
  22331. const path = DrawLayer._svgFactory.createElement("path");
  22332. defs.append(path);
  22333. const pathId = `path_p${this.pageIndex}_${id}`;
  22334. path.setAttribute("id", pathId);
  22335. path.setAttribute("vector-effect", "non-scaling-stroke");
  22336. if (isPathUpdatable) {
  22337. this.#toUpdate.set(id, path);
  22338. }
  22339. const clipPathId = hasClip ? this.#createClipPath(defs, pathId) : null;
  22340. const use = DrawLayer._svgFactory.createElement("use");
  22341. root.append(use);
  22342. use.setAttribute("href", `#${pathId}`);
  22343. this.updateProperties(root, properties);
  22344. this.#mapping.set(id, root);
  22345. return {
  22346. id,
  22347. clipPathId: `url(#${clipPathId})`
  22348. };
  22349. }
  22350. drawOutline(properties, mustRemoveSelfIntersections) {
  22351. const id = DrawLayer.#id++;
  22352. const root = this.#createSVG();
  22353. const defs = DrawLayer._svgFactory.createElement("defs");
  22354. root.append(defs);
  22355. const path = DrawLayer._svgFactory.createElement("path");
  22356. defs.append(path);
  22357. const pathId = `path_p${this.pageIndex}_${id}`;
  22358. path.setAttribute("id", pathId);
  22359. path.setAttribute("vector-effect", "non-scaling-stroke");
  22360. let maskId;
  22361. if (mustRemoveSelfIntersections) {
  22362. const mask = DrawLayer._svgFactory.createElement("mask");
  22363. defs.append(mask);
  22364. maskId = `mask_p${this.pageIndex}_${id}`;
  22365. mask.setAttribute("id", maskId);
  22366. mask.setAttribute("maskUnits", "objectBoundingBox");
  22367. const rect = DrawLayer._svgFactory.createElement("rect");
  22368. mask.append(rect);
  22369. rect.setAttribute("width", "1");
  22370. rect.setAttribute("height", "1");
  22371. rect.setAttribute("fill", "white");
  22372. const use = DrawLayer._svgFactory.createElement("use");
  22373. mask.append(use);
  22374. use.setAttribute("href", `#${pathId}`);
  22375. use.setAttribute("stroke", "none");
  22376. use.setAttribute("fill", "black");
  22377. use.setAttribute("fill-rule", "nonzero");
  22378. use.classList.add("mask");
  22379. }
  22380. const use1 = DrawLayer._svgFactory.createElement("use");
  22381. root.append(use1);
  22382. use1.setAttribute("href", `#${pathId}`);
  22383. if (maskId) {
  22384. use1.setAttribute("mask", `url(#${maskId})`);
  22385. }
  22386. const use2 = use1.cloneNode();
  22387. root.append(use2);
  22388. use1.classList.add("mainOutline");
  22389. use2.classList.add("secondaryOutline");
  22390. this.updateProperties(root, properties);
  22391. this.#mapping.set(id, root);
  22392. return id;
  22393. }
  22394. finalizeDraw(id, properties) {
  22395. this.#toUpdate.delete(id);
  22396. this.updateProperties(id, properties);
  22397. }
  22398. updateProperties(elementOrId, properties) {
  22399. if (!properties) {
  22400. return;
  22401. }
  22402. const {
  22403. root,
  22404. bbox,
  22405. rootClass,
  22406. path
  22407. } = properties;
  22408. const element = typeof elementOrId === "number" ? this.#mapping.get(elementOrId) : elementOrId;
  22409. if (!element) {
  22410. return;
  22411. }
  22412. if (root) {
  22413. this.#updateProperties(element, root);
  22414. }
  22415. if (bbox) {
  22416. DrawLayer.#setBox(element, bbox);
  22417. }
  22418. if (rootClass) {
  22419. const {
  22420. classList
  22421. } = element;
  22422. for (const [className, value] of Object.entries(rootClass)) {
  22423. classList.toggle(className, value);
  22424. }
  22425. }
  22426. if (path) {
  22427. const defs = element.firstChild;
  22428. const pathElement = defs.firstChild;
  22429. this.#updateProperties(pathElement, path);
  22430. }
  22431. }
  22432. updateParent(id, layer) {
  22433. if (layer === this) {
  22434. return;
  22435. }
  22436. const root = this.#mapping.get(id);
  22437. if (!root) {
  22438. return;
  22439. }
  22440. layer.#parent.append(root);
  22441. this.#mapping.delete(id);
  22442. layer.#mapping.set(id, root);
  22443. }
  22444. remove(id) {
  22445. this.#toUpdate.delete(id);
  22446. if (this.#parent === null) {
  22447. return;
  22448. }
  22449. this.#mapping.get(id).remove();
  22450. this.#mapping.delete(id);
  22451. }
  22452. destroy() {
  22453. this.#parent = null;
  22454. for (const root of this.#mapping.values()) {
  22455. root.remove();
  22456. }
  22457. this.#mapping.clear();
  22458. this.#toUpdate.clear();
  22459. }
  22460. }
  22461. ;// ./src/pdf.js
  22462. const pdfjsVersion = "5.1.91";
  22463. const pdfjsBuild = "45cbe8bb0";
  22464. {
  22465. globalThis.pdfjsTestingUtils = {
  22466. HighlightOutliner: HighlightOutliner
  22467. };
  22468. }
  22469. var __webpack_exports__AbortException = __webpack_exports__.AbortException;
  22470. var __webpack_exports__AnnotationEditorLayer = __webpack_exports__.AnnotationEditorLayer;
  22471. var __webpack_exports__AnnotationEditorParamsType = __webpack_exports__.AnnotationEditorParamsType;
  22472. var __webpack_exports__AnnotationEditorType = __webpack_exports__.AnnotationEditorType;
  22473. var __webpack_exports__AnnotationEditorUIManager = __webpack_exports__.AnnotationEditorUIManager;
  22474. var __webpack_exports__AnnotationLayer = __webpack_exports__.AnnotationLayer;
  22475. var __webpack_exports__AnnotationMode = __webpack_exports__.AnnotationMode;
  22476. var __webpack_exports__AnnotationType = __webpack_exports__.AnnotationType;
  22477. var __webpack_exports__ColorPicker = __webpack_exports__.ColorPicker;
  22478. var __webpack_exports__DOMSVGFactory = __webpack_exports__.DOMSVGFactory;
  22479. var __webpack_exports__DrawLayer = __webpack_exports__.DrawLayer;
  22480. var __webpack_exports__FeatureTest = __webpack_exports__.FeatureTest;
  22481. var __webpack_exports__GlobalWorkerOptions = __webpack_exports__.GlobalWorkerOptions;
  22482. var __webpack_exports__ImageKind = __webpack_exports__.ImageKind;
  22483. var __webpack_exports__InvalidPDFException = __webpack_exports__.InvalidPDFException;
  22484. var __webpack_exports__MathClamp = __webpack_exports__.MathClamp;
  22485. var __webpack_exports__OPS = __webpack_exports__.OPS;
  22486. var __webpack_exports__OutputScale = __webpack_exports__.OutputScale;
  22487. var __webpack_exports__PDFDataRangeTransport = __webpack_exports__.PDFDataRangeTransport;
  22488. var __webpack_exports__PDFDateString = __webpack_exports__.PDFDateString;
  22489. var __webpack_exports__PDFWorker = __webpack_exports__.PDFWorker;
  22490. var __webpack_exports__PasswordResponses = __webpack_exports__.PasswordResponses;
  22491. var __webpack_exports__PermissionFlag = __webpack_exports__.PermissionFlag;
  22492. var __webpack_exports__PixelsPerInch = __webpack_exports__.PixelsPerInch;
  22493. var __webpack_exports__RenderingCancelledException = __webpack_exports__.RenderingCancelledException;
  22494. var __webpack_exports__ResponseException = __webpack_exports__.ResponseException;
  22495. var __webpack_exports__SignatureExtractor = __webpack_exports__.SignatureExtractor;
  22496. var __webpack_exports__SupportedImageMimeTypes = __webpack_exports__.SupportedImageMimeTypes;
  22497. var __webpack_exports__TextLayer = __webpack_exports__.TextLayer;
  22498. var __webpack_exports__TouchManager = __webpack_exports__.TouchManager;
  22499. var __webpack_exports__Util = __webpack_exports__.Util;
  22500. var __webpack_exports__VerbosityLevel = __webpack_exports__.VerbosityLevel;
  22501. var __webpack_exports__XfaLayer = __webpack_exports__.XfaLayer;
  22502. var __webpack_exports__build = __webpack_exports__.build;
  22503. var __webpack_exports__createValidAbsoluteUrl = __webpack_exports__.createValidAbsoluteUrl;
  22504. var __webpack_exports__fetchData = __webpack_exports__.fetchData;
  22505. var __webpack_exports__getDocument = __webpack_exports__.getDocument;
  22506. var __webpack_exports__getFilenameFromUrl = __webpack_exports__.getFilenameFromUrl;
  22507. var __webpack_exports__getPdfFilenameFromUrl = __webpack_exports__.getPdfFilenameFromUrl;
  22508. var __webpack_exports__getUuid = __webpack_exports__.getUuid;
  22509. var __webpack_exports__getXfaPageViewport = __webpack_exports__.getXfaPageViewport;
  22510. var __webpack_exports__isDataScheme = __webpack_exports__.isDataScheme;
  22511. var __webpack_exports__isPdfFile = __webpack_exports__.isPdfFile;
  22512. var __webpack_exports__isValidExplicitDest = __webpack_exports__.isValidExplicitDest;
  22513. var __webpack_exports__noContextMenu = __webpack_exports__.noContextMenu;
  22514. var __webpack_exports__normalizeUnicode = __webpack_exports__.normalizeUnicode;
  22515. var __webpack_exports__setLayerDimensions = __webpack_exports__.setLayerDimensions;
  22516. var __webpack_exports__shadow = __webpack_exports__.shadow;
  22517. var __webpack_exports__stopEvent = __webpack_exports__.stopEvent;
  22518. var __webpack_exports__version = __webpack_exports__.version;
  22519. export { __webpack_exports__AbortException as AbortException, __webpack_exports__AnnotationEditorLayer as AnnotationEditorLayer, __webpack_exports__AnnotationEditorParamsType as AnnotationEditorParamsType, __webpack_exports__AnnotationEditorType as AnnotationEditorType, __webpack_exports__AnnotationEditorUIManager as AnnotationEditorUIManager, __webpack_exports__AnnotationLayer as AnnotationLayer, __webpack_exports__AnnotationMode as AnnotationMode, __webpack_exports__AnnotationType as AnnotationType, __webpack_exports__ColorPicker as ColorPicker, __webpack_exports__DOMSVGFactory as DOMSVGFactory, __webpack_exports__DrawLayer as DrawLayer, __webpack_exports__FeatureTest as FeatureTest, __webpack_exports__GlobalWorkerOptions as GlobalWorkerOptions, __webpack_exports__ImageKind as ImageKind, __webpack_exports__InvalidPDFException as InvalidPDFException, __webpack_exports__MathClamp as MathClamp, __webpack_exports__OPS as OPS, __webpack_exports__OutputScale as OutputScale, __webpack_exports__PDFDataRangeTransport as PDFDataRangeTransport, __webpack_exports__PDFDateString as PDFDateString, __webpack_exports__PDFWorker as PDFWorker, __webpack_exports__PasswordResponses as PasswordResponses, __webpack_exports__PermissionFlag as PermissionFlag, __webpack_exports__PixelsPerInch as PixelsPerInch, __webpack_exports__RenderingCancelledException as RenderingCancelledException, __webpack_exports__ResponseException as ResponseException, __webpack_exports__SignatureExtractor as SignatureExtractor, __webpack_exports__SupportedImageMimeTypes as SupportedImageMimeTypes, __webpack_exports__TextLayer as TextLayer, __webpack_exports__TouchManager as TouchManager, __webpack_exports__Util as Util, __webpack_exports__VerbosityLevel as VerbosityLevel, __webpack_exports__XfaLayer as XfaLayer, __webpack_exports__build as build, __webpack_exports__createValidAbsoluteUrl as createValidAbsoluteUrl, __webpack_exports__fetchData as fetchData, __webpack_exports__getDocument as getDocument, __webpack_exports__getFilenameFromUrl as getFilenameFromUrl, __webpack_exports__getPdfFilenameFromUrl as getPdfFilenameFromUrl, __webpack_exports__getUuid as getUuid, __webpack_exports__getXfaPageViewport as getXfaPageViewport, __webpack_exports__isDataScheme as isDataScheme, __webpack_exports__isPdfFile as isPdfFile, __webpack_exports__isValidExplicitDest as isValidExplicitDest, __webpack_exports__noContextMenu as noContextMenu, __webpack_exports__normalizeUnicode as normalizeUnicode, __webpack_exports__setLayerDimensions as setLayerDimensions, __webpack_exports__shadow as shadow, __webpack_exports__stopEvent as stopEvent, __webpack_exports__version as version };
  22520. //# sourceMappingURL=pdf.mjs.map