pdf.mjs 666 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614
  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. ;// ./src/shared/util.js
  23. const isNodeJS = typeof process === "object" && process + "" === "[object process]" && !process.versions.nw && !(process.versions.electron && process.type && process.type !== "browser");
  24. const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
  25. const LINE_FACTOR = 1.35;
  26. const LINE_DESCENT_FACTOR = 0.35;
  27. const BASELINE_FACTOR = LINE_DESCENT_FACTOR / LINE_FACTOR;
  28. const RenderingIntentFlag = {
  29. ANY: 0x01,
  30. DISPLAY: 0x02,
  31. PRINT: 0x04,
  32. SAVE: 0x08,
  33. ANNOTATIONS_FORMS: 0x10,
  34. ANNOTATIONS_STORAGE: 0x20,
  35. ANNOTATIONS_DISABLE: 0x40,
  36. IS_EDITING: 0x80,
  37. OPLIST: 0x100
  38. };
  39. const AnnotationMode = {
  40. DISABLE: 0,
  41. ENABLE: 1,
  42. ENABLE_FORMS: 2,
  43. ENABLE_STORAGE: 3
  44. };
  45. const AnnotationEditorPrefix = "pdfjs_internal_editor_";
  46. const AnnotationEditorType = {
  47. DISABLE: -1,
  48. NONE: 0,
  49. FREETEXT: 3,
  50. HIGHLIGHT: 9,
  51. STAMP: 13,
  52. INK: 15,
  53. SIGNATURE: 101
  54. };
  55. const AnnotationEditorParamsType = {
  56. RESIZE: 1,
  57. CREATE: 2,
  58. FREETEXT_SIZE: 11,
  59. FREETEXT_COLOR: 12,
  60. FREETEXT_OPACITY: 13,
  61. INK_COLOR: 21,
  62. INK_THICKNESS: 22,
  63. INK_OPACITY: 23,
  64. HIGHLIGHT_COLOR: 31,
  65. HIGHLIGHT_DEFAULT_COLOR: 32,
  66. HIGHLIGHT_THICKNESS: 33,
  67. HIGHLIGHT_FREE: 34,
  68. HIGHLIGHT_SHOW_ALL: 35,
  69. DRAW_STEP: 41
  70. };
  71. const PermissionFlag = {
  72. PRINT: 0x04,
  73. MODIFY_CONTENTS: 0x08,
  74. COPY: 0x10,
  75. MODIFY_ANNOTATIONS: 0x20,
  76. FILL_INTERACTIVE_FORMS: 0x100,
  77. COPY_FOR_ACCESSIBILITY: 0x200,
  78. ASSEMBLE: 0x400,
  79. PRINT_HIGH_QUALITY: 0x800
  80. };
  81. const TextRenderingMode = {
  82. FILL: 0,
  83. STROKE: 1,
  84. FILL_STROKE: 2,
  85. INVISIBLE: 3,
  86. FILL_ADD_TO_PATH: 4,
  87. STROKE_ADD_TO_PATH: 5,
  88. FILL_STROKE_ADD_TO_PATH: 6,
  89. ADD_TO_PATH: 7,
  90. FILL_STROKE_MASK: 3,
  91. ADD_TO_PATH_FLAG: 4
  92. };
  93. const util_ImageKind = {
  94. GRAYSCALE_1BPP: 1,
  95. RGB_24BPP: 2,
  96. RGBA_32BPP: 3
  97. };
  98. const AnnotationType = {
  99. TEXT: 1,
  100. LINK: 2,
  101. FREETEXT: 3,
  102. LINE: 4,
  103. SQUARE: 5,
  104. CIRCLE: 6,
  105. POLYGON: 7,
  106. POLYLINE: 8,
  107. HIGHLIGHT: 9,
  108. UNDERLINE: 10,
  109. SQUIGGLY: 11,
  110. STRIKEOUT: 12,
  111. STAMP: 13,
  112. CARET: 14,
  113. INK: 15,
  114. POPUP: 16,
  115. FILEATTACHMENT: 17,
  116. SOUND: 18,
  117. MOVIE: 19,
  118. WIDGET: 20,
  119. SCREEN: 21,
  120. PRINTERMARK: 22,
  121. TRAPNET: 23,
  122. WATERMARK: 24,
  123. THREED: 25,
  124. REDACT: 26
  125. };
  126. const AnnotationReplyType = {
  127. GROUP: "Group",
  128. REPLY: "R"
  129. };
  130. const AnnotationFlag = {
  131. INVISIBLE: 0x01,
  132. HIDDEN: 0x02,
  133. PRINT: 0x04,
  134. NOZOOM: 0x08,
  135. NOROTATE: 0x10,
  136. NOVIEW: 0x20,
  137. READONLY: 0x40,
  138. LOCKED: 0x80,
  139. TOGGLENOVIEW: 0x100,
  140. LOCKEDCONTENTS: 0x200
  141. };
  142. const AnnotationFieldFlag = {
  143. READONLY: 0x0000001,
  144. REQUIRED: 0x0000002,
  145. NOEXPORT: 0x0000004,
  146. MULTILINE: 0x0001000,
  147. PASSWORD: 0x0002000,
  148. NOTOGGLETOOFF: 0x0004000,
  149. RADIO: 0x0008000,
  150. PUSHBUTTON: 0x0010000,
  151. COMBO: 0x0020000,
  152. EDIT: 0x0040000,
  153. SORT: 0x0080000,
  154. FILESELECT: 0x0100000,
  155. MULTISELECT: 0x0200000,
  156. DONOTSPELLCHECK: 0x0400000,
  157. DONOTSCROLL: 0x0800000,
  158. COMB: 0x1000000,
  159. RICHTEXT: 0x2000000,
  160. RADIOSINUNISON: 0x2000000,
  161. COMMITONSELCHANGE: 0x4000000
  162. };
  163. const AnnotationBorderStyleType = {
  164. SOLID: 1,
  165. DASHED: 2,
  166. BEVELED: 3,
  167. INSET: 4,
  168. UNDERLINE: 5
  169. };
  170. const AnnotationActionEventType = {
  171. E: "Mouse Enter",
  172. X: "Mouse Exit",
  173. D: "Mouse Down",
  174. U: "Mouse Up",
  175. Fo: "Focus",
  176. Bl: "Blur",
  177. PO: "PageOpen",
  178. PC: "PageClose",
  179. PV: "PageVisible",
  180. PI: "PageInvisible",
  181. K: "Keystroke",
  182. F: "Format",
  183. V: "Validate",
  184. C: "Calculate"
  185. };
  186. const DocumentActionEventType = {
  187. WC: "WillClose",
  188. WS: "WillSave",
  189. DS: "DidSave",
  190. WP: "WillPrint",
  191. DP: "DidPrint"
  192. };
  193. const PageActionEventType = {
  194. O: "PageOpen",
  195. C: "PageClose"
  196. };
  197. const VerbosityLevel = {
  198. ERRORS: 0,
  199. WARNINGS: 1,
  200. INFOS: 5
  201. };
  202. const OPS = {
  203. dependency: 1,
  204. setLineWidth: 2,
  205. setLineCap: 3,
  206. setLineJoin: 4,
  207. setMiterLimit: 5,
  208. setDash: 6,
  209. setRenderingIntent: 7,
  210. setFlatness: 8,
  211. setGState: 9,
  212. save: 10,
  213. restore: 11,
  214. transform: 12,
  215. moveTo: 13,
  216. lineTo: 14,
  217. curveTo: 15,
  218. curveTo2: 16,
  219. curveTo3: 17,
  220. closePath: 18,
  221. rectangle: 19,
  222. stroke: 20,
  223. closeStroke: 21,
  224. fill: 22,
  225. eoFill: 23,
  226. fillStroke: 24,
  227. eoFillStroke: 25,
  228. closeFillStroke: 26,
  229. closeEOFillStroke: 27,
  230. endPath: 28,
  231. clip: 29,
  232. eoClip: 30,
  233. beginText: 31,
  234. endText: 32,
  235. setCharSpacing: 33,
  236. setWordSpacing: 34,
  237. setHScale: 35,
  238. setLeading: 36,
  239. setFont: 37,
  240. setTextRenderingMode: 38,
  241. setTextRise: 39,
  242. moveText: 40,
  243. setLeadingMoveText: 41,
  244. setTextMatrix: 42,
  245. nextLine: 43,
  246. showText: 44,
  247. showSpacedText: 45,
  248. nextLineShowText: 46,
  249. nextLineSetSpacingShowText: 47,
  250. setCharWidth: 48,
  251. setCharWidthAndBounds: 49,
  252. setStrokeColorSpace: 50,
  253. setFillColorSpace: 51,
  254. setStrokeColor: 52,
  255. setStrokeColorN: 53,
  256. setFillColor: 54,
  257. setFillColorN: 55,
  258. setStrokeGray: 56,
  259. setFillGray: 57,
  260. setStrokeRGBColor: 58,
  261. setFillRGBColor: 59,
  262. setStrokeCMYKColor: 60,
  263. setFillCMYKColor: 61,
  264. shadingFill: 62,
  265. beginInlineImage: 63,
  266. beginImageData: 64,
  267. endInlineImage: 65,
  268. paintXObject: 66,
  269. markPoint: 67,
  270. markPointProps: 68,
  271. beginMarkedContent: 69,
  272. beginMarkedContentProps: 70,
  273. endMarkedContent: 71,
  274. beginCompat: 72,
  275. endCompat: 73,
  276. paintFormXObjectBegin: 74,
  277. paintFormXObjectEnd: 75,
  278. beginGroup: 76,
  279. endGroup: 77,
  280. beginAnnotation: 80,
  281. endAnnotation: 81,
  282. paintImageMaskXObject: 83,
  283. paintImageMaskXObjectGroup: 84,
  284. paintImageXObject: 85,
  285. paintInlineImageXObject: 86,
  286. paintInlineImageXObjectGroup: 87,
  287. paintImageXObjectRepeat: 88,
  288. paintImageMaskXObjectRepeat: 89,
  289. paintSolidColorImageMask: 90,
  290. constructPath: 91,
  291. setStrokeTransparent: 92,
  292. setFillTransparent: 93,
  293. rawFillPath: 94
  294. };
  295. const DrawOPS = {
  296. moveTo: 0,
  297. lineTo: 1,
  298. curveTo: 2,
  299. closePath: 3
  300. };
  301. const PasswordResponses = {
  302. NEED_PASSWORD: 1,
  303. INCORRECT_PASSWORD: 2
  304. };
  305. let verbosity = VerbosityLevel.WARNINGS;
  306. function setVerbosityLevel(level) {
  307. if (Number.isInteger(level)) {
  308. verbosity = level;
  309. }
  310. }
  311. function getVerbosityLevel() {
  312. return verbosity;
  313. }
  314. function info(msg) {
  315. if (verbosity >= VerbosityLevel.INFOS) {
  316. console.log(`Info: ${msg}`);
  317. }
  318. }
  319. function warn(msg) {
  320. if (verbosity >= VerbosityLevel.WARNINGS) {
  321. console.log(`Warning: ${msg}`);
  322. }
  323. }
  324. function unreachable(msg) {
  325. throw new Error(msg);
  326. }
  327. function assert(cond, msg) {
  328. if (!cond) {
  329. unreachable(msg);
  330. }
  331. }
  332. function _isValidProtocol(url) {
  333. switch (url?.protocol) {
  334. case "http:":
  335. case "https:":
  336. case "ftp:":
  337. case "mailto:":
  338. case "tel:":
  339. return true;
  340. default:
  341. return false;
  342. }
  343. }
  344. function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
  345. if (!url) {
  346. return null;
  347. }
  348. if (options && typeof url === "string") {
  349. if (options.addDefaultProtocol && url.startsWith("www.")) {
  350. const dots = url.match(/\./g);
  351. if (dots?.length >= 2) {
  352. url = `http://${url}`;
  353. }
  354. }
  355. if (options.tryConvertEncoding) {
  356. try {
  357. url = stringToUTF8String(url);
  358. } catch {}
  359. }
  360. }
  361. const absoluteUrl = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
  362. return _isValidProtocol(absoluteUrl) ? absoluteUrl : null;
  363. }
  364. function updateUrlHash(url, hash, allowRel = false) {
  365. const res = URL.parse(url);
  366. if (res) {
  367. res.hash = hash;
  368. return res.href;
  369. }
  370. if (allowRel && createValidAbsoluteUrl(url, "http://example.com")) {
  371. return url.split("#", 1)[0] + `${hash ? `#${hash}` : ""}`;
  372. }
  373. return "";
  374. }
  375. function shadow(obj, prop, value, nonSerializable = false) {
  376. Object.defineProperty(obj, prop, {
  377. value,
  378. enumerable: !nonSerializable,
  379. configurable: true,
  380. writable: false
  381. });
  382. return value;
  383. }
  384. const BaseException = function BaseExceptionClosure() {
  385. function BaseException(message, name) {
  386. this.message = message;
  387. this.name = name;
  388. }
  389. BaseException.prototype = new Error();
  390. BaseException.constructor = BaseException;
  391. return BaseException;
  392. }();
  393. class PasswordException extends BaseException {
  394. constructor(msg, code) {
  395. super(msg, "PasswordException");
  396. this.code = code;
  397. }
  398. }
  399. class UnknownErrorException extends BaseException {
  400. constructor(msg, details) {
  401. super(msg, "UnknownErrorException");
  402. this.details = details;
  403. }
  404. }
  405. class InvalidPDFException extends BaseException {
  406. constructor(msg) {
  407. super(msg, "InvalidPDFException");
  408. }
  409. }
  410. class ResponseException extends BaseException {
  411. constructor(msg, status, missing) {
  412. super(msg, "ResponseException");
  413. this.status = status;
  414. this.missing = missing;
  415. }
  416. }
  417. class FormatError extends BaseException {
  418. constructor(msg) {
  419. super(msg, "FormatError");
  420. }
  421. }
  422. class AbortException extends BaseException {
  423. constructor(msg) {
  424. super(msg, "AbortException");
  425. }
  426. }
  427. function bytesToString(bytes) {
  428. if (typeof bytes !== "object" || bytes?.length === undefined) {
  429. unreachable("Invalid argument for bytesToString");
  430. }
  431. const length = bytes.length;
  432. const MAX_ARGUMENT_COUNT = 8192;
  433. if (length < MAX_ARGUMENT_COUNT) {
  434. return String.fromCharCode.apply(null, bytes);
  435. }
  436. const strBuf = [];
  437. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  438. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  439. const chunk = bytes.subarray(i, chunkEnd);
  440. strBuf.push(String.fromCharCode.apply(null, chunk));
  441. }
  442. return strBuf.join("");
  443. }
  444. function stringToBytes(str) {
  445. if (typeof str !== "string") {
  446. unreachable("Invalid argument for stringToBytes");
  447. }
  448. const length = str.length;
  449. const bytes = new Uint8Array(length);
  450. for (let i = 0; i < length; ++i) {
  451. bytes[i] = str.charCodeAt(i) & 0xff;
  452. }
  453. return bytes;
  454. }
  455. function string32(value) {
  456. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  457. }
  458. function objectSize(obj) {
  459. return Object.keys(obj).length;
  460. }
  461. function isLittleEndian() {
  462. const buffer8 = new Uint8Array(4);
  463. buffer8[0] = 1;
  464. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  465. return view32[0] === 1;
  466. }
  467. function isEvalSupported() {
  468. try {
  469. new Function("");
  470. return true;
  471. } catch {
  472. return false;
  473. }
  474. }
  475. class util_FeatureTest {
  476. static get isLittleEndian() {
  477. return shadow(this, "isLittleEndian", isLittleEndian());
  478. }
  479. static get isEvalSupported() {
  480. return shadow(this, "isEvalSupported", isEvalSupported());
  481. }
  482. static get isOffscreenCanvasSupported() {
  483. return shadow(this, "isOffscreenCanvasSupported", typeof OffscreenCanvas !== "undefined");
  484. }
  485. static get isImageDecoderSupported() {
  486. return shadow(this, "isImageDecoderSupported", typeof ImageDecoder !== "undefined");
  487. }
  488. static get platform() {
  489. if (typeof navigator !== "undefined" && typeof navigator?.platform === "string" && typeof navigator?.userAgent === "string") {
  490. const {
  491. platform,
  492. userAgent
  493. } = navigator;
  494. return shadow(this, "platform", {
  495. isAndroid: userAgent.includes("Android"),
  496. isLinux: platform.includes("Linux"),
  497. isMac: platform.includes("Mac"),
  498. isWindows: platform.includes("Win"),
  499. isFirefox: userAgent.includes("Firefox")
  500. });
  501. }
  502. return shadow(this, "platform", {
  503. isAndroid: false,
  504. isLinux: false,
  505. isMac: false,
  506. isWindows: false,
  507. isFirefox: false
  508. });
  509. }
  510. static get isCSSRoundSupported() {
  511. return shadow(this, "isCSSRoundSupported", globalThis.CSS?.supports?.("width: round(1.5px, 1px)"));
  512. }
  513. }
  514. const hexNumbers = Array.from(Array(256).keys(), n => n.toString(16).padStart(2, "0"));
  515. class Util {
  516. static makeHexColor(r, g, b) {
  517. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  518. }
  519. static scaleMinMax(transform, minMax) {
  520. let temp;
  521. if (transform[0]) {
  522. if (transform[0] < 0) {
  523. temp = minMax[0];
  524. minMax[0] = minMax[2];
  525. minMax[2] = temp;
  526. }
  527. minMax[0] *= transform[0];
  528. minMax[2] *= transform[0];
  529. if (transform[3] < 0) {
  530. temp = minMax[1];
  531. minMax[1] = minMax[3];
  532. minMax[3] = temp;
  533. }
  534. minMax[1] *= transform[3];
  535. minMax[3] *= transform[3];
  536. } else {
  537. temp = minMax[0];
  538. minMax[0] = minMax[1];
  539. minMax[1] = temp;
  540. temp = minMax[2];
  541. minMax[2] = minMax[3];
  542. minMax[3] = temp;
  543. if (transform[1] < 0) {
  544. temp = minMax[1];
  545. minMax[1] = minMax[3];
  546. minMax[3] = temp;
  547. }
  548. minMax[1] *= transform[1];
  549. minMax[3] *= transform[1];
  550. if (transform[2] < 0) {
  551. temp = minMax[0];
  552. minMax[0] = minMax[2];
  553. minMax[2] = temp;
  554. }
  555. minMax[0] *= transform[2];
  556. minMax[2] *= transform[2];
  557. }
  558. minMax[0] += transform[4];
  559. minMax[1] += transform[5];
  560. minMax[2] += transform[4];
  561. minMax[3] += transform[5];
  562. }
  563. static transform(m1, m2) {
  564. 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]];
  565. }
  566. static applyTransform(p, m, pos = 0) {
  567. const p0 = p[pos];
  568. const p1 = p[pos + 1];
  569. p[pos] = p0 * m[0] + p1 * m[2] + m[4];
  570. p[pos + 1] = p0 * m[1] + p1 * m[3] + m[5];
  571. }
  572. static applyTransformToBezier(p, transform, pos = 0) {
  573. const m0 = transform[0];
  574. const m1 = transform[1];
  575. const m2 = transform[2];
  576. const m3 = transform[3];
  577. const m4 = transform[4];
  578. const m5 = transform[5];
  579. for (let i = 0; i < 6; i += 2) {
  580. const pI = p[pos + i];
  581. const pI1 = p[pos + i + 1];
  582. p[pos + i] = pI * m0 + pI1 * m2 + m4;
  583. p[pos + i + 1] = pI * m1 + pI1 * m3 + m5;
  584. }
  585. }
  586. static applyInverseTransform(p, m) {
  587. const p0 = p[0];
  588. const p1 = p[1];
  589. const d = m[0] * m[3] - m[1] * m[2];
  590. p[0] = (p0 * m[3] - p1 * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  591. p[1] = (-p0 * m[1] + p1 * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  592. }
  593. static axialAlignedBoundingBox(rect, transform, output) {
  594. const m0 = transform[0];
  595. const m1 = transform[1];
  596. const m2 = transform[2];
  597. const m3 = transform[3];
  598. const m4 = transform[4];
  599. const m5 = transform[5];
  600. const r0 = rect[0];
  601. const r1 = rect[1];
  602. const r2 = rect[2];
  603. const r3 = rect[3];
  604. let a0 = m0 * r0 + m4;
  605. let a2 = a0;
  606. let a1 = m0 * r2 + m4;
  607. let a3 = a1;
  608. let b0 = m3 * r1 + m5;
  609. let b2 = b0;
  610. let b1 = m3 * r3 + m5;
  611. let b3 = b1;
  612. if (m1 !== 0 || m2 !== 0) {
  613. const m1r0 = m1 * r0;
  614. const m1r2 = m1 * r2;
  615. const m2r1 = m2 * r1;
  616. const m2r3 = m2 * r3;
  617. a0 += m2r1;
  618. a3 += m2r1;
  619. a1 += m2r3;
  620. a2 += m2r3;
  621. b0 += m1r0;
  622. b3 += m1r0;
  623. b1 += m1r2;
  624. b2 += m1r2;
  625. }
  626. output[0] = Math.min(output[0], a0, a1, a2, a3);
  627. output[1] = Math.min(output[1], b0, b1, b2, b3);
  628. output[2] = Math.max(output[2], a0, a1, a2, a3);
  629. output[3] = Math.max(output[3], b0, b1, b2, b3);
  630. }
  631. static inverseTransform(m) {
  632. const d = m[0] * m[3] - m[1] * m[2];
  633. 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];
  634. }
  635. static singularValueDecompose2dScale(matrix, output) {
  636. const m0 = matrix[0];
  637. const m1 = matrix[1];
  638. const m2 = matrix[2];
  639. const m3 = matrix[3];
  640. const a = m0 ** 2 + m1 ** 2;
  641. const b = m0 * m2 + m1 * m3;
  642. const c = m2 ** 2 + m3 ** 2;
  643. const first = (a + c) / 2;
  644. const second = Math.sqrt(first ** 2 - (a * c - b ** 2));
  645. output[0] = Math.sqrt(first + second || 1);
  646. output[1] = Math.sqrt(first - second || 1);
  647. }
  648. static normalizeRect(rect) {
  649. const r = rect.slice(0);
  650. if (rect[0] > rect[2]) {
  651. r[0] = rect[2];
  652. r[2] = rect[0];
  653. }
  654. if (rect[1] > rect[3]) {
  655. r[1] = rect[3];
  656. r[3] = rect[1];
  657. }
  658. return r;
  659. }
  660. static intersect(rect1, rect2) {
  661. const xLow = Math.max(Math.min(rect1[0], rect1[2]), Math.min(rect2[0], rect2[2]));
  662. const xHigh = Math.min(Math.max(rect1[0], rect1[2]), Math.max(rect2[0], rect2[2]));
  663. if (xLow > xHigh) {
  664. return null;
  665. }
  666. const yLow = Math.max(Math.min(rect1[1], rect1[3]), Math.min(rect2[1], rect2[3]));
  667. const yHigh = Math.min(Math.max(rect1[1], rect1[3]), Math.max(rect2[1], rect2[3]));
  668. if (yLow > yHigh) {
  669. return null;
  670. }
  671. return [xLow, yLow, xHigh, yHigh];
  672. }
  673. static pointBoundingBox(x, y, minMax) {
  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 rectBoundingBox(x0, y0, x1, y1, minMax) {
  680. minMax[0] = Math.min(minMax[0], x0, x1);
  681. minMax[1] = Math.min(minMax[1], y0, y1);
  682. minMax[2] = Math.max(minMax[2], x0, x1);
  683. minMax[3] = Math.max(minMax[3], y0, y1);
  684. }
  685. static #getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, t, minMax) {
  686. if (t <= 0 || t >= 1) {
  687. return;
  688. }
  689. const mt = 1 - t;
  690. const tt = t * t;
  691. const ttt = tt * t;
  692. const x = mt * (mt * (mt * x0 + 3 * t * x1) + 3 * tt * x2) + ttt * x3;
  693. const y = mt * (mt * (mt * y0 + 3 * t * y1) + 3 * tt * y2) + ttt * y3;
  694. minMax[0] = Math.min(minMax[0], x);
  695. minMax[1] = Math.min(minMax[1], y);
  696. minMax[2] = Math.max(minMax[2], x);
  697. minMax[3] = Math.max(minMax[3], y);
  698. }
  699. static #getExtremum(x0, x1, x2, x3, y0, y1, y2, y3, a, b, c, minMax) {
  700. if (Math.abs(a) < 1e-12) {
  701. if (Math.abs(b) >= 1e-12) {
  702. this.#getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, -c / b, minMax);
  703. }
  704. return;
  705. }
  706. const delta = b ** 2 - 4 * c * a;
  707. if (delta < 0) {
  708. return;
  709. }
  710. const sqrtDelta = Math.sqrt(delta);
  711. const a2 = 2 * a;
  712. this.#getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, (-b + sqrtDelta) / a2, minMax);
  713. this.#getExtremumOnCurve(x0, x1, x2, x3, y0, y1, y2, y3, (-b - sqrtDelta) / a2, minMax);
  714. }
  715. static bezierBoundingBox(x0, y0, x1, y1, x2, y2, x3, y3, minMax) {
  716. minMax[0] = Math.min(minMax[0], x0, x3);
  717. minMax[1] = Math.min(minMax[1], y0, y3);
  718. minMax[2] = Math.max(minMax[2], x0, x3);
  719. minMax[3] = Math.max(minMax[3], y0, y3);
  720. 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);
  721. 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);
  722. }
  723. }
  724. 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]));
  725. function stringToPDFString(str) {
  726. if (str[0] >= "\xEF") {
  727. let encoding;
  728. if (str[0] === "\xFE" && str[1] === "\xFF") {
  729. encoding = "utf-16be";
  730. if (str.length % 2 === 1) {
  731. str = str.slice(0, -1);
  732. }
  733. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  734. encoding = "utf-16le";
  735. if (str.length % 2 === 1) {
  736. str = str.slice(0, -1);
  737. }
  738. } else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
  739. encoding = "utf-8";
  740. }
  741. if (encoding) {
  742. try {
  743. const decoder = new TextDecoder(encoding, {
  744. fatal: true
  745. });
  746. const buffer = stringToBytes(str);
  747. const decoded = decoder.decode(buffer);
  748. if (!decoded.includes("\x1b")) {
  749. return decoded;
  750. }
  751. return decoded.replaceAll(/\x1b[^\x1b]*(?:\x1b|$)/g, "");
  752. } catch (ex) {
  753. warn(`stringToPDFString: "${ex}".`);
  754. }
  755. }
  756. }
  757. const strBuf = [];
  758. for (let i = 0, ii = str.length; i < ii; i++) {
  759. const charCode = str.charCodeAt(i);
  760. if (charCode === 0x1b) {
  761. while (++i < ii && str.charCodeAt(i) !== 0x1b) {}
  762. continue;
  763. }
  764. const code = PDFStringTranslateTable[charCode];
  765. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  766. }
  767. return strBuf.join("");
  768. }
  769. function stringToUTF8String(str) {
  770. return decodeURIComponent(escape(str));
  771. }
  772. function utf8StringToString(str) {
  773. return unescape(encodeURIComponent(str));
  774. }
  775. function isArrayEqual(arr1, arr2) {
  776. if (arr1.length !== arr2.length) {
  777. return false;
  778. }
  779. for (let i = 0, ii = arr1.length; i < ii; i++) {
  780. if (arr1[i] !== arr2[i]) {
  781. return false;
  782. }
  783. }
  784. return true;
  785. }
  786. function getModificationDate(date = new Date()) {
  787. 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")];
  788. return buffer.join("");
  789. }
  790. let NormalizeRegex = null;
  791. let NormalizationMap = null;
  792. function normalizeUnicode(str) {
  793. if (!NormalizeRegex) {
  794. 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;
  795. NormalizationMap = new Map([["ſt", "ſt"]]);
  796. }
  797. return str.replaceAll(NormalizeRegex, (_, p1, p2) => p1 ? p1.normalize("NFKC") : NormalizationMap.get(p2));
  798. }
  799. function getUuid() {
  800. if (typeof crypto.randomUUID === "function") {
  801. return crypto.randomUUID();
  802. }
  803. const buf = new Uint8Array(32);
  804. crypto.getRandomValues(buf);
  805. return bytesToString(buf);
  806. }
  807. const AnnotationPrefix = "pdfjs_internal_id_";
  808. function _isValidExplicitDest(validRef, validName, dest) {
  809. if (!Array.isArray(dest) || dest.length < 2) {
  810. return false;
  811. }
  812. const [page, zoom, ...args] = dest;
  813. if (!validRef(page) && !Number.isInteger(page)) {
  814. return false;
  815. }
  816. if (!validName(zoom)) {
  817. return false;
  818. }
  819. const argsLen = args.length;
  820. let allowNull = true;
  821. switch (zoom.name) {
  822. case "XYZ":
  823. if (argsLen < 2 || argsLen > 3) {
  824. return false;
  825. }
  826. break;
  827. case "Fit":
  828. case "FitB":
  829. return argsLen === 0;
  830. case "FitH":
  831. case "FitBH":
  832. case "FitV":
  833. case "FitBV":
  834. if (argsLen > 1) {
  835. return false;
  836. }
  837. break;
  838. case "FitR":
  839. if (argsLen !== 4) {
  840. return false;
  841. }
  842. allowNull = false;
  843. break;
  844. default:
  845. return false;
  846. }
  847. for (const arg of args) {
  848. if (typeof arg === "number" || allowNull && arg === null) {
  849. continue;
  850. }
  851. return false;
  852. }
  853. return true;
  854. }
  855. function MathClamp(v, min, max) {
  856. return Math.min(Math.max(v, min), max);
  857. }
  858. function toHexUtil(arr) {
  859. if (Uint8Array.prototype.toHex) {
  860. return arr.toHex();
  861. }
  862. return Array.from(arr, num => hexNumbers[num]).join("");
  863. }
  864. function toBase64Util(arr) {
  865. if (Uint8Array.prototype.toBase64) {
  866. return arr.toBase64();
  867. }
  868. return btoa(bytesToString(arr));
  869. }
  870. function fromBase64Util(str) {
  871. if (Uint8Array.fromBase64) {
  872. return Uint8Array.fromBase64(str);
  873. }
  874. return stringToBytes(atob(str));
  875. }
  876. if (typeof Promise.try !== "function") {
  877. Promise.try = function (fn, ...args) {
  878. return new Promise(resolve => {
  879. resolve(fn(...args));
  880. });
  881. };
  882. }
  883. if (typeof Math.sumPrecise !== "function") {
  884. Math.sumPrecise = function (numbers) {
  885. return numbers.reduce((a, b) => a + b, 0);
  886. };
  887. }
  888. ;// ./src/display/display_utils.js
  889. const SVG_NS = "http://www.w3.org/2000/svg";
  890. class PixelsPerInch {
  891. static CSS = 96.0;
  892. static PDF = 72.0;
  893. static PDF_TO_CSS_UNITS = this.CSS / this.PDF;
  894. }
  895. async function fetchData(url, type = "text") {
  896. if (isValidFetchUrl(url, document.baseURI)) {
  897. const response = await fetch(url);
  898. if (!response.ok) {
  899. throw new Error(response.statusText);
  900. }
  901. switch (type) {
  902. case "arraybuffer":
  903. return response.arrayBuffer();
  904. case "blob":
  905. return response.blob();
  906. case "json":
  907. return response.json();
  908. }
  909. return response.text();
  910. }
  911. return new Promise((resolve, reject) => {
  912. const request = new XMLHttpRequest();
  913. request.open("GET", url, true);
  914. request.responseType = type;
  915. request.onreadystatechange = () => {
  916. if (request.readyState !== XMLHttpRequest.DONE) {
  917. return;
  918. }
  919. if (request.status === 200 || request.status === 0) {
  920. switch (type) {
  921. case "arraybuffer":
  922. case "blob":
  923. case "json":
  924. resolve(request.response);
  925. return;
  926. }
  927. resolve(request.responseText);
  928. return;
  929. }
  930. reject(new Error(request.statusText));
  931. };
  932. request.send(null);
  933. });
  934. }
  935. class PageViewport {
  936. constructor({
  937. viewBox,
  938. userUnit,
  939. scale,
  940. rotation,
  941. offsetX = 0,
  942. offsetY = 0,
  943. dontFlip = false
  944. }) {
  945. this.viewBox = viewBox;
  946. this.userUnit = userUnit;
  947. this.scale = scale;
  948. this.rotation = rotation;
  949. this.offsetX = offsetX;
  950. this.offsetY = offsetY;
  951. scale *= userUnit;
  952. const centerX = (viewBox[2] + viewBox[0]) / 2;
  953. const centerY = (viewBox[3] + viewBox[1]) / 2;
  954. let rotateA, rotateB, rotateC, rotateD;
  955. rotation %= 360;
  956. if (rotation < 0) {
  957. rotation += 360;
  958. }
  959. switch (rotation) {
  960. case 180:
  961. rotateA = -1;
  962. rotateB = 0;
  963. rotateC = 0;
  964. rotateD = 1;
  965. break;
  966. case 90:
  967. rotateA = 0;
  968. rotateB = 1;
  969. rotateC = 1;
  970. rotateD = 0;
  971. break;
  972. case 270:
  973. rotateA = 0;
  974. rotateB = -1;
  975. rotateC = -1;
  976. rotateD = 0;
  977. break;
  978. case 0:
  979. rotateA = 1;
  980. rotateB = 0;
  981. rotateC = 0;
  982. rotateD = -1;
  983. break;
  984. default:
  985. throw new Error("PageViewport: Invalid rotation, must be a multiple of 90 degrees.");
  986. }
  987. if (dontFlip) {
  988. rotateC = -rotateC;
  989. rotateD = -rotateD;
  990. }
  991. let offsetCanvasX, offsetCanvasY;
  992. let width, height;
  993. if (rotateA === 0) {
  994. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  995. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  996. width = (viewBox[3] - viewBox[1]) * scale;
  997. height = (viewBox[2] - viewBox[0]) * scale;
  998. } else {
  999. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  1000. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  1001. width = (viewBox[2] - viewBox[0]) * scale;
  1002. height = (viewBox[3] - viewBox[1]) * scale;
  1003. }
  1004. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  1005. this.width = width;
  1006. this.height = height;
  1007. }
  1008. get rawDims() {
  1009. const dims = this.viewBox;
  1010. return shadow(this, "rawDims", {
  1011. pageWidth: dims[2] - dims[0],
  1012. pageHeight: dims[3] - dims[1],
  1013. pageX: dims[0],
  1014. pageY: dims[1]
  1015. });
  1016. }
  1017. clone({
  1018. scale = this.scale,
  1019. rotation = this.rotation,
  1020. offsetX = this.offsetX,
  1021. offsetY = this.offsetY,
  1022. dontFlip = false
  1023. } = {}) {
  1024. return new PageViewport({
  1025. viewBox: this.viewBox.slice(),
  1026. userUnit: this.userUnit,
  1027. scale,
  1028. rotation,
  1029. offsetX,
  1030. offsetY,
  1031. dontFlip
  1032. });
  1033. }
  1034. convertToViewportPoint(x, y) {
  1035. const p = [x, y];
  1036. Util.applyTransform(p, this.transform);
  1037. return p;
  1038. }
  1039. convertToViewportRectangle(rect) {
  1040. const topLeft = [rect[0], rect[1]];
  1041. Util.applyTransform(topLeft, this.transform);
  1042. const bottomRight = [rect[2], rect[3]];
  1043. Util.applyTransform(bottomRight, this.transform);
  1044. return [topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]];
  1045. }
  1046. convertToPdfPoint(x, y) {
  1047. const p = [x, y];
  1048. Util.applyInverseTransform(p, this.transform);
  1049. return p;
  1050. }
  1051. }
  1052. class RenderingCancelledException extends BaseException {
  1053. constructor(msg, extraDelay = 0) {
  1054. super(msg, "RenderingCancelledException");
  1055. this.extraDelay = extraDelay;
  1056. }
  1057. }
  1058. function isDataScheme(url) {
  1059. const ii = url.length;
  1060. let i = 0;
  1061. while (i < ii && url[i].trim() === "") {
  1062. i++;
  1063. }
  1064. return url.substring(i, i + 5).toLowerCase() === "data:";
  1065. }
  1066. function isPdfFile(filename) {
  1067. return typeof filename === "string" && /\.pdf$/i.test(filename);
  1068. }
  1069. function getFilenameFromUrl(url) {
  1070. [url] = url.split(/[#?]/, 1);
  1071. return url.substring(url.lastIndexOf("/") + 1);
  1072. }
  1073. function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
  1074. if (typeof url !== "string") {
  1075. return defaultFilename;
  1076. }
  1077. if (isDataScheme(url)) {
  1078. warn('getPdfFilenameFromUrl: ignore "data:"-URL for performance reasons.');
  1079. return defaultFilename;
  1080. }
  1081. const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  1082. const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  1083. const splitURI = reURI.exec(url);
  1084. let suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]);
  1085. if (suggestedFilename) {
  1086. suggestedFilename = suggestedFilename[0];
  1087. if (suggestedFilename.includes("%")) {
  1088. try {
  1089. suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
  1090. } catch {}
  1091. }
  1092. }
  1093. return suggestedFilename || defaultFilename;
  1094. }
  1095. class StatTimer {
  1096. started = Object.create(null);
  1097. times = [];
  1098. time(name) {
  1099. if (name in this.started) {
  1100. warn(`Timer is already running for ${name}`);
  1101. }
  1102. this.started[name] = Date.now();
  1103. }
  1104. timeEnd(name) {
  1105. if (!(name in this.started)) {
  1106. warn(`Timer has not been started for ${name}`);
  1107. }
  1108. this.times.push({
  1109. name,
  1110. start: this.started[name],
  1111. end: Date.now()
  1112. });
  1113. delete this.started[name];
  1114. }
  1115. toString() {
  1116. const outBuf = [];
  1117. let longest = 0;
  1118. for (const {
  1119. name
  1120. } of this.times) {
  1121. longest = Math.max(name.length, longest);
  1122. }
  1123. for (const {
  1124. name,
  1125. start,
  1126. end
  1127. } of this.times) {
  1128. outBuf.push(`${name.padEnd(longest)} ${end - start}ms\n`);
  1129. }
  1130. return outBuf.join("");
  1131. }
  1132. }
  1133. function isValidFetchUrl(url, baseUrl) {
  1134. const res = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
  1135. return res?.protocol === "http:" || res?.protocol === "https:";
  1136. }
  1137. function noContextMenu(e) {
  1138. e.preventDefault();
  1139. }
  1140. function stopEvent(e) {
  1141. e.preventDefault();
  1142. e.stopPropagation();
  1143. }
  1144. function deprecated(details) {
  1145. console.log("Deprecated API usage: " + details);
  1146. }
  1147. class PDFDateString {
  1148. static #regex;
  1149. static toDateObject(input) {
  1150. if (!input || typeof input !== "string") {
  1151. return null;
  1152. }
  1153. this.#regex ||= new RegExp("^D:" + "(\\d{4})" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "([Z|+|-])?" + "(\\d{2})?" + "'?" + "(\\d{2})?" + "'?");
  1154. const matches = this.#regex.exec(input);
  1155. if (!matches) {
  1156. return null;
  1157. }
  1158. const year = parseInt(matches[1], 10);
  1159. let month = parseInt(matches[2], 10);
  1160. month = month >= 1 && month <= 12 ? month - 1 : 0;
  1161. let day = parseInt(matches[3], 10);
  1162. day = day >= 1 && day <= 31 ? day : 1;
  1163. let hour = parseInt(matches[4], 10);
  1164. hour = hour >= 0 && hour <= 23 ? hour : 0;
  1165. let minute = parseInt(matches[5], 10);
  1166. minute = minute >= 0 && minute <= 59 ? minute : 0;
  1167. let second = parseInt(matches[6], 10);
  1168. second = second >= 0 && second <= 59 ? second : 0;
  1169. const universalTimeRelation = matches[7] || "Z";
  1170. let offsetHour = parseInt(matches[8], 10);
  1171. offsetHour = offsetHour >= 0 && offsetHour <= 23 ? offsetHour : 0;
  1172. let offsetMinute = parseInt(matches[9], 10) || 0;
  1173. offsetMinute = offsetMinute >= 0 && offsetMinute <= 59 ? offsetMinute : 0;
  1174. if (universalTimeRelation === "-") {
  1175. hour += offsetHour;
  1176. minute += offsetMinute;
  1177. } else if (universalTimeRelation === "+") {
  1178. hour -= offsetHour;
  1179. minute -= offsetMinute;
  1180. }
  1181. return new Date(Date.UTC(year, month, day, hour, minute, second));
  1182. }
  1183. }
  1184. function getXfaPageViewport(xfaPage, {
  1185. scale = 1,
  1186. rotation = 0
  1187. }) {
  1188. const {
  1189. width,
  1190. height
  1191. } = xfaPage.attributes.style;
  1192. const viewBox = [0, 0, parseInt(width), parseInt(height)];
  1193. return new PageViewport({
  1194. viewBox,
  1195. userUnit: 1,
  1196. scale,
  1197. rotation
  1198. });
  1199. }
  1200. function getRGB(color) {
  1201. if (color.startsWith("#")) {
  1202. const colorRGB = parseInt(color.slice(1), 16);
  1203. return [(colorRGB & 0xff0000) >> 16, (colorRGB & 0x00ff00) >> 8, colorRGB & 0x0000ff];
  1204. }
  1205. if (color.startsWith("rgb(")) {
  1206. return color.slice(4, -1).split(",").map(x => parseInt(x));
  1207. }
  1208. if (color.startsWith("rgba(")) {
  1209. return color.slice(5, -1).split(",").map(x => parseInt(x)).slice(0, 3);
  1210. }
  1211. warn(`Not a valid color format: "${color}"`);
  1212. return [0, 0, 0];
  1213. }
  1214. function getColorValues(colors) {
  1215. const span = document.createElement("span");
  1216. span.style.visibility = "hidden";
  1217. span.style.colorScheme = "only light";
  1218. document.body.append(span);
  1219. for (const name of colors.keys()) {
  1220. span.style.color = name;
  1221. const computedColor = window.getComputedStyle(span).color;
  1222. colors.set(name, getRGB(computedColor));
  1223. }
  1224. span.remove();
  1225. }
  1226. function getCurrentTransform(ctx) {
  1227. const {
  1228. a,
  1229. b,
  1230. c,
  1231. d,
  1232. e,
  1233. f
  1234. } = ctx.getTransform();
  1235. return [a, b, c, d, e, f];
  1236. }
  1237. function getCurrentTransformInverse(ctx) {
  1238. const {
  1239. a,
  1240. b,
  1241. c,
  1242. d,
  1243. e,
  1244. f
  1245. } = ctx.getTransform().invertSelf();
  1246. return [a, b, c, d, e, f];
  1247. }
  1248. function setLayerDimensions(div, viewport, mustFlip = false, mustRotate = true) {
  1249. if (viewport instanceof PageViewport) {
  1250. const {
  1251. pageWidth,
  1252. pageHeight
  1253. } = viewport.rawDims;
  1254. const {
  1255. style
  1256. } = div;
  1257. const useRound = util_FeatureTest.isCSSRoundSupported;
  1258. const w = `var(--total-scale-factor) * ${pageWidth}px`,
  1259. h = `var(--total-scale-factor) * ${pageHeight}px`;
  1260. const widthStr = useRound ? `round(down, ${w}, var(--scale-round-x))` : `calc(${w})`,
  1261. heightStr = useRound ? `round(down, ${h}, var(--scale-round-y))` : `calc(${h})`;
  1262. if (!mustFlip || viewport.rotation % 180 === 0) {
  1263. style.width = widthStr;
  1264. style.height = heightStr;
  1265. } else {
  1266. style.width = heightStr;
  1267. style.height = widthStr;
  1268. }
  1269. }
  1270. if (mustRotate) {
  1271. div.setAttribute("data-main-rotation", viewport.rotation);
  1272. }
  1273. }
  1274. class OutputScale {
  1275. constructor() {
  1276. const {
  1277. pixelRatio
  1278. } = OutputScale;
  1279. this.sx = pixelRatio;
  1280. this.sy = pixelRatio;
  1281. }
  1282. get scaled() {
  1283. return this.sx !== 1 || this.sy !== 1;
  1284. }
  1285. get symmetric() {
  1286. return this.sx === this.sy;
  1287. }
  1288. limitCanvas(width, height, maxPixels, maxDim) {
  1289. let maxAreaScale = Infinity,
  1290. maxWidthScale = Infinity,
  1291. maxHeightScale = Infinity;
  1292. if (maxPixels > 0) {
  1293. maxAreaScale = Math.sqrt(maxPixels / (width * height));
  1294. }
  1295. if (maxDim !== -1) {
  1296. maxWidthScale = maxDim / width;
  1297. maxHeightScale = maxDim / height;
  1298. }
  1299. const maxScale = Math.min(maxAreaScale, maxWidthScale, maxHeightScale);
  1300. if (this.sx > maxScale || this.sy > maxScale) {
  1301. this.sx = maxScale;
  1302. this.sy = maxScale;
  1303. return true;
  1304. }
  1305. return false;
  1306. }
  1307. static get pixelRatio() {
  1308. return globalThis.devicePixelRatio || 1;
  1309. }
  1310. }
  1311. const SupportedImageMimeTypes = ["image/apng", "image/avif", "image/bmp", "image/gif", "image/jpeg", "image/png", "image/svg+xml", "image/webp", "image/x-icon"];
  1312. ;// ./src/display/editor/toolbar.js
  1313. class EditorToolbar {
  1314. #toolbar = null;
  1315. #colorPicker = null;
  1316. #editor;
  1317. #buttons = null;
  1318. #altText = null;
  1319. #signatureDescriptionButton = null;
  1320. static #l10nRemove = null;
  1321. constructor(editor) {
  1322. this.#editor = editor;
  1323. EditorToolbar.#l10nRemove ||= Object.freeze({
  1324. freetext: "pdfjs-editor-remove-freetext-button",
  1325. highlight: "pdfjs-editor-remove-highlight-button",
  1326. ink: "pdfjs-editor-remove-ink-button",
  1327. stamp: "pdfjs-editor-remove-stamp-button",
  1328. signature: "pdfjs-editor-remove-signature-button"
  1329. });
  1330. }
  1331. render() {
  1332. const editToolbar = this.#toolbar = document.createElement("div");
  1333. editToolbar.classList.add("editToolbar", "hidden");
  1334. editToolbar.setAttribute("role", "toolbar");
  1335. const signal = this.#editor._uiManager._signal;
  1336. editToolbar.addEventListener("contextmenu", noContextMenu, {
  1337. signal
  1338. });
  1339. editToolbar.addEventListener("pointerdown", EditorToolbar.#pointerDown, {
  1340. signal
  1341. });
  1342. const buttons = this.#buttons = document.createElement("div");
  1343. buttons.className = "buttons";
  1344. editToolbar.append(buttons);
  1345. const position = this.#editor.toolbarPosition;
  1346. if (position) {
  1347. const {
  1348. style
  1349. } = editToolbar;
  1350. const x = this.#editor._uiManager.direction === "ltr" ? 1 - position[0] : position[0];
  1351. style.insetInlineEnd = `${100 * x}%`;
  1352. style.top = `calc(${100 * position[1]}% + var(--editor-toolbar-vert-offset))`;
  1353. }
  1354. this.#addDeleteButton();
  1355. return editToolbar;
  1356. }
  1357. get div() {
  1358. return this.#toolbar;
  1359. }
  1360. static #pointerDown(e) {
  1361. e.stopPropagation();
  1362. }
  1363. #focusIn(e) {
  1364. this.#editor._focusEventsAllowed = false;
  1365. stopEvent(e);
  1366. }
  1367. #focusOut(e) {
  1368. this.#editor._focusEventsAllowed = true;
  1369. stopEvent(e);
  1370. }
  1371. #addListenersToElement(element) {
  1372. const signal = this.#editor._uiManager._signal;
  1373. element.addEventListener("focusin", this.#focusIn.bind(this), {
  1374. capture: true,
  1375. signal
  1376. });
  1377. element.addEventListener("focusout", this.#focusOut.bind(this), {
  1378. capture: true,
  1379. signal
  1380. });
  1381. element.addEventListener("contextmenu", noContextMenu, {
  1382. signal
  1383. });
  1384. }
  1385. hide() {
  1386. this.#toolbar.classList.add("hidden");
  1387. this.#colorPicker?.hideDropdown();
  1388. }
  1389. show() {
  1390. this.#toolbar.classList.remove("hidden");
  1391. this.#altText?.shown();
  1392. }
  1393. #addDeleteButton() {
  1394. const {
  1395. editorType,
  1396. _uiManager
  1397. } = this.#editor;
  1398. const button = document.createElement("button");
  1399. button.className = "delete";
  1400. button.tabIndex = 0;
  1401. button.setAttribute("data-l10n-id", EditorToolbar.#l10nRemove[editorType]);
  1402. this.#addListenersToElement(button);
  1403. button.addEventListener("click", e => {
  1404. _uiManager.delete();
  1405. }, {
  1406. signal: _uiManager._signal
  1407. });
  1408. this.#buttons.append(button);
  1409. }
  1410. get #divider() {
  1411. const divider = document.createElement("div");
  1412. divider.className = "divider";
  1413. return divider;
  1414. }
  1415. async addAltText(altText) {
  1416. const button = await altText.render();
  1417. this.#addListenersToElement(button);
  1418. this.#buttons.prepend(button, this.#divider);
  1419. this.#altText = altText;
  1420. }
  1421. addColorPicker(colorPicker) {
  1422. this.#colorPicker = colorPicker;
  1423. const button = colorPicker.renderButton();
  1424. this.#addListenersToElement(button);
  1425. this.#buttons.prepend(button, this.#divider);
  1426. }
  1427. async addEditSignatureButton(signatureManager) {
  1428. const button = this.#signatureDescriptionButton = await signatureManager.renderEditButton(this.#editor);
  1429. this.#addListenersToElement(button);
  1430. this.#buttons.prepend(button, this.#divider);
  1431. }
  1432. updateEditSignatureButton(description) {
  1433. if (this.#signatureDescriptionButton) {
  1434. this.#signatureDescriptionButton.title = description;
  1435. }
  1436. }
  1437. remove() {
  1438. this.#toolbar.remove();
  1439. this.#colorPicker?.destroy();
  1440. this.#colorPicker = null;
  1441. }
  1442. }
  1443. class HighlightToolbar {
  1444. #buttons = null;
  1445. #toolbar = null;
  1446. #uiManager;
  1447. constructor(uiManager) {
  1448. this.#uiManager = uiManager;
  1449. }
  1450. #render() {
  1451. const editToolbar = this.#toolbar = document.createElement("div");
  1452. editToolbar.className = "editToolbar";
  1453. editToolbar.setAttribute("role", "toolbar");
  1454. editToolbar.addEventListener("contextmenu", noContextMenu, {
  1455. signal: this.#uiManager._signal
  1456. });
  1457. const buttons = this.#buttons = document.createElement("div");
  1458. buttons.className = "buttons";
  1459. editToolbar.append(buttons);
  1460. this.#addHighlightButton();
  1461. return editToolbar;
  1462. }
  1463. #getLastPoint(boxes, isLTR) {
  1464. let lastY = 0;
  1465. let lastX = 0;
  1466. for (const box of boxes) {
  1467. const y = box.y + box.height;
  1468. if (y < lastY) {
  1469. continue;
  1470. }
  1471. const x = box.x + (isLTR ? box.width : 0);
  1472. if (y > lastY) {
  1473. lastX = x;
  1474. lastY = y;
  1475. continue;
  1476. }
  1477. if (isLTR) {
  1478. if (x > lastX) {
  1479. lastX = x;
  1480. }
  1481. } else if (x < lastX) {
  1482. lastX = x;
  1483. }
  1484. }
  1485. return [isLTR ? 1 - lastX : lastX, lastY];
  1486. }
  1487. show(parent, boxes, isLTR) {
  1488. const [x, y] = this.#getLastPoint(boxes, isLTR);
  1489. const {
  1490. style
  1491. } = this.#toolbar ||= this.#render();
  1492. parent.append(this.#toolbar);
  1493. style.insetInlineEnd = `${100 * x}%`;
  1494. style.top = `calc(${100 * y}% + var(--editor-toolbar-vert-offset))`;
  1495. }
  1496. hide() {
  1497. this.#toolbar.remove();
  1498. }
  1499. #addHighlightButton() {
  1500. const button = document.createElement("button");
  1501. button.className = "highlightButton";
  1502. button.tabIndex = 0;
  1503. button.setAttribute("data-l10n-id", `pdfjs-highlight-floating-button1`);
  1504. const span = document.createElement("span");
  1505. button.append(span);
  1506. span.className = "visuallyHidden";
  1507. span.setAttribute("data-l10n-id", "pdfjs-highlight-floating-button-label");
  1508. const signal = this.#uiManager._signal;
  1509. button.addEventListener("contextmenu", noContextMenu, {
  1510. signal
  1511. });
  1512. button.addEventListener("click", () => {
  1513. this.#uiManager.highlightSelection("floating_button");
  1514. }, {
  1515. signal
  1516. });
  1517. this.#buttons.append(button);
  1518. }
  1519. }
  1520. ;// ./src/display/editor/tools.js
  1521. function bindEvents(obj, element, names) {
  1522. for (const name of names) {
  1523. element.addEventListener(name, obj[name].bind(obj));
  1524. }
  1525. }
  1526. class IdManager {
  1527. #id = 0;
  1528. get id() {
  1529. return `${AnnotationEditorPrefix}${this.#id++}`;
  1530. }
  1531. }
  1532. class ImageManager {
  1533. #baseId = getUuid();
  1534. #id = 0;
  1535. #cache = null;
  1536. static get _isSVGFittingCanvas() {
  1537. 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>`;
  1538. const canvas = new OffscreenCanvas(1, 3);
  1539. const ctx = canvas.getContext("2d", {
  1540. willReadFrequently: true
  1541. });
  1542. const image = new Image();
  1543. image.src = svg;
  1544. const promise = image.decode().then(() => {
  1545. ctx.drawImage(image, 0, 0, 1, 1, 0, 0, 1, 3);
  1546. return new Uint32Array(ctx.getImageData(0, 0, 1, 1).data.buffer)[0] === 0;
  1547. });
  1548. return shadow(this, "_isSVGFittingCanvas", promise);
  1549. }
  1550. async #get(key, rawData) {
  1551. this.#cache ||= new Map();
  1552. let data = this.#cache.get(key);
  1553. if (data === null) {
  1554. return null;
  1555. }
  1556. if (data?.bitmap) {
  1557. data.refCounter += 1;
  1558. return data;
  1559. }
  1560. try {
  1561. data ||= {
  1562. bitmap: null,
  1563. id: `image_${this.#baseId}_${this.#id++}`,
  1564. refCounter: 0,
  1565. isSvg: false
  1566. };
  1567. let image;
  1568. if (typeof rawData === "string") {
  1569. data.url = rawData;
  1570. image = await fetchData(rawData, "blob");
  1571. } else if (rawData instanceof File) {
  1572. image = data.file = rawData;
  1573. } else if (rawData instanceof Blob) {
  1574. image = rawData;
  1575. }
  1576. if (image.type === "image/svg+xml") {
  1577. const mustRemoveAspectRatioPromise = ImageManager._isSVGFittingCanvas;
  1578. const fileReader = new FileReader();
  1579. const imageElement = new Image();
  1580. const imagePromise = new Promise((resolve, reject) => {
  1581. imageElement.onload = () => {
  1582. data.bitmap = imageElement;
  1583. data.isSvg = true;
  1584. resolve();
  1585. };
  1586. fileReader.onload = async () => {
  1587. const url = data.svgUrl = fileReader.result;
  1588. imageElement.src = (await mustRemoveAspectRatioPromise) ? `${url}#svgView(preserveAspectRatio(none))` : url;
  1589. };
  1590. imageElement.onerror = fileReader.onerror = reject;
  1591. });
  1592. fileReader.readAsDataURL(image);
  1593. await imagePromise;
  1594. } else {
  1595. data.bitmap = await createImageBitmap(image);
  1596. }
  1597. data.refCounter = 1;
  1598. } catch (e) {
  1599. warn(e);
  1600. data = null;
  1601. }
  1602. this.#cache.set(key, data);
  1603. if (data) {
  1604. this.#cache.set(data.id, data);
  1605. }
  1606. return data;
  1607. }
  1608. async getFromFile(file) {
  1609. const {
  1610. lastModified,
  1611. name,
  1612. size,
  1613. type
  1614. } = file;
  1615. return this.#get(`${lastModified}_${name}_${size}_${type}`, file);
  1616. }
  1617. async getFromUrl(url) {
  1618. return this.#get(url, url);
  1619. }
  1620. async getFromBlob(id, blobPromise) {
  1621. const blob = await blobPromise;
  1622. return this.#get(id, blob);
  1623. }
  1624. async getFromId(id) {
  1625. this.#cache ||= new Map();
  1626. const data = this.#cache.get(id);
  1627. if (!data) {
  1628. return null;
  1629. }
  1630. if (data.bitmap) {
  1631. data.refCounter += 1;
  1632. return data;
  1633. }
  1634. if (data.file) {
  1635. return this.getFromFile(data.file);
  1636. }
  1637. if (data.blobPromise) {
  1638. const {
  1639. blobPromise
  1640. } = data;
  1641. delete data.blobPromise;
  1642. return this.getFromBlob(data.id, blobPromise);
  1643. }
  1644. return this.getFromUrl(data.url);
  1645. }
  1646. getFromCanvas(id, canvas) {
  1647. this.#cache ||= new Map();
  1648. let data = this.#cache.get(id);
  1649. if (data?.bitmap) {
  1650. data.refCounter += 1;
  1651. return data;
  1652. }
  1653. const offscreen = new OffscreenCanvas(canvas.width, canvas.height);
  1654. const ctx = offscreen.getContext("2d");
  1655. ctx.drawImage(canvas, 0, 0);
  1656. data = {
  1657. bitmap: offscreen.transferToImageBitmap(),
  1658. id: `image_${this.#baseId}_${this.#id++}`,
  1659. refCounter: 1,
  1660. isSvg: false
  1661. };
  1662. this.#cache.set(id, data);
  1663. this.#cache.set(data.id, data);
  1664. return data;
  1665. }
  1666. getSvgUrl(id) {
  1667. const data = this.#cache.get(id);
  1668. if (!data?.isSvg) {
  1669. return null;
  1670. }
  1671. return data.svgUrl;
  1672. }
  1673. deleteId(id) {
  1674. this.#cache ||= new Map();
  1675. const data = this.#cache.get(id);
  1676. if (!data) {
  1677. return;
  1678. }
  1679. data.refCounter -= 1;
  1680. if (data.refCounter !== 0) {
  1681. return;
  1682. }
  1683. const {
  1684. bitmap
  1685. } = data;
  1686. if (!data.url && !data.file) {
  1687. const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
  1688. const ctx = canvas.getContext("bitmaprenderer");
  1689. ctx.transferFromImageBitmap(bitmap);
  1690. data.blobPromise = canvas.convertToBlob();
  1691. }
  1692. bitmap.close?.();
  1693. data.bitmap = null;
  1694. }
  1695. isValidId(id) {
  1696. return id.startsWith(`image_${this.#baseId}_`);
  1697. }
  1698. }
  1699. class CommandManager {
  1700. #commands = [];
  1701. #locked = false;
  1702. #maxSize;
  1703. #position = -1;
  1704. constructor(maxSize = 128) {
  1705. this.#maxSize = maxSize;
  1706. }
  1707. add({
  1708. cmd,
  1709. undo,
  1710. post,
  1711. mustExec,
  1712. type = NaN,
  1713. overwriteIfSameType = false,
  1714. keepUndo = false
  1715. }) {
  1716. if (mustExec) {
  1717. cmd();
  1718. }
  1719. if (this.#locked) {
  1720. return;
  1721. }
  1722. const save = {
  1723. cmd,
  1724. undo,
  1725. post,
  1726. type
  1727. };
  1728. if (this.#position === -1) {
  1729. if (this.#commands.length > 0) {
  1730. this.#commands.length = 0;
  1731. }
  1732. this.#position = 0;
  1733. this.#commands.push(save);
  1734. return;
  1735. }
  1736. if (overwriteIfSameType && this.#commands[this.#position].type === type) {
  1737. if (keepUndo) {
  1738. save.undo = this.#commands[this.#position].undo;
  1739. }
  1740. this.#commands[this.#position] = save;
  1741. return;
  1742. }
  1743. const next = this.#position + 1;
  1744. if (next === this.#maxSize) {
  1745. this.#commands.splice(0, 1);
  1746. } else {
  1747. this.#position = next;
  1748. if (next < this.#commands.length) {
  1749. this.#commands.splice(next);
  1750. }
  1751. }
  1752. this.#commands.push(save);
  1753. }
  1754. undo() {
  1755. if (this.#position === -1) {
  1756. return;
  1757. }
  1758. this.#locked = true;
  1759. const {
  1760. undo,
  1761. post
  1762. } = this.#commands[this.#position];
  1763. undo();
  1764. post?.();
  1765. this.#locked = false;
  1766. this.#position -= 1;
  1767. }
  1768. redo() {
  1769. if (this.#position < this.#commands.length - 1) {
  1770. this.#position += 1;
  1771. this.#locked = true;
  1772. const {
  1773. cmd,
  1774. post
  1775. } = this.#commands[this.#position];
  1776. cmd();
  1777. post?.();
  1778. this.#locked = false;
  1779. }
  1780. }
  1781. hasSomethingToUndo() {
  1782. return this.#position !== -1;
  1783. }
  1784. hasSomethingToRedo() {
  1785. return this.#position < this.#commands.length - 1;
  1786. }
  1787. cleanType(type) {
  1788. if (this.#position === -1) {
  1789. return;
  1790. }
  1791. for (let i = this.#position; i >= 0; i--) {
  1792. if (this.#commands[i].type !== type) {
  1793. this.#commands.splice(i + 1, this.#position - i);
  1794. this.#position = i;
  1795. return;
  1796. }
  1797. }
  1798. this.#commands.length = 0;
  1799. this.#position = -1;
  1800. }
  1801. destroy() {
  1802. this.#commands = null;
  1803. }
  1804. }
  1805. class KeyboardManager {
  1806. constructor(callbacks) {
  1807. this.buffer = [];
  1808. this.callbacks = new Map();
  1809. this.allKeys = new Set();
  1810. const {
  1811. isMac
  1812. } = util_FeatureTest.platform;
  1813. for (const [keys, callback, options = {}] of callbacks) {
  1814. for (const key of keys) {
  1815. const isMacKey = key.startsWith("mac+");
  1816. if (isMac && isMacKey) {
  1817. this.callbacks.set(key.slice(4), {
  1818. callback,
  1819. options
  1820. });
  1821. this.allKeys.add(key.split("+").at(-1));
  1822. } else if (!isMac && !isMacKey) {
  1823. this.callbacks.set(key, {
  1824. callback,
  1825. options
  1826. });
  1827. this.allKeys.add(key.split("+").at(-1));
  1828. }
  1829. }
  1830. }
  1831. }
  1832. #serialize(event) {
  1833. if (event.altKey) {
  1834. this.buffer.push("alt");
  1835. }
  1836. if (event.ctrlKey) {
  1837. this.buffer.push("ctrl");
  1838. }
  1839. if (event.metaKey) {
  1840. this.buffer.push("meta");
  1841. }
  1842. if (event.shiftKey) {
  1843. this.buffer.push("shift");
  1844. }
  1845. this.buffer.push(event.key);
  1846. const str = this.buffer.join("+");
  1847. this.buffer.length = 0;
  1848. return str;
  1849. }
  1850. exec(self, event) {
  1851. if (!this.allKeys.has(event.key)) {
  1852. return;
  1853. }
  1854. const info = this.callbacks.get(this.#serialize(event));
  1855. if (!info) {
  1856. return;
  1857. }
  1858. const {
  1859. callback,
  1860. options: {
  1861. bubbles = false,
  1862. args = [],
  1863. checker = null
  1864. }
  1865. } = info;
  1866. if (checker && !checker(self, event)) {
  1867. return;
  1868. }
  1869. callback.bind(self, ...args, event)();
  1870. if (!bubbles) {
  1871. stopEvent(event);
  1872. }
  1873. }
  1874. }
  1875. class ColorManager {
  1876. static _colorsMapping = new Map([["CanvasText", [0, 0, 0]], ["Canvas", [255, 255, 255]]]);
  1877. get _colors() {
  1878. const colors = new Map([["CanvasText", null], ["Canvas", null]]);
  1879. getColorValues(colors);
  1880. return shadow(this, "_colors", colors);
  1881. }
  1882. convert(color) {
  1883. const rgb = getRGB(color);
  1884. if (!window.matchMedia("(forced-colors: active)").matches) {
  1885. return rgb;
  1886. }
  1887. for (const [name, RGB] of this._colors) {
  1888. if (RGB.every((x, i) => x === rgb[i])) {
  1889. return ColorManager._colorsMapping.get(name);
  1890. }
  1891. }
  1892. return rgb;
  1893. }
  1894. getHexCode(name) {
  1895. const rgb = this._colors.get(name);
  1896. if (!rgb) {
  1897. return name;
  1898. }
  1899. return Util.makeHexColor(...rgb);
  1900. }
  1901. }
  1902. class AnnotationEditorUIManager {
  1903. #abortController = new AbortController();
  1904. #activeEditor = null;
  1905. #allEditors = new Map();
  1906. #allLayers = new Map();
  1907. #altTextManager = null;
  1908. #annotationStorage = null;
  1909. #changedExistingAnnotations = null;
  1910. #commandManager = new CommandManager();
  1911. #copyPasteAC = null;
  1912. #currentDrawingSession = null;
  1913. #currentPageIndex = 0;
  1914. #deletedAnnotationsElementIds = new Set();
  1915. #draggingEditors = null;
  1916. #editorTypes = null;
  1917. #editorsToRescale = new Set();
  1918. _editorUndoBar = null;
  1919. #enableHighlightFloatingButton = false;
  1920. #enableUpdatedAddImage = false;
  1921. #enableNewAltTextWhenAddingImage = false;
  1922. #filterFactory = null;
  1923. #focusMainContainerTimeoutId = null;
  1924. #focusManagerAC = null;
  1925. #highlightColors = null;
  1926. #highlightWhenShiftUp = false;
  1927. #highlightToolbar = null;
  1928. #idManager = new IdManager();
  1929. #isEnabled = false;
  1930. #isWaiting = false;
  1931. #keyboardManagerAC = null;
  1932. #lastActiveElement = null;
  1933. #mainHighlightColorPicker = null;
  1934. #missingCanvases = null;
  1935. #mlManager = null;
  1936. #mode = AnnotationEditorType.NONE;
  1937. #selectedEditors = new Set();
  1938. #selectedTextNode = null;
  1939. #signatureManager = null;
  1940. #pageColors = null;
  1941. #showAllStates = null;
  1942. #previousStates = {
  1943. isEditing: false,
  1944. isEmpty: true,
  1945. hasSomethingToUndo: false,
  1946. hasSomethingToRedo: false,
  1947. hasSelectedEditor: false,
  1948. hasSelectedText: false
  1949. };
  1950. #translation = [0, 0];
  1951. #translationTimeoutId = null;
  1952. #container = null;
  1953. #viewer = null;
  1954. #updateModeCapability = null;
  1955. static TRANSLATE_SMALL = 1;
  1956. static TRANSLATE_BIG = 10;
  1957. static get _keyboardManager() {
  1958. const proto = AnnotationEditorUIManager.prototype;
  1959. const arrowChecker = self => self.#container.contains(document.activeElement) && document.activeElement.tagName !== "BUTTON" && self.hasSomethingToControl();
  1960. const textInputChecker = (_self, {
  1961. target: el
  1962. }) => {
  1963. if (el instanceof HTMLInputElement) {
  1964. const {
  1965. type
  1966. } = el;
  1967. return type !== "text" && type !== "number";
  1968. }
  1969. return true;
  1970. };
  1971. const small = this.TRANSLATE_SMALL;
  1972. const big = this.TRANSLATE_BIG;
  1973. return shadow(this, "_keyboardManager", new KeyboardManager([[["ctrl+a", "mac+meta+a"], proto.selectAll, {
  1974. checker: textInputChecker
  1975. }], [["ctrl+z", "mac+meta+z"], proto.undo, {
  1976. checker: textInputChecker
  1977. }], [["ctrl+y", "ctrl+shift+z", "mac+meta+shift+z", "ctrl+shift+Z", "mac+meta+shift+Z"], proto.redo, {
  1978. checker: textInputChecker
  1979. }], [["Backspace", "alt+Backspace", "ctrl+Backspace", "shift+Backspace", "mac+Backspace", "mac+alt+Backspace", "mac+ctrl+Backspace", "Delete", "ctrl+Delete", "shift+Delete", "mac+Delete"], proto.delete, {
  1980. checker: textInputChecker
  1981. }], [["Enter", "mac+Enter"], proto.addNewEditorFromKeyboard, {
  1982. checker: (self, {
  1983. target: el
  1984. }) => !(el instanceof HTMLButtonElement) && self.#container.contains(el) && !self.isEnterHandled
  1985. }], [[" ", "mac+ "], proto.addNewEditorFromKeyboard, {
  1986. checker: (self, {
  1987. target: el
  1988. }) => !(el instanceof HTMLButtonElement) && self.#container.contains(document.activeElement)
  1989. }], [["Escape", "mac+Escape"], proto.unselectAll], [["ArrowLeft", "mac+ArrowLeft"], proto.translateSelectedEditors, {
  1990. args: [-small, 0],
  1991. checker: arrowChecker
  1992. }], [["ctrl+ArrowLeft", "mac+shift+ArrowLeft"], proto.translateSelectedEditors, {
  1993. args: [-big, 0],
  1994. checker: arrowChecker
  1995. }], [["ArrowRight", "mac+ArrowRight"], proto.translateSelectedEditors, {
  1996. args: [small, 0],
  1997. checker: arrowChecker
  1998. }], [["ctrl+ArrowRight", "mac+shift+ArrowRight"], proto.translateSelectedEditors, {
  1999. args: [big, 0],
  2000. checker: arrowChecker
  2001. }], [["ArrowUp", "mac+ArrowUp"], proto.translateSelectedEditors, {
  2002. args: [0, -small],
  2003. checker: arrowChecker
  2004. }], [["ctrl+ArrowUp", "mac+shift+ArrowUp"], proto.translateSelectedEditors, {
  2005. args: [0, -big],
  2006. checker: arrowChecker
  2007. }], [["ArrowDown", "mac+ArrowDown"], proto.translateSelectedEditors, {
  2008. args: [0, small],
  2009. checker: arrowChecker
  2010. }], [["ctrl+ArrowDown", "mac+shift+ArrowDown"], proto.translateSelectedEditors, {
  2011. args: [0, big],
  2012. checker: arrowChecker
  2013. }]]));
  2014. }
  2015. constructor(container, viewer, altTextManager, signatureManager, eventBus, pdfDocument, pageColors, highlightColors, enableHighlightFloatingButton, enableUpdatedAddImage, enableNewAltTextWhenAddingImage, mlManager, editorUndoBar, supportsPinchToZoom) {
  2016. const signal = this._signal = this.#abortController.signal;
  2017. this.#container = container;
  2018. this.#viewer = viewer;
  2019. this.#altTextManager = altTextManager;
  2020. this.#signatureManager = signatureManager;
  2021. this._eventBus = eventBus;
  2022. eventBus._on("editingaction", this.onEditingAction.bind(this), {
  2023. signal
  2024. });
  2025. eventBus._on("pagechanging", this.onPageChanging.bind(this), {
  2026. signal
  2027. });
  2028. eventBus._on("scalechanging", this.onScaleChanging.bind(this), {
  2029. signal
  2030. });
  2031. eventBus._on("rotationchanging", this.onRotationChanging.bind(this), {
  2032. signal
  2033. });
  2034. eventBus._on("setpreference", this.onSetPreference.bind(this), {
  2035. signal
  2036. });
  2037. eventBus._on("switchannotationeditorparams", evt => this.updateParams(evt.type, evt.value), {
  2038. signal
  2039. });
  2040. this.#addSelectionListener();
  2041. this.#addDragAndDropListeners();
  2042. this.#addKeyboardManager();
  2043. this.#annotationStorage = pdfDocument.annotationStorage;
  2044. this.#filterFactory = pdfDocument.filterFactory;
  2045. this.#pageColors = pageColors;
  2046. this.#highlightColors = highlightColors || null;
  2047. this.#enableHighlightFloatingButton = enableHighlightFloatingButton;
  2048. this.#enableUpdatedAddImage = enableUpdatedAddImage;
  2049. this.#enableNewAltTextWhenAddingImage = enableNewAltTextWhenAddingImage;
  2050. this.#mlManager = mlManager || null;
  2051. this.viewParameters = {
  2052. realScale: PixelsPerInch.PDF_TO_CSS_UNITS,
  2053. rotation: 0
  2054. };
  2055. this.isShiftKeyDown = false;
  2056. this._editorUndoBar = editorUndoBar || null;
  2057. this._supportsPinchToZoom = supportsPinchToZoom !== false;
  2058. }
  2059. destroy() {
  2060. this.#updateModeCapability?.resolve();
  2061. this.#updateModeCapability = null;
  2062. this.#abortController?.abort();
  2063. this.#abortController = null;
  2064. this._signal = null;
  2065. for (const layer of this.#allLayers.values()) {
  2066. layer.destroy();
  2067. }
  2068. this.#allLayers.clear();
  2069. this.#allEditors.clear();
  2070. this.#editorsToRescale.clear();
  2071. this.#missingCanvases?.clear();
  2072. this.#activeEditor = null;
  2073. this.#selectedEditors.clear();
  2074. this.#commandManager.destroy();
  2075. this.#altTextManager?.destroy();
  2076. this.#signatureManager?.destroy();
  2077. this.#highlightToolbar?.hide();
  2078. this.#highlightToolbar = null;
  2079. this.#mainHighlightColorPicker?.destroy();
  2080. this.#mainHighlightColorPicker = null;
  2081. if (this.#focusMainContainerTimeoutId) {
  2082. clearTimeout(this.#focusMainContainerTimeoutId);
  2083. this.#focusMainContainerTimeoutId = null;
  2084. }
  2085. if (this.#translationTimeoutId) {
  2086. clearTimeout(this.#translationTimeoutId);
  2087. this.#translationTimeoutId = null;
  2088. }
  2089. this._editorUndoBar?.destroy();
  2090. }
  2091. combinedSignal(ac) {
  2092. return AbortSignal.any([this._signal, ac.signal]);
  2093. }
  2094. get mlManager() {
  2095. return this.#mlManager;
  2096. }
  2097. get useNewAltTextFlow() {
  2098. return this.#enableUpdatedAddImage;
  2099. }
  2100. get useNewAltTextWhenAddingImage() {
  2101. return this.#enableNewAltTextWhenAddingImage;
  2102. }
  2103. get hcmFilter() {
  2104. return shadow(this, "hcmFilter", this.#pageColors ? this.#filterFactory.addHCMFilter(this.#pageColors.foreground, this.#pageColors.background) : "none");
  2105. }
  2106. get direction() {
  2107. return shadow(this, "direction", getComputedStyle(this.#container).direction);
  2108. }
  2109. get highlightColors() {
  2110. return shadow(this, "highlightColors", this.#highlightColors ? new Map(this.#highlightColors.split(",").map(pair => pair.split("=").map(x => x.trim()))) : null);
  2111. }
  2112. get highlightColorNames() {
  2113. return shadow(this, "highlightColorNames", this.highlightColors ? new Map(Array.from(this.highlightColors, e => e.reverse())) : null);
  2114. }
  2115. setCurrentDrawingSession(layer) {
  2116. if (layer) {
  2117. this.unselectAll();
  2118. this.disableUserSelect(true);
  2119. } else {
  2120. this.disableUserSelect(false);
  2121. }
  2122. this.#currentDrawingSession = layer;
  2123. }
  2124. setMainHighlightColorPicker(colorPicker) {
  2125. this.#mainHighlightColorPicker = colorPicker;
  2126. }
  2127. editAltText(editor, firstTime = false) {
  2128. this.#altTextManager?.editAltText(this, editor, firstTime);
  2129. }
  2130. getSignature(editor) {
  2131. this.#signatureManager?.getSignature({
  2132. uiManager: this,
  2133. editor
  2134. });
  2135. }
  2136. get signatureManager() {
  2137. return this.#signatureManager;
  2138. }
  2139. switchToMode(mode, callback) {
  2140. this._eventBus.on("annotationeditormodechanged", callback, {
  2141. once: true,
  2142. signal: this._signal
  2143. });
  2144. this._eventBus.dispatch("showannotationeditorui", {
  2145. source: this,
  2146. mode
  2147. });
  2148. }
  2149. setPreference(name, value) {
  2150. this._eventBus.dispatch("setpreference", {
  2151. source: this,
  2152. name,
  2153. value
  2154. });
  2155. }
  2156. onSetPreference({
  2157. name,
  2158. value
  2159. }) {
  2160. switch (name) {
  2161. case "enableNewAltTextWhenAddingImage":
  2162. this.#enableNewAltTextWhenAddingImage = value;
  2163. break;
  2164. }
  2165. }
  2166. onPageChanging({
  2167. pageNumber
  2168. }) {
  2169. this.#currentPageIndex = pageNumber - 1;
  2170. }
  2171. focusMainContainer() {
  2172. this.#container.focus();
  2173. }
  2174. findParent(x, y) {
  2175. for (const layer of this.#allLayers.values()) {
  2176. const {
  2177. x: layerX,
  2178. y: layerY,
  2179. width,
  2180. height
  2181. } = layer.div.getBoundingClientRect();
  2182. if (x >= layerX && x <= layerX + width && y >= layerY && y <= layerY + height) {
  2183. return layer;
  2184. }
  2185. }
  2186. return null;
  2187. }
  2188. disableUserSelect(value = false) {
  2189. this.#viewer.classList.toggle("noUserSelect", value);
  2190. }
  2191. addShouldRescale(editor) {
  2192. this.#editorsToRescale.add(editor);
  2193. }
  2194. removeShouldRescale(editor) {
  2195. this.#editorsToRescale.delete(editor);
  2196. }
  2197. onScaleChanging({
  2198. scale
  2199. }) {
  2200. this.commitOrRemove();
  2201. this.viewParameters.realScale = scale * PixelsPerInch.PDF_TO_CSS_UNITS;
  2202. for (const editor of this.#editorsToRescale) {
  2203. editor.onScaleChanging();
  2204. }
  2205. this.#currentDrawingSession?.onScaleChanging();
  2206. }
  2207. onRotationChanging({
  2208. pagesRotation
  2209. }) {
  2210. this.commitOrRemove();
  2211. this.viewParameters.rotation = pagesRotation;
  2212. }
  2213. #getAnchorElementForSelection({
  2214. anchorNode
  2215. }) {
  2216. return anchorNode.nodeType === Node.TEXT_NODE ? anchorNode.parentElement : anchorNode;
  2217. }
  2218. #getLayerForTextLayer(textLayer) {
  2219. const {
  2220. currentLayer
  2221. } = this;
  2222. if (currentLayer.hasTextLayer(textLayer)) {
  2223. return currentLayer;
  2224. }
  2225. for (const layer of this.#allLayers.values()) {
  2226. if (layer.hasTextLayer(textLayer)) {
  2227. return layer;
  2228. }
  2229. }
  2230. return null;
  2231. }
  2232. highlightSelection(methodOfCreation = "") {
  2233. const selection = document.getSelection();
  2234. if (!selection || selection.isCollapsed) {
  2235. return;
  2236. }
  2237. const {
  2238. anchorNode,
  2239. anchorOffset,
  2240. focusNode,
  2241. focusOffset
  2242. } = selection;
  2243. const text = selection.toString();
  2244. const anchorElement = this.#getAnchorElementForSelection(selection);
  2245. const textLayer = anchorElement.closest(".textLayer");
  2246. const boxes = this.getSelectionBoxes(textLayer);
  2247. if (!boxes) {
  2248. return;
  2249. }
  2250. selection.empty();
  2251. const layer = this.#getLayerForTextLayer(textLayer);
  2252. const isNoneMode = this.#mode === AnnotationEditorType.NONE;
  2253. const callback = () => {
  2254. layer?.createAndAddNewEditor({
  2255. x: 0,
  2256. y: 0
  2257. }, false, {
  2258. methodOfCreation,
  2259. boxes,
  2260. anchorNode,
  2261. anchorOffset,
  2262. focusNode,
  2263. focusOffset,
  2264. text
  2265. });
  2266. if (isNoneMode) {
  2267. this.showAllEditors("highlight", true, true);
  2268. }
  2269. };
  2270. if (isNoneMode) {
  2271. this.switchToMode(AnnotationEditorType.HIGHLIGHT, callback);
  2272. return;
  2273. }
  2274. callback();
  2275. }
  2276. #displayHighlightToolbar() {
  2277. const selection = document.getSelection();
  2278. if (!selection || selection.isCollapsed) {
  2279. return;
  2280. }
  2281. const anchorElement = this.#getAnchorElementForSelection(selection);
  2282. const textLayer = anchorElement.closest(".textLayer");
  2283. const boxes = this.getSelectionBoxes(textLayer);
  2284. if (!boxes) {
  2285. return;
  2286. }
  2287. this.#highlightToolbar ||= new HighlightToolbar(this);
  2288. this.#highlightToolbar.show(textLayer, boxes, this.direction === "ltr");
  2289. }
  2290. addToAnnotationStorage(editor) {
  2291. if (!editor.isEmpty() && this.#annotationStorage && !this.#annotationStorage.has(editor.id)) {
  2292. this.#annotationStorage.setValue(editor.id, editor);
  2293. }
  2294. }
  2295. #selectionChange() {
  2296. const selection = document.getSelection();
  2297. if (!selection || selection.isCollapsed) {
  2298. if (this.#selectedTextNode) {
  2299. this.#highlightToolbar?.hide();
  2300. this.#selectedTextNode = null;
  2301. this.#dispatchUpdateStates({
  2302. hasSelectedText: false
  2303. });
  2304. }
  2305. return;
  2306. }
  2307. const {
  2308. anchorNode
  2309. } = selection;
  2310. if (anchorNode === this.#selectedTextNode) {
  2311. return;
  2312. }
  2313. const anchorElement = this.#getAnchorElementForSelection(selection);
  2314. const textLayer = anchorElement.closest(".textLayer");
  2315. if (!textLayer) {
  2316. if (this.#selectedTextNode) {
  2317. this.#highlightToolbar?.hide();
  2318. this.#selectedTextNode = null;
  2319. this.#dispatchUpdateStates({
  2320. hasSelectedText: false
  2321. });
  2322. }
  2323. return;
  2324. }
  2325. this.#highlightToolbar?.hide();
  2326. this.#selectedTextNode = anchorNode;
  2327. this.#dispatchUpdateStates({
  2328. hasSelectedText: true
  2329. });
  2330. if (this.#mode !== AnnotationEditorType.HIGHLIGHT && this.#mode !== AnnotationEditorType.NONE) {
  2331. return;
  2332. }
  2333. if (this.#mode === AnnotationEditorType.HIGHLIGHT) {
  2334. this.showAllEditors("highlight", true, true);
  2335. }
  2336. this.#highlightWhenShiftUp = this.isShiftKeyDown;
  2337. if (!this.isShiftKeyDown) {
  2338. const activeLayer = this.#mode === AnnotationEditorType.HIGHLIGHT ? this.#getLayerForTextLayer(textLayer) : null;
  2339. activeLayer?.toggleDrawing();
  2340. const ac = new AbortController();
  2341. const signal = this.combinedSignal(ac);
  2342. const pointerup = e => {
  2343. if (e.type === "pointerup" && e.button !== 0) {
  2344. return;
  2345. }
  2346. ac.abort();
  2347. activeLayer?.toggleDrawing(true);
  2348. if (e.type === "pointerup") {
  2349. this.#onSelectEnd("main_toolbar");
  2350. }
  2351. };
  2352. window.addEventListener("pointerup", pointerup, {
  2353. signal
  2354. });
  2355. window.addEventListener("blur", pointerup, {
  2356. signal
  2357. });
  2358. }
  2359. }
  2360. #onSelectEnd(methodOfCreation = "") {
  2361. if (this.#mode === AnnotationEditorType.HIGHLIGHT) {
  2362. this.highlightSelection(methodOfCreation);
  2363. } else if (this.#enableHighlightFloatingButton) {
  2364. this.#displayHighlightToolbar();
  2365. }
  2366. }
  2367. #addSelectionListener() {
  2368. document.addEventListener("selectionchange", this.#selectionChange.bind(this), {
  2369. signal: this._signal
  2370. });
  2371. }
  2372. #addFocusManager() {
  2373. if (this.#focusManagerAC) {
  2374. return;
  2375. }
  2376. this.#focusManagerAC = new AbortController();
  2377. const signal = this.combinedSignal(this.#focusManagerAC);
  2378. window.addEventListener("focus", this.focus.bind(this), {
  2379. signal
  2380. });
  2381. window.addEventListener("blur", this.blur.bind(this), {
  2382. signal
  2383. });
  2384. }
  2385. #removeFocusManager() {
  2386. this.#focusManagerAC?.abort();
  2387. this.#focusManagerAC = null;
  2388. }
  2389. blur() {
  2390. this.isShiftKeyDown = false;
  2391. if (this.#highlightWhenShiftUp) {
  2392. this.#highlightWhenShiftUp = false;
  2393. this.#onSelectEnd("main_toolbar");
  2394. }
  2395. if (!this.hasSelection) {
  2396. return;
  2397. }
  2398. const {
  2399. activeElement
  2400. } = document;
  2401. for (const editor of this.#selectedEditors) {
  2402. if (editor.div.contains(activeElement)) {
  2403. this.#lastActiveElement = [editor, activeElement];
  2404. editor._focusEventsAllowed = false;
  2405. break;
  2406. }
  2407. }
  2408. }
  2409. focus() {
  2410. if (!this.#lastActiveElement) {
  2411. return;
  2412. }
  2413. const [lastEditor, lastActiveElement] = this.#lastActiveElement;
  2414. this.#lastActiveElement = null;
  2415. lastActiveElement.addEventListener("focusin", () => {
  2416. lastEditor._focusEventsAllowed = true;
  2417. }, {
  2418. once: true,
  2419. signal: this._signal
  2420. });
  2421. lastActiveElement.focus();
  2422. }
  2423. #addKeyboardManager() {
  2424. if (this.#keyboardManagerAC) {
  2425. return;
  2426. }
  2427. this.#keyboardManagerAC = new AbortController();
  2428. const signal = this.combinedSignal(this.#keyboardManagerAC);
  2429. window.addEventListener("keydown", this.keydown.bind(this), {
  2430. signal
  2431. });
  2432. window.addEventListener("keyup", this.keyup.bind(this), {
  2433. signal
  2434. });
  2435. }
  2436. #removeKeyboardManager() {
  2437. this.#keyboardManagerAC?.abort();
  2438. this.#keyboardManagerAC = null;
  2439. }
  2440. #addCopyPasteListeners() {
  2441. if (this.#copyPasteAC) {
  2442. return;
  2443. }
  2444. this.#copyPasteAC = new AbortController();
  2445. const signal = this.combinedSignal(this.#copyPasteAC);
  2446. document.addEventListener("copy", this.copy.bind(this), {
  2447. signal
  2448. });
  2449. document.addEventListener("cut", this.cut.bind(this), {
  2450. signal
  2451. });
  2452. document.addEventListener("paste", this.paste.bind(this), {
  2453. signal
  2454. });
  2455. }
  2456. #removeCopyPasteListeners() {
  2457. this.#copyPasteAC?.abort();
  2458. this.#copyPasteAC = null;
  2459. }
  2460. #addDragAndDropListeners() {
  2461. const signal = this._signal;
  2462. document.addEventListener("dragover", this.dragOver.bind(this), {
  2463. signal
  2464. });
  2465. document.addEventListener("drop", this.drop.bind(this), {
  2466. signal
  2467. });
  2468. }
  2469. addEditListeners() {
  2470. this.#addKeyboardManager();
  2471. this.#addCopyPasteListeners();
  2472. }
  2473. removeEditListeners() {
  2474. this.#removeKeyboardManager();
  2475. this.#removeCopyPasteListeners();
  2476. }
  2477. dragOver(event) {
  2478. for (const {
  2479. type
  2480. } of event.dataTransfer.items) {
  2481. for (const editorType of this.#editorTypes) {
  2482. if (editorType.isHandlingMimeForPasting(type)) {
  2483. event.dataTransfer.dropEffect = "copy";
  2484. event.preventDefault();
  2485. return;
  2486. }
  2487. }
  2488. }
  2489. }
  2490. drop(event) {
  2491. for (const item of event.dataTransfer.items) {
  2492. for (const editorType of this.#editorTypes) {
  2493. if (editorType.isHandlingMimeForPasting(item.type)) {
  2494. editorType.paste(item, this.currentLayer);
  2495. event.preventDefault();
  2496. return;
  2497. }
  2498. }
  2499. }
  2500. }
  2501. copy(event) {
  2502. event.preventDefault();
  2503. this.#activeEditor?.commitOrRemove();
  2504. if (!this.hasSelection) {
  2505. return;
  2506. }
  2507. const editors = [];
  2508. for (const editor of this.#selectedEditors) {
  2509. const serialized = editor.serialize(true);
  2510. if (serialized) {
  2511. editors.push(serialized);
  2512. }
  2513. }
  2514. if (editors.length === 0) {
  2515. return;
  2516. }
  2517. event.clipboardData.setData("application/pdfjs", JSON.stringify(editors));
  2518. }
  2519. cut(event) {
  2520. this.copy(event);
  2521. this.delete();
  2522. }
  2523. async paste(event) {
  2524. event.preventDefault();
  2525. const {
  2526. clipboardData
  2527. } = event;
  2528. for (const item of clipboardData.items) {
  2529. for (const editorType of this.#editorTypes) {
  2530. if (editorType.isHandlingMimeForPasting(item.type)) {
  2531. editorType.paste(item, this.currentLayer);
  2532. return;
  2533. }
  2534. }
  2535. }
  2536. let data = clipboardData.getData("application/pdfjs");
  2537. if (!data) {
  2538. return;
  2539. }
  2540. try {
  2541. data = JSON.parse(data);
  2542. } catch (ex) {
  2543. warn(`paste: "${ex.message}".`);
  2544. return;
  2545. }
  2546. if (!Array.isArray(data)) {
  2547. return;
  2548. }
  2549. this.unselectAll();
  2550. const layer = this.currentLayer;
  2551. try {
  2552. const newEditors = [];
  2553. for (const editor of data) {
  2554. const deserializedEditor = await layer.deserialize(editor);
  2555. if (!deserializedEditor) {
  2556. return;
  2557. }
  2558. newEditors.push(deserializedEditor);
  2559. }
  2560. const cmd = () => {
  2561. for (const editor of newEditors) {
  2562. this.#addEditorToLayer(editor);
  2563. }
  2564. this.#selectEditors(newEditors);
  2565. };
  2566. const undo = () => {
  2567. for (const editor of newEditors) {
  2568. editor.remove();
  2569. }
  2570. };
  2571. this.addCommands({
  2572. cmd,
  2573. undo,
  2574. mustExec: true
  2575. });
  2576. } catch (ex) {
  2577. warn(`paste: "${ex.message}".`);
  2578. }
  2579. }
  2580. keydown(event) {
  2581. if (!this.isShiftKeyDown && event.key === "Shift") {
  2582. this.isShiftKeyDown = true;
  2583. }
  2584. if (this.#mode !== AnnotationEditorType.NONE && !this.isEditorHandlingKeyboard) {
  2585. AnnotationEditorUIManager._keyboardManager.exec(this, event);
  2586. }
  2587. }
  2588. keyup(event) {
  2589. if (this.isShiftKeyDown && event.key === "Shift") {
  2590. this.isShiftKeyDown = false;
  2591. if (this.#highlightWhenShiftUp) {
  2592. this.#highlightWhenShiftUp = false;
  2593. this.#onSelectEnd("main_toolbar");
  2594. }
  2595. }
  2596. }
  2597. onEditingAction({
  2598. name
  2599. }) {
  2600. switch (name) {
  2601. case "undo":
  2602. case "redo":
  2603. case "delete":
  2604. case "selectAll":
  2605. this[name]();
  2606. break;
  2607. case "highlightSelection":
  2608. this.highlightSelection("context_menu");
  2609. break;
  2610. }
  2611. }
  2612. #dispatchUpdateStates(details) {
  2613. const hasChanged = Object.entries(details).some(([key, value]) => this.#previousStates[key] !== value);
  2614. if (hasChanged) {
  2615. this._eventBus.dispatch("annotationeditorstateschanged", {
  2616. source: this,
  2617. details: Object.assign(this.#previousStates, details)
  2618. });
  2619. if (this.#mode === AnnotationEditorType.HIGHLIGHT && details.hasSelectedEditor === false) {
  2620. this.#dispatchUpdateUI([[AnnotationEditorParamsType.HIGHLIGHT_FREE, true]]);
  2621. }
  2622. }
  2623. }
  2624. #dispatchUpdateUI(details) {
  2625. this._eventBus.dispatch("annotationeditorparamschanged", {
  2626. source: this,
  2627. details
  2628. });
  2629. }
  2630. setEditingState(isEditing) {
  2631. if (isEditing) {
  2632. this.#addFocusManager();
  2633. this.#addCopyPasteListeners();
  2634. this.#dispatchUpdateStates({
  2635. isEditing: this.#mode !== AnnotationEditorType.NONE,
  2636. isEmpty: this.#isEmpty(),
  2637. hasSomethingToUndo: this.#commandManager.hasSomethingToUndo(),
  2638. hasSomethingToRedo: this.#commandManager.hasSomethingToRedo(),
  2639. hasSelectedEditor: false
  2640. });
  2641. } else {
  2642. this.#removeFocusManager();
  2643. this.#removeCopyPasteListeners();
  2644. this.#dispatchUpdateStates({
  2645. isEditing: false
  2646. });
  2647. this.disableUserSelect(false);
  2648. }
  2649. }
  2650. registerEditorTypes(types) {
  2651. if (this.#editorTypes) {
  2652. return;
  2653. }
  2654. this.#editorTypes = types;
  2655. for (const editorType of this.#editorTypes) {
  2656. this.#dispatchUpdateUI(editorType.defaultPropertiesToUpdate);
  2657. }
  2658. }
  2659. getId() {
  2660. return this.#idManager.id;
  2661. }
  2662. get currentLayer() {
  2663. return this.#allLayers.get(this.#currentPageIndex);
  2664. }
  2665. getLayer(pageIndex) {
  2666. return this.#allLayers.get(pageIndex);
  2667. }
  2668. get currentPageIndex() {
  2669. return this.#currentPageIndex;
  2670. }
  2671. addLayer(layer) {
  2672. this.#allLayers.set(layer.pageIndex, layer);
  2673. if (this.#isEnabled) {
  2674. layer.enable();
  2675. } else {
  2676. layer.disable();
  2677. }
  2678. }
  2679. removeLayer(layer) {
  2680. this.#allLayers.delete(layer.pageIndex);
  2681. }
  2682. async updateMode(mode, editId = null, isFromKeyboard = false) {
  2683. if (this.#mode === mode) {
  2684. return;
  2685. }
  2686. if (this.#updateModeCapability) {
  2687. await this.#updateModeCapability.promise;
  2688. if (!this.#updateModeCapability) {
  2689. return;
  2690. }
  2691. }
  2692. this.#updateModeCapability = Promise.withResolvers();
  2693. this.#currentDrawingSession?.commitOrRemove();
  2694. this.#mode = mode;
  2695. if (mode === AnnotationEditorType.NONE) {
  2696. this.setEditingState(false);
  2697. this.#disableAll();
  2698. this._editorUndoBar?.hide();
  2699. this.#updateModeCapability.resolve();
  2700. return;
  2701. }
  2702. if (mode === AnnotationEditorType.SIGNATURE) {
  2703. await this.#signatureManager?.loadSignatures();
  2704. }
  2705. this.setEditingState(true);
  2706. await this.#enableAll();
  2707. this.unselectAll();
  2708. for (const layer of this.#allLayers.values()) {
  2709. layer.updateMode(mode);
  2710. }
  2711. if (!editId) {
  2712. if (isFromKeyboard) {
  2713. this.addNewEditorFromKeyboard();
  2714. }
  2715. this.#updateModeCapability.resolve();
  2716. return;
  2717. }
  2718. for (const editor of this.#allEditors.values()) {
  2719. if (editor.annotationElementId === editId) {
  2720. this.setSelected(editor);
  2721. editor.enterInEditMode();
  2722. } else {
  2723. editor.unselect();
  2724. }
  2725. }
  2726. this.#updateModeCapability.resolve();
  2727. }
  2728. addNewEditorFromKeyboard() {
  2729. if (this.currentLayer.canCreateNewEmptyEditor()) {
  2730. this.currentLayer.addNewEditor();
  2731. }
  2732. }
  2733. updateToolbar(mode) {
  2734. if (mode === this.#mode) {
  2735. return;
  2736. }
  2737. this._eventBus.dispatch("switchannotationeditormode", {
  2738. source: this,
  2739. mode
  2740. });
  2741. }
  2742. updateParams(type, value) {
  2743. if (!this.#editorTypes) {
  2744. return;
  2745. }
  2746. switch (type) {
  2747. case AnnotationEditorParamsType.CREATE:
  2748. this.currentLayer.addNewEditor(value);
  2749. return;
  2750. case AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR:
  2751. this.#mainHighlightColorPicker?.updateColor(value);
  2752. break;
  2753. case AnnotationEditorParamsType.HIGHLIGHT_SHOW_ALL:
  2754. this._eventBus.dispatch("reporttelemetry", {
  2755. source: this,
  2756. details: {
  2757. type: "editing",
  2758. data: {
  2759. type: "highlight",
  2760. action: "toggle_visibility"
  2761. }
  2762. }
  2763. });
  2764. (this.#showAllStates ||= new Map()).set(type, value);
  2765. this.showAllEditors("highlight", value);
  2766. break;
  2767. }
  2768. for (const editor of this.#selectedEditors) {
  2769. editor.updateParams(type, value);
  2770. }
  2771. for (const editorType of this.#editorTypes) {
  2772. editorType.updateDefaultParams(type, value);
  2773. }
  2774. }
  2775. showAllEditors(type, visible, updateButton = false) {
  2776. for (const editor of this.#allEditors.values()) {
  2777. if (editor.editorType === type) {
  2778. editor.show(visible);
  2779. }
  2780. }
  2781. const state = this.#showAllStates?.get(AnnotationEditorParamsType.HIGHLIGHT_SHOW_ALL) ?? true;
  2782. if (state !== visible) {
  2783. this.#dispatchUpdateUI([[AnnotationEditorParamsType.HIGHLIGHT_SHOW_ALL, visible]]);
  2784. }
  2785. }
  2786. enableWaiting(mustWait = false) {
  2787. if (this.#isWaiting === mustWait) {
  2788. return;
  2789. }
  2790. this.#isWaiting = mustWait;
  2791. for (const layer of this.#allLayers.values()) {
  2792. if (mustWait) {
  2793. layer.disableClick();
  2794. } else {
  2795. layer.enableClick();
  2796. }
  2797. layer.div.classList.toggle("waiting", mustWait);
  2798. }
  2799. }
  2800. async #enableAll() {
  2801. if (!this.#isEnabled) {
  2802. this.#isEnabled = true;
  2803. const promises = [];
  2804. for (const layer of this.#allLayers.values()) {
  2805. promises.push(layer.enable());
  2806. }
  2807. await Promise.all(promises);
  2808. for (const editor of this.#allEditors.values()) {
  2809. editor.enable();
  2810. }
  2811. }
  2812. }
  2813. #disableAll() {
  2814. this.unselectAll();
  2815. if (this.#isEnabled) {
  2816. this.#isEnabled = false;
  2817. for (const layer of this.#allLayers.values()) {
  2818. layer.disable();
  2819. }
  2820. for (const editor of this.#allEditors.values()) {
  2821. editor.disable();
  2822. }
  2823. }
  2824. }
  2825. getEditors(pageIndex) {
  2826. const editors = [];
  2827. for (const editor of this.#allEditors.values()) {
  2828. if (editor.pageIndex === pageIndex) {
  2829. editors.push(editor);
  2830. }
  2831. }
  2832. return editors;
  2833. }
  2834. getEditor(id) {
  2835. return this.#allEditors.get(id);
  2836. }
  2837. addEditor(editor) {
  2838. this.#allEditors.set(editor.id, editor);
  2839. }
  2840. removeEditor(editor) {
  2841. if (editor.div.contains(document.activeElement)) {
  2842. if (this.#focusMainContainerTimeoutId) {
  2843. clearTimeout(this.#focusMainContainerTimeoutId);
  2844. }
  2845. this.#focusMainContainerTimeoutId = setTimeout(() => {
  2846. this.focusMainContainer();
  2847. this.#focusMainContainerTimeoutId = null;
  2848. }, 0);
  2849. }
  2850. this.#allEditors.delete(editor.id);
  2851. if (editor.annotationElementId) {
  2852. this.#missingCanvases?.delete(editor.annotationElementId);
  2853. }
  2854. this.unselect(editor);
  2855. if (!editor.annotationElementId || !this.#deletedAnnotationsElementIds.has(editor.annotationElementId)) {
  2856. this.#annotationStorage?.remove(editor.id);
  2857. }
  2858. }
  2859. addDeletedAnnotationElement(editor) {
  2860. this.#deletedAnnotationsElementIds.add(editor.annotationElementId);
  2861. this.addChangedExistingAnnotation(editor);
  2862. editor.deleted = true;
  2863. }
  2864. isDeletedAnnotationElement(annotationElementId) {
  2865. return this.#deletedAnnotationsElementIds.has(annotationElementId);
  2866. }
  2867. removeDeletedAnnotationElement(editor) {
  2868. this.#deletedAnnotationsElementIds.delete(editor.annotationElementId);
  2869. this.removeChangedExistingAnnotation(editor);
  2870. editor.deleted = false;
  2871. }
  2872. #addEditorToLayer(editor) {
  2873. const layer = this.#allLayers.get(editor.pageIndex);
  2874. if (layer) {
  2875. layer.addOrRebuild(editor);
  2876. } else {
  2877. this.addEditor(editor);
  2878. this.addToAnnotationStorage(editor);
  2879. }
  2880. }
  2881. setActiveEditor(editor) {
  2882. if (this.#activeEditor === editor) {
  2883. return;
  2884. }
  2885. this.#activeEditor = editor;
  2886. if (editor) {
  2887. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2888. }
  2889. }
  2890. get #lastSelectedEditor() {
  2891. let ed = null;
  2892. for (ed of this.#selectedEditors) {}
  2893. return ed;
  2894. }
  2895. updateUI(editor) {
  2896. if (this.#lastSelectedEditor === editor) {
  2897. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2898. }
  2899. }
  2900. updateUIForDefaultProperties(editorType) {
  2901. this.#dispatchUpdateUI(editorType.defaultPropertiesToUpdate);
  2902. }
  2903. toggleSelected(editor) {
  2904. if (this.#selectedEditors.has(editor)) {
  2905. this.#selectedEditors.delete(editor);
  2906. editor.unselect();
  2907. this.#dispatchUpdateStates({
  2908. hasSelectedEditor: this.hasSelection
  2909. });
  2910. return;
  2911. }
  2912. this.#selectedEditors.add(editor);
  2913. editor.select();
  2914. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2915. this.#dispatchUpdateStates({
  2916. hasSelectedEditor: true
  2917. });
  2918. }
  2919. setSelected(editor) {
  2920. this.#currentDrawingSession?.commitOrRemove();
  2921. for (const ed of this.#selectedEditors) {
  2922. if (ed !== editor) {
  2923. ed.unselect();
  2924. }
  2925. }
  2926. this.#selectedEditors.clear();
  2927. this.#selectedEditors.add(editor);
  2928. editor.select();
  2929. this.#dispatchUpdateUI(editor.propertiesToUpdate);
  2930. this.#dispatchUpdateStates({
  2931. hasSelectedEditor: true
  2932. });
  2933. }
  2934. isSelected(editor) {
  2935. return this.#selectedEditors.has(editor);
  2936. }
  2937. get firstSelectedEditor() {
  2938. return this.#selectedEditors.values().next().value;
  2939. }
  2940. unselect(editor) {
  2941. editor.unselect();
  2942. this.#selectedEditors.delete(editor);
  2943. this.#dispatchUpdateStates({
  2944. hasSelectedEditor: this.hasSelection
  2945. });
  2946. }
  2947. get hasSelection() {
  2948. return this.#selectedEditors.size !== 0;
  2949. }
  2950. get isEnterHandled() {
  2951. return this.#selectedEditors.size === 1 && this.firstSelectedEditor.isEnterHandled;
  2952. }
  2953. undo() {
  2954. this.#commandManager.undo();
  2955. this.#dispatchUpdateStates({
  2956. hasSomethingToUndo: this.#commandManager.hasSomethingToUndo(),
  2957. hasSomethingToRedo: true,
  2958. isEmpty: this.#isEmpty()
  2959. });
  2960. this._editorUndoBar?.hide();
  2961. }
  2962. redo() {
  2963. this.#commandManager.redo();
  2964. this.#dispatchUpdateStates({
  2965. hasSomethingToUndo: true,
  2966. hasSomethingToRedo: this.#commandManager.hasSomethingToRedo(),
  2967. isEmpty: this.#isEmpty()
  2968. });
  2969. }
  2970. addCommands(params) {
  2971. this.#commandManager.add(params);
  2972. this.#dispatchUpdateStates({
  2973. hasSomethingToUndo: true,
  2974. hasSomethingToRedo: false,
  2975. isEmpty: this.#isEmpty()
  2976. });
  2977. }
  2978. cleanUndoStack(type) {
  2979. this.#commandManager.cleanType(type);
  2980. }
  2981. #isEmpty() {
  2982. if (this.#allEditors.size === 0) {
  2983. return true;
  2984. }
  2985. if (this.#allEditors.size === 1) {
  2986. for (const editor of this.#allEditors.values()) {
  2987. return editor.isEmpty();
  2988. }
  2989. }
  2990. return false;
  2991. }
  2992. delete() {
  2993. this.commitOrRemove();
  2994. const drawingEditor = this.currentLayer?.endDrawingSession(true);
  2995. if (!this.hasSelection && !drawingEditor) {
  2996. return;
  2997. }
  2998. const editors = drawingEditor ? [drawingEditor] : [...this.#selectedEditors];
  2999. const cmd = () => {
  3000. this._editorUndoBar?.show(undo, editors.length === 1 ? editors[0].editorType : editors.length);
  3001. for (const editor of editors) {
  3002. editor.remove();
  3003. }
  3004. };
  3005. const undo = () => {
  3006. for (const editor of editors) {
  3007. this.#addEditorToLayer(editor);
  3008. }
  3009. };
  3010. this.addCommands({
  3011. cmd,
  3012. undo,
  3013. mustExec: true
  3014. });
  3015. }
  3016. commitOrRemove() {
  3017. this.#activeEditor?.commitOrRemove();
  3018. }
  3019. hasSomethingToControl() {
  3020. return this.#activeEditor || this.hasSelection;
  3021. }
  3022. #selectEditors(editors) {
  3023. for (const editor of this.#selectedEditors) {
  3024. editor.unselect();
  3025. }
  3026. this.#selectedEditors.clear();
  3027. for (const editor of editors) {
  3028. if (editor.isEmpty()) {
  3029. continue;
  3030. }
  3031. this.#selectedEditors.add(editor);
  3032. editor.select();
  3033. }
  3034. this.#dispatchUpdateStates({
  3035. hasSelectedEditor: this.hasSelection
  3036. });
  3037. }
  3038. selectAll() {
  3039. for (const editor of this.#selectedEditors) {
  3040. editor.commit();
  3041. }
  3042. this.#selectEditors(this.#allEditors.values());
  3043. }
  3044. unselectAll() {
  3045. if (this.#activeEditor) {
  3046. this.#activeEditor.commitOrRemove();
  3047. if (this.#mode !== AnnotationEditorType.NONE) {
  3048. return;
  3049. }
  3050. }
  3051. if (this.#currentDrawingSession?.commitOrRemove()) {
  3052. return;
  3053. }
  3054. if (!this.hasSelection) {
  3055. return;
  3056. }
  3057. for (const editor of this.#selectedEditors) {
  3058. editor.unselect();
  3059. }
  3060. this.#selectedEditors.clear();
  3061. this.#dispatchUpdateStates({
  3062. hasSelectedEditor: false
  3063. });
  3064. }
  3065. translateSelectedEditors(x, y, noCommit = false) {
  3066. if (!noCommit) {
  3067. this.commitOrRemove();
  3068. }
  3069. if (!this.hasSelection) {
  3070. return;
  3071. }
  3072. this.#translation[0] += x;
  3073. this.#translation[1] += y;
  3074. const [totalX, totalY] = this.#translation;
  3075. const editors = [...this.#selectedEditors];
  3076. const TIME_TO_WAIT = 1000;
  3077. if (this.#translationTimeoutId) {
  3078. clearTimeout(this.#translationTimeoutId);
  3079. }
  3080. this.#translationTimeoutId = setTimeout(() => {
  3081. this.#translationTimeoutId = null;
  3082. this.#translation[0] = this.#translation[1] = 0;
  3083. this.addCommands({
  3084. cmd: () => {
  3085. for (const editor of editors) {
  3086. if (this.#allEditors.has(editor.id)) {
  3087. editor.translateInPage(totalX, totalY);
  3088. editor.translationDone();
  3089. }
  3090. }
  3091. },
  3092. undo: () => {
  3093. for (const editor of editors) {
  3094. if (this.#allEditors.has(editor.id)) {
  3095. editor.translateInPage(-totalX, -totalY);
  3096. editor.translationDone();
  3097. }
  3098. }
  3099. },
  3100. mustExec: false
  3101. });
  3102. }, TIME_TO_WAIT);
  3103. for (const editor of editors) {
  3104. editor.translateInPage(x, y);
  3105. editor.translationDone();
  3106. }
  3107. }
  3108. setUpDragSession() {
  3109. if (!this.hasSelection) {
  3110. return;
  3111. }
  3112. this.disableUserSelect(true);
  3113. this.#draggingEditors = new Map();
  3114. for (const editor of this.#selectedEditors) {
  3115. this.#draggingEditors.set(editor, {
  3116. savedX: editor.x,
  3117. savedY: editor.y,
  3118. savedPageIndex: editor.pageIndex,
  3119. newX: 0,
  3120. newY: 0,
  3121. newPageIndex: -1
  3122. });
  3123. }
  3124. }
  3125. endDragSession() {
  3126. if (!this.#draggingEditors) {
  3127. return false;
  3128. }
  3129. this.disableUserSelect(false);
  3130. const map = this.#draggingEditors;
  3131. this.#draggingEditors = null;
  3132. let mustBeAddedInUndoStack = false;
  3133. for (const [{
  3134. x,
  3135. y,
  3136. pageIndex
  3137. }, value] of map) {
  3138. value.newX = x;
  3139. value.newY = y;
  3140. value.newPageIndex = pageIndex;
  3141. mustBeAddedInUndoStack ||= x !== value.savedX || y !== value.savedY || pageIndex !== value.savedPageIndex;
  3142. }
  3143. if (!mustBeAddedInUndoStack) {
  3144. return false;
  3145. }
  3146. const move = (editor, x, y, pageIndex) => {
  3147. if (this.#allEditors.has(editor.id)) {
  3148. const parent = this.#allLayers.get(pageIndex);
  3149. if (parent) {
  3150. editor._setParentAndPosition(parent, x, y);
  3151. } else {
  3152. editor.pageIndex = pageIndex;
  3153. editor.x = x;
  3154. editor.y = y;
  3155. }
  3156. }
  3157. };
  3158. this.addCommands({
  3159. cmd: () => {
  3160. for (const [editor, {
  3161. newX,
  3162. newY,
  3163. newPageIndex
  3164. }] of map) {
  3165. move(editor, newX, newY, newPageIndex);
  3166. }
  3167. },
  3168. undo: () => {
  3169. for (const [editor, {
  3170. savedX,
  3171. savedY,
  3172. savedPageIndex
  3173. }] of map) {
  3174. move(editor, savedX, savedY, savedPageIndex);
  3175. }
  3176. },
  3177. mustExec: true
  3178. });
  3179. return true;
  3180. }
  3181. dragSelectedEditors(tx, ty) {
  3182. if (!this.#draggingEditors) {
  3183. return;
  3184. }
  3185. for (const editor of this.#draggingEditors.keys()) {
  3186. editor.drag(tx, ty);
  3187. }
  3188. }
  3189. rebuild(editor) {
  3190. if (editor.parent === null) {
  3191. const parent = this.getLayer(editor.pageIndex);
  3192. if (parent) {
  3193. parent.changeParent(editor);
  3194. parent.addOrRebuild(editor);
  3195. } else {
  3196. this.addEditor(editor);
  3197. this.addToAnnotationStorage(editor);
  3198. editor.rebuild();
  3199. }
  3200. } else {
  3201. editor.parent.addOrRebuild(editor);
  3202. }
  3203. }
  3204. get isEditorHandlingKeyboard() {
  3205. return this.getActive()?.shouldGetKeyboardEvents() || this.#selectedEditors.size === 1 && this.firstSelectedEditor.shouldGetKeyboardEvents();
  3206. }
  3207. isActive(editor) {
  3208. return this.#activeEditor === editor;
  3209. }
  3210. getActive() {
  3211. return this.#activeEditor;
  3212. }
  3213. getMode() {
  3214. return this.#mode;
  3215. }
  3216. get imageManager() {
  3217. return shadow(this, "imageManager", new ImageManager());
  3218. }
  3219. getSelectionBoxes(textLayer) {
  3220. if (!textLayer) {
  3221. return null;
  3222. }
  3223. const selection = document.getSelection();
  3224. for (let i = 0, ii = selection.rangeCount; i < ii; i++) {
  3225. if (!textLayer.contains(selection.getRangeAt(i).commonAncestorContainer)) {
  3226. return null;
  3227. }
  3228. }
  3229. const {
  3230. x: layerX,
  3231. y: layerY,
  3232. width: parentWidth,
  3233. height: parentHeight
  3234. } = textLayer.getBoundingClientRect();
  3235. let rotator;
  3236. switch (textLayer.getAttribute("data-main-rotation")) {
  3237. case "90":
  3238. rotator = (x, y, w, h) => ({
  3239. x: (y - layerY) / parentHeight,
  3240. y: 1 - (x + w - layerX) / parentWidth,
  3241. width: h / parentHeight,
  3242. height: w / parentWidth
  3243. });
  3244. break;
  3245. case "180":
  3246. rotator = (x, y, w, h) => ({
  3247. x: 1 - (x + w - layerX) / parentWidth,
  3248. y: 1 - (y + h - layerY) / parentHeight,
  3249. width: w / parentWidth,
  3250. height: h / parentHeight
  3251. });
  3252. break;
  3253. case "270":
  3254. rotator = (x, y, w, h) => ({
  3255. x: 1 - (y + h - layerY) / parentHeight,
  3256. y: (x - layerX) / parentWidth,
  3257. width: h / parentHeight,
  3258. height: w / parentWidth
  3259. });
  3260. break;
  3261. default:
  3262. rotator = (x, y, w, h) => ({
  3263. x: (x - layerX) / parentWidth,
  3264. y: (y - layerY) / parentHeight,
  3265. width: w / parentWidth,
  3266. height: h / parentHeight
  3267. });
  3268. break;
  3269. }
  3270. const boxes = [];
  3271. for (let i = 0, ii = selection.rangeCount; i < ii; i++) {
  3272. const range = selection.getRangeAt(i);
  3273. if (range.collapsed) {
  3274. continue;
  3275. }
  3276. for (const {
  3277. x,
  3278. y,
  3279. width,
  3280. height
  3281. } of range.getClientRects()) {
  3282. if (width === 0 || height === 0) {
  3283. continue;
  3284. }
  3285. boxes.push(rotator(x, y, width, height));
  3286. }
  3287. }
  3288. return boxes.length === 0 ? null : boxes;
  3289. }
  3290. addChangedExistingAnnotation({
  3291. annotationElementId,
  3292. id
  3293. }) {
  3294. (this.#changedExistingAnnotations ||= new Map()).set(annotationElementId, id);
  3295. }
  3296. removeChangedExistingAnnotation({
  3297. annotationElementId
  3298. }) {
  3299. this.#changedExistingAnnotations?.delete(annotationElementId);
  3300. }
  3301. renderAnnotationElement(annotation) {
  3302. const editorId = this.#changedExistingAnnotations?.get(annotation.data.id);
  3303. if (!editorId) {
  3304. return;
  3305. }
  3306. const editor = this.#annotationStorage.getRawValue(editorId);
  3307. if (!editor) {
  3308. return;
  3309. }
  3310. if (this.#mode === AnnotationEditorType.NONE && !editor.hasBeenModified) {
  3311. return;
  3312. }
  3313. editor.renderAnnotationElement(annotation);
  3314. }
  3315. setMissingCanvas(annotationId, annotationElementId, canvas) {
  3316. const editor = this.#missingCanvases?.get(annotationId);
  3317. if (!editor) {
  3318. return;
  3319. }
  3320. editor.setCanvas(annotationElementId, canvas);
  3321. this.#missingCanvases.delete(annotationId);
  3322. }
  3323. addMissingCanvas(annotationId, editor) {
  3324. (this.#missingCanvases ||= new Map()).set(annotationId, editor);
  3325. }
  3326. }
  3327. ;// ./src/display/editor/alt_text.js
  3328. class AltText {
  3329. #altText = null;
  3330. #altTextDecorative = false;
  3331. #altTextButton = null;
  3332. #altTextButtonLabel = null;
  3333. #altTextTooltip = null;
  3334. #altTextTooltipTimeout = null;
  3335. #altTextWasFromKeyBoard = false;
  3336. #badge = null;
  3337. #editor = null;
  3338. #guessedText = null;
  3339. #textWithDisclaimer = null;
  3340. #useNewAltTextFlow = false;
  3341. static #l10nNewButton = null;
  3342. static _l10n = null;
  3343. constructor(editor) {
  3344. this.#editor = editor;
  3345. this.#useNewAltTextFlow = editor._uiManager.useNewAltTextFlow;
  3346. AltText.#l10nNewButton ||= Object.freeze({
  3347. added: "pdfjs-editor-new-alt-text-added-button",
  3348. "added-label": "pdfjs-editor-new-alt-text-added-button-label",
  3349. missing: "pdfjs-editor-new-alt-text-missing-button",
  3350. "missing-label": "pdfjs-editor-new-alt-text-missing-button-label",
  3351. review: "pdfjs-editor-new-alt-text-to-review-button",
  3352. "review-label": "pdfjs-editor-new-alt-text-to-review-button-label"
  3353. });
  3354. }
  3355. static initialize(l10n) {
  3356. AltText._l10n ??= l10n;
  3357. }
  3358. async render() {
  3359. const altText = this.#altTextButton = document.createElement("button");
  3360. altText.className = "altText";
  3361. altText.tabIndex = "0";
  3362. const label = this.#altTextButtonLabel = document.createElement("span");
  3363. altText.append(label);
  3364. if (this.#useNewAltTextFlow) {
  3365. altText.classList.add("new");
  3366. altText.setAttribute("data-l10n-id", AltText.#l10nNewButton.missing);
  3367. label.setAttribute("data-l10n-id", AltText.#l10nNewButton["missing-label"]);
  3368. } else {
  3369. altText.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-button");
  3370. label.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-button-label");
  3371. }
  3372. const signal = this.#editor._uiManager._signal;
  3373. altText.addEventListener("contextmenu", noContextMenu, {
  3374. signal
  3375. });
  3376. altText.addEventListener("pointerdown", event => event.stopPropagation(), {
  3377. signal
  3378. });
  3379. const onClick = event => {
  3380. event.preventDefault();
  3381. this.#editor._uiManager.editAltText(this.#editor);
  3382. if (this.#useNewAltTextFlow) {
  3383. this.#editor._reportTelemetry({
  3384. action: "pdfjs.image.alt_text.image_status_label_clicked",
  3385. data: {
  3386. label: this.#label
  3387. }
  3388. });
  3389. }
  3390. };
  3391. altText.addEventListener("click", onClick, {
  3392. capture: true,
  3393. signal
  3394. });
  3395. altText.addEventListener("keydown", event => {
  3396. if (event.target === altText && event.key === "Enter") {
  3397. this.#altTextWasFromKeyBoard = true;
  3398. onClick(event);
  3399. }
  3400. }, {
  3401. signal
  3402. });
  3403. await this.#setState();
  3404. return altText;
  3405. }
  3406. get #label() {
  3407. return this.#altText && "added" || this.#altText === null && this.guessedText && "review" || "missing";
  3408. }
  3409. finish() {
  3410. if (!this.#altTextButton) {
  3411. return;
  3412. }
  3413. this.#altTextButton.focus({
  3414. focusVisible: this.#altTextWasFromKeyBoard
  3415. });
  3416. this.#altTextWasFromKeyBoard = false;
  3417. }
  3418. isEmpty() {
  3419. if (this.#useNewAltTextFlow) {
  3420. return this.#altText === null;
  3421. }
  3422. return !this.#altText && !this.#altTextDecorative;
  3423. }
  3424. hasData() {
  3425. if (this.#useNewAltTextFlow) {
  3426. return this.#altText !== null || !!this.#guessedText;
  3427. }
  3428. return this.isEmpty();
  3429. }
  3430. get guessedText() {
  3431. return this.#guessedText;
  3432. }
  3433. async setGuessedText(guessedText) {
  3434. if (this.#altText !== null) {
  3435. return;
  3436. }
  3437. this.#guessedText = guessedText;
  3438. this.#textWithDisclaimer = await AltText._l10n.get("pdfjs-editor-new-alt-text-generated-alt-text-with-disclaimer", {
  3439. generatedAltText: guessedText
  3440. });
  3441. this.#setState();
  3442. }
  3443. toggleAltTextBadge(visibility = false) {
  3444. if (!this.#useNewAltTextFlow || this.#altText) {
  3445. this.#badge?.remove();
  3446. this.#badge = null;
  3447. return;
  3448. }
  3449. if (!this.#badge) {
  3450. const badge = this.#badge = document.createElement("div");
  3451. badge.className = "noAltTextBadge";
  3452. this.#editor.div.append(badge);
  3453. }
  3454. this.#badge.classList.toggle("hidden", !visibility);
  3455. }
  3456. serialize(isForCopying) {
  3457. let altText = this.#altText;
  3458. if (!isForCopying && this.#guessedText === altText) {
  3459. altText = this.#textWithDisclaimer;
  3460. }
  3461. return {
  3462. altText,
  3463. decorative: this.#altTextDecorative,
  3464. guessedText: this.#guessedText,
  3465. textWithDisclaimer: this.#textWithDisclaimer
  3466. };
  3467. }
  3468. get data() {
  3469. return {
  3470. altText: this.#altText,
  3471. decorative: this.#altTextDecorative
  3472. };
  3473. }
  3474. set data({
  3475. altText,
  3476. decorative,
  3477. guessedText,
  3478. textWithDisclaimer,
  3479. cancel = false
  3480. }) {
  3481. if (guessedText) {
  3482. this.#guessedText = guessedText;
  3483. this.#textWithDisclaimer = textWithDisclaimer;
  3484. }
  3485. if (this.#altText === altText && this.#altTextDecorative === decorative) {
  3486. return;
  3487. }
  3488. if (!cancel) {
  3489. this.#altText = altText;
  3490. this.#altTextDecorative = decorative;
  3491. }
  3492. this.#setState();
  3493. }
  3494. toggle(enabled = false) {
  3495. if (!this.#altTextButton) {
  3496. return;
  3497. }
  3498. if (!enabled && this.#altTextTooltipTimeout) {
  3499. clearTimeout(this.#altTextTooltipTimeout);
  3500. this.#altTextTooltipTimeout = null;
  3501. }
  3502. this.#altTextButton.disabled = !enabled;
  3503. }
  3504. shown() {
  3505. this.#editor._reportTelemetry({
  3506. action: "pdfjs.image.alt_text.image_status_label_displayed",
  3507. data: {
  3508. label: this.#label
  3509. }
  3510. });
  3511. }
  3512. destroy() {
  3513. this.#altTextButton?.remove();
  3514. this.#altTextButton = null;
  3515. this.#altTextButtonLabel = null;
  3516. this.#altTextTooltip = null;
  3517. this.#badge?.remove();
  3518. this.#badge = null;
  3519. }
  3520. async #setState() {
  3521. const button = this.#altTextButton;
  3522. if (!button) {
  3523. return;
  3524. }
  3525. if (this.#useNewAltTextFlow) {
  3526. button.classList.toggle("done", !!this.#altText);
  3527. button.setAttribute("data-l10n-id", AltText.#l10nNewButton[this.#label]);
  3528. this.#altTextButtonLabel?.setAttribute("data-l10n-id", AltText.#l10nNewButton[`${this.#label}-label`]);
  3529. if (!this.#altText) {
  3530. this.#altTextTooltip?.remove();
  3531. return;
  3532. }
  3533. } else {
  3534. if (!this.#altText && !this.#altTextDecorative) {
  3535. button.classList.remove("done");
  3536. this.#altTextTooltip?.remove();
  3537. return;
  3538. }
  3539. button.classList.add("done");
  3540. button.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-edit-button");
  3541. }
  3542. let tooltip = this.#altTextTooltip;
  3543. if (!tooltip) {
  3544. this.#altTextTooltip = tooltip = document.createElement("span");
  3545. tooltip.className = "tooltip";
  3546. tooltip.setAttribute("role", "tooltip");
  3547. tooltip.id = `alt-text-tooltip-${this.#editor.id}`;
  3548. const DELAY_TO_SHOW_TOOLTIP = 100;
  3549. const signal = this.#editor._uiManager._signal;
  3550. signal.addEventListener("abort", () => {
  3551. clearTimeout(this.#altTextTooltipTimeout);
  3552. this.#altTextTooltipTimeout = null;
  3553. }, {
  3554. once: true
  3555. });
  3556. button.addEventListener("mouseenter", () => {
  3557. this.#altTextTooltipTimeout = setTimeout(() => {
  3558. this.#altTextTooltipTimeout = null;
  3559. this.#altTextTooltip.classList.add("show");
  3560. this.#editor._reportTelemetry({
  3561. action: "alt_text_tooltip"
  3562. });
  3563. }, DELAY_TO_SHOW_TOOLTIP);
  3564. }, {
  3565. signal
  3566. });
  3567. button.addEventListener("mouseleave", () => {
  3568. if (this.#altTextTooltipTimeout) {
  3569. clearTimeout(this.#altTextTooltipTimeout);
  3570. this.#altTextTooltipTimeout = null;
  3571. }
  3572. this.#altTextTooltip?.classList.remove("show");
  3573. }, {
  3574. signal
  3575. });
  3576. }
  3577. if (this.#altTextDecorative) {
  3578. tooltip.setAttribute("data-l10n-id", "pdfjs-editor-alt-text-decorative-tooltip");
  3579. } else {
  3580. tooltip.removeAttribute("data-l10n-id");
  3581. tooltip.textContent = this.#altText;
  3582. }
  3583. if (!tooltip.parentNode) {
  3584. button.append(tooltip);
  3585. }
  3586. const element = this.#editor.getElementForAltText();
  3587. element?.setAttribute("aria-describedby", tooltip.id);
  3588. }
  3589. }
  3590. ;// ./src/display/touch_manager.js
  3591. class TouchManager {
  3592. #container;
  3593. #isPinching = false;
  3594. #isPinchingStopped = null;
  3595. #isPinchingDisabled;
  3596. #onPinchStart;
  3597. #onPinching;
  3598. #onPinchEnd;
  3599. #pointerDownAC = null;
  3600. #signal;
  3601. #touchInfo = null;
  3602. #touchManagerAC;
  3603. #touchMoveAC = null;
  3604. constructor({
  3605. container,
  3606. isPinchingDisabled = null,
  3607. isPinchingStopped = null,
  3608. onPinchStart = null,
  3609. onPinching = null,
  3610. onPinchEnd = null,
  3611. signal
  3612. }) {
  3613. this.#container = container;
  3614. this.#isPinchingStopped = isPinchingStopped;
  3615. this.#isPinchingDisabled = isPinchingDisabled;
  3616. this.#onPinchStart = onPinchStart;
  3617. this.#onPinching = onPinching;
  3618. this.#onPinchEnd = onPinchEnd;
  3619. this.#touchManagerAC = new AbortController();
  3620. this.#signal = AbortSignal.any([signal, this.#touchManagerAC.signal]);
  3621. container.addEventListener("touchstart", this.#onTouchStart.bind(this), {
  3622. passive: false,
  3623. signal: this.#signal
  3624. });
  3625. }
  3626. get MIN_TOUCH_DISTANCE_TO_PINCH() {
  3627. return 35 / OutputScale.pixelRatio;
  3628. }
  3629. #onTouchStart(evt) {
  3630. if (this.#isPinchingDisabled?.()) {
  3631. return;
  3632. }
  3633. if (evt.touches.length === 1) {
  3634. if (this.#pointerDownAC) {
  3635. return;
  3636. }
  3637. const pointerDownAC = this.#pointerDownAC = new AbortController();
  3638. const signal = AbortSignal.any([this.#signal, pointerDownAC.signal]);
  3639. const container = this.#container;
  3640. const opts = {
  3641. capture: true,
  3642. signal,
  3643. passive: false
  3644. };
  3645. const cancelPointerDown = e => {
  3646. if (e.pointerType === "touch") {
  3647. this.#pointerDownAC?.abort();
  3648. this.#pointerDownAC = null;
  3649. }
  3650. };
  3651. container.addEventListener("pointerdown", e => {
  3652. if (e.pointerType === "touch") {
  3653. stopEvent(e);
  3654. cancelPointerDown(e);
  3655. }
  3656. }, opts);
  3657. container.addEventListener("pointerup", cancelPointerDown, opts);
  3658. container.addEventListener("pointercancel", cancelPointerDown, opts);
  3659. return;
  3660. }
  3661. if (!this.#touchMoveAC) {
  3662. this.#touchMoveAC = new AbortController();
  3663. const signal = AbortSignal.any([this.#signal, this.#touchMoveAC.signal]);
  3664. const container = this.#container;
  3665. const opt = {
  3666. signal,
  3667. capture: false,
  3668. passive: false
  3669. };
  3670. container.addEventListener("touchmove", this.#onTouchMove.bind(this), opt);
  3671. const onTouchEnd = this.#onTouchEnd.bind(this);
  3672. container.addEventListener("touchend", onTouchEnd, opt);
  3673. container.addEventListener("touchcancel", onTouchEnd, opt);
  3674. opt.capture = true;
  3675. container.addEventListener("pointerdown", stopEvent, opt);
  3676. container.addEventListener("pointermove", stopEvent, opt);
  3677. container.addEventListener("pointercancel", stopEvent, opt);
  3678. container.addEventListener("pointerup", stopEvent, opt);
  3679. this.#onPinchStart?.();
  3680. }
  3681. stopEvent(evt);
  3682. if (evt.touches.length !== 2 || this.#isPinchingStopped?.()) {
  3683. this.#touchInfo = null;
  3684. return;
  3685. }
  3686. let [touch0, touch1] = evt.touches;
  3687. if (touch0.identifier > touch1.identifier) {
  3688. [touch0, touch1] = [touch1, touch0];
  3689. }
  3690. this.#touchInfo = {
  3691. touch0X: touch0.screenX,
  3692. touch0Y: touch0.screenY,
  3693. touch1X: touch1.screenX,
  3694. touch1Y: touch1.screenY
  3695. };
  3696. }
  3697. #onTouchMove(evt) {
  3698. if (!this.#touchInfo || evt.touches.length !== 2) {
  3699. return;
  3700. }
  3701. stopEvent(evt);
  3702. let [touch0, touch1] = evt.touches;
  3703. if (touch0.identifier > touch1.identifier) {
  3704. [touch0, touch1] = [touch1, touch0];
  3705. }
  3706. const {
  3707. screenX: screen0X,
  3708. screenY: screen0Y
  3709. } = touch0;
  3710. const {
  3711. screenX: screen1X,
  3712. screenY: screen1Y
  3713. } = touch1;
  3714. const touchInfo = this.#touchInfo;
  3715. const {
  3716. touch0X: pTouch0X,
  3717. touch0Y: pTouch0Y,
  3718. touch1X: pTouch1X,
  3719. touch1Y: pTouch1Y
  3720. } = touchInfo;
  3721. const prevGapX = pTouch1X - pTouch0X;
  3722. const prevGapY = pTouch1Y - pTouch0Y;
  3723. const currGapX = screen1X - screen0X;
  3724. const currGapY = screen1Y - screen0Y;
  3725. const distance = Math.hypot(currGapX, currGapY) || 1;
  3726. const pDistance = Math.hypot(prevGapX, prevGapY) || 1;
  3727. if (!this.#isPinching && Math.abs(pDistance - distance) <= TouchManager.MIN_TOUCH_DISTANCE_TO_PINCH) {
  3728. return;
  3729. }
  3730. touchInfo.touch0X = screen0X;
  3731. touchInfo.touch0Y = screen0Y;
  3732. touchInfo.touch1X = screen1X;
  3733. touchInfo.touch1Y = screen1Y;
  3734. if (!this.#isPinching) {
  3735. this.#isPinching = true;
  3736. return;
  3737. }
  3738. const origin = [(screen0X + screen1X) / 2, (screen0Y + screen1Y) / 2];
  3739. this.#onPinching?.(origin, pDistance, distance);
  3740. }
  3741. #onTouchEnd(evt) {
  3742. if (evt.touches.length >= 2) {
  3743. return;
  3744. }
  3745. if (this.#touchMoveAC) {
  3746. this.#touchMoveAC.abort();
  3747. this.#touchMoveAC = null;
  3748. this.#onPinchEnd?.();
  3749. }
  3750. if (!this.#touchInfo) {
  3751. return;
  3752. }
  3753. stopEvent(evt);
  3754. this.#touchInfo = null;
  3755. this.#isPinching = false;
  3756. }
  3757. destroy() {
  3758. this.#touchManagerAC?.abort();
  3759. this.#touchManagerAC = null;
  3760. this.#pointerDownAC?.abort();
  3761. this.#pointerDownAC = null;
  3762. }
  3763. }
  3764. ;// ./src/display/editor/editor.js
  3765. class AnnotationEditor {
  3766. #accessibilityData = null;
  3767. #allResizerDivs = null;
  3768. #altText = null;
  3769. #disabled = false;
  3770. #dragPointerId = null;
  3771. #dragPointerType = "";
  3772. #keepAspectRatio = false;
  3773. #resizersDiv = null;
  3774. #lastPointerCoords = null;
  3775. #savedDimensions = null;
  3776. #focusAC = null;
  3777. #focusedResizerName = "";
  3778. #hasBeenClicked = false;
  3779. #initialRect = null;
  3780. #isEditing = false;
  3781. #isInEditMode = false;
  3782. #isResizerEnabledForKeyboard = false;
  3783. #moveInDOMTimeout = null;
  3784. #prevDragX = 0;
  3785. #prevDragY = 0;
  3786. #telemetryTimeouts = null;
  3787. #touchManager = null;
  3788. _isCopy = false;
  3789. _editToolbar = null;
  3790. _initialOptions = Object.create(null);
  3791. _initialData = null;
  3792. _isVisible = true;
  3793. _uiManager = null;
  3794. _focusEventsAllowed = true;
  3795. static _l10n = null;
  3796. static _l10nResizer = null;
  3797. #isDraggable = false;
  3798. #zIndex = AnnotationEditor._zIndex++;
  3799. static _borderLineWidth = -1;
  3800. static _colorManager = new ColorManager();
  3801. static _zIndex = 1;
  3802. static _telemetryTimeout = 1000;
  3803. static get _resizerKeyboardManager() {
  3804. const resize = AnnotationEditor.prototype._resizeWithKeyboard;
  3805. const small = AnnotationEditorUIManager.TRANSLATE_SMALL;
  3806. const big = AnnotationEditorUIManager.TRANSLATE_BIG;
  3807. return shadow(this, "_resizerKeyboardManager", new KeyboardManager([[["ArrowLeft", "mac+ArrowLeft"], resize, {
  3808. args: [-small, 0]
  3809. }], [["ctrl+ArrowLeft", "mac+shift+ArrowLeft"], resize, {
  3810. args: [-big, 0]
  3811. }], [["ArrowRight", "mac+ArrowRight"], resize, {
  3812. args: [small, 0]
  3813. }], [["ctrl+ArrowRight", "mac+shift+ArrowRight"], resize, {
  3814. args: [big, 0]
  3815. }], [["ArrowUp", "mac+ArrowUp"], resize, {
  3816. args: [0, -small]
  3817. }], [["ctrl+ArrowUp", "mac+shift+ArrowUp"], resize, {
  3818. args: [0, -big]
  3819. }], [["ArrowDown", "mac+ArrowDown"], resize, {
  3820. args: [0, small]
  3821. }], [["ctrl+ArrowDown", "mac+shift+ArrowDown"], resize, {
  3822. args: [0, big]
  3823. }], [["Escape", "mac+Escape"], AnnotationEditor.prototype._stopResizingWithKeyboard]]));
  3824. }
  3825. constructor(parameters) {
  3826. this.parent = parameters.parent;
  3827. this.id = parameters.id;
  3828. this.width = this.height = null;
  3829. this.pageIndex = parameters.parent.pageIndex;
  3830. this.name = parameters.name;
  3831. this.div = null;
  3832. this._uiManager = parameters.uiManager;
  3833. this.annotationElementId = null;
  3834. this._willKeepAspectRatio = false;
  3835. this._initialOptions.isCentered = parameters.isCentered;
  3836. this._structTreeParentId = null;
  3837. const {
  3838. rotation,
  3839. rawDims: {
  3840. pageWidth,
  3841. pageHeight,
  3842. pageX,
  3843. pageY
  3844. }
  3845. } = this.parent.viewport;
  3846. this.rotation = rotation;
  3847. this.pageRotation = (360 + rotation - this._uiManager.viewParameters.rotation) % 360;
  3848. this.pageDimensions = [pageWidth, pageHeight];
  3849. this.pageTranslation = [pageX, pageY];
  3850. const [width, height] = this.parentDimensions;
  3851. this.x = parameters.x / width;
  3852. this.y = parameters.y / height;
  3853. this.isAttachedToDOM = false;
  3854. this.deleted = false;
  3855. }
  3856. get editorType() {
  3857. return Object.getPrototypeOf(this).constructor._type;
  3858. }
  3859. static get isDrawer() {
  3860. return false;
  3861. }
  3862. static get _defaultLineColor() {
  3863. return shadow(this, "_defaultLineColor", this._colorManager.getHexCode("CanvasText"));
  3864. }
  3865. static deleteAnnotationElement(editor) {
  3866. const fakeEditor = new FakeEditor({
  3867. id: editor.parent.getNextId(),
  3868. parent: editor.parent,
  3869. uiManager: editor._uiManager
  3870. });
  3871. fakeEditor.annotationElementId = editor.annotationElementId;
  3872. fakeEditor.deleted = true;
  3873. fakeEditor._uiManager.addToAnnotationStorage(fakeEditor);
  3874. }
  3875. static initialize(l10n, _uiManager) {
  3876. AnnotationEditor._l10n ??= l10n;
  3877. AnnotationEditor._l10nResizer ||= Object.freeze({
  3878. topLeft: "pdfjs-editor-resizer-top-left",
  3879. topMiddle: "pdfjs-editor-resizer-top-middle",
  3880. topRight: "pdfjs-editor-resizer-top-right",
  3881. middleRight: "pdfjs-editor-resizer-middle-right",
  3882. bottomRight: "pdfjs-editor-resizer-bottom-right",
  3883. bottomMiddle: "pdfjs-editor-resizer-bottom-middle",
  3884. bottomLeft: "pdfjs-editor-resizer-bottom-left",
  3885. middleLeft: "pdfjs-editor-resizer-middle-left"
  3886. });
  3887. if (AnnotationEditor._borderLineWidth !== -1) {
  3888. return;
  3889. }
  3890. const style = getComputedStyle(document.documentElement);
  3891. AnnotationEditor._borderLineWidth = parseFloat(style.getPropertyValue("--outline-width")) || 0;
  3892. }
  3893. static updateDefaultParams(_type, _value) {}
  3894. static get defaultPropertiesToUpdate() {
  3895. return [];
  3896. }
  3897. static isHandlingMimeForPasting(mime) {
  3898. return false;
  3899. }
  3900. static paste(item, parent) {
  3901. unreachable("Not implemented");
  3902. }
  3903. get propertiesToUpdate() {
  3904. return [];
  3905. }
  3906. get _isDraggable() {
  3907. return this.#isDraggable;
  3908. }
  3909. set _isDraggable(value) {
  3910. this.#isDraggable = value;
  3911. this.div?.classList.toggle("draggable", value);
  3912. }
  3913. get isEnterHandled() {
  3914. return true;
  3915. }
  3916. center() {
  3917. const [pageWidth, pageHeight] = this.pageDimensions;
  3918. switch (this.parentRotation) {
  3919. case 90:
  3920. this.x -= this.height * pageHeight / (pageWidth * 2);
  3921. this.y += this.width * pageWidth / (pageHeight * 2);
  3922. break;
  3923. case 180:
  3924. this.x += this.width / 2;
  3925. this.y += this.height / 2;
  3926. break;
  3927. case 270:
  3928. this.x += this.height * pageHeight / (pageWidth * 2);
  3929. this.y -= this.width * pageWidth / (pageHeight * 2);
  3930. break;
  3931. default:
  3932. this.x -= this.width / 2;
  3933. this.y -= this.height / 2;
  3934. break;
  3935. }
  3936. this.fixAndSetPosition();
  3937. }
  3938. addCommands(params) {
  3939. this._uiManager.addCommands(params);
  3940. }
  3941. get currentLayer() {
  3942. return this._uiManager.currentLayer;
  3943. }
  3944. setInBackground() {
  3945. this.div.style.zIndex = 0;
  3946. }
  3947. setInForeground() {
  3948. this.div.style.zIndex = this.#zIndex;
  3949. }
  3950. setParent(parent) {
  3951. if (parent !== null) {
  3952. this.pageIndex = parent.pageIndex;
  3953. this.pageDimensions = parent.pageDimensions;
  3954. } else {
  3955. this.#stopResizing();
  3956. }
  3957. this.parent = parent;
  3958. }
  3959. focusin(event) {
  3960. if (!this._focusEventsAllowed) {
  3961. return;
  3962. }
  3963. if (!this.#hasBeenClicked) {
  3964. this.parent.setSelected(this);
  3965. } else {
  3966. this.#hasBeenClicked = false;
  3967. }
  3968. }
  3969. focusout(event) {
  3970. if (!this._focusEventsAllowed) {
  3971. return;
  3972. }
  3973. if (!this.isAttachedToDOM) {
  3974. return;
  3975. }
  3976. const target = event.relatedTarget;
  3977. if (target?.closest(`#${this.id}`)) {
  3978. return;
  3979. }
  3980. event.preventDefault();
  3981. if (!this.parent?.isMultipleSelection) {
  3982. this.commitOrRemove();
  3983. }
  3984. }
  3985. commitOrRemove() {
  3986. if (this.isEmpty()) {
  3987. this.remove();
  3988. } else {
  3989. this.commit();
  3990. }
  3991. }
  3992. commit() {
  3993. this.addToAnnotationStorage();
  3994. }
  3995. addToAnnotationStorage() {
  3996. this._uiManager.addToAnnotationStorage(this);
  3997. }
  3998. setAt(x, y, tx, ty) {
  3999. const [width, height] = this.parentDimensions;
  4000. [tx, ty] = this.screenToPageTranslation(tx, ty);
  4001. this.x = (x + tx) / width;
  4002. this.y = (y + ty) / height;
  4003. this.fixAndSetPosition();
  4004. }
  4005. _moveAfterPaste(baseX, baseY) {
  4006. const [parentWidth, parentHeight] = this.parentDimensions;
  4007. this.setAt(baseX * parentWidth, baseY * parentHeight, this.width * parentWidth, this.height * parentHeight);
  4008. this._onTranslated();
  4009. }
  4010. #translate([width, height], x, y) {
  4011. [x, y] = this.screenToPageTranslation(x, y);
  4012. this.x += x / width;
  4013. this.y += y / height;
  4014. this._onTranslating(this.x, this.y);
  4015. this.fixAndSetPosition();
  4016. }
  4017. translate(x, y) {
  4018. this.#translate(this.parentDimensions, x, y);
  4019. }
  4020. translateInPage(x, y) {
  4021. this.#initialRect ||= [this.x, this.y, this.width, this.height];
  4022. this.#translate(this.pageDimensions, x, y);
  4023. this.div.scrollIntoView({
  4024. block: "nearest"
  4025. });
  4026. }
  4027. translationDone() {
  4028. this._onTranslated(this.x, this.y);
  4029. }
  4030. drag(tx, ty) {
  4031. this.#initialRect ||= [this.x, this.y, this.width, this.height];
  4032. const {
  4033. div,
  4034. parentDimensions: [parentWidth, parentHeight]
  4035. } = this;
  4036. this.x += tx / parentWidth;
  4037. this.y += ty / parentHeight;
  4038. if (this.parent && (this.x < 0 || this.x > 1 || this.y < 0 || this.y > 1)) {
  4039. const {
  4040. x,
  4041. y
  4042. } = this.div.getBoundingClientRect();
  4043. if (this.parent.findNewParent(this, x, y)) {
  4044. this.x -= Math.floor(this.x);
  4045. this.y -= Math.floor(this.y);
  4046. }
  4047. }
  4048. let {
  4049. x,
  4050. y
  4051. } = this;
  4052. const [bx, by] = this.getBaseTranslation();
  4053. x += bx;
  4054. y += by;
  4055. const {
  4056. style
  4057. } = div;
  4058. style.left = `${(100 * x).toFixed(2)}%`;
  4059. style.top = `${(100 * y).toFixed(2)}%`;
  4060. this._onTranslating(x, y);
  4061. div.scrollIntoView({
  4062. block: "nearest"
  4063. });
  4064. }
  4065. _onTranslating(x, y) {}
  4066. _onTranslated(x, y) {}
  4067. get _hasBeenMoved() {
  4068. return !!this.#initialRect && (this.#initialRect[0] !== this.x || this.#initialRect[1] !== this.y);
  4069. }
  4070. get _hasBeenResized() {
  4071. return !!this.#initialRect && (this.#initialRect[2] !== this.width || this.#initialRect[3] !== this.height);
  4072. }
  4073. getBaseTranslation() {
  4074. const [parentWidth, parentHeight] = this.parentDimensions;
  4075. const {
  4076. _borderLineWidth
  4077. } = AnnotationEditor;
  4078. const x = _borderLineWidth / parentWidth;
  4079. const y = _borderLineWidth / parentHeight;
  4080. switch (this.rotation) {
  4081. case 90:
  4082. return [-x, y];
  4083. case 180:
  4084. return [x, y];
  4085. case 270:
  4086. return [x, -y];
  4087. default:
  4088. return [-x, -y];
  4089. }
  4090. }
  4091. get _mustFixPosition() {
  4092. return true;
  4093. }
  4094. fixAndSetPosition(rotation = this.rotation) {
  4095. const {
  4096. div: {
  4097. style
  4098. },
  4099. pageDimensions: [pageWidth, pageHeight]
  4100. } = this;
  4101. let {
  4102. x,
  4103. y,
  4104. width,
  4105. height
  4106. } = this;
  4107. width *= pageWidth;
  4108. height *= pageHeight;
  4109. x *= pageWidth;
  4110. y *= pageHeight;
  4111. if (this._mustFixPosition) {
  4112. switch (rotation) {
  4113. case 0:
  4114. x = MathClamp(x, 0, pageWidth - width);
  4115. y = MathClamp(y, 0, pageHeight - height);
  4116. break;
  4117. case 90:
  4118. x = MathClamp(x, 0, pageWidth - height);
  4119. y = MathClamp(y, width, pageHeight);
  4120. break;
  4121. case 180:
  4122. x = MathClamp(x, width, pageWidth);
  4123. y = MathClamp(y, height, pageHeight);
  4124. break;
  4125. case 270:
  4126. x = MathClamp(x, height, pageWidth);
  4127. y = MathClamp(y, 0, pageHeight - width);
  4128. break;
  4129. }
  4130. }
  4131. this.x = x /= pageWidth;
  4132. this.y = y /= pageHeight;
  4133. const [bx, by] = this.getBaseTranslation();
  4134. x += bx;
  4135. y += by;
  4136. style.left = `${(100 * x).toFixed(2)}%`;
  4137. style.top = `${(100 * y).toFixed(2)}%`;
  4138. this.moveInDOM();
  4139. }
  4140. static #rotatePoint(x, y, angle) {
  4141. switch (angle) {
  4142. case 90:
  4143. return [y, -x];
  4144. case 180:
  4145. return [-x, -y];
  4146. case 270:
  4147. return [-y, x];
  4148. default:
  4149. return [x, y];
  4150. }
  4151. }
  4152. screenToPageTranslation(x, y) {
  4153. return AnnotationEditor.#rotatePoint(x, y, this.parentRotation);
  4154. }
  4155. pageTranslationToScreen(x, y) {
  4156. return AnnotationEditor.#rotatePoint(x, y, 360 - this.parentRotation);
  4157. }
  4158. #getRotationMatrix(rotation) {
  4159. switch (rotation) {
  4160. case 90:
  4161. {
  4162. const [pageWidth, pageHeight] = this.pageDimensions;
  4163. return [0, -pageWidth / pageHeight, pageHeight / pageWidth, 0];
  4164. }
  4165. case 180:
  4166. return [-1, 0, 0, -1];
  4167. case 270:
  4168. {
  4169. const [pageWidth, pageHeight] = this.pageDimensions;
  4170. return [0, pageWidth / pageHeight, -pageHeight / pageWidth, 0];
  4171. }
  4172. default:
  4173. return [1, 0, 0, 1];
  4174. }
  4175. }
  4176. get parentScale() {
  4177. return this._uiManager.viewParameters.realScale;
  4178. }
  4179. get parentRotation() {
  4180. return (this._uiManager.viewParameters.rotation + this.pageRotation) % 360;
  4181. }
  4182. get parentDimensions() {
  4183. const {
  4184. parentScale,
  4185. pageDimensions: [pageWidth, pageHeight]
  4186. } = this;
  4187. return [pageWidth * parentScale, pageHeight * parentScale];
  4188. }
  4189. setDims(width, height) {
  4190. const [parentWidth, parentHeight] = this.parentDimensions;
  4191. const {
  4192. style
  4193. } = this.div;
  4194. style.width = `${(100 * width / parentWidth).toFixed(2)}%`;
  4195. if (!this.#keepAspectRatio) {
  4196. style.height = `${(100 * height / parentHeight).toFixed(2)}%`;
  4197. }
  4198. }
  4199. fixDims() {
  4200. const {
  4201. style
  4202. } = this.div;
  4203. const {
  4204. height,
  4205. width
  4206. } = style;
  4207. const widthPercent = width.endsWith("%");
  4208. const heightPercent = !this.#keepAspectRatio && height.endsWith("%");
  4209. if (widthPercent && heightPercent) {
  4210. return;
  4211. }
  4212. const [parentWidth, parentHeight] = this.parentDimensions;
  4213. if (!widthPercent) {
  4214. style.width = `${(100 * parseFloat(width) / parentWidth).toFixed(2)}%`;
  4215. }
  4216. if (!this.#keepAspectRatio && !heightPercent) {
  4217. style.height = `${(100 * parseFloat(height) / parentHeight).toFixed(2)}%`;
  4218. }
  4219. }
  4220. getInitialTranslation() {
  4221. return [0, 0];
  4222. }
  4223. #createResizers() {
  4224. if (this.#resizersDiv) {
  4225. return;
  4226. }
  4227. this.#resizersDiv = document.createElement("div");
  4228. this.#resizersDiv.classList.add("resizers");
  4229. const classes = this._willKeepAspectRatio ? ["topLeft", "topRight", "bottomRight", "bottomLeft"] : ["topLeft", "topMiddle", "topRight", "middleRight", "bottomRight", "bottomMiddle", "bottomLeft", "middleLeft"];
  4230. const signal = this._uiManager._signal;
  4231. for (const name of classes) {
  4232. const div = document.createElement("div");
  4233. this.#resizersDiv.append(div);
  4234. div.classList.add("resizer", name);
  4235. div.setAttribute("data-resizer-name", name);
  4236. div.addEventListener("pointerdown", this.#resizerPointerdown.bind(this, name), {
  4237. signal
  4238. });
  4239. div.addEventListener("contextmenu", noContextMenu, {
  4240. signal
  4241. });
  4242. div.tabIndex = -1;
  4243. }
  4244. this.div.prepend(this.#resizersDiv);
  4245. }
  4246. #resizerPointerdown(name, event) {
  4247. event.preventDefault();
  4248. const {
  4249. isMac
  4250. } = util_FeatureTest.platform;
  4251. if (event.button !== 0 || event.ctrlKey && isMac) {
  4252. return;
  4253. }
  4254. this.#altText?.toggle(false);
  4255. const savedDraggable = this._isDraggable;
  4256. this._isDraggable = false;
  4257. this.#lastPointerCoords = [event.screenX, event.screenY];
  4258. const ac = new AbortController();
  4259. const signal = this._uiManager.combinedSignal(ac);
  4260. this.parent.togglePointerEvents(false);
  4261. window.addEventListener("pointermove", this.#resizerPointermove.bind(this, name), {
  4262. passive: true,
  4263. capture: true,
  4264. signal
  4265. });
  4266. window.addEventListener("touchmove", stopEvent, {
  4267. passive: false,
  4268. signal
  4269. });
  4270. window.addEventListener("contextmenu", noContextMenu, {
  4271. signal
  4272. });
  4273. this.#savedDimensions = {
  4274. savedX: this.x,
  4275. savedY: this.y,
  4276. savedWidth: this.width,
  4277. savedHeight: this.height
  4278. };
  4279. const savedParentCursor = this.parent.div.style.cursor;
  4280. const savedCursor = this.div.style.cursor;
  4281. this.div.style.cursor = this.parent.div.style.cursor = window.getComputedStyle(event.target).cursor;
  4282. const pointerUpCallback = () => {
  4283. ac.abort();
  4284. this.parent.togglePointerEvents(true);
  4285. this.#altText?.toggle(true);
  4286. this._isDraggable = savedDraggable;
  4287. this.parent.div.style.cursor = savedParentCursor;
  4288. this.div.style.cursor = savedCursor;
  4289. this.#addResizeToUndoStack();
  4290. };
  4291. window.addEventListener("pointerup", pointerUpCallback, {
  4292. signal
  4293. });
  4294. window.addEventListener("blur", pointerUpCallback, {
  4295. signal
  4296. });
  4297. }
  4298. #resize(x, y, width, height) {
  4299. this.width = width;
  4300. this.height = height;
  4301. this.x = x;
  4302. this.y = y;
  4303. const [parentWidth, parentHeight] = this.parentDimensions;
  4304. this.setDims(parentWidth * width, parentHeight * height);
  4305. this.fixAndSetPosition();
  4306. this._onResized();
  4307. }
  4308. _onResized() {}
  4309. #addResizeToUndoStack() {
  4310. if (!this.#savedDimensions) {
  4311. return;
  4312. }
  4313. const {
  4314. savedX,
  4315. savedY,
  4316. savedWidth,
  4317. savedHeight
  4318. } = this.#savedDimensions;
  4319. this.#savedDimensions = null;
  4320. const newX = this.x;
  4321. const newY = this.y;
  4322. const newWidth = this.width;
  4323. const newHeight = this.height;
  4324. if (newX === savedX && newY === savedY && newWidth === savedWidth && newHeight === savedHeight) {
  4325. return;
  4326. }
  4327. this.addCommands({
  4328. cmd: this.#resize.bind(this, newX, newY, newWidth, newHeight),
  4329. undo: this.#resize.bind(this, savedX, savedY, savedWidth, savedHeight),
  4330. mustExec: true
  4331. });
  4332. }
  4333. static _round(x) {
  4334. return Math.round(x * 10000) / 10000;
  4335. }
  4336. #resizerPointermove(name, event) {
  4337. const [parentWidth, parentHeight] = this.parentDimensions;
  4338. const savedX = this.x;
  4339. const savedY = this.y;
  4340. const savedWidth = this.width;
  4341. const savedHeight = this.height;
  4342. const minWidth = AnnotationEditor.MIN_SIZE / parentWidth;
  4343. const minHeight = AnnotationEditor.MIN_SIZE / parentHeight;
  4344. const rotationMatrix = this.#getRotationMatrix(this.rotation);
  4345. const transf = (x, y) => [rotationMatrix[0] * x + rotationMatrix[2] * y, rotationMatrix[1] * x + rotationMatrix[3] * y];
  4346. const invRotationMatrix = this.#getRotationMatrix(360 - this.rotation);
  4347. const invTransf = (x, y) => [invRotationMatrix[0] * x + invRotationMatrix[2] * y, invRotationMatrix[1] * x + invRotationMatrix[3] * y];
  4348. let getPoint;
  4349. let getOpposite;
  4350. let isDiagonal = false;
  4351. let isHorizontal = false;
  4352. switch (name) {
  4353. case "topLeft":
  4354. isDiagonal = true;
  4355. getPoint = (w, h) => [0, 0];
  4356. getOpposite = (w, h) => [w, h];
  4357. break;
  4358. case "topMiddle":
  4359. getPoint = (w, h) => [w / 2, 0];
  4360. getOpposite = (w, h) => [w / 2, h];
  4361. break;
  4362. case "topRight":
  4363. isDiagonal = true;
  4364. getPoint = (w, h) => [w, 0];
  4365. getOpposite = (w, h) => [0, h];
  4366. break;
  4367. case "middleRight":
  4368. isHorizontal = true;
  4369. getPoint = (w, h) => [w, h / 2];
  4370. getOpposite = (w, h) => [0, h / 2];
  4371. break;
  4372. case "bottomRight":
  4373. isDiagonal = true;
  4374. getPoint = (w, h) => [w, h];
  4375. getOpposite = (w, h) => [0, 0];
  4376. break;
  4377. case "bottomMiddle":
  4378. getPoint = (w, h) => [w / 2, h];
  4379. getOpposite = (w, h) => [w / 2, 0];
  4380. break;
  4381. case "bottomLeft":
  4382. isDiagonal = true;
  4383. getPoint = (w, h) => [0, h];
  4384. getOpposite = (w, h) => [w, 0];
  4385. break;
  4386. case "middleLeft":
  4387. isHorizontal = true;
  4388. getPoint = (w, h) => [0, h / 2];
  4389. getOpposite = (w, h) => [w, h / 2];
  4390. break;
  4391. }
  4392. const point = getPoint(savedWidth, savedHeight);
  4393. const oppositePoint = getOpposite(savedWidth, savedHeight);
  4394. let transfOppositePoint = transf(...oppositePoint);
  4395. const oppositeX = AnnotationEditor._round(savedX + transfOppositePoint[0]);
  4396. const oppositeY = AnnotationEditor._round(savedY + transfOppositePoint[1]);
  4397. let ratioX = 1;
  4398. let ratioY = 1;
  4399. let deltaX, deltaY;
  4400. if (!event.fromKeyboard) {
  4401. const {
  4402. screenX,
  4403. screenY
  4404. } = event;
  4405. const [lastScreenX, lastScreenY] = this.#lastPointerCoords;
  4406. [deltaX, deltaY] = this.screenToPageTranslation(screenX - lastScreenX, screenY - lastScreenY);
  4407. this.#lastPointerCoords[0] = screenX;
  4408. this.#lastPointerCoords[1] = screenY;
  4409. } else {
  4410. ({
  4411. deltaX,
  4412. deltaY
  4413. } = event);
  4414. }
  4415. [deltaX, deltaY] = invTransf(deltaX / parentWidth, deltaY / parentHeight);
  4416. if (isDiagonal) {
  4417. const oldDiag = Math.hypot(savedWidth, savedHeight);
  4418. 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);
  4419. } else if (isHorizontal) {
  4420. ratioX = MathClamp(Math.abs(oppositePoint[0] - point[0] - deltaX), minWidth, 1) / savedWidth;
  4421. } else {
  4422. ratioY = MathClamp(Math.abs(oppositePoint[1] - point[1] - deltaY), minHeight, 1) / savedHeight;
  4423. }
  4424. const newWidth = AnnotationEditor._round(savedWidth * ratioX);
  4425. const newHeight = AnnotationEditor._round(savedHeight * ratioY);
  4426. transfOppositePoint = transf(...getOpposite(newWidth, newHeight));
  4427. const newX = oppositeX - transfOppositePoint[0];
  4428. const newY = oppositeY - transfOppositePoint[1];
  4429. this.#initialRect ||= [this.x, this.y, this.width, this.height];
  4430. this.width = newWidth;
  4431. this.height = newHeight;
  4432. this.x = newX;
  4433. this.y = newY;
  4434. this.setDims(parentWidth * newWidth, parentHeight * newHeight);
  4435. this.fixAndSetPosition();
  4436. this._onResizing();
  4437. }
  4438. _onResizing() {}
  4439. altTextFinish() {
  4440. this.#altText?.finish();
  4441. }
  4442. async addEditToolbar() {
  4443. if (this._editToolbar || this.#isInEditMode) {
  4444. return this._editToolbar;
  4445. }
  4446. this._editToolbar = new EditorToolbar(this);
  4447. this.div.append(this._editToolbar.render());
  4448. if (this.#altText) {
  4449. await this._editToolbar.addAltText(this.#altText);
  4450. }
  4451. return this._editToolbar;
  4452. }
  4453. removeEditToolbar() {
  4454. if (!this._editToolbar) {
  4455. return;
  4456. }
  4457. this._editToolbar.remove();
  4458. this._editToolbar = null;
  4459. this.#altText?.destroy();
  4460. }
  4461. addContainer(container) {
  4462. const editToolbarDiv = this._editToolbar?.div;
  4463. if (editToolbarDiv) {
  4464. editToolbarDiv.before(container);
  4465. } else {
  4466. this.div.append(container);
  4467. }
  4468. }
  4469. getClientDimensions() {
  4470. return this.div.getBoundingClientRect();
  4471. }
  4472. async addAltTextButton() {
  4473. if (this.#altText) {
  4474. return;
  4475. }
  4476. AltText.initialize(AnnotationEditor._l10n);
  4477. this.#altText = new AltText(this);
  4478. if (this.#accessibilityData) {
  4479. this.#altText.data = this.#accessibilityData;
  4480. this.#accessibilityData = null;
  4481. }
  4482. await this.addEditToolbar();
  4483. }
  4484. get altTextData() {
  4485. return this.#altText?.data;
  4486. }
  4487. set altTextData(data) {
  4488. if (!this.#altText) {
  4489. return;
  4490. }
  4491. this.#altText.data = data;
  4492. }
  4493. get guessedAltText() {
  4494. return this.#altText?.guessedText;
  4495. }
  4496. async setGuessedAltText(text) {
  4497. await this.#altText?.setGuessedText(text);
  4498. }
  4499. serializeAltText(isForCopying) {
  4500. return this.#altText?.serialize(isForCopying);
  4501. }
  4502. hasAltText() {
  4503. return !!this.#altText && !this.#altText.isEmpty();
  4504. }
  4505. hasAltTextData() {
  4506. return this.#altText?.hasData() ?? false;
  4507. }
  4508. render() {
  4509. const div = this.div = document.createElement("div");
  4510. div.setAttribute("data-editor-rotation", (360 - this.rotation) % 360);
  4511. div.className = this.name;
  4512. div.setAttribute("id", this.id);
  4513. div.tabIndex = this.#disabled ? -1 : 0;
  4514. div.setAttribute("role", "application");
  4515. if (this.defaultL10nId) {
  4516. div.setAttribute("data-l10n-id", this.defaultL10nId);
  4517. }
  4518. if (!this._isVisible) {
  4519. div.classList.add("hidden");
  4520. }
  4521. this.setInForeground();
  4522. this.#addFocusListeners();
  4523. const [parentWidth, parentHeight] = this.parentDimensions;
  4524. if (this.parentRotation % 180 !== 0) {
  4525. div.style.maxWidth = `${(100 * parentHeight / parentWidth).toFixed(2)}%`;
  4526. div.style.maxHeight = `${(100 * parentWidth / parentHeight).toFixed(2)}%`;
  4527. }
  4528. const [tx, ty] = this.getInitialTranslation();
  4529. this.translate(tx, ty);
  4530. bindEvents(this, div, ["keydown", "pointerdown"]);
  4531. if (this.isResizable && this._uiManager._supportsPinchToZoom) {
  4532. this.#touchManager ||= new TouchManager({
  4533. container: div,
  4534. isPinchingDisabled: () => !this.isSelected,
  4535. onPinchStart: this.#touchPinchStartCallback.bind(this),
  4536. onPinching: this.#touchPinchCallback.bind(this),
  4537. onPinchEnd: this.#touchPinchEndCallback.bind(this),
  4538. signal: this._uiManager._signal
  4539. });
  4540. }
  4541. this._uiManager._editorUndoBar?.hide();
  4542. return div;
  4543. }
  4544. #touchPinchStartCallback() {
  4545. this.#savedDimensions = {
  4546. savedX: this.x,
  4547. savedY: this.y,
  4548. savedWidth: this.width,
  4549. savedHeight: this.height
  4550. };
  4551. this.#altText?.toggle(false);
  4552. this.parent.togglePointerEvents(false);
  4553. }
  4554. #touchPinchCallback(_origin, prevDistance, distance) {
  4555. const slowDownFactor = 0.7;
  4556. let factor = slowDownFactor * (distance / prevDistance) + 1 - slowDownFactor;
  4557. if (factor === 1) {
  4558. return;
  4559. }
  4560. const rotationMatrix = this.#getRotationMatrix(this.rotation);
  4561. const transf = (x, y) => [rotationMatrix[0] * x + rotationMatrix[2] * y, rotationMatrix[1] * x + rotationMatrix[3] * y];
  4562. const [parentWidth, parentHeight] = this.parentDimensions;
  4563. const savedX = this.x;
  4564. const savedY = this.y;
  4565. const savedWidth = this.width;
  4566. const savedHeight = this.height;
  4567. const minWidth = AnnotationEditor.MIN_SIZE / parentWidth;
  4568. const minHeight = AnnotationEditor.MIN_SIZE / parentHeight;
  4569. factor = Math.max(Math.min(factor, 1 / savedWidth, 1 / savedHeight), minWidth / savedWidth, minHeight / savedHeight);
  4570. const newWidth = AnnotationEditor._round(savedWidth * factor);
  4571. const newHeight = AnnotationEditor._round(savedHeight * factor);
  4572. if (newWidth === savedWidth && newHeight === savedHeight) {
  4573. return;
  4574. }
  4575. this.#initialRect ||= [savedX, savedY, savedWidth, savedHeight];
  4576. const transfCenterPoint = transf(savedWidth / 2, savedHeight / 2);
  4577. const centerX = AnnotationEditor._round(savedX + transfCenterPoint[0]);
  4578. const centerY = AnnotationEditor._round(savedY + transfCenterPoint[1]);
  4579. const newTransfCenterPoint = transf(newWidth / 2, newHeight / 2);
  4580. this.x = centerX - newTransfCenterPoint[0];
  4581. this.y = centerY - newTransfCenterPoint[1];
  4582. this.width = newWidth;
  4583. this.height = newHeight;
  4584. this.setDims(parentWidth * newWidth, parentHeight * newHeight);
  4585. this.fixAndSetPosition();
  4586. this._onResizing();
  4587. }
  4588. #touchPinchEndCallback() {
  4589. this.#altText?.toggle(true);
  4590. this.parent.togglePointerEvents(true);
  4591. this.#addResizeToUndoStack();
  4592. }
  4593. pointerdown(event) {
  4594. const {
  4595. isMac
  4596. } = util_FeatureTest.platform;
  4597. if (event.button !== 0 || event.ctrlKey && isMac) {
  4598. event.preventDefault();
  4599. return;
  4600. }
  4601. this.#hasBeenClicked = true;
  4602. if (this._isDraggable) {
  4603. this.#setUpDragSession(event);
  4604. return;
  4605. }
  4606. this.#selectOnPointerEvent(event);
  4607. }
  4608. get isSelected() {
  4609. return this._uiManager.isSelected(this);
  4610. }
  4611. #selectOnPointerEvent(event) {
  4612. const {
  4613. isMac
  4614. } = util_FeatureTest.platform;
  4615. if (event.ctrlKey && !isMac || event.shiftKey || event.metaKey && isMac) {
  4616. this.parent.toggleSelected(this);
  4617. } else {
  4618. this.parent.setSelected(this);
  4619. }
  4620. }
  4621. #setUpDragSession(event) {
  4622. const {
  4623. isSelected
  4624. } = this;
  4625. this._uiManager.setUpDragSession();
  4626. let hasDraggingStarted = false;
  4627. const ac = new AbortController();
  4628. const signal = this._uiManager.combinedSignal(ac);
  4629. const opts = {
  4630. capture: true,
  4631. passive: false,
  4632. signal
  4633. };
  4634. const cancelDrag = e => {
  4635. ac.abort();
  4636. this.#dragPointerId = null;
  4637. this.#hasBeenClicked = false;
  4638. if (!this._uiManager.endDragSession()) {
  4639. this.#selectOnPointerEvent(e);
  4640. }
  4641. if (hasDraggingStarted) {
  4642. this._onStopDragging();
  4643. }
  4644. };
  4645. if (isSelected) {
  4646. this.#prevDragX = event.clientX;
  4647. this.#prevDragY = event.clientY;
  4648. this.#dragPointerId = event.pointerId;
  4649. this.#dragPointerType = event.pointerType;
  4650. window.addEventListener("pointermove", e => {
  4651. if (!hasDraggingStarted) {
  4652. hasDraggingStarted = true;
  4653. this._onStartDragging();
  4654. }
  4655. const {
  4656. clientX: x,
  4657. clientY: y,
  4658. pointerId
  4659. } = e;
  4660. if (pointerId !== this.#dragPointerId) {
  4661. stopEvent(e);
  4662. return;
  4663. }
  4664. const [tx, ty] = this.screenToPageTranslation(x - this.#prevDragX, y - this.#prevDragY);
  4665. this.#prevDragX = x;
  4666. this.#prevDragY = y;
  4667. this._uiManager.dragSelectedEditors(tx, ty);
  4668. }, opts);
  4669. window.addEventListener("touchmove", stopEvent, opts);
  4670. window.addEventListener("pointerdown", e => {
  4671. if (e.pointerType === this.#dragPointerType) {
  4672. if (this.#touchManager || e.isPrimary) {
  4673. cancelDrag(e);
  4674. }
  4675. }
  4676. stopEvent(e);
  4677. }, opts);
  4678. }
  4679. const pointerUpCallback = e => {
  4680. if (!this.#dragPointerId || this.#dragPointerId === e.pointerId) {
  4681. cancelDrag(e);
  4682. return;
  4683. }
  4684. stopEvent(e);
  4685. };
  4686. window.addEventListener("pointerup", pointerUpCallback, {
  4687. signal
  4688. });
  4689. window.addEventListener("blur", pointerUpCallback, {
  4690. signal
  4691. });
  4692. }
  4693. _onStartDragging() {}
  4694. _onStopDragging() {}
  4695. moveInDOM() {
  4696. if (this.#moveInDOMTimeout) {
  4697. clearTimeout(this.#moveInDOMTimeout);
  4698. }
  4699. this.#moveInDOMTimeout = setTimeout(() => {
  4700. this.#moveInDOMTimeout = null;
  4701. this.parent?.moveEditorInDOM(this);
  4702. }, 0);
  4703. }
  4704. _setParentAndPosition(parent, x, y) {
  4705. parent.changeParent(this);
  4706. this.x = x;
  4707. this.y = y;
  4708. this.fixAndSetPosition();
  4709. this._onTranslated();
  4710. }
  4711. getRect(tx, ty, rotation = this.rotation) {
  4712. const scale = this.parentScale;
  4713. const [pageWidth, pageHeight] = this.pageDimensions;
  4714. const [pageX, pageY] = this.pageTranslation;
  4715. const shiftX = tx / scale;
  4716. const shiftY = ty / scale;
  4717. const x = this.x * pageWidth;
  4718. const y = this.y * pageHeight;
  4719. const width = this.width * pageWidth;
  4720. const height = this.height * pageHeight;
  4721. switch (rotation) {
  4722. case 0:
  4723. return [x + shiftX + pageX, pageHeight - y - shiftY - height + pageY, x + shiftX + width + pageX, pageHeight - y - shiftY + pageY];
  4724. case 90:
  4725. return [x + shiftY + pageX, pageHeight - y + shiftX + pageY, x + shiftY + height + pageX, pageHeight - y + shiftX + width + pageY];
  4726. case 180:
  4727. return [x - shiftX - width + pageX, pageHeight - y + shiftY + pageY, x - shiftX + pageX, pageHeight - y + shiftY + height + pageY];
  4728. case 270:
  4729. return [x - shiftY - height + pageX, pageHeight - y - shiftX - width + pageY, x - shiftY + pageX, pageHeight - y - shiftX + pageY];
  4730. default:
  4731. throw new Error("Invalid rotation");
  4732. }
  4733. }
  4734. getRectInCurrentCoords(rect, pageHeight) {
  4735. const [x1, y1, x2, y2] = rect;
  4736. const width = x2 - x1;
  4737. const height = y2 - y1;
  4738. switch (this.rotation) {
  4739. case 0:
  4740. return [x1, pageHeight - y2, width, height];
  4741. case 90:
  4742. return [x1, pageHeight - y1, height, width];
  4743. case 180:
  4744. return [x2, pageHeight - y1, width, height];
  4745. case 270:
  4746. return [x2, pageHeight - y2, height, width];
  4747. default:
  4748. throw new Error("Invalid rotation");
  4749. }
  4750. }
  4751. onceAdded(focus) {}
  4752. isEmpty() {
  4753. return false;
  4754. }
  4755. enableEditMode() {
  4756. this.#isInEditMode = true;
  4757. }
  4758. disableEditMode() {
  4759. this.#isInEditMode = false;
  4760. }
  4761. isInEditMode() {
  4762. return this.#isInEditMode;
  4763. }
  4764. shouldGetKeyboardEvents() {
  4765. return this.#isResizerEnabledForKeyboard;
  4766. }
  4767. needsToBeRebuilt() {
  4768. return this.div && !this.isAttachedToDOM;
  4769. }
  4770. get isOnScreen() {
  4771. const {
  4772. top,
  4773. left,
  4774. bottom,
  4775. right
  4776. } = this.getClientDimensions();
  4777. const {
  4778. innerHeight,
  4779. innerWidth
  4780. } = window;
  4781. return left < innerWidth && right > 0 && top < innerHeight && bottom > 0;
  4782. }
  4783. #addFocusListeners() {
  4784. if (this.#focusAC || !this.div) {
  4785. return;
  4786. }
  4787. this.#focusAC = new AbortController();
  4788. const signal = this._uiManager.combinedSignal(this.#focusAC);
  4789. this.div.addEventListener("focusin", this.focusin.bind(this), {
  4790. signal
  4791. });
  4792. this.div.addEventListener("focusout", this.focusout.bind(this), {
  4793. signal
  4794. });
  4795. }
  4796. rebuild() {
  4797. this.#addFocusListeners();
  4798. }
  4799. rotate(_angle) {}
  4800. resize() {}
  4801. serializeDeleted() {
  4802. return {
  4803. id: this.annotationElementId,
  4804. deleted: true,
  4805. pageIndex: this.pageIndex,
  4806. popupRef: this._initialData?.popupRef || ""
  4807. };
  4808. }
  4809. serialize(isForCopying = false, context = null) {
  4810. unreachable("An editor must be serializable");
  4811. }
  4812. static async deserialize(data, parent, uiManager) {
  4813. const editor = new this.prototype.constructor({
  4814. parent,
  4815. id: parent.getNextId(),
  4816. uiManager
  4817. });
  4818. editor.rotation = data.rotation;
  4819. editor.#accessibilityData = data.accessibilityData;
  4820. editor._isCopy = data.isCopy || false;
  4821. const [pageWidth, pageHeight] = editor.pageDimensions;
  4822. const [x, y, width, height] = editor.getRectInCurrentCoords(data.rect, pageHeight);
  4823. editor.x = x / pageWidth;
  4824. editor.y = y / pageHeight;
  4825. editor.width = width / pageWidth;
  4826. editor.height = height / pageHeight;
  4827. return editor;
  4828. }
  4829. get hasBeenModified() {
  4830. return !!this.annotationElementId && (this.deleted || this.serialize() !== null);
  4831. }
  4832. remove() {
  4833. this.#focusAC?.abort();
  4834. this.#focusAC = null;
  4835. if (!this.isEmpty()) {
  4836. this.commit();
  4837. }
  4838. if (this.parent) {
  4839. this.parent.remove(this);
  4840. } else {
  4841. this._uiManager.removeEditor(this);
  4842. }
  4843. if (this.#moveInDOMTimeout) {
  4844. clearTimeout(this.#moveInDOMTimeout);
  4845. this.#moveInDOMTimeout = null;
  4846. }
  4847. this.#stopResizing();
  4848. this.removeEditToolbar();
  4849. if (this.#telemetryTimeouts) {
  4850. for (const timeout of this.#telemetryTimeouts.values()) {
  4851. clearTimeout(timeout);
  4852. }
  4853. this.#telemetryTimeouts = null;
  4854. }
  4855. this.parent = null;
  4856. this.#touchManager?.destroy();
  4857. this.#touchManager = null;
  4858. }
  4859. get isResizable() {
  4860. return false;
  4861. }
  4862. makeResizable() {
  4863. if (this.isResizable) {
  4864. this.#createResizers();
  4865. this.#resizersDiv.classList.remove("hidden");
  4866. }
  4867. }
  4868. get toolbarPosition() {
  4869. return null;
  4870. }
  4871. keydown(event) {
  4872. if (!this.isResizable || event.target !== this.div || event.key !== "Enter") {
  4873. return;
  4874. }
  4875. this._uiManager.setSelected(this);
  4876. this.#savedDimensions = {
  4877. savedX: this.x,
  4878. savedY: this.y,
  4879. savedWidth: this.width,
  4880. savedHeight: this.height
  4881. };
  4882. const children = this.#resizersDiv.children;
  4883. if (!this.#allResizerDivs) {
  4884. this.#allResizerDivs = Array.from(children);
  4885. const boundResizerKeydown = this.#resizerKeydown.bind(this);
  4886. const boundResizerBlur = this.#resizerBlur.bind(this);
  4887. const signal = this._uiManager._signal;
  4888. for (const div of this.#allResizerDivs) {
  4889. const name = div.getAttribute("data-resizer-name");
  4890. div.setAttribute("role", "spinbutton");
  4891. div.addEventListener("keydown", boundResizerKeydown, {
  4892. signal
  4893. });
  4894. div.addEventListener("blur", boundResizerBlur, {
  4895. signal
  4896. });
  4897. div.addEventListener("focus", this.#resizerFocus.bind(this, name), {
  4898. signal
  4899. });
  4900. div.setAttribute("data-l10n-id", AnnotationEditor._l10nResizer[name]);
  4901. }
  4902. }
  4903. const first = this.#allResizerDivs[0];
  4904. let firstPosition = 0;
  4905. for (const div of children) {
  4906. if (div === first) {
  4907. break;
  4908. }
  4909. firstPosition++;
  4910. }
  4911. const nextFirstPosition = (360 - this.rotation + this.parentRotation) % 360 / 90 * (this.#allResizerDivs.length / 4);
  4912. if (nextFirstPosition !== firstPosition) {
  4913. if (nextFirstPosition < firstPosition) {
  4914. for (let i = 0; i < firstPosition - nextFirstPosition; i++) {
  4915. this.#resizersDiv.append(this.#resizersDiv.firstChild);
  4916. }
  4917. } else if (nextFirstPosition > firstPosition) {
  4918. for (let i = 0; i < nextFirstPosition - firstPosition; i++) {
  4919. this.#resizersDiv.firstChild.before(this.#resizersDiv.lastChild);
  4920. }
  4921. }
  4922. let i = 0;
  4923. for (const child of children) {
  4924. const div = this.#allResizerDivs[i++];
  4925. const name = div.getAttribute("data-resizer-name");
  4926. child.setAttribute("data-l10n-id", AnnotationEditor._l10nResizer[name]);
  4927. }
  4928. }
  4929. this.#setResizerTabIndex(0);
  4930. this.#isResizerEnabledForKeyboard = true;
  4931. this.#resizersDiv.firstChild.focus({
  4932. focusVisible: true
  4933. });
  4934. event.preventDefault();
  4935. event.stopImmediatePropagation();
  4936. }
  4937. #resizerKeydown(event) {
  4938. AnnotationEditor._resizerKeyboardManager.exec(this, event);
  4939. }
  4940. #resizerBlur(event) {
  4941. if (this.#isResizerEnabledForKeyboard && event.relatedTarget?.parentNode !== this.#resizersDiv) {
  4942. this.#stopResizing();
  4943. }
  4944. }
  4945. #resizerFocus(name) {
  4946. this.#focusedResizerName = this.#isResizerEnabledForKeyboard ? name : "";
  4947. }
  4948. #setResizerTabIndex(value) {
  4949. if (!this.#allResizerDivs) {
  4950. return;
  4951. }
  4952. for (const div of this.#allResizerDivs) {
  4953. div.tabIndex = value;
  4954. }
  4955. }
  4956. _resizeWithKeyboard(x, y) {
  4957. if (!this.#isResizerEnabledForKeyboard) {
  4958. return;
  4959. }
  4960. this.#resizerPointermove(this.#focusedResizerName, {
  4961. deltaX: x,
  4962. deltaY: y,
  4963. fromKeyboard: true
  4964. });
  4965. }
  4966. #stopResizing() {
  4967. this.#isResizerEnabledForKeyboard = false;
  4968. this.#setResizerTabIndex(-1);
  4969. this.#addResizeToUndoStack();
  4970. }
  4971. _stopResizingWithKeyboard() {
  4972. this.#stopResizing();
  4973. this.div.focus();
  4974. }
  4975. select() {
  4976. this.makeResizable();
  4977. this.div?.classList.add("selectedEditor");
  4978. if (!this._editToolbar) {
  4979. this.addEditToolbar().then(() => {
  4980. if (this.div?.classList.contains("selectedEditor")) {
  4981. this._editToolbar?.show();
  4982. }
  4983. });
  4984. return;
  4985. }
  4986. this._editToolbar?.show();
  4987. this.#altText?.toggleAltTextBadge(false);
  4988. }
  4989. unselect() {
  4990. this.#resizersDiv?.classList.add("hidden");
  4991. this.div?.classList.remove("selectedEditor");
  4992. if (this.div?.contains(document.activeElement)) {
  4993. this._uiManager.currentLayer.div.focus({
  4994. preventScroll: true
  4995. });
  4996. }
  4997. this._editToolbar?.hide();
  4998. this.#altText?.toggleAltTextBadge(true);
  4999. }
  5000. updateParams(type, value) {}
  5001. disableEditing() {}
  5002. enableEditing() {}
  5003. enterInEditMode() {}
  5004. getElementForAltText() {
  5005. return this.div;
  5006. }
  5007. get contentDiv() {
  5008. return this.div;
  5009. }
  5010. get isEditing() {
  5011. return this.#isEditing;
  5012. }
  5013. set isEditing(value) {
  5014. this.#isEditing = value;
  5015. if (!this.parent) {
  5016. return;
  5017. }
  5018. if (value) {
  5019. this.parent.setSelected(this);
  5020. this.parent.setActiveEditor(this);
  5021. } else {
  5022. this.parent.setActiveEditor(null);
  5023. }
  5024. }
  5025. setAspectRatio(width, height) {
  5026. this.#keepAspectRatio = true;
  5027. const aspectRatio = width / height;
  5028. const {
  5029. style
  5030. } = this.div;
  5031. style.aspectRatio = aspectRatio;
  5032. style.height = "auto";
  5033. }
  5034. static get MIN_SIZE() {
  5035. return 16;
  5036. }
  5037. static canCreateNewEmptyEditor() {
  5038. return true;
  5039. }
  5040. get telemetryInitialData() {
  5041. return {
  5042. action: "added"
  5043. };
  5044. }
  5045. get telemetryFinalData() {
  5046. return null;
  5047. }
  5048. _reportTelemetry(data, mustWait = false) {
  5049. if (mustWait) {
  5050. this.#telemetryTimeouts ||= new Map();
  5051. const {
  5052. action
  5053. } = data;
  5054. let timeout = this.#telemetryTimeouts.get(action);
  5055. if (timeout) {
  5056. clearTimeout(timeout);
  5057. }
  5058. timeout = setTimeout(() => {
  5059. this._reportTelemetry(data);
  5060. this.#telemetryTimeouts.delete(action);
  5061. if (this.#telemetryTimeouts.size === 0) {
  5062. this.#telemetryTimeouts = null;
  5063. }
  5064. }, AnnotationEditor._telemetryTimeout);
  5065. this.#telemetryTimeouts.set(action, timeout);
  5066. return;
  5067. }
  5068. data.type ||= this.editorType;
  5069. this._uiManager._eventBus.dispatch("reporttelemetry", {
  5070. source: this,
  5071. details: {
  5072. type: "editing",
  5073. data
  5074. }
  5075. });
  5076. }
  5077. show(visible = this._isVisible) {
  5078. this.div.classList.toggle("hidden", !visible);
  5079. this._isVisible = visible;
  5080. }
  5081. enable() {
  5082. if (this.div) {
  5083. this.div.tabIndex = 0;
  5084. }
  5085. this.#disabled = false;
  5086. }
  5087. disable() {
  5088. if (this.div) {
  5089. this.div.tabIndex = -1;
  5090. }
  5091. this.#disabled = true;
  5092. }
  5093. renderAnnotationElement(annotation) {
  5094. let content = annotation.container.querySelector(".annotationContent");
  5095. if (!content) {
  5096. content = document.createElement("div");
  5097. content.classList.add("annotationContent", this.editorType);
  5098. annotation.container.prepend(content);
  5099. } else if (content.nodeName === "CANVAS") {
  5100. const canvas = content;
  5101. content = document.createElement("div");
  5102. content.classList.add("annotationContent", this.editorType);
  5103. canvas.before(content);
  5104. }
  5105. return content;
  5106. }
  5107. resetAnnotationElement(annotation) {
  5108. const {
  5109. firstChild
  5110. } = annotation.container;
  5111. if (firstChild?.nodeName === "DIV" && firstChild.classList.contains("annotationContent")) {
  5112. firstChild.remove();
  5113. }
  5114. }
  5115. }
  5116. class FakeEditor extends AnnotationEditor {
  5117. constructor(params) {
  5118. super(params);
  5119. this.annotationElementId = params.annotationElementId;
  5120. this.deleted = true;
  5121. }
  5122. serialize() {
  5123. return this.serializeDeleted();
  5124. }
  5125. }
  5126. ;// ./src/shared/murmurhash3.js
  5127. const SEED = 0xc3d2e1f0;
  5128. const MASK_HIGH = 0xffff0000;
  5129. const MASK_LOW = 0xffff;
  5130. class MurmurHash3_64 {
  5131. constructor(seed) {
  5132. this.h1 = seed ? seed & 0xffffffff : SEED;
  5133. this.h2 = seed ? seed & 0xffffffff : SEED;
  5134. }
  5135. update(input) {
  5136. let data, length;
  5137. if (typeof input === "string") {
  5138. data = new Uint8Array(input.length * 2);
  5139. length = 0;
  5140. for (let i = 0, ii = input.length; i < ii; i++) {
  5141. const code = input.charCodeAt(i);
  5142. if (code <= 0xff) {
  5143. data[length++] = code;
  5144. } else {
  5145. data[length++] = code >>> 8;
  5146. data[length++] = code & 0xff;
  5147. }
  5148. }
  5149. } else if (ArrayBuffer.isView(input)) {
  5150. data = input.slice();
  5151. length = data.byteLength;
  5152. } else {
  5153. throw new Error("Invalid data format, must be a string or TypedArray.");
  5154. }
  5155. const blockCounts = length >> 2;
  5156. const tailLength = length - blockCounts * 4;
  5157. const dataUint32 = new Uint32Array(data.buffer, 0, blockCounts);
  5158. let k1 = 0,
  5159. k2 = 0;
  5160. let h1 = this.h1,
  5161. h2 = this.h2;
  5162. const C1 = 0xcc9e2d51,
  5163. C2 = 0x1b873593;
  5164. const C1_LOW = C1 & MASK_LOW,
  5165. C2_LOW = C2 & MASK_LOW;
  5166. for (let i = 0; i < blockCounts; i++) {
  5167. if (i & 1) {
  5168. k1 = dataUint32[i];
  5169. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  5170. k1 = k1 << 15 | k1 >>> 17;
  5171. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  5172. h1 ^= k1;
  5173. h1 = h1 << 13 | h1 >>> 19;
  5174. h1 = h1 * 5 + 0xe6546b64;
  5175. } else {
  5176. k2 = dataUint32[i];
  5177. k2 = k2 * C1 & MASK_HIGH | k2 * C1_LOW & MASK_LOW;
  5178. k2 = k2 << 15 | k2 >>> 17;
  5179. k2 = k2 * C2 & MASK_HIGH | k2 * C2_LOW & MASK_LOW;
  5180. h2 ^= k2;
  5181. h2 = h2 << 13 | h2 >>> 19;
  5182. h2 = h2 * 5 + 0xe6546b64;
  5183. }
  5184. }
  5185. k1 = 0;
  5186. switch (tailLength) {
  5187. case 3:
  5188. k1 ^= data[blockCounts * 4 + 2] << 16;
  5189. case 2:
  5190. k1 ^= data[blockCounts * 4 + 1] << 8;
  5191. case 1:
  5192. k1 ^= data[blockCounts * 4];
  5193. k1 = k1 * C1 & MASK_HIGH | k1 * C1_LOW & MASK_LOW;
  5194. k1 = k1 << 15 | k1 >>> 17;
  5195. k1 = k1 * C2 & MASK_HIGH | k1 * C2_LOW & MASK_LOW;
  5196. if (blockCounts & 1) {
  5197. h1 ^= k1;
  5198. } else {
  5199. h2 ^= k1;
  5200. }
  5201. }
  5202. this.h1 = h1;
  5203. this.h2 = h2;
  5204. }
  5205. hexdigest() {
  5206. let h1 = this.h1,
  5207. h2 = this.h2;
  5208. h1 ^= h2 >>> 1;
  5209. h1 = h1 * 0xed558ccd & MASK_HIGH | h1 * 0x8ccd & MASK_LOW;
  5210. h2 = h2 * 0xff51afd7 & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xafd7ed55 & MASK_HIGH) >>> 16;
  5211. h1 ^= h2 >>> 1;
  5212. h1 = h1 * 0x1a85ec53 & MASK_HIGH | h1 * 0xec53 & MASK_LOW;
  5213. h2 = h2 * 0xc4ceb9fe & MASK_HIGH | ((h2 << 16 | h1 >>> 16) * 0xb9fe1a85 & MASK_HIGH) >>> 16;
  5214. h1 ^= h2 >>> 1;
  5215. return (h1 >>> 0).toString(16).padStart(8, "0") + (h2 >>> 0).toString(16).padStart(8, "0");
  5216. }
  5217. }
  5218. ;// ./src/display/annotation_storage.js
  5219. const SerializableEmpty = Object.freeze({
  5220. map: null,
  5221. hash: "",
  5222. transfer: undefined
  5223. });
  5224. class AnnotationStorage {
  5225. #modified = false;
  5226. #modifiedIds = null;
  5227. #storage = new Map();
  5228. constructor() {
  5229. this.onSetModified = null;
  5230. this.onResetModified = null;
  5231. this.onAnnotationEditor = null;
  5232. }
  5233. getValue(key, defaultValue) {
  5234. const value = this.#storage.get(key);
  5235. if (value === undefined) {
  5236. return defaultValue;
  5237. }
  5238. return Object.assign(defaultValue, value);
  5239. }
  5240. getRawValue(key) {
  5241. return this.#storage.get(key);
  5242. }
  5243. remove(key) {
  5244. this.#storage.delete(key);
  5245. if (this.#storage.size === 0) {
  5246. this.resetModified();
  5247. }
  5248. if (typeof this.onAnnotationEditor === "function") {
  5249. for (const value of this.#storage.values()) {
  5250. if (value instanceof AnnotationEditor) {
  5251. return;
  5252. }
  5253. }
  5254. this.onAnnotationEditor(null);
  5255. }
  5256. }
  5257. setValue(key, value) {
  5258. const obj = this.#storage.get(key);
  5259. let modified = false;
  5260. if (obj !== undefined) {
  5261. for (const [entry, val] of Object.entries(value)) {
  5262. if (obj[entry] !== val) {
  5263. modified = true;
  5264. obj[entry] = val;
  5265. }
  5266. }
  5267. } else {
  5268. modified = true;
  5269. this.#storage.set(key, value);
  5270. }
  5271. if (modified) {
  5272. this.#setModified();
  5273. }
  5274. if (value instanceof AnnotationEditor && typeof this.onAnnotationEditor === "function") {
  5275. this.onAnnotationEditor(value.constructor._type);
  5276. }
  5277. }
  5278. has(key) {
  5279. return this.#storage.has(key);
  5280. }
  5281. get size() {
  5282. return this.#storage.size;
  5283. }
  5284. #setModified() {
  5285. if (!this.#modified) {
  5286. this.#modified = true;
  5287. if (typeof this.onSetModified === "function") {
  5288. this.onSetModified();
  5289. }
  5290. }
  5291. }
  5292. resetModified() {
  5293. if (this.#modified) {
  5294. this.#modified = false;
  5295. if (typeof this.onResetModified === "function") {
  5296. this.onResetModified();
  5297. }
  5298. }
  5299. }
  5300. get print() {
  5301. return new PrintAnnotationStorage(this);
  5302. }
  5303. get serializable() {
  5304. if (this.#storage.size === 0) {
  5305. return SerializableEmpty;
  5306. }
  5307. const map = new Map(),
  5308. hash = new MurmurHash3_64(),
  5309. transfer = [];
  5310. const context = Object.create(null);
  5311. let hasBitmap = false;
  5312. for (const [key, val] of this.#storage) {
  5313. const serialized = val instanceof AnnotationEditor ? val.serialize(false, context) : val;
  5314. if (serialized) {
  5315. map.set(key, serialized);
  5316. hash.update(`${key}:${JSON.stringify(serialized)}`);
  5317. hasBitmap ||= !!serialized.bitmap;
  5318. }
  5319. }
  5320. if (hasBitmap) {
  5321. for (const value of map.values()) {
  5322. if (value.bitmap) {
  5323. transfer.push(value.bitmap);
  5324. }
  5325. }
  5326. }
  5327. return map.size > 0 ? {
  5328. map,
  5329. hash: hash.hexdigest(),
  5330. transfer
  5331. } : SerializableEmpty;
  5332. }
  5333. get editorStats() {
  5334. let stats = null;
  5335. const typeToEditor = new Map();
  5336. for (const value of this.#storage.values()) {
  5337. if (!(value instanceof AnnotationEditor)) {
  5338. continue;
  5339. }
  5340. const editorStats = value.telemetryFinalData;
  5341. if (!editorStats) {
  5342. continue;
  5343. }
  5344. const {
  5345. type
  5346. } = editorStats;
  5347. if (!typeToEditor.has(type)) {
  5348. typeToEditor.set(type, Object.getPrototypeOf(value).constructor);
  5349. }
  5350. stats ||= Object.create(null);
  5351. const map = stats[type] ||= new Map();
  5352. for (const [key, val] of Object.entries(editorStats)) {
  5353. if (key === "type") {
  5354. continue;
  5355. }
  5356. let counters = map.get(key);
  5357. if (!counters) {
  5358. counters = new Map();
  5359. map.set(key, counters);
  5360. }
  5361. const count = counters.get(val) ?? 0;
  5362. counters.set(val, count + 1);
  5363. }
  5364. }
  5365. for (const [type, editor] of typeToEditor) {
  5366. stats[type] = editor.computeTelemetryFinalData(stats[type]);
  5367. }
  5368. return stats;
  5369. }
  5370. resetModifiedIds() {
  5371. this.#modifiedIds = null;
  5372. }
  5373. get modifiedIds() {
  5374. if (this.#modifiedIds) {
  5375. return this.#modifiedIds;
  5376. }
  5377. const ids = [];
  5378. for (const value of this.#storage.values()) {
  5379. if (!(value instanceof AnnotationEditor) || !value.annotationElementId || !value.serialize()) {
  5380. continue;
  5381. }
  5382. ids.push(value.annotationElementId);
  5383. }
  5384. return this.#modifiedIds = {
  5385. ids: new Set(ids),
  5386. hash: ids.join(",")
  5387. };
  5388. }
  5389. [Symbol.iterator]() {
  5390. return this.#storage.entries();
  5391. }
  5392. }
  5393. class PrintAnnotationStorage extends AnnotationStorage {
  5394. #serializable;
  5395. constructor(parent) {
  5396. super();
  5397. const {
  5398. map,
  5399. hash,
  5400. transfer
  5401. } = parent.serializable;
  5402. const clone = structuredClone(map, transfer ? {
  5403. transfer
  5404. } : null);
  5405. this.#serializable = {
  5406. map: clone,
  5407. hash,
  5408. transfer
  5409. };
  5410. }
  5411. get print() {
  5412. unreachable("Should not call PrintAnnotationStorage.print");
  5413. }
  5414. get serializable() {
  5415. return this.#serializable;
  5416. }
  5417. get modifiedIds() {
  5418. return shadow(this, "modifiedIds", {
  5419. ids: new Set(),
  5420. hash: ""
  5421. });
  5422. }
  5423. }
  5424. ;// ./src/display/font_loader.js
  5425. class FontLoader {
  5426. #systemFonts = new Set();
  5427. constructor({
  5428. ownerDocument = globalThis.document,
  5429. styleElement = null
  5430. }) {
  5431. this._document = ownerDocument;
  5432. this.nativeFontFaces = new Set();
  5433. this.styleElement = null;
  5434. this.loadingRequests = [];
  5435. this.loadTestFontId = 0;
  5436. }
  5437. addNativeFontFace(nativeFontFace) {
  5438. this.nativeFontFaces.add(nativeFontFace);
  5439. this._document.fonts.add(nativeFontFace);
  5440. }
  5441. removeNativeFontFace(nativeFontFace) {
  5442. this.nativeFontFaces.delete(nativeFontFace);
  5443. this._document.fonts.delete(nativeFontFace);
  5444. }
  5445. insertRule(rule) {
  5446. if (!this.styleElement) {
  5447. this.styleElement = this._document.createElement("style");
  5448. this._document.documentElement.getElementsByTagName("head")[0].append(this.styleElement);
  5449. }
  5450. const styleSheet = this.styleElement.sheet;
  5451. styleSheet.insertRule(rule, styleSheet.cssRules.length);
  5452. }
  5453. clear() {
  5454. for (const nativeFontFace of this.nativeFontFaces) {
  5455. this._document.fonts.delete(nativeFontFace);
  5456. }
  5457. this.nativeFontFaces.clear();
  5458. this.#systemFonts.clear();
  5459. if (this.styleElement) {
  5460. this.styleElement.remove();
  5461. this.styleElement = null;
  5462. }
  5463. }
  5464. async loadSystemFont({
  5465. systemFontInfo: info,
  5466. disableFontFace,
  5467. _inspectFont
  5468. }) {
  5469. if (!info || this.#systemFonts.has(info.loadedName)) {
  5470. return;
  5471. }
  5472. assert(!disableFontFace, "loadSystemFont shouldn't be called when `disableFontFace` is set.");
  5473. if (this.isFontLoadingAPISupported) {
  5474. const {
  5475. loadedName,
  5476. src,
  5477. style
  5478. } = info;
  5479. const fontFace = new FontFace(loadedName, src, style);
  5480. this.addNativeFontFace(fontFace);
  5481. try {
  5482. await fontFace.load();
  5483. this.#systemFonts.add(loadedName);
  5484. _inspectFont?.(info);
  5485. } catch {
  5486. warn(`Cannot load system font: ${info.baseFontName}, installing it could help to improve PDF rendering.`);
  5487. this.removeNativeFontFace(fontFace);
  5488. }
  5489. return;
  5490. }
  5491. unreachable("Not implemented: loadSystemFont without the Font Loading API.");
  5492. }
  5493. async bind(font) {
  5494. if (font.attached || font.missingFile && !font.systemFontInfo) {
  5495. return;
  5496. }
  5497. font.attached = true;
  5498. if (font.systemFontInfo) {
  5499. await this.loadSystemFont(font);
  5500. return;
  5501. }
  5502. if (this.isFontLoadingAPISupported) {
  5503. const nativeFontFace = font.createNativeFontFace();
  5504. if (nativeFontFace) {
  5505. this.addNativeFontFace(nativeFontFace);
  5506. try {
  5507. await nativeFontFace.loaded;
  5508. } catch (ex) {
  5509. warn(`Failed to load font '${nativeFontFace.family}': '${ex}'.`);
  5510. font.disableFontFace = true;
  5511. throw ex;
  5512. }
  5513. }
  5514. return;
  5515. }
  5516. const rule = font.createFontFaceRule();
  5517. if (rule) {
  5518. this.insertRule(rule);
  5519. if (this.isSyncFontLoadingSupported) {
  5520. return;
  5521. }
  5522. await new Promise(resolve => {
  5523. const request = this._queueLoadingCallback(resolve);
  5524. this._prepareFontLoadEvent(font, request);
  5525. });
  5526. }
  5527. }
  5528. get isFontLoadingAPISupported() {
  5529. const hasFonts = !!this._document?.fonts;
  5530. return shadow(this, "isFontLoadingAPISupported", hasFonts);
  5531. }
  5532. get isSyncFontLoadingSupported() {
  5533. return shadow(this, "isSyncFontLoadingSupported", isNodeJS || util_FeatureTest.platform.isFirefox);
  5534. }
  5535. _queueLoadingCallback(callback) {
  5536. function completeRequest() {
  5537. assert(!request.done, "completeRequest() cannot be called twice.");
  5538. request.done = true;
  5539. while (loadingRequests.length > 0 && loadingRequests[0].done) {
  5540. const otherRequest = loadingRequests.shift();
  5541. setTimeout(otherRequest.callback, 0);
  5542. }
  5543. }
  5544. const {
  5545. loadingRequests
  5546. } = this;
  5547. const request = {
  5548. done: false,
  5549. complete: completeRequest,
  5550. callback
  5551. };
  5552. loadingRequests.push(request);
  5553. return request;
  5554. }
  5555. get _loadTestFont() {
  5556. 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==");
  5557. return shadow(this, "_loadTestFont", testFont);
  5558. }
  5559. _prepareFontLoadEvent(font, request) {
  5560. function int32(data, offset) {
  5561. return data.charCodeAt(offset) << 24 | data.charCodeAt(offset + 1) << 16 | data.charCodeAt(offset + 2) << 8 | data.charCodeAt(offset + 3) & 0xff;
  5562. }
  5563. function spliceString(s, offset, remove, insert) {
  5564. const chunk1 = s.substring(0, offset);
  5565. const chunk2 = s.substring(offset + remove);
  5566. return chunk1 + insert + chunk2;
  5567. }
  5568. let i, ii;
  5569. const canvas = this._document.createElement("canvas");
  5570. canvas.width = 1;
  5571. canvas.height = 1;
  5572. const ctx = canvas.getContext("2d");
  5573. let called = 0;
  5574. function isFontReady(name, callback) {
  5575. if (++called > 30) {
  5576. warn("Load test font never loaded.");
  5577. callback();
  5578. return;
  5579. }
  5580. ctx.font = "30px " + name;
  5581. ctx.fillText(".", 0, 20);
  5582. const imageData = ctx.getImageData(0, 0, 1, 1);
  5583. if (imageData.data[3] > 0) {
  5584. callback();
  5585. return;
  5586. }
  5587. setTimeout(isFontReady.bind(null, name, callback));
  5588. }
  5589. const loadTestFontId = `lt${Date.now()}${this.loadTestFontId++}`;
  5590. let data = this._loadTestFont;
  5591. const COMMENT_OFFSET = 976;
  5592. data = spliceString(data, COMMENT_OFFSET, loadTestFontId.length, loadTestFontId);
  5593. const CFF_CHECKSUM_OFFSET = 16;
  5594. const XXXX_VALUE = 0x58585858;
  5595. let checksum = int32(data, CFF_CHECKSUM_OFFSET);
  5596. for (i = 0, ii = loadTestFontId.length - 3; i < ii; i += 4) {
  5597. checksum = checksum - XXXX_VALUE + int32(loadTestFontId, i) | 0;
  5598. }
  5599. if (i < loadTestFontId.length) {
  5600. checksum = checksum - XXXX_VALUE + int32(loadTestFontId + "XXX", i) | 0;
  5601. }
  5602. data = spliceString(data, CFF_CHECKSUM_OFFSET, 4, string32(checksum));
  5603. const url = `url(data:font/opentype;base64,${btoa(data)});`;
  5604. const rule = `@font-face {font-family:"${loadTestFontId}";src:${url}}`;
  5605. this.insertRule(rule);
  5606. const div = this._document.createElement("div");
  5607. div.style.visibility = "hidden";
  5608. div.style.width = div.style.height = "10px";
  5609. div.style.position = "absolute";
  5610. div.style.top = div.style.left = "0px";
  5611. for (const name of [font.loadedName, loadTestFontId]) {
  5612. const span = this._document.createElement("span");
  5613. span.textContent = "Hi";
  5614. span.style.fontFamily = name;
  5615. div.append(span);
  5616. }
  5617. this._document.body.append(div);
  5618. isFontReady(loadTestFontId, () => {
  5619. div.remove();
  5620. request.complete();
  5621. });
  5622. }
  5623. }
  5624. class FontFaceObject {
  5625. constructor(translatedData, inspectFont = null) {
  5626. this.compiledGlyphs = Object.create(null);
  5627. for (const i in translatedData) {
  5628. this[i] = translatedData[i];
  5629. }
  5630. this._inspectFont = inspectFont;
  5631. }
  5632. createNativeFontFace() {
  5633. if (!this.data || this.disableFontFace) {
  5634. return null;
  5635. }
  5636. let nativeFontFace;
  5637. if (!this.cssFontInfo) {
  5638. nativeFontFace = new FontFace(this.loadedName, this.data, {});
  5639. } else {
  5640. const css = {
  5641. weight: this.cssFontInfo.fontWeight
  5642. };
  5643. if (this.cssFontInfo.italicAngle) {
  5644. css.style = `oblique ${this.cssFontInfo.italicAngle}deg`;
  5645. }
  5646. nativeFontFace = new FontFace(this.cssFontInfo.fontFamily, this.data, css);
  5647. }
  5648. this._inspectFont?.(this);
  5649. return nativeFontFace;
  5650. }
  5651. createFontFaceRule() {
  5652. if (!this.data || this.disableFontFace) {
  5653. return null;
  5654. }
  5655. const url = `url(data:${this.mimetype};base64,${toBase64Util(this.data)});`;
  5656. let rule;
  5657. if (!this.cssFontInfo) {
  5658. rule = `@font-face {font-family:"${this.loadedName}";src:${url}}`;
  5659. } else {
  5660. let css = `font-weight: ${this.cssFontInfo.fontWeight};`;
  5661. if (this.cssFontInfo.italicAngle) {
  5662. css += `font-style: oblique ${this.cssFontInfo.italicAngle}deg;`;
  5663. }
  5664. rule = `@font-face {font-family:"${this.cssFontInfo.fontFamily}";${css}src:${url}}`;
  5665. }
  5666. this._inspectFont?.(this, url);
  5667. return rule;
  5668. }
  5669. getPathGenerator(objs, character) {
  5670. if (this.compiledGlyphs[character] !== undefined) {
  5671. return this.compiledGlyphs[character];
  5672. }
  5673. const objId = this.loadedName + "_path_" + character;
  5674. let cmds;
  5675. try {
  5676. cmds = objs.get(objId);
  5677. } catch (ex) {
  5678. warn(`getPathGenerator - ignoring character: "${ex}".`);
  5679. }
  5680. const path = new Path2D(cmds || "");
  5681. if (!this.fontExtraProperties) {
  5682. objs.delete(objId);
  5683. }
  5684. return this.compiledGlyphs[character] = path;
  5685. }
  5686. }
  5687. ;// ./src/shared/message_handler.js
  5688. const CallbackKind = {
  5689. DATA: 1,
  5690. ERROR: 2
  5691. };
  5692. const StreamKind = {
  5693. CANCEL: 1,
  5694. CANCEL_COMPLETE: 2,
  5695. CLOSE: 3,
  5696. ENQUEUE: 4,
  5697. ERROR: 5,
  5698. PULL: 6,
  5699. PULL_COMPLETE: 7,
  5700. START_COMPLETE: 8
  5701. };
  5702. function onFn() {}
  5703. function wrapReason(ex) {
  5704. if (ex instanceof AbortException || ex instanceof InvalidPDFException || ex instanceof PasswordException || ex instanceof ResponseException || ex instanceof UnknownErrorException) {
  5705. return ex;
  5706. }
  5707. if (!(ex instanceof Error || typeof ex === "object" && ex !== null)) {
  5708. unreachable('wrapReason: Expected "reason" to be a (possibly cloned) Error.');
  5709. }
  5710. switch (ex.name) {
  5711. case "AbortException":
  5712. return new AbortException(ex.message);
  5713. case "InvalidPDFException":
  5714. return new InvalidPDFException(ex.message);
  5715. case "PasswordException":
  5716. return new PasswordException(ex.message, ex.code);
  5717. case "ResponseException":
  5718. return new ResponseException(ex.message, ex.status, ex.missing);
  5719. case "UnknownErrorException":
  5720. return new UnknownErrorException(ex.message, ex.details);
  5721. }
  5722. return new UnknownErrorException(ex.message, ex.toString());
  5723. }
  5724. class MessageHandler {
  5725. #messageAC = new AbortController();
  5726. constructor(sourceName, targetName, comObj) {
  5727. this.sourceName = sourceName;
  5728. this.targetName = targetName;
  5729. this.comObj = comObj;
  5730. this.callbackId = 1;
  5731. this.streamId = 1;
  5732. this.streamSinks = Object.create(null);
  5733. this.streamControllers = Object.create(null);
  5734. this.callbackCapabilities = Object.create(null);
  5735. this.actionHandler = Object.create(null);
  5736. comObj.addEventListener("message", this.#onMessage.bind(this), {
  5737. signal: this.#messageAC.signal
  5738. });
  5739. }
  5740. #onMessage({
  5741. data
  5742. }) {
  5743. if (data.targetName !== this.sourceName) {
  5744. return;
  5745. }
  5746. if (data.stream) {
  5747. this.#processStreamMessage(data);
  5748. return;
  5749. }
  5750. if (data.callback) {
  5751. const callbackId = data.callbackId;
  5752. const capability = this.callbackCapabilities[callbackId];
  5753. if (!capability) {
  5754. throw new Error(`Cannot resolve callback ${callbackId}`);
  5755. }
  5756. delete this.callbackCapabilities[callbackId];
  5757. if (data.callback === CallbackKind.DATA) {
  5758. capability.resolve(data.data);
  5759. } else if (data.callback === CallbackKind.ERROR) {
  5760. capability.reject(wrapReason(data.reason));
  5761. } else {
  5762. throw new Error("Unexpected callback case");
  5763. }
  5764. return;
  5765. }
  5766. const action = this.actionHandler[data.action];
  5767. if (!action) {
  5768. throw new Error(`Unknown action from worker: ${data.action}`);
  5769. }
  5770. if (data.callbackId) {
  5771. const sourceName = this.sourceName,
  5772. targetName = data.sourceName,
  5773. comObj = this.comObj;
  5774. Promise.try(action, data.data).then(function (result) {
  5775. comObj.postMessage({
  5776. sourceName,
  5777. targetName,
  5778. callback: CallbackKind.DATA,
  5779. callbackId: data.callbackId,
  5780. data: result
  5781. });
  5782. }, function (reason) {
  5783. comObj.postMessage({
  5784. sourceName,
  5785. targetName,
  5786. callback: CallbackKind.ERROR,
  5787. callbackId: data.callbackId,
  5788. reason: wrapReason(reason)
  5789. });
  5790. });
  5791. return;
  5792. }
  5793. if (data.streamId) {
  5794. this.#createStreamSink(data);
  5795. return;
  5796. }
  5797. action(data.data);
  5798. }
  5799. on(actionName, handler) {
  5800. const ah = this.actionHandler;
  5801. if (ah[actionName]) {
  5802. throw new Error(`There is already an actionName called "${actionName}"`);
  5803. }
  5804. ah[actionName] = handler;
  5805. }
  5806. send(actionName, data, transfers) {
  5807. this.comObj.postMessage({
  5808. sourceName: this.sourceName,
  5809. targetName: this.targetName,
  5810. action: actionName,
  5811. data
  5812. }, transfers);
  5813. }
  5814. sendWithPromise(actionName, data, transfers) {
  5815. const callbackId = this.callbackId++;
  5816. const capability = Promise.withResolvers();
  5817. this.callbackCapabilities[callbackId] = capability;
  5818. try {
  5819. this.comObj.postMessage({
  5820. sourceName: this.sourceName,
  5821. targetName: this.targetName,
  5822. action: actionName,
  5823. callbackId,
  5824. data
  5825. }, transfers);
  5826. } catch (ex) {
  5827. capability.reject(ex);
  5828. }
  5829. return capability.promise;
  5830. }
  5831. sendWithStream(actionName, data, queueingStrategy, transfers) {
  5832. const streamId = this.streamId++,
  5833. sourceName = this.sourceName,
  5834. targetName = this.targetName,
  5835. comObj = this.comObj;
  5836. return new ReadableStream({
  5837. start: controller => {
  5838. const startCapability = Promise.withResolvers();
  5839. this.streamControllers[streamId] = {
  5840. controller,
  5841. startCall: startCapability,
  5842. pullCall: null,
  5843. cancelCall: null,
  5844. isClosed: false
  5845. };
  5846. comObj.postMessage({
  5847. sourceName,
  5848. targetName,
  5849. action: actionName,
  5850. streamId,
  5851. data,
  5852. desiredSize: controller.desiredSize
  5853. }, transfers);
  5854. return startCapability.promise;
  5855. },
  5856. pull: controller => {
  5857. const pullCapability = Promise.withResolvers();
  5858. this.streamControllers[streamId].pullCall = pullCapability;
  5859. comObj.postMessage({
  5860. sourceName,
  5861. targetName,
  5862. stream: StreamKind.PULL,
  5863. streamId,
  5864. desiredSize: controller.desiredSize
  5865. });
  5866. return pullCapability.promise;
  5867. },
  5868. cancel: reason => {
  5869. assert(reason instanceof Error, "cancel must have a valid reason");
  5870. const cancelCapability = Promise.withResolvers();
  5871. this.streamControllers[streamId].cancelCall = cancelCapability;
  5872. this.streamControllers[streamId].isClosed = true;
  5873. comObj.postMessage({
  5874. sourceName,
  5875. targetName,
  5876. stream: StreamKind.CANCEL,
  5877. streamId,
  5878. reason: wrapReason(reason)
  5879. });
  5880. return cancelCapability.promise;
  5881. }
  5882. }, queueingStrategy);
  5883. }
  5884. #createStreamSink(data) {
  5885. const streamId = data.streamId,
  5886. sourceName = this.sourceName,
  5887. targetName = data.sourceName,
  5888. comObj = this.comObj;
  5889. const self = this,
  5890. action = this.actionHandler[data.action];
  5891. const streamSink = {
  5892. enqueue(chunk, size = 1, transfers) {
  5893. if (this.isCancelled) {
  5894. return;
  5895. }
  5896. const lastDesiredSize = this.desiredSize;
  5897. this.desiredSize -= size;
  5898. if (lastDesiredSize > 0 && this.desiredSize <= 0) {
  5899. this.sinkCapability = Promise.withResolvers();
  5900. this.ready = this.sinkCapability.promise;
  5901. }
  5902. comObj.postMessage({
  5903. sourceName,
  5904. targetName,
  5905. stream: StreamKind.ENQUEUE,
  5906. streamId,
  5907. chunk
  5908. }, transfers);
  5909. },
  5910. close() {
  5911. if (this.isCancelled) {
  5912. return;
  5913. }
  5914. this.isCancelled = true;
  5915. comObj.postMessage({
  5916. sourceName,
  5917. targetName,
  5918. stream: StreamKind.CLOSE,
  5919. streamId
  5920. });
  5921. delete self.streamSinks[streamId];
  5922. },
  5923. error(reason) {
  5924. assert(reason instanceof Error, "error must have a valid reason");
  5925. if (this.isCancelled) {
  5926. return;
  5927. }
  5928. this.isCancelled = true;
  5929. comObj.postMessage({
  5930. sourceName,
  5931. targetName,
  5932. stream: StreamKind.ERROR,
  5933. streamId,
  5934. reason: wrapReason(reason)
  5935. });
  5936. },
  5937. sinkCapability: Promise.withResolvers(),
  5938. onPull: null,
  5939. onCancel: null,
  5940. isCancelled: false,
  5941. desiredSize: data.desiredSize,
  5942. ready: null
  5943. };
  5944. streamSink.sinkCapability.resolve();
  5945. streamSink.ready = streamSink.sinkCapability.promise;
  5946. this.streamSinks[streamId] = streamSink;
  5947. Promise.try(action, data.data, streamSink).then(function () {
  5948. comObj.postMessage({
  5949. sourceName,
  5950. targetName,
  5951. stream: StreamKind.START_COMPLETE,
  5952. streamId,
  5953. success: true
  5954. });
  5955. }, function (reason) {
  5956. comObj.postMessage({
  5957. sourceName,
  5958. targetName,
  5959. stream: StreamKind.START_COMPLETE,
  5960. streamId,
  5961. reason: wrapReason(reason)
  5962. });
  5963. });
  5964. }
  5965. #processStreamMessage(data) {
  5966. const streamId = data.streamId,
  5967. sourceName = this.sourceName,
  5968. targetName = data.sourceName,
  5969. comObj = this.comObj;
  5970. const streamController = this.streamControllers[streamId],
  5971. streamSink = this.streamSinks[streamId];
  5972. switch (data.stream) {
  5973. case StreamKind.START_COMPLETE:
  5974. if (data.success) {
  5975. streamController.startCall.resolve();
  5976. } else {
  5977. streamController.startCall.reject(wrapReason(data.reason));
  5978. }
  5979. break;
  5980. case StreamKind.PULL_COMPLETE:
  5981. if (data.success) {
  5982. streamController.pullCall.resolve();
  5983. } else {
  5984. streamController.pullCall.reject(wrapReason(data.reason));
  5985. }
  5986. break;
  5987. case StreamKind.PULL:
  5988. if (!streamSink) {
  5989. comObj.postMessage({
  5990. sourceName,
  5991. targetName,
  5992. stream: StreamKind.PULL_COMPLETE,
  5993. streamId,
  5994. success: true
  5995. });
  5996. break;
  5997. }
  5998. if (streamSink.desiredSize <= 0 && data.desiredSize > 0) {
  5999. streamSink.sinkCapability.resolve();
  6000. }
  6001. streamSink.desiredSize = data.desiredSize;
  6002. Promise.try(streamSink.onPull || onFn).then(function () {
  6003. comObj.postMessage({
  6004. sourceName,
  6005. targetName,
  6006. stream: StreamKind.PULL_COMPLETE,
  6007. streamId,
  6008. success: true
  6009. });
  6010. }, function (reason) {
  6011. comObj.postMessage({
  6012. sourceName,
  6013. targetName,
  6014. stream: StreamKind.PULL_COMPLETE,
  6015. streamId,
  6016. reason: wrapReason(reason)
  6017. });
  6018. });
  6019. break;
  6020. case StreamKind.ENQUEUE:
  6021. assert(streamController, "enqueue should have stream controller");
  6022. if (streamController.isClosed) {
  6023. break;
  6024. }
  6025. streamController.controller.enqueue(data.chunk);
  6026. break;
  6027. case StreamKind.CLOSE:
  6028. assert(streamController, "close should have stream controller");
  6029. if (streamController.isClosed) {
  6030. break;
  6031. }
  6032. streamController.isClosed = true;
  6033. streamController.controller.close();
  6034. this.#deleteStreamController(streamController, streamId);
  6035. break;
  6036. case StreamKind.ERROR:
  6037. assert(streamController, "error should have stream controller");
  6038. streamController.controller.error(wrapReason(data.reason));
  6039. this.#deleteStreamController(streamController, streamId);
  6040. break;
  6041. case StreamKind.CANCEL_COMPLETE:
  6042. if (data.success) {
  6043. streamController.cancelCall.resolve();
  6044. } else {
  6045. streamController.cancelCall.reject(wrapReason(data.reason));
  6046. }
  6047. this.#deleteStreamController(streamController, streamId);
  6048. break;
  6049. case StreamKind.CANCEL:
  6050. if (!streamSink) {
  6051. break;
  6052. }
  6053. const dataReason = wrapReason(data.reason);
  6054. Promise.try(streamSink.onCancel || onFn, dataReason).then(function () {
  6055. comObj.postMessage({
  6056. sourceName,
  6057. targetName,
  6058. stream: StreamKind.CANCEL_COMPLETE,
  6059. streamId,
  6060. success: true
  6061. });
  6062. }, function (reason) {
  6063. comObj.postMessage({
  6064. sourceName,
  6065. targetName,
  6066. stream: StreamKind.CANCEL_COMPLETE,
  6067. streamId,
  6068. reason: wrapReason(reason)
  6069. });
  6070. });
  6071. streamSink.sinkCapability.reject(dataReason);
  6072. streamSink.isCancelled = true;
  6073. delete this.streamSinks[streamId];
  6074. break;
  6075. default:
  6076. throw new Error("Unexpected stream case");
  6077. }
  6078. }
  6079. async #deleteStreamController(streamController, streamId) {
  6080. await Promise.allSettled([streamController.startCall?.promise, streamController.pullCall?.promise, streamController.cancelCall?.promise]);
  6081. delete this.streamControllers[streamId];
  6082. }
  6083. destroy() {
  6084. this.#messageAC?.abort();
  6085. this.#messageAC = null;
  6086. }
  6087. }
  6088. ;// ./src/display/canvas_factory.js
  6089. class BaseCanvasFactory {
  6090. #enableHWA = false;
  6091. constructor({
  6092. enableHWA = false
  6093. }) {
  6094. this.#enableHWA = enableHWA;
  6095. }
  6096. create(width, height) {
  6097. if (width <= 0 || height <= 0) {
  6098. throw new Error("Invalid canvas size");
  6099. }
  6100. const canvas = this._createCanvas(width, height);
  6101. return {
  6102. canvas,
  6103. context: canvas.getContext("2d", {
  6104. willReadFrequently: !this.#enableHWA
  6105. })
  6106. };
  6107. }
  6108. reset(canvasAndContext, width, height) {
  6109. if (!canvasAndContext.canvas) {
  6110. throw new Error("Canvas is not specified");
  6111. }
  6112. if (width <= 0 || height <= 0) {
  6113. throw new Error("Invalid canvas size");
  6114. }
  6115. canvasAndContext.canvas.width = width;
  6116. canvasAndContext.canvas.height = height;
  6117. }
  6118. destroy(canvasAndContext) {
  6119. if (!canvasAndContext.canvas) {
  6120. throw new Error("Canvas is not specified");
  6121. }
  6122. canvasAndContext.canvas.width = 0;
  6123. canvasAndContext.canvas.height = 0;
  6124. canvasAndContext.canvas = null;
  6125. canvasAndContext.context = null;
  6126. }
  6127. _createCanvas(width, height) {
  6128. unreachable("Abstract method `_createCanvas` called.");
  6129. }
  6130. }
  6131. class DOMCanvasFactory extends BaseCanvasFactory {
  6132. constructor({
  6133. ownerDocument = globalThis.document,
  6134. enableHWA = false
  6135. }) {
  6136. super({
  6137. enableHWA
  6138. });
  6139. this._document = ownerDocument;
  6140. }
  6141. _createCanvas(width, height) {
  6142. const canvas = this._document.createElement("canvas");
  6143. canvas.width = width;
  6144. canvas.height = height;
  6145. return canvas;
  6146. }
  6147. }
  6148. ;// ./src/display/cmap_reader_factory.js
  6149. class BaseCMapReaderFactory {
  6150. constructor({
  6151. baseUrl = null,
  6152. isCompressed = true
  6153. }) {
  6154. this.baseUrl = baseUrl;
  6155. this.isCompressed = isCompressed;
  6156. }
  6157. async fetch({
  6158. name
  6159. }) {
  6160. if (!this.baseUrl) {
  6161. throw new Error("Ensure that the `cMapUrl` and `cMapPacked` API parameters are provided.");
  6162. }
  6163. if (!name) {
  6164. throw new Error("CMap name must be specified.");
  6165. }
  6166. const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
  6167. return this._fetch(url).then(cMapData => ({
  6168. cMapData,
  6169. isCompressed: this.isCompressed
  6170. })).catch(reason => {
  6171. throw new Error(`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`);
  6172. });
  6173. }
  6174. async _fetch(url) {
  6175. unreachable("Abstract method `_fetch` called.");
  6176. }
  6177. }
  6178. class DOMCMapReaderFactory extends BaseCMapReaderFactory {
  6179. async _fetch(url) {
  6180. const data = await fetchData(url, this.isCompressed ? "arraybuffer" : "text");
  6181. return data instanceof ArrayBuffer ? new Uint8Array(data) : stringToBytes(data);
  6182. }
  6183. }
  6184. ;// ./src/display/filter_factory.js
  6185. class BaseFilterFactory {
  6186. addFilter(maps) {
  6187. return "none";
  6188. }
  6189. addHCMFilter(fgColor, bgColor) {
  6190. return "none";
  6191. }
  6192. addAlphaFilter(map) {
  6193. return "none";
  6194. }
  6195. addLuminosityFilter(map) {
  6196. return "none";
  6197. }
  6198. addHighlightHCMFilter(filterName, fgColor, bgColor, newFgColor, newBgColor) {
  6199. return "none";
  6200. }
  6201. destroy(keepHCM = false) {}
  6202. }
  6203. class DOMFilterFactory extends BaseFilterFactory {
  6204. #baseUrl;
  6205. #_cache;
  6206. #_defs;
  6207. #docId;
  6208. #document;
  6209. #_hcmCache;
  6210. #id = 0;
  6211. constructor({
  6212. docId,
  6213. ownerDocument = globalThis.document
  6214. }) {
  6215. super();
  6216. this.#docId = docId;
  6217. this.#document = ownerDocument;
  6218. }
  6219. get #cache() {
  6220. return this.#_cache ||= new Map();
  6221. }
  6222. get #hcmCache() {
  6223. return this.#_hcmCache ||= new Map();
  6224. }
  6225. get #defs() {
  6226. if (!this.#_defs) {
  6227. const div = this.#document.createElement("div");
  6228. const {
  6229. style
  6230. } = div;
  6231. style.visibility = "hidden";
  6232. style.contain = "strict";
  6233. style.width = style.height = 0;
  6234. style.position = "absolute";
  6235. style.top = style.left = 0;
  6236. style.zIndex = -1;
  6237. const svg = this.#document.createElementNS(SVG_NS, "svg");
  6238. svg.setAttribute("width", 0);
  6239. svg.setAttribute("height", 0);
  6240. this.#_defs = this.#document.createElementNS(SVG_NS, "defs");
  6241. div.append(svg);
  6242. svg.append(this.#_defs);
  6243. this.#document.body.append(div);
  6244. }
  6245. return this.#_defs;
  6246. }
  6247. #createTables(maps) {
  6248. if (maps.length === 1) {
  6249. const mapR = maps[0];
  6250. const buffer = new Array(256);
  6251. for (let i = 0; i < 256; i++) {
  6252. buffer[i] = mapR[i] / 255;
  6253. }
  6254. const table = buffer.join(",");
  6255. return [table, table, table];
  6256. }
  6257. const [mapR, mapG, mapB] = maps;
  6258. const bufferR = new Array(256);
  6259. const bufferG = new Array(256);
  6260. const bufferB = new Array(256);
  6261. for (let i = 0; i < 256; i++) {
  6262. bufferR[i] = mapR[i] / 255;
  6263. bufferG[i] = mapG[i] / 255;
  6264. bufferB[i] = mapB[i] / 255;
  6265. }
  6266. return [bufferR.join(","), bufferG.join(","), bufferB.join(",")];
  6267. }
  6268. #createUrl(id) {
  6269. if (this.#baseUrl === undefined) {
  6270. this.#baseUrl = "";
  6271. const url = this.#document.URL;
  6272. if (url !== this.#document.baseURI) {
  6273. if (isDataScheme(url)) {
  6274. warn('#createUrl: ignore "data:"-URL for performance reasons.');
  6275. } else {
  6276. this.#baseUrl = updateUrlHash(url, "");
  6277. }
  6278. }
  6279. }
  6280. return `url(${this.#baseUrl}#${id})`;
  6281. }
  6282. addFilter(maps) {
  6283. if (!maps) {
  6284. return "none";
  6285. }
  6286. let value = this.#cache.get(maps);
  6287. if (value) {
  6288. return value;
  6289. }
  6290. const [tableR, tableG, tableB] = this.#createTables(maps);
  6291. const key = maps.length === 1 ? tableR : `${tableR}${tableG}${tableB}`;
  6292. value = this.#cache.get(key);
  6293. if (value) {
  6294. this.#cache.set(maps, value);
  6295. return value;
  6296. }
  6297. const id = `g_${this.#docId}_transfer_map_${this.#id++}`;
  6298. const url = this.#createUrl(id);
  6299. this.#cache.set(maps, url);
  6300. this.#cache.set(key, url);
  6301. const filter = this.#createFilter(id);
  6302. this.#addTransferMapConversion(tableR, tableG, tableB, filter);
  6303. return url;
  6304. }
  6305. addHCMFilter(fgColor, bgColor) {
  6306. const key = `${fgColor}-${bgColor}`;
  6307. const filterName = "base";
  6308. let info = this.#hcmCache.get(filterName);
  6309. if (info?.key === key) {
  6310. return info.url;
  6311. }
  6312. if (info) {
  6313. info.filter?.remove();
  6314. info.key = key;
  6315. info.url = "none";
  6316. info.filter = null;
  6317. } else {
  6318. info = {
  6319. key,
  6320. url: "none",
  6321. filter: null
  6322. };
  6323. this.#hcmCache.set(filterName, info);
  6324. }
  6325. if (!fgColor || !bgColor) {
  6326. return info.url;
  6327. }
  6328. const fgRGB = this.#getRGB(fgColor);
  6329. fgColor = Util.makeHexColor(...fgRGB);
  6330. const bgRGB = this.#getRGB(bgColor);
  6331. bgColor = Util.makeHexColor(...bgRGB);
  6332. this.#defs.style.color = "";
  6333. if (fgColor === "#000000" && bgColor === "#ffffff" || fgColor === bgColor) {
  6334. return info.url;
  6335. }
  6336. const map = new Array(256);
  6337. for (let i = 0; i <= 255; i++) {
  6338. const x = i / 255;
  6339. map[i] = x <= 0.03928 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4;
  6340. }
  6341. const table = map.join(",");
  6342. const id = `g_${this.#docId}_hcm_filter`;
  6343. const filter = info.filter = this.#createFilter(id);
  6344. this.#addTransferMapConversion(table, table, table, filter);
  6345. this.#addGrayConversion(filter);
  6346. const getSteps = (c, n) => {
  6347. const start = fgRGB[c] / 255;
  6348. const end = bgRGB[c] / 255;
  6349. const arr = new Array(n + 1);
  6350. for (let i = 0; i <= n; i++) {
  6351. arr[i] = start + i / n * (end - start);
  6352. }
  6353. return arr.join(",");
  6354. };
  6355. this.#addTransferMapConversion(getSteps(0, 5), getSteps(1, 5), getSteps(2, 5), filter);
  6356. info.url = this.#createUrl(id);
  6357. return info.url;
  6358. }
  6359. addAlphaFilter(map) {
  6360. let value = this.#cache.get(map);
  6361. if (value) {
  6362. return value;
  6363. }
  6364. const [tableA] = this.#createTables([map]);
  6365. const key = `alpha_${tableA}`;
  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}_alpha_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.#addTransferMapAlphaConversion(tableA, filter);
  6377. return url;
  6378. }
  6379. addLuminosityFilter(map) {
  6380. let value = this.#cache.get(map || "luminosity");
  6381. if (value) {
  6382. return value;
  6383. }
  6384. let tableA, key;
  6385. if (map) {
  6386. [tableA] = this.#createTables([map]);
  6387. key = `luminosity_${tableA}`;
  6388. } else {
  6389. key = "luminosity";
  6390. }
  6391. value = this.#cache.get(key);
  6392. if (value) {
  6393. this.#cache.set(map, value);
  6394. return value;
  6395. }
  6396. const id = `g_${this.#docId}_luminosity_map_${this.#id++}`;
  6397. const url = this.#createUrl(id);
  6398. this.#cache.set(map, url);
  6399. this.#cache.set(key, url);
  6400. const filter = this.#createFilter(id);
  6401. this.#addLuminosityConversion(filter);
  6402. if (map) {
  6403. this.#addTransferMapAlphaConversion(tableA, filter);
  6404. }
  6405. return url;
  6406. }
  6407. addHighlightHCMFilter(filterName, fgColor, bgColor, newFgColor, newBgColor) {
  6408. const key = `${fgColor}-${bgColor}-${newFgColor}-${newBgColor}`;
  6409. let info = this.#hcmCache.get(filterName);
  6410. if (info?.key === key) {
  6411. return info.url;
  6412. }
  6413. if (info) {
  6414. info.filter?.remove();
  6415. info.key = key;
  6416. info.url = "none";
  6417. info.filter = null;
  6418. } else {
  6419. info = {
  6420. key,
  6421. url: "none",
  6422. filter: null
  6423. };
  6424. this.#hcmCache.set(filterName, info);
  6425. }
  6426. if (!fgColor || !bgColor) {
  6427. return info.url;
  6428. }
  6429. const [fgRGB, bgRGB] = [fgColor, bgColor].map(this.#getRGB.bind(this));
  6430. let fgGray = Math.round(0.2126 * fgRGB[0] + 0.7152 * fgRGB[1] + 0.0722 * fgRGB[2]);
  6431. let bgGray = Math.round(0.2126 * bgRGB[0] + 0.7152 * bgRGB[1] + 0.0722 * bgRGB[2]);
  6432. let [newFgRGB, newBgRGB] = [newFgColor, newBgColor].map(this.#getRGB.bind(this));
  6433. if (bgGray < fgGray) {
  6434. [fgGray, bgGray, newFgRGB, newBgRGB] = [bgGray, fgGray, newBgRGB, newFgRGB];
  6435. }
  6436. this.#defs.style.color = "";
  6437. const getSteps = (fg, bg, n) => {
  6438. const arr = new Array(256);
  6439. const step = (bgGray - fgGray) / n;
  6440. const newStart = fg / 255;
  6441. const newStep = (bg - fg) / (255 * n);
  6442. let prev = 0;
  6443. for (let i = 0; i <= n; i++) {
  6444. const k = Math.round(fgGray + i * step);
  6445. const value = newStart + i * newStep;
  6446. for (let j = prev; j <= k; j++) {
  6447. arr[j] = value;
  6448. }
  6449. prev = k + 1;
  6450. }
  6451. for (let i = prev; i < 256; i++) {
  6452. arr[i] = arr[prev - 1];
  6453. }
  6454. return arr.join(",");
  6455. };
  6456. const id = `g_${this.#docId}_hcm_${filterName}_filter`;
  6457. const filter = info.filter = this.#createFilter(id);
  6458. this.#addGrayConversion(filter);
  6459. this.#addTransferMapConversion(getSteps(newFgRGB[0], newBgRGB[0], 5), getSteps(newFgRGB[1], newBgRGB[1], 5), getSteps(newFgRGB[2], newBgRGB[2], 5), filter);
  6460. info.url = this.#createUrl(id);
  6461. return info.url;
  6462. }
  6463. destroy(keepHCM = false) {
  6464. if (keepHCM && this.#_hcmCache?.size) {
  6465. return;
  6466. }
  6467. this.#_defs?.parentNode.parentNode.remove();
  6468. this.#_defs = null;
  6469. this.#_cache?.clear();
  6470. this.#_cache = null;
  6471. this.#_hcmCache?.clear();
  6472. this.#_hcmCache = null;
  6473. this.#id = 0;
  6474. }
  6475. #addLuminosityConversion(filter) {
  6476. const feColorMatrix = this.#document.createElementNS(SVG_NS, "feColorMatrix");
  6477. feColorMatrix.setAttribute("type", "matrix");
  6478. 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");
  6479. filter.append(feColorMatrix);
  6480. }
  6481. #addGrayConversion(filter) {
  6482. const feColorMatrix = this.#document.createElementNS(SVG_NS, "feColorMatrix");
  6483. feColorMatrix.setAttribute("type", "matrix");
  6484. 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");
  6485. filter.append(feColorMatrix);
  6486. }
  6487. #createFilter(id) {
  6488. const filter = this.#document.createElementNS(SVG_NS, "filter");
  6489. filter.setAttribute("color-interpolation-filters", "sRGB");
  6490. filter.setAttribute("id", id);
  6491. this.#defs.append(filter);
  6492. return filter;
  6493. }
  6494. #appendFeFunc(feComponentTransfer, func, table) {
  6495. const feFunc = this.#document.createElementNS(SVG_NS, func);
  6496. feFunc.setAttribute("type", "discrete");
  6497. feFunc.setAttribute("tableValues", table);
  6498. feComponentTransfer.append(feFunc);
  6499. }
  6500. #addTransferMapConversion(rTable, gTable, bTable, filter) {
  6501. const feComponentTransfer = this.#document.createElementNS(SVG_NS, "feComponentTransfer");
  6502. filter.append(feComponentTransfer);
  6503. this.#appendFeFunc(feComponentTransfer, "feFuncR", rTable);
  6504. this.#appendFeFunc(feComponentTransfer, "feFuncG", gTable);
  6505. this.#appendFeFunc(feComponentTransfer, "feFuncB", bTable);
  6506. }
  6507. #addTransferMapAlphaConversion(aTable, filter) {
  6508. const feComponentTransfer = this.#document.createElementNS(SVG_NS, "feComponentTransfer");
  6509. filter.append(feComponentTransfer);
  6510. this.#appendFeFunc(feComponentTransfer, "feFuncA", aTable);
  6511. }
  6512. #getRGB(color) {
  6513. this.#defs.style.color = color;
  6514. return getRGB(getComputedStyle(this.#defs).getPropertyValue("color"));
  6515. }
  6516. }
  6517. ;// ./src/display/standard_fontdata_factory.js
  6518. class BaseStandardFontDataFactory {
  6519. constructor({
  6520. baseUrl = null
  6521. }) {
  6522. this.baseUrl = baseUrl;
  6523. }
  6524. async fetch({
  6525. filename
  6526. }) {
  6527. if (!this.baseUrl) {
  6528. throw new Error("Ensure that the `standardFontDataUrl` API parameter is provided.");
  6529. }
  6530. if (!filename) {
  6531. throw new Error("Font filename must be specified.");
  6532. }
  6533. const url = `${this.baseUrl}${filename}`;
  6534. return this._fetch(url).catch(reason => {
  6535. throw new Error(`Unable to load font data at: ${url}`);
  6536. });
  6537. }
  6538. async _fetch(url) {
  6539. unreachable("Abstract method `_fetch` called.");
  6540. }
  6541. }
  6542. class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
  6543. async _fetch(url) {
  6544. const data = await fetchData(url, "arraybuffer");
  6545. return new Uint8Array(data);
  6546. }
  6547. }
  6548. ;// ./src/display/wasm_factory.js
  6549. class BaseWasmFactory {
  6550. constructor({
  6551. baseUrl = null
  6552. }) {
  6553. this.baseUrl = baseUrl;
  6554. }
  6555. async fetch({
  6556. filename
  6557. }) {
  6558. if (!this.baseUrl) {
  6559. throw new Error("Ensure that the `wasmUrl` API parameter is provided.");
  6560. }
  6561. if (!filename) {
  6562. throw new Error("Wasm filename must be specified.");
  6563. }
  6564. const url = `${this.baseUrl}${filename}`;
  6565. return this._fetch(url).catch(reason => {
  6566. throw new Error(`Unable to load wasm data at: ${url}`);
  6567. });
  6568. }
  6569. async _fetch(url) {
  6570. unreachable("Abstract method `_fetch` called.");
  6571. }
  6572. }
  6573. class DOMWasmFactory extends BaseWasmFactory {
  6574. async _fetch(url) {
  6575. const data = await fetchData(url, "arraybuffer");
  6576. return new Uint8Array(data);
  6577. }
  6578. }
  6579. ;// ./src/display/node_utils.js
  6580. if (isNodeJS) {
  6581. warn("Please use the `legacy` build in Node.js environments.");
  6582. }
  6583. async function node_utils_fetchData(url) {
  6584. const fs = process.getBuiltinModule("fs");
  6585. const data = await fs.promises.readFile(url);
  6586. return new Uint8Array(data);
  6587. }
  6588. class NodeFilterFactory extends BaseFilterFactory {}
  6589. class NodeCanvasFactory extends BaseCanvasFactory {
  6590. _createCanvas(width, height) {
  6591. const require = process.getBuiltinModule("module").createRequire(import.meta.url);
  6592. const canvas = require("@napi-rs/canvas");
  6593. return canvas.createCanvas(width, height);
  6594. }
  6595. }
  6596. class NodeCMapReaderFactory extends BaseCMapReaderFactory {
  6597. async _fetch(url) {
  6598. return node_utils_fetchData(url);
  6599. }
  6600. }
  6601. class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
  6602. async _fetch(url) {
  6603. return node_utils_fetchData(url);
  6604. }
  6605. }
  6606. class NodeWasmFactory extends BaseWasmFactory {
  6607. async _fetch(url) {
  6608. return node_utils_fetchData(url);
  6609. }
  6610. }
  6611. ;// ./src/display/pattern_helper.js
  6612. const PathType = {
  6613. FILL: "Fill",
  6614. STROKE: "Stroke",
  6615. SHADING: "Shading"
  6616. };
  6617. function applyBoundingBox(ctx, bbox) {
  6618. if (!bbox) {
  6619. return;
  6620. }
  6621. const width = bbox[2] - bbox[0];
  6622. const height = bbox[3] - bbox[1];
  6623. const region = new Path2D();
  6624. region.rect(bbox[0], bbox[1], width, height);
  6625. ctx.clip(region);
  6626. }
  6627. class BaseShadingPattern {
  6628. isModifyingCurrentTransform() {
  6629. return false;
  6630. }
  6631. getPattern() {
  6632. unreachable("Abstract method `getPattern` called.");
  6633. }
  6634. }
  6635. class RadialAxialShadingPattern extends BaseShadingPattern {
  6636. constructor(IR) {
  6637. super();
  6638. this._type = IR[1];
  6639. this._bbox = IR[2];
  6640. this._colorStops = IR[3];
  6641. this._p0 = IR[4];
  6642. this._p1 = IR[5];
  6643. this._r0 = IR[6];
  6644. this._r1 = IR[7];
  6645. this.matrix = null;
  6646. }
  6647. _createGradient(ctx) {
  6648. let grad;
  6649. if (this._type === "axial") {
  6650. grad = ctx.createLinearGradient(this._p0[0], this._p0[1], this._p1[0], this._p1[1]);
  6651. } else if (this._type === "radial") {
  6652. grad = ctx.createRadialGradient(this._p0[0], this._p0[1], this._r0, this._p1[0], this._p1[1], this._r1);
  6653. }
  6654. for (const colorStop of this._colorStops) {
  6655. grad.addColorStop(colorStop[0], colorStop[1]);
  6656. }
  6657. return grad;
  6658. }
  6659. getPattern(ctx, owner, inverse, pathType) {
  6660. let pattern;
  6661. if (pathType === PathType.STROKE || pathType === PathType.FILL) {
  6662. const ownerBBox = owner.current.getClippedPathBoundingBox(pathType, getCurrentTransform(ctx)) || [0, 0, 0, 0];
  6663. const width = Math.ceil(ownerBBox[2] - ownerBBox[0]) || 1;
  6664. const height = Math.ceil(ownerBBox[3] - ownerBBox[1]) || 1;
  6665. const tmpCanvas = owner.cachedCanvases.getCanvas("pattern", width, height);
  6666. const tmpCtx = tmpCanvas.context;
  6667. tmpCtx.clearRect(0, 0, tmpCtx.canvas.width, tmpCtx.canvas.height);
  6668. tmpCtx.beginPath();
  6669. tmpCtx.rect(0, 0, tmpCtx.canvas.width, tmpCtx.canvas.height);
  6670. tmpCtx.translate(-ownerBBox[0], -ownerBBox[1]);
  6671. inverse = Util.transform(inverse, [1, 0, 0, 1, ownerBBox[0], ownerBBox[1]]);
  6672. tmpCtx.transform(...owner.baseTransform);
  6673. if (this.matrix) {
  6674. tmpCtx.transform(...this.matrix);
  6675. }
  6676. applyBoundingBox(tmpCtx, this._bbox);
  6677. tmpCtx.fillStyle = this._createGradient(tmpCtx);
  6678. tmpCtx.fill();
  6679. pattern = ctx.createPattern(tmpCanvas.canvas, "no-repeat");
  6680. const domMatrix = new DOMMatrix(inverse);
  6681. pattern.setTransform(domMatrix);
  6682. } else {
  6683. applyBoundingBox(ctx, this._bbox);
  6684. pattern = this._createGradient(ctx);
  6685. }
  6686. return pattern;
  6687. }
  6688. }
  6689. function drawTriangle(data, context, p1, p2, p3, c1, c2, c3) {
  6690. const coords = context.coords,
  6691. colors = context.colors;
  6692. const bytes = data.data,
  6693. rowSize = data.width * 4;
  6694. let tmp;
  6695. if (coords[p1 + 1] > coords[p2 + 1]) {
  6696. tmp = p1;
  6697. p1 = p2;
  6698. p2 = tmp;
  6699. tmp = c1;
  6700. c1 = c2;
  6701. c2 = tmp;
  6702. }
  6703. if (coords[p2 + 1] > coords[p3 + 1]) {
  6704. tmp = p2;
  6705. p2 = p3;
  6706. p3 = tmp;
  6707. tmp = c2;
  6708. c2 = c3;
  6709. c3 = tmp;
  6710. }
  6711. if (coords[p1 + 1] > coords[p2 + 1]) {
  6712. tmp = p1;
  6713. p1 = p2;
  6714. p2 = tmp;
  6715. tmp = c1;
  6716. c1 = c2;
  6717. c2 = tmp;
  6718. }
  6719. const x1 = (coords[p1] + context.offsetX) * context.scaleX;
  6720. const y1 = (coords[p1 + 1] + context.offsetY) * context.scaleY;
  6721. const x2 = (coords[p2] + context.offsetX) * context.scaleX;
  6722. const y2 = (coords[p2 + 1] + context.offsetY) * context.scaleY;
  6723. const x3 = (coords[p3] + context.offsetX) * context.scaleX;
  6724. const y3 = (coords[p3 + 1] + context.offsetY) * context.scaleY;
  6725. if (y1 >= y3) {
  6726. return;
  6727. }
  6728. const c1r = colors[c1],
  6729. c1g = colors[c1 + 1],
  6730. c1b = colors[c1 + 2];
  6731. const c2r = colors[c2],
  6732. c2g = colors[c2 + 1],
  6733. c2b = colors[c2 + 2];
  6734. const c3r = colors[c3],
  6735. c3g = colors[c3 + 1],
  6736. c3b = colors[c3 + 2];
  6737. const minY = Math.round(y1),
  6738. maxY = Math.round(y3);
  6739. let xa, car, cag, cab;
  6740. let xb, cbr, cbg, cbb;
  6741. for (let y = minY; y <= maxY; y++) {
  6742. if (y < y2) {
  6743. const k = y < y1 ? 0 : (y1 - y) / (y1 - y2);
  6744. xa = x1 - (x1 - x2) * k;
  6745. car = c1r - (c1r - c2r) * k;
  6746. cag = c1g - (c1g - c2g) * k;
  6747. cab = c1b - (c1b - c2b) * k;
  6748. } else {
  6749. let k;
  6750. if (y > y3) {
  6751. k = 1;
  6752. } else if (y2 === y3) {
  6753. k = 0;
  6754. } else {
  6755. k = (y2 - y) / (y2 - y3);
  6756. }
  6757. xa = x2 - (x2 - x3) * k;
  6758. car = c2r - (c2r - c3r) * k;
  6759. cag = c2g - (c2g - c3g) * k;
  6760. cab = c2b - (c2b - c3b) * k;
  6761. }
  6762. let k;
  6763. if (y < y1) {
  6764. k = 0;
  6765. } else if (y > y3) {
  6766. k = 1;
  6767. } else {
  6768. k = (y1 - y) / (y1 - y3);
  6769. }
  6770. xb = x1 - (x1 - x3) * k;
  6771. cbr = c1r - (c1r - c3r) * k;
  6772. cbg = c1g - (c1g - c3g) * k;
  6773. cbb = c1b - (c1b - c3b) * k;
  6774. const x1_ = Math.round(Math.min(xa, xb));
  6775. const x2_ = Math.round(Math.max(xa, xb));
  6776. let j = rowSize * y + x1_ * 4;
  6777. for (let x = x1_; x <= x2_; x++) {
  6778. k = (xa - x) / (xa - xb);
  6779. if (k < 0) {
  6780. k = 0;
  6781. } else if (k > 1) {
  6782. k = 1;
  6783. }
  6784. bytes[j++] = car - (car - cbr) * k | 0;
  6785. bytes[j++] = cag - (cag - cbg) * k | 0;
  6786. bytes[j++] = cab - (cab - cbb) * k | 0;
  6787. bytes[j++] = 255;
  6788. }
  6789. }
  6790. }
  6791. function drawFigure(data, figure, context) {
  6792. const ps = figure.coords;
  6793. const cs = figure.colors;
  6794. let i, ii;
  6795. switch (figure.type) {
  6796. case "lattice":
  6797. const verticesPerRow = figure.verticesPerRow;
  6798. const rows = Math.floor(ps.length / verticesPerRow) - 1;
  6799. const cols = verticesPerRow - 1;
  6800. for (i = 0; i < rows; i++) {
  6801. let q = i * verticesPerRow;
  6802. for (let j = 0; j < cols; j++, q++) {
  6803. drawTriangle(data, context, ps[q], ps[q + 1], ps[q + verticesPerRow], cs[q], cs[q + 1], cs[q + verticesPerRow]);
  6804. drawTriangle(data, context, ps[q + verticesPerRow + 1], ps[q + 1], ps[q + verticesPerRow], cs[q + verticesPerRow + 1], cs[q + 1], cs[q + verticesPerRow]);
  6805. }
  6806. }
  6807. break;
  6808. case "triangles":
  6809. for (i = 0, ii = ps.length; i < ii; i += 3) {
  6810. drawTriangle(data, context, ps[i], ps[i + 1], ps[i + 2], cs[i], cs[i + 1], cs[i + 2]);
  6811. }
  6812. break;
  6813. default:
  6814. throw new Error("illegal figure");
  6815. }
  6816. }
  6817. class MeshShadingPattern extends BaseShadingPattern {
  6818. constructor(IR) {
  6819. super();
  6820. this._coords = IR[2];
  6821. this._colors = IR[3];
  6822. this._figures = IR[4];
  6823. this._bounds = IR[5];
  6824. this._bbox = IR[6];
  6825. this._background = IR[7];
  6826. this.matrix = null;
  6827. }
  6828. _createMeshCanvas(combinedScale, backgroundColor, cachedCanvases) {
  6829. const EXPECTED_SCALE = 1.1;
  6830. const MAX_PATTERN_SIZE = 3000;
  6831. const BORDER_SIZE = 2;
  6832. const offsetX = Math.floor(this._bounds[0]);
  6833. const offsetY = Math.floor(this._bounds[1]);
  6834. const boundsWidth = Math.ceil(this._bounds[2]) - offsetX;
  6835. const boundsHeight = Math.ceil(this._bounds[3]) - offsetY;
  6836. const width = Math.min(Math.ceil(Math.abs(boundsWidth * combinedScale[0] * EXPECTED_SCALE)), MAX_PATTERN_SIZE);
  6837. const height = Math.min(Math.ceil(Math.abs(boundsHeight * combinedScale[1] * EXPECTED_SCALE)), MAX_PATTERN_SIZE);
  6838. const scaleX = boundsWidth / width;
  6839. const scaleY = boundsHeight / height;
  6840. const context = {
  6841. coords: this._coords,
  6842. colors: this._colors,
  6843. offsetX: -offsetX,
  6844. offsetY: -offsetY,
  6845. scaleX: 1 / scaleX,
  6846. scaleY: 1 / scaleY
  6847. };
  6848. const paddedWidth = width + BORDER_SIZE * 2;
  6849. const paddedHeight = height + BORDER_SIZE * 2;
  6850. const tmpCanvas = cachedCanvases.getCanvas("mesh", paddedWidth, paddedHeight);
  6851. const tmpCtx = tmpCanvas.context;
  6852. const data = tmpCtx.createImageData(width, height);
  6853. if (backgroundColor) {
  6854. const bytes = data.data;
  6855. for (let i = 0, ii = bytes.length; i < ii; i += 4) {
  6856. bytes[i] = backgroundColor[0];
  6857. bytes[i + 1] = backgroundColor[1];
  6858. bytes[i + 2] = backgroundColor[2];
  6859. bytes[i + 3] = 255;
  6860. }
  6861. }
  6862. for (const figure of this._figures) {
  6863. drawFigure(data, figure, context);
  6864. }
  6865. tmpCtx.putImageData(data, BORDER_SIZE, BORDER_SIZE);
  6866. const canvas = tmpCanvas.canvas;
  6867. return {
  6868. canvas,
  6869. offsetX: offsetX - BORDER_SIZE * scaleX,
  6870. offsetY: offsetY - BORDER_SIZE * scaleY,
  6871. scaleX,
  6872. scaleY
  6873. };
  6874. }
  6875. isModifyingCurrentTransform() {
  6876. return true;
  6877. }
  6878. getPattern(ctx, owner, inverse, pathType) {
  6879. applyBoundingBox(ctx, this._bbox);
  6880. const scale = new Float32Array(2);
  6881. if (pathType === PathType.SHADING) {
  6882. Util.singularValueDecompose2dScale(getCurrentTransform(ctx), scale);
  6883. } else if (this.matrix) {
  6884. Util.singularValueDecompose2dScale(this.matrix, scale);
  6885. const [matrixScaleX, matrixScaleY] = scale;
  6886. Util.singularValueDecompose2dScale(owner.baseTransform, scale);
  6887. scale[0] *= matrixScaleX;
  6888. scale[1] *= matrixScaleY;
  6889. } else {
  6890. Util.singularValueDecompose2dScale(owner.baseTransform, scale);
  6891. }
  6892. const temporaryPatternCanvas = this._createMeshCanvas(scale, pathType === PathType.SHADING ? null : this._background, owner.cachedCanvases);
  6893. if (pathType !== PathType.SHADING) {
  6894. ctx.setTransform(...owner.baseTransform);
  6895. if (this.matrix) {
  6896. ctx.transform(...this.matrix);
  6897. }
  6898. }
  6899. ctx.translate(temporaryPatternCanvas.offsetX, temporaryPatternCanvas.offsetY);
  6900. ctx.scale(temporaryPatternCanvas.scaleX, temporaryPatternCanvas.scaleY);
  6901. return ctx.createPattern(temporaryPatternCanvas.canvas, "no-repeat");
  6902. }
  6903. }
  6904. class DummyShadingPattern extends BaseShadingPattern {
  6905. getPattern() {
  6906. return "hotpink";
  6907. }
  6908. }
  6909. function getShadingPattern(IR) {
  6910. switch (IR[0]) {
  6911. case "RadialAxial":
  6912. return new RadialAxialShadingPattern(IR);
  6913. case "Mesh":
  6914. return new MeshShadingPattern(IR);
  6915. case "Dummy":
  6916. return new DummyShadingPattern();
  6917. }
  6918. throw new Error(`Unknown IR type: ${IR[0]}`);
  6919. }
  6920. const PaintType = {
  6921. COLORED: 1,
  6922. UNCOLORED: 2
  6923. };
  6924. class TilingPattern {
  6925. static MAX_PATTERN_SIZE = 3000;
  6926. constructor(IR, ctx, canvasGraphicsFactory, baseTransform) {
  6927. this.color = IR[1];
  6928. this.operatorList = IR[2];
  6929. this.matrix = IR[3];
  6930. this.bbox = IR[4];
  6931. this.xstep = IR[5];
  6932. this.ystep = IR[6];
  6933. this.paintType = IR[7];
  6934. this.tilingType = IR[8];
  6935. this.ctx = ctx;
  6936. this.canvasGraphicsFactory = canvasGraphicsFactory;
  6937. this.baseTransform = baseTransform;
  6938. }
  6939. createPatternCanvas(owner) {
  6940. const {
  6941. bbox,
  6942. operatorList,
  6943. paintType,
  6944. tilingType,
  6945. color,
  6946. canvasGraphicsFactory
  6947. } = this;
  6948. let {
  6949. xstep,
  6950. ystep
  6951. } = this;
  6952. xstep = Math.abs(xstep);
  6953. ystep = Math.abs(ystep);
  6954. info("TilingType: " + tilingType);
  6955. const x0 = bbox[0],
  6956. y0 = bbox[1],
  6957. x1 = bbox[2],
  6958. y1 = bbox[3];
  6959. const width = x1 - x0;
  6960. const height = y1 - y0;
  6961. const scale = new Float32Array(2);
  6962. Util.singularValueDecompose2dScale(this.matrix, scale);
  6963. const [matrixScaleX, matrixScaleY] = scale;
  6964. Util.singularValueDecompose2dScale(this.baseTransform, scale);
  6965. const combinedScaleX = matrixScaleX * scale[0];
  6966. const combinedScaleY = matrixScaleY * scale[1];
  6967. let canvasWidth = width,
  6968. canvasHeight = height,
  6969. redrawHorizontally = false,
  6970. redrawVertically = false;
  6971. const xScaledStep = Math.ceil(xstep * combinedScaleX);
  6972. const yScaledStep = Math.ceil(ystep * combinedScaleY);
  6973. const xScaledWidth = Math.ceil(width * combinedScaleX);
  6974. const yScaledHeight = Math.ceil(height * combinedScaleY);
  6975. if (xScaledStep >= xScaledWidth) {
  6976. canvasWidth = xstep;
  6977. } else {
  6978. redrawHorizontally = true;
  6979. }
  6980. if (yScaledStep >= yScaledHeight) {
  6981. canvasHeight = ystep;
  6982. } else {
  6983. redrawVertically = true;
  6984. }
  6985. const dimx = this.getSizeAndScale(canvasWidth, this.ctx.canvas.width, combinedScaleX);
  6986. const dimy = this.getSizeAndScale(canvasHeight, this.ctx.canvas.height, combinedScaleY);
  6987. const tmpCanvas = owner.cachedCanvases.getCanvas("pattern", dimx.size, dimy.size);
  6988. const tmpCtx = tmpCanvas.context;
  6989. const graphics = canvasGraphicsFactory.createCanvasGraphics(tmpCtx);
  6990. graphics.groupLevel = owner.groupLevel;
  6991. this.setFillAndStrokeStyleToContext(graphics, paintType, color);
  6992. tmpCtx.translate(-dimx.scale * x0, -dimy.scale * y0);
  6993. graphics.transform(dimx.scale, 0, 0, dimy.scale, 0, 0);
  6994. tmpCtx.save();
  6995. this.clipBbox(graphics, x0, y0, x1, y1);
  6996. graphics.baseTransform = getCurrentTransform(graphics.ctx);
  6997. graphics.executeOperatorList(operatorList);
  6998. graphics.endDrawing();
  6999. tmpCtx.restore();
  7000. if (redrawHorizontally || redrawVertically) {
  7001. const image = tmpCanvas.canvas;
  7002. if (redrawHorizontally) {
  7003. canvasWidth = xstep;
  7004. }
  7005. if (redrawVertically) {
  7006. canvasHeight = ystep;
  7007. }
  7008. const dimx2 = this.getSizeAndScale(canvasWidth, this.ctx.canvas.width, combinedScaleX);
  7009. const dimy2 = this.getSizeAndScale(canvasHeight, this.ctx.canvas.height, combinedScaleY);
  7010. const xSize = dimx2.size;
  7011. const ySize = dimy2.size;
  7012. const tmpCanvas2 = owner.cachedCanvases.getCanvas("pattern-workaround", xSize, ySize);
  7013. const tmpCtx2 = tmpCanvas2.context;
  7014. const ii = redrawHorizontally ? Math.floor(width / xstep) : 0;
  7015. const jj = redrawVertically ? Math.floor(height / ystep) : 0;
  7016. for (let i = 0; i <= ii; i++) {
  7017. for (let j = 0; j <= jj; j++) {
  7018. tmpCtx2.drawImage(image, xSize * i, ySize * j, xSize, ySize, 0, 0, xSize, ySize);
  7019. }
  7020. }
  7021. return {
  7022. canvas: tmpCanvas2.canvas,
  7023. scaleX: dimx2.scale,
  7024. scaleY: dimy2.scale,
  7025. offsetX: x0,
  7026. offsetY: y0
  7027. };
  7028. }
  7029. return {
  7030. canvas: tmpCanvas.canvas,
  7031. scaleX: dimx.scale,
  7032. scaleY: dimy.scale,
  7033. offsetX: x0,
  7034. offsetY: y0
  7035. };
  7036. }
  7037. getSizeAndScale(step, realOutputSize, scale) {
  7038. const maxSize = Math.max(TilingPattern.MAX_PATTERN_SIZE, realOutputSize);
  7039. let size = Math.ceil(step * scale);
  7040. if (size >= maxSize) {
  7041. size = maxSize;
  7042. } else {
  7043. scale = size / step;
  7044. }
  7045. return {
  7046. scale,
  7047. size
  7048. };
  7049. }
  7050. clipBbox(graphics, x0, y0, x1, y1) {
  7051. const bboxWidth = x1 - x0;
  7052. const bboxHeight = y1 - y0;
  7053. graphics.ctx.rect(x0, y0, bboxWidth, bboxHeight);
  7054. Util.axialAlignedBoundingBox([x0, y0, x1, y1], getCurrentTransform(graphics.ctx), graphics.current.minMax);
  7055. graphics.clip();
  7056. graphics.endPath();
  7057. }
  7058. setFillAndStrokeStyleToContext(graphics, paintType, color) {
  7059. const context = graphics.ctx,
  7060. current = graphics.current;
  7061. switch (paintType) {
  7062. case PaintType.COLORED:
  7063. const ctx = this.ctx;
  7064. context.fillStyle = ctx.fillStyle;
  7065. context.strokeStyle = ctx.strokeStyle;
  7066. current.fillColor = ctx.fillStyle;
  7067. current.strokeColor = ctx.strokeStyle;
  7068. break;
  7069. case PaintType.UNCOLORED:
  7070. const cssColor = Util.makeHexColor(color[0], color[1], color[2]);
  7071. context.fillStyle = cssColor;
  7072. context.strokeStyle = cssColor;
  7073. current.fillColor = cssColor;
  7074. current.strokeColor = cssColor;
  7075. break;
  7076. default:
  7077. throw new FormatError(`Unsupported paint type: ${paintType}`);
  7078. }
  7079. }
  7080. isModifyingCurrentTransform() {
  7081. return false;
  7082. }
  7083. getPattern(ctx, owner, inverse, pathType) {
  7084. let matrix = inverse;
  7085. if (pathType !== PathType.SHADING) {
  7086. matrix = Util.transform(matrix, owner.baseTransform);
  7087. if (this.matrix) {
  7088. matrix = Util.transform(matrix, this.matrix);
  7089. }
  7090. }
  7091. const temporaryPatternCanvas = this.createPatternCanvas(owner);
  7092. let domMatrix = new DOMMatrix(matrix);
  7093. domMatrix = domMatrix.translate(temporaryPatternCanvas.offsetX, temporaryPatternCanvas.offsetY);
  7094. domMatrix = domMatrix.scale(1 / temporaryPatternCanvas.scaleX, 1 / temporaryPatternCanvas.scaleY);
  7095. const pattern = ctx.createPattern(temporaryPatternCanvas.canvas, "repeat");
  7096. pattern.setTransform(domMatrix);
  7097. return pattern;
  7098. }
  7099. }
  7100. ;// ./src/shared/image_utils.js
  7101. function convertToRGBA(params) {
  7102. switch (params.kind) {
  7103. case ImageKind.GRAYSCALE_1BPP:
  7104. return convertBlackAndWhiteToRGBA(params);
  7105. case ImageKind.RGB_24BPP:
  7106. return convertRGBToRGBA(params);
  7107. }
  7108. return null;
  7109. }
  7110. function convertBlackAndWhiteToRGBA({
  7111. src,
  7112. srcPos = 0,
  7113. dest,
  7114. width,
  7115. height,
  7116. nonBlackColor = 0xffffffff,
  7117. inverseDecode = false
  7118. }) {
  7119. const black = util_FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
  7120. const [zeroMapping, oneMapping] = inverseDecode ? [nonBlackColor, black] : [black, nonBlackColor];
  7121. const widthInSource = width >> 3;
  7122. const widthRemainder = width & 7;
  7123. const srcLength = src.length;
  7124. dest = new Uint32Array(dest.buffer);
  7125. let destPos = 0;
  7126. for (let i = 0; i < height; i++) {
  7127. for (const max = srcPos + widthInSource; srcPos < max; srcPos++) {
  7128. const elem = srcPos < srcLength ? src[srcPos] : 255;
  7129. dest[destPos++] = elem & 0b10000000 ? oneMapping : zeroMapping;
  7130. dest[destPos++] = elem & 0b1000000 ? oneMapping : zeroMapping;
  7131. dest[destPos++] = elem & 0b100000 ? oneMapping : zeroMapping;
  7132. dest[destPos++] = elem & 0b10000 ? oneMapping : zeroMapping;
  7133. dest[destPos++] = elem & 0b1000 ? oneMapping : zeroMapping;
  7134. dest[destPos++] = elem & 0b100 ? oneMapping : zeroMapping;
  7135. dest[destPos++] = elem & 0b10 ? oneMapping : zeroMapping;
  7136. dest[destPos++] = elem & 0b1 ? oneMapping : zeroMapping;
  7137. }
  7138. if (widthRemainder === 0) {
  7139. continue;
  7140. }
  7141. const elem = srcPos < srcLength ? src[srcPos++] : 255;
  7142. for (let j = 0; j < widthRemainder; j++) {
  7143. dest[destPos++] = elem & 1 << 7 - j ? oneMapping : zeroMapping;
  7144. }
  7145. }
  7146. return {
  7147. srcPos,
  7148. destPos
  7149. };
  7150. }
  7151. function convertRGBToRGBA({
  7152. src,
  7153. srcPos = 0,
  7154. dest,
  7155. destPos = 0,
  7156. width,
  7157. height
  7158. }) {
  7159. let i = 0;
  7160. const len = width * height * 3;
  7161. const len32 = len >> 2;
  7162. const src32 = new Uint32Array(src.buffer, srcPos, len32);
  7163. if (FeatureTest.isLittleEndian) {
  7164. for (; i < len32 - 2; i += 3, destPos += 4) {
  7165. const s1 = src32[i];
  7166. const s2 = src32[i + 1];
  7167. const s3 = src32[i + 2];
  7168. dest[destPos] = s1 | 0xff000000;
  7169. dest[destPos + 1] = s1 >>> 24 | s2 << 8 | 0xff000000;
  7170. dest[destPos + 2] = s2 >>> 16 | s3 << 16 | 0xff000000;
  7171. dest[destPos + 3] = s3 >>> 8 | 0xff000000;
  7172. }
  7173. for (let j = i * 4, jj = srcPos + len; j < jj; j += 3) {
  7174. dest[destPos++] = src[j] | src[j + 1] << 8 | src[j + 2] << 16 | 0xff000000;
  7175. }
  7176. } else {
  7177. for (; i < len32 - 2; i += 3, destPos += 4) {
  7178. const s1 = src32[i];
  7179. const s2 = src32[i + 1];
  7180. const s3 = src32[i + 2];
  7181. dest[destPos] = s1 | 0xff;
  7182. dest[destPos + 1] = s1 << 24 | s2 >>> 8 | 0xff;
  7183. dest[destPos + 2] = s2 << 16 | s3 >>> 16 | 0xff;
  7184. dest[destPos + 3] = s3 << 8 | 0xff;
  7185. }
  7186. for (let j = i * 4, jj = srcPos + len; j < jj; j += 3) {
  7187. dest[destPos++] = src[j] << 24 | src[j + 1] << 16 | src[j + 2] << 8 | 0xff;
  7188. }
  7189. }
  7190. return {
  7191. srcPos: srcPos + len,
  7192. destPos
  7193. };
  7194. }
  7195. function grayToRGBA(src, dest) {
  7196. if (FeatureTest.isLittleEndian) {
  7197. for (let i = 0, ii = src.length; i < ii; i++) {
  7198. dest[i] = src[i] * 0x10101 | 0xff000000;
  7199. }
  7200. } else {
  7201. for (let i = 0, ii = src.length; i < ii; i++) {
  7202. dest[i] = src[i] * 0x1010100 | 0x000000ff;
  7203. }
  7204. }
  7205. }
  7206. ;// ./src/display/canvas.js
  7207. const MIN_FONT_SIZE = 16;
  7208. const MAX_FONT_SIZE = 100;
  7209. const EXECUTION_TIME = 15;
  7210. const EXECUTION_STEPS = 10;
  7211. const FULL_CHUNK_HEIGHT = 16;
  7212. const SCALE_MATRIX = new DOMMatrix();
  7213. const XY = new Float32Array(2);
  7214. const MIN_MAX_INIT = new Float32Array([Infinity, Infinity, -Infinity, -Infinity]);
  7215. function mirrorContextOperations(ctx, destCtx) {
  7216. if (ctx._removeMirroring) {
  7217. throw new Error("Context is already forwarding operations.");
  7218. }
  7219. ctx.__originalSave = ctx.save;
  7220. ctx.__originalRestore = ctx.restore;
  7221. ctx.__originalRotate = ctx.rotate;
  7222. ctx.__originalScale = ctx.scale;
  7223. ctx.__originalTranslate = ctx.translate;
  7224. ctx.__originalTransform = ctx.transform;
  7225. ctx.__originalSetTransform = ctx.setTransform;
  7226. ctx.__originalResetTransform = ctx.resetTransform;
  7227. ctx.__originalClip = ctx.clip;
  7228. ctx.__originalMoveTo = ctx.moveTo;
  7229. ctx.__originalLineTo = ctx.lineTo;
  7230. ctx.__originalBezierCurveTo = ctx.bezierCurveTo;
  7231. ctx.__originalRect = ctx.rect;
  7232. ctx.__originalClosePath = ctx.closePath;
  7233. ctx.__originalBeginPath = ctx.beginPath;
  7234. ctx._removeMirroring = () => {
  7235. ctx.save = ctx.__originalSave;
  7236. ctx.restore = ctx.__originalRestore;
  7237. ctx.rotate = ctx.__originalRotate;
  7238. ctx.scale = ctx.__originalScale;
  7239. ctx.translate = ctx.__originalTranslate;
  7240. ctx.transform = ctx.__originalTransform;
  7241. ctx.setTransform = ctx.__originalSetTransform;
  7242. ctx.resetTransform = ctx.__originalResetTransform;
  7243. ctx.clip = ctx.__originalClip;
  7244. ctx.moveTo = ctx.__originalMoveTo;
  7245. ctx.lineTo = ctx.__originalLineTo;
  7246. ctx.bezierCurveTo = ctx.__originalBezierCurveTo;
  7247. ctx.rect = ctx.__originalRect;
  7248. ctx.closePath = ctx.__originalClosePath;
  7249. ctx.beginPath = ctx.__originalBeginPath;
  7250. delete ctx._removeMirroring;
  7251. };
  7252. ctx.save = function () {
  7253. destCtx.save();
  7254. this.__originalSave();
  7255. };
  7256. ctx.restore = function () {
  7257. destCtx.restore();
  7258. this.__originalRestore();
  7259. };
  7260. ctx.translate = function (x, y) {
  7261. destCtx.translate(x, y);
  7262. this.__originalTranslate(x, y);
  7263. };
  7264. ctx.scale = function (x, y) {
  7265. destCtx.scale(x, y);
  7266. this.__originalScale(x, y);
  7267. };
  7268. ctx.transform = function (a, b, c, d, e, f) {
  7269. destCtx.transform(a, b, c, d, e, f);
  7270. this.__originalTransform(a, b, c, d, e, f);
  7271. };
  7272. ctx.setTransform = function (a, b, c, d, e, f) {
  7273. destCtx.setTransform(a, b, c, d, e, f);
  7274. this.__originalSetTransform(a, b, c, d, e, f);
  7275. };
  7276. ctx.resetTransform = function () {
  7277. destCtx.resetTransform();
  7278. this.__originalResetTransform();
  7279. };
  7280. ctx.rotate = function (angle) {
  7281. destCtx.rotate(angle);
  7282. this.__originalRotate(angle);
  7283. };
  7284. ctx.clip = function (rule) {
  7285. destCtx.clip(rule);
  7286. this.__originalClip(rule);
  7287. };
  7288. ctx.moveTo = function (x, y) {
  7289. destCtx.moveTo(x, y);
  7290. this.__originalMoveTo(x, y);
  7291. };
  7292. ctx.lineTo = function (x, y) {
  7293. destCtx.lineTo(x, y);
  7294. this.__originalLineTo(x, y);
  7295. };
  7296. ctx.bezierCurveTo = function (cp1x, cp1y, cp2x, cp2y, x, y) {
  7297. destCtx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  7298. this.__originalBezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  7299. };
  7300. ctx.rect = function (x, y, width, height) {
  7301. destCtx.rect(x, y, width, height);
  7302. this.__originalRect(x, y, width, height);
  7303. };
  7304. ctx.closePath = function () {
  7305. destCtx.closePath();
  7306. this.__originalClosePath();
  7307. };
  7308. ctx.beginPath = function () {
  7309. destCtx.beginPath();
  7310. this.__originalBeginPath();
  7311. };
  7312. }
  7313. class CachedCanvases {
  7314. constructor(canvasFactory) {
  7315. this.canvasFactory = canvasFactory;
  7316. this.cache = Object.create(null);
  7317. }
  7318. getCanvas(id, width, height) {
  7319. let canvasEntry;
  7320. if (this.cache[id] !== undefined) {
  7321. canvasEntry = this.cache[id];
  7322. this.canvasFactory.reset(canvasEntry, width, height);
  7323. } else {
  7324. canvasEntry = this.canvasFactory.create(width, height);
  7325. this.cache[id] = canvasEntry;
  7326. }
  7327. return canvasEntry;
  7328. }
  7329. delete(id) {
  7330. delete this.cache[id];
  7331. }
  7332. clear() {
  7333. for (const id in this.cache) {
  7334. const canvasEntry = this.cache[id];
  7335. this.canvasFactory.destroy(canvasEntry);
  7336. delete this.cache[id];
  7337. }
  7338. }
  7339. }
  7340. function drawImageAtIntegerCoords(ctx, srcImg, srcX, srcY, srcW, srcH, destX, destY, destW, destH) {
  7341. const [a, b, c, d, tx, ty] = getCurrentTransform(ctx);
  7342. if (b === 0 && c === 0) {
  7343. const tlX = destX * a + tx;
  7344. const rTlX = Math.round(tlX);
  7345. const tlY = destY * d + ty;
  7346. const rTlY = Math.round(tlY);
  7347. const brX = (destX + destW) * a + tx;
  7348. const rWidth = Math.abs(Math.round(brX) - rTlX) || 1;
  7349. const brY = (destY + destH) * d + ty;
  7350. const rHeight = Math.abs(Math.round(brY) - rTlY) || 1;
  7351. ctx.setTransform(Math.sign(a), 0, 0, Math.sign(d), rTlX, rTlY);
  7352. ctx.drawImage(srcImg, srcX, srcY, srcW, srcH, 0, 0, rWidth, rHeight);
  7353. ctx.setTransform(a, b, c, d, tx, ty);
  7354. return [rWidth, rHeight];
  7355. }
  7356. if (a === 0 && d === 0) {
  7357. const tlX = destY * c + tx;
  7358. const rTlX = Math.round(tlX);
  7359. const tlY = destX * b + ty;
  7360. const rTlY = Math.round(tlY);
  7361. const brX = (destY + destH) * c + tx;
  7362. const rWidth = Math.abs(Math.round(brX) - rTlX) || 1;
  7363. const brY = (destX + destW) * b + ty;
  7364. const rHeight = Math.abs(Math.round(brY) - rTlY) || 1;
  7365. ctx.setTransform(0, Math.sign(b), Math.sign(c), 0, rTlX, rTlY);
  7366. ctx.drawImage(srcImg, srcX, srcY, srcW, srcH, 0, 0, rHeight, rWidth);
  7367. ctx.setTransform(a, b, c, d, tx, ty);
  7368. return [rHeight, rWidth];
  7369. }
  7370. ctx.drawImage(srcImg, srcX, srcY, srcW, srcH, destX, destY, destW, destH);
  7371. const scaleX = Math.hypot(a, b);
  7372. const scaleY = Math.hypot(c, d);
  7373. return [scaleX * destW, scaleY * destH];
  7374. }
  7375. class CanvasExtraState {
  7376. alphaIsShape = false;
  7377. fontSize = 0;
  7378. fontSizeScale = 1;
  7379. textMatrix = null;
  7380. textMatrixScale = 1;
  7381. fontMatrix = FONT_IDENTITY_MATRIX;
  7382. leading = 0;
  7383. x = 0;
  7384. y = 0;
  7385. lineX = 0;
  7386. lineY = 0;
  7387. charSpacing = 0;
  7388. wordSpacing = 0;
  7389. textHScale = 1;
  7390. textRenderingMode = TextRenderingMode.FILL;
  7391. textRise = 0;
  7392. fillColor = "#000000";
  7393. strokeColor = "#000000";
  7394. patternFill = false;
  7395. patternStroke = false;
  7396. fillAlpha = 1;
  7397. strokeAlpha = 1;
  7398. lineWidth = 1;
  7399. activeSMask = null;
  7400. transferMaps = "none";
  7401. constructor(width, height) {
  7402. this.clipBox = new Float32Array([0, 0, width, height]);
  7403. this.minMax = MIN_MAX_INIT.slice();
  7404. }
  7405. clone() {
  7406. const clone = Object.create(this);
  7407. clone.clipBox = this.clipBox.slice();
  7408. clone.minMax = this.minMax.slice();
  7409. return clone;
  7410. }
  7411. getPathBoundingBox(pathType = PathType.FILL, transform = null) {
  7412. const box = this.minMax.slice();
  7413. if (pathType === PathType.STROKE) {
  7414. if (!transform) {
  7415. unreachable("Stroke bounding box must include transform.");
  7416. }
  7417. Util.singularValueDecompose2dScale(transform, XY);
  7418. const xStrokePad = XY[0] * this.lineWidth / 2;
  7419. const yStrokePad = XY[1] * this.lineWidth / 2;
  7420. box[0] -= xStrokePad;
  7421. box[1] -= yStrokePad;
  7422. box[2] += xStrokePad;
  7423. box[3] += yStrokePad;
  7424. }
  7425. return box;
  7426. }
  7427. updateClipFromPath() {
  7428. const intersect = Util.intersect(this.clipBox, this.getPathBoundingBox());
  7429. this.startNewPathAndClipBox(intersect || [0, 0, 0, 0]);
  7430. }
  7431. isEmptyClip() {
  7432. return this.minMax[0] === Infinity;
  7433. }
  7434. startNewPathAndClipBox(box) {
  7435. this.clipBox.set(box, 0);
  7436. this.minMax.set(MIN_MAX_INIT, 0);
  7437. }
  7438. getClippedPathBoundingBox(pathType = PathType.FILL, transform = null) {
  7439. return Util.intersect(this.clipBox, this.getPathBoundingBox(pathType, transform));
  7440. }
  7441. }
  7442. function putBinaryImageData(ctx, imgData) {
  7443. if (imgData instanceof ImageData) {
  7444. ctx.putImageData(imgData, 0, 0);
  7445. return;
  7446. }
  7447. const height = imgData.height,
  7448. width = imgData.width;
  7449. const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  7450. const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  7451. const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  7452. const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  7453. let srcPos = 0,
  7454. destPos;
  7455. const src = imgData.data;
  7456. const dest = chunkImgData.data;
  7457. let i, j, thisChunkHeight, elemsInThisChunk;
  7458. if (imgData.kind === util_ImageKind.GRAYSCALE_1BPP) {
  7459. const srcLength = src.byteLength;
  7460. const dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2);
  7461. const dest32DataLength = dest32.length;
  7462. const fullSrcDiff = width + 7 >> 3;
  7463. const white = 0xffffffff;
  7464. const black = util_FeatureTest.isLittleEndian ? 0xff000000 : 0x000000ff;
  7465. for (i = 0; i < totalChunks; i++) {
  7466. thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  7467. destPos = 0;
  7468. for (j = 0; j < thisChunkHeight; j++) {
  7469. const srcDiff = srcLength - srcPos;
  7470. let k = 0;
  7471. const kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7;
  7472. const kEndUnrolled = kEnd & ~7;
  7473. let mask = 0;
  7474. let srcByte = 0;
  7475. for (; k < kEndUnrolled; k += 8) {
  7476. srcByte = src[srcPos++];
  7477. dest32[destPos++] = srcByte & 128 ? white : black;
  7478. dest32[destPos++] = srcByte & 64 ? white : black;
  7479. dest32[destPos++] = srcByte & 32 ? white : black;
  7480. dest32[destPos++] = srcByte & 16 ? white : black;
  7481. dest32[destPos++] = srcByte & 8 ? white : black;
  7482. dest32[destPos++] = srcByte & 4 ? white : black;
  7483. dest32[destPos++] = srcByte & 2 ? white : black;
  7484. dest32[destPos++] = srcByte & 1 ? white : black;
  7485. }
  7486. for (; k < kEnd; k++) {
  7487. if (mask === 0) {
  7488. srcByte = src[srcPos++];
  7489. mask = 128;
  7490. }
  7491. dest32[destPos++] = srcByte & mask ? white : black;
  7492. mask >>= 1;
  7493. }
  7494. }
  7495. while (destPos < dest32DataLength) {
  7496. dest32[destPos++] = 0;
  7497. }
  7498. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  7499. }
  7500. } else if (imgData.kind === util_ImageKind.RGBA_32BPP) {
  7501. j = 0;
  7502. elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
  7503. for (i = 0; i < fullChunks; i++) {
  7504. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  7505. srcPos += elemsInThisChunk;
  7506. ctx.putImageData(chunkImgData, 0, j);
  7507. j += FULL_CHUNK_HEIGHT;
  7508. }
  7509. if (i < totalChunks) {
  7510. elemsInThisChunk = width * partialChunkHeight * 4;
  7511. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  7512. ctx.putImageData(chunkImgData, 0, j);
  7513. }
  7514. } else if (imgData.kind === util_ImageKind.RGB_24BPP) {
  7515. thisChunkHeight = FULL_CHUNK_HEIGHT;
  7516. elemsInThisChunk = width * thisChunkHeight;
  7517. for (i = 0; i < totalChunks; i++) {
  7518. if (i >= fullChunks) {
  7519. thisChunkHeight = partialChunkHeight;
  7520. elemsInThisChunk = width * thisChunkHeight;
  7521. }
  7522. destPos = 0;
  7523. for (j = elemsInThisChunk; j--;) {
  7524. dest[destPos++] = src[srcPos++];
  7525. dest[destPos++] = src[srcPos++];
  7526. dest[destPos++] = src[srcPos++];
  7527. dest[destPos++] = 255;
  7528. }
  7529. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  7530. }
  7531. } else {
  7532. throw new Error(`bad image kind: ${imgData.kind}`);
  7533. }
  7534. }
  7535. function putBinaryImageMask(ctx, imgData) {
  7536. if (imgData.bitmap) {
  7537. ctx.drawImage(imgData.bitmap, 0, 0);
  7538. return;
  7539. }
  7540. const height = imgData.height,
  7541. width = imgData.width;
  7542. const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  7543. const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  7544. const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  7545. const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  7546. let srcPos = 0;
  7547. const src = imgData.data;
  7548. const dest = chunkImgData.data;
  7549. for (let i = 0; i < totalChunks; i++) {
  7550. const thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  7551. ({
  7552. srcPos
  7553. } = convertBlackAndWhiteToRGBA({
  7554. src,
  7555. srcPos,
  7556. dest,
  7557. width,
  7558. height: thisChunkHeight,
  7559. nonBlackColor: 0
  7560. }));
  7561. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  7562. }
  7563. }
  7564. function copyCtxState(sourceCtx, destCtx) {
  7565. const properties = ["strokeStyle", "fillStyle", "fillRule", "globalAlpha", "lineWidth", "lineCap", "lineJoin", "miterLimit", "globalCompositeOperation", "font", "filter"];
  7566. for (const property of properties) {
  7567. if (sourceCtx[property] !== undefined) {
  7568. destCtx[property] = sourceCtx[property];
  7569. }
  7570. }
  7571. if (sourceCtx.setLineDash !== undefined) {
  7572. destCtx.setLineDash(sourceCtx.getLineDash());
  7573. destCtx.lineDashOffset = sourceCtx.lineDashOffset;
  7574. }
  7575. }
  7576. function resetCtxToDefault(ctx) {
  7577. ctx.strokeStyle = ctx.fillStyle = "#000000";
  7578. ctx.fillRule = "nonzero";
  7579. ctx.globalAlpha = 1;
  7580. ctx.lineWidth = 1;
  7581. ctx.lineCap = "butt";
  7582. ctx.lineJoin = "miter";
  7583. ctx.miterLimit = 10;
  7584. ctx.globalCompositeOperation = "source-over";
  7585. ctx.font = "10px sans-serif";
  7586. if (ctx.setLineDash !== undefined) {
  7587. ctx.setLineDash([]);
  7588. ctx.lineDashOffset = 0;
  7589. }
  7590. const {
  7591. filter
  7592. } = ctx;
  7593. if (filter !== "none" && filter !== "") {
  7594. ctx.filter = "none";
  7595. }
  7596. }
  7597. function getImageSmoothingEnabled(transform, interpolate) {
  7598. if (interpolate) {
  7599. return true;
  7600. }
  7601. Util.singularValueDecompose2dScale(transform, XY);
  7602. const actualScale = Math.fround(OutputScale.pixelRatio * PixelsPerInch.PDF_TO_CSS_UNITS);
  7603. return XY[0] <= actualScale && XY[1] <= actualScale;
  7604. }
  7605. const LINE_CAP_STYLES = ["butt", "round", "square"];
  7606. const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
  7607. const NORMAL_CLIP = {};
  7608. const EO_CLIP = {};
  7609. class CanvasGraphics {
  7610. constructor(canvasCtx, commonObjs, objs, canvasFactory, filterFactory, {
  7611. optionalContentConfig,
  7612. markedContentStack = null
  7613. }, annotationCanvasMap, pageColors) {
  7614. this.ctx = canvasCtx;
  7615. this.current = new CanvasExtraState(this.ctx.canvas.width, this.ctx.canvas.height);
  7616. this.stateStack = [];
  7617. this.pendingClip = null;
  7618. this.pendingEOFill = false;
  7619. this.res = null;
  7620. this.xobjs = null;
  7621. this.commonObjs = commonObjs;
  7622. this.objs = objs;
  7623. this.canvasFactory = canvasFactory;
  7624. this.filterFactory = filterFactory;
  7625. this.groupStack = [];
  7626. this.baseTransform = null;
  7627. this.baseTransformStack = [];
  7628. this.groupLevel = 0;
  7629. this.smaskStack = [];
  7630. this.smaskCounter = 0;
  7631. this.tempSMask = null;
  7632. this.suspendedCtx = null;
  7633. this.contentVisible = true;
  7634. this.markedContentStack = markedContentStack || [];
  7635. this.optionalContentConfig = optionalContentConfig;
  7636. this.cachedCanvases = new CachedCanvases(this.canvasFactory);
  7637. this.cachedPatterns = new Map();
  7638. this.annotationCanvasMap = annotationCanvasMap;
  7639. this.viewportScale = 1;
  7640. this.outputScaleX = 1;
  7641. this.outputScaleY = 1;
  7642. this.pageColors = pageColors;
  7643. this._cachedScaleForStroking = [-1, 0];
  7644. this._cachedGetSinglePixelWidth = null;
  7645. this._cachedBitmapsMap = new Map();
  7646. }
  7647. getObject(data, fallback = null) {
  7648. if (typeof data === "string") {
  7649. return data.startsWith("g_") ? this.commonObjs.get(data) : this.objs.get(data);
  7650. }
  7651. return fallback;
  7652. }
  7653. beginDrawing({
  7654. transform,
  7655. viewport,
  7656. transparency = false,
  7657. background = null
  7658. }) {
  7659. const width = this.ctx.canvas.width;
  7660. const height = this.ctx.canvas.height;
  7661. const savedFillStyle = this.ctx.fillStyle;
  7662. this.ctx.fillStyle = background || "#ffffff";
  7663. this.ctx.fillRect(0, 0, width, height);
  7664. this.ctx.fillStyle = savedFillStyle;
  7665. if (transparency) {
  7666. const transparentCanvas = this.cachedCanvases.getCanvas("transparent", width, height);
  7667. this.compositeCtx = this.ctx;
  7668. this.transparentCanvas = transparentCanvas.canvas;
  7669. this.ctx = transparentCanvas.context;
  7670. this.ctx.save();
  7671. this.ctx.transform(...getCurrentTransform(this.compositeCtx));
  7672. }
  7673. this.ctx.save();
  7674. resetCtxToDefault(this.ctx);
  7675. if (transform) {
  7676. this.ctx.transform(...transform);
  7677. this.outputScaleX = transform[0];
  7678. this.outputScaleY = transform[0];
  7679. }
  7680. this.ctx.transform(...viewport.transform);
  7681. this.viewportScale = viewport.scale;
  7682. this.baseTransform = getCurrentTransform(this.ctx);
  7683. }
  7684. executeOperatorList(operatorList, executionStartIdx, continueCallback, stepper) {
  7685. const argsArray = operatorList.argsArray;
  7686. const fnArray = operatorList.fnArray;
  7687. let i = executionStartIdx || 0;
  7688. const argsArrayLen = argsArray.length;
  7689. if (argsArrayLen === i) {
  7690. return i;
  7691. }
  7692. const chunkOperations = argsArrayLen - i > EXECUTION_STEPS && typeof continueCallback === "function";
  7693. const endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
  7694. let steps = 0;
  7695. const commonObjs = this.commonObjs;
  7696. const objs = this.objs;
  7697. let fnId;
  7698. while (true) {
  7699. if (stepper !== undefined && i === stepper.nextBreakPoint) {
  7700. stepper.breakIt(i, continueCallback);
  7701. return i;
  7702. }
  7703. fnId = fnArray[i];
  7704. if (fnId !== OPS.dependency) {
  7705. this[fnId].apply(this, argsArray[i]);
  7706. } else {
  7707. for (const depObjId of argsArray[i]) {
  7708. const objsPool = depObjId.startsWith("g_") ? commonObjs : objs;
  7709. if (!objsPool.has(depObjId)) {
  7710. objsPool.get(depObjId, continueCallback);
  7711. return i;
  7712. }
  7713. }
  7714. }
  7715. i++;
  7716. if (i === argsArrayLen) {
  7717. return i;
  7718. }
  7719. if (chunkOperations && ++steps > EXECUTION_STEPS) {
  7720. if (Date.now() > endTime) {
  7721. continueCallback();
  7722. return i;
  7723. }
  7724. steps = 0;
  7725. }
  7726. }
  7727. }
  7728. #restoreInitialState() {
  7729. while (this.stateStack.length || this.inSMaskMode) {
  7730. this.restore();
  7731. }
  7732. this.current.activeSMask = null;
  7733. this.ctx.restore();
  7734. if (this.transparentCanvas) {
  7735. this.ctx = this.compositeCtx;
  7736. this.ctx.save();
  7737. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  7738. this.ctx.drawImage(this.transparentCanvas, 0, 0);
  7739. this.ctx.restore();
  7740. this.transparentCanvas = null;
  7741. }
  7742. }
  7743. endDrawing() {
  7744. this.#restoreInitialState();
  7745. this.cachedCanvases.clear();
  7746. this.cachedPatterns.clear();
  7747. for (const cache of this._cachedBitmapsMap.values()) {
  7748. for (const canvas of cache.values()) {
  7749. if (typeof HTMLCanvasElement !== "undefined" && canvas instanceof HTMLCanvasElement) {
  7750. canvas.width = canvas.height = 0;
  7751. }
  7752. }
  7753. cache.clear();
  7754. }
  7755. this._cachedBitmapsMap.clear();
  7756. this.#drawFilter();
  7757. }
  7758. #drawFilter() {
  7759. if (this.pageColors) {
  7760. const hcmFilterId = this.filterFactory.addHCMFilter(this.pageColors.foreground, this.pageColors.background);
  7761. if (hcmFilterId !== "none") {
  7762. const savedFilter = this.ctx.filter;
  7763. this.ctx.filter = hcmFilterId;
  7764. this.ctx.drawImage(this.ctx.canvas, 0, 0);
  7765. this.ctx.filter = savedFilter;
  7766. }
  7767. }
  7768. }
  7769. _scaleImage(img, inverseTransform) {
  7770. const width = img.width ?? img.displayWidth;
  7771. const height = img.height ?? img.displayHeight;
  7772. let widthScale = Math.max(Math.hypot(inverseTransform[0], inverseTransform[1]), 1);
  7773. let heightScale = Math.max(Math.hypot(inverseTransform[2], inverseTransform[3]), 1);
  7774. let paintWidth = width,
  7775. paintHeight = height;
  7776. let tmpCanvasId = "prescale1";
  7777. let tmpCanvas, tmpCtx;
  7778. while (widthScale > 2 && paintWidth > 1 || heightScale > 2 && paintHeight > 1) {
  7779. let newWidth = paintWidth,
  7780. newHeight = paintHeight;
  7781. if (widthScale > 2 && paintWidth > 1) {
  7782. newWidth = paintWidth >= 16384 ? Math.floor(paintWidth / 2) - 1 || 1 : Math.ceil(paintWidth / 2);
  7783. widthScale /= paintWidth / newWidth;
  7784. }
  7785. if (heightScale > 2 && paintHeight > 1) {
  7786. newHeight = paintHeight >= 16384 ? Math.floor(paintHeight / 2) - 1 || 1 : Math.ceil(paintHeight) / 2;
  7787. heightScale /= paintHeight / newHeight;
  7788. }
  7789. tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
  7790. tmpCtx = tmpCanvas.context;
  7791. tmpCtx.clearRect(0, 0, newWidth, newHeight);
  7792. tmpCtx.drawImage(img, 0, 0, paintWidth, paintHeight, 0, 0, newWidth, newHeight);
  7793. img = tmpCanvas.canvas;
  7794. paintWidth = newWidth;
  7795. paintHeight = newHeight;
  7796. tmpCanvasId = tmpCanvasId === "prescale1" ? "prescale2" : "prescale1";
  7797. }
  7798. return {
  7799. img,
  7800. paintWidth,
  7801. paintHeight
  7802. };
  7803. }
  7804. _createMaskCanvas(img) {
  7805. const ctx = this.ctx;
  7806. const {
  7807. width,
  7808. height
  7809. } = img;
  7810. const fillColor = this.current.fillColor;
  7811. const isPatternFill = this.current.patternFill;
  7812. const currentTransform = getCurrentTransform(ctx);
  7813. let cache, cacheKey, scaled, maskCanvas;
  7814. if ((img.bitmap || img.data) && img.count > 1) {
  7815. const mainKey = img.bitmap || img.data.buffer;
  7816. cacheKey = JSON.stringify(isPatternFill ? currentTransform : [currentTransform.slice(0, 4), fillColor]);
  7817. cache = this._cachedBitmapsMap.get(mainKey);
  7818. if (!cache) {
  7819. cache = new Map();
  7820. this._cachedBitmapsMap.set(mainKey, cache);
  7821. }
  7822. const cachedImage = cache.get(cacheKey);
  7823. if (cachedImage && !isPatternFill) {
  7824. const offsetX = Math.round(Math.min(currentTransform[0], currentTransform[2]) + currentTransform[4]);
  7825. const offsetY = Math.round(Math.min(currentTransform[1], currentTransform[3]) + currentTransform[5]);
  7826. return {
  7827. canvas: cachedImage,
  7828. offsetX,
  7829. offsetY
  7830. };
  7831. }
  7832. scaled = cachedImage;
  7833. }
  7834. if (!scaled) {
  7835. maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
  7836. putBinaryImageMask(maskCanvas.context, img);
  7837. }
  7838. let maskToCanvas = Util.transform(currentTransform, [1 / width, 0, 0, -1 / height, 0, 0]);
  7839. maskToCanvas = Util.transform(maskToCanvas, [1, 0, 0, 1, 0, -height]);
  7840. const minMax = MIN_MAX_INIT.slice();
  7841. Util.axialAlignedBoundingBox([0, 0, width, height], maskToCanvas, minMax);
  7842. const [minX, minY, maxX, maxY] = minMax;
  7843. const drawnWidth = Math.round(maxX - minX) || 1;
  7844. const drawnHeight = Math.round(maxY - minY) || 1;
  7845. const fillCanvas = this.cachedCanvases.getCanvas("fillCanvas", drawnWidth, drawnHeight);
  7846. const fillCtx = fillCanvas.context;
  7847. const offsetX = minX;
  7848. const offsetY = minY;
  7849. fillCtx.translate(-offsetX, -offsetY);
  7850. fillCtx.transform(...maskToCanvas);
  7851. if (!scaled) {
  7852. scaled = this._scaleImage(maskCanvas.canvas, getCurrentTransformInverse(fillCtx));
  7853. scaled = scaled.img;
  7854. if (cache && isPatternFill) {
  7855. cache.set(cacheKey, scaled);
  7856. }
  7857. }
  7858. fillCtx.imageSmoothingEnabled = getImageSmoothingEnabled(getCurrentTransform(fillCtx), img.interpolate);
  7859. drawImageAtIntegerCoords(fillCtx, scaled, 0, 0, scaled.width, scaled.height, 0, 0, width, height);
  7860. fillCtx.globalCompositeOperation = "source-in";
  7861. const inverse = Util.transform(getCurrentTransformInverse(fillCtx), [1, 0, 0, 1, -offsetX, -offsetY]);
  7862. fillCtx.fillStyle = isPatternFill ? fillColor.getPattern(ctx, this, inverse, PathType.FILL) : fillColor;
  7863. fillCtx.fillRect(0, 0, width, height);
  7864. if (cache && !isPatternFill) {
  7865. this.cachedCanvases.delete("fillCanvas");
  7866. cache.set(cacheKey, fillCanvas.canvas);
  7867. }
  7868. return {
  7869. canvas: fillCanvas.canvas,
  7870. offsetX: Math.round(offsetX),
  7871. offsetY: Math.round(offsetY)
  7872. };
  7873. }
  7874. setLineWidth(width) {
  7875. if (width !== this.current.lineWidth) {
  7876. this._cachedScaleForStroking[0] = -1;
  7877. }
  7878. this.current.lineWidth = width;
  7879. this.ctx.lineWidth = width;
  7880. }
  7881. setLineCap(style) {
  7882. this.ctx.lineCap = LINE_CAP_STYLES[style];
  7883. }
  7884. setLineJoin(style) {
  7885. this.ctx.lineJoin = LINE_JOIN_STYLES[style];
  7886. }
  7887. setMiterLimit(limit) {
  7888. this.ctx.miterLimit = limit;
  7889. }
  7890. setDash(dashArray, dashPhase) {
  7891. const ctx = this.ctx;
  7892. if (ctx.setLineDash !== undefined) {
  7893. ctx.setLineDash(dashArray);
  7894. ctx.lineDashOffset = dashPhase;
  7895. }
  7896. }
  7897. setRenderingIntent(intent) {}
  7898. setFlatness(flatness) {}
  7899. setGState(states) {
  7900. for (const [key, value] of states) {
  7901. switch (key) {
  7902. case "LW":
  7903. this.setLineWidth(value);
  7904. break;
  7905. case "LC":
  7906. this.setLineCap(value);
  7907. break;
  7908. case "LJ":
  7909. this.setLineJoin(value);
  7910. break;
  7911. case "ML":
  7912. this.setMiterLimit(value);
  7913. break;
  7914. case "D":
  7915. this.setDash(value[0], value[1]);
  7916. break;
  7917. case "RI":
  7918. this.setRenderingIntent(value);
  7919. break;
  7920. case "FL":
  7921. this.setFlatness(value);
  7922. break;
  7923. case "Font":
  7924. this.setFont(value[0], value[1]);
  7925. break;
  7926. case "CA":
  7927. this.current.strokeAlpha = value;
  7928. break;
  7929. case "ca":
  7930. this.ctx.globalAlpha = this.current.fillAlpha = value;
  7931. break;
  7932. case "BM":
  7933. this.ctx.globalCompositeOperation = value;
  7934. break;
  7935. case "SMask":
  7936. this.current.activeSMask = value ? this.tempSMask : null;
  7937. this.tempSMask = null;
  7938. this.checkSMaskState();
  7939. break;
  7940. case "TR":
  7941. this.ctx.filter = this.current.transferMaps = this.filterFactory.addFilter(value);
  7942. break;
  7943. }
  7944. }
  7945. }
  7946. get inSMaskMode() {
  7947. return !!this.suspendedCtx;
  7948. }
  7949. checkSMaskState() {
  7950. const inSMaskMode = this.inSMaskMode;
  7951. if (this.current.activeSMask && !inSMaskMode) {
  7952. this.beginSMaskMode();
  7953. } else if (!this.current.activeSMask && inSMaskMode) {
  7954. this.endSMaskMode();
  7955. }
  7956. }
  7957. beginSMaskMode() {
  7958. if (this.inSMaskMode) {
  7959. throw new Error("beginSMaskMode called while already in smask mode");
  7960. }
  7961. const drawnWidth = this.ctx.canvas.width;
  7962. const drawnHeight = this.ctx.canvas.height;
  7963. const cacheId = "smaskGroupAt" + this.groupLevel;
  7964. const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight);
  7965. this.suspendedCtx = this.ctx;
  7966. const ctx = this.ctx = scratchCanvas.context;
  7967. ctx.setTransform(this.suspendedCtx.getTransform());
  7968. copyCtxState(this.suspendedCtx, ctx);
  7969. mirrorContextOperations(ctx, this.suspendedCtx);
  7970. this.setGState([["BM", "source-over"]]);
  7971. }
  7972. endSMaskMode() {
  7973. if (!this.inSMaskMode) {
  7974. throw new Error("endSMaskMode called while not in smask mode");
  7975. }
  7976. this.ctx._removeMirroring();
  7977. copyCtxState(this.ctx, this.suspendedCtx);
  7978. this.ctx = this.suspendedCtx;
  7979. this.suspendedCtx = null;
  7980. }
  7981. compose(dirtyBox) {
  7982. if (!this.current.activeSMask) {
  7983. return;
  7984. }
  7985. if (!dirtyBox) {
  7986. dirtyBox = [0, 0, this.ctx.canvas.width, this.ctx.canvas.height];
  7987. } else {
  7988. dirtyBox[0] = Math.floor(dirtyBox[0]);
  7989. dirtyBox[1] = Math.floor(dirtyBox[1]);
  7990. dirtyBox[2] = Math.ceil(dirtyBox[2]);
  7991. dirtyBox[3] = Math.ceil(dirtyBox[3]);
  7992. }
  7993. const smask = this.current.activeSMask;
  7994. const suspendedCtx = this.suspendedCtx;
  7995. this.composeSMask(suspendedCtx, smask, this.ctx, dirtyBox);
  7996. this.ctx.save();
  7997. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  7998. this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  7999. this.ctx.restore();
  8000. }
  8001. composeSMask(ctx, smask, layerCtx, layerBox) {
  8002. const layerOffsetX = layerBox[0];
  8003. const layerOffsetY = layerBox[1];
  8004. const layerWidth = layerBox[2] - layerOffsetX;
  8005. const layerHeight = layerBox[3] - layerOffsetY;
  8006. if (layerWidth === 0 || layerHeight === 0) {
  8007. return;
  8008. }
  8009. this.genericComposeSMask(smask.context, layerCtx, layerWidth, layerHeight, smask.subtype, smask.backdrop, smask.transferMap, layerOffsetX, layerOffsetY, smask.offsetX, smask.offsetY);
  8010. ctx.save();
  8011. ctx.globalAlpha = 1;
  8012. ctx.globalCompositeOperation = "source-over";
  8013. ctx.setTransform(1, 0, 0, 1, 0, 0);
  8014. ctx.drawImage(layerCtx.canvas, 0, 0);
  8015. ctx.restore();
  8016. }
  8017. genericComposeSMask(maskCtx, layerCtx, width, height, subtype, backdrop, transferMap, layerOffsetX, layerOffsetY, maskOffsetX, maskOffsetY) {
  8018. let maskCanvas = maskCtx.canvas;
  8019. let maskX = layerOffsetX - maskOffsetX;
  8020. let maskY = layerOffsetY - maskOffsetY;
  8021. if (backdrop) {
  8022. const backdropRGB = Util.makeHexColor(...backdrop);
  8023. if (maskX < 0 || maskY < 0 || maskX + width > maskCanvas.width || maskY + height > maskCanvas.height) {
  8024. const canvas = this.cachedCanvases.getCanvas("maskExtension", width, height);
  8025. const ctx = canvas.context;
  8026. ctx.drawImage(maskCanvas, -maskX, -maskY);
  8027. ctx.globalCompositeOperation = "destination-atop";
  8028. ctx.fillStyle = backdropRGB;
  8029. ctx.fillRect(0, 0, width, height);
  8030. ctx.globalCompositeOperation = "source-over";
  8031. maskCanvas = canvas.canvas;
  8032. maskX = maskY = 0;
  8033. } else {
  8034. maskCtx.save();
  8035. maskCtx.globalAlpha = 1;
  8036. maskCtx.setTransform(1, 0, 0, 1, 0, 0);
  8037. const clip = new Path2D();
  8038. clip.rect(maskX, maskY, width, height);
  8039. maskCtx.clip(clip);
  8040. maskCtx.globalCompositeOperation = "destination-atop";
  8041. maskCtx.fillStyle = backdropRGB;
  8042. maskCtx.fillRect(maskX, maskY, width, height);
  8043. maskCtx.restore();
  8044. }
  8045. }
  8046. layerCtx.save();
  8047. layerCtx.globalAlpha = 1;
  8048. layerCtx.setTransform(1, 0, 0, 1, 0, 0);
  8049. if (subtype === "Alpha" && transferMap) {
  8050. layerCtx.filter = this.filterFactory.addAlphaFilter(transferMap);
  8051. } else if (subtype === "Luminosity") {
  8052. layerCtx.filter = this.filterFactory.addLuminosityFilter(transferMap);
  8053. }
  8054. const clip = new Path2D();
  8055. clip.rect(layerOffsetX, layerOffsetY, width, height);
  8056. layerCtx.clip(clip);
  8057. layerCtx.globalCompositeOperation = "destination-in";
  8058. layerCtx.drawImage(maskCanvas, maskX, maskY, width, height, layerOffsetX, layerOffsetY, width, height);
  8059. layerCtx.restore();
  8060. }
  8061. save() {
  8062. if (this.inSMaskMode) {
  8063. copyCtxState(this.ctx, this.suspendedCtx);
  8064. }
  8065. this.ctx.save();
  8066. const old = this.current;
  8067. this.stateStack.push(old);
  8068. this.current = old.clone();
  8069. }
  8070. restore() {
  8071. if (this.stateStack.length === 0) {
  8072. if (this.inSMaskMode) {
  8073. this.endSMaskMode();
  8074. }
  8075. return;
  8076. }
  8077. this.current = this.stateStack.pop();
  8078. this.ctx.restore();
  8079. if (this.inSMaskMode) {
  8080. copyCtxState(this.suspendedCtx, this.ctx);
  8081. }
  8082. this.checkSMaskState();
  8083. this.pendingClip = null;
  8084. this._cachedScaleForStroking[0] = -1;
  8085. this._cachedGetSinglePixelWidth = null;
  8086. }
  8087. transform(a, b, c, d, e, f) {
  8088. this.ctx.transform(a, b, c, d, e, f);
  8089. this._cachedScaleForStroking[0] = -1;
  8090. this._cachedGetSinglePixelWidth = null;
  8091. }
  8092. constructPath(op, data, minMax) {
  8093. let [path] = data;
  8094. if (!minMax) {
  8095. path ||= data[0] = new Path2D();
  8096. this[op](path);
  8097. return;
  8098. }
  8099. if (!(path instanceof Path2D)) {
  8100. const path2d = data[0] = new Path2D();
  8101. for (let i = 0, ii = path.length; i < ii;) {
  8102. switch (path[i++]) {
  8103. case DrawOPS.moveTo:
  8104. path2d.moveTo(path[i++], path[i++]);
  8105. break;
  8106. case DrawOPS.lineTo:
  8107. path2d.lineTo(path[i++], path[i++]);
  8108. break;
  8109. case DrawOPS.curveTo:
  8110. path2d.bezierCurveTo(path[i++], path[i++], path[i++], path[i++], path[i++], path[i++]);
  8111. break;
  8112. case DrawOPS.closePath:
  8113. path2d.closePath();
  8114. break;
  8115. default:
  8116. warn(`Unrecognized drawing path operator: ${path[i - 1]}`);
  8117. break;
  8118. }
  8119. }
  8120. path = path2d;
  8121. }
  8122. Util.axialAlignedBoundingBox(minMax, getCurrentTransform(this.ctx), this.current.minMax);
  8123. this[op](path);
  8124. }
  8125. closePath() {
  8126. this.ctx.closePath();
  8127. }
  8128. stroke(path, consumePath = true) {
  8129. const ctx = this.ctx;
  8130. const strokeColor = this.current.strokeColor;
  8131. ctx.globalAlpha = this.current.strokeAlpha;
  8132. if (this.contentVisible) {
  8133. if (typeof strokeColor === "object" && strokeColor?.getPattern) {
  8134. const baseTransform = strokeColor.isModifyingCurrentTransform() ? ctx.getTransform() : null;
  8135. ctx.save();
  8136. ctx.strokeStyle = strokeColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.STROKE);
  8137. if (baseTransform) {
  8138. const newPath = new Path2D();
  8139. newPath.addPath(path, ctx.getTransform().invertSelf().multiplySelf(baseTransform));
  8140. path = newPath;
  8141. }
  8142. this.rescaleAndStroke(path, false);
  8143. ctx.restore();
  8144. } else {
  8145. this.rescaleAndStroke(path, true);
  8146. }
  8147. }
  8148. if (consumePath) {
  8149. this.consumePath(path, this.current.getClippedPathBoundingBox(PathType.STROKE, getCurrentTransform(this.ctx)));
  8150. }
  8151. ctx.globalAlpha = this.current.fillAlpha;
  8152. }
  8153. closeStroke(path) {
  8154. this.stroke(path);
  8155. }
  8156. fill(path, consumePath = true) {
  8157. const ctx = this.ctx;
  8158. const fillColor = this.current.fillColor;
  8159. const isPatternFill = this.current.patternFill;
  8160. let needRestore = false;
  8161. if (isPatternFill) {
  8162. const baseTransform = fillColor.isModifyingCurrentTransform() ? ctx.getTransform() : null;
  8163. ctx.save();
  8164. ctx.fillStyle = fillColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.FILL);
  8165. if (baseTransform) {
  8166. const newPath = new Path2D();
  8167. newPath.addPath(path, ctx.getTransform().invertSelf().multiplySelf(baseTransform));
  8168. path = newPath;
  8169. }
  8170. needRestore = true;
  8171. }
  8172. const intersect = this.current.getClippedPathBoundingBox();
  8173. if (this.contentVisible && intersect !== null) {
  8174. if (this.pendingEOFill) {
  8175. ctx.fill(path, "evenodd");
  8176. this.pendingEOFill = false;
  8177. } else {
  8178. ctx.fill(path);
  8179. }
  8180. }
  8181. if (needRestore) {
  8182. ctx.restore();
  8183. }
  8184. if (consumePath) {
  8185. this.consumePath(path, intersect);
  8186. }
  8187. }
  8188. eoFill(path) {
  8189. this.pendingEOFill = true;
  8190. this.fill(path);
  8191. }
  8192. fillStroke(path) {
  8193. this.fill(path, false);
  8194. this.stroke(path, false);
  8195. this.consumePath(path);
  8196. }
  8197. eoFillStroke(path) {
  8198. this.pendingEOFill = true;
  8199. this.fillStroke(path);
  8200. }
  8201. closeFillStroke(path) {
  8202. this.fillStroke(path);
  8203. }
  8204. closeEOFillStroke(path) {
  8205. this.pendingEOFill = true;
  8206. this.fillStroke(path);
  8207. }
  8208. endPath(path) {
  8209. this.consumePath(path);
  8210. }
  8211. rawFillPath(path) {
  8212. this.ctx.fill(path);
  8213. }
  8214. clip() {
  8215. this.pendingClip = NORMAL_CLIP;
  8216. }
  8217. eoClip() {
  8218. this.pendingClip = EO_CLIP;
  8219. }
  8220. beginText() {
  8221. this.current.textMatrix = null;
  8222. this.current.textMatrixScale = 1;
  8223. this.current.x = this.current.lineX = 0;
  8224. this.current.y = this.current.lineY = 0;
  8225. }
  8226. endText() {
  8227. const paths = this.pendingTextPaths;
  8228. const ctx = this.ctx;
  8229. if (paths === undefined) {
  8230. return;
  8231. }
  8232. const newPath = new Path2D();
  8233. const invTransf = ctx.getTransform().invertSelf();
  8234. for (const {
  8235. transform,
  8236. x,
  8237. y,
  8238. fontSize,
  8239. path
  8240. } of paths) {
  8241. newPath.addPath(path, new DOMMatrix(transform).preMultiplySelf(invTransf).translate(x, y).scale(fontSize, -fontSize));
  8242. }
  8243. ctx.clip(newPath);
  8244. delete this.pendingTextPaths;
  8245. }
  8246. setCharSpacing(spacing) {
  8247. this.current.charSpacing = spacing;
  8248. }
  8249. setWordSpacing(spacing) {
  8250. this.current.wordSpacing = spacing;
  8251. }
  8252. setHScale(scale) {
  8253. this.current.textHScale = scale / 100;
  8254. }
  8255. setLeading(leading) {
  8256. this.current.leading = -leading;
  8257. }
  8258. setFont(fontRefName, size) {
  8259. const fontObj = this.commonObjs.get(fontRefName);
  8260. const current = this.current;
  8261. if (!fontObj) {
  8262. throw new Error(`Can't find font for ${fontRefName}`);
  8263. }
  8264. current.fontMatrix = fontObj.fontMatrix || FONT_IDENTITY_MATRIX;
  8265. if (current.fontMatrix[0] === 0 || current.fontMatrix[3] === 0) {
  8266. warn("Invalid font matrix for font " + fontRefName);
  8267. }
  8268. if (size < 0) {
  8269. size = -size;
  8270. current.fontDirection = -1;
  8271. } else {
  8272. current.fontDirection = 1;
  8273. }
  8274. this.current.font = fontObj;
  8275. this.current.fontSize = size;
  8276. if (fontObj.isType3Font) {
  8277. return;
  8278. }
  8279. const name = fontObj.loadedName || "sans-serif";
  8280. const typeface = fontObj.systemFontInfo?.css || `"${name}", ${fontObj.fallbackName}`;
  8281. let bold = "normal";
  8282. if (fontObj.black) {
  8283. bold = "900";
  8284. } else if (fontObj.bold) {
  8285. bold = "bold";
  8286. }
  8287. const italic = fontObj.italic ? "italic" : "normal";
  8288. let browserFontSize = size;
  8289. if (size < MIN_FONT_SIZE) {
  8290. browserFontSize = MIN_FONT_SIZE;
  8291. } else if (size > MAX_FONT_SIZE) {
  8292. browserFontSize = MAX_FONT_SIZE;
  8293. }
  8294. this.current.fontSizeScale = size / browserFontSize;
  8295. this.ctx.font = `${italic} ${bold} ${browserFontSize}px ${typeface}`;
  8296. }
  8297. setTextRenderingMode(mode) {
  8298. this.current.textRenderingMode = mode;
  8299. }
  8300. setTextRise(rise) {
  8301. this.current.textRise = rise;
  8302. }
  8303. moveText(x, y) {
  8304. this.current.x = this.current.lineX += x;
  8305. this.current.y = this.current.lineY += y;
  8306. }
  8307. setLeadingMoveText(x, y) {
  8308. this.setLeading(-y);
  8309. this.moveText(x, y);
  8310. }
  8311. setTextMatrix(matrix) {
  8312. const {
  8313. current
  8314. } = this;
  8315. current.textMatrix = matrix;
  8316. current.textMatrixScale = Math.hypot(matrix[0], matrix[1]);
  8317. current.x = current.lineX = 0;
  8318. current.y = current.lineY = 0;
  8319. }
  8320. nextLine() {
  8321. this.moveText(0, this.current.leading);
  8322. }
  8323. #getScaledPath(path, currentTransform, transform) {
  8324. const newPath = new Path2D();
  8325. newPath.addPath(path, new DOMMatrix(transform).invertSelf().multiplySelf(currentTransform));
  8326. return newPath;
  8327. }
  8328. paintChar(character, x, y, patternFillTransform, patternStrokeTransform) {
  8329. const ctx = this.ctx;
  8330. const current = this.current;
  8331. const font = current.font;
  8332. const textRenderingMode = current.textRenderingMode;
  8333. const fontSize = current.fontSize / current.fontSizeScale;
  8334. const fillStrokeMode = textRenderingMode & TextRenderingMode.FILL_STROKE_MASK;
  8335. const isAddToPathSet = !!(textRenderingMode & TextRenderingMode.ADD_TO_PATH_FLAG);
  8336. const patternFill = current.patternFill && !font.missingFile;
  8337. const patternStroke = current.patternStroke && !font.missingFile;
  8338. let path;
  8339. if (font.disableFontFace || isAddToPathSet || patternFill || patternStroke) {
  8340. path = font.getPathGenerator(this.commonObjs, character);
  8341. }
  8342. if (font.disableFontFace || patternFill || patternStroke) {
  8343. ctx.save();
  8344. ctx.translate(x, y);
  8345. ctx.scale(fontSize, -fontSize);
  8346. let currentTransform;
  8347. if (fillStrokeMode === TextRenderingMode.FILL || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8348. if (patternFillTransform) {
  8349. currentTransform = ctx.getTransform();
  8350. ctx.setTransform(...patternFillTransform);
  8351. ctx.fill(this.#getScaledPath(path, currentTransform, patternFillTransform));
  8352. } else {
  8353. ctx.fill(path);
  8354. }
  8355. }
  8356. if (fillStrokeMode === TextRenderingMode.STROKE || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8357. if (patternStrokeTransform) {
  8358. currentTransform ||= ctx.getTransform();
  8359. ctx.setTransform(...patternStrokeTransform);
  8360. const {
  8361. a,
  8362. b,
  8363. c,
  8364. d
  8365. } = currentTransform;
  8366. const invPatternTransform = Util.inverseTransform(patternStrokeTransform);
  8367. const transf = Util.transform([a, b, c, d, 0, 0], invPatternTransform);
  8368. Util.singularValueDecompose2dScale(transf, XY);
  8369. ctx.lineWidth *= Math.max(XY[0], XY[1]) / fontSize;
  8370. ctx.stroke(this.#getScaledPath(path, currentTransform, patternStrokeTransform));
  8371. } else {
  8372. ctx.lineWidth /= fontSize;
  8373. ctx.stroke(path);
  8374. }
  8375. }
  8376. ctx.restore();
  8377. } else {
  8378. if (fillStrokeMode === TextRenderingMode.FILL || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8379. ctx.fillText(character, x, y);
  8380. }
  8381. if (fillStrokeMode === TextRenderingMode.STROKE || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8382. ctx.strokeText(character, x, y);
  8383. }
  8384. }
  8385. if (isAddToPathSet) {
  8386. const paths = this.pendingTextPaths ||= [];
  8387. paths.push({
  8388. transform: getCurrentTransform(ctx),
  8389. x,
  8390. y,
  8391. fontSize,
  8392. path
  8393. });
  8394. }
  8395. }
  8396. get isFontSubpixelAAEnabled() {
  8397. const {
  8398. context: ctx
  8399. } = this.cachedCanvases.getCanvas("isFontSubpixelAAEnabled", 10, 10);
  8400. ctx.scale(1.5, 1);
  8401. ctx.fillText("I", 0, 10);
  8402. const data = ctx.getImageData(0, 0, 10, 10).data;
  8403. let enabled = false;
  8404. for (let i = 3; i < data.length; i += 4) {
  8405. if (data[i] > 0 && data[i] < 255) {
  8406. enabled = true;
  8407. break;
  8408. }
  8409. }
  8410. return shadow(this, "isFontSubpixelAAEnabled", enabled);
  8411. }
  8412. showText(glyphs) {
  8413. const current = this.current;
  8414. const font = current.font;
  8415. if (font.isType3Font) {
  8416. return this.showType3Text(glyphs);
  8417. }
  8418. const fontSize = current.fontSize;
  8419. if (fontSize === 0) {
  8420. return undefined;
  8421. }
  8422. const ctx = this.ctx;
  8423. const fontSizeScale = current.fontSizeScale;
  8424. const charSpacing = current.charSpacing;
  8425. const wordSpacing = current.wordSpacing;
  8426. const fontDirection = current.fontDirection;
  8427. const textHScale = current.textHScale * fontDirection;
  8428. const glyphsLength = glyphs.length;
  8429. const vertical = font.vertical;
  8430. const spacingDir = vertical ? 1 : -1;
  8431. const defaultVMetrics = font.defaultVMetrics;
  8432. const widthAdvanceScale = fontSize * current.fontMatrix[0];
  8433. const simpleFillText = current.textRenderingMode === TextRenderingMode.FILL && !font.disableFontFace && !current.patternFill;
  8434. ctx.save();
  8435. if (current.textMatrix) {
  8436. ctx.transform(...current.textMatrix);
  8437. }
  8438. ctx.translate(current.x, current.y + current.textRise);
  8439. if (fontDirection > 0) {
  8440. ctx.scale(textHScale, -1);
  8441. } else {
  8442. ctx.scale(textHScale, 1);
  8443. }
  8444. let patternFillTransform, patternStrokeTransform;
  8445. if (current.patternFill) {
  8446. ctx.save();
  8447. const pattern = current.fillColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.FILL);
  8448. patternFillTransform = getCurrentTransform(ctx);
  8449. ctx.restore();
  8450. ctx.fillStyle = pattern;
  8451. }
  8452. if (current.patternStroke) {
  8453. ctx.save();
  8454. const pattern = current.strokeColor.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.STROKE);
  8455. patternStrokeTransform = getCurrentTransform(ctx);
  8456. ctx.restore();
  8457. ctx.strokeStyle = pattern;
  8458. }
  8459. let lineWidth = current.lineWidth;
  8460. const scale = current.textMatrixScale;
  8461. if (scale === 0 || lineWidth === 0) {
  8462. const fillStrokeMode = current.textRenderingMode & TextRenderingMode.FILL_STROKE_MASK;
  8463. if (fillStrokeMode === TextRenderingMode.STROKE || fillStrokeMode === TextRenderingMode.FILL_STROKE) {
  8464. lineWidth = this.getSinglePixelWidth();
  8465. }
  8466. } else {
  8467. lineWidth /= scale;
  8468. }
  8469. if (fontSizeScale !== 1.0) {
  8470. ctx.scale(fontSizeScale, fontSizeScale);
  8471. lineWidth /= fontSizeScale;
  8472. }
  8473. ctx.lineWidth = lineWidth;
  8474. if (font.isInvalidPDFjsFont) {
  8475. const chars = [];
  8476. let width = 0;
  8477. for (const glyph of glyphs) {
  8478. chars.push(glyph.unicode);
  8479. width += glyph.width;
  8480. }
  8481. ctx.fillText(chars.join(""), 0, 0);
  8482. current.x += width * widthAdvanceScale * textHScale;
  8483. ctx.restore();
  8484. this.compose();
  8485. return undefined;
  8486. }
  8487. let x = 0,
  8488. i;
  8489. for (i = 0; i < glyphsLength; ++i) {
  8490. const glyph = glyphs[i];
  8491. if (typeof glyph === "number") {
  8492. x += spacingDir * glyph * fontSize / 1000;
  8493. continue;
  8494. }
  8495. let restoreNeeded = false;
  8496. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  8497. const character = glyph.fontChar;
  8498. const accent = glyph.accent;
  8499. let scaledX, scaledY;
  8500. let width = glyph.width;
  8501. if (vertical) {
  8502. const vmetric = glyph.vmetric || defaultVMetrics;
  8503. const vx = -(glyph.vmetric ? vmetric[1] : width * 0.5) * widthAdvanceScale;
  8504. const vy = vmetric[2] * widthAdvanceScale;
  8505. width = vmetric ? -vmetric[0] : width;
  8506. scaledX = vx / fontSizeScale;
  8507. scaledY = (x + vy) / fontSizeScale;
  8508. } else {
  8509. scaledX = x / fontSizeScale;
  8510. scaledY = 0;
  8511. }
  8512. if (font.remeasure && width > 0) {
  8513. const measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
  8514. if (width < measuredWidth && this.isFontSubpixelAAEnabled) {
  8515. const characterScaleX = width / measuredWidth;
  8516. restoreNeeded = true;
  8517. ctx.save();
  8518. ctx.scale(characterScaleX, 1);
  8519. scaledX /= characterScaleX;
  8520. } else if (width !== measuredWidth) {
  8521. scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
  8522. }
  8523. }
  8524. if (this.contentVisible && (glyph.isInFont || font.missingFile)) {
  8525. if (simpleFillText && !accent) {
  8526. ctx.fillText(character, scaledX, scaledY);
  8527. } else {
  8528. this.paintChar(character, scaledX, scaledY, patternFillTransform, patternStrokeTransform);
  8529. if (accent) {
  8530. const scaledAccentX = scaledX + fontSize * accent.offset.x / fontSizeScale;
  8531. const scaledAccentY = scaledY - fontSize * accent.offset.y / fontSizeScale;
  8532. this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY, patternFillTransform, patternStrokeTransform);
  8533. }
  8534. }
  8535. }
  8536. const charWidth = vertical ? width * widthAdvanceScale - spacing * fontDirection : width * widthAdvanceScale + spacing * fontDirection;
  8537. x += charWidth;
  8538. if (restoreNeeded) {
  8539. ctx.restore();
  8540. }
  8541. }
  8542. if (vertical) {
  8543. current.y -= x;
  8544. } else {
  8545. current.x += x * textHScale;
  8546. }
  8547. ctx.restore();
  8548. this.compose();
  8549. return undefined;
  8550. }
  8551. showType3Text(glyphs) {
  8552. const ctx = this.ctx;
  8553. const current = this.current;
  8554. const font = current.font;
  8555. const fontSize = current.fontSize;
  8556. const fontDirection = current.fontDirection;
  8557. const spacingDir = font.vertical ? 1 : -1;
  8558. const charSpacing = current.charSpacing;
  8559. const wordSpacing = current.wordSpacing;
  8560. const textHScale = current.textHScale * fontDirection;
  8561. const fontMatrix = current.fontMatrix || FONT_IDENTITY_MATRIX;
  8562. const glyphsLength = glyphs.length;
  8563. const isTextInvisible = current.textRenderingMode === TextRenderingMode.INVISIBLE;
  8564. let i, glyph, width, spacingLength;
  8565. if (isTextInvisible || fontSize === 0) {
  8566. return;
  8567. }
  8568. this._cachedScaleForStroking[0] = -1;
  8569. this._cachedGetSinglePixelWidth = null;
  8570. ctx.save();
  8571. if (current.textMatrix) {
  8572. ctx.transform(...current.textMatrix);
  8573. }
  8574. ctx.translate(current.x, current.y + current.textRise);
  8575. ctx.scale(textHScale, fontDirection);
  8576. for (i = 0; i < glyphsLength; ++i) {
  8577. glyph = glyphs[i];
  8578. if (typeof glyph === "number") {
  8579. spacingLength = spacingDir * glyph * fontSize / 1000;
  8580. this.ctx.translate(spacingLength, 0);
  8581. current.x += spacingLength * textHScale;
  8582. continue;
  8583. }
  8584. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  8585. const operatorList = font.charProcOperatorList[glyph.operatorListId];
  8586. if (!operatorList) {
  8587. warn(`Type3 character "${glyph.operatorListId}" is not available.`);
  8588. } else if (this.contentVisible) {
  8589. this.save();
  8590. ctx.scale(fontSize, fontSize);
  8591. ctx.transform(...fontMatrix);
  8592. this.executeOperatorList(operatorList);
  8593. this.restore();
  8594. }
  8595. const p = [glyph.width, 0];
  8596. Util.applyTransform(p, fontMatrix);
  8597. width = p[0] * fontSize + spacing;
  8598. ctx.translate(width, 0);
  8599. current.x += width * textHScale;
  8600. }
  8601. ctx.restore();
  8602. }
  8603. setCharWidth(xWidth, yWidth) {}
  8604. setCharWidthAndBounds(xWidth, yWidth, llx, lly, urx, ury) {
  8605. const clip = new Path2D();
  8606. clip.rect(llx, lly, urx - llx, ury - lly);
  8607. this.ctx.clip(clip);
  8608. this.endPath();
  8609. }
  8610. getColorN_Pattern(IR) {
  8611. let pattern;
  8612. if (IR[0] === "TilingPattern") {
  8613. const baseTransform = this.baseTransform || getCurrentTransform(this.ctx);
  8614. const canvasGraphicsFactory = {
  8615. createCanvasGraphics: ctx => new CanvasGraphics(ctx, this.commonObjs, this.objs, this.canvasFactory, this.filterFactory, {
  8616. optionalContentConfig: this.optionalContentConfig,
  8617. markedContentStack: this.markedContentStack
  8618. })
  8619. };
  8620. pattern = new TilingPattern(IR, this.ctx, canvasGraphicsFactory, baseTransform);
  8621. } else {
  8622. pattern = this._getPattern(IR[1], IR[2]);
  8623. }
  8624. return pattern;
  8625. }
  8626. setStrokeColorN() {
  8627. this.current.strokeColor = this.getColorN_Pattern(arguments);
  8628. this.current.patternStroke = true;
  8629. }
  8630. setFillColorN() {
  8631. this.current.fillColor = this.getColorN_Pattern(arguments);
  8632. this.current.patternFill = true;
  8633. }
  8634. setStrokeRGBColor(r, g, b) {
  8635. this.ctx.strokeStyle = this.current.strokeColor = Util.makeHexColor(r, g, b);
  8636. this.current.patternStroke = false;
  8637. }
  8638. setStrokeTransparent() {
  8639. this.ctx.strokeStyle = this.current.strokeColor = "transparent";
  8640. this.current.patternStroke = false;
  8641. }
  8642. setFillRGBColor(r, g, b) {
  8643. this.ctx.fillStyle = this.current.fillColor = Util.makeHexColor(r, g, b);
  8644. this.current.patternFill = false;
  8645. }
  8646. setFillTransparent() {
  8647. this.ctx.fillStyle = this.current.fillColor = "transparent";
  8648. this.current.patternFill = false;
  8649. }
  8650. _getPattern(objId, matrix = null) {
  8651. let pattern;
  8652. if (this.cachedPatterns.has(objId)) {
  8653. pattern = this.cachedPatterns.get(objId);
  8654. } else {
  8655. pattern = getShadingPattern(this.getObject(objId));
  8656. this.cachedPatterns.set(objId, pattern);
  8657. }
  8658. if (matrix) {
  8659. pattern.matrix = matrix;
  8660. }
  8661. return pattern;
  8662. }
  8663. shadingFill(objId) {
  8664. if (!this.contentVisible) {
  8665. return;
  8666. }
  8667. const ctx = this.ctx;
  8668. this.save();
  8669. const pattern = this._getPattern(objId);
  8670. ctx.fillStyle = pattern.getPattern(ctx, this, getCurrentTransformInverse(ctx), PathType.SHADING);
  8671. const inv = getCurrentTransformInverse(ctx);
  8672. if (inv) {
  8673. const {
  8674. width,
  8675. height
  8676. } = ctx.canvas;
  8677. const minMax = MIN_MAX_INIT.slice();
  8678. Util.axialAlignedBoundingBox([0, 0, width, height], inv, minMax);
  8679. const [x0, y0, x1, y1] = minMax;
  8680. this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
  8681. } else {
  8682. this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
  8683. }
  8684. this.compose(this.current.getClippedPathBoundingBox());
  8685. this.restore();
  8686. }
  8687. beginInlineImage() {
  8688. unreachable("Should not call beginInlineImage");
  8689. }
  8690. beginImageData() {
  8691. unreachable("Should not call beginImageData");
  8692. }
  8693. paintFormXObjectBegin(matrix, bbox) {
  8694. if (!this.contentVisible) {
  8695. return;
  8696. }
  8697. this.save();
  8698. this.baseTransformStack.push(this.baseTransform);
  8699. if (matrix) {
  8700. this.transform(...matrix);
  8701. }
  8702. this.baseTransform = getCurrentTransform(this.ctx);
  8703. if (bbox) {
  8704. Util.axialAlignedBoundingBox(bbox, this.baseTransform, this.current.minMax);
  8705. const [x0, y0, x1, y1] = bbox;
  8706. const clip = new Path2D();
  8707. clip.rect(x0, y0, x1 - x0, y1 - y0);
  8708. this.ctx.clip(clip);
  8709. this.endPath();
  8710. }
  8711. }
  8712. paintFormXObjectEnd() {
  8713. if (!this.contentVisible) {
  8714. return;
  8715. }
  8716. this.restore();
  8717. this.baseTransform = this.baseTransformStack.pop();
  8718. }
  8719. beginGroup(group) {
  8720. if (!this.contentVisible) {
  8721. return;
  8722. }
  8723. this.save();
  8724. if (this.inSMaskMode) {
  8725. this.endSMaskMode();
  8726. this.current.activeSMask = null;
  8727. }
  8728. const currentCtx = this.ctx;
  8729. if (!group.isolated) {
  8730. info("TODO: Support non-isolated groups.");
  8731. }
  8732. if (group.knockout) {
  8733. warn("Knockout groups not supported.");
  8734. }
  8735. const currentTransform = getCurrentTransform(currentCtx);
  8736. if (group.matrix) {
  8737. currentCtx.transform(...group.matrix);
  8738. }
  8739. if (!group.bbox) {
  8740. throw new Error("Bounding box is required.");
  8741. }
  8742. let bounds = MIN_MAX_INIT.slice();
  8743. Util.axialAlignedBoundingBox(group.bbox, getCurrentTransform(currentCtx), bounds);
  8744. const canvasBounds = [0, 0, currentCtx.canvas.width, currentCtx.canvas.height];
  8745. bounds = Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
  8746. const offsetX = Math.floor(bounds[0]);
  8747. const offsetY = Math.floor(bounds[1]);
  8748. const drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
  8749. const drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
  8750. this.current.startNewPathAndClipBox([0, 0, drawnWidth, drawnHeight]);
  8751. let cacheId = "groupAt" + this.groupLevel;
  8752. if (group.smask) {
  8753. cacheId += "_smask_" + this.smaskCounter++ % 2;
  8754. }
  8755. const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight);
  8756. const groupCtx = scratchCanvas.context;
  8757. groupCtx.translate(-offsetX, -offsetY);
  8758. groupCtx.transform(...currentTransform);
  8759. let clip = new Path2D();
  8760. const [x0, y0, x1, y1] = group.bbox;
  8761. clip.rect(x0, y0, x1 - x0, y1 - y0);
  8762. if (group.matrix) {
  8763. const path = new Path2D();
  8764. path.addPath(clip, new DOMMatrix(group.matrix));
  8765. clip = path;
  8766. }
  8767. groupCtx.clip(clip);
  8768. if (group.smask) {
  8769. this.smaskStack.push({
  8770. canvas: scratchCanvas.canvas,
  8771. context: groupCtx,
  8772. offsetX,
  8773. offsetY,
  8774. subtype: group.smask.subtype,
  8775. backdrop: group.smask.backdrop,
  8776. transferMap: group.smask.transferMap || null,
  8777. startTransformInverse: null
  8778. });
  8779. } else {
  8780. currentCtx.setTransform(1, 0, 0, 1, 0, 0);
  8781. currentCtx.translate(offsetX, offsetY);
  8782. currentCtx.save();
  8783. }
  8784. copyCtxState(currentCtx, groupCtx);
  8785. this.ctx = groupCtx;
  8786. this.setGState([["BM", "source-over"], ["ca", 1], ["CA", 1]]);
  8787. this.groupStack.push(currentCtx);
  8788. this.groupLevel++;
  8789. }
  8790. endGroup(group) {
  8791. if (!this.contentVisible) {
  8792. return;
  8793. }
  8794. this.groupLevel--;
  8795. const groupCtx = this.ctx;
  8796. const ctx = this.groupStack.pop();
  8797. this.ctx = ctx;
  8798. this.ctx.imageSmoothingEnabled = false;
  8799. if (group.smask) {
  8800. this.tempSMask = this.smaskStack.pop();
  8801. this.restore();
  8802. } else {
  8803. this.ctx.restore();
  8804. const currentMtx = getCurrentTransform(this.ctx);
  8805. this.restore();
  8806. this.ctx.save();
  8807. this.ctx.setTransform(...currentMtx);
  8808. const dirtyBox = MIN_MAX_INIT.slice();
  8809. Util.axialAlignedBoundingBox([0, 0, groupCtx.canvas.width, groupCtx.canvas.height], currentMtx, dirtyBox);
  8810. this.ctx.drawImage(groupCtx.canvas, 0, 0);
  8811. this.ctx.restore();
  8812. this.compose(dirtyBox);
  8813. }
  8814. }
  8815. beginAnnotation(id, rect, transform, matrix, hasOwnCanvas) {
  8816. this.#restoreInitialState();
  8817. resetCtxToDefault(this.ctx);
  8818. this.ctx.save();
  8819. this.save();
  8820. if (this.baseTransform) {
  8821. this.ctx.setTransform(...this.baseTransform);
  8822. }
  8823. if (rect) {
  8824. const width = rect[2] - rect[0];
  8825. const height = rect[3] - rect[1];
  8826. if (hasOwnCanvas && this.annotationCanvasMap) {
  8827. transform = transform.slice();
  8828. transform[4] -= rect[0];
  8829. transform[5] -= rect[1];
  8830. rect = rect.slice();
  8831. rect[0] = rect[1] = 0;
  8832. rect[2] = width;
  8833. rect[3] = height;
  8834. Util.singularValueDecompose2dScale(getCurrentTransform(this.ctx), XY);
  8835. const {
  8836. viewportScale
  8837. } = this;
  8838. const canvasWidth = Math.ceil(width * this.outputScaleX * viewportScale);
  8839. const canvasHeight = Math.ceil(height * this.outputScaleY * viewportScale);
  8840. this.annotationCanvas = this.canvasFactory.create(canvasWidth, canvasHeight);
  8841. const {
  8842. canvas,
  8843. context
  8844. } = this.annotationCanvas;
  8845. this.annotationCanvasMap.set(id, canvas);
  8846. this.annotationCanvas.savedCtx = this.ctx;
  8847. this.ctx = context;
  8848. this.ctx.save();
  8849. this.ctx.setTransform(XY[0], 0, 0, -XY[1], 0, height * XY[1]);
  8850. resetCtxToDefault(this.ctx);
  8851. } else {
  8852. resetCtxToDefault(this.ctx);
  8853. this.endPath();
  8854. const clip = new Path2D();
  8855. clip.rect(rect[0], rect[1], width, height);
  8856. this.ctx.clip(clip);
  8857. }
  8858. }
  8859. this.current = new CanvasExtraState(this.ctx.canvas.width, this.ctx.canvas.height);
  8860. this.transform(...transform);
  8861. this.transform(...matrix);
  8862. }
  8863. endAnnotation() {
  8864. if (this.annotationCanvas) {
  8865. this.ctx.restore();
  8866. this.#drawFilter();
  8867. this.ctx = this.annotationCanvas.savedCtx;
  8868. delete this.annotationCanvas.savedCtx;
  8869. delete this.annotationCanvas;
  8870. }
  8871. }
  8872. paintImageMaskXObject(img) {
  8873. if (!this.contentVisible) {
  8874. return;
  8875. }
  8876. const count = img.count;
  8877. img = this.getObject(img.data, img);
  8878. img.count = count;
  8879. const ctx = this.ctx;
  8880. const mask = this._createMaskCanvas(img);
  8881. const maskCanvas = mask.canvas;
  8882. ctx.save();
  8883. ctx.setTransform(1, 0, 0, 1, 0, 0);
  8884. ctx.drawImage(maskCanvas, mask.offsetX, mask.offsetY);
  8885. ctx.restore();
  8886. this.compose();
  8887. }
  8888. paintImageMaskXObjectRepeat(img, scaleX, skewX = 0, skewY = 0, scaleY, positions) {
  8889. if (!this.contentVisible) {
  8890. return;
  8891. }
  8892. img = this.getObject(img.data, img);
  8893. const ctx = this.ctx;
  8894. ctx.save();
  8895. const currentTransform = getCurrentTransform(ctx);
  8896. ctx.transform(scaleX, skewX, skewY, scaleY, 0, 0);
  8897. const mask = this._createMaskCanvas(img);
  8898. ctx.setTransform(1, 0, 0, 1, mask.offsetX - currentTransform[4], mask.offsetY - currentTransform[5]);
  8899. for (let i = 0, ii = positions.length; i < ii; i += 2) {
  8900. const trans = Util.transform(currentTransform, [scaleX, skewX, skewY, scaleY, positions[i], positions[i + 1]]);
  8901. ctx.drawImage(mask.canvas, trans[4], trans[5]);
  8902. }
  8903. ctx.restore();
  8904. this.compose();
  8905. }
  8906. paintImageMaskXObjectGroup(images) {
  8907. if (!this.contentVisible) {
  8908. return;
  8909. }
  8910. const ctx = this.ctx;
  8911. const fillColor = this.current.fillColor;
  8912. const isPatternFill = this.current.patternFill;
  8913. for (const image of images) {
  8914. const {
  8915. data,
  8916. width,
  8917. height,
  8918. transform
  8919. } = image;
  8920. const maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
  8921. const maskCtx = maskCanvas.context;
  8922. maskCtx.save();
  8923. const img = this.getObject(data, image);
  8924. putBinaryImageMask(maskCtx, img);
  8925. maskCtx.globalCompositeOperation = "source-in";
  8926. maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this, getCurrentTransformInverse(ctx), PathType.FILL) : fillColor;
  8927. maskCtx.fillRect(0, 0, width, height);
  8928. maskCtx.restore();
  8929. ctx.save();
  8930. ctx.transform(...transform);
  8931. ctx.scale(1, -1);
  8932. drawImageAtIntegerCoords(ctx, maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
  8933. ctx.restore();
  8934. }
  8935. this.compose();
  8936. }
  8937. paintImageXObject(objId) {
  8938. if (!this.contentVisible) {
  8939. return;
  8940. }
  8941. const imgData = this.getObject(objId);
  8942. if (!imgData) {
  8943. warn("Dependent image isn't ready yet");
  8944. return;
  8945. }
  8946. this.paintInlineImageXObject(imgData);
  8947. }
  8948. paintImageXObjectRepeat(objId, scaleX, scaleY, positions) {
  8949. if (!this.contentVisible) {
  8950. return;
  8951. }
  8952. const imgData = this.getObject(objId);
  8953. if (!imgData) {
  8954. warn("Dependent image isn't ready yet");
  8955. return;
  8956. }
  8957. const width = imgData.width;
  8958. const height = imgData.height;
  8959. const map = [];
  8960. for (let i = 0, ii = positions.length; i < ii; i += 2) {
  8961. map.push({
  8962. transform: [scaleX, 0, 0, scaleY, positions[i], positions[i + 1]],
  8963. x: 0,
  8964. y: 0,
  8965. w: width,
  8966. h: height
  8967. });
  8968. }
  8969. this.paintInlineImageXObjectGroup(imgData, map);
  8970. }
  8971. applyTransferMapsToCanvas(ctx) {
  8972. if (this.current.transferMaps !== "none") {
  8973. ctx.filter = this.current.transferMaps;
  8974. ctx.drawImage(ctx.canvas, 0, 0);
  8975. ctx.filter = "none";
  8976. }
  8977. return ctx.canvas;
  8978. }
  8979. applyTransferMapsToBitmap(imgData) {
  8980. if (this.current.transferMaps === "none") {
  8981. return imgData.bitmap;
  8982. }
  8983. const {
  8984. bitmap,
  8985. width,
  8986. height
  8987. } = imgData;
  8988. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", width, height);
  8989. const tmpCtx = tmpCanvas.context;
  8990. tmpCtx.filter = this.current.transferMaps;
  8991. tmpCtx.drawImage(bitmap, 0, 0);
  8992. tmpCtx.filter = "none";
  8993. return tmpCanvas.canvas;
  8994. }
  8995. paintInlineImageXObject(imgData) {
  8996. if (!this.contentVisible) {
  8997. return;
  8998. }
  8999. const width = imgData.width;
  9000. const height = imgData.height;
  9001. const ctx = this.ctx;
  9002. this.save();
  9003. const {
  9004. filter
  9005. } = ctx;
  9006. if (filter !== "none" && filter !== "") {
  9007. ctx.filter = "none";
  9008. }
  9009. ctx.scale(1 / width, -1 / height);
  9010. let imgToPaint;
  9011. if (imgData.bitmap) {
  9012. imgToPaint = this.applyTransferMapsToBitmap(imgData);
  9013. } else if (typeof HTMLElement === "function" && imgData instanceof HTMLElement || !imgData.data) {
  9014. imgToPaint = imgData;
  9015. } else {
  9016. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", width, height);
  9017. const tmpCtx = tmpCanvas.context;
  9018. putBinaryImageData(tmpCtx, imgData);
  9019. imgToPaint = this.applyTransferMapsToCanvas(tmpCtx);
  9020. }
  9021. const scaled = this._scaleImage(imgToPaint, getCurrentTransformInverse(ctx));
  9022. ctx.imageSmoothingEnabled = getImageSmoothingEnabled(getCurrentTransform(ctx), imgData.interpolate);
  9023. drawImageAtIntegerCoords(ctx, scaled.img, 0, 0, scaled.paintWidth, scaled.paintHeight, 0, -height, width, height);
  9024. this.compose();
  9025. this.restore();
  9026. }
  9027. paintInlineImageXObjectGroup(imgData, map) {
  9028. if (!this.contentVisible) {
  9029. return;
  9030. }
  9031. const ctx = this.ctx;
  9032. let imgToPaint;
  9033. if (imgData.bitmap) {
  9034. imgToPaint = imgData.bitmap;
  9035. } else {
  9036. const w = imgData.width;
  9037. const h = imgData.height;
  9038. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", w, h);
  9039. const tmpCtx = tmpCanvas.context;
  9040. putBinaryImageData(tmpCtx, imgData);
  9041. imgToPaint = this.applyTransferMapsToCanvas(tmpCtx);
  9042. }
  9043. for (const entry of map) {
  9044. ctx.save();
  9045. ctx.transform(...entry.transform);
  9046. ctx.scale(1, -1);
  9047. drawImageAtIntegerCoords(ctx, imgToPaint, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1);
  9048. ctx.restore();
  9049. }
  9050. this.compose();
  9051. }
  9052. paintSolidColorImageMask() {
  9053. if (!this.contentVisible) {
  9054. return;
  9055. }
  9056. this.ctx.fillRect(0, 0, 1, 1);
  9057. this.compose();
  9058. }
  9059. markPoint(tag) {}
  9060. markPointProps(tag, properties) {}
  9061. beginMarkedContent(tag) {
  9062. this.markedContentStack.push({
  9063. visible: true
  9064. });
  9065. }
  9066. beginMarkedContentProps(tag, properties) {
  9067. if (tag === "OC") {
  9068. this.markedContentStack.push({
  9069. visible: this.optionalContentConfig.isVisible(properties)
  9070. });
  9071. } else {
  9072. this.markedContentStack.push({
  9073. visible: true
  9074. });
  9075. }
  9076. this.contentVisible = this.isContentVisible();
  9077. }
  9078. endMarkedContent() {
  9079. this.markedContentStack.pop();
  9080. this.contentVisible = this.isContentVisible();
  9081. }
  9082. beginCompat() {}
  9083. endCompat() {}
  9084. consumePath(path, clipBox) {
  9085. const isEmpty = this.current.isEmptyClip();
  9086. if (this.pendingClip) {
  9087. this.current.updateClipFromPath();
  9088. }
  9089. if (!this.pendingClip) {
  9090. this.compose(clipBox);
  9091. }
  9092. const ctx = this.ctx;
  9093. if (this.pendingClip) {
  9094. if (!isEmpty) {
  9095. if (this.pendingClip === EO_CLIP) {
  9096. ctx.clip(path, "evenodd");
  9097. } else {
  9098. ctx.clip(path);
  9099. }
  9100. }
  9101. this.pendingClip = null;
  9102. }
  9103. this.current.startNewPathAndClipBox(this.current.clipBox);
  9104. }
  9105. getSinglePixelWidth() {
  9106. if (!this._cachedGetSinglePixelWidth) {
  9107. const m = getCurrentTransform(this.ctx);
  9108. if (m[1] === 0 && m[2] === 0) {
  9109. this._cachedGetSinglePixelWidth = 1 / Math.min(Math.abs(m[0]), Math.abs(m[3]));
  9110. } else {
  9111. const absDet = Math.abs(m[0] * m[3] - m[2] * m[1]);
  9112. const normX = Math.hypot(m[0], m[2]);
  9113. const normY = Math.hypot(m[1], m[3]);
  9114. this._cachedGetSinglePixelWidth = Math.max(normX, normY) / absDet;
  9115. }
  9116. }
  9117. return this._cachedGetSinglePixelWidth;
  9118. }
  9119. getScaleForStroking() {
  9120. if (this._cachedScaleForStroking[0] === -1) {
  9121. const {
  9122. lineWidth
  9123. } = this.current;
  9124. const {
  9125. a,
  9126. b,
  9127. c,
  9128. d
  9129. } = this.ctx.getTransform();
  9130. let scaleX, scaleY;
  9131. if (b === 0 && c === 0) {
  9132. const normX = Math.abs(a);
  9133. const normY = Math.abs(d);
  9134. if (normX === normY) {
  9135. if (lineWidth === 0) {
  9136. scaleX = scaleY = 1 / normX;
  9137. } else {
  9138. const scaledLineWidth = normX * lineWidth;
  9139. scaleX = scaleY = scaledLineWidth < 1 ? 1 / scaledLineWidth : 1;
  9140. }
  9141. } else if (lineWidth === 0) {
  9142. scaleX = 1 / normX;
  9143. scaleY = 1 / normY;
  9144. } else {
  9145. const scaledXLineWidth = normX * lineWidth;
  9146. const scaledYLineWidth = normY * lineWidth;
  9147. scaleX = scaledXLineWidth < 1 ? 1 / scaledXLineWidth : 1;
  9148. scaleY = scaledYLineWidth < 1 ? 1 / scaledYLineWidth : 1;
  9149. }
  9150. } else {
  9151. const absDet = Math.abs(a * d - b * c);
  9152. const normX = Math.hypot(a, b);
  9153. const normY = Math.hypot(c, d);
  9154. if (lineWidth === 0) {
  9155. scaleX = normY / absDet;
  9156. scaleY = normX / absDet;
  9157. } else {
  9158. const baseArea = lineWidth * absDet;
  9159. scaleX = normY > baseArea ? normY / baseArea : 1;
  9160. scaleY = normX > baseArea ? normX / baseArea : 1;
  9161. }
  9162. }
  9163. this._cachedScaleForStroking[0] = scaleX;
  9164. this._cachedScaleForStroking[1] = scaleY;
  9165. }
  9166. return this._cachedScaleForStroking;
  9167. }
  9168. rescaleAndStroke(path, saveRestore) {
  9169. const {
  9170. ctx,
  9171. current: {
  9172. lineWidth
  9173. }
  9174. } = this;
  9175. const [scaleX, scaleY] = this.getScaleForStroking();
  9176. if (scaleX === scaleY) {
  9177. ctx.lineWidth = (lineWidth || 1) * scaleX;
  9178. ctx.stroke(path);
  9179. return;
  9180. }
  9181. const dashes = ctx.getLineDash();
  9182. if (saveRestore) {
  9183. ctx.save();
  9184. }
  9185. ctx.scale(scaleX, scaleY);
  9186. SCALE_MATRIX.a = 1 / scaleX;
  9187. SCALE_MATRIX.d = 1 / scaleY;
  9188. const newPath = new Path2D();
  9189. newPath.addPath(path, SCALE_MATRIX);
  9190. if (dashes.length > 0) {
  9191. const scale = Math.max(scaleX, scaleY);
  9192. ctx.setLineDash(dashes.map(x => x / scale));
  9193. ctx.lineDashOffset /= scale;
  9194. }
  9195. ctx.lineWidth = lineWidth || 1;
  9196. ctx.stroke(newPath);
  9197. if (saveRestore) {
  9198. ctx.restore();
  9199. }
  9200. }
  9201. isContentVisible() {
  9202. for (let i = this.markedContentStack.length - 1; i >= 0; i--) {
  9203. if (!this.markedContentStack[i].visible) {
  9204. return false;
  9205. }
  9206. }
  9207. return true;
  9208. }
  9209. }
  9210. for (const op in OPS) {
  9211. if (CanvasGraphics.prototype[op] !== undefined) {
  9212. CanvasGraphics.prototype[OPS[op]] = CanvasGraphics.prototype[op];
  9213. }
  9214. }
  9215. ;// ./src/display/worker_options.js
  9216. class GlobalWorkerOptions {
  9217. static #port = null;
  9218. static #src = "";
  9219. static get workerPort() {
  9220. return this.#port;
  9221. }
  9222. static set workerPort(val) {
  9223. if (!(typeof Worker !== "undefined" && val instanceof Worker) && val !== null) {
  9224. throw new Error("Invalid `workerPort` type.");
  9225. }
  9226. this.#port = val;
  9227. }
  9228. static get workerSrc() {
  9229. return this.#src;
  9230. }
  9231. static set workerSrc(val) {
  9232. if (typeof val !== "string") {
  9233. throw new Error("Invalid `workerSrc` type.");
  9234. }
  9235. this.#src = val;
  9236. }
  9237. }
  9238. ;// ./src/display/metadata.js
  9239. class Metadata {
  9240. #map;
  9241. #data;
  9242. constructor({
  9243. parsedData,
  9244. rawData
  9245. }) {
  9246. this.#map = parsedData;
  9247. this.#data = rawData;
  9248. }
  9249. getRaw() {
  9250. return this.#data;
  9251. }
  9252. get(name) {
  9253. return this.#map.get(name) ?? null;
  9254. }
  9255. [Symbol.iterator]() {
  9256. return this.#map.entries();
  9257. }
  9258. }
  9259. ;// ./src/display/optional_content_config.js
  9260. const INTERNAL = Symbol("INTERNAL");
  9261. class OptionalContentGroup {
  9262. #isDisplay = false;
  9263. #isPrint = false;
  9264. #userSet = false;
  9265. #visible = true;
  9266. constructor(renderingIntent, {
  9267. name,
  9268. intent,
  9269. usage,
  9270. rbGroups
  9271. }) {
  9272. this.#isDisplay = !!(renderingIntent & RenderingIntentFlag.DISPLAY);
  9273. this.#isPrint = !!(renderingIntent & RenderingIntentFlag.PRINT);
  9274. this.name = name;
  9275. this.intent = intent;
  9276. this.usage = usage;
  9277. this.rbGroups = rbGroups;
  9278. }
  9279. get visible() {
  9280. if (this.#userSet) {
  9281. return this.#visible;
  9282. }
  9283. if (!this.#visible) {
  9284. return false;
  9285. }
  9286. const {
  9287. print,
  9288. view
  9289. } = this.usage;
  9290. if (this.#isDisplay) {
  9291. return view?.viewState !== "OFF";
  9292. } else if (this.#isPrint) {
  9293. return print?.printState !== "OFF";
  9294. }
  9295. return true;
  9296. }
  9297. _setVisible(internal, visible, userSet = false) {
  9298. if (internal !== INTERNAL) {
  9299. unreachable("Internal method `_setVisible` called.");
  9300. }
  9301. this.#userSet = userSet;
  9302. this.#visible = visible;
  9303. }
  9304. }
  9305. class OptionalContentConfig {
  9306. #cachedGetHash = null;
  9307. #groups = new Map();
  9308. #initialHash = null;
  9309. #order = null;
  9310. constructor(data, renderingIntent = RenderingIntentFlag.DISPLAY) {
  9311. this.renderingIntent = renderingIntent;
  9312. this.name = null;
  9313. this.creator = null;
  9314. if (data === null) {
  9315. return;
  9316. }
  9317. this.name = data.name;
  9318. this.creator = data.creator;
  9319. this.#order = data.order;
  9320. for (const group of data.groups) {
  9321. this.#groups.set(group.id, new OptionalContentGroup(renderingIntent, group));
  9322. }
  9323. if (data.baseState === "OFF") {
  9324. for (const group of this.#groups.values()) {
  9325. group._setVisible(INTERNAL, false);
  9326. }
  9327. }
  9328. for (const on of data.on) {
  9329. this.#groups.get(on)._setVisible(INTERNAL, true);
  9330. }
  9331. for (const off of data.off) {
  9332. this.#groups.get(off)._setVisible(INTERNAL, false);
  9333. }
  9334. this.#initialHash = this.getHash();
  9335. }
  9336. #evaluateVisibilityExpression(array) {
  9337. const length = array.length;
  9338. if (length < 2) {
  9339. return true;
  9340. }
  9341. const operator = array[0];
  9342. for (let i = 1; i < length; i++) {
  9343. const element = array[i];
  9344. let state;
  9345. if (Array.isArray(element)) {
  9346. state = this.#evaluateVisibilityExpression(element);
  9347. } else if (this.#groups.has(element)) {
  9348. state = this.#groups.get(element).visible;
  9349. } else {
  9350. warn(`Optional content group not found: ${element}`);
  9351. return true;
  9352. }
  9353. switch (operator) {
  9354. case "And":
  9355. if (!state) {
  9356. return false;
  9357. }
  9358. break;
  9359. case "Or":
  9360. if (state) {
  9361. return true;
  9362. }
  9363. break;
  9364. case "Not":
  9365. return !state;
  9366. default:
  9367. return true;
  9368. }
  9369. }
  9370. return operator === "And";
  9371. }
  9372. isVisible(group) {
  9373. if (this.#groups.size === 0) {
  9374. return true;
  9375. }
  9376. if (!group) {
  9377. info("Optional content group not defined.");
  9378. return true;
  9379. }
  9380. if (group.type === "OCG") {
  9381. if (!this.#groups.has(group.id)) {
  9382. warn(`Optional content group not found: ${group.id}`);
  9383. return true;
  9384. }
  9385. return this.#groups.get(group.id).visible;
  9386. } else if (group.type === "OCMD") {
  9387. if (group.expression) {
  9388. return this.#evaluateVisibilityExpression(group.expression);
  9389. }
  9390. if (!group.policy || group.policy === "AnyOn") {
  9391. for (const id of group.ids) {
  9392. if (!this.#groups.has(id)) {
  9393. warn(`Optional content group not found: ${id}`);
  9394. return true;
  9395. }
  9396. if (this.#groups.get(id).visible) {
  9397. return true;
  9398. }
  9399. }
  9400. return false;
  9401. } else if (group.policy === "AllOn") {
  9402. for (const id of group.ids) {
  9403. if (!this.#groups.has(id)) {
  9404. warn(`Optional content group not found: ${id}`);
  9405. return true;
  9406. }
  9407. if (!this.#groups.get(id).visible) {
  9408. return false;
  9409. }
  9410. }
  9411. return true;
  9412. } else if (group.policy === "AnyOff") {
  9413. for (const id of group.ids) {
  9414. if (!this.#groups.has(id)) {
  9415. warn(`Optional content group not found: ${id}`);
  9416. return true;
  9417. }
  9418. if (!this.#groups.get(id).visible) {
  9419. return true;
  9420. }
  9421. }
  9422. return false;
  9423. } else if (group.policy === "AllOff") {
  9424. for (const id of group.ids) {
  9425. if (!this.#groups.has(id)) {
  9426. warn(`Optional content group not found: ${id}`);
  9427. return true;
  9428. }
  9429. if (this.#groups.get(id).visible) {
  9430. return false;
  9431. }
  9432. }
  9433. return true;
  9434. }
  9435. warn(`Unknown optional content policy ${group.policy}.`);
  9436. return true;
  9437. }
  9438. warn(`Unknown group type ${group.type}.`);
  9439. return true;
  9440. }
  9441. setVisibility(id, visible = true, preserveRB = true) {
  9442. const group = this.#groups.get(id);
  9443. if (!group) {
  9444. warn(`Optional content group not found: ${id}`);
  9445. return;
  9446. }
  9447. if (preserveRB && visible && group.rbGroups.length) {
  9448. for (const rbGroup of group.rbGroups) {
  9449. for (const otherId of rbGroup) {
  9450. if (otherId !== id) {
  9451. this.#groups.get(otherId)?._setVisible(INTERNAL, false, true);
  9452. }
  9453. }
  9454. }
  9455. }
  9456. group._setVisible(INTERNAL, !!visible, true);
  9457. this.#cachedGetHash = null;
  9458. }
  9459. setOCGState({
  9460. state,
  9461. preserveRB
  9462. }) {
  9463. let operator;
  9464. for (const elem of state) {
  9465. switch (elem) {
  9466. case "ON":
  9467. case "OFF":
  9468. case "Toggle":
  9469. operator = elem;
  9470. continue;
  9471. }
  9472. const group = this.#groups.get(elem);
  9473. if (!group) {
  9474. continue;
  9475. }
  9476. switch (operator) {
  9477. case "ON":
  9478. this.setVisibility(elem, true, preserveRB);
  9479. break;
  9480. case "OFF":
  9481. this.setVisibility(elem, false, preserveRB);
  9482. break;
  9483. case "Toggle":
  9484. this.setVisibility(elem, !group.visible, preserveRB);
  9485. break;
  9486. }
  9487. }
  9488. this.#cachedGetHash = null;
  9489. }
  9490. get hasInitialVisibility() {
  9491. return this.#initialHash === null || this.getHash() === this.#initialHash;
  9492. }
  9493. getOrder() {
  9494. if (!this.#groups.size) {
  9495. return null;
  9496. }
  9497. if (this.#order) {
  9498. return this.#order.slice();
  9499. }
  9500. return [...this.#groups.keys()];
  9501. }
  9502. getGroup(id) {
  9503. return this.#groups.get(id) || null;
  9504. }
  9505. getHash() {
  9506. if (this.#cachedGetHash !== null) {
  9507. return this.#cachedGetHash;
  9508. }
  9509. const hash = new MurmurHash3_64();
  9510. for (const [id, group] of this.#groups) {
  9511. hash.update(`${id}:${group.visible}`);
  9512. }
  9513. return this.#cachedGetHash = hash.hexdigest();
  9514. }
  9515. [Symbol.iterator]() {
  9516. return this.#groups.entries();
  9517. }
  9518. }
  9519. ;// ./src/display/transport_stream.js
  9520. class PDFDataTransportStream {
  9521. constructor(pdfDataRangeTransport, {
  9522. disableRange = false,
  9523. disableStream = false
  9524. }) {
  9525. assert(pdfDataRangeTransport, 'PDFDataTransportStream - missing required "pdfDataRangeTransport" argument.');
  9526. const {
  9527. length,
  9528. initialData,
  9529. progressiveDone,
  9530. contentDispositionFilename
  9531. } = pdfDataRangeTransport;
  9532. this._queuedChunks = [];
  9533. this._progressiveDone = progressiveDone;
  9534. this._contentDispositionFilename = contentDispositionFilename;
  9535. if (initialData?.length > 0) {
  9536. const buffer = initialData instanceof Uint8Array && initialData.byteLength === initialData.buffer.byteLength ? initialData.buffer : new Uint8Array(initialData).buffer;
  9537. this._queuedChunks.push(buffer);
  9538. }
  9539. this._pdfDataRangeTransport = pdfDataRangeTransport;
  9540. this._isStreamingSupported = !disableStream;
  9541. this._isRangeSupported = !disableRange;
  9542. this._contentLength = length;
  9543. this._fullRequestReader = null;
  9544. this._rangeReaders = [];
  9545. pdfDataRangeTransport.addRangeListener((begin, chunk) => {
  9546. this._onReceiveData({
  9547. begin,
  9548. chunk
  9549. });
  9550. });
  9551. pdfDataRangeTransport.addProgressListener((loaded, total) => {
  9552. this._onProgress({
  9553. loaded,
  9554. total
  9555. });
  9556. });
  9557. pdfDataRangeTransport.addProgressiveReadListener(chunk => {
  9558. this._onReceiveData({
  9559. chunk
  9560. });
  9561. });
  9562. pdfDataRangeTransport.addProgressiveDoneListener(() => {
  9563. this._onProgressiveDone();
  9564. });
  9565. pdfDataRangeTransport.transportReady();
  9566. }
  9567. _onReceiveData({
  9568. begin,
  9569. chunk
  9570. }) {
  9571. const buffer = chunk instanceof Uint8Array && chunk.byteLength === chunk.buffer.byteLength ? chunk.buffer : new Uint8Array(chunk).buffer;
  9572. if (begin === undefined) {
  9573. if (this._fullRequestReader) {
  9574. this._fullRequestReader._enqueue(buffer);
  9575. } else {
  9576. this._queuedChunks.push(buffer);
  9577. }
  9578. } else {
  9579. const found = this._rangeReaders.some(function (rangeReader) {
  9580. if (rangeReader._begin !== begin) {
  9581. return false;
  9582. }
  9583. rangeReader._enqueue(buffer);
  9584. return true;
  9585. });
  9586. assert(found, "_onReceiveData - no `PDFDataTransportStreamRangeReader` instance found.");
  9587. }
  9588. }
  9589. get _progressiveDataLength() {
  9590. return this._fullRequestReader?._loaded ?? 0;
  9591. }
  9592. _onProgress(evt) {
  9593. if (evt.total === undefined) {
  9594. this._rangeReaders[0]?.onProgress?.({
  9595. loaded: evt.loaded
  9596. });
  9597. } else {
  9598. this._fullRequestReader?.onProgress?.({
  9599. loaded: evt.loaded,
  9600. total: evt.total
  9601. });
  9602. }
  9603. }
  9604. _onProgressiveDone() {
  9605. this._fullRequestReader?.progressiveDone();
  9606. this._progressiveDone = true;
  9607. }
  9608. _removeRangeReader(reader) {
  9609. const i = this._rangeReaders.indexOf(reader);
  9610. if (i >= 0) {
  9611. this._rangeReaders.splice(i, 1);
  9612. }
  9613. }
  9614. getFullReader() {
  9615. assert(!this._fullRequestReader, "PDFDataTransportStream.getFullReader can only be called once.");
  9616. const queuedChunks = this._queuedChunks;
  9617. this._queuedChunks = null;
  9618. return new PDFDataTransportStreamReader(this, queuedChunks, this._progressiveDone, this._contentDispositionFilename);
  9619. }
  9620. getRangeReader(begin, end) {
  9621. if (end <= this._progressiveDataLength) {
  9622. return null;
  9623. }
  9624. const reader = new PDFDataTransportStreamRangeReader(this, begin, end);
  9625. this._pdfDataRangeTransport.requestDataRange(begin, end);
  9626. this._rangeReaders.push(reader);
  9627. return reader;
  9628. }
  9629. cancelAllRequests(reason) {
  9630. this._fullRequestReader?.cancel(reason);
  9631. for (const reader of this._rangeReaders.slice(0)) {
  9632. reader.cancel(reason);
  9633. }
  9634. this._pdfDataRangeTransport.abort();
  9635. }
  9636. }
  9637. class PDFDataTransportStreamReader {
  9638. constructor(stream, queuedChunks, progressiveDone = false, contentDispositionFilename = null) {
  9639. this._stream = stream;
  9640. this._done = progressiveDone || false;
  9641. this._filename = isPdfFile(contentDispositionFilename) ? contentDispositionFilename : null;
  9642. this._queuedChunks = queuedChunks || [];
  9643. this._loaded = 0;
  9644. for (const chunk of this._queuedChunks) {
  9645. this._loaded += chunk.byteLength;
  9646. }
  9647. this._requests = [];
  9648. this._headersReady = Promise.resolve();
  9649. stream._fullRequestReader = this;
  9650. this.onProgress = null;
  9651. }
  9652. _enqueue(chunk) {
  9653. if (this._done) {
  9654. return;
  9655. }
  9656. if (this._requests.length > 0) {
  9657. const requestCapability = this._requests.shift();
  9658. requestCapability.resolve({
  9659. value: chunk,
  9660. done: false
  9661. });
  9662. } else {
  9663. this._queuedChunks.push(chunk);
  9664. }
  9665. this._loaded += chunk.byteLength;
  9666. }
  9667. get headersReady() {
  9668. return this._headersReady;
  9669. }
  9670. get filename() {
  9671. return this._filename;
  9672. }
  9673. get isRangeSupported() {
  9674. return this._stream._isRangeSupported;
  9675. }
  9676. get isStreamingSupported() {
  9677. return this._stream._isStreamingSupported;
  9678. }
  9679. get contentLength() {
  9680. return this._stream._contentLength;
  9681. }
  9682. async read() {
  9683. if (this._queuedChunks.length > 0) {
  9684. const chunk = this._queuedChunks.shift();
  9685. return {
  9686. value: chunk,
  9687. done: false
  9688. };
  9689. }
  9690. if (this._done) {
  9691. return {
  9692. value: undefined,
  9693. done: true
  9694. };
  9695. }
  9696. const requestCapability = Promise.withResolvers();
  9697. this._requests.push(requestCapability);
  9698. return requestCapability.promise;
  9699. }
  9700. cancel(reason) {
  9701. this._done = true;
  9702. for (const requestCapability of this._requests) {
  9703. requestCapability.resolve({
  9704. value: undefined,
  9705. done: true
  9706. });
  9707. }
  9708. this._requests.length = 0;
  9709. }
  9710. progressiveDone() {
  9711. if (this._done) {
  9712. return;
  9713. }
  9714. this._done = true;
  9715. }
  9716. }
  9717. class PDFDataTransportStreamRangeReader {
  9718. constructor(stream, begin, end) {
  9719. this._stream = stream;
  9720. this._begin = begin;
  9721. this._end = end;
  9722. this._queuedChunk = null;
  9723. this._requests = [];
  9724. this._done = false;
  9725. this.onProgress = null;
  9726. }
  9727. _enqueue(chunk) {
  9728. if (this._done) {
  9729. return;
  9730. }
  9731. if (this._requests.length === 0) {
  9732. this._queuedChunk = chunk;
  9733. } else {
  9734. const requestsCapability = this._requests.shift();
  9735. requestsCapability.resolve({
  9736. value: chunk,
  9737. done: false
  9738. });
  9739. for (const requestCapability of this._requests) {
  9740. requestCapability.resolve({
  9741. value: undefined,
  9742. done: true
  9743. });
  9744. }
  9745. this._requests.length = 0;
  9746. }
  9747. this._done = true;
  9748. this._stream._removeRangeReader(this);
  9749. }
  9750. get isStreamingSupported() {
  9751. return false;
  9752. }
  9753. async read() {
  9754. if (this._queuedChunk) {
  9755. const chunk = this._queuedChunk;
  9756. this._queuedChunk = null;
  9757. return {
  9758. value: chunk,
  9759. done: false
  9760. };
  9761. }
  9762. if (this._done) {
  9763. return {
  9764. value: undefined,
  9765. done: true
  9766. };
  9767. }
  9768. const requestCapability = Promise.withResolvers();
  9769. this._requests.push(requestCapability);
  9770. return requestCapability.promise;
  9771. }
  9772. cancel(reason) {
  9773. this._done = true;
  9774. for (const requestCapability of this._requests) {
  9775. requestCapability.resolve({
  9776. value: undefined,
  9777. done: true
  9778. });
  9779. }
  9780. this._requests.length = 0;
  9781. this._stream._removeRangeReader(this);
  9782. }
  9783. }
  9784. ;// ./src/display/content_disposition.js
  9785. function getFilenameFromContentDispositionHeader(contentDisposition) {
  9786. let needsEncodingFixup = true;
  9787. let tmp = toParamRegExp("filename\\*", "i").exec(contentDisposition);
  9788. if (tmp) {
  9789. tmp = tmp[1];
  9790. let filename = rfc2616unquote(tmp);
  9791. filename = unescape(filename);
  9792. filename = rfc5987decode(filename);
  9793. filename = rfc2047decode(filename);
  9794. return fixupEncoding(filename);
  9795. }
  9796. tmp = rfc2231getparam(contentDisposition);
  9797. if (tmp) {
  9798. const filename = rfc2047decode(tmp);
  9799. return fixupEncoding(filename);
  9800. }
  9801. tmp = toParamRegExp("filename", "i").exec(contentDisposition);
  9802. if (tmp) {
  9803. tmp = tmp[1];
  9804. let filename = rfc2616unquote(tmp);
  9805. filename = rfc2047decode(filename);
  9806. return fixupEncoding(filename);
  9807. }
  9808. function toParamRegExp(attributePattern, flags) {
  9809. return new RegExp("(?:^|;)\\s*" + attributePattern + "\\s*=\\s*" + "(" + '[^";\\s][^;\\s]*' + "|" + '"(?:[^"\\\\]|\\\\"?)+"?' + ")", flags);
  9810. }
  9811. function textdecode(encoding, value) {
  9812. if (encoding) {
  9813. if (!/^[\x00-\xFF]+$/.test(value)) {
  9814. return value;
  9815. }
  9816. try {
  9817. const decoder = new TextDecoder(encoding, {
  9818. fatal: true
  9819. });
  9820. const buffer = stringToBytes(value);
  9821. value = decoder.decode(buffer);
  9822. needsEncodingFixup = false;
  9823. } catch {}
  9824. }
  9825. return value;
  9826. }
  9827. function fixupEncoding(value) {
  9828. if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
  9829. value = textdecode("utf-8", value);
  9830. if (needsEncodingFixup) {
  9831. value = textdecode("iso-8859-1", value);
  9832. }
  9833. }
  9834. return value;
  9835. }
  9836. function rfc2231getparam(contentDispositionStr) {
  9837. const matches = [];
  9838. let match;
  9839. const iter = toParamRegExp("filename\\*((?!0\\d)\\d+)(\\*?)", "ig");
  9840. while ((match = iter.exec(contentDispositionStr)) !== null) {
  9841. let [, n, quot, part] = match;
  9842. n = parseInt(n, 10);
  9843. if (n in matches) {
  9844. if (n === 0) {
  9845. break;
  9846. }
  9847. continue;
  9848. }
  9849. matches[n] = [quot, part];
  9850. }
  9851. const parts = [];
  9852. for (let n = 0; n < matches.length; ++n) {
  9853. if (!(n in matches)) {
  9854. break;
  9855. }
  9856. let [quot, part] = matches[n];
  9857. part = rfc2616unquote(part);
  9858. if (quot) {
  9859. part = unescape(part);
  9860. if (n === 0) {
  9861. part = rfc5987decode(part);
  9862. }
  9863. }
  9864. parts.push(part);
  9865. }
  9866. return parts.join("");
  9867. }
  9868. function rfc2616unquote(value) {
  9869. if (value.startsWith('"')) {
  9870. const parts = value.slice(1).split('\\"');
  9871. for (let i = 0; i < parts.length; ++i) {
  9872. const quotindex = parts[i].indexOf('"');
  9873. if (quotindex !== -1) {
  9874. parts[i] = parts[i].slice(0, quotindex);
  9875. parts.length = i + 1;
  9876. }
  9877. parts[i] = parts[i].replaceAll(/\\(.)/g, "$1");
  9878. }
  9879. value = parts.join('"');
  9880. }
  9881. return value;
  9882. }
  9883. function rfc5987decode(extvalue) {
  9884. const encodingend = extvalue.indexOf("'");
  9885. if (encodingend === -1) {
  9886. return extvalue;
  9887. }
  9888. const encoding = extvalue.slice(0, encodingend);
  9889. const langvalue = extvalue.slice(encodingend + 1);
  9890. const value = langvalue.replace(/^[^']*'/, "");
  9891. return textdecode(encoding, value);
  9892. }
  9893. function rfc2047decode(value) {
  9894. if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {
  9895. return value;
  9896. }
  9897. return value.replaceAll(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (matches, charset, encoding, text) {
  9898. if (encoding === "q" || encoding === "Q") {
  9899. text = text.replaceAll("_", " ");
  9900. text = text.replaceAll(/=([0-9a-fA-F]{2})/g, function (match, hex) {
  9901. return String.fromCharCode(parseInt(hex, 16));
  9902. });
  9903. return textdecode(charset, text);
  9904. }
  9905. try {
  9906. text = atob(text);
  9907. } catch {}
  9908. return textdecode(charset, text);
  9909. });
  9910. }
  9911. return "";
  9912. }
  9913. ;// ./src/display/network_utils.js
  9914. function createHeaders(isHttp, httpHeaders) {
  9915. const headers = new Headers();
  9916. if (!isHttp || !httpHeaders || typeof httpHeaders !== "object") {
  9917. return headers;
  9918. }
  9919. for (const key in httpHeaders) {
  9920. const val = httpHeaders[key];
  9921. if (val !== undefined) {
  9922. headers.append(key, val);
  9923. }
  9924. }
  9925. return headers;
  9926. }
  9927. function getResponseOrigin(url) {
  9928. return URL.parse(url)?.origin ?? null;
  9929. }
  9930. function validateRangeRequestCapabilities({
  9931. responseHeaders,
  9932. isHttp,
  9933. rangeChunkSize,
  9934. disableRange
  9935. }) {
  9936. const returnValues = {
  9937. allowRangeRequests: false,
  9938. suggestedLength: undefined
  9939. };
  9940. const length = parseInt(responseHeaders.get("Content-Length"), 10);
  9941. if (!Number.isInteger(length)) {
  9942. return returnValues;
  9943. }
  9944. returnValues.suggestedLength = length;
  9945. if (length <= 2 * rangeChunkSize) {
  9946. return returnValues;
  9947. }
  9948. if (disableRange || !isHttp) {
  9949. return returnValues;
  9950. }
  9951. if (responseHeaders.get("Accept-Ranges") !== "bytes") {
  9952. return returnValues;
  9953. }
  9954. const contentEncoding = responseHeaders.get("Content-Encoding") || "identity";
  9955. if (contentEncoding !== "identity") {
  9956. return returnValues;
  9957. }
  9958. returnValues.allowRangeRequests = true;
  9959. return returnValues;
  9960. }
  9961. function extractFilenameFromHeader(responseHeaders) {
  9962. const contentDisposition = responseHeaders.get("Content-Disposition");
  9963. if (contentDisposition) {
  9964. let filename = getFilenameFromContentDispositionHeader(contentDisposition);
  9965. if (filename.includes("%")) {
  9966. try {
  9967. filename = decodeURIComponent(filename);
  9968. } catch {}
  9969. }
  9970. if (isPdfFile(filename)) {
  9971. return filename;
  9972. }
  9973. }
  9974. return null;
  9975. }
  9976. function createResponseError(status, url) {
  9977. return new ResponseException(`Unexpected server response (${status}) while retrieving PDF "${url}".`, status, status === 404 || status === 0 && url.startsWith("file:"));
  9978. }
  9979. function validateResponseStatus(status) {
  9980. return status === 200 || status === 206;
  9981. }
  9982. ;// ./src/display/fetch_stream.js
  9983. function createFetchOptions(headers, withCredentials, abortController) {
  9984. return {
  9985. method: "GET",
  9986. headers,
  9987. signal: abortController.signal,
  9988. mode: "cors",
  9989. credentials: withCredentials ? "include" : "same-origin",
  9990. redirect: "follow"
  9991. };
  9992. }
  9993. function getArrayBuffer(val) {
  9994. if (val instanceof Uint8Array) {
  9995. return val.buffer;
  9996. }
  9997. if (val instanceof ArrayBuffer) {
  9998. return val;
  9999. }
  10000. warn(`getArrayBuffer - unexpected data format: ${val}`);
  10001. return new Uint8Array(val).buffer;
  10002. }
  10003. class PDFFetchStream {
  10004. _responseOrigin = null;
  10005. constructor(source) {
  10006. this.source = source;
  10007. this.isHttp = /^https?:/i.test(source.url);
  10008. this.headers = createHeaders(this.isHttp, source.httpHeaders);
  10009. this._fullRequestReader = null;
  10010. this._rangeRequestReaders = [];
  10011. }
  10012. get _progressiveDataLength() {
  10013. return this._fullRequestReader?._loaded ?? 0;
  10014. }
  10015. getFullReader() {
  10016. assert(!this._fullRequestReader, "PDFFetchStream.getFullReader can only be called once.");
  10017. this._fullRequestReader = new PDFFetchStreamReader(this);
  10018. return this._fullRequestReader;
  10019. }
  10020. getRangeReader(begin, end) {
  10021. if (end <= this._progressiveDataLength) {
  10022. return null;
  10023. }
  10024. const reader = new PDFFetchStreamRangeReader(this, begin, end);
  10025. this._rangeRequestReaders.push(reader);
  10026. return reader;
  10027. }
  10028. cancelAllRequests(reason) {
  10029. this._fullRequestReader?.cancel(reason);
  10030. for (const reader of this._rangeRequestReaders.slice(0)) {
  10031. reader.cancel(reason);
  10032. }
  10033. }
  10034. }
  10035. class PDFFetchStreamReader {
  10036. constructor(stream) {
  10037. this._stream = stream;
  10038. this._reader = null;
  10039. this._loaded = 0;
  10040. this._filename = null;
  10041. const source = stream.source;
  10042. this._withCredentials = source.withCredentials || false;
  10043. this._contentLength = source.length;
  10044. this._headersCapability = Promise.withResolvers();
  10045. this._disableRange = source.disableRange || false;
  10046. this._rangeChunkSize = source.rangeChunkSize;
  10047. if (!this._rangeChunkSize && !this._disableRange) {
  10048. this._disableRange = true;
  10049. }
  10050. this._abortController = new AbortController();
  10051. this._isStreamingSupported = !source.disableStream;
  10052. this._isRangeSupported = !source.disableRange;
  10053. const headers = new Headers(stream.headers);
  10054. const url = source.url;
  10055. fetch(url, createFetchOptions(headers, this._withCredentials, this._abortController)).then(response => {
  10056. stream._responseOrigin = getResponseOrigin(response.url);
  10057. if (!validateResponseStatus(response.status)) {
  10058. throw createResponseError(response.status, url);
  10059. }
  10060. this._reader = response.body.getReader();
  10061. this._headersCapability.resolve();
  10062. const responseHeaders = response.headers;
  10063. const {
  10064. allowRangeRequests,
  10065. suggestedLength
  10066. } = validateRangeRequestCapabilities({
  10067. responseHeaders,
  10068. isHttp: stream.isHttp,
  10069. rangeChunkSize: this._rangeChunkSize,
  10070. disableRange: this._disableRange
  10071. });
  10072. this._isRangeSupported = allowRangeRequests;
  10073. this._contentLength = suggestedLength || this._contentLength;
  10074. this._filename = extractFilenameFromHeader(responseHeaders);
  10075. if (!this._isStreamingSupported && this._isRangeSupported) {
  10076. this.cancel(new AbortException("Streaming is disabled."));
  10077. }
  10078. }).catch(this._headersCapability.reject);
  10079. this.onProgress = null;
  10080. }
  10081. get headersReady() {
  10082. return this._headersCapability.promise;
  10083. }
  10084. get filename() {
  10085. return this._filename;
  10086. }
  10087. get contentLength() {
  10088. return this._contentLength;
  10089. }
  10090. get isRangeSupported() {
  10091. return this._isRangeSupported;
  10092. }
  10093. get isStreamingSupported() {
  10094. return this._isStreamingSupported;
  10095. }
  10096. async read() {
  10097. await this._headersCapability.promise;
  10098. const {
  10099. value,
  10100. done
  10101. } = await this._reader.read();
  10102. if (done) {
  10103. return {
  10104. value,
  10105. done
  10106. };
  10107. }
  10108. this._loaded += value.byteLength;
  10109. this.onProgress?.({
  10110. loaded: this._loaded,
  10111. total: this._contentLength
  10112. });
  10113. return {
  10114. value: getArrayBuffer(value),
  10115. done: false
  10116. };
  10117. }
  10118. cancel(reason) {
  10119. this._reader?.cancel(reason);
  10120. this._abortController.abort();
  10121. }
  10122. }
  10123. class PDFFetchStreamRangeReader {
  10124. constructor(stream, begin, end) {
  10125. this._stream = stream;
  10126. this._reader = null;
  10127. this._loaded = 0;
  10128. const source = stream.source;
  10129. this._withCredentials = source.withCredentials || false;
  10130. this._readCapability = Promise.withResolvers();
  10131. this._isStreamingSupported = !source.disableStream;
  10132. this._abortController = new AbortController();
  10133. const headers = new Headers(stream.headers);
  10134. headers.append("Range", `bytes=${begin}-${end - 1}`);
  10135. const url = source.url;
  10136. fetch(url, createFetchOptions(headers, this._withCredentials, this._abortController)).then(response => {
  10137. const responseOrigin = getResponseOrigin(response.url);
  10138. if (responseOrigin !== stream._responseOrigin) {
  10139. throw new Error(`Expected range response-origin "${responseOrigin}" to match "${stream._responseOrigin}".`);
  10140. }
  10141. if (!validateResponseStatus(response.status)) {
  10142. throw createResponseError(response.status, url);
  10143. }
  10144. this._readCapability.resolve();
  10145. this._reader = response.body.getReader();
  10146. }).catch(this._readCapability.reject);
  10147. this.onProgress = null;
  10148. }
  10149. get isStreamingSupported() {
  10150. return this._isStreamingSupported;
  10151. }
  10152. async read() {
  10153. await this._readCapability.promise;
  10154. const {
  10155. value,
  10156. done
  10157. } = await this._reader.read();
  10158. if (done) {
  10159. return {
  10160. value,
  10161. done
  10162. };
  10163. }
  10164. this._loaded += value.byteLength;
  10165. this.onProgress?.({
  10166. loaded: this._loaded
  10167. });
  10168. return {
  10169. value: getArrayBuffer(value),
  10170. done: false
  10171. };
  10172. }
  10173. cancel(reason) {
  10174. this._reader?.cancel(reason);
  10175. this._abortController.abort();
  10176. }
  10177. }
  10178. ;// ./src/display/network.js
  10179. const OK_RESPONSE = 200;
  10180. const PARTIAL_CONTENT_RESPONSE = 206;
  10181. function network_getArrayBuffer(xhr) {
  10182. const data = xhr.response;
  10183. if (typeof data !== "string") {
  10184. return data;
  10185. }
  10186. return stringToBytes(data).buffer;
  10187. }
  10188. class NetworkManager {
  10189. _responseOrigin = null;
  10190. constructor({
  10191. url,
  10192. httpHeaders,
  10193. withCredentials
  10194. }) {
  10195. this.url = url;
  10196. this.isHttp = /^https?:/i.test(url);
  10197. this.headers = createHeaders(this.isHttp, httpHeaders);
  10198. this.withCredentials = withCredentials || false;
  10199. this.currXhrId = 0;
  10200. this.pendingRequests = Object.create(null);
  10201. }
  10202. request(args) {
  10203. const xhr = new XMLHttpRequest();
  10204. const xhrId = this.currXhrId++;
  10205. const pendingRequest = this.pendingRequests[xhrId] = {
  10206. xhr
  10207. };
  10208. xhr.open("GET", this.url);
  10209. xhr.withCredentials = this.withCredentials;
  10210. for (const [key, val] of this.headers) {
  10211. xhr.setRequestHeader(key, val);
  10212. }
  10213. if (this.isHttp && "begin" in args && "end" in args) {
  10214. xhr.setRequestHeader("Range", `bytes=${args.begin}-${args.end - 1}`);
  10215. pendingRequest.expectedStatus = PARTIAL_CONTENT_RESPONSE;
  10216. } else {
  10217. pendingRequest.expectedStatus = OK_RESPONSE;
  10218. }
  10219. xhr.responseType = "arraybuffer";
  10220. assert(args.onError, "Expected `onError` callback to be provided.");
  10221. xhr.onerror = () => {
  10222. args.onError(xhr.status);
  10223. };
  10224. xhr.onreadystatechange = this.onStateChange.bind(this, xhrId);
  10225. xhr.onprogress = this.onProgress.bind(this, xhrId);
  10226. pendingRequest.onHeadersReceived = args.onHeadersReceived;
  10227. pendingRequest.onDone = args.onDone;
  10228. pendingRequest.onError = args.onError;
  10229. pendingRequest.onProgress = args.onProgress;
  10230. xhr.send(null);
  10231. return xhrId;
  10232. }
  10233. onProgress(xhrId, evt) {
  10234. const pendingRequest = this.pendingRequests[xhrId];
  10235. if (!pendingRequest) {
  10236. return;
  10237. }
  10238. pendingRequest.onProgress?.(evt);
  10239. }
  10240. onStateChange(xhrId, evt) {
  10241. const pendingRequest = this.pendingRequests[xhrId];
  10242. if (!pendingRequest) {
  10243. return;
  10244. }
  10245. const xhr = pendingRequest.xhr;
  10246. if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) {
  10247. pendingRequest.onHeadersReceived();
  10248. delete pendingRequest.onHeadersReceived;
  10249. }
  10250. if (xhr.readyState !== 4) {
  10251. return;
  10252. }
  10253. if (!(xhrId in this.pendingRequests)) {
  10254. return;
  10255. }
  10256. delete this.pendingRequests[xhrId];
  10257. if (xhr.status === 0 && this.isHttp) {
  10258. pendingRequest.onError(xhr.status);
  10259. return;
  10260. }
  10261. const xhrStatus = xhr.status || OK_RESPONSE;
  10262. const ok_response_on_range_request = xhrStatus === OK_RESPONSE && pendingRequest.expectedStatus === PARTIAL_CONTENT_RESPONSE;
  10263. if (!ok_response_on_range_request && xhrStatus !== pendingRequest.expectedStatus) {
  10264. pendingRequest.onError(xhr.status);
  10265. return;
  10266. }
  10267. const chunk = network_getArrayBuffer(xhr);
  10268. if (xhrStatus === PARTIAL_CONTENT_RESPONSE) {
  10269. const rangeHeader = xhr.getResponseHeader("Content-Range");
  10270. const matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader);
  10271. if (matches) {
  10272. pendingRequest.onDone({
  10273. begin: parseInt(matches[1], 10),
  10274. chunk
  10275. });
  10276. } else {
  10277. warn(`Missing or invalid "Content-Range" header.`);
  10278. pendingRequest.onError(0);
  10279. }
  10280. } else if (chunk) {
  10281. pendingRequest.onDone({
  10282. begin: 0,
  10283. chunk
  10284. });
  10285. } else {
  10286. pendingRequest.onError(xhr.status);
  10287. }
  10288. }
  10289. getRequestXhr(xhrId) {
  10290. return this.pendingRequests[xhrId].xhr;
  10291. }
  10292. isPendingRequest(xhrId) {
  10293. return xhrId in this.pendingRequests;
  10294. }
  10295. abortRequest(xhrId) {
  10296. const xhr = this.pendingRequests[xhrId].xhr;
  10297. delete this.pendingRequests[xhrId];
  10298. xhr.abort();
  10299. }
  10300. }
  10301. class PDFNetworkStream {
  10302. constructor(source) {
  10303. this._source = source;
  10304. this._manager = new NetworkManager(source);
  10305. this._rangeChunkSize = source.rangeChunkSize;
  10306. this._fullRequestReader = null;
  10307. this._rangeRequestReaders = [];
  10308. }
  10309. _onRangeRequestReaderClosed(reader) {
  10310. const i = this._rangeRequestReaders.indexOf(reader);
  10311. if (i >= 0) {
  10312. this._rangeRequestReaders.splice(i, 1);
  10313. }
  10314. }
  10315. getFullReader() {
  10316. assert(!this._fullRequestReader, "PDFNetworkStream.getFullReader can only be called once.");
  10317. this._fullRequestReader = new PDFNetworkStreamFullRequestReader(this._manager, this._source);
  10318. return this._fullRequestReader;
  10319. }
  10320. getRangeReader(begin, end) {
  10321. const reader = new PDFNetworkStreamRangeRequestReader(this._manager, begin, end);
  10322. reader.onClosed = this._onRangeRequestReaderClosed.bind(this);
  10323. this._rangeRequestReaders.push(reader);
  10324. return reader;
  10325. }
  10326. cancelAllRequests(reason) {
  10327. this._fullRequestReader?.cancel(reason);
  10328. for (const reader of this._rangeRequestReaders.slice(0)) {
  10329. reader.cancel(reason);
  10330. }
  10331. }
  10332. }
  10333. class PDFNetworkStreamFullRequestReader {
  10334. constructor(manager, source) {
  10335. this._manager = manager;
  10336. this._url = source.url;
  10337. this._fullRequestId = manager.request({
  10338. onHeadersReceived: this._onHeadersReceived.bind(this),
  10339. onDone: this._onDone.bind(this),
  10340. onError: this._onError.bind(this),
  10341. onProgress: this._onProgress.bind(this)
  10342. });
  10343. this._headersCapability = Promise.withResolvers();
  10344. this._disableRange = source.disableRange || false;
  10345. this._contentLength = source.length;
  10346. this._rangeChunkSize = source.rangeChunkSize;
  10347. if (!this._rangeChunkSize && !this._disableRange) {
  10348. this._disableRange = true;
  10349. }
  10350. this._isStreamingSupported = false;
  10351. this._isRangeSupported = false;
  10352. this._cachedChunks = [];
  10353. this._requests = [];
  10354. this._done = false;
  10355. this._storedError = undefined;
  10356. this._filename = null;
  10357. this.onProgress = null;
  10358. }
  10359. _onHeadersReceived() {
  10360. const fullRequestXhrId = this._fullRequestId;
  10361. const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
  10362. this._manager._responseOrigin = getResponseOrigin(fullRequestXhr.responseURL);
  10363. const rawResponseHeaders = fullRequestXhr.getAllResponseHeaders();
  10364. const responseHeaders = new Headers(rawResponseHeaders ? rawResponseHeaders.trimStart().replace(/[^\S ]+$/, "").split(/[\r\n]+/).map(x => {
  10365. const [key, ...val] = x.split(": ");
  10366. return [key, val.join(": ")];
  10367. }) : []);
  10368. const {
  10369. allowRangeRequests,
  10370. suggestedLength
  10371. } = validateRangeRequestCapabilities({
  10372. responseHeaders,
  10373. isHttp: this._manager.isHttp,
  10374. rangeChunkSize: this._rangeChunkSize,
  10375. disableRange: this._disableRange
  10376. });
  10377. if (allowRangeRequests) {
  10378. this._isRangeSupported = true;
  10379. }
  10380. this._contentLength = suggestedLength || this._contentLength;
  10381. this._filename = extractFilenameFromHeader(responseHeaders);
  10382. if (this._isRangeSupported) {
  10383. this._manager.abortRequest(fullRequestXhrId);
  10384. }
  10385. this._headersCapability.resolve();
  10386. }
  10387. _onDone(data) {
  10388. if (data) {
  10389. if (this._requests.length > 0) {
  10390. const requestCapability = this._requests.shift();
  10391. requestCapability.resolve({
  10392. value: data.chunk,
  10393. done: false
  10394. });
  10395. } else {
  10396. this._cachedChunks.push(data.chunk);
  10397. }
  10398. }
  10399. this._done = true;
  10400. if (this._cachedChunks.length > 0) {
  10401. return;
  10402. }
  10403. for (const requestCapability of this._requests) {
  10404. requestCapability.resolve({
  10405. value: undefined,
  10406. done: true
  10407. });
  10408. }
  10409. this._requests.length = 0;
  10410. }
  10411. _onError(status) {
  10412. this._storedError = createResponseError(status, this._url);
  10413. this._headersCapability.reject(this._storedError);
  10414. for (const requestCapability of this._requests) {
  10415. requestCapability.reject(this._storedError);
  10416. }
  10417. this._requests.length = 0;
  10418. this._cachedChunks.length = 0;
  10419. }
  10420. _onProgress(evt) {
  10421. this.onProgress?.({
  10422. loaded: evt.loaded,
  10423. total: evt.lengthComputable ? evt.total : this._contentLength
  10424. });
  10425. }
  10426. get filename() {
  10427. return this._filename;
  10428. }
  10429. get isRangeSupported() {
  10430. return this._isRangeSupported;
  10431. }
  10432. get isStreamingSupported() {
  10433. return this._isStreamingSupported;
  10434. }
  10435. get contentLength() {
  10436. return this._contentLength;
  10437. }
  10438. get headersReady() {
  10439. return this._headersCapability.promise;
  10440. }
  10441. async read() {
  10442. await this._headersCapability.promise;
  10443. if (this._storedError) {
  10444. throw this._storedError;
  10445. }
  10446. if (this._cachedChunks.length > 0) {
  10447. const chunk = this._cachedChunks.shift();
  10448. return {
  10449. value: chunk,
  10450. done: false
  10451. };
  10452. }
  10453. if (this._done) {
  10454. return {
  10455. value: undefined,
  10456. done: true
  10457. };
  10458. }
  10459. const requestCapability = Promise.withResolvers();
  10460. this._requests.push(requestCapability);
  10461. return requestCapability.promise;
  10462. }
  10463. cancel(reason) {
  10464. this._done = true;
  10465. this._headersCapability.reject(reason);
  10466. for (const requestCapability of this._requests) {
  10467. requestCapability.resolve({
  10468. value: undefined,
  10469. done: true
  10470. });
  10471. }
  10472. this._requests.length = 0;
  10473. if (this._manager.isPendingRequest(this._fullRequestId)) {
  10474. this._manager.abortRequest(this._fullRequestId);
  10475. }
  10476. this._fullRequestReader = null;
  10477. }
  10478. }
  10479. class PDFNetworkStreamRangeRequestReader {
  10480. constructor(manager, begin, end) {
  10481. this._manager = manager;
  10482. this._url = manager.url;
  10483. this._requestId = manager.request({
  10484. begin,
  10485. end,
  10486. onHeadersReceived: this._onHeadersReceived.bind(this),
  10487. onDone: this._onDone.bind(this),
  10488. onError: this._onError.bind(this),
  10489. onProgress: this._onProgress.bind(this)
  10490. });
  10491. this._requests = [];
  10492. this._queuedChunk = null;
  10493. this._done = false;
  10494. this._storedError = undefined;
  10495. this.onProgress = null;
  10496. this.onClosed = null;
  10497. }
  10498. _onHeadersReceived() {
  10499. const responseOrigin = getResponseOrigin(this._manager.getRequestXhr(this._requestId)?.responseURL);
  10500. if (responseOrigin !== this._manager._responseOrigin) {
  10501. this._storedError = new Error(`Expected range response-origin "${responseOrigin}" to match "${this._manager._responseOrigin}".`);
  10502. this._onError(0);
  10503. }
  10504. }
  10505. _close() {
  10506. this.onClosed?.(this);
  10507. }
  10508. _onDone(data) {
  10509. const chunk = data.chunk;
  10510. if (this._requests.length > 0) {
  10511. const requestCapability = this._requests.shift();
  10512. requestCapability.resolve({
  10513. value: chunk,
  10514. done: false
  10515. });
  10516. } else {
  10517. this._queuedChunk = chunk;
  10518. }
  10519. this._done = true;
  10520. for (const requestCapability of this._requests) {
  10521. requestCapability.resolve({
  10522. value: undefined,
  10523. done: true
  10524. });
  10525. }
  10526. this._requests.length = 0;
  10527. this._close();
  10528. }
  10529. _onError(status) {
  10530. this._storedError ??= createResponseError(status, this._url);
  10531. for (const requestCapability of this._requests) {
  10532. requestCapability.reject(this._storedError);
  10533. }
  10534. this._requests.length = 0;
  10535. this._queuedChunk = null;
  10536. }
  10537. _onProgress(evt) {
  10538. if (!this.isStreamingSupported) {
  10539. this.onProgress?.({
  10540. loaded: evt.loaded
  10541. });
  10542. }
  10543. }
  10544. get isStreamingSupported() {
  10545. return false;
  10546. }
  10547. async read() {
  10548. if (this._storedError) {
  10549. throw this._storedError;
  10550. }
  10551. if (this._queuedChunk !== null) {
  10552. const chunk = this._queuedChunk;
  10553. this._queuedChunk = null;
  10554. return {
  10555. value: chunk,
  10556. done: false
  10557. };
  10558. }
  10559. if (this._done) {
  10560. return {
  10561. value: undefined,
  10562. done: true
  10563. };
  10564. }
  10565. const requestCapability = Promise.withResolvers();
  10566. this._requests.push(requestCapability);
  10567. return requestCapability.promise;
  10568. }
  10569. cancel(reason) {
  10570. this._done = true;
  10571. for (const requestCapability of this._requests) {
  10572. requestCapability.resolve({
  10573. value: undefined,
  10574. done: true
  10575. });
  10576. }
  10577. this._requests.length = 0;
  10578. if (this._manager.isPendingRequest(this._requestId)) {
  10579. this._manager.abortRequest(this._requestId);
  10580. }
  10581. this._close();
  10582. }
  10583. }
  10584. ;// ./src/display/node_stream.js
  10585. const urlRegex = /^[a-z][a-z0-9\-+.]+:/i;
  10586. function parseUrlOrPath(sourceUrl) {
  10587. if (urlRegex.test(sourceUrl)) {
  10588. return new URL(sourceUrl);
  10589. }
  10590. const url = process.getBuiltinModule("url");
  10591. return new URL(url.pathToFileURL(sourceUrl));
  10592. }
  10593. class PDFNodeStream {
  10594. constructor(source) {
  10595. this.source = source;
  10596. this.url = parseUrlOrPath(source.url);
  10597. assert(this.url.protocol === "file:", "PDFNodeStream only supports file:// URLs.");
  10598. this._fullRequestReader = null;
  10599. this._rangeRequestReaders = [];
  10600. }
  10601. get _progressiveDataLength() {
  10602. return this._fullRequestReader?._loaded ?? 0;
  10603. }
  10604. getFullReader() {
  10605. assert(!this._fullRequestReader, "PDFNodeStream.getFullReader can only be called once.");
  10606. this._fullRequestReader = new PDFNodeStreamFsFullReader(this);
  10607. return this._fullRequestReader;
  10608. }
  10609. getRangeReader(start, end) {
  10610. if (end <= this._progressiveDataLength) {
  10611. return null;
  10612. }
  10613. const rangeReader = new PDFNodeStreamFsRangeReader(this, start, end);
  10614. this._rangeRequestReaders.push(rangeReader);
  10615. return rangeReader;
  10616. }
  10617. cancelAllRequests(reason) {
  10618. this._fullRequestReader?.cancel(reason);
  10619. for (const reader of this._rangeRequestReaders.slice(0)) {
  10620. reader.cancel(reason);
  10621. }
  10622. }
  10623. }
  10624. class PDFNodeStreamFsFullReader {
  10625. constructor(stream) {
  10626. this._url = stream.url;
  10627. this._done = false;
  10628. this._storedError = null;
  10629. this.onProgress = null;
  10630. const source = stream.source;
  10631. this._contentLength = source.length;
  10632. this._loaded = 0;
  10633. this._filename = null;
  10634. this._disableRange = source.disableRange || false;
  10635. this._rangeChunkSize = source.rangeChunkSize;
  10636. if (!this._rangeChunkSize && !this._disableRange) {
  10637. this._disableRange = true;
  10638. }
  10639. this._isStreamingSupported = !source.disableStream;
  10640. this._isRangeSupported = !source.disableRange;
  10641. this._readableStream = null;
  10642. this._readCapability = Promise.withResolvers();
  10643. this._headersCapability = Promise.withResolvers();
  10644. const fs = process.getBuiltinModule("fs");
  10645. fs.promises.lstat(this._url).then(stat => {
  10646. this._contentLength = stat.size;
  10647. this._setReadableStream(fs.createReadStream(this._url));
  10648. this._headersCapability.resolve();
  10649. }, error => {
  10650. if (error.code === "ENOENT") {
  10651. error = createResponseError(0, this._url.href);
  10652. }
  10653. this._storedError = error;
  10654. this._headersCapability.reject(error);
  10655. });
  10656. }
  10657. get headersReady() {
  10658. return this._headersCapability.promise;
  10659. }
  10660. get filename() {
  10661. return this._filename;
  10662. }
  10663. get contentLength() {
  10664. return this._contentLength;
  10665. }
  10666. get isRangeSupported() {
  10667. return this._isRangeSupported;
  10668. }
  10669. get isStreamingSupported() {
  10670. return this._isStreamingSupported;
  10671. }
  10672. async read() {
  10673. await this._readCapability.promise;
  10674. if (this._done) {
  10675. return {
  10676. value: undefined,
  10677. done: true
  10678. };
  10679. }
  10680. if (this._storedError) {
  10681. throw this._storedError;
  10682. }
  10683. const chunk = this._readableStream.read();
  10684. if (chunk === null) {
  10685. this._readCapability = Promise.withResolvers();
  10686. return this.read();
  10687. }
  10688. this._loaded += chunk.length;
  10689. this.onProgress?.({
  10690. loaded: this._loaded,
  10691. total: this._contentLength
  10692. });
  10693. const buffer = new Uint8Array(chunk).buffer;
  10694. return {
  10695. value: buffer,
  10696. done: false
  10697. };
  10698. }
  10699. cancel(reason) {
  10700. if (!this._readableStream) {
  10701. this._error(reason);
  10702. return;
  10703. }
  10704. this._readableStream.destroy(reason);
  10705. }
  10706. _error(reason) {
  10707. this._storedError = reason;
  10708. this._readCapability.resolve();
  10709. }
  10710. _setReadableStream(readableStream) {
  10711. this._readableStream = readableStream;
  10712. readableStream.on("readable", () => {
  10713. this._readCapability.resolve();
  10714. });
  10715. readableStream.on("end", () => {
  10716. readableStream.destroy();
  10717. this._done = true;
  10718. this._readCapability.resolve();
  10719. });
  10720. readableStream.on("error", reason => {
  10721. this._error(reason);
  10722. });
  10723. if (!this._isStreamingSupported && this._isRangeSupported) {
  10724. this._error(new AbortException("streaming is disabled"));
  10725. }
  10726. if (this._storedError) {
  10727. this._readableStream.destroy(this._storedError);
  10728. }
  10729. }
  10730. }
  10731. class PDFNodeStreamFsRangeReader {
  10732. constructor(stream, start, end) {
  10733. this._url = stream.url;
  10734. this._done = false;
  10735. this._storedError = null;
  10736. this.onProgress = null;
  10737. this._loaded = 0;
  10738. this._readableStream = null;
  10739. this._readCapability = Promise.withResolvers();
  10740. const source = stream.source;
  10741. this._isStreamingSupported = !source.disableStream;
  10742. const fs = process.getBuiltinModule("fs");
  10743. this._setReadableStream(fs.createReadStream(this._url, {
  10744. start,
  10745. end: end - 1
  10746. }));
  10747. }
  10748. get isStreamingSupported() {
  10749. return this._isStreamingSupported;
  10750. }
  10751. async read() {
  10752. await this._readCapability.promise;
  10753. if (this._done) {
  10754. return {
  10755. value: undefined,
  10756. done: true
  10757. };
  10758. }
  10759. if (this._storedError) {
  10760. throw this._storedError;
  10761. }
  10762. const chunk = this._readableStream.read();
  10763. if (chunk === null) {
  10764. this._readCapability = Promise.withResolvers();
  10765. return this.read();
  10766. }
  10767. this._loaded += chunk.length;
  10768. this.onProgress?.({
  10769. loaded: this._loaded
  10770. });
  10771. const buffer = new Uint8Array(chunk).buffer;
  10772. return {
  10773. value: buffer,
  10774. done: false
  10775. };
  10776. }
  10777. cancel(reason) {
  10778. if (!this._readableStream) {
  10779. this._error(reason);
  10780. return;
  10781. }
  10782. this._readableStream.destroy(reason);
  10783. }
  10784. _error(reason) {
  10785. this._storedError = reason;
  10786. this._readCapability.resolve();
  10787. }
  10788. _setReadableStream(readableStream) {
  10789. this._readableStream = readableStream;
  10790. readableStream.on("readable", () => {
  10791. this._readCapability.resolve();
  10792. });
  10793. readableStream.on("end", () => {
  10794. readableStream.destroy();
  10795. this._done = true;
  10796. this._readCapability.resolve();
  10797. });
  10798. readableStream.on("error", reason => {
  10799. this._error(reason);
  10800. });
  10801. if (this._storedError) {
  10802. this._readableStream.destroy(this._storedError);
  10803. }
  10804. }
  10805. }
  10806. ;// ./src/display/text_layer.js
  10807. const MAX_TEXT_DIVS_TO_RENDER = 100000;
  10808. const DEFAULT_FONT_SIZE = 30;
  10809. class TextLayer {
  10810. #capability = Promise.withResolvers();
  10811. #container = null;
  10812. #disableProcessItems = false;
  10813. #fontInspectorEnabled = !!globalThis.FontInspector?.enabled;
  10814. #lang = null;
  10815. #layoutTextParams = null;
  10816. #pageHeight = 0;
  10817. #pageWidth = 0;
  10818. #reader = null;
  10819. #rootContainer = null;
  10820. #rotation = 0;
  10821. #scale = 0;
  10822. #styleCache = Object.create(null);
  10823. #textContentItemsStr = [];
  10824. #textContentSource = null;
  10825. #textDivs = [];
  10826. #textDivProperties = new WeakMap();
  10827. #transform = null;
  10828. static #ascentCache = new Map();
  10829. static #canvasContexts = new Map();
  10830. static #canvasCtxFonts = new WeakMap();
  10831. static #minFontSize = null;
  10832. static #pendingTextLayers = new Set();
  10833. constructor({
  10834. textContentSource,
  10835. container,
  10836. viewport
  10837. }) {
  10838. if (textContentSource instanceof ReadableStream) {
  10839. this.#textContentSource = textContentSource;
  10840. } else if (typeof textContentSource === "object") {
  10841. this.#textContentSource = new ReadableStream({
  10842. start(controller) {
  10843. controller.enqueue(textContentSource);
  10844. controller.close();
  10845. }
  10846. });
  10847. } else {
  10848. throw new Error('No "textContentSource" parameter specified.');
  10849. }
  10850. this.#container = this.#rootContainer = container;
  10851. this.#scale = viewport.scale * OutputScale.pixelRatio;
  10852. this.#rotation = viewport.rotation;
  10853. this.#layoutTextParams = {
  10854. div: null,
  10855. properties: null,
  10856. ctx: null
  10857. };
  10858. const {
  10859. pageWidth,
  10860. pageHeight,
  10861. pageX,
  10862. pageY
  10863. } = viewport.rawDims;
  10864. this.#transform = [1, 0, 0, -1, -pageX, pageY + pageHeight];
  10865. this.#pageWidth = pageWidth;
  10866. this.#pageHeight = pageHeight;
  10867. TextLayer.#ensureMinFontSizeComputed();
  10868. setLayerDimensions(container, viewport);
  10869. this.#capability.promise.finally(() => {
  10870. TextLayer.#pendingTextLayers.delete(this);
  10871. this.#layoutTextParams = null;
  10872. this.#styleCache = null;
  10873. }).catch(() => {});
  10874. }
  10875. static get fontFamilyMap() {
  10876. const {
  10877. isWindows,
  10878. isFirefox
  10879. } = util_FeatureTest.platform;
  10880. return shadow(this, "fontFamilyMap", new Map([["sans-serif", `${isWindows && isFirefox ? "Calibri, " : ""}sans-serif`], ["monospace", `${isWindows && isFirefox ? "Lucida Console, " : ""}monospace`]]));
  10881. }
  10882. render() {
  10883. const pump = () => {
  10884. this.#reader.read().then(({
  10885. value,
  10886. done
  10887. }) => {
  10888. if (done) {
  10889. this.#capability.resolve();
  10890. return;
  10891. }
  10892. this.#lang ??= value.lang;
  10893. Object.assign(this.#styleCache, value.styles);
  10894. this.#processItems(value.items);
  10895. pump();
  10896. }, this.#capability.reject);
  10897. };
  10898. this.#reader = this.#textContentSource.getReader();
  10899. TextLayer.#pendingTextLayers.add(this);
  10900. pump();
  10901. return this.#capability.promise;
  10902. }
  10903. update({
  10904. viewport,
  10905. onBefore = null
  10906. }) {
  10907. const scale = viewport.scale * OutputScale.pixelRatio;
  10908. const rotation = viewport.rotation;
  10909. if (rotation !== this.#rotation) {
  10910. onBefore?.();
  10911. this.#rotation = rotation;
  10912. setLayerDimensions(this.#rootContainer, {
  10913. rotation
  10914. });
  10915. }
  10916. if (scale !== this.#scale) {
  10917. onBefore?.();
  10918. this.#scale = scale;
  10919. const params = {
  10920. div: null,
  10921. properties: null,
  10922. ctx: TextLayer.#getCtx(this.#lang)
  10923. };
  10924. for (const div of this.#textDivs) {
  10925. params.properties = this.#textDivProperties.get(div);
  10926. params.div = div;
  10927. this.#layout(params);
  10928. }
  10929. }
  10930. }
  10931. cancel() {
  10932. const abortEx = new AbortException("TextLayer task cancelled.");
  10933. this.#reader?.cancel(abortEx).catch(() => {});
  10934. this.#reader = null;
  10935. this.#capability.reject(abortEx);
  10936. }
  10937. get textDivs() {
  10938. return this.#textDivs;
  10939. }
  10940. get textContentItemsStr() {
  10941. return this.#textContentItemsStr;
  10942. }
  10943. #processItems(items) {
  10944. if (this.#disableProcessItems) {
  10945. return;
  10946. }
  10947. this.#layoutTextParams.ctx ??= TextLayer.#getCtx(this.#lang);
  10948. const textDivs = this.#textDivs,
  10949. textContentItemsStr = this.#textContentItemsStr;
  10950. for (const item of items) {
  10951. if (textDivs.length > MAX_TEXT_DIVS_TO_RENDER) {
  10952. warn("Ignoring additional textDivs for performance reasons.");
  10953. this.#disableProcessItems = true;
  10954. return;
  10955. }
  10956. if (item.str === undefined) {
  10957. if (item.type === "beginMarkedContentProps" || item.type === "beginMarkedContent") {
  10958. const parent = this.#container;
  10959. this.#container = document.createElement("span");
  10960. this.#container.classList.add("markedContent");
  10961. if (item.id !== null) {
  10962. this.#container.setAttribute("id", `${item.id}`);
  10963. }
  10964. parent.append(this.#container);
  10965. } else if (item.type === "endMarkedContent") {
  10966. this.#container = this.#container.parentNode;
  10967. }
  10968. continue;
  10969. }
  10970. textContentItemsStr.push(item.str);
  10971. this.#appendText(item);
  10972. }
  10973. }
  10974. #appendText(geom) {
  10975. const textDiv = document.createElement("span");
  10976. const textDivProperties = {
  10977. angle: 0,
  10978. canvasWidth: 0,
  10979. hasText: geom.str !== "",
  10980. hasEOL: geom.hasEOL,
  10981. fontSize: 0
  10982. };
  10983. this.#textDivs.push(textDiv);
  10984. const tx = Util.transform(this.#transform, geom.transform);
  10985. let angle = Math.atan2(tx[1], tx[0]);
  10986. const style = this.#styleCache[geom.fontName];
  10987. if (style.vertical) {
  10988. angle += Math.PI / 2;
  10989. }
  10990. let fontFamily = this.#fontInspectorEnabled && style.fontSubstitution || style.fontFamily;
  10991. fontFamily = TextLayer.fontFamilyMap.get(fontFamily) || fontFamily;
  10992. const fontHeight = Math.hypot(tx[2], tx[3]);
  10993. const fontAscent = fontHeight * TextLayer.#getAscent(fontFamily, style, this.#lang);
  10994. let left, top;
  10995. if (angle === 0) {
  10996. left = tx[4];
  10997. top = tx[5] - fontAscent;
  10998. } else {
  10999. left = tx[4] + fontAscent * Math.sin(angle);
  11000. top = tx[5] - fontAscent * Math.cos(angle);
  11001. }
  11002. const scaleFactorStr = "calc(var(--total-scale-factor) *";
  11003. const divStyle = textDiv.style;
  11004. if (this.#container === this.#rootContainer) {
  11005. divStyle.left = `${(100 * left / this.#pageWidth).toFixed(2)}%`;
  11006. divStyle.top = `${(100 * top / this.#pageHeight).toFixed(2)}%`;
  11007. } else {
  11008. divStyle.left = `${scaleFactorStr}${left.toFixed(2)}px)`;
  11009. divStyle.top = `${scaleFactorStr}${top.toFixed(2)}px)`;
  11010. }
  11011. divStyle.fontSize = `${scaleFactorStr}${(TextLayer.#minFontSize * fontHeight).toFixed(2)}px)`;
  11012. divStyle.fontFamily = fontFamily;
  11013. textDivProperties.fontSize = fontHeight;
  11014. textDiv.setAttribute("role", "presentation");
  11015. textDiv.textContent = geom.str;
  11016. textDiv.dir = geom.dir;
  11017. if (this.#fontInspectorEnabled) {
  11018. textDiv.dataset.fontName = style.fontSubstitutionLoadedName || geom.fontName;
  11019. }
  11020. if (angle !== 0) {
  11021. textDivProperties.angle = angle * (180 / Math.PI);
  11022. }
  11023. let shouldScaleText = false;
  11024. if (geom.str.length > 1) {
  11025. shouldScaleText = true;
  11026. } else if (geom.str !== " " && geom.transform[0] !== geom.transform[3]) {
  11027. const absScaleX = Math.abs(geom.transform[0]),
  11028. absScaleY = Math.abs(geom.transform[3]);
  11029. if (absScaleX !== absScaleY && Math.max(absScaleX, absScaleY) / Math.min(absScaleX, absScaleY) > 1.5) {
  11030. shouldScaleText = true;
  11031. }
  11032. }
  11033. if (shouldScaleText) {
  11034. textDivProperties.canvasWidth = style.vertical ? geom.height : geom.width;
  11035. }
  11036. this.#textDivProperties.set(textDiv, textDivProperties);
  11037. this.#layoutTextParams.div = textDiv;
  11038. this.#layoutTextParams.properties = textDivProperties;
  11039. this.#layout(this.#layoutTextParams);
  11040. if (textDivProperties.hasText) {
  11041. this.#container.append(textDiv);
  11042. }
  11043. if (textDivProperties.hasEOL) {
  11044. const br = document.createElement("br");
  11045. br.setAttribute("role", "presentation");
  11046. this.#container.append(br);
  11047. }
  11048. }
  11049. #layout(params) {
  11050. const {
  11051. div,
  11052. properties,
  11053. ctx
  11054. } = params;
  11055. const {
  11056. style
  11057. } = div;
  11058. let transform = "";
  11059. if (TextLayer.#minFontSize > 1) {
  11060. transform = `scale(${1 / TextLayer.#minFontSize})`;
  11061. }
  11062. if (properties.canvasWidth !== 0 && properties.hasText) {
  11063. const {
  11064. fontFamily
  11065. } = style;
  11066. const {
  11067. canvasWidth,
  11068. fontSize
  11069. } = properties;
  11070. TextLayer.#ensureCtxFont(ctx, fontSize * this.#scale, fontFamily);
  11071. const {
  11072. width
  11073. } = ctx.measureText(div.textContent);
  11074. if (width > 0) {
  11075. transform = `scaleX(${canvasWidth * this.#scale / width}) ${transform}`;
  11076. }
  11077. }
  11078. if (properties.angle !== 0) {
  11079. transform = `rotate(${properties.angle}deg) ${transform}`;
  11080. }
  11081. if (transform.length > 0) {
  11082. style.transform = transform;
  11083. }
  11084. }
  11085. static cleanup() {
  11086. if (this.#pendingTextLayers.size > 0) {
  11087. return;
  11088. }
  11089. this.#ascentCache.clear();
  11090. for (const {
  11091. canvas
  11092. } of this.#canvasContexts.values()) {
  11093. canvas.remove();
  11094. }
  11095. this.#canvasContexts.clear();
  11096. }
  11097. static #getCtx(lang = null) {
  11098. let ctx = this.#canvasContexts.get(lang ||= "");
  11099. if (!ctx) {
  11100. const canvas = document.createElement("canvas");
  11101. canvas.className = "hiddenCanvasElement";
  11102. canvas.lang = lang;
  11103. document.body.append(canvas);
  11104. ctx = canvas.getContext("2d", {
  11105. alpha: false,
  11106. willReadFrequently: true
  11107. });
  11108. this.#canvasContexts.set(lang, ctx);
  11109. this.#canvasCtxFonts.set(ctx, {
  11110. size: 0,
  11111. family: ""
  11112. });
  11113. }
  11114. return ctx;
  11115. }
  11116. static #ensureCtxFont(ctx, size, family) {
  11117. const cached = this.#canvasCtxFonts.get(ctx);
  11118. if (size === cached.size && family === cached.family) {
  11119. return;
  11120. }
  11121. ctx.font = `${size}px ${family}`;
  11122. cached.size = size;
  11123. cached.family = family;
  11124. }
  11125. static #ensureMinFontSizeComputed() {
  11126. if (this.#minFontSize !== null) {
  11127. return;
  11128. }
  11129. const div = document.createElement("div");
  11130. div.style.opacity = 0;
  11131. div.style.lineHeight = 1;
  11132. div.style.fontSize = "1px";
  11133. div.style.position = "absolute";
  11134. div.textContent = "X";
  11135. document.body.append(div);
  11136. this.#minFontSize = div.getBoundingClientRect().height;
  11137. div.remove();
  11138. }
  11139. static #getAscent(fontFamily, style, lang) {
  11140. const cachedAscent = this.#ascentCache.get(fontFamily);
  11141. if (cachedAscent) {
  11142. return cachedAscent;
  11143. }
  11144. const ctx = this.#getCtx(lang);
  11145. ctx.canvas.width = ctx.canvas.height = DEFAULT_FONT_SIZE;
  11146. this.#ensureCtxFont(ctx, DEFAULT_FONT_SIZE, fontFamily);
  11147. const metrics = ctx.measureText("");
  11148. const ascent = metrics.fontBoundingBoxAscent;
  11149. const descent = Math.abs(metrics.fontBoundingBoxDescent);
  11150. ctx.canvas.width = ctx.canvas.height = 0;
  11151. let ratio = 0.8;
  11152. if (ascent) {
  11153. ratio = ascent / (ascent + descent);
  11154. } else {
  11155. if (util_FeatureTest.platform.isFirefox) {
  11156. warn("Enable the `dom.textMetrics.fontBoundingBox.enabled` preference " + "in `about:config` to improve TextLayer rendering.");
  11157. }
  11158. if (style.ascent) {
  11159. ratio = style.ascent;
  11160. } else if (style.descent) {
  11161. ratio = 1 + style.descent;
  11162. }
  11163. }
  11164. this.#ascentCache.set(fontFamily, ratio);
  11165. return ratio;
  11166. }
  11167. }
  11168. ;// ./src/display/xfa_text.js
  11169. class XfaText {
  11170. static textContent(xfa) {
  11171. const items = [];
  11172. const output = {
  11173. items,
  11174. styles: Object.create(null)
  11175. };
  11176. function walk(node) {
  11177. if (!node) {
  11178. return;
  11179. }
  11180. let str = null;
  11181. const name = node.name;
  11182. if (name === "#text") {
  11183. str = node.value;
  11184. } else if (!XfaText.shouldBuildText(name)) {
  11185. return;
  11186. } else if (node?.attributes?.textContent) {
  11187. str = node.attributes.textContent;
  11188. } else if (node.value) {
  11189. str = node.value;
  11190. }
  11191. if (str !== null) {
  11192. items.push({
  11193. str
  11194. });
  11195. }
  11196. if (!node.children) {
  11197. return;
  11198. }
  11199. for (const child of node.children) {
  11200. walk(child);
  11201. }
  11202. }
  11203. walk(xfa);
  11204. return output;
  11205. }
  11206. static shouldBuildText(name) {
  11207. return !(name === "textarea" || name === "input" || name === "option" || name === "select");
  11208. }
  11209. }
  11210. ;// ./src/display/api.js
  11211. const DEFAULT_RANGE_CHUNK_SIZE = 65536;
  11212. const RENDERING_CANCELLED_TIMEOUT = 100;
  11213. function getDocument(src = {}) {
  11214. if (typeof src === "string" || src instanceof URL) {
  11215. src = {
  11216. url: src
  11217. };
  11218. } else if (src instanceof ArrayBuffer || ArrayBuffer.isView(src)) {
  11219. src = {
  11220. data: src
  11221. };
  11222. }
  11223. const task = new PDFDocumentLoadingTask();
  11224. const {
  11225. docId
  11226. } = task;
  11227. const url = src.url ? getUrlProp(src.url) : null;
  11228. const data = src.data ? getDataProp(src.data) : null;
  11229. const httpHeaders = src.httpHeaders || null;
  11230. const withCredentials = src.withCredentials === true;
  11231. const password = src.password ?? null;
  11232. const rangeTransport = src.range instanceof PDFDataRangeTransport ? src.range : null;
  11233. const rangeChunkSize = Number.isInteger(src.rangeChunkSize) && src.rangeChunkSize > 0 ? src.rangeChunkSize : DEFAULT_RANGE_CHUNK_SIZE;
  11234. let worker = src.worker instanceof PDFWorker ? src.worker : null;
  11235. const verbosity = src.verbosity;
  11236. const docBaseUrl = typeof src.docBaseUrl === "string" && !isDataScheme(src.docBaseUrl) ? src.docBaseUrl : null;
  11237. const cMapUrl = getFactoryUrlProp(src.cMapUrl);
  11238. const cMapPacked = src.cMapPacked !== false;
  11239. const CMapReaderFactory = src.CMapReaderFactory || (isNodeJS ? NodeCMapReaderFactory : DOMCMapReaderFactory);
  11240. const iccUrl = getFactoryUrlProp(src.iccUrl);
  11241. const standardFontDataUrl = getFactoryUrlProp(src.standardFontDataUrl);
  11242. const StandardFontDataFactory = src.StandardFontDataFactory || (isNodeJS ? NodeStandardFontDataFactory : DOMStandardFontDataFactory);
  11243. const wasmUrl = getFactoryUrlProp(src.wasmUrl);
  11244. const WasmFactory = src.WasmFactory || (isNodeJS ? NodeWasmFactory : DOMWasmFactory);
  11245. const ignoreErrors = src.stopAtErrors !== true;
  11246. const maxImageSize = Number.isInteger(src.maxImageSize) && src.maxImageSize > -1 ? src.maxImageSize : -1;
  11247. const isEvalSupported = src.isEvalSupported !== false;
  11248. const isOffscreenCanvasSupported = typeof src.isOffscreenCanvasSupported === "boolean" ? src.isOffscreenCanvasSupported : !isNodeJS;
  11249. const isImageDecoderSupported = typeof src.isImageDecoderSupported === "boolean" ? src.isImageDecoderSupported : !isNodeJS && (util_FeatureTest.platform.isFirefox || !globalThis.chrome);
  11250. const canvasMaxAreaInBytes = Number.isInteger(src.canvasMaxAreaInBytes) ? src.canvasMaxAreaInBytes : -1;
  11251. const disableFontFace = typeof src.disableFontFace === "boolean" ? src.disableFontFace : isNodeJS;
  11252. const fontExtraProperties = src.fontExtraProperties === true;
  11253. const enableXfa = src.enableXfa === true;
  11254. const ownerDocument = src.ownerDocument || globalThis.document;
  11255. const disableRange = src.disableRange === true;
  11256. const disableStream = src.disableStream === true;
  11257. const disableAutoFetch = src.disableAutoFetch === true;
  11258. const pdfBug = src.pdfBug === true;
  11259. const CanvasFactory = src.CanvasFactory || (isNodeJS ? NodeCanvasFactory : DOMCanvasFactory);
  11260. const FilterFactory = src.FilterFactory || (isNodeJS ? NodeFilterFactory : DOMFilterFactory);
  11261. const enableHWA = src.enableHWA === true;
  11262. const useWasm = src.useWasm !== false;
  11263. const length = rangeTransport ? rangeTransport.length : src.length ?? NaN;
  11264. const useSystemFonts = typeof src.useSystemFonts === "boolean" ? src.useSystemFonts : !isNodeJS && !disableFontFace;
  11265. 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));
  11266. const styleElement = null;
  11267. setVerbosityLevel(verbosity);
  11268. const transportFactory = {
  11269. canvasFactory: new CanvasFactory({
  11270. ownerDocument,
  11271. enableHWA
  11272. }),
  11273. filterFactory: new FilterFactory({
  11274. docId,
  11275. ownerDocument
  11276. }),
  11277. cMapReaderFactory: useWorkerFetch ? null : new CMapReaderFactory({
  11278. baseUrl: cMapUrl,
  11279. isCompressed: cMapPacked
  11280. }),
  11281. standardFontDataFactory: useWorkerFetch ? null : new StandardFontDataFactory({
  11282. baseUrl: standardFontDataUrl
  11283. }),
  11284. wasmFactory: useWorkerFetch ? null : new WasmFactory({
  11285. baseUrl: wasmUrl
  11286. })
  11287. };
  11288. if (!worker) {
  11289. const workerParams = {
  11290. verbosity,
  11291. port: GlobalWorkerOptions.workerPort
  11292. };
  11293. worker = workerParams.port ? PDFWorker.fromPort(workerParams) : new PDFWorker(workerParams);
  11294. task._worker = worker;
  11295. }
  11296. const docParams = {
  11297. docId,
  11298. apiVersion: "5.2.133",
  11299. data,
  11300. password,
  11301. disableAutoFetch,
  11302. rangeChunkSize,
  11303. length,
  11304. docBaseUrl,
  11305. enableXfa,
  11306. evaluatorOptions: {
  11307. maxImageSize,
  11308. disableFontFace,
  11309. ignoreErrors,
  11310. isEvalSupported,
  11311. isOffscreenCanvasSupported,
  11312. isImageDecoderSupported,
  11313. canvasMaxAreaInBytes,
  11314. fontExtraProperties,
  11315. useSystemFonts,
  11316. useWasm,
  11317. useWorkerFetch,
  11318. cMapUrl,
  11319. iccUrl,
  11320. standardFontDataUrl,
  11321. wasmUrl
  11322. }
  11323. };
  11324. const transportParams = {
  11325. ownerDocument,
  11326. pdfBug,
  11327. styleElement,
  11328. loadingParams: {
  11329. disableAutoFetch,
  11330. enableXfa
  11331. }
  11332. };
  11333. worker.promise.then(function () {
  11334. if (task.destroyed) {
  11335. throw new Error("Loading aborted");
  11336. }
  11337. if (worker.destroyed) {
  11338. throw new Error("Worker was destroyed");
  11339. }
  11340. const workerIdPromise = worker.messageHandler.sendWithPromise("GetDocRequest", docParams, data ? [data.buffer] : null);
  11341. let networkStream;
  11342. if (rangeTransport) {
  11343. networkStream = new PDFDataTransportStream(rangeTransport, {
  11344. disableRange,
  11345. disableStream
  11346. });
  11347. } else if (!data) {
  11348. if (!url) {
  11349. throw new Error("getDocument - no `url` parameter provided.");
  11350. }
  11351. let NetworkStream;
  11352. if (isNodeJS) {
  11353. if (isValidFetchUrl(url)) {
  11354. if (typeof fetch === "undefined" || typeof Response === "undefined" || !("body" in Response.prototype)) {
  11355. throw new Error("getDocument - the Fetch API was disabled in Node.js, see `--no-experimental-fetch`.");
  11356. }
  11357. NetworkStream = PDFFetchStream;
  11358. } else {
  11359. NetworkStream = PDFNodeStream;
  11360. }
  11361. } else {
  11362. NetworkStream = isValidFetchUrl(url) ? PDFFetchStream : PDFNetworkStream;
  11363. }
  11364. networkStream = new NetworkStream({
  11365. url,
  11366. length,
  11367. httpHeaders,
  11368. withCredentials,
  11369. rangeChunkSize,
  11370. disableRange,
  11371. disableStream
  11372. });
  11373. }
  11374. return workerIdPromise.then(workerId => {
  11375. if (task.destroyed) {
  11376. throw new Error("Loading aborted");
  11377. }
  11378. if (worker.destroyed) {
  11379. throw new Error("Worker was destroyed");
  11380. }
  11381. const messageHandler = new MessageHandler(docId, workerId, worker.port);
  11382. const transport = new WorkerTransport(messageHandler, task, networkStream, transportParams, transportFactory);
  11383. task._transport = transport;
  11384. messageHandler.send("Ready", null);
  11385. });
  11386. }).catch(task._capability.reject);
  11387. return task;
  11388. }
  11389. function getUrlProp(val) {
  11390. if (val instanceof URL) {
  11391. return val.href;
  11392. }
  11393. if (typeof val === "string") {
  11394. if (isNodeJS) {
  11395. return val;
  11396. }
  11397. const url = URL.parse(val, window.location);
  11398. if (url) {
  11399. return url.href;
  11400. }
  11401. }
  11402. throw new Error("Invalid PDF url data: " + "either string or URL-object is expected in the url property.");
  11403. }
  11404. function getDataProp(val) {
  11405. if (isNodeJS && typeof Buffer !== "undefined" && val instanceof Buffer) {
  11406. throw new Error("Please provide binary data as `Uint8Array`, rather than `Buffer`.");
  11407. }
  11408. if (val instanceof Uint8Array && val.byteLength === val.buffer.byteLength) {
  11409. return val;
  11410. }
  11411. if (typeof val === "string") {
  11412. return stringToBytes(val);
  11413. }
  11414. if (val instanceof ArrayBuffer || ArrayBuffer.isView(val) || typeof val === "object" && !isNaN(val?.length)) {
  11415. return new Uint8Array(val);
  11416. }
  11417. throw new Error("Invalid PDF binary data: either TypedArray, " + "string, or array-like object is expected in the data property.");
  11418. }
  11419. function getFactoryUrlProp(val) {
  11420. if (typeof val !== "string") {
  11421. return null;
  11422. }
  11423. if (val.endsWith("/")) {
  11424. return val;
  11425. }
  11426. throw new Error(`Invalid factory url: "${val}" must include trailing slash.`);
  11427. }
  11428. const isRefProxy = v => typeof v === "object" && Number.isInteger(v?.num) && v.num >= 0 && Number.isInteger(v?.gen) && v.gen >= 0;
  11429. const isNameProxy = v => typeof v === "object" && typeof v?.name === "string";
  11430. const isValidExplicitDest = _isValidExplicitDest.bind(null, isRefProxy, isNameProxy);
  11431. class PDFDocumentLoadingTask {
  11432. static #docId = 0;
  11433. _capability = Promise.withResolvers();
  11434. _transport = null;
  11435. _worker = null;
  11436. docId = `d${PDFDocumentLoadingTask.#docId++}`;
  11437. destroyed = false;
  11438. onPassword = null;
  11439. onProgress = null;
  11440. get promise() {
  11441. return this._capability.promise;
  11442. }
  11443. async destroy() {
  11444. this.destroyed = true;
  11445. try {
  11446. if (this._worker?.port) {
  11447. this._worker._pendingDestroy = true;
  11448. }
  11449. await this._transport?.destroy();
  11450. } catch (ex) {
  11451. if (this._worker?.port) {
  11452. delete this._worker._pendingDestroy;
  11453. }
  11454. throw ex;
  11455. }
  11456. this._transport = null;
  11457. this._worker?.destroy();
  11458. this._worker = null;
  11459. }
  11460. async getData() {
  11461. return this._transport.getData();
  11462. }
  11463. }
  11464. class PDFDataRangeTransport {
  11465. constructor(length, initialData, progressiveDone = false, contentDispositionFilename = null) {
  11466. this.length = length;
  11467. this.initialData = initialData;
  11468. this.progressiveDone = progressiveDone;
  11469. this.contentDispositionFilename = contentDispositionFilename;
  11470. this._rangeListeners = [];
  11471. this._progressListeners = [];
  11472. this._progressiveReadListeners = [];
  11473. this._progressiveDoneListeners = [];
  11474. this._readyCapability = Promise.withResolvers();
  11475. }
  11476. addRangeListener(listener) {
  11477. this._rangeListeners.push(listener);
  11478. }
  11479. addProgressListener(listener) {
  11480. this._progressListeners.push(listener);
  11481. }
  11482. addProgressiveReadListener(listener) {
  11483. this._progressiveReadListeners.push(listener);
  11484. }
  11485. addProgressiveDoneListener(listener) {
  11486. this._progressiveDoneListeners.push(listener);
  11487. }
  11488. onDataRange(begin, chunk) {
  11489. for (const listener of this._rangeListeners) {
  11490. listener(begin, chunk);
  11491. }
  11492. }
  11493. onDataProgress(loaded, total) {
  11494. this._readyCapability.promise.then(() => {
  11495. for (const listener of this._progressListeners) {
  11496. listener(loaded, total);
  11497. }
  11498. });
  11499. }
  11500. onDataProgressiveRead(chunk) {
  11501. this._readyCapability.promise.then(() => {
  11502. for (const listener of this._progressiveReadListeners) {
  11503. listener(chunk);
  11504. }
  11505. });
  11506. }
  11507. onDataProgressiveDone() {
  11508. this._readyCapability.promise.then(() => {
  11509. for (const listener of this._progressiveDoneListeners) {
  11510. listener();
  11511. }
  11512. });
  11513. }
  11514. transportReady() {
  11515. this._readyCapability.resolve();
  11516. }
  11517. requestDataRange(begin, end) {
  11518. unreachable("Abstract method PDFDataRangeTransport.requestDataRange");
  11519. }
  11520. abort() {}
  11521. }
  11522. class PDFDocumentProxy {
  11523. constructor(pdfInfo, transport) {
  11524. this._pdfInfo = pdfInfo;
  11525. this._transport = transport;
  11526. }
  11527. get annotationStorage() {
  11528. return this._transport.annotationStorage;
  11529. }
  11530. get canvasFactory() {
  11531. return this._transport.canvasFactory;
  11532. }
  11533. get filterFactory() {
  11534. return this._transport.filterFactory;
  11535. }
  11536. get numPages() {
  11537. return this._pdfInfo.numPages;
  11538. }
  11539. get fingerprints() {
  11540. return this._pdfInfo.fingerprints;
  11541. }
  11542. get isPureXfa() {
  11543. return shadow(this, "isPureXfa", !!this._transport._htmlForXfa);
  11544. }
  11545. get allXfaHtml() {
  11546. return this._transport._htmlForXfa;
  11547. }
  11548. getPage(pageNumber) {
  11549. return this._transport.getPage(pageNumber);
  11550. }
  11551. getPageIndex(ref) {
  11552. return this._transport.getPageIndex(ref);
  11553. }
  11554. getDestinations() {
  11555. return this._transport.getDestinations();
  11556. }
  11557. getDestination(id) {
  11558. return this._transport.getDestination(id);
  11559. }
  11560. getPageLabels() {
  11561. return this._transport.getPageLabels();
  11562. }
  11563. getPageLayout() {
  11564. return this._transport.getPageLayout();
  11565. }
  11566. getPageMode() {
  11567. return this._transport.getPageMode();
  11568. }
  11569. getViewerPreferences() {
  11570. return this._transport.getViewerPreferences();
  11571. }
  11572. getOpenAction() {
  11573. return this._transport.getOpenAction();
  11574. }
  11575. getAttachments() {
  11576. return this._transport.getAttachments();
  11577. }
  11578. getJSActions() {
  11579. return this._transport.getDocJSActions();
  11580. }
  11581. getOutline() {
  11582. return this._transport.getOutline();
  11583. }
  11584. getOptionalContentConfig({
  11585. intent = "display"
  11586. } = {}) {
  11587. const {
  11588. renderingIntent
  11589. } = this._transport.getRenderingIntent(intent);
  11590. return this._transport.getOptionalContentConfig(renderingIntent);
  11591. }
  11592. getPermissions() {
  11593. return this._transport.getPermissions();
  11594. }
  11595. getMetadata() {
  11596. return this._transport.getMetadata();
  11597. }
  11598. getMarkInfo() {
  11599. return this._transport.getMarkInfo();
  11600. }
  11601. getData() {
  11602. return this._transport.getData();
  11603. }
  11604. saveDocument() {
  11605. return this._transport.saveDocument();
  11606. }
  11607. getDownloadInfo() {
  11608. return this._transport.downloadInfoCapability.promise;
  11609. }
  11610. cleanup(keepLoadedFonts = false) {
  11611. return this._transport.startCleanup(keepLoadedFonts || this.isPureXfa);
  11612. }
  11613. destroy() {
  11614. return this.loadingTask.destroy();
  11615. }
  11616. cachedPageNumber(ref) {
  11617. return this._transport.cachedPageNumber(ref);
  11618. }
  11619. get loadingParams() {
  11620. return this._transport.loadingParams;
  11621. }
  11622. get loadingTask() {
  11623. return this._transport.loadingTask;
  11624. }
  11625. getFieldObjects() {
  11626. return this._transport.getFieldObjects();
  11627. }
  11628. hasJSActions() {
  11629. return this._transport.hasJSActions();
  11630. }
  11631. getCalculationOrderIds() {
  11632. return this._transport.getCalculationOrderIds();
  11633. }
  11634. }
  11635. class PDFPageProxy {
  11636. #pendingCleanup = false;
  11637. constructor(pageIndex, pageInfo, transport, pdfBug = false) {
  11638. this._pageIndex = pageIndex;
  11639. this._pageInfo = pageInfo;
  11640. this._transport = transport;
  11641. this._stats = pdfBug ? new StatTimer() : null;
  11642. this._pdfBug = pdfBug;
  11643. this.commonObjs = transport.commonObjs;
  11644. this.objs = new PDFObjects();
  11645. this._intentStates = new Map();
  11646. this.destroyed = false;
  11647. }
  11648. get pageNumber() {
  11649. return this._pageIndex + 1;
  11650. }
  11651. get rotate() {
  11652. return this._pageInfo.rotate;
  11653. }
  11654. get ref() {
  11655. return this._pageInfo.ref;
  11656. }
  11657. get userUnit() {
  11658. return this._pageInfo.userUnit;
  11659. }
  11660. get view() {
  11661. return this._pageInfo.view;
  11662. }
  11663. getViewport({
  11664. scale,
  11665. rotation = this.rotate,
  11666. offsetX = 0,
  11667. offsetY = 0,
  11668. dontFlip = false
  11669. } = {}) {
  11670. return new PageViewport({
  11671. viewBox: this.view,
  11672. userUnit: this.userUnit,
  11673. scale,
  11674. rotation,
  11675. offsetX,
  11676. offsetY,
  11677. dontFlip
  11678. });
  11679. }
  11680. getAnnotations({
  11681. intent = "display"
  11682. } = {}) {
  11683. const {
  11684. renderingIntent
  11685. } = this._transport.getRenderingIntent(intent);
  11686. return this._transport.getAnnotations(this._pageIndex, renderingIntent);
  11687. }
  11688. getJSActions() {
  11689. return this._transport.getPageJSActions(this._pageIndex);
  11690. }
  11691. get filterFactory() {
  11692. return this._transport.filterFactory;
  11693. }
  11694. get isPureXfa() {
  11695. return shadow(this, "isPureXfa", !!this._transport._htmlForXfa);
  11696. }
  11697. async getXfa() {
  11698. return this._transport._htmlForXfa?.children[this._pageIndex] || null;
  11699. }
  11700. render({
  11701. canvasContext,
  11702. viewport,
  11703. intent = "display",
  11704. annotationMode = AnnotationMode.ENABLE,
  11705. transform = null,
  11706. background = null,
  11707. optionalContentConfigPromise = null,
  11708. annotationCanvasMap = null,
  11709. pageColors = null,
  11710. printAnnotationStorage = null,
  11711. isEditing = false
  11712. }) {
  11713. this._stats?.time("Overall");
  11714. const intentArgs = this._transport.getRenderingIntent(intent, annotationMode, printAnnotationStorage, isEditing);
  11715. const {
  11716. renderingIntent,
  11717. cacheKey
  11718. } = intentArgs;
  11719. this.#pendingCleanup = false;
  11720. optionalContentConfigPromise ||= this._transport.getOptionalContentConfig(renderingIntent);
  11721. let intentState = this._intentStates.get(cacheKey);
  11722. if (!intentState) {
  11723. intentState = Object.create(null);
  11724. this._intentStates.set(cacheKey, intentState);
  11725. }
  11726. if (intentState.streamReaderCancelTimeout) {
  11727. clearTimeout(intentState.streamReaderCancelTimeout);
  11728. intentState.streamReaderCancelTimeout = null;
  11729. }
  11730. const intentPrint = !!(renderingIntent & RenderingIntentFlag.PRINT);
  11731. if (!intentState.displayReadyCapability) {
  11732. intentState.displayReadyCapability = Promise.withResolvers();
  11733. intentState.operatorList = {
  11734. fnArray: [],
  11735. argsArray: [],
  11736. lastChunk: false,
  11737. separateAnnots: null
  11738. };
  11739. this._stats?.time("Page Request");
  11740. this._pumpOperatorList(intentArgs);
  11741. }
  11742. const complete = error => {
  11743. intentState.renderTasks.delete(internalRenderTask);
  11744. if (intentPrint) {
  11745. this.#pendingCleanup = true;
  11746. }
  11747. this.#tryCleanup();
  11748. if (error) {
  11749. internalRenderTask.capability.reject(error);
  11750. this._abortOperatorList({
  11751. intentState,
  11752. reason: error instanceof Error ? error : new Error(error)
  11753. });
  11754. } else {
  11755. internalRenderTask.capability.resolve();
  11756. }
  11757. if (this._stats) {
  11758. this._stats.timeEnd("Rendering");
  11759. this._stats.timeEnd("Overall");
  11760. if (globalThis.Stats?.enabled) {
  11761. globalThis.Stats.add(this.pageNumber, this._stats);
  11762. }
  11763. }
  11764. };
  11765. const internalRenderTask = new InternalRenderTask({
  11766. callback: complete,
  11767. params: {
  11768. canvasContext,
  11769. viewport,
  11770. transform,
  11771. background
  11772. },
  11773. objs: this.objs,
  11774. commonObjs: this.commonObjs,
  11775. annotationCanvasMap,
  11776. operatorList: intentState.operatorList,
  11777. pageIndex: this._pageIndex,
  11778. canvasFactory: this._transport.canvasFactory,
  11779. filterFactory: this._transport.filterFactory,
  11780. useRequestAnimationFrame: !intentPrint,
  11781. pdfBug: this._pdfBug,
  11782. pageColors
  11783. });
  11784. (intentState.renderTasks ||= new Set()).add(internalRenderTask);
  11785. const renderTask = internalRenderTask.task;
  11786. Promise.all([intentState.displayReadyCapability.promise, optionalContentConfigPromise]).then(([transparency, optionalContentConfig]) => {
  11787. if (this.destroyed) {
  11788. complete();
  11789. return;
  11790. }
  11791. this._stats?.time("Rendering");
  11792. if (!(optionalContentConfig.renderingIntent & renderingIntent)) {
  11793. throw new Error("Must use the same `intent`-argument when calling the `PDFPageProxy.render` " + "and `PDFDocumentProxy.getOptionalContentConfig` methods.");
  11794. }
  11795. internalRenderTask.initializeGraphics({
  11796. transparency,
  11797. optionalContentConfig
  11798. });
  11799. internalRenderTask.operatorListChanged();
  11800. }).catch(complete);
  11801. return renderTask;
  11802. }
  11803. getOperatorList({
  11804. intent = "display",
  11805. annotationMode = AnnotationMode.ENABLE,
  11806. printAnnotationStorage = null,
  11807. isEditing = false
  11808. } = {}) {
  11809. function operatorListChanged() {
  11810. if (intentState.operatorList.lastChunk) {
  11811. intentState.opListReadCapability.resolve(intentState.operatorList);
  11812. intentState.renderTasks.delete(opListTask);
  11813. }
  11814. }
  11815. const intentArgs = this._transport.getRenderingIntent(intent, annotationMode, printAnnotationStorage, isEditing, true);
  11816. let intentState = this._intentStates.get(intentArgs.cacheKey);
  11817. if (!intentState) {
  11818. intentState = Object.create(null);
  11819. this._intentStates.set(intentArgs.cacheKey, intentState);
  11820. }
  11821. let opListTask;
  11822. if (!intentState.opListReadCapability) {
  11823. opListTask = Object.create(null);
  11824. opListTask.operatorListChanged = operatorListChanged;
  11825. intentState.opListReadCapability = Promise.withResolvers();
  11826. (intentState.renderTasks ||= new Set()).add(opListTask);
  11827. intentState.operatorList = {
  11828. fnArray: [],
  11829. argsArray: [],
  11830. lastChunk: false,
  11831. separateAnnots: null
  11832. };
  11833. this._stats?.time("Page Request");
  11834. this._pumpOperatorList(intentArgs);
  11835. }
  11836. return intentState.opListReadCapability.promise;
  11837. }
  11838. streamTextContent({
  11839. includeMarkedContent = false,
  11840. disableNormalization = false
  11841. } = {}) {
  11842. const TEXT_CONTENT_CHUNK_SIZE = 100;
  11843. return this._transport.messageHandler.sendWithStream("GetTextContent", {
  11844. pageIndex: this._pageIndex,
  11845. includeMarkedContent: includeMarkedContent === true,
  11846. disableNormalization: disableNormalization === true
  11847. }, {
  11848. highWaterMark: TEXT_CONTENT_CHUNK_SIZE,
  11849. size(textContent) {
  11850. return textContent.items.length;
  11851. }
  11852. });
  11853. }
  11854. getTextContent(params = {}) {
  11855. if (this._transport._htmlForXfa) {
  11856. return this.getXfa().then(xfa => XfaText.textContent(xfa));
  11857. }
  11858. const readableStream = this.streamTextContent(params);
  11859. return new Promise(function (resolve, reject) {
  11860. function pump() {
  11861. reader.read().then(function ({
  11862. value,
  11863. done
  11864. }) {
  11865. if (done) {
  11866. resolve(textContent);
  11867. return;
  11868. }
  11869. textContent.lang ??= value.lang;
  11870. Object.assign(textContent.styles, value.styles);
  11871. textContent.items.push(...value.items);
  11872. pump();
  11873. }, reject);
  11874. }
  11875. const reader = readableStream.getReader();
  11876. const textContent = {
  11877. items: [],
  11878. styles: Object.create(null),
  11879. lang: null
  11880. };
  11881. pump();
  11882. });
  11883. }
  11884. getStructTree() {
  11885. return this._transport.getStructTree(this._pageIndex);
  11886. }
  11887. _destroy() {
  11888. this.destroyed = true;
  11889. const waitOn = [];
  11890. for (const intentState of this._intentStates.values()) {
  11891. this._abortOperatorList({
  11892. intentState,
  11893. reason: new Error("Page was destroyed."),
  11894. force: true
  11895. });
  11896. if (intentState.opListReadCapability) {
  11897. continue;
  11898. }
  11899. for (const internalRenderTask of intentState.renderTasks) {
  11900. waitOn.push(internalRenderTask.completed);
  11901. internalRenderTask.cancel();
  11902. }
  11903. }
  11904. this.objs.clear();
  11905. this.#pendingCleanup = false;
  11906. return Promise.all(waitOn);
  11907. }
  11908. cleanup(resetStats = false) {
  11909. this.#pendingCleanup = true;
  11910. const success = this.#tryCleanup();
  11911. if (resetStats && success) {
  11912. this._stats &&= new StatTimer();
  11913. }
  11914. return success;
  11915. }
  11916. #tryCleanup() {
  11917. if (!this.#pendingCleanup || this.destroyed) {
  11918. return false;
  11919. }
  11920. for (const {
  11921. renderTasks,
  11922. operatorList
  11923. } of this._intentStates.values()) {
  11924. if (renderTasks.size > 0 || !operatorList.lastChunk) {
  11925. return false;
  11926. }
  11927. }
  11928. this._intentStates.clear();
  11929. this.objs.clear();
  11930. this.#pendingCleanup = false;
  11931. return true;
  11932. }
  11933. _startRenderPage(transparency, cacheKey) {
  11934. const intentState = this._intentStates.get(cacheKey);
  11935. if (!intentState) {
  11936. return;
  11937. }
  11938. this._stats?.timeEnd("Page Request");
  11939. intentState.displayReadyCapability?.resolve(transparency);
  11940. }
  11941. _renderPageChunk(operatorListChunk, intentState) {
  11942. for (let i = 0, ii = operatorListChunk.length; i < ii; i++) {
  11943. intentState.operatorList.fnArray.push(operatorListChunk.fnArray[i]);
  11944. intentState.operatorList.argsArray.push(operatorListChunk.argsArray[i]);
  11945. }
  11946. intentState.operatorList.lastChunk = operatorListChunk.lastChunk;
  11947. intentState.operatorList.separateAnnots = operatorListChunk.separateAnnots;
  11948. for (const internalRenderTask of intentState.renderTasks) {
  11949. internalRenderTask.operatorListChanged();
  11950. }
  11951. if (operatorListChunk.lastChunk) {
  11952. this.#tryCleanup();
  11953. }
  11954. }
  11955. _pumpOperatorList({
  11956. renderingIntent,
  11957. cacheKey,
  11958. annotationStorageSerializable,
  11959. modifiedIds
  11960. }) {
  11961. const {
  11962. map,
  11963. transfer
  11964. } = annotationStorageSerializable;
  11965. const readableStream = this._transport.messageHandler.sendWithStream("GetOperatorList", {
  11966. pageIndex: this._pageIndex,
  11967. intent: renderingIntent,
  11968. cacheKey,
  11969. annotationStorage: map,
  11970. modifiedIds
  11971. }, transfer);
  11972. const reader = readableStream.getReader();
  11973. const intentState = this._intentStates.get(cacheKey);
  11974. intentState.streamReader = reader;
  11975. const pump = () => {
  11976. reader.read().then(({
  11977. value,
  11978. done
  11979. }) => {
  11980. if (done) {
  11981. intentState.streamReader = null;
  11982. return;
  11983. }
  11984. if (this._transport.destroyed) {
  11985. return;
  11986. }
  11987. this._renderPageChunk(value, intentState);
  11988. pump();
  11989. }, reason => {
  11990. intentState.streamReader = null;
  11991. if (this._transport.destroyed) {
  11992. return;
  11993. }
  11994. if (intentState.operatorList) {
  11995. intentState.operatorList.lastChunk = true;
  11996. for (const internalRenderTask of intentState.renderTasks) {
  11997. internalRenderTask.operatorListChanged();
  11998. }
  11999. this.#tryCleanup();
  12000. }
  12001. if (intentState.displayReadyCapability) {
  12002. intentState.displayReadyCapability.reject(reason);
  12003. } else if (intentState.opListReadCapability) {
  12004. intentState.opListReadCapability.reject(reason);
  12005. } else {
  12006. throw reason;
  12007. }
  12008. });
  12009. };
  12010. pump();
  12011. }
  12012. _abortOperatorList({
  12013. intentState,
  12014. reason,
  12015. force = false
  12016. }) {
  12017. if (!intentState.streamReader) {
  12018. return;
  12019. }
  12020. if (intentState.streamReaderCancelTimeout) {
  12021. clearTimeout(intentState.streamReaderCancelTimeout);
  12022. intentState.streamReaderCancelTimeout = null;
  12023. }
  12024. if (!force) {
  12025. if (intentState.renderTasks.size > 0) {
  12026. return;
  12027. }
  12028. if (reason instanceof RenderingCancelledException) {
  12029. let delay = RENDERING_CANCELLED_TIMEOUT;
  12030. if (reason.extraDelay > 0 && reason.extraDelay < 1000) {
  12031. delay += reason.extraDelay;
  12032. }
  12033. intentState.streamReaderCancelTimeout = setTimeout(() => {
  12034. intentState.streamReaderCancelTimeout = null;
  12035. this._abortOperatorList({
  12036. intentState,
  12037. reason,
  12038. force: true
  12039. });
  12040. }, delay);
  12041. return;
  12042. }
  12043. }
  12044. intentState.streamReader.cancel(new AbortException(reason.message)).catch(() => {});
  12045. intentState.streamReader = null;
  12046. if (this._transport.destroyed) {
  12047. return;
  12048. }
  12049. for (const [curCacheKey, curIntentState] of this._intentStates) {
  12050. if (curIntentState === intentState) {
  12051. this._intentStates.delete(curCacheKey);
  12052. break;
  12053. }
  12054. }
  12055. this.cleanup();
  12056. }
  12057. get stats() {
  12058. return this._stats;
  12059. }
  12060. }
  12061. class LoopbackPort {
  12062. #listeners = new Map();
  12063. #deferred = Promise.resolve();
  12064. postMessage(obj, transfer) {
  12065. const event = {
  12066. data: structuredClone(obj, transfer ? {
  12067. transfer
  12068. } : null)
  12069. };
  12070. this.#deferred.then(() => {
  12071. for (const [listener] of this.#listeners) {
  12072. listener.call(this, event);
  12073. }
  12074. });
  12075. }
  12076. addEventListener(name, listener, options = null) {
  12077. let rmAbort = null;
  12078. if (options?.signal instanceof AbortSignal) {
  12079. const {
  12080. signal
  12081. } = options;
  12082. if (signal.aborted) {
  12083. warn("LoopbackPort - cannot use an `aborted` signal.");
  12084. return;
  12085. }
  12086. const onAbort = () => this.removeEventListener(name, listener);
  12087. rmAbort = () => signal.removeEventListener("abort", onAbort);
  12088. signal.addEventListener("abort", onAbort);
  12089. }
  12090. this.#listeners.set(listener, rmAbort);
  12091. }
  12092. removeEventListener(name, listener) {
  12093. const rmAbort = this.#listeners.get(listener);
  12094. rmAbort?.();
  12095. this.#listeners.delete(listener);
  12096. }
  12097. terminate() {
  12098. for (const [, rmAbort] of this.#listeners) {
  12099. rmAbort?.();
  12100. }
  12101. this.#listeners.clear();
  12102. }
  12103. }
  12104. class PDFWorker {
  12105. static #fakeWorkerId = 0;
  12106. static #isWorkerDisabled = false;
  12107. static #workerPorts;
  12108. static {
  12109. if (isNodeJS) {
  12110. this.#isWorkerDisabled = true;
  12111. GlobalWorkerOptions.workerSrc ||= "./pdf.worker.mjs";
  12112. }
  12113. this._isSameOrigin = (baseUrl, otherUrl) => {
  12114. const base = URL.parse(baseUrl);
  12115. if (!base?.origin || base.origin === "null") {
  12116. return false;
  12117. }
  12118. const other = new URL(otherUrl, base);
  12119. return base.origin === other.origin;
  12120. };
  12121. this._createCDNWrapper = url => {
  12122. const wrapper = `await import("${url}");`;
  12123. return URL.createObjectURL(new Blob([wrapper], {
  12124. type: "text/javascript"
  12125. }));
  12126. };
  12127. }
  12128. constructor({
  12129. name = null,
  12130. port = null,
  12131. verbosity = getVerbosityLevel()
  12132. } = {}) {
  12133. this.name = name;
  12134. this.destroyed = false;
  12135. this.verbosity = verbosity;
  12136. this._readyCapability = Promise.withResolvers();
  12137. this._port = null;
  12138. this._webWorker = null;
  12139. this._messageHandler = null;
  12140. if (port) {
  12141. if (PDFWorker.#workerPorts?.has(port)) {
  12142. throw new Error("Cannot use more than one PDFWorker per port.");
  12143. }
  12144. (PDFWorker.#workerPorts ||= new WeakMap()).set(port, this);
  12145. this._initializeFromPort(port);
  12146. return;
  12147. }
  12148. this._initialize();
  12149. }
  12150. get promise() {
  12151. return this._readyCapability.promise;
  12152. }
  12153. #resolve() {
  12154. this._readyCapability.resolve();
  12155. this._messageHandler.send("configure", {
  12156. verbosity: this.verbosity
  12157. });
  12158. }
  12159. get port() {
  12160. return this._port;
  12161. }
  12162. get messageHandler() {
  12163. return this._messageHandler;
  12164. }
  12165. _initializeFromPort(port) {
  12166. this._port = port;
  12167. this._messageHandler = new MessageHandler("main", "worker", port);
  12168. this._messageHandler.on("ready", function () {});
  12169. this.#resolve();
  12170. }
  12171. _initialize() {
  12172. if (PDFWorker.#isWorkerDisabled || PDFWorker.#mainThreadWorkerMessageHandler) {
  12173. this._setupFakeWorker();
  12174. return;
  12175. }
  12176. let {
  12177. workerSrc
  12178. } = PDFWorker;
  12179. try {
  12180. if (!PDFWorker._isSameOrigin(window.location, workerSrc)) {
  12181. workerSrc = PDFWorker._createCDNWrapper(new URL(workerSrc, window.location).href);
  12182. }
  12183. const worker = new Worker(workerSrc, {
  12184. type: "module"
  12185. });
  12186. const messageHandler = new MessageHandler("main", "worker", worker);
  12187. const terminateEarly = () => {
  12188. ac.abort();
  12189. messageHandler.destroy();
  12190. worker.terminate();
  12191. if (this.destroyed) {
  12192. this._readyCapability.reject(new Error("Worker was destroyed"));
  12193. } else {
  12194. this._setupFakeWorker();
  12195. }
  12196. };
  12197. const ac = new AbortController();
  12198. worker.addEventListener("error", () => {
  12199. if (!this._webWorker) {
  12200. terminateEarly();
  12201. }
  12202. }, {
  12203. signal: ac.signal
  12204. });
  12205. messageHandler.on("test", data => {
  12206. ac.abort();
  12207. if (this.destroyed || !data) {
  12208. terminateEarly();
  12209. return;
  12210. }
  12211. this._messageHandler = messageHandler;
  12212. this._port = worker;
  12213. this._webWorker = worker;
  12214. this.#resolve();
  12215. });
  12216. messageHandler.on("ready", data => {
  12217. ac.abort();
  12218. if (this.destroyed) {
  12219. terminateEarly();
  12220. return;
  12221. }
  12222. try {
  12223. sendTest();
  12224. } catch {
  12225. this._setupFakeWorker();
  12226. }
  12227. });
  12228. const sendTest = () => {
  12229. const testObj = new Uint8Array();
  12230. messageHandler.send("test", testObj, [testObj.buffer]);
  12231. };
  12232. sendTest();
  12233. return;
  12234. } catch {
  12235. info("The worker has been disabled.");
  12236. }
  12237. this._setupFakeWorker();
  12238. }
  12239. _setupFakeWorker() {
  12240. if (!PDFWorker.#isWorkerDisabled) {
  12241. warn("Setting up fake worker.");
  12242. PDFWorker.#isWorkerDisabled = true;
  12243. }
  12244. PDFWorker._setupFakeWorkerGlobal.then(WorkerMessageHandler => {
  12245. if (this.destroyed) {
  12246. this._readyCapability.reject(new Error("Worker was destroyed"));
  12247. return;
  12248. }
  12249. const port = new LoopbackPort();
  12250. this._port = port;
  12251. const id = `fake${PDFWorker.#fakeWorkerId++}`;
  12252. const workerHandler = new MessageHandler(id + "_worker", id, port);
  12253. WorkerMessageHandler.setup(workerHandler, port);
  12254. this._messageHandler = new MessageHandler(id, id + "_worker", port);
  12255. this.#resolve();
  12256. }).catch(reason => {
  12257. this._readyCapability.reject(new Error(`Setting up fake worker failed: "${reason.message}".`));
  12258. });
  12259. }
  12260. destroy() {
  12261. this.destroyed = true;
  12262. this._webWorker?.terminate();
  12263. this._webWorker = null;
  12264. PDFWorker.#workerPorts?.delete(this._port);
  12265. this._port = null;
  12266. this._messageHandler?.destroy();
  12267. this._messageHandler = null;
  12268. }
  12269. static fromPort(params) {
  12270. if (!params?.port) {
  12271. throw new Error("PDFWorker.fromPort - invalid method signature.");
  12272. }
  12273. const cachedPort = this.#workerPorts?.get(params.port);
  12274. if (cachedPort) {
  12275. if (cachedPort._pendingDestroy) {
  12276. throw new Error("PDFWorker.fromPort - the worker is being destroyed.\n" + "Please remember to await `PDFDocumentLoadingTask.destroy()`-calls.");
  12277. }
  12278. return cachedPort;
  12279. }
  12280. return new PDFWorker(params);
  12281. }
  12282. static get workerSrc() {
  12283. if (GlobalWorkerOptions.workerSrc) {
  12284. return GlobalWorkerOptions.workerSrc;
  12285. }
  12286. throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
  12287. }
  12288. static get #mainThreadWorkerMessageHandler() {
  12289. try {
  12290. return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
  12291. } catch {
  12292. return null;
  12293. }
  12294. }
  12295. static get _setupFakeWorkerGlobal() {
  12296. const loader = async () => {
  12297. if (this.#mainThreadWorkerMessageHandler) {
  12298. return this.#mainThreadWorkerMessageHandler;
  12299. }
  12300. const worker = await import(
  12301. /*webpackIgnore: true*/
  12302. /*@vite-ignore*/
  12303. this.workerSrc);
  12304. return worker.WorkerMessageHandler;
  12305. };
  12306. return shadow(this, "_setupFakeWorkerGlobal", loader());
  12307. }
  12308. }
  12309. class WorkerTransport {
  12310. #methodPromises = new Map();
  12311. #pageCache = new Map();
  12312. #pagePromises = new Map();
  12313. #pageRefCache = new Map();
  12314. #passwordCapability = null;
  12315. constructor(messageHandler, loadingTask, networkStream, params, factory) {
  12316. this.messageHandler = messageHandler;
  12317. this.loadingTask = loadingTask;
  12318. this.commonObjs = new PDFObjects();
  12319. this.fontLoader = new FontLoader({
  12320. ownerDocument: params.ownerDocument,
  12321. styleElement: params.styleElement
  12322. });
  12323. this.loadingParams = params.loadingParams;
  12324. this._params = params;
  12325. this.canvasFactory = factory.canvasFactory;
  12326. this.filterFactory = factory.filterFactory;
  12327. this.cMapReaderFactory = factory.cMapReaderFactory;
  12328. this.standardFontDataFactory = factory.standardFontDataFactory;
  12329. this.wasmFactory = factory.wasmFactory;
  12330. this.destroyed = false;
  12331. this.destroyCapability = null;
  12332. this._networkStream = networkStream;
  12333. this._fullReader = null;
  12334. this._lastProgress = null;
  12335. this.downloadInfoCapability = Promise.withResolvers();
  12336. this.setupMessageHandler();
  12337. }
  12338. #cacheSimpleMethod(name, data = null) {
  12339. const cachedPromise = this.#methodPromises.get(name);
  12340. if (cachedPromise) {
  12341. return cachedPromise;
  12342. }
  12343. const promise = this.messageHandler.sendWithPromise(name, data);
  12344. this.#methodPromises.set(name, promise);
  12345. return promise;
  12346. }
  12347. get annotationStorage() {
  12348. return shadow(this, "annotationStorage", new AnnotationStorage());
  12349. }
  12350. getRenderingIntent(intent, annotationMode = AnnotationMode.ENABLE, printAnnotationStorage = null, isEditing = false, isOpList = false) {
  12351. let renderingIntent = RenderingIntentFlag.DISPLAY;
  12352. let annotationStorageSerializable = SerializableEmpty;
  12353. switch (intent) {
  12354. case "any":
  12355. renderingIntent = RenderingIntentFlag.ANY;
  12356. break;
  12357. case "display":
  12358. break;
  12359. case "print":
  12360. renderingIntent = RenderingIntentFlag.PRINT;
  12361. break;
  12362. default:
  12363. warn(`getRenderingIntent - invalid intent: ${intent}`);
  12364. }
  12365. const annotationStorage = renderingIntent & RenderingIntentFlag.PRINT && printAnnotationStorage instanceof PrintAnnotationStorage ? printAnnotationStorage : this.annotationStorage;
  12366. switch (annotationMode) {
  12367. case AnnotationMode.DISABLE:
  12368. renderingIntent += RenderingIntentFlag.ANNOTATIONS_DISABLE;
  12369. break;
  12370. case AnnotationMode.ENABLE:
  12371. break;
  12372. case AnnotationMode.ENABLE_FORMS:
  12373. renderingIntent += RenderingIntentFlag.ANNOTATIONS_FORMS;
  12374. break;
  12375. case AnnotationMode.ENABLE_STORAGE:
  12376. renderingIntent += RenderingIntentFlag.ANNOTATIONS_STORAGE;
  12377. annotationStorageSerializable = annotationStorage.serializable;
  12378. break;
  12379. default:
  12380. warn(`getRenderingIntent - invalid annotationMode: ${annotationMode}`);
  12381. }
  12382. if (isEditing) {
  12383. renderingIntent += RenderingIntentFlag.IS_EDITING;
  12384. }
  12385. if (isOpList) {
  12386. renderingIntent += RenderingIntentFlag.OPLIST;
  12387. }
  12388. const {
  12389. ids: modifiedIds,
  12390. hash: modifiedIdsHash
  12391. } = annotationStorage.modifiedIds;
  12392. const cacheKeyBuf = [renderingIntent, annotationStorageSerializable.hash, modifiedIdsHash];
  12393. return {
  12394. renderingIntent,
  12395. cacheKey: cacheKeyBuf.join("_"),
  12396. annotationStorageSerializable,
  12397. modifiedIds
  12398. };
  12399. }
  12400. destroy() {
  12401. if (this.destroyCapability) {
  12402. return this.destroyCapability.promise;
  12403. }
  12404. this.destroyed = true;
  12405. this.destroyCapability = Promise.withResolvers();
  12406. this.#passwordCapability?.reject(new Error("Worker was destroyed during onPassword callback"));
  12407. const waitOn = [];
  12408. for (const page of this.#pageCache.values()) {
  12409. waitOn.push(page._destroy());
  12410. }
  12411. this.#pageCache.clear();
  12412. this.#pagePromises.clear();
  12413. this.#pageRefCache.clear();
  12414. if (this.hasOwnProperty("annotationStorage")) {
  12415. this.annotationStorage.resetModified();
  12416. }
  12417. const terminated = this.messageHandler.sendWithPromise("Terminate", null);
  12418. waitOn.push(terminated);
  12419. Promise.all(waitOn).then(() => {
  12420. this.commonObjs.clear();
  12421. this.fontLoader.clear();
  12422. this.#methodPromises.clear();
  12423. this.filterFactory.destroy();
  12424. TextLayer.cleanup();
  12425. this._networkStream?.cancelAllRequests(new AbortException("Worker was terminated."));
  12426. this.messageHandler?.destroy();
  12427. this.messageHandler = null;
  12428. this.destroyCapability.resolve();
  12429. }, this.destroyCapability.reject);
  12430. return this.destroyCapability.promise;
  12431. }
  12432. setupMessageHandler() {
  12433. const {
  12434. messageHandler,
  12435. loadingTask
  12436. } = this;
  12437. messageHandler.on("GetReader", (data, sink) => {
  12438. assert(this._networkStream, "GetReader - no `IPDFStream` instance available.");
  12439. this._fullReader = this._networkStream.getFullReader();
  12440. this._fullReader.onProgress = evt => {
  12441. this._lastProgress = {
  12442. loaded: evt.loaded,
  12443. total: evt.total
  12444. };
  12445. };
  12446. sink.onPull = () => {
  12447. this._fullReader.read().then(function ({
  12448. value,
  12449. done
  12450. }) {
  12451. if (done) {
  12452. sink.close();
  12453. return;
  12454. }
  12455. assert(value instanceof ArrayBuffer, "GetReader - expected an ArrayBuffer.");
  12456. sink.enqueue(new Uint8Array(value), 1, [value]);
  12457. }).catch(reason => {
  12458. sink.error(reason);
  12459. });
  12460. };
  12461. sink.onCancel = reason => {
  12462. this._fullReader.cancel(reason);
  12463. sink.ready.catch(readyReason => {
  12464. if (this.destroyed) {
  12465. return;
  12466. }
  12467. throw readyReason;
  12468. });
  12469. };
  12470. });
  12471. messageHandler.on("ReaderHeadersReady", async data => {
  12472. await this._fullReader.headersReady;
  12473. const {
  12474. isStreamingSupported,
  12475. isRangeSupported,
  12476. contentLength
  12477. } = this._fullReader;
  12478. if (!isStreamingSupported || !isRangeSupported) {
  12479. if (this._lastProgress) {
  12480. loadingTask.onProgress?.(this._lastProgress);
  12481. }
  12482. this._fullReader.onProgress = evt => {
  12483. loadingTask.onProgress?.({
  12484. loaded: evt.loaded,
  12485. total: evt.total
  12486. });
  12487. };
  12488. }
  12489. return {
  12490. isStreamingSupported,
  12491. isRangeSupported,
  12492. contentLength
  12493. };
  12494. });
  12495. messageHandler.on("GetRangeReader", (data, sink) => {
  12496. assert(this._networkStream, "GetRangeReader - no `IPDFStream` instance available.");
  12497. const rangeReader = this._networkStream.getRangeReader(data.begin, data.end);
  12498. if (!rangeReader) {
  12499. sink.close();
  12500. return;
  12501. }
  12502. sink.onPull = () => {
  12503. rangeReader.read().then(function ({
  12504. value,
  12505. done
  12506. }) {
  12507. if (done) {
  12508. sink.close();
  12509. return;
  12510. }
  12511. assert(value instanceof ArrayBuffer, "GetRangeReader - expected an ArrayBuffer.");
  12512. sink.enqueue(new Uint8Array(value), 1, [value]);
  12513. }).catch(reason => {
  12514. sink.error(reason);
  12515. });
  12516. };
  12517. sink.onCancel = reason => {
  12518. rangeReader.cancel(reason);
  12519. sink.ready.catch(readyReason => {
  12520. if (this.destroyed) {
  12521. return;
  12522. }
  12523. throw readyReason;
  12524. });
  12525. };
  12526. });
  12527. messageHandler.on("GetDoc", ({
  12528. pdfInfo
  12529. }) => {
  12530. this._numPages = pdfInfo.numPages;
  12531. this._htmlForXfa = pdfInfo.htmlForXfa;
  12532. delete pdfInfo.htmlForXfa;
  12533. loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
  12534. });
  12535. messageHandler.on("DocException", ex => {
  12536. loadingTask._capability.reject(wrapReason(ex));
  12537. });
  12538. messageHandler.on("PasswordRequest", ex => {
  12539. this.#passwordCapability = Promise.withResolvers();
  12540. try {
  12541. if (!loadingTask.onPassword) {
  12542. throw wrapReason(ex);
  12543. }
  12544. const updatePassword = password => {
  12545. if (password instanceof Error) {
  12546. this.#passwordCapability.reject(password);
  12547. } else {
  12548. this.#passwordCapability.resolve({
  12549. password
  12550. });
  12551. }
  12552. };
  12553. loadingTask.onPassword(updatePassword, ex.code);
  12554. } catch (err) {
  12555. this.#passwordCapability.reject(err);
  12556. }
  12557. return this.#passwordCapability.promise;
  12558. });
  12559. messageHandler.on("DataLoaded", data => {
  12560. loadingTask.onProgress?.({
  12561. loaded: data.length,
  12562. total: data.length
  12563. });
  12564. this.downloadInfoCapability.resolve(data);
  12565. });
  12566. messageHandler.on("StartRenderPage", data => {
  12567. if (this.destroyed) {
  12568. return;
  12569. }
  12570. const page = this.#pageCache.get(data.pageIndex);
  12571. page._startRenderPage(data.transparency, data.cacheKey);
  12572. });
  12573. messageHandler.on("commonobj", ([id, type, exportedData]) => {
  12574. if (this.destroyed) {
  12575. return null;
  12576. }
  12577. if (this.commonObjs.has(id)) {
  12578. return null;
  12579. }
  12580. switch (type) {
  12581. case "Font":
  12582. if ("error" in exportedData) {
  12583. const exportedError = exportedData.error;
  12584. warn(`Error during font loading: ${exportedError}`);
  12585. this.commonObjs.resolve(id, exportedError);
  12586. break;
  12587. }
  12588. const inspectFont = this._params.pdfBug && globalThis.FontInspector?.enabled ? (font, url) => globalThis.FontInspector.fontAdded(font, url) : null;
  12589. const font = new FontFaceObject(exportedData, inspectFont);
  12590. this.fontLoader.bind(font).catch(() => messageHandler.sendWithPromise("FontFallback", {
  12591. id
  12592. })).finally(() => {
  12593. if (!font.fontExtraProperties && font.data) {
  12594. font.data = null;
  12595. }
  12596. this.commonObjs.resolve(id, font);
  12597. });
  12598. break;
  12599. case "CopyLocalImage":
  12600. const {
  12601. imageRef
  12602. } = exportedData;
  12603. assert(imageRef, "The imageRef must be defined.");
  12604. for (const pageProxy of this.#pageCache.values()) {
  12605. for (const [, data] of pageProxy.objs) {
  12606. if (data?.ref !== imageRef) {
  12607. continue;
  12608. }
  12609. if (!data.dataLen) {
  12610. return null;
  12611. }
  12612. this.commonObjs.resolve(id, structuredClone(data));
  12613. return data.dataLen;
  12614. }
  12615. }
  12616. break;
  12617. case "FontPath":
  12618. case "Image":
  12619. case "Pattern":
  12620. this.commonObjs.resolve(id, exportedData);
  12621. break;
  12622. default:
  12623. throw new Error(`Got unknown common object type ${type}`);
  12624. }
  12625. return null;
  12626. });
  12627. messageHandler.on("obj", ([id, pageIndex, type, imageData]) => {
  12628. if (this.destroyed) {
  12629. return;
  12630. }
  12631. const pageProxy = this.#pageCache.get(pageIndex);
  12632. if (pageProxy.objs.has(id)) {
  12633. return;
  12634. }
  12635. if (pageProxy._intentStates.size === 0) {
  12636. imageData?.bitmap?.close();
  12637. return;
  12638. }
  12639. switch (type) {
  12640. case "Image":
  12641. case "Pattern":
  12642. pageProxy.objs.resolve(id, imageData);
  12643. break;
  12644. default:
  12645. throw new Error(`Got unknown object type ${type}`);
  12646. }
  12647. });
  12648. messageHandler.on("DocProgress", data => {
  12649. if (this.destroyed) {
  12650. return;
  12651. }
  12652. loadingTask.onProgress?.({
  12653. loaded: data.loaded,
  12654. total: data.total
  12655. });
  12656. });
  12657. messageHandler.on("FetchBinaryData", async data => {
  12658. if (this.destroyed) {
  12659. throw new Error("Worker was destroyed.");
  12660. }
  12661. const factory = this[data.type];
  12662. if (!factory) {
  12663. throw new Error(`${data.type} not initialized, see the \`useWorkerFetch\` parameter.`);
  12664. }
  12665. return factory.fetch(data);
  12666. });
  12667. }
  12668. getData() {
  12669. return this.messageHandler.sendWithPromise("GetData", null);
  12670. }
  12671. saveDocument() {
  12672. if (this.annotationStorage.size <= 0) {
  12673. warn("saveDocument called while `annotationStorage` is empty, " + "please use the getData-method instead.");
  12674. }
  12675. const {
  12676. map,
  12677. transfer
  12678. } = this.annotationStorage.serializable;
  12679. return this.messageHandler.sendWithPromise("SaveDocument", {
  12680. isPureXfa: !!this._htmlForXfa,
  12681. numPages: this._numPages,
  12682. annotationStorage: map,
  12683. filename: this._fullReader?.filename ?? null
  12684. }, transfer).finally(() => {
  12685. this.annotationStorage.resetModified();
  12686. });
  12687. }
  12688. getPage(pageNumber) {
  12689. if (!Number.isInteger(pageNumber) || pageNumber <= 0 || pageNumber > this._numPages) {
  12690. return Promise.reject(new Error("Invalid page request."));
  12691. }
  12692. const pageIndex = pageNumber - 1,
  12693. cachedPromise = this.#pagePromises.get(pageIndex);
  12694. if (cachedPromise) {
  12695. return cachedPromise;
  12696. }
  12697. const promise = this.messageHandler.sendWithPromise("GetPage", {
  12698. pageIndex
  12699. }).then(pageInfo => {
  12700. if (this.destroyed) {
  12701. throw new Error("Transport destroyed");
  12702. }
  12703. if (pageInfo.refStr) {
  12704. this.#pageRefCache.set(pageInfo.refStr, pageNumber);
  12705. }
  12706. const page = new PDFPageProxy(pageIndex, pageInfo, this, this._params.pdfBug);
  12707. this.#pageCache.set(pageIndex, page);
  12708. return page;
  12709. });
  12710. this.#pagePromises.set(pageIndex, promise);
  12711. return promise;
  12712. }
  12713. getPageIndex(ref) {
  12714. if (!isRefProxy(ref)) {
  12715. return Promise.reject(new Error("Invalid pageIndex request."));
  12716. }
  12717. return this.messageHandler.sendWithPromise("GetPageIndex", {
  12718. num: ref.num,
  12719. gen: ref.gen
  12720. });
  12721. }
  12722. getAnnotations(pageIndex, intent) {
  12723. return this.messageHandler.sendWithPromise("GetAnnotations", {
  12724. pageIndex,
  12725. intent
  12726. });
  12727. }
  12728. getFieldObjects() {
  12729. return this.#cacheSimpleMethod("GetFieldObjects");
  12730. }
  12731. hasJSActions() {
  12732. return this.#cacheSimpleMethod("HasJSActions");
  12733. }
  12734. getCalculationOrderIds() {
  12735. return this.messageHandler.sendWithPromise("GetCalculationOrderIds", null);
  12736. }
  12737. getDestinations() {
  12738. return this.messageHandler.sendWithPromise("GetDestinations", null);
  12739. }
  12740. getDestination(id) {
  12741. if (typeof id !== "string") {
  12742. return Promise.reject(new Error("Invalid destination request."));
  12743. }
  12744. return this.messageHandler.sendWithPromise("GetDestination", {
  12745. id
  12746. });
  12747. }
  12748. getPageLabels() {
  12749. return this.messageHandler.sendWithPromise("GetPageLabels", null);
  12750. }
  12751. getPageLayout() {
  12752. return this.messageHandler.sendWithPromise("GetPageLayout", null);
  12753. }
  12754. getPageMode() {
  12755. return this.messageHandler.sendWithPromise("GetPageMode", null);
  12756. }
  12757. getViewerPreferences() {
  12758. return this.messageHandler.sendWithPromise("GetViewerPreferences", null);
  12759. }
  12760. getOpenAction() {
  12761. return this.messageHandler.sendWithPromise("GetOpenAction", null);
  12762. }
  12763. getAttachments() {
  12764. return this.messageHandler.sendWithPromise("GetAttachments", null);
  12765. }
  12766. getDocJSActions() {
  12767. return this.#cacheSimpleMethod("GetDocJSActions");
  12768. }
  12769. getPageJSActions(pageIndex) {
  12770. return this.messageHandler.sendWithPromise("GetPageJSActions", {
  12771. pageIndex
  12772. });
  12773. }
  12774. getStructTree(pageIndex) {
  12775. return this.messageHandler.sendWithPromise("GetStructTree", {
  12776. pageIndex
  12777. });
  12778. }
  12779. getOutline() {
  12780. return this.messageHandler.sendWithPromise("GetOutline", null);
  12781. }
  12782. getOptionalContentConfig(renderingIntent) {
  12783. return this.#cacheSimpleMethod("GetOptionalContentConfig").then(data => new OptionalContentConfig(data, renderingIntent));
  12784. }
  12785. getPermissions() {
  12786. return this.messageHandler.sendWithPromise("GetPermissions", null);
  12787. }
  12788. getMetadata() {
  12789. const name = "GetMetadata",
  12790. cachedPromise = this.#methodPromises.get(name);
  12791. if (cachedPromise) {
  12792. return cachedPromise;
  12793. }
  12794. const promise = this.messageHandler.sendWithPromise(name, null).then(results => ({
  12795. info: results[0],
  12796. metadata: results[1] ? new Metadata(results[1]) : null,
  12797. contentDispositionFilename: this._fullReader?.filename ?? null,
  12798. contentLength: this._fullReader?.contentLength ?? null
  12799. }));
  12800. this.#methodPromises.set(name, promise);
  12801. return promise;
  12802. }
  12803. getMarkInfo() {
  12804. return this.messageHandler.sendWithPromise("GetMarkInfo", null);
  12805. }
  12806. async startCleanup(keepLoadedFonts = false) {
  12807. if (this.destroyed) {
  12808. return;
  12809. }
  12810. await this.messageHandler.sendWithPromise("Cleanup", null);
  12811. for (const page of this.#pageCache.values()) {
  12812. const cleanupSuccessful = page.cleanup();
  12813. if (!cleanupSuccessful) {
  12814. throw new Error(`startCleanup: Page ${page.pageNumber} is currently rendering.`);
  12815. }
  12816. }
  12817. this.commonObjs.clear();
  12818. if (!keepLoadedFonts) {
  12819. this.fontLoader.clear();
  12820. }
  12821. this.#methodPromises.clear();
  12822. this.filterFactory.destroy(true);
  12823. TextLayer.cleanup();
  12824. }
  12825. cachedPageNumber(ref) {
  12826. if (!isRefProxy(ref)) {
  12827. return null;
  12828. }
  12829. const refStr = ref.gen === 0 ? `${ref.num}R` : `${ref.num}R${ref.gen}`;
  12830. return this.#pageRefCache.get(refStr) ?? null;
  12831. }
  12832. }
  12833. const INITIAL_DATA = Symbol("INITIAL_DATA");
  12834. class PDFObjects {
  12835. #objs = Object.create(null);
  12836. #ensureObj(objId) {
  12837. return this.#objs[objId] ||= {
  12838. ...Promise.withResolvers(),
  12839. data: INITIAL_DATA
  12840. };
  12841. }
  12842. get(objId, callback = null) {
  12843. if (callback) {
  12844. const obj = this.#ensureObj(objId);
  12845. obj.promise.then(() => callback(obj.data));
  12846. return null;
  12847. }
  12848. const obj = this.#objs[objId];
  12849. if (!obj || obj.data === INITIAL_DATA) {
  12850. throw new Error(`Requesting object that isn't resolved yet ${objId}.`);
  12851. }
  12852. return obj.data;
  12853. }
  12854. has(objId) {
  12855. const obj = this.#objs[objId];
  12856. return !!obj && obj.data !== INITIAL_DATA;
  12857. }
  12858. delete(objId) {
  12859. const obj = this.#objs[objId];
  12860. if (!obj || obj.data === INITIAL_DATA) {
  12861. return false;
  12862. }
  12863. delete this.#objs[objId];
  12864. return true;
  12865. }
  12866. resolve(objId, data = null) {
  12867. const obj = this.#ensureObj(objId);
  12868. obj.data = data;
  12869. obj.resolve();
  12870. }
  12871. clear() {
  12872. for (const objId in this.#objs) {
  12873. const {
  12874. data
  12875. } = this.#objs[objId];
  12876. data?.bitmap?.close();
  12877. }
  12878. this.#objs = Object.create(null);
  12879. }
  12880. *[Symbol.iterator]() {
  12881. for (const objId in this.#objs) {
  12882. const {
  12883. data
  12884. } = this.#objs[objId];
  12885. if (data === INITIAL_DATA) {
  12886. continue;
  12887. }
  12888. yield [objId, data];
  12889. }
  12890. }
  12891. }
  12892. class RenderTask {
  12893. #internalRenderTask = null;
  12894. onContinue = null;
  12895. onError = null;
  12896. constructor(internalRenderTask) {
  12897. this.#internalRenderTask = internalRenderTask;
  12898. }
  12899. get promise() {
  12900. return this.#internalRenderTask.capability.promise;
  12901. }
  12902. cancel(extraDelay = 0) {
  12903. this.#internalRenderTask.cancel(null, extraDelay);
  12904. }
  12905. get separateAnnots() {
  12906. const {
  12907. separateAnnots
  12908. } = this.#internalRenderTask.operatorList;
  12909. if (!separateAnnots) {
  12910. return false;
  12911. }
  12912. const {
  12913. annotationCanvasMap
  12914. } = this.#internalRenderTask;
  12915. return separateAnnots.form || separateAnnots.canvas && annotationCanvasMap?.size > 0;
  12916. }
  12917. }
  12918. class InternalRenderTask {
  12919. #rAF = null;
  12920. static #canvasInUse = new WeakSet();
  12921. constructor({
  12922. callback,
  12923. params,
  12924. objs,
  12925. commonObjs,
  12926. annotationCanvasMap,
  12927. operatorList,
  12928. pageIndex,
  12929. canvasFactory,
  12930. filterFactory,
  12931. useRequestAnimationFrame = false,
  12932. pdfBug = false,
  12933. pageColors = null
  12934. }) {
  12935. this.callback = callback;
  12936. this.params = params;
  12937. this.objs = objs;
  12938. this.commonObjs = commonObjs;
  12939. this.annotationCanvasMap = annotationCanvasMap;
  12940. this.operatorListIdx = null;
  12941. this.operatorList = operatorList;
  12942. this._pageIndex = pageIndex;
  12943. this.canvasFactory = canvasFactory;
  12944. this.filterFactory = filterFactory;
  12945. this._pdfBug = pdfBug;
  12946. this.pageColors = pageColors;
  12947. this.running = false;
  12948. this.graphicsReadyCallback = null;
  12949. this.graphicsReady = false;
  12950. this._useRequestAnimationFrame = useRequestAnimationFrame === true && typeof window !== "undefined";
  12951. this.cancelled = false;
  12952. this.capability = Promise.withResolvers();
  12953. this.task = new RenderTask(this);
  12954. this._cancelBound = this.cancel.bind(this);
  12955. this._continueBound = this._continue.bind(this);
  12956. this._scheduleNextBound = this._scheduleNext.bind(this);
  12957. this._nextBound = this._next.bind(this);
  12958. this._canvas = params.canvasContext.canvas;
  12959. }
  12960. get completed() {
  12961. return this.capability.promise.catch(function () {});
  12962. }
  12963. initializeGraphics({
  12964. transparency = false,
  12965. optionalContentConfig
  12966. }) {
  12967. if (this.cancelled) {
  12968. return;
  12969. }
  12970. if (this._canvas) {
  12971. if (InternalRenderTask.#canvasInUse.has(this._canvas)) {
  12972. throw new Error("Cannot use the same canvas during multiple render() operations. " + "Use different canvas or ensure previous operations were " + "cancelled or completed.");
  12973. }
  12974. InternalRenderTask.#canvasInUse.add(this._canvas);
  12975. }
  12976. if (this._pdfBug && globalThis.StepperManager?.enabled) {
  12977. this.stepper = globalThis.StepperManager.create(this._pageIndex);
  12978. this.stepper.init(this.operatorList);
  12979. this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
  12980. }
  12981. const {
  12982. canvasContext,
  12983. viewport,
  12984. transform,
  12985. background
  12986. } = this.params;
  12987. this.gfx = new CanvasGraphics(canvasContext, this.commonObjs, this.objs, this.canvasFactory, this.filterFactory, {
  12988. optionalContentConfig
  12989. }, this.annotationCanvasMap, this.pageColors);
  12990. this.gfx.beginDrawing({
  12991. transform,
  12992. viewport,
  12993. transparency,
  12994. background
  12995. });
  12996. this.operatorListIdx = 0;
  12997. this.graphicsReady = true;
  12998. this.graphicsReadyCallback?.();
  12999. }
  13000. cancel(error = null, extraDelay = 0) {
  13001. this.running = false;
  13002. this.cancelled = true;
  13003. this.gfx?.endDrawing();
  13004. if (this.#rAF) {
  13005. window.cancelAnimationFrame(this.#rAF);
  13006. this.#rAF = null;
  13007. }
  13008. InternalRenderTask.#canvasInUse.delete(this._canvas);
  13009. error ||= new RenderingCancelledException(`Rendering cancelled, page ${this._pageIndex + 1}`, extraDelay);
  13010. this.callback(error);
  13011. this.task.onError?.(error);
  13012. }
  13013. operatorListChanged() {
  13014. if (!this.graphicsReady) {
  13015. this.graphicsReadyCallback ||= this._continueBound;
  13016. return;
  13017. }
  13018. this.stepper?.updateOperatorList(this.operatorList);
  13019. if (this.running) {
  13020. return;
  13021. }
  13022. this._continue();
  13023. }
  13024. _continue() {
  13025. this.running = true;
  13026. if (this.cancelled) {
  13027. return;
  13028. }
  13029. if (this.task.onContinue) {
  13030. this.task.onContinue(this._scheduleNextBound);
  13031. } else {
  13032. this._scheduleNext();
  13033. }
  13034. }
  13035. _scheduleNext() {
  13036. if (this._useRequestAnimationFrame) {
  13037. this.#rAF = window.requestAnimationFrame(() => {
  13038. this.#rAF = null;
  13039. this._nextBound().catch(this._cancelBound);
  13040. });
  13041. } else {
  13042. Promise.resolve().then(this._nextBound).catch(this._cancelBound);
  13043. }
  13044. }
  13045. async _next() {
  13046. if (this.cancelled) {
  13047. return;
  13048. }
  13049. this.operatorListIdx = this.gfx.executeOperatorList(this.operatorList, this.operatorListIdx, this._continueBound, this.stepper);
  13050. if (this.operatorListIdx === this.operatorList.argsArray.length) {
  13051. this.running = false;
  13052. if (this.operatorList.lastChunk) {
  13053. this.gfx.endDrawing();
  13054. InternalRenderTask.#canvasInUse.delete(this._canvas);
  13055. this.callback();
  13056. }
  13057. }
  13058. }
  13059. }
  13060. const version = "5.2.133";
  13061. const build = "4f7761353";
  13062. ;// ./src/shared/scripting_utils.js
  13063. function makeColorComp(n) {
  13064. return Math.floor(Math.max(0, Math.min(1, n)) * 255).toString(16).padStart(2, "0");
  13065. }
  13066. function scaleAndClamp(x) {
  13067. return Math.max(0, Math.min(255, 255 * x));
  13068. }
  13069. class ColorConverters {
  13070. static CMYK_G([c, y, m, k]) {
  13071. return ["G", 1 - Math.min(1, 0.3 * c + 0.59 * m + 0.11 * y + k)];
  13072. }
  13073. static G_CMYK([g]) {
  13074. return ["CMYK", 0, 0, 0, 1 - g];
  13075. }
  13076. static G_RGB([g]) {
  13077. return ["RGB", g, g, g];
  13078. }
  13079. static G_rgb([g]) {
  13080. g = scaleAndClamp(g);
  13081. return [g, g, g];
  13082. }
  13083. static G_HTML([g]) {
  13084. const G = makeColorComp(g);
  13085. return `#${G}${G}${G}`;
  13086. }
  13087. static RGB_G([r, g, b]) {
  13088. return ["G", 0.3 * r + 0.59 * g + 0.11 * b];
  13089. }
  13090. static RGB_rgb(color) {
  13091. return color.map(scaleAndClamp);
  13092. }
  13093. static RGB_HTML(color) {
  13094. return `#${color.map(makeColorComp).join("")}`;
  13095. }
  13096. static T_HTML() {
  13097. return "#00000000";
  13098. }
  13099. static T_rgb() {
  13100. return [null];
  13101. }
  13102. static CMYK_RGB([c, y, m, k]) {
  13103. return ["RGB", 1 - Math.min(1, c + k), 1 - Math.min(1, m + k), 1 - Math.min(1, y + k)];
  13104. }
  13105. static CMYK_rgb([c, y, m, k]) {
  13106. return [scaleAndClamp(1 - Math.min(1, c + k)), scaleAndClamp(1 - Math.min(1, m + k)), scaleAndClamp(1 - Math.min(1, y + k))];
  13107. }
  13108. static CMYK_HTML(components) {
  13109. const rgb = this.CMYK_RGB(components).slice(1);
  13110. return this.RGB_HTML(rgb);
  13111. }
  13112. static RGB_CMYK([r, g, b]) {
  13113. const c = 1 - r;
  13114. const m = 1 - g;
  13115. const y = 1 - b;
  13116. const k = Math.min(c, m, y);
  13117. return ["CMYK", c, m, y, k];
  13118. }
  13119. }
  13120. ;// ./src/display/svg_factory.js
  13121. class BaseSVGFactory {
  13122. create(width, height, skipDimensions = false) {
  13123. if (width <= 0 || height <= 0) {
  13124. throw new Error("Invalid SVG dimensions");
  13125. }
  13126. const svg = this._createSVG("svg:svg");
  13127. svg.setAttribute("version", "1.1");
  13128. if (!skipDimensions) {
  13129. svg.setAttribute("width", `${width}px`);
  13130. svg.setAttribute("height", `${height}px`);
  13131. }
  13132. svg.setAttribute("preserveAspectRatio", "none");
  13133. svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
  13134. return svg;
  13135. }
  13136. createElement(type) {
  13137. if (typeof type !== "string") {
  13138. throw new Error("Invalid SVG element type");
  13139. }
  13140. return this._createSVG(type);
  13141. }
  13142. _createSVG(type) {
  13143. unreachable("Abstract method `_createSVG` called.");
  13144. }
  13145. }
  13146. class DOMSVGFactory extends BaseSVGFactory {
  13147. _createSVG(type) {
  13148. return document.createElementNS(SVG_NS, type);
  13149. }
  13150. }
  13151. ;// ./src/display/xfa_layer.js
  13152. class XfaLayer {
  13153. static setupStorage(html, id, element, storage, intent) {
  13154. const storedData = storage.getValue(id, {
  13155. value: null
  13156. });
  13157. switch (element.name) {
  13158. case "textarea":
  13159. if (storedData.value !== null) {
  13160. html.textContent = storedData.value;
  13161. }
  13162. if (intent === "print") {
  13163. break;
  13164. }
  13165. html.addEventListener("input", event => {
  13166. storage.setValue(id, {
  13167. value: event.target.value
  13168. });
  13169. });
  13170. break;
  13171. case "input":
  13172. if (element.attributes.type === "radio" || element.attributes.type === "checkbox") {
  13173. if (storedData.value === element.attributes.xfaOn) {
  13174. html.setAttribute("checked", true);
  13175. } else if (storedData.value === element.attributes.xfaOff) {
  13176. html.removeAttribute("checked");
  13177. }
  13178. if (intent === "print") {
  13179. break;
  13180. }
  13181. html.addEventListener("change", event => {
  13182. storage.setValue(id, {
  13183. value: event.target.checked ? event.target.getAttribute("xfaOn") : event.target.getAttribute("xfaOff")
  13184. });
  13185. });
  13186. } else {
  13187. if (storedData.value !== null) {
  13188. html.setAttribute("value", storedData.value);
  13189. }
  13190. if (intent === "print") {
  13191. break;
  13192. }
  13193. html.addEventListener("input", event => {
  13194. storage.setValue(id, {
  13195. value: event.target.value
  13196. });
  13197. });
  13198. }
  13199. break;
  13200. case "select":
  13201. if (storedData.value !== null) {
  13202. html.setAttribute("value", storedData.value);
  13203. for (const option of element.children) {
  13204. if (option.attributes.value === storedData.value) {
  13205. option.attributes.selected = true;
  13206. } else if (option.attributes.hasOwnProperty("selected")) {
  13207. delete option.attributes.selected;
  13208. }
  13209. }
  13210. }
  13211. html.addEventListener("input", event => {
  13212. const options = event.target.options;
  13213. const value = options.selectedIndex === -1 ? "" : options[options.selectedIndex].value;
  13214. storage.setValue(id, {
  13215. value
  13216. });
  13217. });
  13218. break;
  13219. }
  13220. }
  13221. static setAttributes({
  13222. html,
  13223. element,
  13224. storage = null,
  13225. intent,
  13226. linkService
  13227. }) {
  13228. const {
  13229. attributes
  13230. } = element;
  13231. const isHTMLAnchorElement = html instanceof HTMLAnchorElement;
  13232. if (attributes.type === "radio") {
  13233. attributes.name = `${attributes.name}-${intent}`;
  13234. }
  13235. for (const [key, value] of Object.entries(attributes)) {
  13236. if (value === null || value === undefined) {
  13237. continue;
  13238. }
  13239. switch (key) {
  13240. case "class":
  13241. if (value.length) {
  13242. html.setAttribute(key, value.join(" "));
  13243. }
  13244. break;
  13245. case "dataId":
  13246. break;
  13247. case "id":
  13248. html.setAttribute("data-element-id", value);
  13249. break;
  13250. case "style":
  13251. Object.assign(html.style, value);
  13252. break;
  13253. case "textContent":
  13254. html.textContent = value;
  13255. break;
  13256. default:
  13257. if (!isHTMLAnchorElement || key !== "href" && key !== "newWindow") {
  13258. html.setAttribute(key, value);
  13259. }
  13260. }
  13261. }
  13262. if (isHTMLAnchorElement) {
  13263. linkService.addLinkAttributes(html, attributes.href, attributes.newWindow);
  13264. }
  13265. if (storage && attributes.dataId) {
  13266. this.setupStorage(html, attributes.dataId, element, storage);
  13267. }
  13268. }
  13269. static render(parameters) {
  13270. const storage = parameters.annotationStorage;
  13271. const linkService = parameters.linkService;
  13272. const root = parameters.xfaHtml;
  13273. const intent = parameters.intent || "display";
  13274. const rootHtml = document.createElement(root.name);
  13275. if (root.attributes) {
  13276. this.setAttributes({
  13277. html: rootHtml,
  13278. element: root,
  13279. intent,
  13280. linkService
  13281. });
  13282. }
  13283. const isNotForRichText = intent !== "richText";
  13284. const rootDiv = parameters.div;
  13285. rootDiv.append(rootHtml);
  13286. if (parameters.viewport) {
  13287. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  13288. rootDiv.style.transform = transform;
  13289. }
  13290. if (isNotForRichText) {
  13291. rootDiv.setAttribute("class", "xfaLayer xfaFont");
  13292. }
  13293. const textDivs = [];
  13294. if (root.children.length === 0) {
  13295. if (root.value) {
  13296. const node = document.createTextNode(root.value);
  13297. rootHtml.append(node);
  13298. if (isNotForRichText && XfaText.shouldBuildText(root.name)) {
  13299. textDivs.push(node);
  13300. }
  13301. }
  13302. return {
  13303. textDivs
  13304. };
  13305. }
  13306. const stack = [[root, -1, rootHtml]];
  13307. while (stack.length > 0) {
  13308. const [parent, i, html] = stack.at(-1);
  13309. if (i + 1 === parent.children.length) {
  13310. stack.pop();
  13311. continue;
  13312. }
  13313. const child = parent.children[++stack.at(-1)[1]];
  13314. if (child === null) {
  13315. continue;
  13316. }
  13317. const {
  13318. name
  13319. } = child;
  13320. if (name === "#text") {
  13321. const node = document.createTextNode(child.value);
  13322. textDivs.push(node);
  13323. html.append(node);
  13324. continue;
  13325. }
  13326. const childHtml = child?.attributes?.xmlns ? document.createElementNS(child.attributes.xmlns, name) : document.createElement(name);
  13327. html.append(childHtml);
  13328. if (child.attributes) {
  13329. this.setAttributes({
  13330. html: childHtml,
  13331. element: child,
  13332. storage,
  13333. intent,
  13334. linkService
  13335. });
  13336. }
  13337. if (child.children?.length > 0) {
  13338. stack.push([child, -1, childHtml]);
  13339. } else if (child.value) {
  13340. const node = document.createTextNode(child.value);
  13341. if (isNotForRichText && XfaText.shouldBuildText(name)) {
  13342. textDivs.push(node);
  13343. }
  13344. childHtml.append(node);
  13345. }
  13346. }
  13347. for (const el of rootDiv.querySelectorAll(".xfaNonInteractive input, .xfaNonInteractive textarea")) {
  13348. el.setAttribute("readOnly", true);
  13349. }
  13350. return {
  13351. textDivs
  13352. };
  13353. }
  13354. static update(parameters) {
  13355. const transform = `matrix(${parameters.viewport.transform.join(",")})`;
  13356. parameters.div.style.transform = transform;
  13357. parameters.div.hidden = false;
  13358. }
  13359. }
  13360. ;// ./src/display/annotation_layer.js
  13361. const DEFAULT_TAB_INDEX = 1000;
  13362. const annotation_layer_DEFAULT_FONT_SIZE = 9;
  13363. const GetElementsByNameSet = new WeakSet();
  13364. class AnnotationElementFactory {
  13365. static create(parameters) {
  13366. const subtype = parameters.data.annotationType;
  13367. switch (subtype) {
  13368. case AnnotationType.LINK:
  13369. return new LinkAnnotationElement(parameters);
  13370. case AnnotationType.TEXT:
  13371. return new TextAnnotationElement(parameters);
  13372. case AnnotationType.WIDGET:
  13373. const fieldType = parameters.data.fieldType;
  13374. switch (fieldType) {
  13375. case "Tx":
  13376. return new TextWidgetAnnotationElement(parameters);
  13377. case "Btn":
  13378. if (parameters.data.radioButton) {
  13379. return new RadioButtonWidgetAnnotationElement(parameters);
  13380. } else if (parameters.data.checkBox) {
  13381. return new CheckboxWidgetAnnotationElement(parameters);
  13382. }
  13383. return new PushButtonWidgetAnnotationElement(parameters);
  13384. case "Ch":
  13385. return new ChoiceWidgetAnnotationElement(parameters);
  13386. case "Sig":
  13387. return new SignatureWidgetAnnotationElement(parameters);
  13388. }
  13389. return new WidgetAnnotationElement(parameters);
  13390. case AnnotationType.POPUP:
  13391. return new PopupAnnotationElement(parameters);
  13392. case AnnotationType.FREETEXT:
  13393. return new FreeTextAnnotationElement(parameters);
  13394. case AnnotationType.LINE:
  13395. return new LineAnnotationElement(parameters);
  13396. case AnnotationType.SQUARE:
  13397. return new SquareAnnotationElement(parameters);
  13398. case AnnotationType.CIRCLE:
  13399. return new CircleAnnotationElement(parameters);
  13400. case AnnotationType.POLYLINE:
  13401. return new PolylineAnnotationElement(parameters);
  13402. case AnnotationType.CARET:
  13403. return new CaretAnnotationElement(parameters);
  13404. case AnnotationType.INK:
  13405. return new InkAnnotationElement(parameters);
  13406. case AnnotationType.POLYGON:
  13407. return new PolygonAnnotationElement(parameters);
  13408. case AnnotationType.HIGHLIGHT:
  13409. return new HighlightAnnotationElement(parameters);
  13410. case AnnotationType.UNDERLINE:
  13411. return new UnderlineAnnotationElement(parameters);
  13412. case AnnotationType.SQUIGGLY:
  13413. return new SquigglyAnnotationElement(parameters);
  13414. case AnnotationType.STRIKEOUT:
  13415. return new StrikeOutAnnotationElement(parameters);
  13416. case AnnotationType.STAMP:
  13417. return new StampAnnotationElement(parameters);
  13418. case AnnotationType.FILEATTACHMENT:
  13419. return new FileAttachmentAnnotationElement(parameters);
  13420. default:
  13421. return new AnnotationElement(parameters);
  13422. }
  13423. }
  13424. }
  13425. class AnnotationElement {
  13426. #updates = null;
  13427. #hasBorder = false;
  13428. #popupElement = null;
  13429. constructor(parameters, {
  13430. isRenderable = false,
  13431. ignoreBorder = false,
  13432. createQuadrilaterals = false
  13433. } = {}) {
  13434. this.isRenderable = isRenderable;
  13435. this.data = parameters.data;
  13436. this.layer = parameters.layer;
  13437. this.linkService = parameters.linkService;
  13438. this.downloadManager = parameters.downloadManager;
  13439. this.imageResourcesPath = parameters.imageResourcesPath;
  13440. this.renderForms = parameters.renderForms;
  13441. this.svgFactory = parameters.svgFactory;
  13442. this.annotationStorage = parameters.annotationStorage;
  13443. this.enableScripting = parameters.enableScripting;
  13444. this.hasJSActions = parameters.hasJSActions;
  13445. this._fieldObjects = parameters.fieldObjects;
  13446. this.parent = parameters.parent;
  13447. if (isRenderable) {
  13448. this.container = this._createContainer(ignoreBorder);
  13449. }
  13450. if (createQuadrilaterals) {
  13451. this._createQuadrilaterals();
  13452. }
  13453. }
  13454. static _hasPopupData({
  13455. titleObj,
  13456. contentsObj,
  13457. richText
  13458. }) {
  13459. return !!(titleObj?.str || contentsObj?.str || richText?.str);
  13460. }
  13461. get _isEditable() {
  13462. return this.data.isEditable;
  13463. }
  13464. get hasPopupData() {
  13465. return AnnotationElement._hasPopupData(this.data);
  13466. }
  13467. updateEdited(params) {
  13468. if (!this.container) {
  13469. return;
  13470. }
  13471. this.#updates ||= {
  13472. rect: this.data.rect.slice(0)
  13473. };
  13474. const {
  13475. rect
  13476. } = params;
  13477. if (rect) {
  13478. this.#setRectEdited(rect);
  13479. }
  13480. this.#popupElement?.popup.updateEdited(params);
  13481. }
  13482. resetEdited() {
  13483. if (!this.#updates) {
  13484. return;
  13485. }
  13486. this.#setRectEdited(this.#updates.rect);
  13487. this.#popupElement?.popup.resetEdited();
  13488. this.#updates = null;
  13489. }
  13490. #setRectEdited(rect) {
  13491. const {
  13492. container: {
  13493. style
  13494. },
  13495. data: {
  13496. rect: currentRect,
  13497. rotation
  13498. },
  13499. parent: {
  13500. viewport: {
  13501. rawDims: {
  13502. pageWidth,
  13503. pageHeight,
  13504. pageX,
  13505. pageY
  13506. }
  13507. }
  13508. }
  13509. } = this;
  13510. currentRect?.splice(0, 4, ...rect);
  13511. style.left = `${100 * (rect[0] - pageX) / pageWidth}%`;
  13512. style.top = `${100 * (pageHeight - rect[3] + pageY) / pageHeight}%`;
  13513. if (rotation === 0) {
  13514. style.width = `${100 * (rect[2] - rect[0]) / pageWidth}%`;
  13515. style.height = `${100 * (rect[3] - rect[1]) / pageHeight}%`;
  13516. } else {
  13517. this.setRotation(rotation);
  13518. }
  13519. }
  13520. _createContainer(ignoreBorder) {
  13521. const {
  13522. data,
  13523. parent: {
  13524. page,
  13525. viewport
  13526. }
  13527. } = this;
  13528. const container = document.createElement("section");
  13529. container.setAttribute("data-annotation-id", data.id);
  13530. if (!(this instanceof WidgetAnnotationElement)) {
  13531. container.tabIndex = DEFAULT_TAB_INDEX;
  13532. }
  13533. const {
  13534. style
  13535. } = container;
  13536. style.zIndex = this.parent.zIndex++;
  13537. if (data.alternativeText) {
  13538. container.title = data.alternativeText;
  13539. }
  13540. if (data.noRotate) {
  13541. container.classList.add("norotate");
  13542. }
  13543. if (!data.rect || this instanceof PopupAnnotationElement) {
  13544. const {
  13545. rotation
  13546. } = data;
  13547. if (!data.hasOwnCanvas && rotation !== 0) {
  13548. this.setRotation(rotation, container);
  13549. }
  13550. return container;
  13551. }
  13552. const {
  13553. width,
  13554. height
  13555. } = this;
  13556. if (!ignoreBorder && data.borderStyle.width > 0) {
  13557. style.borderWidth = `${data.borderStyle.width}px`;
  13558. const horizontalRadius = data.borderStyle.horizontalCornerRadius;
  13559. const verticalRadius = data.borderStyle.verticalCornerRadius;
  13560. if (horizontalRadius > 0 || verticalRadius > 0) {
  13561. const radius = `calc(${horizontalRadius}px * var(--total-scale-factor)) / calc(${verticalRadius}px * var(--total-scale-factor))`;
  13562. style.borderRadius = radius;
  13563. } else if (this instanceof RadioButtonWidgetAnnotationElement) {
  13564. const radius = `calc(${width}px * var(--total-scale-factor)) / calc(${height}px * var(--total-scale-factor))`;
  13565. style.borderRadius = radius;
  13566. }
  13567. switch (data.borderStyle.style) {
  13568. case AnnotationBorderStyleType.SOLID:
  13569. style.borderStyle = "solid";
  13570. break;
  13571. case AnnotationBorderStyleType.DASHED:
  13572. style.borderStyle = "dashed";
  13573. break;
  13574. case AnnotationBorderStyleType.BEVELED:
  13575. warn("Unimplemented border style: beveled");
  13576. break;
  13577. case AnnotationBorderStyleType.INSET:
  13578. warn("Unimplemented border style: inset");
  13579. break;
  13580. case AnnotationBorderStyleType.UNDERLINE:
  13581. style.borderBottomStyle = "solid";
  13582. break;
  13583. default:
  13584. break;
  13585. }
  13586. const borderColor = data.borderColor || null;
  13587. if (borderColor) {
  13588. this.#hasBorder = true;
  13589. style.borderColor = Util.makeHexColor(borderColor[0] | 0, borderColor[1] | 0, borderColor[2] | 0);
  13590. } else {
  13591. style.borderWidth = 0;
  13592. }
  13593. }
  13594. 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]]);
  13595. const {
  13596. pageWidth,
  13597. pageHeight,
  13598. pageX,
  13599. pageY
  13600. } = viewport.rawDims;
  13601. style.left = `${100 * (rect[0] - pageX) / pageWidth}%`;
  13602. style.top = `${100 * (rect[1] - pageY) / pageHeight}%`;
  13603. const {
  13604. rotation
  13605. } = data;
  13606. if (data.hasOwnCanvas || rotation === 0) {
  13607. style.width = `${100 * width / pageWidth}%`;
  13608. style.height = `${100 * height / pageHeight}%`;
  13609. } else {
  13610. this.setRotation(rotation, container);
  13611. }
  13612. return container;
  13613. }
  13614. setRotation(angle, container = this.container) {
  13615. if (!this.data.rect) {
  13616. return;
  13617. }
  13618. const {
  13619. pageWidth,
  13620. pageHeight
  13621. } = this.parent.viewport.rawDims;
  13622. let {
  13623. width,
  13624. height
  13625. } = this;
  13626. if (angle % 180 !== 0) {
  13627. [width, height] = [height, width];
  13628. }
  13629. container.style.width = `${100 * width / pageWidth}%`;
  13630. container.style.height = `${100 * height / pageHeight}%`;
  13631. container.setAttribute("data-main-rotation", (360 - angle) % 360);
  13632. }
  13633. get _commonActions() {
  13634. const setColor = (jsName, styleName, event) => {
  13635. const color = event.detail[jsName];
  13636. const colorType = color[0];
  13637. const colorArray = color.slice(1);
  13638. event.target.style[styleName] = ColorConverters[`${colorType}_HTML`](colorArray);
  13639. this.annotationStorage.setValue(this.data.id, {
  13640. [styleName]: ColorConverters[`${colorType}_rgb`](colorArray)
  13641. });
  13642. };
  13643. return shadow(this, "_commonActions", {
  13644. display: event => {
  13645. const {
  13646. display
  13647. } = event.detail;
  13648. const hidden = display % 2 === 1;
  13649. this.container.style.visibility = hidden ? "hidden" : "visible";
  13650. this.annotationStorage.setValue(this.data.id, {
  13651. noView: hidden,
  13652. noPrint: display === 1 || display === 2
  13653. });
  13654. },
  13655. print: event => {
  13656. this.annotationStorage.setValue(this.data.id, {
  13657. noPrint: !event.detail.print
  13658. });
  13659. },
  13660. hidden: event => {
  13661. const {
  13662. hidden
  13663. } = event.detail;
  13664. this.container.style.visibility = hidden ? "hidden" : "visible";
  13665. this.annotationStorage.setValue(this.data.id, {
  13666. noPrint: hidden,
  13667. noView: hidden
  13668. });
  13669. },
  13670. focus: event => {
  13671. setTimeout(() => event.target.focus({
  13672. preventScroll: false
  13673. }), 0);
  13674. },
  13675. userName: event => {
  13676. event.target.title = event.detail.userName;
  13677. },
  13678. readonly: event => {
  13679. event.target.disabled = event.detail.readonly;
  13680. },
  13681. required: event => {
  13682. this._setRequired(event.target, event.detail.required);
  13683. },
  13684. bgColor: event => {
  13685. setColor("bgColor", "backgroundColor", event);
  13686. },
  13687. fillColor: event => {
  13688. setColor("fillColor", "backgroundColor", event);
  13689. },
  13690. fgColor: event => {
  13691. setColor("fgColor", "color", event);
  13692. },
  13693. textColor: event => {
  13694. setColor("textColor", "color", event);
  13695. },
  13696. borderColor: event => {
  13697. setColor("borderColor", "borderColor", event);
  13698. },
  13699. strokeColor: event => {
  13700. setColor("strokeColor", "borderColor", event);
  13701. },
  13702. rotation: event => {
  13703. const angle = event.detail.rotation;
  13704. this.setRotation(angle);
  13705. this.annotationStorage.setValue(this.data.id, {
  13706. rotation: angle
  13707. });
  13708. }
  13709. });
  13710. }
  13711. _dispatchEventFromSandbox(actions, jsEvent) {
  13712. const commonActions = this._commonActions;
  13713. for (const name of Object.keys(jsEvent.detail)) {
  13714. const action = actions[name] || commonActions[name];
  13715. action?.(jsEvent);
  13716. }
  13717. }
  13718. _setDefaultPropertiesFromJS(element) {
  13719. if (!this.enableScripting) {
  13720. return;
  13721. }
  13722. const storedData = this.annotationStorage.getRawValue(this.data.id);
  13723. if (!storedData) {
  13724. return;
  13725. }
  13726. const commonActions = this._commonActions;
  13727. for (const [actionName, detail] of Object.entries(storedData)) {
  13728. const action = commonActions[actionName];
  13729. if (action) {
  13730. const eventProxy = {
  13731. detail: {
  13732. [actionName]: detail
  13733. },
  13734. target: element
  13735. };
  13736. action(eventProxy);
  13737. delete storedData[actionName];
  13738. }
  13739. }
  13740. }
  13741. _createQuadrilaterals() {
  13742. if (!this.container) {
  13743. return;
  13744. }
  13745. const {
  13746. quadPoints
  13747. } = this.data;
  13748. if (!quadPoints) {
  13749. return;
  13750. }
  13751. const [rectBlX, rectBlY, rectTrX, rectTrY] = this.data.rect.map(x => Math.fround(x));
  13752. if (quadPoints.length === 8) {
  13753. const [trX, trY, blX, blY] = quadPoints.subarray(2, 6);
  13754. if (rectTrX === trX && rectTrY === trY && rectBlX === blX && rectBlY === blY) {
  13755. return;
  13756. }
  13757. }
  13758. const {
  13759. style
  13760. } = this.container;
  13761. let svgBuffer;
  13762. if (this.#hasBorder) {
  13763. const {
  13764. borderColor,
  13765. borderWidth
  13766. } = style;
  13767. style.borderWidth = 0;
  13768. 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}">`];
  13769. this.container.classList.add("hasBorder");
  13770. }
  13771. const width = rectTrX - rectBlX;
  13772. const height = rectTrY - rectBlY;
  13773. const {
  13774. svgFactory
  13775. } = this;
  13776. const svg = svgFactory.createElement("svg");
  13777. svg.classList.add("quadrilateralsContainer");
  13778. svg.setAttribute("width", 0);
  13779. svg.setAttribute("height", 0);
  13780. const defs = svgFactory.createElement("defs");
  13781. svg.append(defs);
  13782. const clipPath = svgFactory.createElement("clipPath");
  13783. const id = `clippath_${this.data.id}`;
  13784. clipPath.setAttribute("id", id);
  13785. clipPath.setAttribute("clipPathUnits", "objectBoundingBox");
  13786. defs.append(clipPath);
  13787. for (let i = 2, ii = quadPoints.length; i < ii; i += 8) {
  13788. const trX = quadPoints[i];
  13789. const trY = quadPoints[i + 1];
  13790. const blX = quadPoints[i + 2];
  13791. const blY = quadPoints[i + 3];
  13792. const rect = svgFactory.createElement("rect");
  13793. const x = (blX - rectBlX) / width;
  13794. const y = (rectTrY - trY) / height;
  13795. const rectWidth = (trX - blX) / width;
  13796. const rectHeight = (trY - blY) / height;
  13797. rect.setAttribute("x", x);
  13798. rect.setAttribute("y", y);
  13799. rect.setAttribute("width", rectWidth);
  13800. rect.setAttribute("height", rectHeight);
  13801. clipPath.append(rect);
  13802. svgBuffer?.push(`<rect vector-effect="non-scaling-stroke" x="${x}" y="${y}" width="${rectWidth}" height="${rectHeight}"/>`);
  13803. }
  13804. if (this.#hasBorder) {
  13805. svgBuffer.push(`</g></svg>')`);
  13806. style.backgroundImage = svgBuffer.join("");
  13807. }
  13808. this.container.append(svg);
  13809. this.container.style.clipPath = `url(#${id})`;
  13810. }
  13811. _createPopup() {
  13812. const {
  13813. data
  13814. } = this;
  13815. const popup = this.#popupElement = new PopupAnnotationElement({
  13816. data: {
  13817. color: data.color,
  13818. titleObj: data.titleObj,
  13819. modificationDate: data.modificationDate,
  13820. contentsObj: data.contentsObj,
  13821. richText: data.richText,
  13822. parentRect: data.rect,
  13823. borderStyle: 0,
  13824. id: `popup_${data.id}`,
  13825. rotation: data.rotation
  13826. },
  13827. parent: this.parent,
  13828. elements: [this]
  13829. });
  13830. this.parent.div.append(popup.render());
  13831. }
  13832. render() {
  13833. unreachable("Abstract method `AnnotationElement.render` called");
  13834. }
  13835. _getElementsByName(name, skipId = null) {
  13836. const fields = [];
  13837. if (this._fieldObjects) {
  13838. const fieldObj = this._fieldObjects[name];
  13839. if (fieldObj) {
  13840. for (const {
  13841. page,
  13842. id,
  13843. exportValues
  13844. } of fieldObj) {
  13845. if (page === -1) {
  13846. continue;
  13847. }
  13848. if (id === skipId) {
  13849. continue;
  13850. }
  13851. const exportValue = typeof exportValues === "string" ? exportValues : null;
  13852. const domElement = document.querySelector(`[data-element-id="${id}"]`);
  13853. if (domElement && !GetElementsByNameSet.has(domElement)) {
  13854. warn(`_getElementsByName - element not allowed: ${id}`);
  13855. continue;
  13856. }
  13857. fields.push({
  13858. id,
  13859. exportValue,
  13860. domElement
  13861. });
  13862. }
  13863. }
  13864. return fields;
  13865. }
  13866. for (const domElement of document.getElementsByName(name)) {
  13867. const {
  13868. exportValue
  13869. } = domElement;
  13870. const id = domElement.getAttribute("data-element-id");
  13871. if (id === skipId) {
  13872. continue;
  13873. }
  13874. if (!GetElementsByNameSet.has(domElement)) {
  13875. continue;
  13876. }
  13877. fields.push({
  13878. id,
  13879. exportValue,
  13880. domElement
  13881. });
  13882. }
  13883. return fields;
  13884. }
  13885. show() {
  13886. if (this.container) {
  13887. this.container.hidden = false;
  13888. }
  13889. this.popup?.maybeShow();
  13890. }
  13891. hide() {
  13892. if (this.container) {
  13893. this.container.hidden = true;
  13894. }
  13895. this.popup?.forceHide();
  13896. }
  13897. getElementsToTriggerPopup() {
  13898. return this.container;
  13899. }
  13900. addHighlightArea() {
  13901. const triggers = this.getElementsToTriggerPopup();
  13902. if (Array.isArray(triggers)) {
  13903. for (const element of triggers) {
  13904. element.classList.add("highlightArea");
  13905. }
  13906. } else {
  13907. triggers.classList.add("highlightArea");
  13908. }
  13909. }
  13910. _editOnDoubleClick() {
  13911. if (!this._isEditable) {
  13912. return;
  13913. }
  13914. const {
  13915. annotationEditorType: mode,
  13916. data: {
  13917. id: editId
  13918. }
  13919. } = this;
  13920. this.container.addEventListener("dblclick", () => {
  13921. this.linkService.eventBus?.dispatch("switchannotationeditormode", {
  13922. source: this,
  13923. mode,
  13924. editId
  13925. });
  13926. });
  13927. }
  13928. get width() {
  13929. return this.data.rect[2] - this.data.rect[0];
  13930. }
  13931. get height() {
  13932. return this.data.rect[3] - this.data.rect[1];
  13933. }
  13934. }
  13935. class LinkAnnotationElement extends AnnotationElement {
  13936. constructor(parameters, options = null) {
  13937. super(parameters, {
  13938. isRenderable: true,
  13939. ignoreBorder: !!options?.ignoreBorder,
  13940. createQuadrilaterals: true
  13941. });
  13942. this.isTooltipOnly = parameters.data.isTooltipOnly;
  13943. }
  13944. render() {
  13945. const {
  13946. data,
  13947. linkService
  13948. } = this;
  13949. const link = document.createElement("a");
  13950. link.setAttribute("data-element-id", data.id);
  13951. let isBound = false;
  13952. if (data.url) {
  13953. linkService.addLinkAttributes(link, data.url, data.newWindow);
  13954. isBound = true;
  13955. } else if (data.action) {
  13956. this._bindNamedAction(link, data.action);
  13957. isBound = true;
  13958. } else if (data.attachment) {
  13959. this.#bindAttachment(link, data.attachment, data.attachmentDest);
  13960. isBound = true;
  13961. } else if (data.setOCGState) {
  13962. this.#bindSetOCGState(link, data.setOCGState);
  13963. isBound = true;
  13964. } else if (data.dest) {
  13965. this._bindLink(link, data.dest);
  13966. isBound = true;
  13967. } else {
  13968. if (data.actions && (data.actions.Action || data.actions["Mouse Up"] || data.actions["Mouse Down"]) && this.enableScripting && this.hasJSActions) {
  13969. this._bindJSAction(link, data);
  13970. isBound = true;
  13971. }
  13972. if (data.resetForm) {
  13973. this._bindResetFormAction(link, data.resetForm);
  13974. isBound = true;
  13975. } else if (this.isTooltipOnly && !isBound) {
  13976. this._bindLink(link, "");
  13977. isBound = true;
  13978. }
  13979. }
  13980. this.container.classList.add("linkAnnotation");
  13981. if (isBound) {
  13982. this.container.append(link);
  13983. }
  13984. return this.container;
  13985. }
  13986. #setInternalLink() {
  13987. this.container.setAttribute("data-internal-link", "");
  13988. }
  13989. _bindLink(link, destination) {
  13990. link.href = this.linkService.getDestinationHash(destination);
  13991. link.onclick = () => {
  13992. if (destination) {
  13993. this.linkService.goToDestination(destination);
  13994. }
  13995. return false;
  13996. };
  13997. if (destination || destination === "") {
  13998. this.#setInternalLink();
  13999. }
  14000. }
  14001. _bindNamedAction(link, action) {
  14002. link.href = this.linkService.getAnchorUrl("");
  14003. link.onclick = () => {
  14004. this.linkService.executeNamedAction(action);
  14005. return false;
  14006. };
  14007. this.#setInternalLink();
  14008. }
  14009. #bindAttachment(link, attachment, dest = null) {
  14010. link.href = this.linkService.getAnchorUrl("");
  14011. if (attachment.description) {
  14012. link.title = attachment.description;
  14013. }
  14014. link.onclick = () => {
  14015. this.downloadManager?.openOrDownloadData(attachment.content, attachment.filename, dest);
  14016. return false;
  14017. };
  14018. this.#setInternalLink();
  14019. }
  14020. #bindSetOCGState(link, action) {
  14021. link.href = this.linkService.getAnchorUrl("");
  14022. link.onclick = () => {
  14023. this.linkService.executeSetOCGState(action);
  14024. return false;
  14025. };
  14026. this.#setInternalLink();
  14027. }
  14028. _bindJSAction(link, data) {
  14029. link.href = this.linkService.getAnchorUrl("");
  14030. const map = new Map([["Action", "onclick"], ["Mouse Up", "onmouseup"], ["Mouse Down", "onmousedown"]]);
  14031. for (const name of Object.keys(data.actions)) {
  14032. const jsName = map.get(name);
  14033. if (!jsName) {
  14034. continue;
  14035. }
  14036. link[jsName] = () => {
  14037. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14038. source: this,
  14039. detail: {
  14040. id: data.id,
  14041. name
  14042. }
  14043. });
  14044. return false;
  14045. };
  14046. }
  14047. if (!link.onclick) {
  14048. link.onclick = () => false;
  14049. }
  14050. this.#setInternalLink();
  14051. }
  14052. _bindResetFormAction(link, resetForm) {
  14053. const otherClickAction = link.onclick;
  14054. if (!otherClickAction) {
  14055. link.href = this.linkService.getAnchorUrl("");
  14056. }
  14057. this.#setInternalLink();
  14058. if (!this._fieldObjects) {
  14059. warn(`_bindResetFormAction - "resetForm" action not supported, ` + "ensure that the `fieldObjects` parameter is provided.");
  14060. if (!otherClickAction) {
  14061. link.onclick = () => false;
  14062. }
  14063. return;
  14064. }
  14065. link.onclick = () => {
  14066. otherClickAction?.();
  14067. const {
  14068. fields: resetFormFields,
  14069. refs: resetFormRefs,
  14070. include
  14071. } = resetForm;
  14072. const allFields = [];
  14073. if (resetFormFields.length !== 0 || resetFormRefs.length !== 0) {
  14074. const fieldIds = new Set(resetFormRefs);
  14075. for (const fieldName of resetFormFields) {
  14076. const fields = this._fieldObjects[fieldName] || [];
  14077. for (const {
  14078. id
  14079. } of fields) {
  14080. fieldIds.add(id);
  14081. }
  14082. }
  14083. for (const fields of Object.values(this._fieldObjects)) {
  14084. for (const field of fields) {
  14085. if (fieldIds.has(field.id) === include) {
  14086. allFields.push(field);
  14087. }
  14088. }
  14089. }
  14090. } else {
  14091. for (const fields of Object.values(this._fieldObjects)) {
  14092. allFields.push(...fields);
  14093. }
  14094. }
  14095. const storage = this.annotationStorage;
  14096. const allIds = [];
  14097. for (const field of allFields) {
  14098. const {
  14099. id
  14100. } = field;
  14101. allIds.push(id);
  14102. switch (field.type) {
  14103. case "text":
  14104. {
  14105. const value = field.defaultValue || "";
  14106. storage.setValue(id, {
  14107. value
  14108. });
  14109. break;
  14110. }
  14111. case "checkbox":
  14112. case "radiobutton":
  14113. {
  14114. const value = field.defaultValue === field.exportValues;
  14115. storage.setValue(id, {
  14116. value
  14117. });
  14118. break;
  14119. }
  14120. case "combobox":
  14121. case "listbox":
  14122. {
  14123. const value = field.defaultValue || "";
  14124. storage.setValue(id, {
  14125. value
  14126. });
  14127. break;
  14128. }
  14129. default:
  14130. continue;
  14131. }
  14132. const domElement = document.querySelector(`[data-element-id="${id}"]`);
  14133. if (!domElement) {
  14134. continue;
  14135. } else if (!GetElementsByNameSet.has(domElement)) {
  14136. warn(`_bindResetFormAction - element not allowed: ${id}`);
  14137. continue;
  14138. }
  14139. domElement.dispatchEvent(new Event("resetform"));
  14140. }
  14141. if (this.enableScripting) {
  14142. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14143. source: this,
  14144. detail: {
  14145. id: "app",
  14146. ids: allIds,
  14147. name: "ResetForm"
  14148. }
  14149. });
  14150. }
  14151. return false;
  14152. };
  14153. }
  14154. }
  14155. class TextAnnotationElement extends AnnotationElement {
  14156. constructor(parameters) {
  14157. super(parameters, {
  14158. isRenderable: true
  14159. });
  14160. }
  14161. render() {
  14162. this.container.classList.add("textAnnotation");
  14163. const image = document.createElement("img");
  14164. image.src = this.imageResourcesPath + "annotation-" + this.data.name.toLowerCase() + ".svg";
  14165. image.setAttribute("data-l10n-id", "pdfjs-text-annotation-type");
  14166. image.setAttribute("data-l10n-args", JSON.stringify({
  14167. type: this.data.name
  14168. }));
  14169. if (!this.data.popupRef && this.hasPopupData) {
  14170. this._createPopup();
  14171. }
  14172. this.container.append(image);
  14173. return this.container;
  14174. }
  14175. }
  14176. class WidgetAnnotationElement extends AnnotationElement {
  14177. render() {
  14178. return this.container;
  14179. }
  14180. showElementAndHideCanvas(element) {
  14181. if (this.data.hasOwnCanvas) {
  14182. if (element.previousSibling?.nodeName === "CANVAS") {
  14183. element.previousSibling.hidden = true;
  14184. }
  14185. element.hidden = false;
  14186. }
  14187. }
  14188. _getKeyModifier(event) {
  14189. return util_FeatureTest.platform.isMac ? event.metaKey : event.ctrlKey;
  14190. }
  14191. _setEventListener(element, elementData, baseName, eventName, valueGetter) {
  14192. if (baseName.includes("mouse")) {
  14193. element.addEventListener(baseName, event => {
  14194. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14195. source: this,
  14196. detail: {
  14197. id: this.data.id,
  14198. name: eventName,
  14199. value: valueGetter(event),
  14200. shift: event.shiftKey,
  14201. modifier: this._getKeyModifier(event)
  14202. }
  14203. });
  14204. });
  14205. } else {
  14206. element.addEventListener(baseName, event => {
  14207. if (baseName === "blur") {
  14208. if (!elementData.focused || !event.relatedTarget) {
  14209. return;
  14210. }
  14211. elementData.focused = false;
  14212. } else if (baseName === "focus") {
  14213. if (elementData.focused) {
  14214. return;
  14215. }
  14216. elementData.focused = true;
  14217. }
  14218. if (!valueGetter) {
  14219. return;
  14220. }
  14221. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14222. source: this,
  14223. detail: {
  14224. id: this.data.id,
  14225. name: eventName,
  14226. value: valueGetter(event)
  14227. }
  14228. });
  14229. });
  14230. }
  14231. }
  14232. _setEventListeners(element, elementData, names, getter) {
  14233. for (const [baseName, eventName] of names) {
  14234. if (eventName === "Action" || this.data.actions?.[eventName]) {
  14235. if (eventName === "Focus" || eventName === "Blur") {
  14236. elementData ||= {
  14237. focused: false
  14238. };
  14239. }
  14240. this._setEventListener(element, elementData, baseName, eventName, getter);
  14241. if (eventName === "Focus" && !this.data.actions?.Blur) {
  14242. this._setEventListener(element, elementData, "blur", "Blur", null);
  14243. } else if (eventName === "Blur" && !this.data.actions?.Focus) {
  14244. this._setEventListener(element, elementData, "focus", "Focus", null);
  14245. }
  14246. }
  14247. }
  14248. }
  14249. _setBackgroundColor(element) {
  14250. const color = this.data.backgroundColor || null;
  14251. element.style.backgroundColor = color === null ? "transparent" : Util.makeHexColor(color[0], color[1], color[2]);
  14252. }
  14253. _setTextStyle(element) {
  14254. const TEXT_ALIGNMENT = ["left", "center", "right"];
  14255. const {
  14256. fontColor
  14257. } = this.data.defaultAppearanceData;
  14258. const fontSize = this.data.defaultAppearanceData.fontSize || annotation_layer_DEFAULT_FONT_SIZE;
  14259. const style = element.style;
  14260. let computedFontSize;
  14261. const BORDER_SIZE = 2;
  14262. const roundToOneDecimal = x => Math.round(10 * x) / 10;
  14263. if (this.data.multiLine) {
  14264. const height = Math.abs(this.data.rect[3] - this.data.rect[1] - BORDER_SIZE);
  14265. const numberOfLines = Math.round(height / (LINE_FACTOR * fontSize)) || 1;
  14266. const lineHeight = height / numberOfLines;
  14267. computedFontSize = Math.min(fontSize, roundToOneDecimal(lineHeight / LINE_FACTOR));
  14268. } else {
  14269. const height = Math.abs(this.data.rect[3] - this.data.rect[1] - BORDER_SIZE);
  14270. computedFontSize = Math.min(fontSize, roundToOneDecimal(height / LINE_FACTOR));
  14271. }
  14272. style.fontSize = `calc(${computedFontSize}px * var(--total-scale-factor))`;
  14273. style.color = Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
  14274. if (this.data.textAlignment !== null) {
  14275. style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
  14276. }
  14277. }
  14278. _setRequired(element, isRequired) {
  14279. if (isRequired) {
  14280. element.setAttribute("required", true);
  14281. } else {
  14282. element.removeAttribute("required");
  14283. }
  14284. element.setAttribute("aria-required", isRequired);
  14285. }
  14286. }
  14287. class TextWidgetAnnotationElement extends WidgetAnnotationElement {
  14288. constructor(parameters) {
  14289. const isRenderable = parameters.renderForms || parameters.data.hasOwnCanvas || !parameters.data.hasAppearance && !!parameters.data.fieldValue;
  14290. super(parameters, {
  14291. isRenderable
  14292. });
  14293. }
  14294. setPropertyOnSiblings(base, key, value, keyInStorage) {
  14295. const storage = this.annotationStorage;
  14296. for (const element of this._getElementsByName(base.name, base.id)) {
  14297. if (element.domElement) {
  14298. element.domElement[key] = value;
  14299. }
  14300. storage.setValue(element.id, {
  14301. [keyInStorage]: value
  14302. });
  14303. }
  14304. }
  14305. render() {
  14306. const storage = this.annotationStorage;
  14307. const id = this.data.id;
  14308. this.container.classList.add("textWidgetAnnotation");
  14309. let element = null;
  14310. if (this.renderForms) {
  14311. const storedData = storage.getValue(id, {
  14312. value: this.data.fieldValue
  14313. });
  14314. let textContent = storedData.value || "";
  14315. const maxLen = storage.getValue(id, {
  14316. charLimit: this.data.maxLen
  14317. }).charLimit;
  14318. if (maxLen && textContent.length > maxLen) {
  14319. textContent = textContent.slice(0, maxLen);
  14320. }
  14321. let fieldFormattedValues = storedData.formattedValue || this.data.textContent?.join("\n") || null;
  14322. if (fieldFormattedValues && this.data.comb) {
  14323. fieldFormattedValues = fieldFormattedValues.replaceAll(/\s+/g, "");
  14324. }
  14325. const elementData = {
  14326. userValue: textContent,
  14327. formattedValue: fieldFormattedValues,
  14328. lastCommittedValue: null,
  14329. commitKey: 1,
  14330. focused: false
  14331. };
  14332. if (this.data.multiLine) {
  14333. element = document.createElement("textarea");
  14334. element.textContent = fieldFormattedValues ?? textContent;
  14335. if (this.data.doNotScroll) {
  14336. element.style.overflowY = "hidden";
  14337. }
  14338. } else {
  14339. element = document.createElement("input");
  14340. element.type = this.data.password ? "password" : "text";
  14341. element.setAttribute("value", fieldFormattedValues ?? textContent);
  14342. if (this.data.doNotScroll) {
  14343. element.style.overflowX = "hidden";
  14344. }
  14345. }
  14346. if (this.data.hasOwnCanvas) {
  14347. element.hidden = true;
  14348. }
  14349. GetElementsByNameSet.add(element);
  14350. element.setAttribute("data-element-id", id);
  14351. element.disabled = this.data.readOnly;
  14352. element.name = this.data.fieldName;
  14353. element.tabIndex = DEFAULT_TAB_INDEX;
  14354. this._setRequired(element, this.data.required);
  14355. if (maxLen) {
  14356. element.maxLength = maxLen;
  14357. }
  14358. element.addEventListener("input", event => {
  14359. storage.setValue(id, {
  14360. value: event.target.value
  14361. });
  14362. this.setPropertyOnSiblings(element, "value", event.target.value, "value");
  14363. elementData.formattedValue = null;
  14364. });
  14365. element.addEventListener("resetform", event => {
  14366. const defaultValue = this.data.defaultFieldValue ?? "";
  14367. element.value = elementData.userValue = defaultValue;
  14368. elementData.formattedValue = null;
  14369. });
  14370. let blurListener = event => {
  14371. const {
  14372. formattedValue
  14373. } = elementData;
  14374. if (formattedValue !== null && formattedValue !== undefined) {
  14375. event.target.value = formattedValue;
  14376. }
  14377. event.target.scrollLeft = 0;
  14378. };
  14379. if (this.enableScripting && this.hasJSActions) {
  14380. element.addEventListener("focus", event => {
  14381. if (elementData.focused) {
  14382. return;
  14383. }
  14384. const {
  14385. target
  14386. } = event;
  14387. if (elementData.userValue) {
  14388. target.value = elementData.userValue;
  14389. }
  14390. elementData.lastCommittedValue = target.value;
  14391. elementData.commitKey = 1;
  14392. if (!this.data.actions?.Focus) {
  14393. elementData.focused = true;
  14394. }
  14395. });
  14396. element.addEventListener("updatefromsandbox", jsEvent => {
  14397. this.showElementAndHideCanvas(jsEvent.target);
  14398. const actions = {
  14399. value(event) {
  14400. elementData.userValue = event.detail.value ?? "";
  14401. storage.setValue(id, {
  14402. value: elementData.userValue.toString()
  14403. });
  14404. event.target.value = elementData.userValue;
  14405. },
  14406. formattedValue(event) {
  14407. const {
  14408. formattedValue
  14409. } = event.detail;
  14410. elementData.formattedValue = formattedValue;
  14411. if (formattedValue !== null && formattedValue !== undefined && event.target !== document.activeElement) {
  14412. event.target.value = formattedValue;
  14413. }
  14414. storage.setValue(id, {
  14415. formattedValue
  14416. });
  14417. },
  14418. selRange(event) {
  14419. event.target.setSelectionRange(...event.detail.selRange);
  14420. },
  14421. charLimit: event => {
  14422. const {
  14423. charLimit
  14424. } = event.detail;
  14425. const {
  14426. target
  14427. } = event;
  14428. if (charLimit === 0) {
  14429. target.removeAttribute("maxLength");
  14430. return;
  14431. }
  14432. target.setAttribute("maxLength", charLimit);
  14433. let value = elementData.userValue;
  14434. if (!value || value.length <= charLimit) {
  14435. return;
  14436. }
  14437. value = value.slice(0, charLimit);
  14438. target.value = elementData.userValue = value;
  14439. storage.setValue(id, {
  14440. value
  14441. });
  14442. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14443. source: this,
  14444. detail: {
  14445. id,
  14446. name: "Keystroke",
  14447. value,
  14448. willCommit: true,
  14449. commitKey: 1,
  14450. selStart: target.selectionStart,
  14451. selEnd: target.selectionEnd
  14452. }
  14453. });
  14454. }
  14455. };
  14456. this._dispatchEventFromSandbox(actions, jsEvent);
  14457. });
  14458. element.addEventListener("keydown", event => {
  14459. elementData.commitKey = 1;
  14460. let commitKey = -1;
  14461. if (event.key === "Escape") {
  14462. commitKey = 0;
  14463. } else if (event.key === "Enter" && !this.data.multiLine) {
  14464. commitKey = 2;
  14465. } else if (event.key === "Tab") {
  14466. elementData.commitKey = 3;
  14467. }
  14468. if (commitKey === -1) {
  14469. return;
  14470. }
  14471. const {
  14472. value
  14473. } = event.target;
  14474. if (elementData.lastCommittedValue === value) {
  14475. return;
  14476. }
  14477. elementData.lastCommittedValue = value;
  14478. elementData.userValue = value;
  14479. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14480. source: this,
  14481. detail: {
  14482. id,
  14483. name: "Keystroke",
  14484. value,
  14485. willCommit: true,
  14486. commitKey,
  14487. selStart: event.target.selectionStart,
  14488. selEnd: event.target.selectionEnd
  14489. }
  14490. });
  14491. });
  14492. const _blurListener = blurListener;
  14493. blurListener = null;
  14494. element.addEventListener("blur", event => {
  14495. if (!elementData.focused || !event.relatedTarget) {
  14496. return;
  14497. }
  14498. if (!this.data.actions?.Blur) {
  14499. elementData.focused = false;
  14500. }
  14501. const {
  14502. value
  14503. } = event.target;
  14504. elementData.userValue = value;
  14505. if (elementData.lastCommittedValue !== value) {
  14506. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14507. source: this,
  14508. detail: {
  14509. id,
  14510. name: "Keystroke",
  14511. value,
  14512. willCommit: true,
  14513. commitKey: elementData.commitKey,
  14514. selStart: event.target.selectionStart,
  14515. selEnd: event.target.selectionEnd
  14516. }
  14517. });
  14518. }
  14519. _blurListener(event);
  14520. });
  14521. if (this.data.actions?.Keystroke) {
  14522. element.addEventListener("beforeinput", event => {
  14523. elementData.lastCommittedValue = null;
  14524. const {
  14525. data,
  14526. target
  14527. } = event;
  14528. const {
  14529. value,
  14530. selectionStart,
  14531. selectionEnd
  14532. } = target;
  14533. let selStart = selectionStart,
  14534. selEnd = selectionEnd;
  14535. switch (event.inputType) {
  14536. case "deleteWordBackward":
  14537. {
  14538. const match = value.substring(0, selectionStart).match(/\w*[^\w]*$/);
  14539. if (match) {
  14540. selStart -= match[0].length;
  14541. }
  14542. break;
  14543. }
  14544. case "deleteWordForward":
  14545. {
  14546. const match = value.substring(selectionStart).match(/^[^\w]*\w*/);
  14547. if (match) {
  14548. selEnd += match[0].length;
  14549. }
  14550. break;
  14551. }
  14552. case "deleteContentBackward":
  14553. if (selectionStart === selectionEnd) {
  14554. selStart -= 1;
  14555. }
  14556. break;
  14557. case "deleteContentForward":
  14558. if (selectionStart === selectionEnd) {
  14559. selEnd += 1;
  14560. }
  14561. break;
  14562. }
  14563. event.preventDefault();
  14564. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14565. source: this,
  14566. detail: {
  14567. id,
  14568. name: "Keystroke",
  14569. value,
  14570. change: data || "",
  14571. willCommit: false,
  14572. selStart,
  14573. selEnd
  14574. }
  14575. });
  14576. });
  14577. }
  14578. this._setEventListeners(element, elementData, [["focus", "Focus"], ["blur", "Blur"], ["mousedown", "Mouse Down"], ["mouseenter", "Mouse Enter"], ["mouseleave", "Mouse Exit"], ["mouseup", "Mouse Up"]], event => event.target.value);
  14579. }
  14580. if (blurListener) {
  14581. element.addEventListener("blur", blurListener);
  14582. }
  14583. if (this.data.comb) {
  14584. const fieldWidth = this.data.rect[2] - this.data.rect[0];
  14585. const combWidth = fieldWidth / maxLen;
  14586. element.classList.add("comb");
  14587. element.style.letterSpacing = `calc(${combWidth}px * var(--total-scale-factor) - 1ch)`;
  14588. }
  14589. } else {
  14590. element = document.createElement("div");
  14591. element.textContent = this.data.fieldValue;
  14592. element.style.verticalAlign = "middle";
  14593. element.style.display = "table-cell";
  14594. if (this.data.hasOwnCanvas) {
  14595. element.hidden = true;
  14596. }
  14597. }
  14598. this._setTextStyle(element);
  14599. this._setBackgroundColor(element);
  14600. this._setDefaultPropertiesFromJS(element);
  14601. this.container.append(element);
  14602. return this.container;
  14603. }
  14604. }
  14605. class SignatureWidgetAnnotationElement extends WidgetAnnotationElement {
  14606. constructor(parameters) {
  14607. super(parameters, {
  14608. isRenderable: !!parameters.data.hasOwnCanvas
  14609. });
  14610. }
  14611. }
  14612. class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
  14613. constructor(parameters) {
  14614. super(parameters, {
  14615. isRenderable: parameters.renderForms
  14616. });
  14617. }
  14618. render() {
  14619. const storage = this.annotationStorage;
  14620. const data = this.data;
  14621. const id = data.id;
  14622. let value = storage.getValue(id, {
  14623. value: data.exportValue === data.fieldValue
  14624. }).value;
  14625. if (typeof value === "string") {
  14626. value = value !== "Off";
  14627. storage.setValue(id, {
  14628. value
  14629. });
  14630. }
  14631. this.container.classList.add("buttonWidgetAnnotation", "checkBox");
  14632. const element = document.createElement("input");
  14633. GetElementsByNameSet.add(element);
  14634. element.setAttribute("data-element-id", id);
  14635. element.disabled = data.readOnly;
  14636. this._setRequired(element, this.data.required);
  14637. element.type = "checkbox";
  14638. element.name = data.fieldName;
  14639. if (value) {
  14640. element.setAttribute("checked", true);
  14641. }
  14642. element.setAttribute("exportValue", data.exportValue);
  14643. element.tabIndex = DEFAULT_TAB_INDEX;
  14644. element.addEventListener("change", event => {
  14645. const {
  14646. name,
  14647. checked
  14648. } = event.target;
  14649. for (const checkbox of this._getElementsByName(name, id)) {
  14650. const curChecked = checked && checkbox.exportValue === data.exportValue;
  14651. if (checkbox.domElement) {
  14652. checkbox.domElement.checked = curChecked;
  14653. }
  14654. storage.setValue(checkbox.id, {
  14655. value: curChecked
  14656. });
  14657. }
  14658. storage.setValue(id, {
  14659. value: checked
  14660. });
  14661. });
  14662. element.addEventListener("resetform", event => {
  14663. const defaultValue = data.defaultFieldValue || "Off";
  14664. event.target.checked = defaultValue === data.exportValue;
  14665. });
  14666. if (this.enableScripting && this.hasJSActions) {
  14667. element.addEventListener("updatefromsandbox", jsEvent => {
  14668. const actions = {
  14669. value(event) {
  14670. event.target.checked = event.detail.value !== "Off";
  14671. storage.setValue(id, {
  14672. value: event.target.checked
  14673. });
  14674. }
  14675. };
  14676. this._dispatchEventFromSandbox(actions, jsEvent);
  14677. });
  14678. 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);
  14679. }
  14680. this._setBackgroundColor(element);
  14681. this._setDefaultPropertiesFromJS(element);
  14682. this.container.append(element);
  14683. return this.container;
  14684. }
  14685. }
  14686. class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
  14687. constructor(parameters) {
  14688. super(parameters, {
  14689. isRenderable: parameters.renderForms
  14690. });
  14691. }
  14692. render() {
  14693. this.container.classList.add("buttonWidgetAnnotation", "radioButton");
  14694. const storage = this.annotationStorage;
  14695. const data = this.data;
  14696. const id = data.id;
  14697. let value = storage.getValue(id, {
  14698. value: data.fieldValue === data.buttonValue
  14699. }).value;
  14700. if (typeof value === "string") {
  14701. value = value !== data.buttonValue;
  14702. storage.setValue(id, {
  14703. value
  14704. });
  14705. }
  14706. if (value) {
  14707. for (const radio of this._getElementsByName(data.fieldName, id)) {
  14708. storage.setValue(radio.id, {
  14709. value: false
  14710. });
  14711. }
  14712. }
  14713. const element = document.createElement("input");
  14714. GetElementsByNameSet.add(element);
  14715. element.setAttribute("data-element-id", id);
  14716. element.disabled = data.readOnly;
  14717. this._setRequired(element, this.data.required);
  14718. element.type = "radio";
  14719. element.name = data.fieldName;
  14720. if (value) {
  14721. element.setAttribute("checked", true);
  14722. }
  14723. element.tabIndex = DEFAULT_TAB_INDEX;
  14724. element.addEventListener("change", event => {
  14725. const {
  14726. name,
  14727. checked
  14728. } = event.target;
  14729. for (const radio of this._getElementsByName(name, id)) {
  14730. storage.setValue(radio.id, {
  14731. value: false
  14732. });
  14733. }
  14734. storage.setValue(id, {
  14735. value: checked
  14736. });
  14737. });
  14738. element.addEventListener("resetform", event => {
  14739. const defaultValue = data.defaultFieldValue;
  14740. event.target.checked = defaultValue !== null && defaultValue !== undefined && defaultValue === data.buttonValue;
  14741. });
  14742. if (this.enableScripting && this.hasJSActions) {
  14743. const pdfButtonValue = data.buttonValue;
  14744. element.addEventListener("updatefromsandbox", jsEvent => {
  14745. const actions = {
  14746. value: event => {
  14747. const checked = pdfButtonValue === event.detail.value;
  14748. for (const radio of this._getElementsByName(event.target.name)) {
  14749. const curChecked = checked && radio.id === id;
  14750. if (radio.domElement) {
  14751. radio.domElement.checked = curChecked;
  14752. }
  14753. storage.setValue(radio.id, {
  14754. value: curChecked
  14755. });
  14756. }
  14757. }
  14758. };
  14759. this._dispatchEventFromSandbox(actions, jsEvent);
  14760. });
  14761. 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);
  14762. }
  14763. this._setBackgroundColor(element);
  14764. this._setDefaultPropertiesFromJS(element);
  14765. this.container.append(element);
  14766. return this.container;
  14767. }
  14768. }
  14769. class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
  14770. constructor(parameters) {
  14771. super(parameters, {
  14772. ignoreBorder: parameters.data.hasAppearance
  14773. });
  14774. }
  14775. render() {
  14776. const container = super.render();
  14777. container.classList.add("buttonWidgetAnnotation", "pushButton");
  14778. const linkElement = container.lastChild;
  14779. if (this.enableScripting && this.hasJSActions && linkElement) {
  14780. this._setDefaultPropertiesFromJS(linkElement);
  14781. linkElement.addEventListener("updatefromsandbox", jsEvent => {
  14782. this._dispatchEventFromSandbox({}, jsEvent);
  14783. });
  14784. }
  14785. return container;
  14786. }
  14787. }
  14788. class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
  14789. constructor(parameters) {
  14790. super(parameters, {
  14791. isRenderable: parameters.renderForms
  14792. });
  14793. }
  14794. render() {
  14795. this.container.classList.add("choiceWidgetAnnotation");
  14796. const storage = this.annotationStorage;
  14797. const id = this.data.id;
  14798. const storedData = storage.getValue(id, {
  14799. value: this.data.fieldValue
  14800. });
  14801. const selectElement = document.createElement("select");
  14802. GetElementsByNameSet.add(selectElement);
  14803. selectElement.setAttribute("data-element-id", id);
  14804. selectElement.disabled = this.data.readOnly;
  14805. this._setRequired(selectElement, this.data.required);
  14806. selectElement.name = this.data.fieldName;
  14807. selectElement.tabIndex = DEFAULT_TAB_INDEX;
  14808. let addAnEmptyEntry = this.data.combo && this.data.options.length > 0;
  14809. if (!this.data.combo) {
  14810. selectElement.size = this.data.options.length;
  14811. if (this.data.multiSelect) {
  14812. selectElement.multiple = true;
  14813. }
  14814. }
  14815. selectElement.addEventListener("resetform", event => {
  14816. const defaultValue = this.data.defaultFieldValue;
  14817. for (const option of selectElement.options) {
  14818. option.selected = option.value === defaultValue;
  14819. }
  14820. });
  14821. for (const option of this.data.options) {
  14822. const optionElement = document.createElement("option");
  14823. optionElement.textContent = option.displayValue;
  14824. optionElement.value = option.exportValue;
  14825. if (storedData.value.includes(option.exportValue)) {
  14826. optionElement.setAttribute("selected", true);
  14827. addAnEmptyEntry = false;
  14828. }
  14829. selectElement.append(optionElement);
  14830. }
  14831. let removeEmptyEntry = null;
  14832. if (addAnEmptyEntry) {
  14833. const noneOptionElement = document.createElement("option");
  14834. noneOptionElement.value = " ";
  14835. noneOptionElement.setAttribute("hidden", true);
  14836. noneOptionElement.setAttribute("selected", true);
  14837. selectElement.prepend(noneOptionElement);
  14838. removeEmptyEntry = () => {
  14839. noneOptionElement.remove();
  14840. selectElement.removeEventListener("input", removeEmptyEntry);
  14841. removeEmptyEntry = null;
  14842. };
  14843. selectElement.addEventListener("input", removeEmptyEntry);
  14844. }
  14845. const getValue = isExport => {
  14846. const name = isExport ? "value" : "textContent";
  14847. const {
  14848. options,
  14849. multiple
  14850. } = selectElement;
  14851. if (!multiple) {
  14852. return options.selectedIndex === -1 ? null : options[options.selectedIndex][name];
  14853. }
  14854. return Array.prototype.filter.call(options, option => option.selected).map(option => option[name]);
  14855. };
  14856. let selectedValues = getValue(false);
  14857. const getItems = event => {
  14858. const options = event.target.options;
  14859. return Array.prototype.map.call(options, option => ({
  14860. displayValue: option.textContent,
  14861. exportValue: option.value
  14862. }));
  14863. };
  14864. if (this.enableScripting && this.hasJSActions) {
  14865. selectElement.addEventListener("updatefromsandbox", jsEvent => {
  14866. const actions = {
  14867. value(event) {
  14868. removeEmptyEntry?.();
  14869. const value = event.detail.value;
  14870. const values = new Set(Array.isArray(value) ? value : [value]);
  14871. for (const option of selectElement.options) {
  14872. option.selected = values.has(option.value);
  14873. }
  14874. storage.setValue(id, {
  14875. value: getValue(true)
  14876. });
  14877. selectedValues = getValue(false);
  14878. },
  14879. multipleSelection(event) {
  14880. selectElement.multiple = true;
  14881. },
  14882. remove(event) {
  14883. const options = selectElement.options;
  14884. const index = event.detail.remove;
  14885. options[index].selected = false;
  14886. selectElement.remove(index);
  14887. if (options.length > 0) {
  14888. const i = Array.prototype.findIndex.call(options, option => option.selected);
  14889. if (i === -1) {
  14890. options[0].selected = true;
  14891. }
  14892. }
  14893. storage.setValue(id, {
  14894. value: getValue(true),
  14895. items: getItems(event)
  14896. });
  14897. selectedValues = getValue(false);
  14898. },
  14899. clear(event) {
  14900. while (selectElement.length !== 0) {
  14901. selectElement.remove(0);
  14902. }
  14903. storage.setValue(id, {
  14904. value: null,
  14905. items: []
  14906. });
  14907. selectedValues = getValue(false);
  14908. },
  14909. insert(event) {
  14910. const {
  14911. index,
  14912. displayValue,
  14913. exportValue
  14914. } = event.detail.insert;
  14915. const selectChild = selectElement.children[index];
  14916. const optionElement = document.createElement("option");
  14917. optionElement.textContent = displayValue;
  14918. optionElement.value = exportValue;
  14919. if (selectChild) {
  14920. selectChild.before(optionElement);
  14921. } else {
  14922. selectElement.append(optionElement);
  14923. }
  14924. storage.setValue(id, {
  14925. value: getValue(true),
  14926. items: getItems(event)
  14927. });
  14928. selectedValues = getValue(false);
  14929. },
  14930. items(event) {
  14931. const {
  14932. items
  14933. } = event.detail;
  14934. while (selectElement.length !== 0) {
  14935. selectElement.remove(0);
  14936. }
  14937. for (const item of items) {
  14938. const {
  14939. displayValue,
  14940. exportValue
  14941. } = item;
  14942. const optionElement = document.createElement("option");
  14943. optionElement.textContent = displayValue;
  14944. optionElement.value = exportValue;
  14945. selectElement.append(optionElement);
  14946. }
  14947. if (selectElement.options.length > 0) {
  14948. selectElement.options[0].selected = true;
  14949. }
  14950. storage.setValue(id, {
  14951. value: getValue(true),
  14952. items: getItems(event)
  14953. });
  14954. selectedValues = getValue(false);
  14955. },
  14956. indices(event) {
  14957. const indices = new Set(event.detail.indices);
  14958. for (const option of event.target.options) {
  14959. option.selected = indices.has(option.index);
  14960. }
  14961. storage.setValue(id, {
  14962. value: getValue(true)
  14963. });
  14964. selectedValues = getValue(false);
  14965. },
  14966. editable(event) {
  14967. event.target.disabled = !event.detail.editable;
  14968. }
  14969. };
  14970. this._dispatchEventFromSandbox(actions, jsEvent);
  14971. });
  14972. selectElement.addEventListener("input", event => {
  14973. const exportValue = getValue(true);
  14974. const change = getValue(false);
  14975. storage.setValue(id, {
  14976. value: exportValue
  14977. });
  14978. event.preventDefault();
  14979. this.linkService.eventBus?.dispatch("dispatcheventinsandbox", {
  14980. source: this,
  14981. detail: {
  14982. id,
  14983. name: "Keystroke",
  14984. value: selectedValues,
  14985. change,
  14986. changeEx: exportValue,
  14987. willCommit: false,
  14988. commitKey: 1,
  14989. keyDown: false
  14990. }
  14991. });
  14992. });
  14993. 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);
  14994. } else {
  14995. selectElement.addEventListener("input", function (event) {
  14996. storage.setValue(id, {
  14997. value: getValue(true)
  14998. });
  14999. });
  15000. }
  15001. if (this.data.combo) {
  15002. this._setTextStyle(selectElement);
  15003. } else {}
  15004. this._setBackgroundColor(selectElement);
  15005. this._setDefaultPropertiesFromJS(selectElement);
  15006. this.container.append(selectElement);
  15007. return this.container;
  15008. }
  15009. }
  15010. class PopupAnnotationElement extends AnnotationElement {
  15011. constructor(parameters) {
  15012. const {
  15013. data,
  15014. elements
  15015. } = parameters;
  15016. super(parameters, {
  15017. isRenderable: AnnotationElement._hasPopupData(data)
  15018. });
  15019. this.elements = elements;
  15020. this.popup = null;
  15021. }
  15022. render() {
  15023. this.container.classList.add("popupAnnotation");
  15024. const popup = this.popup = new PopupElement({
  15025. container: this.container,
  15026. color: this.data.color,
  15027. titleObj: this.data.titleObj,
  15028. modificationDate: this.data.modificationDate,
  15029. contentsObj: this.data.contentsObj,
  15030. richText: this.data.richText,
  15031. rect: this.data.rect,
  15032. parentRect: this.data.parentRect || null,
  15033. parent: this.parent,
  15034. elements: this.elements,
  15035. open: this.data.open
  15036. });
  15037. const elementIds = [];
  15038. for (const element of this.elements) {
  15039. element.popup = popup;
  15040. element.container.ariaHasPopup = "dialog";
  15041. elementIds.push(element.data.id);
  15042. element.addHighlightArea();
  15043. }
  15044. this.container.setAttribute("aria-controls", elementIds.map(id => `${AnnotationPrefix}${id}`).join(","));
  15045. return this.container;
  15046. }
  15047. }
  15048. class PopupElement {
  15049. #boundKeyDown = this.#keyDown.bind(this);
  15050. #boundHide = this.#hide.bind(this);
  15051. #boundShow = this.#show.bind(this);
  15052. #boundToggle = this.#toggle.bind(this);
  15053. #color = null;
  15054. #container = null;
  15055. #contentsObj = null;
  15056. #dateObj = null;
  15057. #elements = null;
  15058. #parent = null;
  15059. #parentRect = null;
  15060. #pinned = false;
  15061. #popup = null;
  15062. #position = null;
  15063. #rect = null;
  15064. #richText = null;
  15065. #titleObj = null;
  15066. #updates = null;
  15067. #wasVisible = false;
  15068. constructor({
  15069. container,
  15070. color,
  15071. elements,
  15072. titleObj,
  15073. modificationDate,
  15074. contentsObj,
  15075. richText,
  15076. parent,
  15077. rect,
  15078. parentRect,
  15079. open
  15080. }) {
  15081. this.#container = container;
  15082. this.#titleObj = titleObj;
  15083. this.#contentsObj = contentsObj;
  15084. this.#richText = richText;
  15085. this.#parent = parent;
  15086. this.#color = color;
  15087. this.#rect = rect;
  15088. this.#parentRect = parentRect;
  15089. this.#elements = elements;
  15090. this.#dateObj = PDFDateString.toDateObject(modificationDate);
  15091. this.trigger = elements.flatMap(e => e.getElementsToTriggerPopup());
  15092. for (const element of this.trigger) {
  15093. element.addEventListener("click", this.#boundToggle);
  15094. element.addEventListener("mouseenter", this.#boundShow);
  15095. element.addEventListener("mouseleave", this.#boundHide);
  15096. element.classList.add("popupTriggerArea");
  15097. }
  15098. for (const element of elements) {
  15099. element.container?.addEventListener("keydown", this.#boundKeyDown);
  15100. }
  15101. this.#container.hidden = true;
  15102. if (open) {
  15103. this.#toggle();
  15104. }
  15105. }
  15106. render() {
  15107. if (this.#popup) {
  15108. return;
  15109. }
  15110. const popup = this.#popup = document.createElement("div");
  15111. popup.className = "popup";
  15112. if (this.#color) {
  15113. const baseColor = popup.style.outlineColor = Util.makeHexColor(...this.#color);
  15114. popup.style.backgroundColor = `color-mix(in srgb, ${baseColor} 30%, white)`;
  15115. }
  15116. const header = document.createElement("span");
  15117. header.className = "header";
  15118. const title = document.createElement("h1");
  15119. header.append(title);
  15120. ({
  15121. dir: title.dir,
  15122. str: title.textContent
  15123. } = this.#titleObj);
  15124. popup.append(header);
  15125. if (this.#dateObj) {
  15126. const modificationDate = document.createElement("span");
  15127. modificationDate.classList.add("popupDate");
  15128. modificationDate.setAttribute("data-l10n-id", "pdfjs-annotation-date-time-string");
  15129. modificationDate.setAttribute("data-l10n-args", JSON.stringify({
  15130. dateObj: this.#dateObj.valueOf()
  15131. }));
  15132. header.append(modificationDate);
  15133. }
  15134. const html = this.#html;
  15135. if (html) {
  15136. XfaLayer.render({
  15137. xfaHtml: html,
  15138. intent: "richText",
  15139. div: popup
  15140. });
  15141. popup.lastChild.classList.add("richText", "popupContent");
  15142. } else {
  15143. const contents = this._formatContents(this.#contentsObj);
  15144. popup.append(contents);
  15145. }
  15146. this.#container.append(popup);
  15147. }
  15148. get #html() {
  15149. const richText = this.#richText;
  15150. const contentsObj = this.#contentsObj;
  15151. if (richText?.str && (!contentsObj?.str || contentsObj.str === richText.str)) {
  15152. return this.#richText.html || null;
  15153. }
  15154. return null;
  15155. }
  15156. get #fontSize() {
  15157. return this.#html?.attributes?.style?.fontSize || 0;
  15158. }
  15159. get #fontColor() {
  15160. return this.#html?.attributes?.style?.color || null;
  15161. }
  15162. #makePopupContent(text) {
  15163. const popupLines = [];
  15164. const popupContent = {
  15165. str: text,
  15166. html: {
  15167. name: "div",
  15168. attributes: {
  15169. dir: "auto"
  15170. },
  15171. children: [{
  15172. name: "p",
  15173. children: popupLines
  15174. }]
  15175. }
  15176. };
  15177. const lineAttributes = {
  15178. style: {
  15179. color: this.#fontColor,
  15180. fontSize: this.#fontSize ? `calc(${this.#fontSize}px * var(--total-scale-factor))` : ""
  15181. }
  15182. };
  15183. for (const line of text.split("\n")) {
  15184. popupLines.push({
  15185. name: "span",
  15186. value: line,
  15187. attributes: lineAttributes
  15188. });
  15189. }
  15190. return popupContent;
  15191. }
  15192. _formatContents({
  15193. str,
  15194. dir
  15195. }) {
  15196. const p = document.createElement("p");
  15197. p.classList.add("popupContent");
  15198. p.dir = dir;
  15199. const lines = str.split(/(?:\r\n?|\n)/);
  15200. for (let i = 0, ii = lines.length; i < ii; ++i) {
  15201. const line = lines[i];
  15202. p.append(document.createTextNode(line));
  15203. if (i < ii - 1) {
  15204. p.append(document.createElement("br"));
  15205. }
  15206. }
  15207. return p;
  15208. }
  15209. #keyDown(event) {
  15210. if (event.altKey || event.shiftKey || event.ctrlKey || event.metaKey) {
  15211. return;
  15212. }
  15213. if (event.key === "Enter" || event.key === "Escape" && this.#pinned) {
  15214. this.#toggle();
  15215. }
  15216. }
  15217. updateEdited({
  15218. rect,
  15219. popupContent
  15220. }) {
  15221. this.#updates ||= {
  15222. contentsObj: this.#contentsObj,
  15223. richText: this.#richText
  15224. };
  15225. if (rect) {
  15226. this.#position = null;
  15227. }
  15228. if (popupContent) {
  15229. this.#richText = this.#makePopupContent(popupContent);
  15230. this.#contentsObj = null;
  15231. }
  15232. this.#popup?.remove();
  15233. this.#popup = null;
  15234. }
  15235. resetEdited() {
  15236. if (!this.#updates) {
  15237. return;
  15238. }
  15239. ({
  15240. contentsObj: this.#contentsObj,
  15241. richText: this.#richText
  15242. } = this.#updates);
  15243. this.#updates = null;
  15244. this.#popup?.remove();
  15245. this.#popup = null;
  15246. this.#position = null;
  15247. }
  15248. #setPosition() {
  15249. if (this.#position !== null) {
  15250. return;
  15251. }
  15252. const {
  15253. page: {
  15254. view
  15255. },
  15256. viewport: {
  15257. rawDims: {
  15258. pageWidth,
  15259. pageHeight,
  15260. pageX,
  15261. pageY
  15262. }
  15263. }
  15264. } = this.#parent;
  15265. let useParentRect = !!this.#parentRect;
  15266. let rect = useParentRect ? this.#parentRect : this.#rect;
  15267. for (const element of this.#elements) {
  15268. if (!rect || Util.intersect(element.data.rect, rect) !== null) {
  15269. rect = element.data.rect;
  15270. useParentRect = true;
  15271. break;
  15272. }
  15273. }
  15274. const normalizedRect = Util.normalizeRect([rect[0], view[3] - rect[1] + view[1], rect[2], view[3] - rect[3] + view[1]]);
  15275. const HORIZONTAL_SPACE_AFTER_ANNOTATION = 5;
  15276. const parentWidth = useParentRect ? rect[2] - rect[0] + HORIZONTAL_SPACE_AFTER_ANNOTATION : 0;
  15277. const popupLeft = normalizedRect[0] + parentWidth;
  15278. const popupTop = normalizedRect[1];
  15279. this.#position = [100 * (popupLeft - pageX) / pageWidth, 100 * (popupTop - pageY) / pageHeight];
  15280. const {
  15281. style
  15282. } = this.#container;
  15283. style.left = `${this.#position[0]}%`;
  15284. style.top = `${this.#position[1]}%`;
  15285. }
  15286. #toggle() {
  15287. this.#pinned = !this.#pinned;
  15288. if (this.#pinned) {
  15289. this.#show();
  15290. this.#container.addEventListener("click", this.#boundToggle);
  15291. this.#container.addEventListener("keydown", this.#boundKeyDown);
  15292. } else {
  15293. this.#hide();
  15294. this.#container.removeEventListener("click", this.#boundToggle);
  15295. this.#container.removeEventListener("keydown", this.#boundKeyDown);
  15296. }
  15297. }
  15298. #show() {
  15299. if (!this.#popup) {
  15300. this.render();
  15301. }
  15302. if (!this.isVisible) {
  15303. this.#setPosition();
  15304. this.#container.hidden = false;
  15305. this.#container.style.zIndex = parseInt(this.#container.style.zIndex) + 1000;
  15306. } else if (this.#pinned) {
  15307. this.#container.classList.add("focused");
  15308. }
  15309. }
  15310. #hide() {
  15311. this.#container.classList.remove("focused");
  15312. if (this.#pinned || !this.isVisible) {
  15313. return;
  15314. }
  15315. this.#container.hidden = true;
  15316. this.#container.style.zIndex = parseInt(this.#container.style.zIndex) - 1000;
  15317. }
  15318. forceHide() {
  15319. this.#wasVisible = this.isVisible;
  15320. if (!this.#wasVisible) {
  15321. return;
  15322. }
  15323. this.#container.hidden = true;
  15324. }
  15325. maybeShow() {
  15326. if (!this.#wasVisible) {
  15327. return;
  15328. }
  15329. if (!this.#popup) {
  15330. this.#show();
  15331. }
  15332. this.#wasVisible = false;
  15333. this.#container.hidden = false;
  15334. }
  15335. get isVisible() {
  15336. return this.#container.hidden === false;
  15337. }
  15338. }
  15339. class FreeTextAnnotationElement extends AnnotationElement {
  15340. constructor(parameters) {
  15341. super(parameters, {
  15342. isRenderable: true,
  15343. ignoreBorder: true
  15344. });
  15345. this.textContent = parameters.data.textContent;
  15346. this.textPosition = parameters.data.textPosition;
  15347. this.annotationEditorType = AnnotationEditorType.FREETEXT;
  15348. }
  15349. render() {
  15350. this.container.classList.add("freeTextAnnotation");
  15351. if (this.textContent) {
  15352. const content = document.createElement("div");
  15353. content.classList.add("annotationTextContent");
  15354. content.setAttribute("role", "comment");
  15355. for (const line of this.textContent) {
  15356. const lineSpan = document.createElement("span");
  15357. lineSpan.textContent = line;
  15358. content.append(lineSpan);
  15359. }
  15360. this.container.append(content);
  15361. }
  15362. if (!this.data.popupRef && this.hasPopupData) {
  15363. this._createPopup();
  15364. }
  15365. this._editOnDoubleClick();
  15366. return this.container;
  15367. }
  15368. }
  15369. class LineAnnotationElement extends AnnotationElement {
  15370. #line = null;
  15371. constructor(parameters) {
  15372. super(parameters, {
  15373. isRenderable: true,
  15374. ignoreBorder: true
  15375. });
  15376. }
  15377. render() {
  15378. this.container.classList.add("lineAnnotation");
  15379. const {
  15380. data,
  15381. width,
  15382. height
  15383. } = this;
  15384. const svg = this.svgFactory.create(width, height, true);
  15385. const line = this.#line = this.svgFactory.createElement("svg:line");
  15386. line.setAttribute("x1", data.rect[2] - data.lineCoordinates[0]);
  15387. line.setAttribute("y1", data.rect[3] - data.lineCoordinates[1]);
  15388. line.setAttribute("x2", data.rect[2] - data.lineCoordinates[2]);
  15389. line.setAttribute("y2", data.rect[3] - data.lineCoordinates[3]);
  15390. line.setAttribute("stroke-width", data.borderStyle.width || 1);
  15391. line.setAttribute("stroke", "transparent");
  15392. line.setAttribute("fill", "transparent");
  15393. svg.append(line);
  15394. this.container.append(svg);
  15395. if (!data.popupRef && this.hasPopupData) {
  15396. this._createPopup();
  15397. }
  15398. return this.container;
  15399. }
  15400. getElementsToTriggerPopup() {
  15401. return this.#line;
  15402. }
  15403. addHighlightArea() {
  15404. this.container.classList.add("highlightArea");
  15405. }
  15406. }
  15407. class SquareAnnotationElement extends AnnotationElement {
  15408. #square = null;
  15409. constructor(parameters) {
  15410. super(parameters, {
  15411. isRenderable: true,
  15412. ignoreBorder: true
  15413. });
  15414. }
  15415. render() {
  15416. this.container.classList.add("squareAnnotation");
  15417. const {
  15418. data,
  15419. width,
  15420. height
  15421. } = this;
  15422. const svg = this.svgFactory.create(width, height, true);
  15423. const borderWidth = data.borderStyle.width;
  15424. const square = this.#square = this.svgFactory.createElement("svg:rect");
  15425. square.setAttribute("x", borderWidth / 2);
  15426. square.setAttribute("y", borderWidth / 2);
  15427. square.setAttribute("width", width - borderWidth);
  15428. square.setAttribute("height", height - borderWidth);
  15429. square.setAttribute("stroke-width", borderWidth || 1);
  15430. square.setAttribute("stroke", "transparent");
  15431. square.setAttribute("fill", "transparent");
  15432. svg.append(square);
  15433. this.container.append(svg);
  15434. if (!data.popupRef && this.hasPopupData) {
  15435. this._createPopup();
  15436. }
  15437. return this.container;
  15438. }
  15439. getElementsToTriggerPopup() {
  15440. return this.#square;
  15441. }
  15442. addHighlightArea() {
  15443. this.container.classList.add("highlightArea");
  15444. }
  15445. }
  15446. class CircleAnnotationElement extends AnnotationElement {
  15447. #circle = null;
  15448. constructor(parameters) {
  15449. super(parameters, {
  15450. isRenderable: true,
  15451. ignoreBorder: true
  15452. });
  15453. }
  15454. render() {
  15455. this.container.classList.add("circleAnnotation");
  15456. const {
  15457. data,
  15458. width,
  15459. height
  15460. } = this;
  15461. const svg = this.svgFactory.create(width, height, true);
  15462. const borderWidth = data.borderStyle.width;
  15463. const circle = this.#circle = this.svgFactory.createElement("svg:ellipse");
  15464. circle.setAttribute("cx", width / 2);
  15465. circle.setAttribute("cy", height / 2);
  15466. circle.setAttribute("rx", width / 2 - borderWidth / 2);
  15467. circle.setAttribute("ry", height / 2 - borderWidth / 2);
  15468. circle.setAttribute("stroke-width", borderWidth || 1);
  15469. circle.setAttribute("stroke", "transparent");
  15470. circle.setAttribute("fill", "transparent");
  15471. svg.append(circle);
  15472. this.container.append(svg);
  15473. if (!data.popupRef && this.hasPopupData) {
  15474. this._createPopup();
  15475. }
  15476. return this.container;
  15477. }
  15478. getElementsToTriggerPopup() {
  15479. return this.#circle;
  15480. }
  15481. addHighlightArea() {
  15482. this.container.classList.add("highlightArea");
  15483. }
  15484. }
  15485. class PolylineAnnotationElement extends AnnotationElement {
  15486. #polyline = null;
  15487. constructor(parameters) {
  15488. super(parameters, {
  15489. isRenderable: true,
  15490. ignoreBorder: true
  15491. });
  15492. this.containerClassName = "polylineAnnotation";
  15493. this.svgElementName = "svg:polyline";
  15494. }
  15495. render() {
  15496. this.container.classList.add(this.containerClassName);
  15497. const {
  15498. data: {
  15499. rect,
  15500. vertices,
  15501. borderStyle,
  15502. popupRef
  15503. },
  15504. width,
  15505. height
  15506. } = this;
  15507. if (!vertices) {
  15508. return this.container;
  15509. }
  15510. const svg = this.svgFactory.create(width, height, true);
  15511. let points = [];
  15512. for (let i = 0, ii = vertices.length; i < ii; i += 2) {
  15513. const x = vertices[i] - rect[0];
  15514. const y = rect[3] - vertices[i + 1];
  15515. points.push(`${x},${y}`);
  15516. }
  15517. points = points.join(" ");
  15518. const polyline = this.#polyline = this.svgFactory.createElement(this.svgElementName);
  15519. polyline.setAttribute("points", points);
  15520. polyline.setAttribute("stroke-width", borderStyle.width || 1);
  15521. polyline.setAttribute("stroke", "transparent");
  15522. polyline.setAttribute("fill", "transparent");
  15523. svg.append(polyline);
  15524. this.container.append(svg);
  15525. if (!popupRef && this.hasPopupData) {
  15526. this._createPopup();
  15527. }
  15528. return this.container;
  15529. }
  15530. getElementsToTriggerPopup() {
  15531. return this.#polyline;
  15532. }
  15533. addHighlightArea() {
  15534. this.container.classList.add("highlightArea");
  15535. }
  15536. }
  15537. class PolygonAnnotationElement extends PolylineAnnotationElement {
  15538. constructor(parameters) {
  15539. super(parameters);
  15540. this.containerClassName = "polygonAnnotation";
  15541. this.svgElementName = "svg:polygon";
  15542. }
  15543. }
  15544. class CaretAnnotationElement extends AnnotationElement {
  15545. constructor(parameters) {
  15546. super(parameters, {
  15547. isRenderable: true,
  15548. ignoreBorder: true
  15549. });
  15550. }
  15551. render() {
  15552. this.container.classList.add("caretAnnotation");
  15553. if (!this.data.popupRef && this.hasPopupData) {
  15554. this._createPopup();
  15555. }
  15556. return this.container;
  15557. }
  15558. }
  15559. class InkAnnotationElement extends AnnotationElement {
  15560. #polylinesGroupElement = null;
  15561. #polylines = [];
  15562. constructor(parameters) {
  15563. super(parameters, {
  15564. isRenderable: true,
  15565. ignoreBorder: true
  15566. });
  15567. this.containerClassName = "inkAnnotation";
  15568. this.svgElementName = "svg:polyline";
  15569. this.annotationEditorType = this.data.it === "InkHighlight" ? AnnotationEditorType.HIGHLIGHT : AnnotationEditorType.INK;
  15570. }
  15571. #getTransform(rotation, rect) {
  15572. switch (rotation) {
  15573. case 90:
  15574. return {
  15575. transform: `rotate(90) translate(${-rect[0]},${rect[1]}) scale(1,-1)`,
  15576. width: rect[3] - rect[1],
  15577. height: rect[2] - rect[0]
  15578. };
  15579. case 180:
  15580. return {
  15581. transform: `rotate(180) translate(${-rect[2]},${rect[1]}) scale(1,-1)`,
  15582. width: rect[2] - rect[0],
  15583. height: rect[3] - rect[1]
  15584. };
  15585. case 270:
  15586. return {
  15587. transform: `rotate(270) translate(${-rect[2]},${rect[3]}) scale(1,-1)`,
  15588. width: rect[3] - rect[1],
  15589. height: rect[2] - rect[0]
  15590. };
  15591. default:
  15592. return {
  15593. transform: `translate(${-rect[0]},${rect[3]}) scale(1,-1)`,
  15594. width: rect[2] - rect[0],
  15595. height: rect[3] - rect[1]
  15596. };
  15597. }
  15598. }
  15599. render() {
  15600. this.container.classList.add(this.containerClassName);
  15601. const {
  15602. data: {
  15603. rect,
  15604. rotation,
  15605. inkLists,
  15606. borderStyle,
  15607. popupRef
  15608. }
  15609. } = this;
  15610. const {
  15611. transform,
  15612. width,
  15613. height
  15614. } = this.#getTransform(rotation, rect);
  15615. const svg = this.svgFactory.create(width, height, true);
  15616. const g = this.#polylinesGroupElement = this.svgFactory.createElement("svg:g");
  15617. svg.append(g);
  15618. g.setAttribute("stroke-width", borderStyle.width || 1);
  15619. g.setAttribute("stroke-linecap", "round");
  15620. g.setAttribute("stroke-linejoin", "round");
  15621. g.setAttribute("stroke-miterlimit", 10);
  15622. g.setAttribute("stroke", "transparent");
  15623. g.setAttribute("fill", "transparent");
  15624. g.setAttribute("transform", transform);
  15625. for (let i = 0, ii = inkLists.length; i < ii; i++) {
  15626. const polyline = this.svgFactory.createElement(this.svgElementName);
  15627. this.#polylines.push(polyline);
  15628. polyline.setAttribute("points", inkLists[i].join(","));
  15629. g.append(polyline);
  15630. }
  15631. if (!popupRef && this.hasPopupData) {
  15632. this._createPopup();
  15633. }
  15634. this.container.append(svg);
  15635. this._editOnDoubleClick();
  15636. return this.container;
  15637. }
  15638. updateEdited(params) {
  15639. super.updateEdited(params);
  15640. const {
  15641. thickness,
  15642. points,
  15643. rect
  15644. } = params;
  15645. const g = this.#polylinesGroupElement;
  15646. if (thickness >= 0) {
  15647. g.setAttribute("stroke-width", thickness || 1);
  15648. }
  15649. if (points) {
  15650. for (let i = 0, ii = this.#polylines.length; i < ii; i++) {
  15651. this.#polylines[i].setAttribute("points", points[i].join(","));
  15652. }
  15653. }
  15654. if (rect) {
  15655. const {
  15656. transform,
  15657. width,
  15658. height
  15659. } = this.#getTransform(this.data.rotation, rect);
  15660. const root = g.parentElement;
  15661. root.setAttribute("viewBox", `0 0 ${width} ${height}`);
  15662. g.setAttribute("transform", transform);
  15663. }
  15664. }
  15665. getElementsToTriggerPopup() {
  15666. return this.#polylines;
  15667. }
  15668. addHighlightArea() {
  15669. this.container.classList.add("highlightArea");
  15670. }
  15671. }
  15672. class HighlightAnnotationElement extends AnnotationElement {
  15673. constructor(parameters) {
  15674. super(parameters, {
  15675. isRenderable: true,
  15676. ignoreBorder: true,
  15677. createQuadrilaterals: true
  15678. });
  15679. this.annotationEditorType = AnnotationEditorType.HIGHLIGHT;
  15680. }
  15681. render() {
  15682. if (!this.data.popupRef && this.hasPopupData) {
  15683. this._createPopup();
  15684. }
  15685. this.container.classList.add("highlightAnnotation");
  15686. this._editOnDoubleClick();
  15687. return this.container;
  15688. }
  15689. }
  15690. class UnderlineAnnotationElement extends AnnotationElement {
  15691. constructor(parameters) {
  15692. super(parameters, {
  15693. isRenderable: true,
  15694. ignoreBorder: true,
  15695. createQuadrilaterals: true
  15696. });
  15697. }
  15698. render() {
  15699. if (!this.data.popupRef && this.hasPopupData) {
  15700. this._createPopup();
  15701. }
  15702. this.container.classList.add("underlineAnnotation");
  15703. return this.container;
  15704. }
  15705. }
  15706. class SquigglyAnnotationElement extends AnnotationElement {
  15707. constructor(parameters) {
  15708. super(parameters, {
  15709. isRenderable: true,
  15710. ignoreBorder: true,
  15711. createQuadrilaterals: true
  15712. });
  15713. }
  15714. render() {
  15715. if (!this.data.popupRef && this.hasPopupData) {
  15716. this._createPopup();
  15717. }
  15718. this.container.classList.add("squigglyAnnotation");
  15719. return this.container;
  15720. }
  15721. }
  15722. class StrikeOutAnnotationElement extends AnnotationElement {
  15723. constructor(parameters) {
  15724. super(parameters, {
  15725. isRenderable: true,
  15726. ignoreBorder: true,
  15727. createQuadrilaterals: true
  15728. });
  15729. }
  15730. render() {
  15731. if (!this.data.popupRef && this.hasPopupData) {
  15732. this._createPopup();
  15733. }
  15734. this.container.classList.add("strikeoutAnnotation");
  15735. return this.container;
  15736. }
  15737. }
  15738. class StampAnnotationElement extends AnnotationElement {
  15739. constructor(parameters) {
  15740. super(parameters, {
  15741. isRenderable: true,
  15742. ignoreBorder: true
  15743. });
  15744. this.annotationEditorType = AnnotationEditorType.STAMP;
  15745. }
  15746. render() {
  15747. this.container.classList.add("stampAnnotation");
  15748. this.container.setAttribute("role", "img");
  15749. if (!this.data.popupRef && this.hasPopupData) {
  15750. this._createPopup();
  15751. }
  15752. this._editOnDoubleClick();
  15753. return this.container;
  15754. }
  15755. }
  15756. class FileAttachmentAnnotationElement extends AnnotationElement {
  15757. #trigger = null;
  15758. constructor(parameters) {
  15759. super(parameters, {
  15760. isRenderable: true
  15761. });
  15762. const {
  15763. file
  15764. } = this.data;
  15765. this.filename = file.filename;
  15766. this.content = file.content;
  15767. this.linkService.eventBus?.dispatch("fileattachmentannotation", {
  15768. source: this,
  15769. ...file
  15770. });
  15771. }
  15772. render() {
  15773. this.container.classList.add("fileAttachmentAnnotation");
  15774. const {
  15775. container,
  15776. data
  15777. } = this;
  15778. let trigger;
  15779. if (data.hasAppearance || data.fillAlpha === 0) {
  15780. trigger = document.createElement("div");
  15781. } else {
  15782. trigger = document.createElement("img");
  15783. trigger.src = `${this.imageResourcesPath}annotation-${/paperclip/i.test(data.name) ? "paperclip" : "pushpin"}.svg`;
  15784. if (data.fillAlpha && data.fillAlpha < 1) {
  15785. trigger.style = `filter: opacity(${Math.round(data.fillAlpha * 100)}%);`;
  15786. }
  15787. }
  15788. trigger.addEventListener("dblclick", this.#download.bind(this));
  15789. this.#trigger = trigger;
  15790. const {
  15791. isMac
  15792. } = util_FeatureTest.platform;
  15793. container.addEventListener("keydown", evt => {
  15794. if (evt.key === "Enter" && (isMac ? evt.metaKey : evt.ctrlKey)) {
  15795. this.#download();
  15796. }
  15797. });
  15798. if (!data.popupRef && this.hasPopupData) {
  15799. this._createPopup();
  15800. } else {
  15801. trigger.classList.add("popupTriggerArea");
  15802. }
  15803. container.append(trigger);
  15804. return container;
  15805. }
  15806. getElementsToTriggerPopup() {
  15807. return this.#trigger;
  15808. }
  15809. addHighlightArea() {
  15810. this.container.classList.add("highlightArea");
  15811. }
  15812. #download() {
  15813. this.downloadManager?.openOrDownloadData(this.content, this.filename);
  15814. }
  15815. }
  15816. class AnnotationLayer {
  15817. #accessibilityManager = null;
  15818. #annotationCanvasMap = null;
  15819. #editableAnnotations = new Map();
  15820. #structTreeLayer = null;
  15821. constructor({
  15822. div,
  15823. accessibilityManager,
  15824. annotationCanvasMap,
  15825. annotationEditorUIManager,
  15826. page,
  15827. viewport,
  15828. structTreeLayer
  15829. }) {
  15830. this.div = div;
  15831. this.#accessibilityManager = accessibilityManager;
  15832. this.#annotationCanvasMap = annotationCanvasMap;
  15833. this.#structTreeLayer = structTreeLayer || null;
  15834. this.page = page;
  15835. this.viewport = viewport;
  15836. this.zIndex = 0;
  15837. this._annotationEditorUIManager = annotationEditorUIManager;
  15838. }
  15839. hasEditableAnnotations() {
  15840. return this.#editableAnnotations.size > 0;
  15841. }
  15842. async #appendElement(element, id) {
  15843. const contentElement = element.firstChild || element;
  15844. const annotationId = contentElement.id = `${AnnotationPrefix}${id}`;
  15845. const ariaAttributes = await this.#structTreeLayer?.getAriaAttributes(annotationId);
  15846. if (ariaAttributes) {
  15847. for (const [key, value] of ariaAttributes) {
  15848. contentElement.setAttribute(key, value);
  15849. }
  15850. }
  15851. this.div.append(element);
  15852. this.#accessibilityManager?.moveElementInDOM(this.div, element, contentElement, false);
  15853. }
  15854. async render(params) {
  15855. const {
  15856. annotations
  15857. } = params;
  15858. const layer = this.div;
  15859. setLayerDimensions(layer, this.viewport);
  15860. const popupToElements = new Map();
  15861. const elementParams = {
  15862. data: null,
  15863. layer,
  15864. linkService: params.linkService,
  15865. downloadManager: params.downloadManager,
  15866. imageResourcesPath: params.imageResourcesPath || "",
  15867. renderForms: params.renderForms !== false,
  15868. svgFactory: new DOMSVGFactory(),
  15869. annotationStorage: params.annotationStorage || new AnnotationStorage(),
  15870. enableScripting: params.enableScripting === true,
  15871. hasJSActions: params.hasJSActions,
  15872. fieldObjects: params.fieldObjects,
  15873. parent: this,
  15874. elements: null
  15875. };
  15876. for (const data of annotations) {
  15877. if (data.noHTML) {
  15878. continue;
  15879. }
  15880. const isPopupAnnotation = data.annotationType === AnnotationType.POPUP;
  15881. if (!isPopupAnnotation) {
  15882. if (data.rect[2] === data.rect[0] || data.rect[3] === data.rect[1]) {
  15883. continue;
  15884. }
  15885. } else {
  15886. const elements = popupToElements.get(data.id);
  15887. if (!elements) {
  15888. continue;
  15889. }
  15890. elementParams.elements = elements;
  15891. }
  15892. elementParams.data = data;
  15893. const element = AnnotationElementFactory.create(elementParams);
  15894. if (!element.isRenderable) {
  15895. continue;
  15896. }
  15897. if (!isPopupAnnotation && data.popupRef) {
  15898. const elements = popupToElements.get(data.popupRef);
  15899. if (!elements) {
  15900. popupToElements.set(data.popupRef, [element]);
  15901. } else {
  15902. elements.push(element);
  15903. }
  15904. }
  15905. const rendered = element.render();
  15906. if (data.hidden) {
  15907. rendered.style.visibility = "hidden";
  15908. }
  15909. await this.#appendElement(rendered, data.id);
  15910. if (element._isEditable) {
  15911. this.#editableAnnotations.set(element.data.id, element);
  15912. this._annotationEditorUIManager?.renderAnnotationElement(element);
  15913. }
  15914. }
  15915. this.#setAnnotationCanvasMap();
  15916. }
  15917. async addLinkAnnotations(annotations, linkService) {
  15918. const elementParams = {
  15919. data: null,
  15920. layer: this.div,
  15921. linkService,
  15922. svgFactory: new DOMSVGFactory(),
  15923. parent: this
  15924. };
  15925. for (const data of annotations) {
  15926. data.borderStyle ||= AnnotationLayer._defaultBorderStyle;
  15927. elementParams.data = data;
  15928. const element = AnnotationElementFactory.create(elementParams);
  15929. if (!element.isRenderable) {
  15930. continue;
  15931. }
  15932. const rendered = element.render();
  15933. await this.#appendElement(rendered, data.id);
  15934. }
  15935. }
  15936. update({
  15937. viewport
  15938. }) {
  15939. const layer = this.div;
  15940. this.viewport = viewport;
  15941. setLayerDimensions(layer, {
  15942. rotation: viewport.rotation
  15943. });
  15944. this.#setAnnotationCanvasMap();
  15945. layer.hidden = false;
  15946. }
  15947. #setAnnotationCanvasMap() {
  15948. if (!this.#annotationCanvasMap) {
  15949. return;
  15950. }
  15951. const layer = this.div;
  15952. for (const [id, canvas] of this.#annotationCanvasMap) {
  15953. const element = layer.querySelector(`[data-annotation-id="${id}"]`);
  15954. if (!element) {
  15955. continue;
  15956. }
  15957. canvas.className = "annotationContent";
  15958. const {
  15959. firstChild
  15960. } = element;
  15961. if (!firstChild) {
  15962. element.append(canvas);
  15963. } else if (firstChild.nodeName === "CANVAS") {
  15964. firstChild.replaceWith(canvas);
  15965. } else if (!firstChild.classList.contains("annotationContent")) {
  15966. firstChild.before(canvas);
  15967. } else {
  15968. firstChild.after(canvas);
  15969. }
  15970. const editableAnnotation = this.#editableAnnotations.get(id);
  15971. if (!editableAnnotation) {
  15972. continue;
  15973. }
  15974. if (editableAnnotation._hasNoCanvas) {
  15975. this._annotationEditorUIManager?.setMissingCanvas(id, element.id, canvas);
  15976. editableAnnotation._hasNoCanvas = false;
  15977. } else {
  15978. editableAnnotation.canvas = canvas;
  15979. }
  15980. }
  15981. this.#annotationCanvasMap.clear();
  15982. }
  15983. getEditableAnnotations() {
  15984. return Array.from(this.#editableAnnotations.values());
  15985. }
  15986. getEditableAnnotation(id) {
  15987. return this.#editableAnnotations.get(id);
  15988. }
  15989. static get _defaultBorderStyle() {
  15990. return shadow(this, "_defaultBorderStyle", Object.freeze({
  15991. width: 1,
  15992. rawWidth: 1,
  15993. style: AnnotationBorderStyleType.SOLID,
  15994. dashArray: [3],
  15995. horizontalCornerRadius: 0,
  15996. verticalCornerRadius: 0
  15997. }));
  15998. }
  15999. }
  16000. ;// ./src/display/editor/freetext.js
  16001. const EOL_PATTERN = /\r\n?|\n/g;
  16002. class FreeTextEditor extends AnnotationEditor {
  16003. #color;
  16004. #content = "";
  16005. #editorDivId = `${this.id}-editor`;
  16006. #editModeAC = null;
  16007. #fontSize;
  16008. static _freeTextDefaultContent = "";
  16009. static _internalPadding = 0;
  16010. static _defaultColor = null;
  16011. static _defaultFontSize = 10;
  16012. static get _keyboardManager() {
  16013. const proto = FreeTextEditor.prototype;
  16014. const arrowChecker = self => self.isEmpty();
  16015. const small = AnnotationEditorUIManager.TRANSLATE_SMALL;
  16016. const big = AnnotationEditorUIManager.TRANSLATE_BIG;
  16017. return shadow(this, "_keyboardManager", new KeyboardManager([[["ctrl+s", "mac+meta+s", "ctrl+p", "mac+meta+p"], proto.commitOrRemove, {
  16018. bubbles: true
  16019. }], [["ctrl+Enter", "mac+meta+Enter", "Escape", "mac+Escape"], proto.commitOrRemove], [["ArrowLeft", "mac+ArrowLeft"], proto._translateEmpty, {
  16020. args: [-small, 0],
  16021. checker: arrowChecker
  16022. }], [["ctrl+ArrowLeft", "mac+shift+ArrowLeft"], proto._translateEmpty, {
  16023. args: [-big, 0],
  16024. checker: arrowChecker
  16025. }], [["ArrowRight", "mac+ArrowRight"], proto._translateEmpty, {
  16026. args: [small, 0],
  16027. checker: arrowChecker
  16028. }], [["ctrl+ArrowRight", "mac+shift+ArrowRight"], proto._translateEmpty, {
  16029. args: [big, 0],
  16030. checker: arrowChecker
  16031. }], [["ArrowUp", "mac+ArrowUp"], proto._translateEmpty, {
  16032. args: [0, -small],
  16033. checker: arrowChecker
  16034. }], [["ctrl+ArrowUp", "mac+shift+ArrowUp"], proto._translateEmpty, {
  16035. args: [0, -big],
  16036. checker: arrowChecker
  16037. }], [["ArrowDown", "mac+ArrowDown"], proto._translateEmpty, {
  16038. args: [0, small],
  16039. checker: arrowChecker
  16040. }], [["ctrl+ArrowDown", "mac+shift+ArrowDown"], proto._translateEmpty, {
  16041. args: [0, big],
  16042. checker: arrowChecker
  16043. }]]));
  16044. }
  16045. static _type = "freetext";
  16046. static _editorType = AnnotationEditorType.FREETEXT;
  16047. constructor(params) {
  16048. super({
  16049. ...params,
  16050. name: "freeTextEditor"
  16051. });
  16052. this.#color = params.color || FreeTextEditor._defaultColor || AnnotationEditor._defaultLineColor;
  16053. this.#fontSize = params.fontSize || FreeTextEditor._defaultFontSize;
  16054. }
  16055. static initialize(l10n, uiManager) {
  16056. AnnotationEditor.initialize(l10n, uiManager);
  16057. const style = getComputedStyle(document.documentElement);
  16058. this._internalPadding = parseFloat(style.getPropertyValue("--freetext-padding"));
  16059. }
  16060. static updateDefaultParams(type, value) {
  16061. switch (type) {
  16062. case AnnotationEditorParamsType.FREETEXT_SIZE:
  16063. FreeTextEditor._defaultFontSize = value;
  16064. break;
  16065. case AnnotationEditorParamsType.FREETEXT_COLOR:
  16066. FreeTextEditor._defaultColor = value;
  16067. break;
  16068. }
  16069. }
  16070. updateParams(type, value) {
  16071. switch (type) {
  16072. case AnnotationEditorParamsType.FREETEXT_SIZE:
  16073. this.#updateFontSize(value);
  16074. break;
  16075. case AnnotationEditorParamsType.FREETEXT_COLOR:
  16076. this.#updateColor(value);
  16077. break;
  16078. }
  16079. }
  16080. static get defaultPropertiesToUpdate() {
  16081. return [[AnnotationEditorParamsType.FREETEXT_SIZE, FreeTextEditor._defaultFontSize], [AnnotationEditorParamsType.FREETEXT_COLOR, FreeTextEditor._defaultColor || AnnotationEditor._defaultLineColor]];
  16082. }
  16083. get propertiesToUpdate() {
  16084. return [[AnnotationEditorParamsType.FREETEXT_SIZE, this.#fontSize], [AnnotationEditorParamsType.FREETEXT_COLOR, this.#color]];
  16085. }
  16086. #updateFontSize(fontSize) {
  16087. const setFontsize = size => {
  16088. this.editorDiv.style.fontSize = `calc(${size}px * var(--total-scale-factor))`;
  16089. this.translate(0, -(size - this.#fontSize) * this.parentScale);
  16090. this.#fontSize = size;
  16091. this.#setEditorDimensions();
  16092. };
  16093. const savedFontsize = this.#fontSize;
  16094. this.addCommands({
  16095. cmd: setFontsize.bind(this, fontSize),
  16096. undo: setFontsize.bind(this, savedFontsize),
  16097. post: this._uiManager.updateUI.bind(this._uiManager, this),
  16098. mustExec: true,
  16099. type: AnnotationEditorParamsType.FREETEXT_SIZE,
  16100. overwriteIfSameType: true,
  16101. keepUndo: true
  16102. });
  16103. }
  16104. #updateColor(color) {
  16105. const setColor = col => {
  16106. this.#color = this.editorDiv.style.color = col;
  16107. };
  16108. const savedColor = this.#color;
  16109. this.addCommands({
  16110. cmd: setColor.bind(this, color),
  16111. undo: setColor.bind(this, savedColor),
  16112. post: this._uiManager.updateUI.bind(this._uiManager, this),
  16113. mustExec: true,
  16114. type: AnnotationEditorParamsType.FREETEXT_COLOR,
  16115. overwriteIfSameType: true,
  16116. keepUndo: true
  16117. });
  16118. }
  16119. _translateEmpty(x, y) {
  16120. this._uiManager.translateSelectedEditors(x, y, true);
  16121. }
  16122. getInitialTranslation() {
  16123. const scale = this.parentScale;
  16124. return [-FreeTextEditor._internalPadding * scale, -(FreeTextEditor._internalPadding + this.#fontSize) * scale];
  16125. }
  16126. rebuild() {
  16127. if (!this.parent) {
  16128. return;
  16129. }
  16130. super.rebuild();
  16131. if (this.div === null) {
  16132. return;
  16133. }
  16134. if (!this.isAttachedToDOM) {
  16135. this.parent.add(this);
  16136. }
  16137. }
  16138. enableEditMode() {
  16139. if (this.isInEditMode()) {
  16140. return;
  16141. }
  16142. this.parent.setEditingState(false);
  16143. this.parent.updateToolbar(AnnotationEditorType.FREETEXT);
  16144. super.enableEditMode();
  16145. this.overlayDiv.classList.remove("enabled");
  16146. this.editorDiv.contentEditable = true;
  16147. this._isDraggable = false;
  16148. this.div.removeAttribute("aria-activedescendant");
  16149. this.#editModeAC = new AbortController();
  16150. const signal = this._uiManager.combinedSignal(this.#editModeAC);
  16151. this.editorDiv.addEventListener("keydown", this.editorDivKeydown.bind(this), {
  16152. signal
  16153. });
  16154. this.editorDiv.addEventListener("focus", this.editorDivFocus.bind(this), {
  16155. signal
  16156. });
  16157. this.editorDiv.addEventListener("blur", this.editorDivBlur.bind(this), {
  16158. signal
  16159. });
  16160. this.editorDiv.addEventListener("input", this.editorDivInput.bind(this), {
  16161. signal
  16162. });
  16163. this.editorDiv.addEventListener("paste", this.editorDivPaste.bind(this), {
  16164. signal
  16165. });
  16166. }
  16167. disableEditMode() {
  16168. if (!this.isInEditMode()) {
  16169. return;
  16170. }
  16171. this.parent.setEditingState(true);
  16172. super.disableEditMode();
  16173. this.overlayDiv.classList.add("enabled");
  16174. this.editorDiv.contentEditable = false;
  16175. this.div.setAttribute("aria-activedescendant", this.#editorDivId);
  16176. this._isDraggable = true;
  16177. this.#editModeAC?.abort();
  16178. this.#editModeAC = null;
  16179. this.div.focus({
  16180. preventScroll: true
  16181. });
  16182. this.isEditing = false;
  16183. this.parent.div.classList.add("freetextEditing");
  16184. }
  16185. focusin(event) {
  16186. if (!this._focusEventsAllowed) {
  16187. return;
  16188. }
  16189. super.focusin(event);
  16190. if (event.target !== this.editorDiv) {
  16191. this.editorDiv.focus();
  16192. }
  16193. }
  16194. onceAdded(focus) {
  16195. if (this.width) {
  16196. return;
  16197. }
  16198. this.enableEditMode();
  16199. if (focus) {
  16200. this.editorDiv.focus();
  16201. }
  16202. if (this._initialOptions?.isCentered) {
  16203. this.center();
  16204. }
  16205. this._initialOptions = null;
  16206. }
  16207. isEmpty() {
  16208. return !this.editorDiv || this.editorDiv.innerText.trim() === "";
  16209. }
  16210. remove() {
  16211. this.isEditing = false;
  16212. if (this.parent) {
  16213. this.parent.setEditingState(true);
  16214. this.parent.div.classList.add("freetextEditing");
  16215. }
  16216. super.remove();
  16217. }
  16218. #extractText() {
  16219. const buffer = [];
  16220. this.editorDiv.normalize();
  16221. let prevChild = null;
  16222. for (const child of this.editorDiv.childNodes) {
  16223. if (prevChild?.nodeType === Node.TEXT_NODE && child.nodeName === "BR") {
  16224. continue;
  16225. }
  16226. buffer.push(FreeTextEditor.#getNodeContent(child));
  16227. prevChild = child;
  16228. }
  16229. return buffer.join("\n");
  16230. }
  16231. #setEditorDimensions() {
  16232. const [parentWidth, parentHeight] = this.parentDimensions;
  16233. let rect;
  16234. if (this.isAttachedToDOM) {
  16235. rect = this.div.getBoundingClientRect();
  16236. } else {
  16237. const {
  16238. currentLayer,
  16239. div
  16240. } = this;
  16241. const savedDisplay = div.style.display;
  16242. const savedVisibility = div.classList.contains("hidden");
  16243. div.classList.remove("hidden");
  16244. div.style.display = "hidden";
  16245. currentLayer.div.append(this.div);
  16246. rect = div.getBoundingClientRect();
  16247. div.remove();
  16248. div.style.display = savedDisplay;
  16249. div.classList.toggle("hidden", savedVisibility);
  16250. }
  16251. if (this.rotation % 180 === this.parentRotation % 180) {
  16252. this.width = rect.width / parentWidth;
  16253. this.height = rect.height / parentHeight;
  16254. } else {
  16255. this.width = rect.height / parentWidth;
  16256. this.height = rect.width / parentHeight;
  16257. }
  16258. this.fixAndSetPosition();
  16259. }
  16260. commit() {
  16261. if (!this.isInEditMode()) {
  16262. return;
  16263. }
  16264. super.commit();
  16265. this.disableEditMode();
  16266. const savedText = this.#content;
  16267. const newText = this.#content = this.#extractText().trimEnd();
  16268. if (savedText === newText) {
  16269. return;
  16270. }
  16271. const setText = text => {
  16272. this.#content = text;
  16273. if (!text) {
  16274. this.remove();
  16275. return;
  16276. }
  16277. this.#setContent();
  16278. this._uiManager.rebuild(this);
  16279. this.#setEditorDimensions();
  16280. };
  16281. this.addCommands({
  16282. cmd: () => {
  16283. setText(newText);
  16284. },
  16285. undo: () => {
  16286. setText(savedText);
  16287. },
  16288. mustExec: false
  16289. });
  16290. this.#setEditorDimensions();
  16291. }
  16292. shouldGetKeyboardEvents() {
  16293. return this.isInEditMode();
  16294. }
  16295. enterInEditMode() {
  16296. this.enableEditMode();
  16297. this.editorDiv.focus();
  16298. }
  16299. dblclick(event) {
  16300. this.enterInEditMode();
  16301. }
  16302. keydown(event) {
  16303. if (event.target === this.div && event.key === "Enter") {
  16304. this.enterInEditMode();
  16305. event.preventDefault();
  16306. }
  16307. }
  16308. editorDivKeydown(event) {
  16309. FreeTextEditor._keyboardManager.exec(this, event);
  16310. }
  16311. editorDivFocus(event) {
  16312. this.isEditing = true;
  16313. }
  16314. editorDivBlur(event) {
  16315. this.isEditing = false;
  16316. }
  16317. editorDivInput(event) {
  16318. this.parent.div.classList.toggle("freetextEditing", this.isEmpty());
  16319. }
  16320. disableEditing() {
  16321. this.editorDiv.setAttribute("role", "comment");
  16322. this.editorDiv.removeAttribute("aria-multiline");
  16323. }
  16324. enableEditing() {
  16325. this.editorDiv.setAttribute("role", "textbox");
  16326. this.editorDiv.setAttribute("aria-multiline", true);
  16327. }
  16328. render() {
  16329. if (this.div) {
  16330. return this.div;
  16331. }
  16332. let baseX, baseY;
  16333. if (this._isCopy || this.annotationElementId) {
  16334. baseX = this.x;
  16335. baseY = this.y;
  16336. }
  16337. super.render();
  16338. this.editorDiv = document.createElement("div");
  16339. this.editorDiv.className = "internal";
  16340. this.editorDiv.setAttribute("id", this.#editorDivId);
  16341. this.editorDiv.setAttribute("data-l10n-id", "pdfjs-free-text2");
  16342. this.editorDiv.setAttribute("data-l10n-attrs", "default-content");
  16343. this.enableEditing();
  16344. this.editorDiv.contentEditable = true;
  16345. const {
  16346. style
  16347. } = this.editorDiv;
  16348. style.fontSize = `calc(${this.#fontSize}px * var(--total-scale-factor))`;
  16349. style.color = this.#color;
  16350. this.div.append(this.editorDiv);
  16351. this.overlayDiv = document.createElement("div");
  16352. this.overlayDiv.classList.add("overlay", "enabled");
  16353. this.div.append(this.overlayDiv);
  16354. bindEvents(this, this.div, ["dblclick", "keydown"]);
  16355. if (this._isCopy || this.annotationElementId) {
  16356. const [parentWidth, parentHeight] = this.parentDimensions;
  16357. if (this.annotationElementId) {
  16358. const {
  16359. position
  16360. } = this._initialData;
  16361. let [tx, ty] = this.getInitialTranslation();
  16362. [tx, ty] = this.pageTranslationToScreen(tx, ty);
  16363. const [pageWidth, pageHeight] = this.pageDimensions;
  16364. const [pageX, pageY] = this.pageTranslation;
  16365. let posX, posY;
  16366. switch (this.rotation) {
  16367. case 0:
  16368. posX = baseX + (position[0] - pageX) / pageWidth;
  16369. posY = baseY + this.height - (position[1] - pageY) / pageHeight;
  16370. break;
  16371. case 90:
  16372. posX = baseX + (position[0] - pageX) / pageWidth;
  16373. posY = baseY - (position[1] - pageY) / pageHeight;
  16374. [tx, ty] = [ty, -tx];
  16375. break;
  16376. case 180:
  16377. posX = baseX - this.width + (position[0] - pageX) / pageWidth;
  16378. posY = baseY - (position[1] - pageY) / pageHeight;
  16379. [tx, ty] = [-tx, -ty];
  16380. break;
  16381. case 270:
  16382. posX = baseX + (position[0] - pageX - this.height * pageHeight) / pageWidth;
  16383. posY = baseY + (position[1] - pageY - this.width * pageWidth) / pageHeight;
  16384. [tx, ty] = [-ty, tx];
  16385. break;
  16386. }
  16387. this.setAt(posX * parentWidth, posY * parentHeight, tx, ty);
  16388. } else {
  16389. this._moveAfterPaste(baseX, baseY);
  16390. }
  16391. this.#setContent();
  16392. this._isDraggable = true;
  16393. this.editorDiv.contentEditable = false;
  16394. } else {
  16395. this._isDraggable = false;
  16396. this.editorDiv.contentEditable = true;
  16397. }
  16398. return this.div;
  16399. }
  16400. static #getNodeContent(node) {
  16401. return (node.nodeType === Node.TEXT_NODE ? node.nodeValue : node.innerText).replaceAll(EOL_PATTERN, "");
  16402. }
  16403. editorDivPaste(event) {
  16404. const clipboardData = event.clipboardData || window.clipboardData;
  16405. const {
  16406. types
  16407. } = clipboardData;
  16408. if (types.length === 1 && types[0] === "text/plain") {
  16409. return;
  16410. }
  16411. event.preventDefault();
  16412. const paste = FreeTextEditor.#deserializeContent(clipboardData.getData("text") || "").replaceAll(EOL_PATTERN, "\n");
  16413. if (!paste) {
  16414. return;
  16415. }
  16416. const selection = window.getSelection();
  16417. if (!selection.rangeCount) {
  16418. return;
  16419. }
  16420. this.editorDiv.normalize();
  16421. selection.deleteFromDocument();
  16422. const range = selection.getRangeAt(0);
  16423. if (!paste.includes("\n")) {
  16424. range.insertNode(document.createTextNode(paste));
  16425. this.editorDiv.normalize();
  16426. selection.collapseToStart();
  16427. return;
  16428. }
  16429. const {
  16430. startContainer,
  16431. startOffset
  16432. } = range;
  16433. const bufferBefore = [];
  16434. const bufferAfter = [];
  16435. if (startContainer.nodeType === Node.TEXT_NODE) {
  16436. const parent = startContainer.parentElement;
  16437. bufferAfter.push(startContainer.nodeValue.slice(startOffset).replaceAll(EOL_PATTERN, ""));
  16438. if (parent !== this.editorDiv) {
  16439. let buffer = bufferBefore;
  16440. for (const child of this.editorDiv.childNodes) {
  16441. if (child === parent) {
  16442. buffer = bufferAfter;
  16443. continue;
  16444. }
  16445. buffer.push(FreeTextEditor.#getNodeContent(child));
  16446. }
  16447. }
  16448. bufferBefore.push(startContainer.nodeValue.slice(0, startOffset).replaceAll(EOL_PATTERN, ""));
  16449. } else if (startContainer === this.editorDiv) {
  16450. let buffer = bufferBefore;
  16451. let i = 0;
  16452. for (const child of this.editorDiv.childNodes) {
  16453. if (i++ === startOffset) {
  16454. buffer = bufferAfter;
  16455. }
  16456. buffer.push(FreeTextEditor.#getNodeContent(child));
  16457. }
  16458. }
  16459. this.#content = `${bufferBefore.join("\n")}${paste}${bufferAfter.join("\n")}`;
  16460. this.#setContent();
  16461. const newRange = new Range();
  16462. let beforeLength = Math.sumPrecise(bufferBefore.map(line => line.length));
  16463. for (const {
  16464. firstChild
  16465. } of this.editorDiv.childNodes) {
  16466. if (firstChild.nodeType === Node.TEXT_NODE) {
  16467. const length = firstChild.nodeValue.length;
  16468. if (beforeLength <= length) {
  16469. newRange.setStart(firstChild, beforeLength);
  16470. newRange.setEnd(firstChild, beforeLength);
  16471. break;
  16472. }
  16473. beforeLength -= length;
  16474. }
  16475. }
  16476. selection.removeAllRanges();
  16477. selection.addRange(newRange);
  16478. }
  16479. #setContent() {
  16480. this.editorDiv.replaceChildren();
  16481. if (!this.#content) {
  16482. return;
  16483. }
  16484. for (const line of this.#content.split("\n")) {
  16485. const div = document.createElement("div");
  16486. div.append(line ? document.createTextNode(line) : document.createElement("br"));
  16487. this.editorDiv.append(div);
  16488. }
  16489. }
  16490. #serializeContent() {
  16491. return this.#content.replaceAll("\xa0", " ");
  16492. }
  16493. static #deserializeContent(content) {
  16494. return content.replaceAll(" ", "\xa0");
  16495. }
  16496. get contentDiv() {
  16497. return this.editorDiv;
  16498. }
  16499. static async deserialize(data, parent, uiManager) {
  16500. let initialData = null;
  16501. if (data instanceof FreeTextAnnotationElement) {
  16502. const {
  16503. data: {
  16504. defaultAppearanceData: {
  16505. fontSize,
  16506. fontColor
  16507. },
  16508. rect,
  16509. rotation,
  16510. id,
  16511. popupRef
  16512. },
  16513. textContent,
  16514. textPosition,
  16515. parent: {
  16516. page: {
  16517. pageNumber
  16518. }
  16519. }
  16520. } = data;
  16521. if (!textContent || textContent.length === 0) {
  16522. return null;
  16523. }
  16524. initialData = data = {
  16525. annotationType: AnnotationEditorType.FREETEXT,
  16526. color: Array.from(fontColor),
  16527. fontSize,
  16528. value: textContent.join("\n"),
  16529. position: textPosition,
  16530. pageIndex: pageNumber - 1,
  16531. rect: rect.slice(0),
  16532. rotation,
  16533. id,
  16534. deleted: false,
  16535. popupRef
  16536. };
  16537. }
  16538. const editor = await super.deserialize(data, parent, uiManager);
  16539. editor.#fontSize = data.fontSize;
  16540. editor.#color = Util.makeHexColor(...data.color);
  16541. editor.#content = FreeTextEditor.#deserializeContent(data.value);
  16542. editor.annotationElementId = data.id || null;
  16543. editor._initialData = initialData;
  16544. return editor;
  16545. }
  16546. serialize(isForCopying = false) {
  16547. if (this.isEmpty()) {
  16548. return null;
  16549. }
  16550. if (this.deleted) {
  16551. return this.serializeDeleted();
  16552. }
  16553. const padding = FreeTextEditor._internalPadding * this.parentScale;
  16554. const rect = this.getRect(padding, padding);
  16555. const color = AnnotationEditor._colorManager.convert(this.isAttachedToDOM ? getComputedStyle(this.editorDiv).color : this.#color);
  16556. const serialized = {
  16557. annotationType: AnnotationEditorType.FREETEXT,
  16558. color,
  16559. fontSize: this.#fontSize,
  16560. value: this.#serializeContent(),
  16561. pageIndex: this.pageIndex,
  16562. rect,
  16563. rotation: this.rotation,
  16564. structTreeParentId: this._structTreeParentId
  16565. };
  16566. if (isForCopying) {
  16567. serialized.isCopy = true;
  16568. return serialized;
  16569. }
  16570. if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
  16571. return null;
  16572. }
  16573. serialized.id = this.annotationElementId;
  16574. return serialized;
  16575. }
  16576. #hasElementChanged(serialized) {
  16577. const {
  16578. value,
  16579. fontSize,
  16580. color,
  16581. pageIndex
  16582. } = this._initialData;
  16583. return this._hasBeenMoved || serialized.value !== value || serialized.fontSize !== fontSize || serialized.color.some((c, i) => c !== color[i]) || serialized.pageIndex !== pageIndex;
  16584. }
  16585. renderAnnotationElement(annotation) {
  16586. const content = super.renderAnnotationElement(annotation);
  16587. if (this.deleted) {
  16588. return content;
  16589. }
  16590. const {
  16591. style
  16592. } = content;
  16593. style.fontSize = `calc(${this.#fontSize}px * var(--total-scale-factor))`;
  16594. style.color = this.#color;
  16595. content.replaceChildren();
  16596. for (const line of this.#content.split("\n")) {
  16597. const div = document.createElement("div");
  16598. div.append(line ? document.createTextNode(line) : document.createElement("br"));
  16599. content.append(div);
  16600. }
  16601. const padding = FreeTextEditor._internalPadding * this.parentScale;
  16602. annotation.updateEdited({
  16603. rect: this.getRect(padding, padding),
  16604. popupContent: this.#content
  16605. });
  16606. return content;
  16607. }
  16608. resetAnnotationElement(annotation) {
  16609. super.resetAnnotationElement(annotation);
  16610. annotation.resetEdited();
  16611. }
  16612. }
  16613. ;// ./src/display/editor/drawers/outline.js
  16614. class Outline {
  16615. static PRECISION = 1e-4;
  16616. toSVGPath() {
  16617. unreachable("Abstract method `toSVGPath` must be implemented.");
  16618. }
  16619. get box() {
  16620. unreachable("Abstract getter `box` must be implemented.");
  16621. }
  16622. serialize(_bbox, _rotation) {
  16623. unreachable("Abstract method `serialize` must be implemented.");
  16624. }
  16625. static _rescale(src, tx, ty, sx, sy, dest) {
  16626. dest ||= new Float32Array(src.length);
  16627. for (let i = 0, ii = src.length; i < ii; i += 2) {
  16628. dest[i] = tx + src[i] * sx;
  16629. dest[i + 1] = ty + src[i + 1] * sy;
  16630. }
  16631. return dest;
  16632. }
  16633. static _rescaleAndSwap(src, tx, ty, sx, sy, dest) {
  16634. dest ||= new Float32Array(src.length);
  16635. for (let i = 0, ii = src.length; i < ii; i += 2) {
  16636. dest[i] = tx + src[i + 1] * sx;
  16637. dest[i + 1] = ty + src[i] * sy;
  16638. }
  16639. return dest;
  16640. }
  16641. static _translate(src, tx, ty, dest) {
  16642. dest ||= new Float32Array(src.length);
  16643. for (let i = 0, ii = src.length; i < ii; i += 2) {
  16644. dest[i] = tx + src[i];
  16645. dest[i + 1] = ty + src[i + 1];
  16646. }
  16647. return dest;
  16648. }
  16649. static svgRound(x) {
  16650. return Math.round(x * 10000);
  16651. }
  16652. static _normalizePoint(x, y, parentWidth, parentHeight, rotation) {
  16653. switch (rotation) {
  16654. case 90:
  16655. return [1 - y / parentWidth, x / parentHeight];
  16656. case 180:
  16657. return [1 - x / parentWidth, 1 - y / parentHeight];
  16658. case 270:
  16659. return [y / parentWidth, 1 - x / parentHeight];
  16660. default:
  16661. return [x / parentWidth, y / parentHeight];
  16662. }
  16663. }
  16664. static _normalizePagePoint(x, y, rotation) {
  16665. switch (rotation) {
  16666. case 90:
  16667. return [1 - y, x];
  16668. case 180:
  16669. return [1 - x, 1 - y];
  16670. case 270:
  16671. return [y, 1 - x];
  16672. default:
  16673. return [x, y];
  16674. }
  16675. }
  16676. static createBezierPoints(x1, y1, x2, y2, x3, y3) {
  16677. return [(x1 + 5 * x2) / 6, (y1 + 5 * y2) / 6, (5 * x2 + x3) / 6, (5 * y2 + y3) / 6, (x2 + x3) / 2, (y2 + y3) / 2];
  16678. }
  16679. }
  16680. ;// ./src/display/editor/drawers/freedraw.js
  16681. class FreeDrawOutliner {
  16682. #box;
  16683. #bottom = [];
  16684. #innerMargin;
  16685. #isLTR;
  16686. #top = [];
  16687. #last = new Float32Array(18);
  16688. #lastX;
  16689. #lastY;
  16690. #min;
  16691. #min_dist;
  16692. #scaleFactor;
  16693. #thickness;
  16694. #points = [];
  16695. static #MIN_DIST = 8;
  16696. static #MIN_DIFF = 2;
  16697. static #MIN = FreeDrawOutliner.#MIN_DIST + FreeDrawOutliner.#MIN_DIFF;
  16698. constructor({
  16699. x,
  16700. y
  16701. }, box, scaleFactor, thickness, isLTR, innerMargin = 0) {
  16702. this.#box = box;
  16703. this.#thickness = thickness * scaleFactor;
  16704. this.#isLTR = isLTR;
  16705. this.#last.set([NaN, NaN, NaN, NaN, x, y], 6);
  16706. this.#innerMargin = innerMargin;
  16707. this.#min_dist = FreeDrawOutliner.#MIN_DIST * scaleFactor;
  16708. this.#min = FreeDrawOutliner.#MIN * scaleFactor;
  16709. this.#scaleFactor = scaleFactor;
  16710. this.#points.push(x, y);
  16711. }
  16712. isEmpty() {
  16713. return isNaN(this.#last[8]);
  16714. }
  16715. #getLastCoords() {
  16716. const lastTop = this.#last.subarray(4, 6);
  16717. const lastBottom = this.#last.subarray(16, 18);
  16718. const [x, y, width, height] = this.#box;
  16719. 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];
  16720. }
  16721. add({
  16722. x,
  16723. y
  16724. }) {
  16725. this.#lastX = x;
  16726. this.#lastY = y;
  16727. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16728. let [x1, y1, x2, y2] = this.#last.subarray(8, 12);
  16729. const diffX = x - x2;
  16730. const diffY = y - y2;
  16731. const d = Math.hypot(diffX, diffY);
  16732. if (d < this.#min) {
  16733. return false;
  16734. }
  16735. const diffD = d - this.#min_dist;
  16736. const K = diffD / d;
  16737. const shiftX = K * diffX;
  16738. const shiftY = K * diffY;
  16739. let x0 = x1;
  16740. let y0 = y1;
  16741. x1 = x2;
  16742. y1 = y2;
  16743. x2 += shiftX;
  16744. y2 += shiftY;
  16745. this.#points?.push(x, y);
  16746. const nX = -shiftY / diffD;
  16747. const nY = shiftX / diffD;
  16748. const thX = nX * this.#thickness;
  16749. const thY = nY * this.#thickness;
  16750. this.#last.set(this.#last.subarray(2, 8), 0);
  16751. this.#last.set([x2 + thX, y2 + thY], 4);
  16752. this.#last.set(this.#last.subarray(14, 18), 12);
  16753. this.#last.set([x2 - thX, y2 - thY], 16);
  16754. if (isNaN(this.#last[6])) {
  16755. if (this.#top.length === 0) {
  16756. this.#last.set([x1 + thX, y1 + thY], 2);
  16757. this.#top.push(NaN, NaN, NaN, NaN, (x1 + thX - layerX) / layerWidth, (y1 + thY - layerY) / layerHeight);
  16758. this.#last.set([x1 - thX, y1 - thY], 14);
  16759. this.#bottom.push(NaN, NaN, NaN, NaN, (x1 - thX - layerX) / layerWidth, (y1 - thY - layerY) / layerHeight);
  16760. }
  16761. this.#last.set([x0, y0, x1, y1, x2, y2], 6);
  16762. return !this.isEmpty();
  16763. }
  16764. this.#last.set([x0, y0, x1, y1, x2, y2], 6);
  16765. const angle = Math.abs(Math.atan2(y0 - y1, x0 - x1) - Math.atan2(shiftY, shiftX));
  16766. if (angle < Math.PI / 2) {
  16767. [x1, y1, x2, y2] = this.#last.subarray(2, 6);
  16768. this.#top.push(NaN, NaN, NaN, NaN, ((x1 + x2) / 2 - layerX) / layerWidth, ((y1 + y2) / 2 - layerY) / layerHeight);
  16769. [x1, y1, x0, y0] = this.#last.subarray(14, 18);
  16770. this.#bottom.push(NaN, NaN, NaN, NaN, ((x0 + x1) / 2 - layerX) / layerWidth, ((y0 + y1) / 2 - layerY) / layerHeight);
  16771. return true;
  16772. }
  16773. [x0, y0, x1, y1, x2, y2] = this.#last.subarray(0, 6);
  16774. 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);
  16775. [x2, y2, x1, y1, x0, y0] = this.#last.subarray(12, 18);
  16776. 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);
  16777. return true;
  16778. }
  16779. toSVGPath() {
  16780. if (this.isEmpty()) {
  16781. return "";
  16782. }
  16783. const top = this.#top;
  16784. const bottom = this.#bottom;
  16785. if (isNaN(this.#last[6]) && !this.isEmpty()) {
  16786. return this.#toSVGPathTwoPoints();
  16787. }
  16788. const buffer = [];
  16789. buffer.push(`M${top[4]} ${top[5]}`);
  16790. for (let i = 6; i < top.length; i += 6) {
  16791. if (isNaN(top[i])) {
  16792. buffer.push(`L${top[i + 4]} ${top[i + 5]}`);
  16793. } else {
  16794. buffer.push(`C${top[i]} ${top[i + 1]} ${top[i + 2]} ${top[i + 3]} ${top[i + 4]} ${top[i + 5]}`);
  16795. }
  16796. }
  16797. this.#toSVGPathEnd(buffer);
  16798. for (let i = bottom.length - 6; i >= 6; i -= 6) {
  16799. if (isNaN(bottom[i])) {
  16800. buffer.push(`L${bottom[i + 4]} ${bottom[i + 5]}`);
  16801. } else {
  16802. buffer.push(`C${bottom[i]} ${bottom[i + 1]} ${bottom[i + 2]} ${bottom[i + 3]} ${bottom[i + 4]} ${bottom[i + 5]}`);
  16803. }
  16804. }
  16805. this.#toSVGPathStart(buffer);
  16806. return buffer.join(" ");
  16807. }
  16808. #toSVGPathTwoPoints() {
  16809. const [x, y, width, height] = this.#box;
  16810. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16811. 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`;
  16812. }
  16813. #toSVGPathStart(buffer) {
  16814. const bottom = this.#bottom;
  16815. buffer.push(`L${bottom[4]} ${bottom[5]} Z`);
  16816. }
  16817. #toSVGPathEnd(buffer) {
  16818. const [x, y, width, height] = this.#box;
  16819. const lastTop = this.#last.subarray(4, 6);
  16820. const lastBottom = this.#last.subarray(16, 18);
  16821. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16822. 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}`);
  16823. }
  16824. newFreeDrawOutline(outline, points, box, scaleFactor, innerMargin, isLTR) {
  16825. return new FreeDrawOutline(outline, points, box, scaleFactor, innerMargin, isLTR);
  16826. }
  16827. getOutlines() {
  16828. const top = this.#top;
  16829. const bottom = this.#bottom;
  16830. const last = this.#last;
  16831. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16832. const points = new Float32Array((this.#points?.length ?? 0) + 2);
  16833. for (let i = 0, ii = points.length - 2; i < ii; i += 2) {
  16834. points[i] = (this.#points[i] - layerX) / layerWidth;
  16835. points[i + 1] = (this.#points[i + 1] - layerY) / layerHeight;
  16836. }
  16837. points[points.length - 2] = (this.#lastX - layerX) / layerWidth;
  16838. points[points.length - 1] = (this.#lastY - layerY) / layerHeight;
  16839. if (isNaN(last[6]) && !this.isEmpty()) {
  16840. return this.#getOutlineTwoPoints(points);
  16841. }
  16842. const outline = new Float32Array(this.#top.length + 24 + this.#bottom.length);
  16843. let N = top.length;
  16844. for (let i = 0; i < N; i += 2) {
  16845. if (isNaN(top[i])) {
  16846. outline[i] = outline[i + 1] = NaN;
  16847. continue;
  16848. }
  16849. outline[i] = top[i];
  16850. outline[i + 1] = top[i + 1];
  16851. }
  16852. N = this.#getOutlineEnd(outline, N);
  16853. for (let i = bottom.length - 6; i >= 6; i -= 6) {
  16854. for (let j = 0; j < 6; j += 2) {
  16855. if (isNaN(bottom[i + j])) {
  16856. outline[N] = outline[N + 1] = NaN;
  16857. N += 2;
  16858. continue;
  16859. }
  16860. outline[N] = bottom[i + j];
  16861. outline[N + 1] = bottom[i + j + 1];
  16862. N += 2;
  16863. }
  16864. }
  16865. this.#getOutlineStart(outline, N);
  16866. return this.newFreeDrawOutline(outline, points, this.#box, this.#scaleFactor, this.#innerMargin, this.#isLTR);
  16867. }
  16868. #getOutlineTwoPoints(points) {
  16869. const last = this.#last;
  16870. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16871. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16872. const outline = new Float32Array(36);
  16873. 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);
  16874. return this.newFreeDrawOutline(outline, points, this.#box, this.#scaleFactor, this.#innerMargin, this.#isLTR);
  16875. }
  16876. #getOutlineStart(outline, pos) {
  16877. const bottom = this.#bottom;
  16878. outline.set([NaN, NaN, NaN, NaN, bottom[4], bottom[5]], pos);
  16879. return pos += 6;
  16880. }
  16881. #getOutlineEnd(outline, pos) {
  16882. const lastTop = this.#last.subarray(4, 6);
  16883. const lastBottom = this.#last.subarray(16, 18);
  16884. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  16885. const [lastTopX, lastTopY, lastBottomX, lastBottomY] = this.#getLastCoords();
  16886. 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);
  16887. return pos += 24;
  16888. }
  16889. }
  16890. class FreeDrawOutline extends Outline {
  16891. #box;
  16892. #bbox = new Float32Array(4);
  16893. #innerMargin;
  16894. #isLTR;
  16895. #points;
  16896. #scaleFactor;
  16897. #outline;
  16898. constructor(outline, points, box, scaleFactor, innerMargin, isLTR) {
  16899. super();
  16900. this.#outline = outline;
  16901. this.#points = points;
  16902. this.#box = box;
  16903. this.#scaleFactor = scaleFactor;
  16904. this.#innerMargin = innerMargin;
  16905. this.#isLTR = isLTR;
  16906. this.lastPoint = [NaN, NaN];
  16907. this.#computeMinMax(isLTR);
  16908. const [x, y, width, height] = this.#bbox;
  16909. for (let i = 0, ii = outline.length; i < ii; i += 2) {
  16910. outline[i] = (outline[i] - x) / width;
  16911. outline[i + 1] = (outline[i + 1] - y) / height;
  16912. }
  16913. for (let i = 0, ii = points.length; i < ii; i += 2) {
  16914. points[i] = (points[i] - x) / width;
  16915. points[i + 1] = (points[i + 1] - y) / height;
  16916. }
  16917. }
  16918. toSVGPath() {
  16919. const buffer = [`M${this.#outline[4]} ${this.#outline[5]}`];
  16920. for (let i = 6, ii = this.#outline.length; i < ii; i += 6) {
  16921. if (isNaN(this.#outline[i])) {
  16922. buffer.push(`L${this.#outline[i + 4]} ${this.#outline[i + 5]}`);
  16923. continue;
  16924. }
  16925. 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]}`);
  16926. }
  16927. buffer.push("Z");
  16928. return buffer.join(" ");
  16929. }
  16930. serialize([blX, blY, trX, trY], rotation) {
  16931. const width = trX - blX;
  16932. const height = trY - blY;
  16933. let outline;
  16934. let points;
  16935. switch (rotation) {
  16936. case 0:
  16937. outline = Outline._rescale(this.#outline, blX, trY, width, -height);
  16938. points = Outline._rescale(this.#points, blX, trY, width, -height);
  16939. break;
  16940. case 90:
  16941. outline = Outline._rescaleAndSwap(this.#outline, blX, blY, width, height);
  16942. points = Outline._rescaleAndSwap(this.#points, blX, blY, width, height);
  16943. break;
  16944. case 180:
  16945. outline = Outline._rescale(this.#outline, trX, blY, -width, height);
  16946. points = Outline._rescale(this.#points, trX, blY, -width, height);
  16947. break;
  16948. case 270:
  16949. outline = Outline._rescaleAndSwap(this.#outline, trX, trY, -width, -height);
  16950. points = Outline._rescaleAndSwap(this.#points, trX, trY, -width, -height);
  16951. break;
  16952. }
  16953. return {
  16954. outline: Array.from(outline),
  16955. points: [Array.from(points)]
  16956. };
  16957. }
  16958. #computeMinMax(isLTR) {
  16959. const outline = this.#outline;
  16960. let lastX = outline[4];
  16961. let lastY = outline[5];
  16962. const minMax = [lastX, lastY, lastX, lastY];
  16963. let lastPointX = lastX;
  16964. let lastPointY = lastY;
  16965. const ltrCallback = isLTR ? Math.max : Math.min;
  16966. for (let i = 6, ii = outline.length; i < ii; i += 6) {
  16967. const x = outline[i + 4],
  16968. y = outline[i + 5];
  16969. if (isNaN(outline[i])) {
  16970. Util.pointBoundingBox(x, y, minMax);
  16971. if (lastPointY < y) {
  16972. lastPointX = x;
  16973. lastPointY = y;
  16974. } else if (lastPointY === y) {
  16975. lastPointX = ltrCallback(lastPointX, x);
  16976. }
  16977. } else {
  16978. const bbox = [Infinity, Infinity, -Infinity, -Infinity];
  16979. Util.bezierBoundingBox(lastX, lastY, ...outline.slice(i, i + 6), bbox);
  16980. Util.rectBoundingBox(...bbox, minMax);
  16981. if (lastPointY < bbox[3]) {
  16982. lastPointX = bbox[2];
  16983. lastPointY = bbox[3];
  16984. } else if (lastPointY === bbox[3]) {
  16985. lastPointX = ltrCallback(lastPointX, bbox[2]);
  16986. }
  16987. }
  16988. lastX = x;
  16989. lastY = y;
  16990. }
  16991. const bbox = this.#bbox;
  16992. bbox[0] = minMax[0] - this.#innerMargin;
  16993. bbox[1] = minMax[1] - this.#innerMargin;
  16994. bbox[2] = minMax[2] - minMax[0] + 2 * this.#innerMargin;
  16995. bbox[3] = minMax[3] - minMax[1] + 2 * this.#innerMargin;
  16996. this.lastPoint = [lastPointX, lastPointY];
  16997. }
  16998. get box() {
  16999. return this.#bbox;
  17000. }
  17001. newOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin = 0) {
  17002. return new FreeDrawOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin);
  17003. }
  17004. getNewOutline(thickness, innerMargin) {
  17005. const [x, y, width, height] = this.#bbox;
  17006. const [layerX, layerY, layerWidth, layerHeight] = this.#box;
  17007. const sx = width * layerWidth;
  17008. const sy = height * layerHeight;
  17009. const tx = x * layerWidth + layerX;
  17010. const ty = y * layerHeight + layerY;
  17011. const outliner = this.newOutliner({
  17012. x: this.#points[0] * sx + tx,
  17013. y: this.#points[1] * sy + ty
  17014. }, this.#box, this.#scaleFactor, thickness, this.#isLTR, innerMargin ?? this.#innerMargin);
  17015. for (let i = 2; i < this.#points.length; i += 2) {
  17016. outliner.add({
  17017. x: this.#points[i] * sx + tx,
  17018. y: this.#points[i + 1] * sy + ty
  17019. });
  17020. }
  17021. return outliner.getOutlines();
  17022. }
  17023. }
  17024. ;// ./src/display/editor/drawers/highlight.js
  17025. class HighlightOutliner {
  17026. #box;
  17027. #lastPoint;
  17028. #verticalEdges = [];
  17029. #intervals = [];
  17030. constructor(boxes, borderWidth = 0, innerMargin = 0, isLTR = true) {
  17031. const minMax = [Infinity, Infinity, -Infinity, -Infinity];
  17032. const NUMBER_OF_DIGITS = 4;
  17033. const EPSILON = 10 ** -NUMBER_OF_DIGITS;
  17034. for (const {
  17035. x,
  17036. y,
  17037. width,
  17038. height
  17039. } of boxes) {
  17040. const x1 = Math.floor((x - borderWidth) / EPSILON) * EPSILON;
  17041. const x2 = Math.ceil((x + width + borderWidth) / EPSILON) * EPSILON;
  17042. const y1 = Math.floor((y - borderWidth) / EPSILON) * EPSILON;
  17043. const y2 = Math.ceil((y + height + borderWidth) / EPSILON) * EPSILON;
  17044. const left = [x1, y1, y2, true];
  17045. const right = [x2, y1, y2, false];
  17046. this.#verticalEdges.push(left, right);
  17047. Util.rectBoundingBox(x1, y1, x2, y2, minMax);
  17048. }
  17049. const bboxWidth = minMax[2] - minMax[0] + 2 * innerMargin;
  17050. const bboxHeight = minMax[3] - minMax[1] + 2 * innerMargin;
  17051. const shiftedMinX = minMax[0] - innerMargin;
  17052. const shiftedMinY = minMax[1] - innerMargin;
  17053. const lastEdge = this.#verticalEdges.at(isLTR ? -1 : -2);
  17054. const lastPoint = [lastEdge[0], lastEdge[2]];
  17055. for (const edge of this.#verticalEdges) {
  17056. const [x, y1, y2] = edge;
  17057. edge[0] = (x - shiftedMinX) / bboxWidth;
  17058. edge[1] = (y1 - shiftedMinY) / bboxHeight;
  17059. edge[2] = (y2 - shiftedMinY) / bboxHeight;
  17060. }
  17061. this.#box = new Float32Array([shiftedMinX, shiftedMinY, bboxWidth, bboxHeight]);
  17062. this.#lastPoint = lastPoint;
  17063. }
  17064. getOutlines() {
  17065. this.#verticalEdges.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
  17066. const outlineVerticalEdges = [];
  17067. for (const edge of this.#verticalEdges) {
  17068. if (edge[3]) {
  17069. outlineVerticalEdges.push(...this.#breakEdge(edge));
  17070. this.#insert(edge);
  17071. } else {
  17072. this.#remove(edge);
  17073. outlineVerticalEdges.push(...this.#breakEdge(edge));
  17074. }
  17075. }
  17076. return this.#getOutlines(outlineVerticalEdges);
  17077. }
  17078. #getOutlines(outlineVerticalEdges) {
  17079. const edges = [];
  17080. const allEdges = new Set();
  17081. for (const edge of outlineVerticalEdges) {
  17082. const [x, y1, y2] = edge;
  17083. edges.push([x, y1, edge], [x, y2, edge]);
  17084. }
  17085. edges.sort((a, b) => a[1] - b[1] || a[0] - b[0]);
  17086. for (let i = 0, ii = edges.length; i < ii; i += 2) {
  17087. const edge1 = edges[i][2];
  17088. const edge2 = edges[i + 1][2];
  17089. edge1.push(edge2);
  17090. edge2.push(edge1);
  17091. allEdges.add(edge1);
  17092. allEdges.add(edge2);
  17093. }
  17094. const outlines = [];
  17095. let outline;
  17096. while (allEdges.size > 0) {
  17097. const edge = allEdges.values().next().value;
  17098. let [x, y1, y2, edge1, edge2] = edge;
  17099. allEdges.delete(edge);
  17100. let lastPointX = x;
  17101. let lastPointY = y1;
  17102. outline = [x, y2];
  17103. outlines.push(outline);
  17104. while (true) {
  17105. let e;
  17106. if (allEdges.has(edge1)) {
  17107. e = edge1;
  17108. } else if (allEdges.has(edge2)) {
  17109. e = edge2;
  17110. } else {
  17111. break;
  17112. }
  17113. allEdges.delete(e);
  17114. [x, y1, y2, edge1, edge2] = e;
  17115. if (lastPointX !== x) {
  17116. outline.push(lastPointX, lastPointY, x, lastPointY === y1 ? y1 : y2);
  17117. lastPointX = x;
  17118. }
  17119. lastPointY = lastPointY === y1 ? y2 : y1;
  17120. }
  17121. outline.push(lastPointX, lastPointY);
  17122. }
  17123. return new HighlightOutline(outlines, this.#box, this.#lastPoint);
  17124. }
  17125. #binarySearch(y) {
  17126. const array = this.#intervals;
  17127. let start = 0;
  17128. let end = array.length - 1;
  17129. while (start <= end) {
  17130. const middle = start + end >> 1;
  17131. const y1 = array[middle][0];
  17132. if (y1 === y) {
  17133. return middle;
  17134. }
  17135. if (y1 < y) {
  17136. start = middle + 1;
  17137. } else {
  17138. end = middle - 1;
  17139. }
  17140. }
  17141. return end + 1;
  17142. }
  17143. #insert([, y1, y2]) {
  17144. const index = this.#binarySearch(y1);
  17145. this.#intervals.splice(index, 0, [y1, y2]);
  17146. }
  17147. #remove([, y1, y2]) {
  17148. const index = this.#binarySearch(y1);
  17149. for (let i = index; i < this.#intervals.length; i++) {
  17150. const [start, end] = this.#intervals[i];
  17151. if (start !== y1) {
  17152. break;
  17153. }
  17154. if (start === y1 && end === y2) {
  17155. this.#intervals.splice(i, 1);
  17156. return;
  17157. }
  17158. }
  17159. for (let i = index - 1; i >= 0; i--) {
  17160. const [start, end] = this.#intervals[i];
  17161. if (start !== y1) {
  17162. break;
  17163. }
  17164. if (start === y1 && end === y2) {
  17165. this.#intervals.splice(i, 1);
  17166. return;
  17167. }
  17168. }
  17169. }
  17170. #breakEdge(edge) {
  17171. const [x, y1, y2] = edge;
  17172. const results = [[x, y1, y2]];
  17173. const index = this.#binarySearch(y2);
  17174. for (let i = 0; i < index; i++) {
  17175. const [start, end] = this.#intervals[i];
  17176. for (let j = 0, jj = results.length; j < jj; j++) {
  17177. const [, y3, y4] = results[j];
  17178. if (end <= y3 || y4 <= start) {
  17179. continue;
  17180. }
  17181. if (y3 >= start) {
  17182. if (y4 > end) {
  17183. results[j][1] = end;
  17184. } else {
  17185. if (jj === 1) {
  17186. return [];
  17187. }
  17188. results.splice(j, 1);
  17189. j--;
  17190. jj--;
  17191. }
  17192. continue;
  17193. }
  17194. results[j][2] = start;
  17195. if (y4 > end) {
  17196. results.push([x, end, y4]);
  17197. }
  17198. }
  17199. }
  17200. return results;
  17201. }
  17202. }
  17203. class HighlightOutline extends Outline {
  17204. #box;
  17205. #outlines;
  17206. constructor(outlines, box, lastPoint) {
  17207. super();
  17208. this.#outlines = outlines;
  17209. this.#box = box;
  17210. this.lastPoint = lastPoint;
  17211. }
  17212. toSVGPath() {
  17213. const buffer = [];
  17214. for (const polygon of this.#outlines) {
  17215. let [prevX, prevY] = polygon;
  17216. buffer.push(`M${prevX} ${prevY}`);
  17217. for (let i = 2; i < polygon.length; i += 2) {
  17218. const x = polygon[i];
  17219. const y = polygon[i + 1];
  17220. if (x === prevX) {
  17221. buffer.push(`V${y}`);
  17222. prevY = y;
  17223. } else if (y === prevY) {
  17224. buffer.push(`H${x}`);
  17225. prevX = x;
  17226. }
  17227. }
  17228. buffer.push("Z");
  17229. }
  17230. return buffer.join(" ");
  17231. }
  17232. serialize([blX, blY, trX, trY], _rotation) {
  17233. const outlines = [];
  17234. const width = trX - blX;
  17235. const height = trY - blY;
  17236. for (const outline of this.#outlines) {
  17237. const points = new Array(outline.length);
  17238. for (let i = 0; i < outline.length; i += 2) {
  17239. points[i] = blX + outline[i] * width;
  17240. points[i + 1] = trY - outline[i + 1] * height;
  17241. }
  17242. outlines.push(points);
  17243. }
  17244. return outlines;
  17245. }
  17246. get box() {
  17247. return this.#box;
  17248. }
  17249. get classNamesForOutlining() {
  17250. return ["highlightOutline"];
  17251. }
  17252. }
  17253. class FreeHighlightOutliner extends FreeDrawOutliner {
  17254. newFreeDrawOutline(outline, points, box, scaleFactor, innerMargin, isLTR) {
  17255. return new FreeHighlightOutline(outline, points, box, scaleFactor, innerMargin, isLTR);
  17256. }
  17257. }
  17258. class FreeHighlightOutline extends FreeDrawOutline {
  17259. newOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin = 0) {
  17260. return new FreeHighlightOutliner(point, box, scaleFactor, thickness, isLTR, innerMargin);
  17261. }
  17262. }
  17263. ;// ./src/display/editor/color_picker.js
  17264. class ColorPicker {
  17265. #button = null;
  17266. #buttonSwatch = null;
  17267. #defaultColor;
  17268. #dropdown = null;
  17269. #dropdownWasFromKeyboard = false;
  17270. #isMainColorPicker = false;
  17271. #editor = null;
  17272. #eventBus;
  17273. #openDropdownAC = null;
  17274. #uiManager = null;
  17275. #type;
  17276. static #l10nColor = null;
  17277. static get _keyboardManager() {
  17278. 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]]));
  17279. }
  17280. constructor({
  17281. editor = null,
  17282. uiManager = null
  17283. }) {
  17284. if (editor) {
  17285. this.#isMainColorPicker = false;
  17286. this.#type = AnnotationEditorParamsType.HIGHLIGHT_COLOR;
  17287. this.#editor = editor;
  17288. } else {
  17289. this.#isMainColorPicker = true;
  17290. this.#type = AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR;
  17291. }
  17292. this.#uiManager = editor?._uiManager || uiManager;
  17293. this.#eventBus = this.#uiManager._eventBus;
  17294. this.#defaultColor = editor?.color || this.#uiManager?.highlightColors.values().next().value || "#FFFF98";
  17295. ColorPicker.#l10nColor ||= Object.freeze({
  17296. blue: "pdfjs-editor-colorpicker-blue",
  17297. green: "pdfjs-editor-colorpicker-green",
  17298. pink: "pdfjs-editor-colorpicker-pink",
  17299. red: "pdfjs-editor-colorpicker-red",
  17300. yellow: "pdfjs-editor-colorpicker-yellow"
  17301. });
  17302. }
  17303. renderButton() {
  17304. const button = this.#button = document.createElement("button");
  17305. button.className = "colorPicker";
  17306. button.tabIndex = "0";
  17307. button.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-button");
  17308. button.setAttribute("aria-haspopup", true);
  17309. const signal = this.#uiManager._signal;
  17310. button.addEventListener("click", this.#openDropdown.bind(this), {
  17311. signal
  17312. });
  17313. button.addEventListener("keydown", this.#keyDown.bind(this), {
  17314. signal
  17315. });
  17316. const swatch = this.#buttonSwatch = document.createElement("span");
  17317. swatch.className = "swatch";
  17318. swatch.setAttribute("aria-hidden", true);
  17319. swatch.style.backgroundColor = this.#defaultColor;
  17320. button.append(swatch);
  17321. return button;
  17322. }
  17323. renderMainDropdown() {
  17324. const dropdown = this.#dropdown = this.#getDropdownRoot();
  17325. dropdown.setAttribute("aria-orientation", "horizontal");
  17326. dropdown.setAttribute("aria-labelledby", "highlightColorPickerLabel");
  17327. return dropdown;
  17328. }
  17329. #getDropdownRoot() {
  17330. const div = document.createElement("div");
  17331. const signal = this.#uiManager._signal;
  17332. div.addEventListener("contextmenu", noContextMenu, {
  17333. signal
  17334. });
  17335. div.className = "dropdown";
  17336. div.role = "listbox";
  17337. div.setAttribute("aria-multiselectable", false);
  17338. div.setAttribute("aria-orientation", "vertical");
  17339. div.setAttribute("data-l10n-id", "pdfjs-editor-colorpicker-dropdown");
  17340. for (const [name, color] of this.#uiManager.highlightColors) {
  17341. const button = document.createElement("button");
  17342. button.tabIndex = "0";
  17343. button.role = "option";
  17344. button.setAttribute("data-color", color);
  17345. button.title = name;
  17346. button.setAttribute("data-l10n-id", ColorPicker.#l10nColor[name]);
  17347. const swatch = document.createElement("span");
  17348. button.append(swatch);
  17349. swatch.className = "swatch";
  17350. swatch.style.backgroundColor = color;
  17351. button.setAttribute("aria-selected", color === this.#defaultColor);
  17352. button.addEventListener("click", this.#colorSelect.bind(this, color), {
  17353. signal
  17354. });
  17355. div.append(button);
  17356. }
  17357. div.addEventListener("keydown", this.#keyDown.bind(this), {
  17358. signal
  17359. });
  17360. return div;
  17361. }
  17362. #colorSelect(color, event) {
  17363. event.stopPropagation();
  17364. this.#eventBus.dispatch("switchannotationeditorparams", {
  17365. source: this,
  17366. type: this.#type,
  17367. value: color
  17368. });
  17369. }
  17370. _colorSelectFromKeyboard(event) {
  17371. if (event.target === this.#button) {
  17372. this.#openDropdown(event);
  17373. return;
  17374. }
  17375. const color = event.target.getAttribute("data-color");
  17376. if (!color) {
  17377. return;
  17378. }
  17379. this.#colorSelect(color, event);
  17380. }
  17381. _moveToNext(event) {
  17382. if (!this.#isDropdownVisible) {
  17383. this.#openDropdown(event);
  17384. return;
  17385. }
  17386. if (event.target === this.#button) {
  17387. this.#dropdown.firstChild?.focus();
  17388. return;
  17389. }
  17390. event.target.nextSibling?.focus();
  17391. }
  17392. _moveToPrevious(event) {
  17393. if (event.target === this.#dropdown?.firstChild || event.target === this.#button) {
  17394. if (this.#isDropdownVisible) {
  17395. this._hideDropdownFromKeyboard();
  17396. }
  17397. return;
  17398. }
  17399. if (!this.#isDropdownVisible) {
  17400. this.#openDropdown(event);
  17401. }
  17402. event.target.previousSibling?.focus();
  17403. }
  17404. _moveToBeginning(event) {
  17405. if (!this.#isDropdownVisible) {
  17406. this.#openDropdown(event);
  17407. return;
  17408. }
  17409. this.#dropdown.firstChild?.focus();
  17410. }
  17411. _moveToEnd(event) {
  17412. if (!this.#isDropdownVisible) {
  17413. this.#openDropdown(event);
  17414. return;
  17415. }
  17416. this.#dropdown.lastChild?.focus();
  17417. }
  17418. #keyDown(event) {
  17419. ColorPicker._keyboardManager.exec(this, event);
  17420. }
  17421. #openDropdown(event) {
  17422. if (this.#isDropdownVisible) {
  17423. this.hideDropdown();
  17424. return;
  17425. }
  17426. this.#dropdownWasFromKeyboard = event.detail === 0;
  17427. if (!this.#openDropdownAC) {
  17428. this.#openDropdownAC = new AbortController();
  17429. window.addEventListener("pointerdown", this.#pointerDown.bind(this), {
  17430. signal: this.#uiManager.combinedSignal(this.#openDropdownAC)
  17431. });
  17432. }
  17433. if (this.#dropdown) {
  17434. this.#dropdown.classList.remove("hidden");
  17435. return;
  17436. }
  17437. const root = this.#dropdown = this.#getDropdownRoot();
  17438. this.#button.append(root);
  17439. }
  17440. #pointerDown(event) {
  17441. if (this.#dropdown?.contains(event.target)) {
  17442. return;
  17443. }
  17444. this.hideDropdown();
  17445. }
  17446. hideDropdown() {
  17447. this.#dropdown?.classList.add("hidden");
  17448. this.#openDropdownAC?.abort();
  17449. this.#openDropdownAC = null;
  17450. }
  17451. get #isDropdownVisible() {
  17452. return this.#dropdown && !this.#dropdown.classList.contains("hidden");
  17453. }
  17454. _hideDropdownFromKeyboard() {
  17455. if (this.#isMainColorPicker) {
  17456. return;
  17457. }
  17458. if (!this.#isDropdownVisible) {
  17459. this.#editor?.unselect();
  17460. return;
  17461. }
  17462. this.hideDropdown();
  17463. this.#button.focus({
  17464. preventScroll: true,
  17465. focusVisible: this.#dropdownWasFromKeyboard
  17466. });
  17467. }
  17468. updateColor(color) {
  17469. if (this.#buttonSwatch) {
  17470. this.#buttonSwatch.style.backgroundColor = color;
  17471. }
  17472. if (!this.#dropdown) {
  17473. return;
  17474. }
  17475. const i = this.#uiManager.highlightColors.values();
  17476. for (const child of this.#dropdown.children) {
  17477. child.setAttribute("aria-selected", i.next().value === color);
  17478. }
  17479. }
  17480. destroy() {
  17481. this.#button?.remove();
  17482. this.#button = null;
  17483. this.#buttonSwatch = null;
  17484. this.#dropdown?.remove();
  17485. this.#dropdown = null;
  17486. }
  17487. }
  17488. ;// ./src/display/editor/highlight.js
  17489. class HighlightEditor extends AnnotationEditor {
  17490. #anchorNode = null;
  17491. #anchorOffset = 0;
  17492. #boxes;
  17493. #clipPathId = null;
  17494. #colorPicker = null;
  17495. #focusOutlines = null;
  17496. #focusNode = null;
  17497. #focusOffset = 0;
  17498. #highlightDiv = null;
  17499. #highlightOutlines = null;
  17500. #id = null;
  17501. #isFreeHighlight = false;
  17502. #lastPoint = null;
  17503. #opacity;
  17504. #outlineId = null;
  17505. #text = "";
  17506. #thickness;
  17507. #methodOfCreation = "";
  17508. static _defaultColor = null;
  17509. static _defaultOpacity = 1;
  17510. static _defaultThickness = 12;
  17511. static _type = "highlight";
  17512. static _editorType = AnnotationEditorType.HIGHLIGHT;
  17513. static _freeHighlightId = -1;
  17514. static _freeHighlight = null;
  17515. static _freeHighlightClipId = "";
  17516. static get _keyboardManager() {
  17517. const proto = HighlightEditor.prototype;
  17518. return shadow(this, "_keyboardManager", new KeyboardManager([[["ArrowLeft", "mac+ArrowLeft"], proto._moveCaret, {
  17519. args: [0]
  17520. }], [["ArrowRight", "mac+ArrowRight"], proto._moveCaret, {
  17521. args: [1]
  17522. }], [["ArrowUp", "mac+ArrowUp"], proto._moveCaret, {
  17523. args: [2]
  17524. }], [["ArrowDown", "mac+ArrowDown"], proto._moveCaret, {
  17525. args: [3]
  17526. }]]));
  17527. }
  17528. constructor(params) {
  17529. super({
  17530. ...params,
  17531. name: "highlightEditor"
  17532. });
  17533. this.color = params.color || HighlightEditor._defaultColor;
  17534. this.#thickness = params.thickness || HighlightEditor._defaultThickness;
  17535. this.#opacity = params.opacity || HighlightEditor._defaultOpacity;
  17536. this.#boxes = params.boxes || null;
  17537. this.#methodOfCreation = params.methodOfCreation || "";
  17538. this.#text = params.text || "";
  17539. this._isDraggable = false;
  17540. this.defaultL10nId = "pdfjs-editor-highlight-editor";
  17541. if (params.highlightId > -1) {
  17542. this.#isFreeHighlight = true;
  17543. this.#createFreeOutlines(params);
  17544. this.#addToDrawLayer();
  17545. } else if (this.#boxes) {
  17546. this.#anchorNode = params.anchorNode;
  17547. this.#anchorOffset = params.anchorOffset;
  17548. this.#focusNode = params.focusNode;
  17549. this.#focusOffset = params.focusOffset;
  17550. this.#createOutlines();
  17551. this.#addToDrawLayer();
  17552. this.rotate(this.rotation);
  17553. }
  17554. }
  17555. get telemetryInitialData() {
  17556. return {
  17557. action: "added",
  17558. type: this.#isFreeHighlight ? "free_highlight" : "highlight",
  17559. color: this._uiManager.highlightColorNames.get(this.color),
  17560. thickness: this.#thickness,
  17561. methodOfCreation: this.#methodOfCreation
  17562. };
  17563. }
  17564. get telemetryFinalData() {
  17565. return {
  17566. type: "highlight",
  17567. color: this._uiManager.highlightColorNames.get(this.color)
  17568. };
  17569. }
  17570. static computeTelemetryFinalData(data) {
  17571. return {
  17572. numberOfColors: data.get("color").size
  17573. };
  17574. }
  17575. #createOutlines() {
  17576. const outliner = new HighlightOutliner(this.#boxes, 0.001);
  17577. this.#highlightOutlines = outliner.getOutlines();
  17578. [this.x, this.y, this.width, this.height] = this.#highlightOutlines.box;
  17579. const outlinerForOutline = new HighlightOutliner(this.#boxes, 0.0025, 0.001, this._uiManager.direction === "ltr");
  17580. this.#focusOutlines = outlinerForOutline.getOutlines();
  17581. const {
  17582. lastPoint
  17583. } = this.#focusOutlines;
  17584. this.#lastPoint = [(lastPoint[0] - this.x) / this.width, (lastPoint[1] - this.y) / this.height];
  17585. }
  17586. #createFreeOutlines({
  17587. highlightOutlines,
  17588. highlightId,
  17589. clipPathId
  17590. }) {
  17591. this.#highlightOutlines = highlightOutlines;
  17592. const extraThickness = 1.5;
  17593. this.#focusOutlines = highlightOutlines.getNewOutline(this.#thickness / 2 + extraThickness, 0.0025);
  17594. if (highlightId >= 0) {
  17595. this.#id = highlightId;
  17596. this.#clipPathId = clipPathId;
  17597. this.parent.drawLayer.finalizeDraw(highlightId, {
  17598. bbox: highlightOutlines.box,
  17599. path: {
  17600. d: highlightOutlines.toSVGPath()
  17601. }
  17602. });
  17603. this.#outlineId = this.parent.drawLayer.drawOutline({
  17604. rootClass: {
  17605. highlightOutline: true,
  17606. free: true
  17607. },
  17608. bbox: this.#focusOutlines.box,
  17609. path: {
  17610. d: this.#focusOutlines.toSVGPath()
  17611. }
  17612. }, true);
  17613. } else if (this.parent) {
  17614. const angle = this.parent.viewport.rotation;
  17615. this.parent.drawLayer.updateProperties(this.#id, {
  17616. bbox: HighlightEditor.#rotateBbox(this.#highlightOutlines.box, (angle - this.rotation + 360) % 360),
  17617. path: {
  17618. d: highlightOutlines.toSVGPath()
  17619. }
  17620. });
  17621. this.parent.drawLayer.updateProperties(this.#outlineId, {
  17622. bbox: HighlightEditor.#rotateBbox(this.#focusOutlines.box, angle),
  17623. path: {
  17624. d: this.#focusOutlines.toSVGPath()
  17625. }
  17626. });
  17627. }
  17628. const [x, y, width, height] = highlightOutlines.box;
  17629. switch (this.rotation) {
  17630. case 0:
  17631. this.x = x;
  17632. this.y = y;
  17633. this.width = width;
  17634. this.height = height;
  17635. break;
  17636. case 90:
  17637. {
  17638. const [pageWidth, pageHeight] = this.parentDimensions;
  17639. this.x = y;
  17640. this.y = 1 - x;
  17641. this.width = width * pageHeight / pageWidth;
  17642. this.height = height * pageWidth / pageHeight;
  17643. break;
  17644. }
  17645. case 180:
  17646. this.x = 1 - x;
  17647. this.y = 1 - y;
  17648. this.width = width;
  17649. this.height = height;
  17650. break;
  17651. case 270:
  17652. {
  17653. const [pageWidth, pageHeight] = this.parentDimensions;
  17654. this.x = 1 - y;
  17655. this.y = x;
  17656. this.width = width * pageHeight / pageWidth;
  17657. this.height = height * pageWidth / pageHeight;
  17658. break;
  17659. }
  17660. }
  17661. const {
  17662. lastPoint
  17663. } = this.#focusOutlines;
  17664. this.#lastPoint = [(lastPoint[0] - x) / width, (lastPoint[1] - y) / height];
  17665. }
  17666. static initialize(l10n, uiManager) {
  17667. AnnotationEditor.initialize(l10n, uiManager);
  17668. HighlightEditor._defaultColor ||= uiManager.highlightColors?.values().next().value || "#fff066";
  17669. }
  17670. static updateDefaultParams(type, value) {
  17671. switch (type) {
  17672. case AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR:
  17673. HighlightEditor._defaultColor = value;
  17674. break;
  17675. case AnnotationEditorParamsType.HIGHLIGHT_THICKNESS:
  17676. HighlightEditor._defaultThickness = value;
  17677. break;
  17678. }
  17679. }
  17680. translateInPage(x, y) {}
  17681. get toolbarPosition() {
  17682. return this.#lastPoint;
  17683. }
  17684. updateParams(type, value) {
  17685. switch (type) {
  17686. case AnnotationEditorParamsType.HIGHLIGHT_COLOR:
  17687. this.#updateColor(value);
  17688. break;
  17689. case AnnotationEditorParamsType.HIGHLIGHT_THICKNESS:
  17690. this.#updateThickness(value);
  17691. break;
  17692. }
  17693. }
  17694. static get defaultPropertiesToUpdate() {
  17695. return [[AnnotationEditorParamsType.HIGHLIGHT_DEFAULT_COLOR, HighlightEditor._defaultColor], [AnnotationEditorParamsType.HIGHLIGHT_THICKNESS, HighlightEditor._defaultThickness]];
  17696. }
  17697. get propertiesToUpdate() {
  17698. return [[AnnotationEditorParamsType.HIGHLIGHT_COLOR, this.color || HighlightEditor._defaultColor], [AnnotationEditorParamsType.HIGHLIGHT_THICKNESS, this.#thickness || HighlightEditor._defaultThickness], [AnnotationEditorParamsType.HIGHLIGHT_FREE, this.#isFreeHighlight]];
  17699. }
  17700. #updateColor(color) {
  17701. const setColorAndOpacity = (col, opa) => {
  17702. this.color = col;
  17703. this.#opacity = opa;
  17704. this.parent?.drawLayer.updateProperties(this.#id, {
  17705. root: {
  17706. fill: col,
  17707. "fill-opacity": opa
  17708. }
  17709. });
  17710. this.#colorPicker?.updateColor(col);
  17711. };
  17712. const savedColor = this.color;
  17713. const savedOpacity = this.#opacity;
  17714. this.addCommands({
  17715. cmd: setColorAndOpacity.bind(this, color, HighlightEditor._defaultOpacity),
  17716. undo: setColorAndOpacity.bind(this, savedColor, savedOpacity),
  17717. post: this._uiManager.updateUI.bind(this._uiManager, this),
  17718. mustExec: true,
  17719. type: AnnotationEditorParamsType.HIGHLIGHT_COLOR,
  17720. overwriteIfSameType: true,
  17721. keepUndo: true
  17722. });
  17723. this._reportTelemetry({
  17724. action: "color_changed",
  17725. color: this._uiManager.highlightColorNames.get(color)
  17726. }, true);
  17727. }
  17728. #updateThickness(thickness) {
  17729. const savedThickness = this.#thickness;
  17730. const setThickness = th => {
  17731. this.#thickness = th;
  17732. this.#changeThickness(th);
  17733. };
  17734. this.addCommands({
  17735. cmd: setThickness.bind(this, thickness),
  17736. undo: setThickness.bind(this, savedThickness),
  17737. post: this._uiManager.updateUI.bind(this._uiManager, this),
  17738. mustExec: true,
  17739. type: AnnotationEditorParamsType.INK_THICKNESS,
  17740. overwriteIfSameType: true,
  17741. keepUndo: true
  17742. });
  17743. this._reportTelemetry({
  17744. action: "thickness_changed",
  17745. thickness
  17746. }, true);
  17747. }
  17748. async addEditToolbar() {
  17749. const toolbar = await super.addEditToolbar();
  17750. if (!toolbar) {
  17751. return null;
  17752. }
  17753. if (this._uiManager.highlightColors) {
  17754. this.#colorPicker = new ColorPicker({
  17755. editor: this
  17756. });
  17757. toolbar.addColorPicker(this.#colorPicker);
  17758. }
  17759. return toolbar;
  17760. }
  17761. disableEditing() {
  17762. super.disableEditing();
  17763. this.div.classList.toggle("disabled", true);
  17764. }
  17765. enableEditing() {
  17766. super.enableEditing();
  17767. this.div.classList.toggle("disabled", false);
  17768. }
  17769. fixAndSetPosition() {
  17770. return super.fixAndSetPosition(this.#getRotation());
  17771. }
  17772. getBaseTranslation() {
  17773. return [0, 0];
  17774. }
  17775. getRect(tx, ty) {
  17776. return super.getRect(tx, ty, this.#getRotation());
  17777. }
  17778. onceAdded(focus) {
  17779. if (!this.annotationElementId) {
  17780. this.parent.addUndoableEditor(this);
  17781. }
  17782. if (focus) {
  17783. this.div.focus();
  17784. }
  17785. }
  17786. remove() {
  17787. this.#cleanDrawLayer();
  17788. this._reportTelemetry({
  17789. action: "deleted"
  17790. });
  17791. super.remove();
  17792. }
  17793. rebuild() {
  17794. if (!this.parent) {
  17795. return;
  17796. }
  17797. super.rebuild();
  17798. if (this.div === null) {
  17799. return;
  17800. }
  17801. this.#addToDrawLayer();
  17802. if (!this.isAttachedToDOM) {
  17803. this.parent.add(this);
  17804. }
  17805. }
  17806. setParent(parent) {
  17807. let mustBeSelected = false;
  17808. if (this.parent && !parent) {
  17809. this.#cleanDrawLayer();
  17810. } else if (parent) {
  17811. this.#addToDrawLayer(parent);
  17812. mustBeSelected = !this.parent && this.div?.classList.contains("selectedEditor");
  17813. }
  17814. super.setParent(parent);
  17815. this.show(this._isVisible);
  17816. if (mustBeSelected) {
  17817. this.select();
  17818. }
  17819. }
  17820. #changeThickness(thickness) {
  17821. if (!this.#isFreeHighlight) {
  17822. return;
  17823. }
  17824. this.#createFreeOutlines({
  17825. highlightOutlines: this.#highlightOutlines.getNewOutline(thickness / 2)
  17826. });
  17827. this.fixAndSetPosition();
  17828. const [parentWidth, parentHeight] = this.parentDimensions;
  17829. this.setDims(this.width * parentWidth, this.height * parentHeight);
  17830. }
  17831. #cleanDrawLayer() {
  17832. if (this.#id === null || !this.parent) {
  17833. return;
  17834. }
  17835. this.parent.drawLayer.remove(this.#id);
  17836. this.#id = null;
  17837. this.parent.drawLayer.remove(this.#outlineId);
  17838. this.#outlineId = null;
  17839. }
  17840. #addToDrawLayer(parent = this.parent) {
  17841. if (this.#id !== null) {
  17842. return;
  17843. }
  17844. ({
  17845. id: this.#id,
  17846. clipPathId: this.#clipPathId
  17847. } = parent.drawLayer.draw({
  17848. bbox: this.#highlightOutlines.box,
  17849. root: {
  17850. viewBox: "0 0 1 1",
  17851. fill: this.color,
  17852. "fill-opacity": this.#opacity
  17853. },
  17854. rootClass: {
  17855. highlight: true,
  17856. free: this.#isFreeHighlight
  17857. },
  17858. path: {
  17859. d: this.#highlightOutlines.toSVGPath()
  17860. }
  17861. }, false, true));
  17862. this.#outlineId = parent.drawLayer.drawOutline({
  17863. rootClass: {
  17864. highlightOutline: true,
  17865. free: this.#isFreeHighlight
  17866. },
  17867. bbox: this.#focusOutlines.box,
  17868. path: {
  17869. d: this.#focusOutlines.toSVGPath()
  17870. }
  17871. }, this.#isFreeHighlight);
  17872. if (this.#highlightDiv) {
  17873. this.#highlightDiv.style.clipPath = this.#clipPathId;
  17874. }
  17875. }
  17876. static #rotateBbox([x, y, width, height], angle) {
  17877. switch (angle) {
  17878. case 90:
  17879. return [1 - y - height, x, height, width];
  17880. case 180:
  17881. return [1 - x - width, 1 - y - height, width, height];
  17882. case 270:
  17883. return [y, 1 - x - width, height, width];
  17884. }
  17885. return [x, y, width, height];
  17886. }
  17887. rotate(angle) {
  17888. const {
  17889. drawLayer
  17890. } = this.parent;
  17891. let box;
  17892. if (this.#isFreeHighlight) {
  17893. angle = (angle - this.rotation + 360) % 360;
  17894. box = HighlightEditor.#rotateBbox(this.#highlightOutlines.box, angle);
  17895. } else {
  17896. box = HighlightEditor.#rotateBbox([this.x, this.y, this.width, this.height], angle);
  17897. }
  17898. drawLayer.updateProperties(this.#id, {
  17899. bbox: box,
  17900. root: {
  17901. "data-main-rotation": angle
  17902. }
  17903. });
  17904. drawLayer.updateProperties(this.#outlineId, {
  17905. bbox: HighlightEditor.#rotateBbox(this.#focusOutlines.box, angle),
  17906. root: {
  17907. "data-main-rotation": angle
  17908. }
  17909. });
  17910. }
  17911. render() {
  17912. if (this.div) {
  17913. return this.div;
  17914. }
  17915. const div = super.render();
  17916. if (this.#text) {
  17917. div.setAttribute("aria-label", this.#text);
  17918. div.setAttribute("role", "mark");
  17919. }
  17920. if (this.#isFreeHighlight) {
  17921. div.classList.add("free");
  17922. } else {
  17923. this.div.addEventListener("keydown", this.#keydown.bind(this), {
  17924. signal: this._uiManager._signal
  17925. });
  17926. }
  17927. const highlightDiv = this.#highlightDiv = document.createElement("div");
  17928. div.append(highlightDiv);
  17929. highlightDiv.setAttribute("aria-hidden", "true");
  17930. highlightDiv.className = "internal";
  17931. highlightDiv.style.clipPath = this.#clipPathId;
  17932. const [parentWidth, parentHeight] = this.parentDimensions;
  17933. this.setDims(this.width * parentWidth, this.height * parentHeight);
  17934. bindEvents(this, this.#highlightDiv, ["pointerover", "pointerleave"]);
  17935. this.enableEditing();
  17936. return div;
  17937. }
  17938. pointerover() {
  17939. if (!this.isSelected) {
  17940. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  17941. rootClass: {
  17942. hovered: true
  17943. }
  17944. });
  17945. }
  17946. }
  17947. pointerleave() {
  17948. if (!this.isSelected) {
  17949. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  17950. rootClass: {
  17951. hovered: false
  17952. }
  17953. });
  17954. }
  17955. }
  17956. #keydown(event) {
  17957. HighlightEditor._keyboardManager.exec(this, event);
  17958. }
  17959. _moveCaret(direction) {
  17960. this.parent.unselect(this);
  17961. switch (direction) {
  17962. case 0:
  17963. case 2:
  17964. this.#setCaret(true);
  17965. break;
  17966. case 1:
  17967. case 3:
  17968. this.#setCaret(false);
  17969. break;
  17970. }
  17971. }
  17972. #setCaret(start) {
  17973. if (!this.#anchorNode) {
  17974. return;
  17975. }
  17976. const selection = window.getSelection();
  17977. if (start) {
  17978. selection.setPosition(this.#anchorNode, this.#anchorOffset);
  17979. } else {
  17980. selection.setPosition(this.#focusNode, this.#focusOffset);
  17981. }
  17982. }
  17983. select() {
  17984. super.select();
  17985. if (!this.#outlineId) {
  17986. return;
  17987. }
  17988. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  17989. rootClass: {
  17990. hovered: false,
  17991. selected: true
  17992. }
  17993. });
  17994. }
  17995. unselect() {
  17996. super.unselect();
  17997. if (!this.#outlineId) {
  17998. return;
  17999. }
  18000. this.parent?.drawLayer.updateProperties(this.#outlineId, {
  18001. rootClass: {
  18002. selected: false
  18003. }
  18004. });
  18005. if (!this.#isFreeHighlight) {
  18006. this.#setCaret(false);
  18007. }
  18008. }
  18009. get _mustFixPosition() {
  18010. return !this.#isFreeHighlight;
  18011. }
  18012. show(visible = this._isVisible) {
  18013. super.show(visible);
  18014. if (this.parent) {
  18015. this.parent.drawLayer.updateProperties(this.#id, {
  18016. rootClass: {
  18017. hidden: !visible
  18018. }
  18019. });
  18020. this.parent.drawLayer.updateProperties(this.#outlineId, {
  18021. rootClass: {
  18022. hidden: !visible
  18023. }
  18024. });
  18025. }
  18026. }
  18027. #getRotation() {
  18028. return this.#isFreeHighlight ? this.rotation : 0;
  18029. }
  18030. #serializeBoxes() {
  18031. if (this.#isFreeHighlight) {
  18032. return null;
  18033. }
  18034. const [pageWidth, pageHeight] = this.pageDimensions;
  18035. const [pageX, pageY] = this.pageTranslation;
  18036. const boxes = this.#boxes;
  18037. const quadPoints = new Float32Array(boxes.length * 8);
  18038. let i = 0;
  18039. for (const {
  18040. x,
  18041. y,
  18042. width,
  18043. height
  18044. } of boxes) {
  18045. const sx = x * pageWidth + pageX;
  18046. const sy = (1 - y) * pageHeight + pageY;
  18047. quadPoints[i] = quadPoints[i + 4] = sx;
  18048. quadPoints[i + 1] = quadPoints[i + 3] = sy;
  18049. quadPoints[i + 2] = quadPoints[i + 6] = sx + width * pageWidth;
  18050. quadPoints[i + 5] = quadPoints[i + 7] = sy - height * pageHeight;
  18051. i += 8;
  18052. }
  18053. return quadPoints;
  18054. }
  18055. #serializeOutlines(rect) {
  18056. return this.#highlightOutlines.serialize(rect, this.#getRotation());
  18057. }
  18058. static startHighlighting(parent, isLTR, {
  18059. target: textLayer,
  18060. x,
  18061. y
  18062. }) {
  18063. const {
  18064. x: layerX,
  18065. y: layerY,
  18066. width: parentWidth,
  18067. height: parentHeight
  18068. } = textLayer.getBoundingClientRect();
  18069. const ac = new AbortController();
  18070. const signal = parent.combinedSignal(ac);
  18071. const pointerUpCallback = e => {
  18072. ac.abort();
  18073. this.#endHighlight(parent, e);
  18074. };
  18075. window.addEventListener("blur", pointerUpCallback, {
  18076. signal
  18077. });
  18078. window.addEventListener("pointerup", pointerUpCallback, {
  18079. signal
  18080. });
  18081. window.addEventListener("pointerdown", stopEvent, {
  18082. capture: true,
  18083. passive: false,
  18084. signal
  18085. });
  18086. window.addEventListener("contextmenu", noContextMenu, {
  18087. signal
  18088. });
  18089. textLayer.addEventListener("pointermove", this.#highlightMove.bind(this, parent), {
  18090. signal
  18091. });
  18092. this._freeHighlight = new FreeHighlightOutliner({
  18093. x,
  18094. y
  18095. }, [layerX, layerY, parentWidth, parentHeight], parent.scale, this._defaultThickness / 2, isLTR, 0.001);
  18096. ({
  18097. id: this._freeHighlightId,
  18098. clipPathId: this._freeHighlightClipId
  18099. } = parent.drawLayer.draw({
  18100. bbox: [0, 0, 1, 1],
  18101. root: {
  18102. viewBox: "0 0 1 1",
  18103. fill: this._defaultColor,
  18104. "fill-opacity": this._defaultOpacity
  18105. },
  18106. rootClass: {
  18107. highlight: true,
  18108. free: true
  18109. },
  18110. path: {
  18111. d: this._freeHighlight.toSVGPath()
  18112. }
  18113. }, true, true));
  18114. }
  18115. static #highlightMove(parent, event) {
  18116. if (this._freeHighlight.add(event)) {
  18117. parent.drawLayer.updateProperties(this._freeHighlightId, {
  18118. path: {
  18119. d: this._freeHighlight.toSVGPath()
  18120. }
  18121. });
  18122. }
  18123. }
  18124. static #endHighlight(parent, event) {
  18125. if (!this._freeHighlight.isEmpty()) {
  18126. parent.createAndAddNewEditor(event, false, {
  18127. highlightId: this._freeHighlightId,
  18128. highlightOutlines: this._freeHighlight.getOutlines(),
  18129. clipPathId: this._freeHighlightClipId,
  18130. methodOfCreation: "main_toolbar"
  18131. });
  18132. } else {
  18133. parent.drawLayer.remove(this._freeHighlightId);
  18134. }
  18135. this._freeHighlightId = -1;
  18136. this._freeHighlight = null;
  18137. this._freeHighlightClipId = "";
  18138. }
  18139. static async deserialize(data, parent, uiManager) {
  18140. let initialData = null;
  18141. if (data instanceof HighlightAnnotationElement) {
  18142. const {
  18143. data: {
  18144. quadPoints,
  18145. rect,
  18146. rotation,
  18147. id,
  18148. color,
  18149. opacity,
  18150. popupRef
  18151. },
  18152. parent: {
  18153. page: {
  18154. pageNumber
  18155. }
  18156. }
  18157. } = data;
  18158. initialData = data = {
  18159. annotationType: AnnotationEditorType.HIGHLIGHT,
  18160. color: Array.from(color),
  18161. opacity,
  18162. quadPoints,
  18163. boxes: null,
  18164. pageIndex: pageNumber - 1,
  18165. rect: rect.slice(0),
  18166. rotation,
  18167. id,
  18168. deleted: false,
  18169. popupRef
  18170. };
  18171. } else if (data instanceof InkAnnotationElement) {
  18172. const {
  18173. data: {
  18174. inkLists,
  18175. rect,
  18176. rotation,
  18177. id,
  18178. color,
  18179. borderStyle: {
  18180. rawWidth: thickness
  18181. },
  18182. popupRef
  18183. },
  18184. parent: {
  18185. page: {
  18186. pageNumber
  18187. }
  18188. }
  18189. } = data;
  18190. initialData = data = {
  18191. annotationType: AnnotationEditorType.HIGHLIGHT,
  18192. color: Array.from(color),
  18193. thickness,
  18194. inkLists,
  18195. boxes: null,
  18196. pageIndex: pageNumber - 1,
  18197. rect: rect.slice(0),
  18198. rotation,
  18199. id,
  18200. deleted: false,
  18201. popupRef
  18202. };
  18203. }
  18204. const {
  18205. color,
  18206. quadPoints,
  18207. inkLists,
  18208. opacity
  18209. } = data;
  18210. const editor = await super.deserialize(data, parent, uiManager);
  18211. editor.color = Util.makeHexColor(...color);
  18212. editor.#opacity = opacity || 1;
  18213. if (inkLists) {
  18214. editor.#thickness = data.thickness;
  18215. }
  18216. editor.annotationElementId = data.id || null;
  18217. editor._initialData = initialData;
  18218. const [pageWidth, pageHeight] = editor.pageDimensions;
  18219. const [pageX, pageY] = editor.pageTranslation;
  18220. if (quadPoints) {
  18221. const boxes = editor.#boxes = [];
  18222. for (let i = 0; i < quadPoints.length; i += 8) {
  18223. boxes.push({
  18224. x: (quadPoints[i] - pageX) / pageWidth,
  18225. y: 1 - (quadPoints[i + 1] - pageY) / pageHeight,
  18226. width: (quadPoints[i + 2] - quadPoints[i]) / pageWidth,
  18227. height: (quadPoints[i + 1] - quadPoints[i + 5]) / pageHeight
  18228. });
  18229. }
  18230. editor.#createOutlines();
  18231. editor.#addToDrawLayer();
  18232. editor.rotate(editor.rotation);
  18233. } else if (inkLists) {
  18234. editor.#isFreeHighlight = true;
  18235. const points = inkLists[0];
  18236. const point = {
  18237. x: points[0] - pageX,
  18238. y: pageHeight - (points[1] - pageY)
  18239. };
  18240. const outliner = new FreeHighlightOutliner(point, [0, 0, pageWidth, pageHeight], 1, editor.#thickness / 2, true, 0.001);
  18241. for (let i = 0, ii = points.length; i < ii; i += 2) {
  18242. point.x = points[i] - pageX;
  18243. point.y = pageHeight - (points[i + 1] - pageY);
  18244. outliner.add(point);
  18245. }
  18246. const {
  18247. id,
  18248. clipPathId
  18249. } = parent.drawLayer.draw({
  18250. bbox: [0, 0, 1, 1],
  18251. root: {
  18252. viewBox: "0 0 1 1",
  18253. fill: editor.color,
  18254. "fill-opacity": editor._defaultOpacity
  18255. },
  18256. rootClass: {
  18257. highlight: true,
  18258. free: true
  18259. },
  18260. path: {
  18261. d: outliner.toSVGPath()
  18262. }
  18263. }, true, true);
  18264. editor.#createFreeOutlines({
  18265. highlightOutlines: outliner.getOutlines(),
  18266. highlightId: id,
  18267. clipPathId
  18268. });
  18269. editor.#addToDrawLayer();
  18270. editor.rotate(editor.parentRotation);
  18271. }
  18272. return editor;
  18273. }
  18274. serialize(isForCopying = false) {
  18275. if (this.isEmpty() || isForCopying) {
  18276. return null;
  18277. }
  18278. if (this.deleted) {
  18279. return this.serializeDeleted();
  18280. }
  18281. const rect = this.getRect(0, 0);
  18282. const color = AnnotationEditor._colorManager.convert(this.color);
  18283. const serialized = {
  18284. annotationType: AnnotationEditorType.HIGHLIGHT,
  18285. color,
  18286. opacity: this.#opacity,
  18287. thickness: this.#thickness,
  18288. quadPoints: this.#serializeBoxes(),
  18289. outlines: this.#serializeOutlines(rect),
  18290. pageIndex: this.pageIndex,
  18291. rect,
  18292. rotation: this.#getRotation(),
  18293. structTreeParentId: this._structTreeParentId
  18294. };
  18295. if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
  18296. return null;
  18297. }
  18298. serialized.id = this.annotationElementId;
  18299. return serialized;
  18300. }
  18301. #hasElementChanged(serialized) {
  18302. const {
  18303. color
  18304. } = this._initialData;
  18305. return serialized.color.some((c, i) => c !== color[i]);
  18306. }
  18307. renderAnnotationElement(annotation) {
  18308. annotation.updateEdited({
  18309. rect: this.getRect(0, 0)
  18310. });
  18311. return null;
  18312. }
  18313. static canCreateNewEmptyEditor() {
  18314. return false;
  18315. }
  18316. }
  18317. ;// ./src/display/editor/draw.js
  18318. class DrawingOptions {
  18319. #svgProperties = Object.create(null);
  18320. updateProperty(name, value) {
  18321. this[name] = value;
  18322. this.updateSVGProperty(name, value);
  18323. }
  18324. updateProperties(properties) {
  18325. if (!properties) {
  18326. return;
  18327. }
  18328. for (const [name, value] of Object.entries(properties)) {
  18329. if (!name.startsWith("_")) {
  18330. this.updateProperty(name, value);
  18331. }
  18332. }
  18333. }
  18334. updateSVGProperty(name, value) {
  18335. this.#svgProperties[name] = value;
  18336. }
  18337. toSVGProperties() {
  18338. const root = this.#svgProperties;
  18339. this.#svgProperties = Object.create(null);
  18340. return {
  18341. root
  18342. };
  18343. }
  18344. reset() {
  18345. this.#svgProperties = Object.create(null);
  18346. }
  18347. updateAll(options = this) {
  18348. this.updateProperties(options);
  18349. }
  18350. clone() {
  18351. unreachable("Not implemented");
  18352. }
  18353. }
  18354. class DrawingEditor extends AnnotationEditor {
  18355. #drawOutlines = null;
  18356. #mustBeCommitted;
  18357. _drawId = null;
  18358. static _currentDrawId = -1;
  18359. static _currentParent = null;
  18360. static #currentDraw = null;
  18361. static #currentDrawingAC = null;
  18362. static #currentDrawingOptions = null;
  18363. static #currentPointerId = NaN;
  18364. static #currentPointerType = null;
  18365. static #currentPointerIds = null;
  18366. static #currentMoveTimestamp = NaN;
  18367. static _INNER_MARGIN = 3;
  18368. constructor(params) {
  18369. super(params);
  18370. this.#mustBeCommitted = params.mustBeCommitted || false;
  18371. this._addOutlines(params);
  18372. }
  18373. _addOutlines(params) {
  18374. if (params.drawOutlines) {
  18375. this.#createDrawOutlines(params);
  18376. this.#addToDrawLayer();
  18377. }
  18378. }
  18379. #createDrawOutlines({
  18380. drawOutlines,
  18381. drawId,
  18382. drawingOptions
  18383. }) {
  18384. this.#drawOutlines = drawOutlines;
  18385. this._drawingOptions ||= drawingOptions;
  18386. if (drawId >= 0) {
  18387. this._drawId = drawId;
  18388. this.parent.drawLayer.finalizeDraw(drawId, drawOutlines.defaultProperties);
  18389. } else {
  18390. this._drawId = this.#createDrawing(drawOutlines, this.parent);
  18391. }
  18392. this.#updateBbox(drawOutlines.box);
  18393. }
  18394. #createDrawing(drawOutlines, parent) {
  18395. const {
  18396. id
  18397. } = parent.drawLayer.draw(DrawingEditor._mergeSVGProperties(this._drawingOptions.toSVGProperties(), drawOutlines.defaultSVGProperties), false, false);
  18398. return id;
  18399. }
  18400. static _mergeSVGProperties(p1, p2) {
  18401. const p1Keys = new Set(Object.keys(p1));
  18402. for (const [key, value] of Object.entries(p2)) {
  18403. if (p1Keys.has(key)) {
  18404. Object.assign(p1[key], value);
  18405. } else {
  18406. p1[key] = value;
  18407. }
  18408. }
  18409. return p1;
  18410. }
  18411. static getDefaultDrawingOptions(_options) {
  18412. unreachable("Not implemented");
  18413. }
  18414. static get typesMap() {
  18415. unreachable("Not implemented");
  18416. }
  18417. static get isDrawer() {
  18418. return true;
  18419. }
  18420. static get supportMultipleDrawings() {
  18421. return false;
  18422. }
  18423. static updateDefaultParams(type, value) {
  18424. const propertyName = this.typesMap.get(type);
  18425. if (propertyName) {
  18426. this._defaultDrawingOptions.updateProperty(propertyName, value);
  18427. }
  18428. if (this._currentParent) {
  18429. DrawingEditor.#currentDraw.updateProperty(propertyName, value);
  18430. this._currentParent.drawLayer.updateProperties(this._currentDrawId, this._defaultDrawingOptions.toSVGProperties());
  18431. }
  18432. }
  18433. updateParams(type, value) {
  18434. const propertyName = this.constructor.typesMap.get(type);
  18435. if (propertyName) {
  18436. this._updateProperty(type, propertyName, value);
  18437. }
  18438. }
  18439. static get defaultPropertiesToUpdate() {
  18440. const properties = [];
  18441. const options = this._defaultDrawingOptions;
  18442. for (const [type, name] of this.typesMap) {
  18443. properties.push([type, options[name]]);
  18444. }
  18445. return properties;
  18446. }
  18447. get propertiesToUpdate() {
  18448. const properties = [];
  18449. const {
  18450. _drawingOptions
  18451. } = this;
  18452. for (const [type, name] of this.constructor.typesMap) {
  18453. properties.push([type, _drawingOptions[name]]);
  18454. }
  18455. return properties;
  18456. }
  18457. _updateProperty(type, name, value) {
  18458. const options = this._drawingOptions;
  18459. const savedValue = options[name];
  18460. const setter = val => {
  18461. options.updateProperty(name, val);
  18462. const bbox = this.#drawOutlines.updateProperty(name, val);
  18463. if (bbox) {
  18464. this.#updateBbox(bbox);
  18465. }
  18466. this.parent?.drawLayer.updateProperties(this._drawId, options.toSVGProperties());
  18467. };
  18468. this.addCommands({
  18469. cmd: setter.bind(this, value),
  18470. undo: setter.bind(this, savedValue),
  18471. post: this._uiManager.updateUI.bind(this._uiManager, this),
  18472. mustExec: true,
  18473. type,
  18474. overwriteIfSameType: true,
  18475. keepUndo: true
  18476. });
  18477. }
  18478. _onResizing() {
  18479. this.parent?.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties(this.#drawOutlines.getPathResizingSVGProperties(this.#convertToDrawSpace()), {
  18480. bbox: this.#rotateBox()
  18481. }));
  18482. }
  18483. _onResized() {
  18484. this.parent?.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties(this.#drawOutlines.getPathResizedSVGProperties(this.#convertToDrawSpace()), {
  18485. bbox: this.#rotateBox()
  18486. }));
  18487. }
  18488. _onTranslating(_x, _y) {
  18489. this.parent?.drawLayer.updateProperties(this._drawId, {
  18490. bbox: this.#rotateBox()
  18491. });
  18492. }
  18493. _onTranslated() {
  18494. this.parent?.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties(this.#drawOutlines.getPathTranslatedSVGProperties(this.#convertToDrawSpace(), this.parentDimensions), {
  18495. bbox: this.#rotateBox()
  18496. }));
  18497. }
  18498. _onStartDragging() {
  18499. this.parent?.drawLayer.updateProperties(this._drawId, {
  18500. rootClass: {
  18501. moving: true
  18502. }
  18503. });
  18504. }
  18505. _onStopDragging() {
  18506. this.parent?.drawLayer.updateProperties(this._drawId, {
  18507. rootClass: {
  18508. moving: false
  18509. }
  18510. });
  18511. }
  18512. commit() {
  18513. super.commit();
  18514. this.disableEditMode();
  18515. this.disableEditing();
  18516. }
  18517. disableEditing() {
  18518. super.disableEditing();
  18519. this.div.classList.toggle("disabled", true);
  18520. }
  18521. enableEditing() {
  18522. super.enableEditing();
  18523. this.div.classList.toggle("disabled", false);
  18524. }
  18525. getBaseTranslation() {
  18526. return [0, 0];
  18527. }
  18528. get isResizable() {
  18529. return true;
  18530. }
  18531. onceAdded(focus) {
  18532. if (!this.annotationElementId) {
  18533. this.parent.addUndoableEditor(this);
  18534. }
  18535. this._isDraggable = true;
  18536. if (this.#mustBeCommitted) {
  18537. this.#mustBeCommitted = false;
  18538. this.commit();
  18539. this.parent.setSelected(this);
  18540. if (focus && this.isOnScreen) {
  18541. this.div.focus();
  18542. }
  18543. }
  18544. }
  18545. remove() {
  18546. this.#cleanDrawLayer();
  18547. super.remove();
  18548. }
  18549. rebuild() {
  18550. if (!this.parent) {
  18551. return;
  18552. }
  18553. super.rebuild();
  18554. if (this.div === null) {
  18555. return;
  18556. }
  18557. this.#addToDrawLayer();
  18558. this.#updateBbox(this.#drawOutlines.box);
  18559. if (!this.isAttachedToDOM) {
  18560. this.parent.add(this);
  18561. }
  18562. }
  18563. setParent(parent) {
  18564. let mustBeSelected = false;
  18565. if (this.parent && !parent) {
  18566. this._uiManager.removeShouldRescale(this);
  18567. this.#cleanDrawLayer();
  18568. } else if (parent) {
  18569. this._uiManager.addShouldRescale(this);
  18570. this.#addToDrawLayer(parent);
  18571. mustBeSelected = !this.parent && this.div?.classList.contains("selectedEditor");
  18572. }
  18573. super.setParent(parent);
  18574. if (mustBeSelected) {
  18575. this.select();
  18576. }
  18577. }
  18578. #cleanDrawLayer() {
  18579. if (this._drawId === null || !this.parent) {
  18580. return;
  18581. }
  18582. this.parent.drawLayer.remove(this._drawId);
  18583. this._drawId = null;
  18584. this._drawingOptions.reset();
  18585. }
  18586. #addToDrawLayer(parent = this.parent) {
  18587. if (this._drawId !== null && this.parent === parent) {
  18588. return;
  18589. }
  18590. if (this._drawId !== null) {
  18591. this.parent.drawLayer.updateParent(this._drawId, parent.drawLayer);
  18592. return;
  18593. }
  18594. this._drawingOptions.updateAll();
  18595. this._drawId = this.#createDrawing(this.#drawOutlines, parent);
  18596. }
  18597. #convertToParentSpace([x, y, width, height]) {
  18598. const {
  18599. parentDimensions: [pW, pH],
  18600. rotation
  18601. } = this;
  18602. switch (rotation) {
  18603. case 90:
  18604. return [y, 1 - x, width * (pH / pW), height * (pW / pH)];
  18605. case 180:
  18606. return [1 - x, 1 - y, width, height];
  18607. case 270:
  18608. return [1 - y, x, width * (pH / pW), height * (pW / pH)];
  18609. default:
  18610. return [x, y, width, height];
  18611. }
  18612. }
  18613. #convertToDrawSpace() {
  18614. const {
  18615. x,
  18616. y,
  18617. width,
  18618. height,
  18619. parentDimensions: [pW, pH],
  18620. rotation
  18621. } = this;
  18622. switch (rotation) {
  18623. case 90:
  18624. return [1 - y, x, width * (pW / pH), height * (pH / pW)];
  18625. case 180:
  18626. return [1 - x, 1 - y, width, height];
  18627. case 270:
  18628. return [y, 1 - x, width * (pW / pH), height * (pH / pW)];
  18629. default:
  18630. return [x, y, width, height];
  18631. }
  18632. }
  18633. #updateBbox(bbox) {
  18634. [this.x, this.y, this.width, this.height] = this.#convertToParentSpace(bbox);
  18635. if (this.div) {
  18636. this.fixAndSetPosition();
  18637. const [parentWidth, parentHeight] = this.parentDimensions;
  18638. this.setDims(this.width * parentWidth, this.height * parentHeight);
  18639. }
  18640. this._onResized();
  18641. }
  18642. #rotateBox() {
  18643. const {
  18644. x,
  18645. y,
  18646. width,
  18647. height,
  18648. rotation,
  18649. parentRotation,
  18650. parentDimensions: [pW, pH]
  18651. } = this;
  18652. switch ((rotation * 4 + parentRotation) / 90) {
  18653. case 1:
  18654. return [1 - y - height, x, height, width];
  18655. case 2:
  18656. return [1 - x - width, 1 - y - height, width, height];
  18657. case 3:
  18658. return [y, 1 - x - width, height, width];
  18659. case 4:
  18660. return [x, y - width * (pW / pH), height * (pH / pW), width * (pW / pH)];
  18661. case 5:
  18662. return [1 - y, x, width * (pW / pH), height * (pH / pW)];
  18663. case 6:
  18664. return [1 - x - height * (pH / pW), 1 - y, height * (pH / pW), width * (pW / pH)];
  18665. case 7:
  18666. return [y - width * (pW / pH), 1 - x - height * (pH / pW), width * (pW / pH), height * (pH / pW)];
  18667. case 8:
  18668. return [x - width, y - height, width, height];
  18669. case 9:
  18670. return [1 - y, x - width, height, width];
  18671. case 10:
  18672. return [1 - x, 1 - y, width, height];
  18673. case 11:
  18674. return [y - height, 1 - x, height, width];
  18675. case 12:
  18676. return [x - height * (pH / pW), y, height * (pH / pW), width * (pW / pH)];
  18677. case 13:
  18678. return [1 - y - width * (pW / pH), x - height * (pH / pW), width * (pW / pH), height * (pH / pW)];
  18679. case 14:
  18680. return [1 - x, 1 - y - width * (pW / pH), height * (pH / pW), width * (pW / pH)];
  18681. case 15:
  18682. return [y, 1 - x, width * (pW / pH), height * (pH / pW)];
  18683. default:
  18684. return [x, y, width, height];
  18685. }
  18686. }
  18687. rotate() {
  18688. if (!this.parent) {
  18689. return;
  18690. }
  18691. this.parent.drawLayer.updateProperties(this._drawId, DrawingEditor._mergeSVGProperties({
  18692. bbox: this.#rotateBox()
  18693. }, this.#drawOutlines.updateRotation((this.parentRotation - this.rotation + 360) % 360)));
  18694. }
  18695. onScaleChanging() {
  18696. if (!this.parent) {
  18697. return;
  18698. }
  18699. this.#updateBbox(this.#drawOutlines.updateParentDimensions(this.parentDimensions, this.parent.scale));
  18700. }
  18701. static onScaleChangingWhenDrawing() {}
  18702. render() {
  18703. if (this.div) {
  18704. return this.div;
  18705. }
  18706. let baseX, baseY;
  18707. if (this._isCopy) {
  18708. baseX = this.x;
  18709. baseY = this.y;
  18710. }
  18711. const div = super.render();
  18712. div.classList.add("draw");
  18713. const drawDiv = document.createElement("div");
  18714. div.append(drawDiv);
  18715. drawDiv.setAttribute("aria-hidden", "true");
  18716. drawDiv.className = "internal";
  18717. const [parentWidth, parentHeight] = this.parentDimensions;
  18718. this.setDims(this.width * parentWidth, this.height * parentHeight);
  18719. this._uiManager.addShouldRescale(this);
  18720. this.disableEditing();
  18721. if (this._isCopy) {
  18722. this._moveAfterPaste(baseX, baseY);
  18723. }
  18724. return div;
  18725. }
  18726. static createDrawerInstance(_x, _y, _parentWidth, _parentHeight, _rotation) {
  18727. unreachable("Not implemented");
  18728. }
  18729. static startDrawing(parent, uiManager, _isLTR, event) {
  18730. const {
  18731. target,
  18732. offsetX: x,
  18733. offsetY: y,
  18734. pointerId,
  18735. pointerType
  18736. } = event;
  18737. if (DrawingEditor.#currentPointerType && DrawingEditor.#currentPointerType !== pointerType) {
  18738. return;
  18739. }
  18740. const {
  18741. viewport: {
  18742. rotation
  18743. }
  18744. } = parent;
  18745. const {
  18746. width: parentWidth,
  18747. height: parentHeight
  18748. } = target.getBoundingClientRect();
  18749. const ac = DrawingEditor.#currentDrawingAC = new AbortController();
  18750. const signal = parent.combinedSignal(ac);
  18751. DrawingEditor.#currentPointerId ||= pointerId;
  18752. DrawingEditor.#currentPointerType ??= pointerType;
  18753. window.addEventListener("pointerup", e => {
  18754. if (DrawingEditor.#currentPointerId === e.pointerId) {
  18755. this._endDraw(e);
  18756. } else {
  18757. DrawingEditor.#currentPointerIds?.delete(e.pointerId);
  18758. }
  18759. }, {
  18760. signal
  18761. });
  18762. window.addEventListener("pointercancel", e => {
  18763. if (DrawingEditor.#currentPointerId === e.pointerId) {
  18764. this._currentParent.endDrawingSession();
  18765. } else {
  18766. DrawingEditor.#currentPointerIds?.delete(e.pointerId);
  18767. }
  18768. }, {
  18769. signal
  18770. });
  18771. window.addEventListener("pointerdown", e => {
  18772. if (DrawingEditor.#currentPointerType !== e.pointerType) {
  18773. return;
  18774. }
  18775. (DrawingEditor.#currentPointerIds ||= new Set()).add(e.pointerId);
  18776. if (DrawingEditor.#currentDraw.isCancellable()) {
  18777. DrawingEditor.#currentDraw.removeLastElement();
  18778. if (DrawingEditor.#currentDraw.isEmpty()) {
  18779. this._currentParent.endDrawingSession(true);
  18780. } else {
  18781. this._endDraw(null);
  18782. }
  18783. }
  18784. }, {
  18785. capture: true,
  18786. passive: false,
  18787. signal
  18788. });
  18789. window.addEventListener("contextmenu", noContextMenu, {
  18790. signal
  18791. });
  18792. target.addEventListener("pointermove", this._drawMove.bind(this), {
  18793. signal
  18794. });
  18795. target.addEventListener("touchmove", e => {
  18796. if (e.timeStamp === DrawingEditor.#currentMoveTimestamp) {
  18797. stopEvent(e);
  18798. }
  18799. }, {
  18800. signal
  18801. });
  18802. parent.toggleDrawing();
  18803. uiManager._editorUndoBar?.hide();
  18804. if (DrawingEditor.#currentDraw) {
  18805. parent.drawLayer.updateProperties(this._currentDrawId, DrawingEditor.#currentDraw.startNew(x, y, parentWidth, parentHeight, rotation));
  18806. return;
  18807. }
  18808. uiManager.updateUIForDefaultProperties(this);
  18809. DrawingEditor.#currentDraw = this.createDrawerInstance(x, y, parentWidth, parentHeight, rotation);
  18810. DrawingEditor.#currentDrawingOptions = this.getDefaultDrawingOptions();
  18811. this._currentParent = parent;
  18812. ({
  18813. id: this._currentDrawId
  18814. } = parent.drawLayer.draw(this._mergeSVGProperties(DrawingEditor.#currentDrawingOptions.toSVGProperties(), DrawingEditor.#currentDraw.defaultSVGProperties), true, false));
  18815. }
  18816. static _drawMove(event) {
  18817. DrawingEditor.#currentMoveTimestamp = -1;
  18818. if (!DrawingEditor.#currentDraw) {
  18819. return;
  18820. }
  18821. const {
  18822. offsetX,
  18823. offsetY,
  18824. pointerId
  18825. } = event;
  18826. if (DrawingEditor.#currentPointerId !== pointerId) {
  18827. return;
  18828. }
  18829. if (DrawingEditor.#currentPointerIds?.size >= 1) {
  18830. this._endDraw(event);
  18831. return;
  18832. }
  18833. this._currentParent.drawLayer.updateProperties(this._currentDrawId, DrawingEditor.#currentDraw.add(offsetX, offsetY));
  18834. DrawingEditor.#currentMoveTimestamp = event.timeStamp;
  18835. stopEvent(event);
  18836. }
  18837. static _cleanup(all) {
  18838. if (all) {
  18839. this._currentDrawId = -1;
  18840. this._currentParent = null;
  18841. DrawingEditor.#currentDraw = null;
  18842. DrawingEditor.#currentDrawingOptions = null;
  18843. DrawingEditor.#currentPointerType = null;
  18844. DrawingEditor.#currentMoveTimestamp = NaN;
  18845. }
  18846. if (DrawingEditor.#currentDrawingAC) {
  18847. DrawingEditor.#currentDrawingAC.abort();
  18848. DrawingEditor.#currentDrawingAC = null;
  18849. DrawingEditor.#currentPointerId = NaN;
  18850. DrawingEditor.#currentPointerIds = null;
  18851. }
  18852. }
  18853. static _endDraw(event) {
  18854. const parent = this._currentParent;
  18855. if (!parent) {
  18856. return;
  18857. }
  18858. parent.toggleDrawing(true);
  18859. this._cleanup(false);
  18860. if (event?.target === parent.div) {
  18861. parent.drawLayer.updateProperties(this._currentDrawId, DrawingEditor.#currentDraw.end(event.offsetX, event.offsetY));
  18862. }
  18863. if (this.supportMultipleDrawings) {
  18864. const draw = DrawingEditor.#currentDraw;
  18865. const drawId = this._currentDrawId;
  18866. const lastElement = draw.getLastElement();
  18867. parent.addCommands({
  18868. cmd: () => {
  18869. parent.drawLayer.updateProperties(drawId, draw.setLastElement(lastElement));
  18870. },
  18871. undo: () => {
  18872. parent.drawLayer.updateProperties(drawId, draw.removeLastElement());
  18873. },
  18874. mustExec: false,
  18875. type: AnnotationEditorParamsType.DRAW_STEP
  18876. });
  18877. return;
  18878. }
  18879. this.endDrawing(false);
  18880. }
  18881. static endDrawing(isAborted) {
  18882. const parent = this._currentParent;
  18883. if (!parent) {
  18884. return null;
  18885. }
  18886. parent.toggleDrawing(true);
  18887. parent.cleanUndoStack(AnnotationEditorParamsType.DRAW_STEP);
  18888. if (!DrawingEditor.#currentDraw.isEmpty()) {
  18889. const {
  18890. pageDimensions: [pageWidth, pageHeight],
  18891. scale
  18892. } = parent;
  18893. const editor = parent.createAndAddNewEditor({
  18894. offsetX: 0,
  18895. offsetY: 0
  18896. }, false, {
  18897. drawId: this._currentDrawId,
  18898. drawOutlines: DrawingEditor.#currentDraw.getOutlines(pageWidth * scale, pageHeight * scale, scale, this._INNER_MARGIN),
  18899. drawingOptions: DrawingEditor.#currentDrawingOptions,
  18900. mustBeCommitted: !isAborted
  18901. });
  18902. this._cleanup(true);
  18903. return editor;
  18904. }
  18905. parent.drawLayer.remove(this._currentDrawId);
  18906. this._cleanup(true);
  18907. return null;
  18908. }
  18909. createDrawingOptions(_data) {}
  18910. static deserializeDraw(_pageX, _pageY, _pageWidth, _pageHeight, _innerWidth, _data) {
  18911. unreachable("Not implemented");
  18912. }
  18913. static async deserialize(data, parent, uiManager) {
  18914. const {
  18915. rawDims: {
  18916. pageWidth,
  18917. pageHeight,
  18918. pageX,
  18919. pageY
  18920. }
  18921. } = parent.viewport;
  18922. const drawOutlines = this.deserializeDraw(pageX, pageY, pageWidth, pageHeight, this._INNER_MARGIN, data);
  18923. const editor = await super.deserialize(data, parent, uiManager);
  18924. editor.createDrawingOptions(data);
  18925. editor.#createDrawOutlines({
  18926. drawOutlines
  18927. });
  18928. editor.#addToDrawLayer();
  18929. editor.onScaleChanging();
  18930. editor.rotate();
  18931. return editor;
  18932. }
  18933. serializeDraw(isForCopying) {
  18934. const [pageX, pageY] = this.pageTranslation;
  18935. const [pageWidth, pageHeight] = this.pageDimensions;
  18936. return this.#drawOutlines.serialize([pageX, pageY, pageWidth, pageHeight], isForCopying);
  18937. }
  18938. renderAnnotationElement(annotation) {
  18939. annotation.updateEdited({
  18940. rect: this.getRect(0, 0)
  18941. });
  18942. return null;
  18943. }
  18944. static canCreateNewEmptyEditor() {
  18945. return false;
  18946. }
  18947. }
  18948. ;// ./src/display/editor/drawers/inkdraw.js
  18949. class InkDrawOutliner {
  18950. #last = new Float64Array(6);
  18951. #line;
  18952. #lines;
  18953. #rotation;
  18954. #thickness;
  18955. #points;
  18956. #lastSVGPath = "";
  18957. #lastIndex = 0;
  18958. #outlines = new InkDrawOutline();
  18959. #parentWidth;
  18960. #parentHeight;
  18961. constructor(x, y, parentWidth, parentHeight, rotation, thickness) {
  18962. this.#parentWidth = parentWidth;
  18963. this.#parentHeight = parentHeight;
  18964. this.#rotation = rotation;
  18965. this.#thickness = thickness;
  18966. [x, y] = this.#normalizePoint(x, y);
  18967. const line = this.#line = [NaN, NaN, NaN, NaN, x, y];
  18968. this.#points = [x, y];
  18969. this.#lines = [{
  18970. line,
  18971. points: this.#points
  18972. }];
  18973. this.#last.set(line, 0);
  18974. }
  18975. updateProperty(name, value) {
  18976. if (name === "stroke-width") {
  18977. this.#thickness = value;
  18978. }
  18979. }
  18980. #normalizePoint(x, y) {
  18981. return Outline._normalizePoint(x, y, this.#parentWidth, this.#parentHeight, this.#rotation);
  18982. }
  18983. isEmpty() {
  18984. return !this.#lines || this.#lines.length === 0;
  18985. }
  18986. isCancellable() {
  18987. return this.#points.length <= 10;
  18988. }
  18989. add(x, y) {
  18990. [x, y] = this.#normalizePoint(x, y);
  18991. const [x1, y1, x2, y2] = this.#last.subarray(2, 6);
  18992. const diffX = x - x2;
  18993. const diffY = y - y2;
  18994. const d = Math.hypot(this.#parentWidth * diffX, this.#parentHeight * diffY);
  18995. if (d <= 2) {
  18996. return null;
  18997. }
  18998. this.#points.push(x, y);
  18999. if (isNaN(x1)) {
  19000. this.#last.set([x2, y2, x, y], 2);
  19001. this.#line.push(NaN, NaN, NaN, NaN, x, y);
  19002. return {
  19003. path: {
  19004. d: this.toSVGPath()
  19005. }
  19006. };
  19007. }
  19008. if (isNaN(this.#last[0])) {
  19009. this.#line.splice(6, 6);
  19010. }
  19011. this.#last.set([x1, y1, x2, y2, x, y], 0);
  19012. this.#line.push(...Outline.createBezierPoints(x1, y1, x2, y2, x, y));
  19013. return {
  19014. path: {
  19015. d: this.toSVGPath()
  19016. }
  19017. };
  19018. }
  19019. end(x, y) {
  19020. const change = this.add(x, y);
  19021. if (change) {
  19022. return change;
  19023. }
  19024. if (this.#points.length === 2) {
  19025. return {
  19026. path: {
  19027. d: this.toSVGPath()
  19028. }
  19029. };
  19030. }
  19031. return null;
  19032. }
  19033. startNew(x, y, parentWidth, parentHeight, rotation) {
  19034. this.#parentWidth = parentWidth;
  19035. this.#parentHeight = parentHeight;
  19036. this.#rotation = rotation;
  19037. [x, y] = this.#normalizePoint(x, y);
  19038. const line = this.#line = [NaN, NaN, NaN, NaN, x, y];
  19039. this.#points = [x, y];
  19040. const last = this.#lines.at(-1);
  19041. if (last) {
  19042. last.line = new Float32Array(last.line);
  19043. last.points = new Float32Array(last.points);
  19044. }
  19045. this.#lines.push({
  19046. line,
  19047. points: this.#points
  19048. });
  19049. this.#last.set(line, 0);
  19050. this.#lastIndex = 0;
  19051. this.toSVGPath();
  19052. return null;
  19053. }
  19054. getLastElement() {
  19055. return this.#lines.at(-1);
  19056. }
  19057. setLastElement(element) {
  19058. if (!this.#lines) {
  19059. return this.#outlines.setLastElement(element);
  19060. }
  19061. this.#lines.push(element);
  19062. this.#line = element.line;
  19063. this.#points = element.points;
  19064. this.#lastIndex = 0;
  19065. return {
  19066. path: {
  19067. d: this.toSVGPath()
  19068. }
  19069. };
  19070. }
  19071. removeLastElement() {
  19072. if (!this.#lines) {
  19073. return this.#outlines.removeLastElement();
  19074. }
  19075. this.#lines.pop();
  19076. this.#lastSVGPath = "";
  19077. for (let i = 0, ii = this.#lines.length; i < ii; i++) {
  19078. const {
  19079. line,
  19080. points
  19081. } = this.#lines[i];
  19082. this.#line = line;
  19083. this.#points = points;
  19084. this.#lastIndex = 0;
  19085. this.toSVGPath();
  19086. }
  19087. return {
  19088. path: {
  19089. d: this.#lastSVGPath
  19090. }
  19091. };
  19092. }
  19093. toSVGPath() {
  19094. const firstX = Outline.svgRound(this.#line[4]);
  19095. const firstY = Outline.svgRound(this.#line[5]);
  19096. if (this.#points.length === 2) {
  19097. this.#lastSVGPath = `${this.#lastSVGPath} M ${firstX} ${firstY} Z`;
  19098. return this.#lastSVGPath;
  19099. }
  19100. if (this.#points.length <= 6) {
  19101. const i = this.#lastSVGPath.lastIndexOf("M");
  19102. this.#lastSVGPath = `${this.#lastSVGPath.slice(0, i)} M ${firstX} ${firstY}`;
  19103. this.#lastIndex = 6;
  19104. }
  19105. if (this.#points.length === 4) {
  19106. const secondX = Outline.svgRound(this.#line[10]);
  19107. const secondY = Outline.svgRound(this.#line[11]);
  19108. this.#lastSVGPath = `${this.#lastSVGPath} L ${secondX} ${secondY}`;
  19109. this.#lastIndex = 12;
  19110. return this.#lastSVGPath;
  19111. }
  19112. const buffer = [];
  19113. if (this.#lastIndex === 0) {
  19114. buffer.push(`M ${firstX} ${firstY}`);
  19115. this.#lastIndex = 6;
  19116. }
  19117. for (let i = this.#lastIndex, ii = this.#line.length; i < ii; i += 6) {
  19118. const [c1x, c1y, c2x, c2y, x, y] = this.#line.slice(i, i + 6).map(Outline.svgRound);
  19119. buffer.push(`C${c1x} ${c1y} ${c2x} ${c2y} ${x} ${y}`);
  19120. }
  19121. this.#lastSVGPath += buffer.join(" ");
  19122. this.#lastIndex = this.#line.length;
  19123. return this.#lastSVGPath;
  19124. }
  19125. getOutlines(parentWidth, parentHeight, scale, innerMargin) {
  19126. const last = this.#lines.at(-1);
  19127. last.line = new Float32Array(last.line);
  19128. last.points = new Float32Array(last.points);
  19129. this.#outlines.build(this.#lines, parentWidth, parentHeight, scale, this.#rotation, this.#thickness, innerMargin);
  19130. this.#last = null;
  19131. this.#line = null;
  19132. this.#lines = null;
  19133. this.#lastSVGPath = null;
  19134. return this.#outlines;
  19135. }
  19136. get defaultSVGProperties() {
  19137. return {
  19138. root: {
  19139. viewBox: "0 0 10000 10000"
  19140. },
  19141. rootClass: {
  19142. draw: true
  19143. },
  19144. bbox: [0, 0, 1, 1]
  19145. };
  19146. }
  19147. }
  19148. class InkDrawOutline extends Outline {
  19149. #bbox;
  19150. #currentRotation = 0;
  19151. #innerMargin;
  19152. #lines;
  19153. #parentWidth;
  19154. #parentHeight;
  19155. #parentScale;
  19156. #rotation;
  19157. #thickness;
  19158. build(lines, parentWidth, parentHeight, parentScale, rotation, thickness, innerMargin) {
  19159. this.#parentWidth = parentWidth;
  19160. this.#parentHeight = parentHeight;
  19161. this.#parentScale = parentScale;
  19162. this.#rotation = rotation;
  19163. this.#thickness = thickness;
  19164. this.#innerMargin = innerMargin ?? 0;
  19165. this.#lines = lines;
  19166. this.#computeBbox();
  19167. }
  19168. get thickness() {
  19169. return this.#thickness;
  19170. }
  19171. setLastElement(element) {
  19172. this.#lines.push(element);
  19173. return {
  19174. path: {
  19175. d: this.toSVGPath()
  19176. }
  19177. };
  19178. }
  19179. removeLastElement() {
  19180. this.#lines.pop();
  19181. return {
  19182. path: {
  19183. d: this.toSVGPath()
  19184. }
  19185. };
  19186. }
  19187. toSVGPath() {
  19188. const buffer = [];
  19189. for (const {
  19190. line
  19191. } of this.#lines) {
  19192. buffer.push(`M${Outline.svgRound(line[4])} ${Outline.svgRound(line[5])}`);
  19193. if (line.length === 6) {
  19194. buffer.push("Z");
  19195. continue;
  19196. }
  19197. if (line.length === 12 && isNaN(line[6])) {
  19198. buffer.push(`L${Outline.svgRound(line[10])} ${Outline.svgRound(line[11])}`);
  19199. continue;
  19200. }
  19201. for (let i = 6, ii = line.length; i < ii; i += 6) {
  19202. const [c1x, c1y, c2x, c2y, x, y] = line.subarray(i, i + 6).map(Outline.svgRound);
  19203. buffer.push(`C${c1x} ${c1y} ${c2x} ${c2y} ${x} ${y}`);
  19204. }
  19205. }
  19206. return buffer.join("");
  19207. }
  19208. serialize([pageX, pageY, pageWidth, pageHeight], isForCopying) {
  19209. const serializedLines = [];
  19210. const serializedPoints = [];
  19211. const [x, y, width, height] = this.#getBBoxWithNoMargin();
  19212. let tx, ty, sx, sy, x1, y1, x2, y2, rescaleFn;
  19213. switch (this.#rotation) {
  19214. case 0:
  19215. rescaleFn = Outline._rescale;
  19216. tx = pageX;
  19217. ty = pageY + pageHeight;
  19218. sx = pageWidth;
  19219. sy = -pageHeight;
  19220. x1 = pageX + x * pageWidth;
  19221. y1 = pageY + (1 - y - height) * pageHeight;
  19222. x2 = pageX + (x + width) * pageWidth;
  19223. y2 = pageY + (1 - y) * pageHeight;
  19224. break;
  19225. case 90:
  19226. rescaleFn = Outline._rescaleAndSwap;
  19227. tx = pageX;
  19228. ty = pageY;
  19229. sx = pageWidth;
  19230. sy = pageHeight;
  19231. x1 = pageX + y * pageWidth;
  19232. y1 = pageY + x * pageHeight;
  19233. x2 = pageX + (y + height) * pageWidth;
  19234. y2 = pageY + (x + width) * pageHeight;
  19235. break;
  19236. case 180:
  19237. rescaleFn = Outline._rescale;
  19238. tx = pageX + pageWidth;
  19239. ty = pageY;
  19240. sx = -pageWidth;
  19241. sy = pageHeight;
  19242. x1 = pageX + (1 - x - width) * pageWidth;
  19243. y1 = pageY + y * pageHeight;
  19244. x2 = pageX + (1 - x) * pageWidth;
  19245. y2 = pageY + (y + height) * pageHeight;
  19246. break;
  19247. case 270:
  19248. rescaleFn = Outline._rescaleAndSwap;
  19249. tx = pageX + pageWidth;
  19250. ty = pageY + pageHeight;
  19251. sx = -pageWidth;
  19252. sy = -pageHeight;
  19253. x1 = pageX + (1 - y - height) * pageWidth;
  19254. y1 = pageY + (1 - x - width) * pageHeight;
  19255. x2 = pageX + (1 - y) * pageWidth;
  19256. y2 = pageY + (1 - x) * pageHeight;
  19257. break;
  19258. }
  19259. for (const {
  19260. line,
  19261. points
  19262. } of this.#lines) {
  19263. serializedLines.push(rescaleFn(line, tx, ty, sx, sy, isForCopying ? new Array(line.length) : null));
  19264. serializedPoints.push(rescaleFn(points, tx, ty, sx, sy, isForCopying ? new Array(points.length) : null));
  19265. }
  19266. return {
  19267. lines: serializedLines,
  19268. points: serializedPoints,
  19269. rect: [x1, y1, x2, y2]
  19270. };
  19271. }
  19272. static deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, {
  19273. paths: {
  19274. lines,
  19275. points
  19276. },
  19277. rotation,
  19278. thickness
  19279. }) {
  19280. const newLines = [];
  19281. let tx, ty, sx, sy, rescaleFn;
  19282. switch (rotation) {
  19283. case 0:
  19284. rescaleFn = Outline._rescale;
  19285. tx = -pageX / pageWidth;
  19286. ty = pageY / pageHeight + 1;
  19287. sx = 1 / pageWidth;
  19288. sy = -1 / pageHeight;
  19289. break;
  19290. case 90:
  19291. rescaleFn = Outline._rescaleAndSwap;
  19292. tx = -pageY / pageHeight;
  19293. ty = -pageX / pageWidth;
  19294. sx = 1 / pageHeight;
  19295. sy = 1 / pageWidth;
  19296. break;
  19297. case 180:
  19298. rescaleFn = Outline._rescale;
  19299. tx = pageX / pageWidth + 1;
  19300. ty = -pageY / pageHeight;
  19301. sx = -1 / pageWidth;
  19302. sy = 1 / pageHeight;
  19303. break;
  19304. case 270:
  19305. rescaleFn = Outline._rescaleAndSwap;
  19306. tx = pageY / pageHeight + 1;
  19307. ty = pageX / pageWidth + 1;
  19308. sx = -1 / pageHeight;
  19309. sy = -1 / pageWidth;
  19310. break;
  19311. }
  19312. if (!lines) {
  19313. lines = [];
  19314. for (const point of points) {
  19315. const len = point.length;
  19316. if (len === 2) {
  19317. lines.push(new Float32Array([NaN, NaN, NaN, NaN, point[0], point[1]]));
  19318. continue;
  19319. }
  19320. if (len === 4) {
  19321. lines.push(new Float32Array([NaN, NaN, NaN, NaN, point[0], point[1], NaN, NaN, NaN, NaN, point[2], point[3]]));
  19322. continue;
  19323. }
  19324. const line = new Float32Array(3 * (len - 2));
  19325. lines.push(line);
  19326. let [x1, y1, x2, y2] = point.subarray(0, 4);
  19327. line.set([NaN, NaN, NaN, NaN, x1, y1], 0);
  19328. for (let i = 4; i < len; i += 2) {
  19329. const x = point[i];
  19330. const y = point[i + 1];
  19331. line.set(Outline.createBezierPoints(x1, y1, x2, y2, x, y), (i - 2) * 3);
  19332. [x1, y1, x2, y2] = [x2, y2, x, y];
  19333. }
  19334. }
  19335. }
  19336. for (let i = 0, ii = lines.length; i < ii; i++) {
  19337. newLines.push({
  19338. line: rescaleFn(lines[i].map(x => x ?? NaN), tx, ty, sx, sy),
  19339. points: rescaleFn(points[i].map(x => x ?? NaN), tx, ty, sx, sy)
  19340. });
  19341. }
  19342. const outlines = new this.prototype.constructor();
  19343. outlines.build(newLines, pageWidth, pageHeight, 1, rotation, thickness, innerMargin);
  19344. return outlines;
  19345. }
  19346. #getMarginComponents(thickness = this.#thickness) {
  19347. const margin = this.#innerMargin + thickness / 2 * this.#parentScale;
  19348. return this.#rotation % 180 === 0 ? [margin / this.#parentWidth, margin / this.#parentHeight] : [margin / this.#parentHeight, margin / this.#parentWidth];
  19349. }
  19350. #getBBoxWithNoMargin() {
  19351. const [x, y, width, height] = this.#bbox;
  19352. const [marginX, marginY] = this.#getMarginComponents(0);
  19353. return [x + marginX, y + marginY, width - 2 * marginX, height - 2 * marginY];
  19354. }
  19355. #computeBbox() {
  19356. const bbox = this.#bbox = new Float32Array([Infinity, Infinity, -Infinity, -Infinity]);
  19357. for (const {
  19358. line
  19359. } of this.#lines) {
  19360. if (line.length <= 12) {
  19361. for (let i = 4, ii = line.length; i < ii; i += 6) {
  19362. Util.pointBoundingBox(line[i], line[i + 1], bbox);
  19363. }
  19364. continue;
  19365. }
  19366. let lastX = line[4],
  19367. lastY = line[5];
  19368. for (let i = 6, ii = line.length; i < ii; i += 6) {
  19369. const [c1x, c1y, c2x, c2y, x, y] = line.subarray(i, i + 6);
  19370. Util.bezierBoundingBox(lastX, lastY, c1x, c1y, c2x, c2y, x, y, bbox);
  19371. lastX = x;
  19372. lastY = y;
  19373. }
  19374. }
  19375. const [marginX, marginY] = this.#getMarginComponents();
  19376. bbox[0] = MathClamp(bbox[0] - marginX, 0, 1);
  19377. bbox[1] = MathClamp(bbox[1] - marginY, 0, 1);
  19378. bbox[2] = MathClamp(bbox[2] + marginX, 0, 1);
  19379. bbox[3] = MathClamp(bbox[3] + marginY, 0, 1);
  19380. bbox[2] -= bbox[0];
  19381. bbox[3] -= bbox[1];
  19382. }
  19383. get box() {
  19384. return this.#bbox;
  19385. }
  19386. updateProperty(name, value) {
  19387. if (name === "stroke-width") {
  19388. return this.#updateThickness(value);
  19389. }
  19390. return null;
  19391. }
  19392. #updateThickness(thickness) {
  19393. const [oldMarginX, oldMarginY] = this.#getMarginComponents();
  19394. this.#thickness = thickness;
  19395. const [newMarginX, newMarginY] = this.#getMarginComponents();
  19396. const [diffMarginX, diffMarginY] = [newMarginX - oldMarginX, newMarginY - oldMarginY];
  19397. const bbox = this.#bbox;
  19398. bbox[0] -= diffMarginX;
  19399. bbox[1] -= diffMarginY;
  19400. bbox[2] += 2 * diffMarginX;
  19401. bbox[3] += 2 * diffMarginY;
  19402. return bbox;
  19403. }
  19404. updateParentDimensions([width, height], scale) {
  19405. const [oldMarginX, oldMarginY] = this.#getMarginComponents();
  19406. this.#parentWidth = width;
  19407. this.#parentHeight = height;
  19408. this.#parentScale = scale;
  19409. const [newMarginX, newMarginY] = this.#getMarginComponents();
  19410. const diffMarginX = newMarginX - oldMarginX;
  19411. const diffMarginY = newMarginY - oldMarginY;
  19412. const bbox = this.#bbox;
  19413. bbox[0] -= diffMarginX;
  19414. bbox[1] -= diffMarginY;
  19415. bbox[2] += 2 * diffMarginX;
  19416. bbox[3] += 2 * diffMarginY;
  19417. return bbox;
  19418. }
  19419. updateRotation(rotation) {
  19420. this.#currentRotation = rotation;
  19421. return {
  19422. path: {
  19423. transform: this.rotationTransform
  19424. }
  19425. };
  19426. }
  19427. get viewBox() {
  19428. return this.#bbox.map(Outline.svgRound).join(" ");
  19429. }
  19430. get defaultProperties() {
  19431. const [x, y] = this.#bbox;
  19432. return {
  19433. root: {
  19434. viewBox: this.viewBox
  19435. },
  19436. path: {
  19437. "transform-origin": `${Outline.svgRound(x)} ${Outline.svgRound(y)}`
  19438. }
  19439. };
  19440. }
  19441. get rotationTransform() {
  19442. const [,, width, height] = this.#bbox;
  19443. let a = 0,
  19444. b = 0,
  19445. c = 0,
  19446. d = 0,
  19447. e = 0,
  19448. f = 0;
  19449. switch (this.#currentRotation) {
  19450. case 90:
  19451. b = height / width;
  19452. c = -width / height;
  19453. e = width;
  19454. break;
  19455. case 180:
  19456. a = -1;
  19457. d = -1;
  19458. e = width;
  19459. f = height;
  19460. break;
  19461. case 270:
  19462. b = -height / width;
  19463. c = width / height;
  19464. f = height;
  19465. break;
  19466. default:
  19467. return "";
  19468. }
  19469. return `matrix(${a} ${b} ${c} ${d} ${Outline.svgRound(e)} ${Outline.svgRound(f)})`;
  19470. }
  19471. getPathResizingSVGProperties([newX, newY, newWidth, newHeight]) {
  19472. const [marginX, marginY] = this.#getMarginComponents();
  19473. const [x, y, width, height] = this.#bbox;
  19474. if (Math.abs(width - marginX) <= Outline.PRECISION || Math.abs(height - marginY) <= Outline.PRECISION) {
  19475. const tx = newX + newWidth / 2 - (x + width / 2);
  19476. const ty = newY + newHeight / 2 - (y + height / 2);
  19477. return {
  19478. path: {
  19479. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`,
  19480. transform: `${this.rotationTransform} translate(${tx} ${ty})`
  19481. }
  19482. };
  19483. }
  19484. const s1x = (newWidth - 2 * marginX) / (width - 2 * marginX);
  19485. const s1y = (newHeight - 2 * marginY) / (height - 2 * marginY);
  19486. const s2x = width / newWidth;
  19487. const s2y = height / newHeight;
  19488. return {
  19489. path: {
  19490. "transform-origin": `${Outline.svgRound(x)} ${Outline.svgRound(y)}`,
  19491. transform: `${this.rotationTransform} scale(${s2x} ${s2y}) ` + `translate(${Outline.svgRound(marginX)} ${Outline.svgRound(marginY)}) scale(${s1x} ${s1y}) ` + `translate(${Outline.svgRound(-marginX)} ${Outline.svgRound(-marginY)})`
  19492. }
  19493. };
  19494. }
  19495. getPathResizedSVGProperties([newX, newY, newWidth, newHeight]) {
  19496. const [marginX, marginY] = this.#getMarginComponents();
  19497. const bbox = this.#bbox;
  19498. const [x, y, width, height] = bbox;
  19499. bbox[0] = newX;
  19500. bbox[1] = newY;
  19501. bbox[2] = newWidth;
  19502. bbox[3] = newHeight;
  19503. if (Math.abs(width - marginX) <= Outline.PRECISION || Math.abs(height - marginY) <= Outline.PRECISION) {
  19504. const tx = newX + newWidth / 2 - (x + width / 2);
  19505. const ty = newY + newHeight / 2 - (y + height / 2);
  19506. for (const {
  19507. line,
  19508. points
  19509. } of this.#lines) {
  19510. Outline._translate(line, tx, ty, line);
  19511. Outline._translate(points, tx, ty, points);
  19512. }
  19513. return {
  19514. root: {
  19515. viewBox: this.viewBox
  19516. },
  19517. path: {
  19518. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`,
  19519. transform: this.rotationTransform || null,
  19520. d: this.toSVGPath()
  19521. }
  19522. };
  19523. }
  19524. const s1x = (newWidth - 2 * marginX) / (width - 2 * marginX);
  19525. const s1y = (newHeight - 2 * marginY) / (height - 2 * marginY);
  19526. const tx = -s1x * (x + marginX) + newX + marginX;
  19527. const ty = -s1y * (y + marginY) + newY + marginY;
  19528. if (s1x !== 1 || s1y !== 1 || tx !== 0 || ty !== 0) {
  19529. for (const {
  19530. line,
  19531. points
  19532. } of this.#lines) {
  19533. Outline._rescale(line, tx, ty, s1x, s1y, line);
  19534. Outline._rescale(points, tx, ty, s1x, s1y, points);
  19535. }
  19536. }
  19537. return {
  19538. root: {
  19539. viewBox: this.viewBox
  19540. },
  19541. path: {
  19542. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`,
  19543. transform: this.rotationTransform || null,
  19544. d: this.toSVGPath()
  19545. }
  19546. };
  19547. }
  19548. getPathTranslatedSVGProperties([newX, newY], parentDimensions) {
  19549. const [newParentWidth, newParentHeight] = parentDimensions;
  19550. const bbox = this.#bbox;
  19551. const tx = newX - bbox[0];
  19552. const ty = newY - bbox[1];
  19553. if (this.#parentWidth === newParentWidth && this.#parentHeight === newParentHeight) {
  19554. for (const {
  19555. line,
  19556. points
  19557. } of this.#lines) {
  19558. Outline._translate(line, tx, ty, line);
  19559. Outline._translate(points, tx, ty, points);
  19560. }
  19561. } else {
  19562. const sx = this.#parentWidth / newParentWidth;
  19563. const sy = this.#parentHeight / newParentHeight;
  19564. this.#parentWidth = newParentWidth;
  19565. this.#parentHeight = newParentHeight;
  19566. for (const {
  19567. line,
  19568. points
  19569. } of this.#lines) {
  19570. Outline._rescale(line, tx, ty, sx, sy, line);
  19571. Outline._rescale(points, tx, ty, sx, sy, points);
  19572. }
  19573. bbox[2] *= sx;
  19574. bbox[3] *= sy;
  19575. }
  19576. bbox[0] = newX;
  19577. bbox[1] = newY;
  19578. return {
  19579. root: {
  19580. viewBox: this.viewBox
  19581. },
  19582. path: {
  19583. d: this.toSVGPath(),
  19584. "transform-origin": `${Outline.svgRound(newX)} ${Outline.svgRound(newY)}`
  19585. }
  19586. };
  19587. }
  19588. get defaultSVGProperties() {
  19589. const bbox = this.#bbox;
  19590. return {
  19591. root: {
  19592. viewBox: this.viewBox
  19593. },
  19594. rootClass: {
  19595. draw: true
  19596. },
  19597. path: {
  19598. d: this.toSVGPath(),
  19599. "transform-origin": `${Outline.svgRound(bbox[0])} ${Outline.svgRound(bbox[1])}`,
  19600. transform: this.rotationTransform || null
  19601. },
  19602. bbox
  19603. };
  19604. }
  19605. }
  19606. ;// ./src/display/editor/ink.js
  19607. class InkDrawingOptions extends DrawingOptions {
  19608. constructor(viewerParameters) {
  19609. super();
  19610. this._viewParameters = viewerParameters;
  19611. super.updateProperties({
  19612. fill: "none",
  19613. stroke: AnnotationEditor._defaultLineColor,
  19614. "stroke-opacity": 1,
  19615. "stroke-width": 1,
  19616. "stroke-linecap": "round",
  19617. "stroke-linejoin": "round",
  19618. "stroke-miterlimit": 10
  19619. });
  19620. }
  19621. updateSVGProperty(name, value) {
  19622. if (name === "stroke-width") {
  19623. value ??= this["stroke-width"];
  19624. value *= this._viewParameters.realScale;
  19625. }
  19626. super.updateSVGProperty(name, value);
  19627. }
  19628. clone() {
  19629. const clone = new InkDrawingOptions(this._viewParameters);
  19630. clone.updateAll(this);
  19631. return clone;
  19632. }
  19633. }
  19634. class InkEditor extends DrawingEditor {
  19635. static _type = "ink";
  19636. static _editorType = AnnotationEditorType.INK;
  19637. static _defaultDrawingOptions = null;
  19638. constructor(params) {
  19639. super({
  19640. ...params,
  19641. name: "inkEditor"
  19642. });
  19643. this._willKeepAspectRatio = true;
  19644. this.defaultL10nId = "pdfjs-editor-ink-editor";
  19645. }
  19646. static initialize(l10n, uiManager) {
  19647. AnnotationEditor.initialize(l10n, uiManager);
  19648. this._defaultDrawingOptions = new InkDrawingOptions(uiManager.viewParameters);
  19649. }
  19650. static getDefaultDrawingOptions(options) {
  19651. const clone = this._defaultDrawingOptions.clone();
  19652. clone.updateProperties(options);
  19653. return clone;
  19654. }
  19655. static get supportMultipleDrawings() {
  19656. return true;
  19657. }
  19658. static get typesMap() {
  19659. return shadow(this, "typesMap", new Map([[AnnotationEditorParamsType.INK_THICKNESS, "stroke-width"], [AnnotationEditorParamsType.INK_COLOR, "stroke"], [AnnotationEditorParamsType.INK_OPACITY, "stroke-opacity"]]));
  19660. }
  19661. static createDrawerInstance(x, y, parentWidth, parentHeight, rotation) {
  19662. return new InkDrawOutliner(x, y, parentWidth, parentHeight, rotation, this._defaultDrawingOptions["stroke-width"]);
  19663. }
  19664. static deserializeDraw(pageX, pageY, pageWidth, pageHeight, innerMargin, data) {
  19665. return InkDrawOutline.deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, data);
  19666. }
  19667. static async deserialize(data, parent, uiManager) {
  19668. let initialData = null;
  19669. if (data instanceof InkAnnotationElement) {
  19670. const {
  19671. data: {
  19672. inkLists,
  19673. rect,
  19674. rotation,
  19675. id,
  19676. color,
  19677. opacity,
  19678. borderStyle: {
  19679. rawWidth: thickness
  19680. },
  19681. popupRef
  19682. },
  19683. parent: {
  19684. page: {
  19685. pageNumber
  19686. }
  19687. }
  19688. } = data;
  19689. initialData = data = {
  19690. annotationType: AnnotationEditorType.INK,
  19691. color: Array.from(color),
  19692. thickness,
  19693. opacity,
  19694. paths: {
  19695. points: inkLists
  19696. },
  19697. boxes: null,
  19698. pageIndex: pageNumber - 1,
  19699. rect: rect.slice(0),
  19700. rotation,
  19701. id,
  19702. deleted: false,
  19703. popupRef
  19704. };
  19705. }
  19706. const editor = await super.deserialize(data, parent, uiManager);
  19707. editor.annotationElementId = data.id || null;
  19708. editor._initialData = initialData;
  19709. return editor;
  19710. }
  19711. onScaleChanging() {
  19712. if (!this.parent) {
  19713. return;
  19714. }
  19715. super.onScaleChanging();
  19716. const {
  19717. _drawId,
  19718. _drawingOptions,
  19719. parent
  19720. } = this;
  19721. _drawingOptions.updateSVGProperty("stroke-width");
  19722. parent.drawLayer.updateProperties(_drawId, _drawingOptions.toSVGProperties());
  19723. }
  19724. static onScaleChangingWhenDrawing() {
  19725. const parent = this._currentParent;
  19726. if (!parent) {
  19727. return;
  19728. }
  19729. super.onScaleChangingWhenDrawing();
  19730. this._defaultDrawingOptions.updateSVGProperty("stroke-width");
  19731. parent.drawLayer.updateProperties(this._currentDrawId, this._defaultDrawingOptions.toSVGProperties());
  19732. }
  19733. createDrawingOptions({
  19734. color,
  19735. thickness,
  19736. opacity
  19737. }) {
  19738. this._drawingOptions = InkEditor.getDefaultDrawingOptions({
  19739. stroke: Util.makeHexColor(...color),
  19740. "stroke-width": thickness,
  19741. "stroke-opacity": opacity
  19742. });
  19743. }
  19744. serialize(isForCopying = false) {
  19745. if (this.isEmpty()) {
  19746. return null;
  19747. }
  19748. if (this.deleted) {
  19749. return this.serializeDeleted();
  19750. }
  19751. const {
  19752. lines,
  19753. points,
  19754. rect
  19755. } = this.serializeDraw(isForCopying);
  19756. const {
  19757. _drawingOptions: {
  19758. stroke,
  19759. "stroke-opacity": opacity,
  19760. "stroke-width": thickness
  19761. }
  19762. } = this;
  19763. const serialized = {
  19764. annotationType: AnnotationEditorType.INK,
  19765. color: AnnotationEditor._colorManager.convert(stroke),
  19766. opacity,
  19767. thickness,
  19768. paths: {
  19769. lines,
  19770. points
  19771. },
  19772. pageIndex: this.pageIndex,
  19773. rect,
  19774. rotation: this.rotation,
  19775. structTreeParentId: this._structTreeParentId
  19776. };
  19777. if (isForCopying) {
  19778. serialized.isCopy = true;
  19779. return serialized;
  19780. }
  19781. if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
  19782. return null;
  19783. }
  19784. serialized.id = this.annotationElementId;
  19785. return serialized;
  19786. }
  19787. #hasElementChanged(serialized) {
  19788. const {
  19789. color,
  19790. thickness,
  19791. opacity,
  19792. pageIndex
  19793. } = this._initialData;
  19794. return this._hasBeenMoved || this._hasBeenResized || serialized.color.some((c, i) => c !== color[i]) || serialized.thickness !== thickness || serialized.opacity !== opacity || serialized.pageIndex !== pageIndex;
  19795. }
  19796. renderAnnotationElement(annotation) {
  19797. const {
  19798. points,
  19799. rect
  19800. } = this.serializeDraw(false);
  19801. annotation.updateEdited({
  19802. rect,
  19803. thickness: this._drawingOptions["stroke-width"],
  19804. points
  19805. });
  19806. return null;
  19807. }
  19808. }
  19809. ;// ./src/display/editor/drawers/contour.js
  19810. class ContourDrawOutline extends InkDrawOutline {
  19811. toSVGPath() {
  19812. let path = super.toSVGPath();
  19813. if (!path.endsWith("Z")) {
  19814. path += "Z";
  19815. }
  19816. return path;
  19817. }
  19818. }
  19819. ;// ./src/display/editor/drawers/signaturedraw.js
  19820. const BASE_HEADER_LENGTH = 8;
  19821. const POINTS_PROPERTIES_NUMBER = 3;
  19822. class SignatureExtractor {
  19823. static #PARAMETERS = {
  19824. maxDim: 512,
  19825. sigmaSFactor: 0.02,
  19826. sigmaR: 25,
  19827. kernelSize: 16
  19828. };
  19829. static #neighborIndexToId(i0, j0, i, j) {
  19830. i -= i0;
  19831. j -= j0;
  19832. if (i === 0) {
  19833. return j > 0 ? 0 : 4;
  19834. }
  19835. if (i === 1) {
  19836. return j + 6;
  19837. }
  19838. return 2 - j;
  19839. }
  19840. static #neighborIdToIndex = new Int32Array([0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1, 1, 0, 1, 1]);
  19841. static #clockwiseNonZero(buf, width, i0, j0, i, j, offset) {
  19842. const id = this.#neighborIndexToId(i0, j0, i, j);
  19843. for (let k = 0; k < 8; k++) {
  19844. const kk = (-k + id - offset + 16) % 8;
  19845. const shiftI = this.#neighborIdToIndex[2 * kk];
  19846. const shiftJ = this.#neighborIdToIndex[2 * kk + 1];
  19847. if (buf[(i0 + shiftI) * width + (j0 + shiftJ)] !== 0) {
  19848. return kk;
  19849. }
  19850. }
  19851. return -1;
  19852. }
  19853. static #counterClockwiseNonZero(buf, width, i0, j0, i, j, offset) {
  19854. const id = this.#neighborIndexToId(i0, j0, i, j);
  19855. for (let k = 0; k < 8; k++) {
  19856. const kk = (k + id + offset + 16) % 8;
  19857. const shiftI = this.#neighborIdToIndex[2 * kk];
  19858. const shiftJ = this.#neighborIdToIndex[2 * kk + 1];
  19859. if (buf[(i0 + shiftI) * width + (j0 + shiftJ)] !== 0) {
  19860. return kk;
  19861. }
  19862. }
  19863. return -1;
  19864. }
  19865. static #findContours(buf, width, height, threshold) {
  19866. const N = buf.length;
  19867. const types = new Int32Array(N);
  19868. for (let i = 0; i < N; i++) {
  19869. types[i] = buf[i] <= threshold ? 1 : 0;
  19870. }
  19871. for (let i = 1; i < height - 1; i++) {
  19872. types[i * width] = types[i * width + width - 1] = 0;
  19873. }
  19874. for (let i = 0; i < width; i++) {
  19875. types[i] = types[width * height - 1 - i] = 0;
  19876. }
  19877. let nbd = 1;
  19878. let lnbd;
  19879. const contours = [];
  19880. for (let i = 1; i < height - 1; i++) {
  19881. lnbd = 1;
  19882. for (let j = 1; j < width - 1; j++) {
  19883. const ij = i * width + j;
  19884. const pix = types[ij];
  19885. if (pix === 0) {
  19886. continue;
  19887. }
  19888. let i2 = i;
  19889. let j2 = j;
  19890. if (pix === 1 && types[ij - 1] === 0) {
  19891. nbd += 1;
  19892. j2 -= 1;
  19893. } else if (pix >= 1 && types[ij + 1] === 0) {
  19894. nbd += 1;
  19895. j2 += 1;
  19896. if (pix > 1) {
  19897. lnbd = pix;
  19898. }
  19899. } else {
  19900. if (pix !== 1) {
  19901. lnbd = Math.abs(pix);
  19902. }
  19903. continue;
  19904. }
  19905. const points = [j, i];
  19906. const isHole = j2 === j + 1;
  19907. const contour = {
  19908. isHole,
  19909. points,
  19910. id: nbd,
  19911. parent: 0
  19912. };
  19913. contours.push(contour);
  19914. let contour0;
  19915. for (const c of contours) {
  19916. if (c.id === lnbd) {
  19917. contour0 = c;
  19918. break;
  19919. }
  19920. }
  19921. if (!contour0) {
  19922. contour.parent = isHole ? lnbd : 0;
  19923. } else if (contour0.isHole) {
  19924. contour.parent = isHole ? contour0.parent : lnbd;
  19925. } else {
  19926. contour.parent = isHole ? lnbd : contour0.parent;
  19927. }
  19928. const k = this.#clockwiseNonZero(types, width, i, j, i2, j2, 0);
  19929. if (k === -1) {
  19930. types[ij] = -nbd;
  19931. if (types[ij] !== 1) {
  19932. lnbd = Math.abs(types[ij]);
  19933. }
  19934. continue;
  19935. }
  19936. let shiftI = this.#neighborIdToIndex[2 * k];
  19937. let shiftJ = this.#neighborIdToIndex[2 * k + 1];
  19938. const i1 = i + shiftI;
  19939. const j1 = j + shiftJ;
  19940. i2 = i1;
  19941. j2 = j1;
  19942. let i3 = i;
  19943. let j3 = j;
  19944. while (true) {
  19945. const kk = this.#counterClockwiseNonZero(types, width, i3, j3, i2, j2, 1);
  19946. shiftI = this.#neighborIdToIndex[2 * kk];
  19947. shiftJ = this.#neighborIdToIndex[2 * kk + 1];
  19948. const i4 = i3 + shiftI;
  19949. const j4 = j3 + shiftJ;
  19950. points.push(j4, i4);
  19951. const ij3 = i3 * width + j3;
  19952. if (types[ij3 + 1] === 0) {
  19953. types[ij3] = -nbd;
  19954. } else if (types[ij3] === 1) {
  19955. types[ij3] = nbd;
  19956. }
  19957. if (i4 === i && j4 === j && i3 === i1 && j3 === j1) {
  19958. if (types[ij] !== 1) {
  19959. lnbd = Math.abs(types[ij]);
  19960. }
  19961. break;
  19962. } else {
  19963. i2 = i3;
  19964. j2 = j3;
  19965. i3 = i4;
  19966. j3 = j4;
  19967. }
  19968. }
  19969. }
  19970. }
  19971. return contours;
  19972. }
  19973. static #douglasPeuckerHelper(points, start, end, output) {
  19974. if (end - start <= 4) {
  19975. for (let i = start; i < end - 2; i += 2) {
  19976. output.push(points[i], points[i + 1]);
  19977. }
  19978. return;
  19979. }
  19980. const ax = points[start];
  19981. const ay = points[start + 1];
  19982. const abx = points[end - 4] - ax;
  19983. const aby = points[end - 3] - ay;
  19984. const dist = Math.hypot(abx, aby);
  19985. const nabx = abx / dist;
  19986. const naby = aby / dist;
  19987. const aa = nabx * ay - naby * ax;
  19988. const m = aby / abx;
  19989. const invS = 1 / dist;
  19990. const phi = Math.atan(m);
  19991. const cosPhi = Math.cos(phi);
  19992. const sinPhi = Math.sin(phi);
  19993. const tmax = invS * (Math.abs(cosPhi) + Math.abs(sinPhi));
  19994. const poly = invS * (1 - tmax + tmax ** 2);
  19995. const partialPhi = Math.max(Math.atan(Math.abs(sinPhi + cosPhi) * poly), Math.atan(Math.abs(sinPhi - cosPhi) * poly));
  19996. let dmax = 0;
  19997. let index = start;
  19998. for (let i = start + 2; i < end - 2; i += 2) {
  19999. const d = Math.abs(aa - nabx * points[i + 1] + naby * points[i]);
  20000. if (d > dmax) {
  20001. index = i;
  20002. dmax = d;
  20003. }
  20004. }
  20005. if (dmax > (dist * partialPhi) ** 2) {
  20006. this.#douglasPeuckerHelper(points, start, index + 2, output);
  20007. this.#douglasPeuckerHelper(points, index, end, output);
  20008. } else {
  20009. output.push(ax, ay);
  20010. }
  20011. }
  20012. static #douglasPeucker(points) {
  20013. const output = [];
  20014. const len = points.length;
  20015. this.#douglasPeuckerHelper(points, 0, len, output);
  20016. output.push(points[len - 2], points[len - 1]);
  20017. return output.length <= 4 ? null : output;
  20018. }
  20019. static #bilateralFilter(buf, width, height, sigmaS, sigmaR, kernelSize) {
  20020. const kernel = new Float32Array(kernelSize ** 2);
  20021. const sigmaS2 = -2 * sigmaS ** 2;
  20022. const halfSize = kernelSize >> 1;
  20023. for (let i = 0; i < kernelSize; i++) {
  20024. const x = (i - halfSize) ** 2;
  20025. for (let j = 0; j < kernelSize; j++) {
  20026. kernel[i * kernelSize + j] = Math.exp((x + (j - halfSize) ** 2) / sigmaS2);
  20027. }
  20028. }
  20029. const rangeValues = new Float32Array(256);
  20030. const sigmaR2 = -2 * sigmaR ** 2;
  20031. for (let i = 0; i < 256; i++) {
  20032. rangeValues[i] = Math.exp(i ** 2 / sigmaR2);
  20033. }
  20034. const N = buf.length;
  20035. const out = new Uint8Array(N);
  20036. const histogram = new Uint32Array(256);
  20037. for (let i = 0; i < height; i++) {
  20038. for (let j = 0; j < width; j++) {
  20039. const ij = i * width + j;
  20040. const center = buf[ij];
  20041. let sum = 0;
  20042. let norm = 0;
  20043. for (let k = 0; k < kernelSize; k++) {
  20044. const y = i + k - halfSize;
  20045. if (y < 0 || y >= height) {
  20046. continue;
  20047. }
  20048. for (let l = 0; l < kernelSize; l++) {
  20049. const x = j + l - halfSize;
  20050. if (x < 0 || x >= width) {
  20051. continue;
  20052. }
  20053. const neighbour = buf[y * width + x];
  20054. const w = kernel[k * kernelSize + l] * rangeValues[Math.abs(neighbour - center)];
  20055. sum += neighbour * w;
  20056. norm += w;
  20057. }
  20058. }
  20059. const pix = out[ij] = Math.round(sum / norm);
  20060. histogram[pix]++;
  20061. }
  20062. }
  20063. return [out, histogram];
  20064. }
  20065. static #getHistogram(buf) {
  20066. const histogram = new Uint32Array(256);
  20067. for (const g of buf) {
  20068. histogram[g]++;
  20069. }
  20070. return histogram;
  20071. }
  20072. static #toUint8(buf) {
  20073. const N = buf.length;
  20074. const out = new Uint8ClampedArray(N >> 2);
  20075. let max = -Infinity;
  20076. let min = Infinity;
  20077. for (let i = 0, ii = out.length; i < ii; i++) {
  20078. const A = buf[(i << 2) + 3];
  20079. if (A === 0) {
  20080. max = out[i] = 0xff;
  20081. continue;
  20082. }
  20083. const pix = out[i] = buf[i << 2];
  20084. if (pix > max) {
  20085. max = pix;
  20086. }
  20087. if (pix < min) {
  20088. min = pix;
  20089. }
  20090. }
  20091. const ratio = 255 / (max - min);
  20092. for (let i = 0; i < N; i++) {
  20093. out[i] = (out[i] - min) * ratio;
  20094. }
  20095. return out;
  20096. }
  20097. static #guessThreshold(histogram) {
  20098. let i;
  20099. let M = -Infinity;
  20100. let L = -Infinity;
  20101. const min = histogram.findIndex(v => v !== 0);
  20102. let pos = min;
  20103. let spos = min;
  20104. for (i = min; i < 256; i++) {
  20105. const v = histogram[i];
  20106. if (v > M) {
  20107. if (i - pos > L) {
  20108. L = i - pos;
  20109. spos = i - 1;
  20110. }
  20111. M = v;
  20112. pos = i;
  20113. }
  20114. }
  20115. for (i = spos - 1; i >= 0; i--) {
  20116. if (histogram[i] > histogram[i + 1]) {
  20117. break;
  20118. }
  20119. }
  20120. return i;
  20121. }
  20122. static #getGrayPixels(bitmap) {
  20123. const originalBitmap = bitmap;
  20124. const {
  20125. width,
  20126. height
  20127. } = bitmap;
  20128. const {
  20129. maxDim
  20130. } = this.#PARAMETERS;
  20131. let newWidth = width;
  20132. let newHeight = height;
  20133. if (width > maxDim || height > maxDim) {
  20134. let prevWidth = width;
  20135. let prevHeight = height;
  20136. let steps = Math.log2(Math.max(width, height) / maxDim);
  20137. const isteps = Math.floor(steps);
  20138. steps = steps === isteps ? isteps - 1 : isteps;
  20139. for (let i = 0; i < steps; i++) {
  20140. newWidth = prevWidth;
  20141. newHeight = prevHeight;
  20142. if (newWidth > maxDim) {
  20143. newWidth = Math.ceil(newWidth / 2);
  20144. }
  20145. if (newHeight > maxDim) {
  20146. newHeight = Math.ceil(newHeight / 2);
  20147. }
  20148. const offscreen = new OffscreenCanvas(newWidth, newHeight);
  20149. const ctx = offscreen.getContext("2d");
  20150. ctx.drawImage(bitmap, 0, 0, prevWidth, prevHeight, 0, 0, newWidth, newHeight);
  20151. prevWidth = newWidth;
  20152. prevHeight = newHeight;
  20153. if (bitmap !== originalBitmap) {
  20154. bitmap.close();
  20155. }
  20156. bitmap = offscreen.transferToImageBitmap();
  20157. }
  20158. const ratio = Math.min(maxDim / newWidth, maxDim / newHeight);
  20159. newWidth = Math.round(newWidth * ratio);
  20160. newHeight = Math.round(newHeight * ratio);
  20161. }
  20162. const offscreen = new OffscreenCanvas(newWidth, newHeight);
  20163. const ctx = offscreen.getContext("2d", {
  20164. willReadFrequently: true
  20165. });
  20166. ctx.filter = "grayscale(1)";
  20167. ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, newWidth, newHeight);
  20168. const grayImage = ctx.getImageData(0, 0, newWidth, newHeight).data;
  20169. const uint8Buf = this.#toUint8(grayImage);
  20170. return [uint8Buf, newWidth, newHeight];
  20171. }
  20172. static extractContoursFromText(text, {
  20173. fontFamily,
  20174. fontStyle,
  20175. fontWeight
  20176. }, pageWidth, pageHeight, rotation, innerMargin) {
  20177. let canvas = new OffscreenCanvas(1, 1);
  20178. let ctx = canvas.getContext("2d", {
  20179. alpha: false
  20180. });
  20181. const fontSize = 200;
  20182. const font = ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
  20183. const {
  20184. actualBoundingBoxLeft,
  20185. actualBoundingBoxRight,
  20186. actualBoundingBoxAscent,
  20187. actualBoundingBoxDescent,
  20188. fontBoundingBoxAscent,
  20189. fontBoundingBoxDescent,
  20190. width
  20191. } = ctx.measureText(text);
  20192. const SCALE = 1.5;
  20193. const canvasWidth = Math.ceil(Math.max(Math.abs(actualBoundingBoxLeft) + Math.abs(actualBoundingBoxRight) || 0, width) * SCALE);
  20194. const canvasHeight = Math.ceil(Math.max(Math.abs(actualBoundingBoxAscent) + Math.abs(actualBoundingBoxDescent) || fontSize, Math.abs(fontBoundingBoxAscent) + Math.abs(fontBoundingBoxDescent) || fontSize) * SCALE);
  20195. canvas = new OffscreenCanvas(canvasWidth, canvasHeight);
  20196. ctx = canvas.getContext("2d", {
  20197. alpha: true,
  20198. willReadFrequently: true
  20199. });
  20200. ctx.font = font;
  20201. ctx.filter = "grayscale(1)";
  20202. ctx.fillStyle = "white";
  20203. ctx.fillRect(0, 0, canvasWidth, canvasHeight);
  20204. ctx.fillStyle = "black";
  20205. ctx.fillText(text, canvasWidth * (SCALE - 1) / 2, canvasHeight * (3 - SCALE) / 2);
  20206. const uint8Buf = this.#toUint8(ctx.getImageData(0, 0, canvasWidth, canvasHeight).data);
  20207. const histogram = this.#getHistogram(uint8Buf);
  20208. const threshold = this.#guessThreshold(histogram);
  20209. const contourList = this.#findContours(uint8Buf, canvasWidth, canvasHeight, threshold);
  20210. return this.processDrawnLines({
  20211. lines: {
  20212. curves: contourList,
  20213. width: canvasWidth,
  20214. height: canvasHeight
  20215. },
  20216. pageWidth,
  20217. pageHeight,
  20218. rotation,
  20219. innerMargin,
  20220. mustSmooth: true,
  20221. areContours: true
  20222. });
  20223. }
  20224. static process(bitmap, pageWidth, pageHeight, rotation, innerMargin) {
  20225. const [uint8Buf, width, height] = this.#getGrayPixels(bitmap);
  20226. const [buffer, histogram] = this.#bilateralFilter(uint8Buf, width, height, Math.hypot(width, height) * this.#PARAMETERS.sigmaSFactor, this.#PARAMETERS.sigmaR, this.#PARAMETERS.kernelSize);
  20227. const threshold = this.#guessThreshold(histogram);
  20228. const contourList = this.#findContours(buffer, width, height, threshold);
  20229. return this.processDrawnLines({
  20230. lines: {
  20231. curves: contourList,
  20232. width,
  20233. height
  20234. },
  20235. pageWidth,
  20236. pageHeight,
  20237. rotation,
  20238. innerMargin,
  20239. mustSmooth: true,
  20240. areContours: true
  20241. });
  20242. }
  20243. static processDrawnLines({
  20244. lines,
  20245. pageWidth,
  20246. pageHeight,
  20247. rotation,
  20248. innerMargin,
  20249. mustSmooth,
  20250. areContours
  20251. }) {
  20252. if (rotation % 180 !== 0) {
  20253. [pageWidth, pageHeight] = [pageHeight, pageWidth];
  20254. }
  20255. const {
  20256. curves,
  20257. width,
  20258. height
  20259. } = lines;
  20260. const thickness = lines.thickness ?? 0;
  20261. const linesAndPoints = [];
  20262. const ratio = Math.min(pageWidth / width, pageHeight / height);
  20263. const xScale = ratio / pageWidth;
  20264. const yScale = ratio / pageHeight;
  20265. const newCurves = [];
  20266. for (const {
  20267. points
  20268. } of curves) {
  20269. const reducedPoints = mustSmooth ? this.#douglasPeucker(points) : points;
  20270. if (!reducedPoints) {
  20271. continue;
  20272. }
  20273. newCurves.push(reducedPoints);
  20274. const len = reducedPoints.length;
  20275. const newPoints = new Float32Array(len);
  20276. const line = new Float32Array(3 * (len === 2 ? 2 : len - 2));
  20277. linesAndPoints.push({
  20278. line,
  20279. points: newPoints
  20280. });
  20281. if (len === 2) {
  20282. newPoints[0] = reducedPoints[0] * xScale;
  20283. newPoints[1] = reducedPoints[1] * yScale;
  20284. line.set([NaN, NaN, NaN, NaN, newPoints[0], newPoints[1]], 0);
  20285. continue;
  20286. }
  20287. let [x1, y1, x2, y2] = reducedPoints;
  20288. x1 *= xScale;
  20289. y1 *= yScale;
  20290. x2 *= xScale;
  20291. y2 *= yScale;
  20292. newPoints.set([x1, y1, x2, y2], 0);
  20293. line.set([NaN, NaN, NaN, NaN, x1, y1], 0);
  20294. for (let i = 4; i < len; i += 2) {
  20295. const x = newPoints[i] = reducedPoints[i] * xScale;
  20296. const y = newPoints[i + 1] = reducedPoints[i + 1] * yScale;
  20297. line.set(Outline.createBezierPoints(x1, y1, x2, y2, x, y), (i - 2) * 3);
  20298. [x1, y1, x2, y2] = [x2, y2, x, y];
  20299. }
  20300. }
  20301. if (linesAndPoints.length === 0) {
  20302. return null;
  20303. }
  20304. const outline = areContours ? new ContourDrawOutline() : new InkDrawOutline();
  20305. outline.build(linesAndPoints, pageWidth, pageHeight, 1, rotation, areContours ? 0 : thickness, innerMargin);
  20306. return {
  20307. outline,
  20308. newCurves,
  20309. areContours,
  20310. thickness,
  20311. width,
  20312. height
  20313. };
  20314. }
  20315. static async compressSignature({
  20316. outlines,
  20317. areContours,
  20318. thickness,
  20319. width,
  20320. height
  20321. }) {
  20322. let minDiff = Infinity;
  20323. let maxDiff = -Infinity;
  20324. let outlinesLength = 0;
  20325. for (const points of outlines) {
  20326. outlinesLength += points.length;
  20327. for (let i = 2, ii = points.length; i < ii; i++) {
  20328. const dx = points[i] - points[i - 2];
  20329. minDiff = Math.min(minDiff, dx);
  20330. maxDiff = Math.max(maxDiff, dx);
  20331. }
  20332. }
  20333. let bufferType;
  20334. if (minDiff >= -128 && maxDiff <= 127) {
  20335. bufferType = Int8Array;
  20336. } else if (minDiff >= -32768 && maxDiff <= 32767) {
  20337. bufferType = Int16Array;
  20338. } else {
  20339. bufferType = Int32Array;
  20340. }
  20341. const len = outlines.length;
  20342. const headerLength = BASE_HEADER_LENGTH + POINTS_PROPERTIES_NUMBER * len;
  20343. const header = new Uint32Array(headerLength);
  20344. let offset = 0;
  20345. header[offset++] = headerLength * Uint32Array.BYTES_PER_ELEMENT + (outlinesLength - 2 * len) * bufferType.BYTES_PER_ELEMENT;
  20346. header[offset++] = 0;
  20347. header[offset++] = width;
  20348. header[offset++] = height;
  20349. header[offset++] = areContours ? 0 : 1;
  20350. header[offset++] = Math.max(0, Math.floor(thickness ?? 0));
  20351. header[offset++] = len;
  20352. header[offset++] = bufferType.BYTES_PER_ELEMENT;
  20353. for (const points of outlines) {
  20354. header[offset++] = points.length - 2;
  20355. header[offset++] = points[0];
  20356. header[offset++] = points[1];
  20357. }
  20358. const cs = new CompressionStream("deflate-raw");
  20359. const writer = cs.writable.getWriter();
  20360. await writer.ready;
  20361. writer.write(header);
  20362. const BufferCtor = bufferType.prototype.constructor;
  20363. for (const points of outlines) {
  20364. const diffs = new BufferCtor(points.length - 2);
  20365. for (let i = 2, ii = points.length; i < ii; i++) {
  20366. diffs[i - 2] = points[i] - points[i - 2];
  20367. }
  20368. writer.write(diffs);
  20369. }
  20370. writer.close();
  20371. const buf = await new Response(cs.readable).arrayBuffer();
  20372. const bytes = new Uint8Array(buf);
  20373. return toBase64Util(bytes);
  20374. }
  20375. static async decompressSignature(signatureData) {
  20376. try {
  20377. const bytes = fromBase64Util(signatureData);
  20378. const {
  20379. readable,
  20380. writable
  20381. } = new DecompressionStream("deflate-raw");
  20382. const writer = writable.getWriter();
  20383. await writer.ready;
  20384. writer.write(bytes).then(async () => {
  20385. await writer.ready;
  20386. await writer.close();
  20387. }).catch(() => {});
  20388. let data = null;
  20389. let offset = 0;
  20390. for await (const chunk of readable) {
  20391. data ||= new Uint8Array(new Uint32Array(chunk.buffer, 0, 4)[0]);
  20392. data.set(chunk, offset);
  20393. offset += chunk.length;
  20394. }
  20395. const header = new Uint32Array(data.buffer, 0, data.length >> 2);
  20396. const version = header[1];
  20397. if (version !== 0) {
  20398. throw new Error(`Invalid version: ${version}`);
  20399. }
  20400. const width = header[2];
  20401. const height = header[3];
  20402. const areContours = header[4] === 0;
  20403. const thickness = header[5];
  20404. const numberOfDrawings = header[6];
  20405. const bufferType = header[7];
  20406. const outlines = [];
  20407. const diffsOffset = (BASE_HEADER_LENGTH + POINTS_PROPERTIES_NUMBER * numberOfDrawings) * Uint32Array.BYTES_PER_ELEMENT;
  20408. let diffs;
  20409. switch (bufferType) {
  20410. case Int8Array.BYTES_PER_ELEMENT:
  20411. diffs = new Int8Array(data.buffer, diffsOffset);
  20412. break;
  20413. case Int16Array.BYTES_PER_ELEMENT:
  20414. diffs = new Int16Array(data.buffer, diffsOffset);
  20415. break;
  20416. case Int32Array.BYTES_PER_ELEMENT:
  20417. diffs = new Int32Array(data.buffer, diffsOffset);
  20418. break;
  20419. }
  20420. offset = 0;
  20421. for (let i = 0; i < numberOfDrawings; i++) {
  20422. const len = header[POINTS_PROPERTIES_NUMBER * i + BASE_HEADER_LENGTH];
  20423. const points = new Float32Array(len + 2);
  20424. outlines.push(points);
  20425. for (let j = 0; j < POINTS_PROPERTIES_NUMBER - 1; j++) {
  20426. points[j] = header[POINTS_PROPERTIES_NUMBER * i + BASE_HEADER_LENGTH + j + 1];
  20427. }
  20428. for (let j = 0; j < len; j++) {
  20429. points[j + 2] = points[j] + diffs[offset++];
  20430. }
  20431. }
  20432. return {
  20433. areContours,
  20434. thickness,
  20435. outlines,
  20436. width,
  20437. height
  20438. };
  20439. } catch (e) {
  20440. warn(`decompressSignature: ${e}`);
  20441. return null;
  20442. }
  20443. }
  20444. }
  20445. ;// ./src/display/editor/signature.js
  20446. class SignatureOptions extends DrawingOptions {
  20447. constructor() {
  20448. super();
  20449. super.updateProperties({
  20450. fill: AnnotationEditor._defaultLineColor,
  20451. "stroke-width": 0
  20452. });
  20453. }
  20454. clone() {
  20455. const clone = new SignatureOptions();
  20456. clone.updateAll(this);
  20457. return clone;
  20458. }
  20459. }
  20460. class DrawnSignatureOptions extends InkDrawingOptions {
  20461. constructor(viewerParameters) {
  20462. super(viewerParameters);
  20463. super.updateProperties({
  20464. stroke: AnnotationEditor._defaultLineColor,
  20465. "stroke-width": 1
  20466. });
  20467. }
  20468. clone() {
  20469. const clone = new DrawnSignatureOptions(this._viewParameters);
  20470. clone.updateAll(this);
  20471. return clone;
  20472. }
  20473. }
  20474. class SignatureEditor extends DrawingEditor {
  20475. #isExtracted = false;
  20476. #description = null;
  20477. #signatureData = null;
  20478. #signatureUUID = null;
  20479. static _type = "signature";
  20480. static _editorType = AnnotationEditorType.SIGNATURE;
  20481. static _defaultDrawingOptions = null;
  20482. constructor(params) {
  20483. super({
  20484. ...params,
  20485. mustBeCommitted: true,
  20486. name: "signatureEditor"
  20487. });
  20488. this._willKeepAspectRatio = true;
  20489. this.#signatureData = params.signatureData || null;
  20490. this.#description = null;
  20491. this.defaultL10nId = "pdfjs-editor-signature-editor1";
  20492. }
  20493. static initialize(l10n, uiManager) {
  20494. AnnotationEditor.initialize(l10n, uiManager);
  20495. this._defaultDrawingOptions = new SignatureOptions();
  20496. this._defaultDrawnSignatureOptions = new DrawnSignatureOptions(uiManager.viewParameters);
  20497. }
  20498. static getDefaultDrawingOptions(options) {
  20499. const clone = this._defaultDrawingOptions.clone();
  20500. clone.updateProperties(options);
  20501. return clone;
  20502. }
  20503. static get supportMultipleDrawings() {
  20504. return false;
  20505. }
  20506. static get typesMap() {
  20507. return shadow(this, "typesMap", new Map());
  20508. }
  20509. static get isDrawer() {
  20510. return false;
  20511. }
  20512. get telemetryFinalData() {
  20513. return {
  20514. type: "signature",
  20515. hasDescription: !!this.#description
  20516. };
  20517. }
  20518. static computeTelemetryFinalData(data) {
  20519. const hasDescriptionStats = data.get("hasDescription");
  20520. return {
  20521. hasAltText: hasDescriptionStats.get(true) ?? 0,
  20522. hasNoAltText: hasDescriptionStats.get(false) ?? 0
  20523. };
  20524. }
  20525. get isResizable() {
  20526. return true;
  20527. }
  20528. onScaleChanging() {
  20529. if (this._drawId === null) {
  20530. return;
  20531. }
  20532. super.onScaleChanging();
  20533. }
  20534. render() {
  20535. if (this.div) {
  20536. return this.div;
  20537. }
  20538. let baseX, baseY;
  20539. const {
  20540. _isCopy
  20541. } = this;
  20542. if (_isCopy) {
  20543. this._isCopy = false;
  20544. baseX = this.x;
  20545. baseY = this.y;
  20546. }
  20547. super.render();
  20548. if (this._drawId === null) {
  20549. if (this.#signatureData) {
  20550. const {
  20551. lines,
  20552. mustSmooth,
  20553. areContours,
  20554. description,
  20555. uuid,
  20556. heightInPage
  20557. } = this.#signatureData;
  20558. const {
  20559. rawDims: {
  20560. pageWidth,
  20561. pageHeight
  20562. },
  20563. rotation
  20564. } = this.parent.viewport;
  20565. const outline = SignatureExtractor.processDrawnLines({
  20566. lines,
  20567. pageWidth,
  20568. pageHeight,
  20569. rotation,
  20570. innerMargin: SignatureEditor._INNER_MARGIN,
  20571. mustSmooth,
  20572. areContours
  20573. });
  20574. this.addSignature(outline, heightInPage, description, uuid);
  20575. } else {
  20576. this.div.setAttribute("data-l10n-args", JSON.stringify({
  20577. description: ""
  20578. }));
  20579. this.div.hidden = true;
  20580. this._uiManager.getSignature(this);
  20581. }
  20582. }
  20583. if (_isCopy) {
  20584. this._isCopy = true;
  20585. this._moveAfterPaste(baseX, baseY);
  20586. }
  20587. return this.div;
  20588. }
  20589. setUuid(uuid) {
  20590. this.#signatureUUID = uuid;
  20591. this.addEditToolbar();
  20592. }
  20593. getUuid() {
  20594. return this.#signatureUUID;
  20595. }
  20596. get description() {
  20597. return this.#description;
  20598. }
  20599. set description(description) {
  20600. this.#description = description;
  20601. super.addEditToolbar().then(toolbar => {
  20602. toolbar?.updateEditSignatureButton(description);
  20603. });
  20604. }
  20605. getSignaturePreview() {
  20606. const {
  20607. newCurves,
  20608. areContours,
  20609. thickness,
  20610. width,
  20611. height
  20612. } = this.#signatureData;
  20613. const maxDim = Math.max(width, height);
  20614. const outlineData = SignatureExtractor.processDrawnLines({
  20615. lines: {
  20616. curves: newCurves.map(points => ({
  20617. points
  20618. })),
  20619. thickness,
  20620. width,
  20621. height
  20622. },
  20623. pageWidth: maxDim,
  20624. pageHeight: maxDim,
  20625. rotation: 0,
  20626. innerMargin: 0,
  20627. mustSmooth: false,
  20628. areContours
  20629. });
  20630. return {
  20631. areContours,
  20632. outline: outlineData.outline
  20633. };
  20634. }
  20635. async addEditToolbar() {
  20636. const toolbar = await super.addEditToolbar();
  20637. if (!toolbar) {
  20638. return null;
  20639. }
  20640. if (this._uiManager.signatureManager && this.#description !== null) {
  20641. await toolbar.addEditSignatureButton(this._uiManager.signatureManager, this.#signatureUUID, this.#description);
  20642. toolbar.show();
  20643. }
  20644. return toolbar;
  20645. }
  20646. addSignature(data, heightInPage, description, uuid) {
  20647. const {
  20648. x: savedX,
  20649. y: savedY
  20650. } = this;
  20651. const {
  20652. outline
  20653. } = this.#signatureData = data;
  20654. this.#isExtracted = outline instanceof ContourDrawOutline;
  20655. this.#description = description;
  20656. this.div.setAttribute("data-l10n-args", JSON.stringify({
  20657. description
  20658. }));
  20659. let drawingOptions;
  20660. if (this.#isExtracted) {
  20661. drawingOptions = SignatureEditor.getDefaultDrawingOptions();
  20662. } else {
  20663. drawingOptions = SignatureEditor._defaultDrawnSignatureOptions.clone();
  20664. drawingOptions.updateProperties({
  20665. "stroke-width": outline.thickness
  20666. });
  20667. }
  20668. this._addOutlines({
  20669. drawOutlines: outline,
  20670. drawingOptions
  20671. });
  20672. const [parentWidth, parentHeight] = this.parentDimensions;
  20673. const [, pageHeight] = this.pageDimensions;
  20674. let newHeight = heightInPage / pageHeight;
  20675. newHeight = newHeight >= 1 ? 0.5 : newHeight;
  20676. this.width *= newHeight / this.height;
  20677. if (this.width >= 1) {
  20678. newHeight *= 0.9 / this.width;
  20679. this.width = 0.9;
  20680. }
  20681. this.height = newHeight;
  20682. this.setDims(parentWidth * this.width, parentHeight * this.height);
  20683. this.x = savedX;
  20684. this.y = savedY;
  20685. this.center();
  20686. this._onResized();
  20687. this.onScaleChanging();
  20688. this.rotate();
  20689. this._uiManager.addToAnnotationStorage(this);
  20690. this.setUuid(uuid);
  20691. this._reportTelemetry({
  20692. action: "pdfjs.signature.inserted",
  20693. data: {
  20694. hasBeenSaved: !!uuid,
  20695. hasDescription: !!description
  20696. }
  20697. });
  20698. this.div.hidden = false;
  20699. }
  20700. getFromImage(bitmap) {
  20701. const {
  20702. rawDims: {
  20703. pageWidth,
  20704. pageHeight
  20705. },
  20706. rotation
  20707. } = this.parent.viewport;
  20708. return SignatureExtractor.process(bitmap, pageWidth, pageHeight, rotation, SignatureEditor._INNER_MARGIN);
  20709. }
  20710. getFromText(text, fontInfo) {
  20711. const {
  20712. rawDims: {
  20713. pageWidth,
  20714. pageHeight
  20715. },
  20716. rotation
  20717. } = this.parent.viewport;
  20718. return SignatureExtractor.extractContoursFromText(text, fontInfo, pageWidth, pageHeight, rotation, SignatureEditor._INNER_MARGIN);
  20719. }
  20720. getDrawnSignature(curves) {
  20721. const {
  20722. rawDims: {
  20723. pageWidth,
  20724. pageHeight
  20725. },
  20726. rotation
  20727. } = this.parent.viewport;
  20728. return SignatureExtractor.processDrawnLines({
  20729. lines: curves,
  20730. pageWidth,
  20731. pageHeight,
  20732. rotation,
  20733. innerMargin: SignatureEditor._INNER_MARGIN,
  20734. mustSmooth: false,
  20735. areContours: false
  20736. });
  20737. }
  20738. createDrawingOptions({
  20739. areContours,
  20740. thickness
  20741. }) {
  20742. if (areContours) {
  20743. this._drawingOptions = SignatureEditor.getDefaultDrawingOptions();
  20744. } else {
  20745. this._drawingOptions = SignatureEditor._defaultDrawnSignatureOptions.clone();
  20746. this._drawingOptions.updateProperties({
  20747. "stroke-width": thickness
  20748. });
  20749. }
  20750. }
  20751. serialize(isForCopying = false) {
  20752. if (this.isEmpty()) {
  20753. return null;
  20754. }
  20755. const {
  20756. lines,
  20757. points,
  20758. rect
  20759. } = this.serializeDraw(isForCopying);
  20760. const {
  20761. _drawingOptions: {
  20762. "stroke-width": thickness
  20763. }
  20764. } = this;
  20765. const serialized = {
  20766. annotationType: AnnotationEditorType.SIGNATURE,
  20767. isSignature: true,
  20768. areContours: this.#isExtracted,
  20769. color: [0, 0, 0],
  20770. thickness: this.#isExtracted ? 0 : thickness,
  20771. pageIndex: this.pageIndex,
  20772. rect,
  20773. rotation: this.rotation,
  20774. structTreeParentId: this._structTreeParentId
  20775. };
  20776. if (isForCopying) {
  20777. serialized.paths = {
  20778. lines,
  20779. points
  20780. };
  20781. serialized.uuid = this.#signatureUUID;
  20782. serialized.isCopy = true;
  20783. } else {
  20784. serialized.lines = lines;
  20785. }
  20786. if (this.#description) {
  20787. serialized.accessibilityData = {
  20788. type: "Figure",
  20789. alt: this.#description
  20790. };
  20791. }
  20792. return serialized;
  20793. }
  20794. static deserializeDraw(pageX, pageY, pageWidth, pageHeight, innerMargin, data) {
  20795. if (data.areContours) {
  20796. return ContourDrawOutline.deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, data);
  20797. }
  20798. return InkDrawOutline.deserialize(pageX, pageY, pageWidth, pageHeight, innerMargin, data);
  20799. }
  20800. static async deserialize(data, parent, uiManager) {
  20801. const editor = await super.deserialize(data, parent, uiManager);
  20802. editor.#isExtracted = data.areContours;
  20803. editor.#description = data.accessibilityData?.alt || "";
  20804. editor.#signatureUUID = data.uuid;
  20805. return editor;
  20806. }
  20807. }
  20808. ;// ./src/display/editor/stamp.js
  20809. class StampEditor extends AnnotationEditor {
  20810. #bitmap = null;
  20811. #bitmapId = null;
  20812. #bitmapPromise = null;
  20813. #bitmapUrl = null;
  20814. #bitmapFile = null;
  20815. #bitmapFileName = "";
  20816. #canvas = null;
  20817. #missingCanvas = false;
  20818. #resizeTimeoutId = null;
  20819. #isSvg = false;
  20820. #hasBeenAddedInUndoStack = false;
  20821. static _type = "stamp";
  20822. static _editorType = AnnotationEditorType.STAMP;
  20823. constructor(params) {
  20824. super({
  20825. ...params,
  20826. name: "stampEditor"
  20827. });
  20828. this.#bitmapUrl = params.bitmapUrl;
  20829. this.#bitmapFile = params.bitmapFile;
  20830. this.defaultL10nId = "pdfjs-editor-stamp-editor";
  20831. }
  20832. static initialize(l10n, uiManager) {
  20833. AnnotationEditor.initialize(l10n, uiManager);
  20834. }
  20835. static isHandlingMimeForPasting(mime) {
  20836. return SupportedImageMimeTypes.includes(mime);
  20837. }
  20838. static paste(item, parent) {
  20839. parent.pasteEditor(AnnotationEditorType.STAMP, {
  20840. bitmapFile: item.getAsFile()
  20841. });
  20842. }
  20843. altTextFinish() {
  20844. if (this._uiManager.useNewAltTextFlow) {
  20845. this.div.hidden = false;
  20846. }
  20847. super.altTextFinish();
  20848. }
  20849. get telemetryFinalData() {
  20850. return {
  20851. type: "stamp",
  20852. hasAltText: !!this.altTextData?.altText
  20853. };
  20854. }
  20855. static computeTelemetryFinalData(data) {
  20856. const hasAltTextStats = data.get("hasAltText");
  20857. return {
  20858. hasAltText: hasAltTextStats.get(true) ?? 0,
  20859. hasNoAltText: hasAltTextStats.get(false) ?? 0
  20860. };
  20861. }
  20862. #getBitmapFetched(data, fromId = false) {
  20863. if (!data) {
  20864. this.remove();
  20865. return;
  20866. }
  20867. this.#bitmap = data.bitmap;
  20868. if (!fromId) {
  20869. this.#bitmapId = data.id;
  20870. this.#isSvg = data.isSvg;
  20871. }
  20872. if (data.file) {
  20873. this.#bitmapFileName = data.file.name;
  20874. }
  20875. this.#createCanvas();
  20876. }
  20877. #getBitmapDone() {
  20878. this.#bitmapPromise = null;
  20879. this._uiManager.enableWaiting(false);
  20880. if (!this.#canvas) {
  20881. return;
  20882. }
  20883. if (this._uiManager.useNewAltTextWhenAddingImage && this._uiManager.useNewAltTextFlow && this.#bitmap) {
  20884. this._editToolbar.hide();
  20885. this._uiManager.editAltText(this, true);
  20886. return;
  20887. }
  20888. if (!this._uiManager.useNewAltTextWhenAddingImage && this._uiManager.useNewAltTextFlow && this.#bitmap) {
  20889. this._reportTelemetry({
  20890. action: "pdfjs.image.image_added",
  20891. data: {
  20892. alt_text_modal: false,
  20893. alt_text_type: "empty"
  20894. }
  20895. });
  20896. try {
  20897. this.mlGuessAltText();
  20898. } catch {}
  20899. }
  20900. this.div.focus();
  20901. }
  20902. async mlGuessAltText(imageData = null, updateAltTextData = true) {
  20903. if (this.hasAltTextData()) {
  20904. return null;
  20905. }
  20906. const {
  20907. mlManager
  20908. } = this._uiManager;
  20909. if (!mlManager) {
  20910. throw new Error("No ML.");
  20911. }
  20912. if (!(await mlManager.isEnabledFor("altText"))) {
  20913. throw new Error("ML isn't enabled for alt text.");
  20914. }
  20915. const {
  20916. data,
  20917. width,
  20918. height
  20919. } = imageData || this.copyCanvas(null, null, true).imageData;
  20920. const response = await mlManager.guess({
  20921. name: "altText",
  20922. request: {
  20923. data,
  20924. width,
  20925. height,
  20926. channels: data.length / (width * height)
  20927. }
  20928. });
  20929. if (!response) {
  20930. throw new Error("No response from the AI service.");
  20931. }
  20932. if (response.error) {
  20933. throw new Error("Error from the AI service.");
  20934. }
  20935. if (response.cancel) {
  20936. return null;
  20937. }
  20938. if (!response.output) {
  20939. throw new Error("No valid response from the AI service.");
  20940. }
  20941. const altText = response.output;
  20942. await this.setGuessedAltText(altText);
  20943. if (updateAltTextData && !this.hasAltTextData()) {
  20944. this.altTextData = {
  20945. alt: altText,
  20946. decorative: false
  20947. };
  20948. }
  20949. return altText;
  20950. }
  20951. #getBitmap() {
  20952. if (this.#bitmapId) {
  20953. this._uiManager.enableWaiting(true);
  20954. this._uiManager.imageManager.getFromId(this.#bitmapId).then(data => this.#getBitmapFetched(data, true)).finally(() => this.#getBitmapDone());
  20955. return;
  20956. }
  20957. if (this.#bitmapUrl) {
  20958. const url = this.#bitmapUrl;
  20959. this.#bitmapUrl = null;
  20960. this._uiManager.enableWaiting(true);
  20961. this.#bitmapPromise = this._uiManager.imageManager.getFromUrl(url).then(data => this.#getBitmapFetched(data)).finally(() => this.#getBitmapDone());
  20962. return;
  20963. }
  20964. if (this.#bitmapFile) {
  20965. const file = this.#bitmapFile;
  20966. this.#bitmapFile = null;
  20967. this._uiManager.enableWaiting(true);
  20968. this.#bitmapPromise = this._uiManager.imageManager.getFromFile(file).then(data => this.#getBitmapFetched(data)).finally(() => this.#getBitmapDone());
  20969. return;
  20970. }
  20971. const input = document.createElement("input");
  20972. input.type = "file";
  20973. input.accept = SupportedImageMimeTypes.join(",");
  20974. const signal = this._uiManager._signal;
  20975. this.#bitmapPromise = new Promise(resolve => {
  20976. input.addEventListener("change", async () => {
  20977. if (!input.files || input.files.length === 0) {
  20978. this.remove();
  20979. } else {
  20980. this._uiManager.enableWaiting(true);
  20981. const data = await this._uiManager.imageManager.getFromFile(input.files[0]);
  20982. this._reportTelemetry({
  20983. action: "pdfjs.image.image_selected",
  20984. data: {
  20985. alt_text_modal: this._uiManager.useNewAltTextFlow
  20986. }
  20987. });
  20988. this.#getBitmapFetched(data);
  20989. }
  20990. resolve();
  20991. }, {
  20992. signal
  20993. });
  20994. input.addEventListener("cancel", () => {
  20995. this.remove();
  20996. resolve();
  20997. }, {
  20998. signal
  20999. });
  21000. }).finally(() => this.#getBitmapDone());
  21001. input.click();
  21002. }
  21003. remove() {
  21004. if (this.#bitmapId) {
  21005. this.#bitmap = null;
  21006. this._uiManager.imageManager.deleteId(this.#bitmapId);
  21007. this.#canvas?.remove();
  21008. this.#canvas = null;
  21009. if (this.#resizeTimeoutId) {
  21010. clearTimeout(this.#resizeTimeoutId);
  21011. this.#resizeTimeoutId = null;
  21012. }
  21013. }
  21014. super.remove();
  21015. }
  21016. rebuild() {
  21017. if (!this.parent) {
  21018. if (this.#bitmapId) {
  21019. this.#getBitmap();
  21020. }
  21021. return;
  21022. }
  21023. super.rebuild();
  21024. if (this.div === null) {
  21025. return;
  21026. }
  21027. if (this.#bitmapId && this.#canvas === null) {
  21028. this.#getBitmap();
  21029. }
  21030. if (!this.isAttachedToDOM) {
  21031. this.parent.add(this);
  21032. }
  21033. }
  21034. onceAdded(focus) {
  21035. this._isDraggable = true;
  21036. if (focus) {
  21037. this.div.focus();
  21038. }
  21039. }
  21040. isEmpty() {
  21041. return !(this.#bitmapPromise || this.#bitmap || this.#bitmapUrl || this.#bitmapFile || this.#bitmapId || this.#missingCanvas);
  21042. }
  21043. get isResizable() {
  21044. return true;
  21045. }
  21046. render() {
  21047. if (this.div) {
  21048. return this.div;
  21049. }
  21050. let baseX, baseY;
  21051. if (this._isCopy) {
  21052. baseX = this.x;
  21053. baseY = this.y;
  21054. }
  21055. super.render();
  21056. this.div.hidden = true;
  21057. this.addAltTextButton();
  21058. if (!this.#missingCanvas) {
  21059. if (this.#bitmap) {
  21060. this.#createCanvas();
  21061. } else {
  21062. this.#getBitmap();
  21063. }
  21064. }
  21065. if (this._isCopy) {
  21066. this._moveAfterPaste(baseX, baseY);
  21067. }
  21068. this._uiManager.addShouldRescale(this);
  21069. return this.div;
  21070. }
  21071. setCanvas(annotationElementId, canvas) {
  21072. const {
  21073. id: bitmapId,
  21074. bitmap
  21075. } = this._uiManager.imageManager.getFromCanvas(annotationElementId, canvas);
  21076. canvas.remove();
  21077. if (bitmapId && this._uiManager.imageManager.isValidId(bitmapId)) {
  21078. this.#bitmapId = bitmapId;
  21079. if (bitmap) {
  21080. this.#bitmap = bitmap;
  21081. }
  21082. this.#missingCanvas = false;
  21083. this.#createCanvas();
  21084. }
  21085. }
  21086. _onResized() {
  21087. this.onScaleChanging();
  21088. }
  21089. onScaleChanging() {
  21090. if (!this.parent) {
  21091. return;
  21092. }
  21093. if (this.#resizeTimeoutId !== null) {
  21094. clearTimeout(this.#resizeTimeoutId);
  21095. }
  21096. const TIME_TO_WAIT = 200;
  21097. this.#resizeTimeoutId = setTimeout(() => {
  21098. this.#resizeTimeoutId = null;
  21099. this.#drawBitmap();
  21100. }, TIME_TO_WAIT);
  21101. }
  21102. #createCanvas() {
  21103. const {
  21104. div
  21105. } = this;
  21106. let {
  21107. width,
  21108. height
  21109. } = this.#bitmap;
  21110. const [pageWidth, pageHeight] = this.pageDimensions;
  21111. const MAX_RATIO = 0.75;
  21112. if (this.width) {
  21113. width = this.width * pageWidth;
  21114. height = this.height * pageHeight;
  21115. } else if (width > MAX_RATIO * pageWidth || height > MAX_RATIO * pageHeight) {
  21116. const factor = Math.min(MAX_RATIO * pageWidth / width, MAX_RATIO * pageHeight / height);
  21117. width *= factor;
  21118. height *= factor;
  21119. }
  21120. const [parentWidth, parentHeight] = this.parentDimensions;
  21121. this.setDims(width * parentWidth / pageWidth, height * parentHeight / pageHeight);
  21122. this._uiManager.enableWaiting(false);
  21123. const canvas = this.#canvas = document.createElement("canvas");
  21124. canvas.setAttribute("role", "img");
  21125. this.addContainer(canvas);
  21126. this.width = width / pageWidth;
  21127. this.height = height / pageHeight;
  21128. if (this._initialOptions?.isCentered) {
  21129. this.center();
  21130. } else {
  21131. this.fixAndSetPosition();
  21132. }
  21133. this._initialOptions = null;
  21134. if (!this._uiManager.useNewAltTextWhenAddingImage || !this._uiManager.useNewAltTextFlow || this.annotationElementId) {
  21135. div.hidden = false;
  21136. }
  21137. this.#drawBitmap();
  21138. if (!this.#hasBeenAddedInUndoStack) {
  21139. this.parent.addUndoableEditor(this);
  21140. this.#hasBeenAddedInUndoStack = true;
  21141. }
  21142. this._reportTelemetry({
  21143. action: "inserted_image"
  21144. });
  21145. if (this.#bitmapFileName) {
  21146. this.div.setAttribute("aria-description", this.#bitmapFileName);
  21147. }
  21148. }
  21149. copyCanvas(maxDataDimension, maxPreviewDimension, createImageData = false) {
  21150. if (!maxDataDimension) {
  21151. maxDataDimension = 224;
  21152. }
  21153. const {
  21154. width: bitmapWidth,
  21155. height: bitmapHeight
  21156. } = this.#bitmap;
  21157. const outputScale = new OutputScale();
  21158. let bitmap = this.#bitmap;
  21159. let width = bitmapWidth,
  21160. height = bitmapHeight;
  21161. let canvas = null;
  21162. if (maxPreviewDimension) {
  21163. if (bitmapWidth > maxPreviewDimension || bitmapHeight > maxPreviewDimension) {
  21164. const ratio = Math.min(maxPreviewDimension / bitmapWidth, maxPreviewDimension / bitmapHeight);
  21165. width = Math.floor(bitmapWidth * ratio);
  21166. height = Math.floor(bitmapHeight * ratio);
  21167. }
  21168. canvas = document.createElement("canvas");
  21169. const scaledWidth = canvas.width = Math.ceil(width * outputScale.sx);
  21170. const scaledHeight = canvas.height = Math.ceil(height * outputScale.sy);
  21171. if (!this.#isSvg) {
  21172. bitmap = this.#scaleBitmap(scaledWidth, scaledHeight);
  21173. }
  21174. const ctx = canvas.getContext("2d");
  21175. ctx.filter = this._uiManager.hcmFilter;
  21176. let white = "white",
  21177. black = "#cfcfd8";
  21178. if (this._uiManager.hcmFilter !== "none") {
  21179. black = "black";
  21180. } else if (window.matchMedia?.("(prefers-color-scheme: dark)").matches) {
  21181. white = "#8f8f9d";
  21182. black = "#42414d";
  21183. }
  21184. const boxDim = 15;
  21185. const boxDimWidth = boxDim * outputScale.sx;
  21186. const boxDimHeight = boxDim * outputScale.sy;
  21187. const pattern = new OffscreenCanvas(boxDimWidth * 2, boxDimHeight * 2);
  21188. const patternCtx = pattern.getContext("2d");
  21189. patternCtx.fillStyle = white;
  21190. patternCtx.fillRect(0, 0, boxDimWidth * 2, boxDimHeight * 2);
  21191. patternCtx.fillStyle = black;
  21192. patternCtx.fillRect(0, 0, boxDimWidth, boxDimHeight);
  21193. patternCtx.fillRect(boxDimWidth, boxDimHeight, boxDimWidth, boxDimHeight);
  21194. ctx.fillStyle = ctx.createPattern(pattern, "repeat");
  21195. ctx.fillRect(0, 0, scaledWidth, scaledHeight);
  21196. ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, scaledWidth, scaledHeight);
  21197. }
  21198. let imageData = null;
  21199. if (createImageData) {
  21200. let dataWidth, dataHeight;
  21201. if (outputScale.symmetric && bitmap.width < maxDataDimension && bitmap.height < maxDataDimension) {
  21202. dataWidth = bitmap.width;
  21203. dataHeight = bitmap.height;
  21204. } else {
  21205. bitmap = this.#bitmap;
  21206. if (bitmapWidth > maxDataDimension || bitmapHeight > maxDataDimension) {
  21207. const ratio = Math.min(maxDataDimension / bitmapWidth, maxDataDimension / bitmapHeight);
  21208. dataWidth = Math.floor(bitmapWidth * ratio);
  21209. dataHeight = Math.floor(bitmapHeight * ratio);
  21210. if (!this.#isSvg) {
  21211. bitmap = this.#scaleBitmap(dataWidth, dataHeight);
  21212. }
  21213. }
  21214. }
  21215. const offscreen = new OffscreenCanvas(dataWidth, dataHeight);
  21216. const offscreenCtx = offscreen.getContext("2d", {
  21217. willReadFrequently: true
  21218. });
  21219. offscreenCtx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, dataWidth, dataHeight);
  21220. imageData = {
  21221. width: dataWidth,
  21222. height: dataHeight,
  21223. data: offscreenCtx.getImageData(0, 0, dataWidth, dataHeight).data
  21224. };
  21225. }
  21226. return {
  21227. canvas,
  21228. width,
  21229. height,
  21230. imageData
  21231. };
  21232. }
  21233. #scaleBitmap(width, height) {
  21234. const {
  21235. width: bitmapWidth,
  21236. height: bitmapHeight
  21237. } = this.#bitmap;
  21238. let newWidth = bitmapWidth;
  21239. let newHeight = bitmapHeight;
  21240. let bitmap = this.#bitmap;
  21241. while (newWidth > 2 * width || newHeight > 2 * height) {
  21242. const prevWidth = newWidth;
  21243. const prevHeight = newHeight;
  21244. if (newWidth > 2 * width) {
  21245. newWidth = newWidth >= 16384 ? Math.floor(newWidth / 2) - 1 : Math.ceil(newWidth / 2);
  21246. }
  21247. if (newHeight > 2 * height) {
  21248. newHeight = newHeight >= 16384 ? Math.floor(newHeight / 2) - 1 : Math.ceil(newHeight / 2);
  21249. }
  21250. const offscreen = new OffscreenCanvas(newWidth, newHeight);
  21251. const ctx = offscreen.getContext("2d");
  21252. ctx.drawImage(bitmap, 0, 0, prevWidth, prevHeight, 0, 0, newWidth, newHeight);
  21253. bitmap = offscreen.transferToImageBitmap();
  21254. }
  21255. return bitmap;
  21256. }
  21257. #drawBitmap() {
  21258. const [parentWidth, parentHeight] = this.parentDimensions;
  21259. const {
  21260. width,
  21261. height
  21262. } = this;
  21263. const outputScale = new OutputScale();
  21264. const scaledWidth = Math.ceil(width * parentWidth * outputScale.sx);
  21265. const scaledHeight = Math.ceil(height * parentHeight * outputScale.sy);
  21266. const canvas = this.#canvas;
  21267. if (!canvas || canvas.width === scaledWidth && canvas.height === scaledHeight) {
  21268. return;
  21269. }
  21270. canvas.width = scaledWidth;
  21271. canvas.height = scaledHeight;
  21272. const bitmap = this.#isSvg ? this.#bitmap : this.#scaleBitmap(scaledWidth, scaledHeight);
  21273. const ctx = canvas.getContext("2d");
  21274. ctx.filter = this._uiManager.hcmFilter;
  21275. ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, 0, 0, scaledWidth, scaledHeight);
  21276. }
  21277. #serializeBitmap(toUrl) {
  21278. if (toUrl) {
  21279. if (this.#isSvg) {
  21280. const url = this._uiManager.imageManager.getSvgUrl(this.#bitmapId);
  21281. if (url) {
  21282. return url;
  21283. }
  21284. }
  21285. const canvas = document.createElement("canvas");
  21286. ({
  21287. width: canvas.width,
  21288. height: canvas.height
  21289. } = this.#bitmap);
  21290. const ctx = canvas.getContext("2d");
  21291. ctx.drawImage(this.#bitmap, 0, 0);
  21292. return canvas.toDataURL();
  21293. }
  21294. if (this.#isSvg) {
  21295. const [pageWidth, pageHeight] = this.pageDimensions;
  21296. const width = Math.round(this.width * pageWidth * PixelsPerInch.PDF_TO_CSS_UNITS);
  21297. const height = Math.round(this.height * pageHeight * PixelsPerInch.PDF_TO_CSS_UNITS);
  21298. const offscreen = new OffscreenCanvas(width, height);
  21299. const ctx = offscreen.getContext("2d");
  21300. ctx.drawImage(this.#bitmap, 0, 0, this.#bitmap.width, this.#bitmap.height, 0, 0, width, height);
  21301. return offscreen.transferToImageBitmap();
  21302. }
  21303. return structuredClone(this.#bitmap);
  21304. }
  21305. static async deserialize(data, parent, uiManager) {
  21306. let initialData = null;
  21307. let missingCanvas = false;
  21308. if (data instanceof StampAnnotationElement) {
  21309. const {
  21310. data: {
  21311. rect,
  21312. rotation,
  21313. id,
  21314. structParent,
  21315. popupRef
  21316. },
  21317. container,
  21318. parent: {
  21319. page: {
  21320. pageNumber
  21321. }
  21322. },
  21323. canvas
  21324. } = data;
  21325. let bitmapId, bitmap;
  21326. if (canvas) {
  21327. delete data.canvas;
  21328. ({
  21329. id: bitmapId,
  21330. bitmap
  21331. } = uiManager.imageManager.getFromCanvas(container.id, canvas));
  21332. canvas.remove();
  21333. } else {
  21334. missingCanvas = true;
  21335. data._hasNoCanvas = true;
  21336. }
  21337. const altText = (await parent._structTree.getAriaAttributes(`${AnnotationPrefix}${id}`))?.get("aria-label") || "";
  21338. initialData = data = {
  21339. annotationType: AnnotationEditorType.STAMP,
  21340. bitmapId,
  21341. bitmap,
  21342. pageIndex: pageNumber - 1,
  21343. rect: rect.slice(0),
  21344. rotation,
  21345. id,
  21346. deleted: false,
  21347. accessibilityData: {
  21348. decorative: false,
  21349. altText
  21350. },
  21351. isSvg: false,
  21352. structParent,
  21353. popupRef
  21354. };
  21355. }
  21356. const editor = await super.deserialize(data, parent, uiManager);
  21357. const {
  21358. rect,
  21359. bitmap,
  21360. bitmapUrl,
  21361. bitmapId,
  21362. isSvg,
  21363. accessibilityData
  21364. } = data;
  21365. if (missingCanvas) {
  21366. uiManager.addMissingCanvas(data.id, editor);
  21367. editor.#missingCanvas = true;
  21368. } else if (bitmapId && uiManager.imageManager.isValidId(bitmapId)) {
  21369. editor.#bitmapId = bitmapId;
  21370. if (bitmap) {
  21371. editor.#bitmap = bitmap;
  21372. }
  21373. } else {
  21374. editor.#bitmapUrl = bitmapUrl;
  21375. }
  21376. editor.#isSvg = isSvg;
  21377. const [parentWidth, parentHeight] = editor.pageDimensions;
  21378. editor.width = (rect[2] - rect[0]) / parentWidth;
  21379. editor.height = (rect[3] - rect[1]) / parentHeight;
  21380. editor.annotationElementId = data.id || null;
  21381. if (accessibilityData) {
  21382. editor.altTextData = accessibilityData;
  21383. }
  21384. editor._initialData = initialData;
  21385. editor.#hasBeenAddedInUndoStack = !!initialData;
  21386. return editor;
  21387. }
  21388. serialize(isForCopying = false, context = null) {
  21389. if (this.isEmpty()) {
  21390. return null;
  21391. }
  21392. if (this.deleted) {
  21393. return this.serializeDeleted();
  21394. }
  21395. const serialized = {
  21396. annotationType: AnnotationEditorType.STAMP,
  21397. bitmapId: this.#bitmapId,
  21398. pageIndex: this.pageIndex,
  21399. rect: this.getRect(0, 0),
  21400. rotation: this.rotation,
  21401. isSvg: this.#isSvg,
  21402. structTreeParentId: this._structTreeParentId
  21403. };
  21404. if (isForCopying) {
  21405. serialized.bitmapUrl = this.#serializeBitmap(true);
  21406. serialized.accessibilityData = this.serializeAltText(true);
  21407. serialized.isCopy = true;
  21408. return serialized;
  21409. }
  21410. const {
  21411. decorative,
  21412. altText
  21413. } = this.serializeAltText(false);
  21414. if (!decorative && altText) {
  21415. serialized.accessibilityData = {
  21416. type: "Figure",
  21417. alt: altText
  21418. };
  21419. }
  21420. if (this.annotationElementId) {
  21421. const changes = this.#hasElementChanged(serialized);
  21422. if (changes.isSame) {
  21423. return null;
  21424. }
  21425. if (changes.isSameAltText) {
  21426. delete serialized.accessibilityData;
  21427. } else {
  21428. serialized.accessibilityData.structParent = this._initialData.structParent ?? -1;
  21429. }
  21430. }
  21431. serialized.id = this.annotationElementId;
  21432. if (context === null) {
  21433. return serialized;
  21434. }
  21435. context.stamps ||= new Map();
  21436. const area = this.#isSvg ? (serialized.rect[2] - serialized.rect[0]) * (serialized.rect[3] - serialized.rect[1]) : null;
  21437. if (!context.stamps.has(this.#bitmapId)) {
  21438. context.stamps.set(this.#bitmapId, {
  21439. area,
  21440. serialized
  21441. });
  21442. serialized.bitmap = this.#serializeBitmap(false);
  21443. } else if (this.#isSvg) {
  21444. const prevData = context.stamps.get(this.#bitmapId);
  21445. if (area > prevData.area) {
  21446. prevData.area = area;
  21447. prevData.serialized.bitmap.close();
  21448. prevData.serialized.bitmap = this.#serializeBitmap(false);
  21449. }
  21450. }
  21451. return serialized;
  21452. }
  21453. #hasElementChanged(serialized) {
  21454. const {
  21455. pageIndex,
  21456. accessibilityData: {
  21457. altText
  21458. }
  21459. } = this._initialData;
  21460. const isSamePageIndex = serialized.pageIndex === pageIndex;
  21461. const isSameAltText = (serialized.accessibilityData?.alt || "") === altText;
  21462. return {
  21463. isSame: !this._hasBeenMoved && !this._hasBeenResized && isSamePageIndex && isSameAltText,
  21464. isSameAltText
  21465. };
  21466. }
  21467. renderAnnotationElement(annotation) {
  21468. annotation.updateEdited({
  21469. rect: this.getRect(0, 0)
  21470. });
  21471. return null;
  21472. }
  21473. }
  21474. ;// ./src/display/editor/annotation_editor_layer.js
  21475. class AnnotationEditorLayer {
  21476. #accessibilityManager;
  21477. #allowClick = false;
  21478. #annotationLayer = null;
  21479. #clickAC = null;
  21480. #editorFocusTimeoutId = null;
  21481. #editors = new Map();
  21482. #hadPointerDown = false;
  21483. #isDisabling = false;
  21484. #isEnabling = false;
  21485. #drawingAC = null;
  21486. #focusedElement = null;
  21487. #textLayer = null;
  21488. #textSelectionAC = null;
  21489. #uiManager;
  21490. static _initialized = false;
  21491. static #editorTypes = new Map([FreeTextEditor, InkEditor, StampEditor, HighlightEditor, SignatureEditor].map(type => [type._editorType, type]));
  21492. constructor({
  21493. uiManager,
  21494. pageIndex,
  21495. div,
  21496. structTreeLayer,
  21497. accessibilityManager,
  21498. annotationLayer,
  21499. drawLayer,
  21500. textLayer,
  21501. viewport,
  21502. l10n
  21503. }) {
  21504. const editorTypes = [...AnnotationEditorLayer.#editorTypes.values()];
  21505. if (!AnnotationEditorLayer._initialized) {
  21506. AnnotationEditorLayer._initialized = true;
  21507. for (const editorType of editorTypes) {
  21508. editorType.initialize(l10n, uiManager);
  21509. }
  21510. }
  21511. uiManager.registerEditorTypes(editorTypes);
  21512. this.#uiManager = uiManager;
  21513. this.pageIndex = pageIndex;
  21514. this.div = div;
  21515. this.#accessibilityManager = accessibilityManager;
  21516. this.#annotationLayer = annotationLayer;
  21517. this.viewport = viewport;
  21518. this.#textLayer = textLayer;
  21519. this.drawLayer = drawLayer;
  21520. this._structTree = structTreeLayer;
  21521. this.#uiManager.addLayer(this);
  21522. }
  21523. get isEmpty() {
  21524. return this.#editors.size === 0;
  21525. }
  21526. get isInvisible() {
  21527. return this.isEmpty && this.#uiManager.getMode() === AnnotationEditorType.NONE;
  21528. }
  21529. updateToolbar(mode) {
  21530. this.#uiManager.updateToolbar(mode);
  21531. }
  21532. updateMode(mode = this.#uiManager.getMode()) {
  21533. this.#cleanup();
  21534. switch (mode) {
  21535. case AnnotationEditorType.NONE:
  21536. this.disableTextSelection();
  21537. this.togglePointerEvents(false);
  21538. this.toggleAnnotationLayerPointerEvents(true);
  21539. this.disableClick();
  21540. return;
  21541. case AnnotationEditorType.INK:
  21542. this.disableTextSelection();
  21543. this.togglePointerEvents(true);
  21544. this.enableClick();
  21545. break;
  21546. case AnnotationEditorType.HIGHLIGHT:
  21547. this.enableTextSelection();
  21548. this.togglePointerEvents(false);
  21549. this.disableClick();
  21550. break;
  21551. default:
  21552. this.disableTextSelection();
  21553. this.togglePointerEvents(true);
  21554. this.enableClick();
  21555. }
  21556. this.toggleAnnotationLayerPointerEvents(false);
  21557. const {
  21558. classList
  21559. } = this.div;
  21560. for (const editorType of AnnotationEditorLayer.#editorTypes.values()) {
  21561. classList.toggle(`${editorType._type}Editing`, mode === editorType._editorType);
  21562. }
  21563. this.div.hidden = false;
  21564. }
  21565. hasTextLayer(textLayer) {
  21566. return textLayer === this.#textLayer?.div;
  21567. }
  21568. setEditingState(isEditing) {
  21569. this.#uiManager.setEditingState(isEditing);
  21570. }
  21571. addCommands(params) {
  21572. this.#uiManager.addCommands(params);
  21573. }
  21574. cleanUndoStack(type) {
  21575. this.#uiManager.cleanUndoStack(type);
  21576. }
  21577. toggleDrawing(enabled = false) {
  21578. this.div.classList.toggle("drawing", !enabled);
  21579. }
  21580. togglePointerEvents(enabled = false) {
  21581. this.div.classList.toggle("disabled", !enabled);
  21582. }
  21583. toggleAnnotationLayerPointerEvents(enabled = false) {
  21584. this.#annotationLayer?.div.classList.toggle("disabled", !enabled);
  21585. }
  21586. async enable() {
  21587. this.#isEnabling = true;
  21588. this.div.tabIndex = 0;
  21589. this.togglePointerEvents(true);
  21590. const annotationElementIds = new Set();
  21591. for (const editor of this.#editors.values()) {
  21592. editor.enableEditing();
  21593. editor.show(true);
  21594. if (editor.annotationElementId) {
  21595. this.#uiManager.removeChangedExistingAnnotation(editor);
  21596. annotationElementIds.add(editor.annotationElementId);
  21597. }
  21598. }
  21599. if (!this.#annotationLayer) {
  21600. this.#isEnabling = false;
  21601. return;
  21602. }
  21603. const editables = this.#annotationLayer.getEditableAnnotations();
  21604. for (const editable of editables) {
  21605. editable.hide();
  21606. if (this.#uiManager.isDeletedAnnotationElement(editable.data.id)) {
  21607. continue;
  21608. }
  21609. if (annotationElementIds.has(editable.data.id)) {
  21610. continue;
  21611. }
  21612. const editor = await this.deserialize(editable);
  21613. if (!editor) {
  21614. continue;
  21615. }
  21616. this.addOrRebuild(editor);
  21617. editor.enableEditing();
  21618. }
  21619. this.#isEnabling = false;
  21620. }
  21621. disable() {
  21622. this.#isDisabling = true;
  21623. this.div.tabIndex = -1;
  21624. this.togglePointerEvents(false);
  21625. const changedAnnotations = new Map();
  21626. const resetAnnotations = new Map();
  21627. for (const editor of this.#editors.values()) {
  21628. editor.disableEditing();
  21629. if (!editor.annotationElementId) {
  21630. continue;
  21631. }
  21632. if (editor.serialize() !== null) {
  21633. changedAnnotations.set(editor.annotationElementId, editor);
  21634. continue;
  21635. } else {
  21636. resetAnnotations.set(editor.annotationElementId, editor);
  21637. }
  21638. this.getEditableAnnotation(editor.annotationElementId)?.show();
  21639. editor.remove();
  21640. }
  21641. if (this.#annotationLayer) {
  21642. const editables = this.#annotationLayer.getEditableAnnotations();
  21643. for (const editable of editables) {
  21644. const {
  21645. id
  21646. } = editable.data;
  21647. if (this.#uiManager.isDeletedAnnotationElement(id)) {
  21648. continue;
  21649. }
  21650. let editor = resetAnnotations.get(id);
  21651. if (editor) {
  21652. editor.resetAnnotationElement(editable);
  21653. editor.show(false);
  21654. editable.show();
  21655. continue;
  21656. }
  21657. editor = changedAnnotations.get(id);
  21658. if (editor) {
  21659. this.#uiManager.addChangedExistingAnnotation(editor);
  21660. if (editor.renderAnnotationElement(editable)) {
  21661. editor.show(false);
  21662. }
  21663. }
  21664. editable.show();
  21665. }
  21666. }
  21667. this.#cleanup();
  21668. if (this.isEmpty) {
  21669. this.div.hidden = true;
  21670. }
  21671. const {
  21672. classList
  21673. } = this.div;
  21674. for (const editorType of AnnotationEditorLayer.#editorTypes.values()) {
  21675. classList.remove(`${editorType._type}Editing`);
  21676. }
  21677. this.disableTextSelection();
  21678. this.toggleAnnotationLayerPointerEvents(true);
  21679. this.#isDisabling = false;
  21680. }
  21681. getEditableAnnotation(id) {
  21682. return this.#annotationLayer?.getEditableAnnotation(id) || null;
  21683. }
  21684. setActiveEditor(editor) {
  21685. const currentActive = this.#uiManager.getActive();
  21686. if (currentActive === editor) {
  21687. return;
  21688. }
  21689. this.#uiManager.setActiveEditor(editor);
  21690. }
  21691. enableTextSelection() {
  21692. this.div.tabIndex = -1;
  21693. if (this.#textLayer?.div && !this.#textSelectionAC) {
  21694. this.#textSelectionAC = new AbortController();
  21695. const signal = this.#uiManager.combinedSignal(this.#textSelectionAC);
  21696. this.#textLayer.div.addEventListener("pointerdown", this.#textLayerPointerDown.bind(this), {
  21697. signal
  21698. });
  21699. this.#textLayer.div.classList.add("highlighting");
  21700. }
  21701. }
  21702. disableTextSelection() {
  21703. this.div.tabIndex = 0;
  21704. if (this.#textLayer?.div && this.#textSelectionAC) {
  21705. this.#textSelectionAC.abort();
  21706. this.#textSelectionAC = null;
  21707. this.#textLayer.div.classList.remove("highlighting");
  21708. }
  21709. }
  21710. #textLayerPointerDown(event) {
  21711. this.#uiManager.unselectAll();
  21712. const {
  21713. target
  21714. } = event;
  21715. if (target === this.#textLayer.div || (target.getAttribute("role") === "img" || target.classList.contains("endOfContent")) && this.#textLayer.div.contains(target)) {
  21716. const {
  21717. isMac
  21718. } = util_FeatureTest.platform;
  21719. if (event.button !== 0 || event.ctrlKey && isMac) {
  21720. return;
  21721. }
  21722. this.#uiManager.showAllEditors("highlight", true, true);
  21723. this.#textLayer.div.classList.add("free");
  21724. this.toggleDrawing();
  21725. HighlightEditor.startHighlighting(this, this.#uiManager.direction === "ltr", {
  21726. target: this.#textLayer.div,
  21727. x: event.x,
  21728. y: event.y
  21729. });
  21730. this.#textLayer.div.addEventListener("pointerup", () => {
  21731. this.#textLayer.div.classList.remove("free");
  21732. this.toggleDrawing(true);
  21733. }, {
  21734. once: true,
  21735. signal: this.#uiManager._signal
  21736. });
  21737. event.preventDefault();
  21738. }
  21739. }
  21740. enableClick() {
  21741. if (this.#clickAC) {
  21742. return;
  21743. }
  21744. this.#clickAC = new AbortController();
  21745. const signal = this.#uiManager.combinedSignal(this.#clickAC);
  21746. this.div.addEventListener("pointerdown", this.pointerdown.bind(this), {
  21747. signal
  21748. });
  21749. const pointerup = this.pointerup.bind(this);
  21750. this.div.addEventListener("pointerup", pointerup, {
  21751. signal
  21752. });
  21753. this.div.addEventListener("pointercancel", pointerup, {
  21754. signal
  21755. });
  21756. }
  21757. disableClick() {
  21758. this.#clickAC?.abort();
  21759. this.#clickAC = null;
  21760. }
  21761. attach(editor) {
  21762. this.#editors.set(editor.id, editor);
  21763. const {
  21764. annotationElementId
  21765. } = editor;
  21766. if (annotationElementId && this.#uiManager.isDeletedAnnotationElement(annotationElementId)) {
  21767. this.#uiManager.removeDeletedAnnotationElement(editor);
  21768. }
  21769. }
  21770. detach(editor) {
  21771. this.#editors.delete(editor.id);
  21772. this.#accessibilityManager?.removePointerInTextLayer(editor.contentDiv);
  21773. if (!this.#isDisabling && editor.annotationElementId) {
  21774. this.#uiManager.addDeletedAnnotationElement(editor);
  21775. }
  21776. }
  21777. remove(editor) {
  21778. this.detach(editor);
  21779. this.#uiManager.removeEditor(editor);
  21780. editor.div.remove();
  21781. editor.isAttachedToDOM = false;
  21782. }
  21783. changeParent(editor) {
  21784. if (editor.parent === this) {
  21785. return;
  21786. }
  21787. if (editor.parent && editor.annotationElementId) {
  21788. this.#uiManager.addDeletedAnnotationElement(editor.annotationElementId);
  21789. AnnotationEditor.deleteAnnotationElement(editor);
  21790. editor.annotationElementId = null;
  21791. }
  21792. this.attach(editor);
  21793. editor.parent?.detach(editor);
  21794. editor.setParent(this);
  21795. if (editor.div && editor.isAttachedToDOM) {
  21796. editor.div.remove();
  21797. this.div.append(editor.div);
  21798. }
  21799. }
  21800. add(editor) {
  21801. if (editor.parent === this && editor.isAttachedToDOM) {
  21802. return;
  21803. }
  21804. this.changeParent(editor);
  21805. this.#uiManager.addEditor(editor);
  21806. this.attach(editor);
  21807. if (!editor.isAttachedToDOM) {
  21808. const div = editor.render();
  21809. this.div.append(div);
  21810. editor.isAttachedToDOM = true;
  21811. }
  21812. editor.fixAndSetPosition();
  21813. editor.onceAdded(!this.#isEnabling);
  21814. this.#uiManager.addToAnnotationStorage(editor);
  21815. editor._reportTelemetry(editor.telemetryInitialData);
  21816. }
  21817. moveEditorInDOM(editor) {
  21818. if (!editor.isAttachedToDOM) {
  21819. return;
  21820. }
  21821. const {
  21822. activeElement
  21823. } = document;
  21824. if (editor.div.contains(activeElement) && !this.#editorFocusTimeoutId) {
  21825. editor._focusEventsAllowed = false;
  21826. this.#editorFocusTimeoutId = setTimeout(() => {
  21827. this.#editorFocusTimeoutId = null;
  21828. if (!editor.div.contains(document.activeElement)) {
  21829. editor.div.addEventListener("focusin", () => {
  21830. editor._focusEventsAllowed = true;
  21831. }, {
  21832. once: true,
  21833. signal: this.#uiManager._signal
  21834. });
  21835. activeElement.focus();
  21836. } else {
  21837. editor._focusEventsAllowed = true;
  21838. }
  21839. }, 0);
  21840. }
  21841. editor._structTreeParentId = this.#accessibilityManager?.moveElementInDOM(this.div, editor.div, editor.contentDiv, true);
  21842. }
  21843. addOrRebuild(editor) {
  21844. if (editor.needsToBeRebuilt()) {
  21845. editor.parent ||= this;
  21846. editor.rebuild();
  21847. editor.show();
  21848. } else {
  21849. this.add(editor);
  21850. }
  21851. }
  21852. addUndoableEditor(editor) {
  21853. const cmd = () => editor._uiManager.rebuild(editor);
  21854. const undo = () => {
  21855. editor.remove();
  21856. };
  21857. this.addCommands({
  21858. cmd,
  21859. undo,
  21860. mustExec: false
  21861. });
  21862. }
  21863. getNextId() {
  21864. return this.#uiManager.getId();
  21865. }
  21866. get #currentEditorType() {
  21867. return AnnotationEditorLayer.#editorTypes.get(this.#uiManager.getMode());
  21868. }
  21869. combinedSignal(ac) {
  21870. return this.#uiManager.combinedSignal(ac);
  21871. }
  21872. #createNewEditor(params) {
  21873. const editorType = this.#currentEditorType;
  21874. return editorType ? new editorType.prototype.constructor(params) : null;
  21875. }
  21876. canCreateNewEmptyEditor() {
  21877. return this.#currentEditorType?.canCreateNewEmptyEditor();
  21878. }
  21879. async pasteEditor(mode, params) {
  21880. this.#uiManager.updateToolbar(mode);
  21881. await this.#uiManager.updateMode(mode);
  21882. const {
  21883. offsetX,
  21884. offsetY
  21885. } = this.#getCenterPoint();
  21886. const id = this.getNextId();
  21887. const editor = this.#createNewEditor({
  21888. parent: this,
  21889. id,
  21890. x: offsetX,
  21891. y: offsetY,
  21892. uiManager: this.#uiManager,
  21893. isCentered: true,
  21894. ...params
  21895. });
  21896. if (editor) {
  21897. this.add(editor);
  21898. }
  21899. }
  21900. async deserialize(data) {
  21901. return (await AnnotationEditorLayer.#editorTypes.get(data.annotationType ?? data.annotationEditorType)?.deserialize(data, this, this.#uiManager)) || null;
  21902. }
  21903. createAndAddNewEditor(event, isCentered, data = {}) {
  21904. const id = this.getNextId();
  21905. const editor = this.#createNewEditor({
  21906. parent: this,
  21907. id,
  21908. x: event.offsetX,
  21909. y: event.offsetY,
  21910. uiManager: this.#uiManager,
  21911. isCentered,
  21912. ...data
  21913. });
  21914. if (editor) {
  21915. this.add(editor);
  21916. }
  21917. return editor;
  21918. }
  21919. #getCenterPoint() {
  21920. const {
  21921. x,
  21922. y,
  21923. width,
  21924. height
  21925. } = this.div.getBoundingClientRect();
  21926. const tlX = Math.max(0, x);
  21927. const tlY = Math.max(0, y);
  21928. const brX = Math.min(window.innerWidth, x + width);
  21929. const brY = Math.min(window.innerHeight, y + height);
  21930. const centerX = (tlX + brX) / 2 - x;
  21931. const centerY = (tlY + brY) / 2 - y;
  21932. const [offsetX, offsetY] = this.viewport.rotation % 180 === 0 ? [centerX, centerY] : [centerY, centerX];
  21933. return {
  21934. offsetX,
  21935. offsetY
  21936. };
  21937. }
  21938. addNewEditor(data = {}) {
  21939. this.createAndAddNewEditor(this.#getCenterPoint(), true, data);
  21940. }
  21941. setSelected(editor) {
  21942. this.#uiManager.setSelected(editor);
  21943. }
  21944. toggleSelected(editor) {
  21945. this.#uiManager.toggleSelected(editor);
  21946. }
  21947. unselect(editor) {
  21948. this.#uiManager.unselect(editor);
  21949. }
  21950. pointerup(event) {
  21951. const {
  21952. isMac
  21953. } = util_FeatureTest.platform;
  21954. if (event.button !== 0 || event.ctrlKey && isMac) {
  21955. return;
  21956. }
  21957. if (event.target !== this.div) {
  21958. return;
  21959. }
  21960. if (!this.#hadPointerDown) {
  21961. return;
  21962. }
  21963. this.#hadPointerDown = false;
  21964. if (this.#currentEditorType?.isDrawer && this.#currentEditorType.supportMultipleDrawings) {
  21965. return;
  21966. }
  21967. if (!this.#allowClick) {
  21968. this.#allowClick = true;
  21969. return;
  21970. }
  21971. const currentMode = this.#uiManager.getMode();
  21972. if (currentMode === AnnotationEditorType.STAMP || currentMode === AnnotationEditorType.SIGNATURE) {
  21973. this.#uiManager.unselectAll();
  21974. return;
  21975. }
  21976. this.createAndAddNewEditor(event, false);
  21977. }
  21978. pointerdown(event) {
  21979. if (this.#uiManager.getMode() === AnnotationEditorType.HIGHLIGHT) {
  21980. this.enableTextSelection();
  21981. }
  21982. if (this.#hadPointerDown) {
  21983. this.#hadPointerDown = false;
  21984. return;
  21985. }
  21986. const {
  21987. isMac
  21988. } = util_FeatureTest.platform;
  21989. if (event.button !== 0 || event.ctrlKey && isMac) {
  21990. return;
  21991. }
  21992. if (event.target !== this.div) {
  21993. return;
  21994. }
  21995. this.#hadPointerDown = true;
  21996. if (this.#currentEditorType?.isDrawer) {
  21997. this.startDrawingSession(event);
  21998. return;
  21999. }
  22000. const editor = this.#uiManager.getActive();
  22001. this.#allowClick = !editor || editor.isEmpty();
  22002. }
  22003. startDrawingSession(event) {
  22004. this.div.focus({
  22005. preventScroll: true
  22006. });
  22007. if (this.#drawingAC) {
  22008. this.#currentEditorType.startDrawing(this, this.#uiManager, false, event);
  22009. return;
  22010. }
  22011. this.#uiManager.setCurrentDrawingSession(this);
  22012. this.#drawingAC = new AbortController();
  22013. const signal = this.#uiManager.combinedSignal(this.#drawingAC);
  22014. this.div.addEventListener("blur", ({
  22015. relatedTarget
  22016. }) => {
  22017. if (relatedTarget && !this.div.contains(relatedTarget)) {
  22018. this.#focusedElement = null;
  22019. this.commitOrRemove();
  22020. }
  22021. }, {
  22022. signal
  22023. });
  22024. this.#currentEditorType.startDrawing(this, this.#uiManager, false, event);
  22025. }
  22026. pause(on) {
  22027. if (on) {
  22028. const {
  22029. activeElement
  22030. } = document;
  22031. if (this.div.contains(activeElement)) {
  22032. this.#focusedElement = activeElement;
  22033. }
  22034. return;
  22035. }
  22036. if (this.#focusedElement) {
  22037. setTimeout(() => {
  22038. this.#focusedElement?.focus();
  22039. this.#focusedElement = null;
  22040. }, 0);
  22041. }
  22042. }
  22043. endDrawingSession(isAborted = false) {
  22044. if (!this.#drawingAC) {
  22045. return null;
  22046. }
  22047. this.#uiManager.setCurrentDrawingSession(null);
  22048. this.#drawingAC.abort();
  22049. this.#drawingAC = null;
  22050. this.#focusedElement = null;
  22051. return this.#currentEditorType.endDrawing(isAborted);
  22052. }
  22053. findNewParent(editor, x, y) {
  22054. const layer = this.#uiManager.findParent(x, y);
  22055. if (layer === null || layer === this) {
  22056. return false;
  22057. }
  22058. layer.changeParent(editor);
  22059. return true;
  22060. }
  22061. commitOrRemove() {
  22062. if (this.#drawingAC) {
  22063. this.endDrawingSession();
  22064. return true;
  22065. }
  22066. return false;
  22067. }
  22068. onScaleChanging() {
  22069. if (!this.#drawingAC) {
  22070. return;
  22071. }
  22072. this.#currentEditorType.onScaleChangingWhenDrawing(this);
  22073. }
  22074. destroy() {
  22075. this.commitOrRemove();
  22076. if (this.#uiManager.getActive()?.parent === this) {
  22077. this.#uiManager.commitOrRemove();
  22078. this.#uiManager.setActiveEditor(null);
  22079. }
  22080. if (this.#editorFocusTimeoutId) {
  22081. clearTimeout(this.#editorFocusTimeoutId);
  22082. this.#editorFocusTimeoutId = null;
  22083. }
  22084. for (const editor of this.#editors.values()) {
  22085. this.#accessibilityManager?.removePointerInTextLayer(editor.contentDiv);
  22086. editor.setParent(null);
  22087. editor.isAttachedToDOM = false;
  22088. editor.div.remove();
  22089. }
  22090. this.div = null;
  22091. this.#editors.clear();
  22092. this.#uiManager.removeLayer(this);
  22093. }
  22094. #cleanup() {
  22095. for (const editor of this.#editors.values()) {
  22096. if (editor.isEmpty()) {
  22097. editor.remove();
  22098. }
  22099. }
  22100. }
  22101. render({
  22102. viewport
  22103. }) {
  22104. this.viewport = viewport;
  22105. setLayerDimensions(this.div, viewport);
  22106. for (const editor of this.#uiManager.getEditors(this.pageIndex)) {
  22107. this.add(editor);
  22108. editor.rebuild();
  22109. }
  22110. this.updateMode();
  22111. }
  22112. update({
  22113. viewport
  22114. }) {
  22115. this.#uiManager.commitOrRemove();
  22116. this.#cleanup();
  22117. const oldRotation = this.viewport.rotation;
  22118. const rotation = viewport.rotation;
  22119. this.viewport = viewport;
  22120. setLayerDimensions(this.div, {
  22121. rotation
  22122. });
  22123. if (oldRotation !== rotation) {
  22124. for (const editor of this.#editors.values()) {
  22125. editor.rotate(rotation);
  22126. }
  22127. }
  22128. }
  22129. get pageDimensions() {
  22130. const {
  22131. pageWidth,
  22132. pageHeight
  22133. } = this.viewport.rawDims;
  22134. return [pageWidth, pageHeight];
  22135. }
  22136. get scale() {
  22137. return this.#uiManager.viewParameters.realScale;
  22138. }
  22139. }
  22140. ;// ./src/display/draw_layer.js
  22141. class DrawLayer {
  22142. #parent = null;
  22143. #mapping = new Map();
  22144. #toUpdate = new Map();
  22145. static #id = 0;
  22146. constructor({
  22147. pageIndex
  22148. }) {
  22149. this.pageIndex = pageIndex;
  22150. }
  22151. setParent(parent) {
  22152. if (!this.#parent) {
  22153. this.#parent = parent;
  22154. return;
  22155. }
  22156. if (this.#parent !== parent) {
  22157. if (this.#mapping.size > 0) {
  22158. for (const root of this.#mapping.values()) {
  22159. root.remove();
  22160. parent.append(root);
  22161. }
  22162. }
  22163. this.#parent = parent;
  22164. }
  22165. }
  22166. static get _svgFactory() {
  22167. return shadow(this, "_svgFactory", new DOMSVGFactory());
  22168. }
  22169. static #setBox(element, [x, y, width, height]) {
  22170. const {
  22171. style
  22172. } = element;
  22173. style.top = `${100 * y}%`;
  22174. style.left = `${100 * x}%`;
  22175. style.width = `${100 * width}%`;
  22176. style.height = `${100 * height}%`;
  22177. }
  22178. #createSVG() {
  22179. const svg = DrawLayer._svgFactory.create(1, 1, true);
  22180. this.#parent.append(svg);
  22181. svg.setAttribute("aria-hidden", true);
  22182. return svg;
  22183. }
  22184. #createClipPath(defs, pathId) {
  22185. const clipPath = DrawLayer._svgFactory.createElement("clipPath");
  22186. defs.append(clipPath);
  22187. const clipPathId = `clip_${pathId}`;
  22188. clipPath.setAttribute("id", clipPathId);
  22189. clipPath.setAttribute("clipPathUnits", "objectBoundingBox");
  22190. const clipPathUse = DrawLayer._svgFactory.createElement("use");
  22191. clipPath.append(clipPathUse);
  22192. clipPathUse.setAttribute("href", `#${pathId}`);
  22193. clipPathUse.classList.add("clip");
  22194. return clipPathId;
  22195. }
  22196. #updateProperties(element, properties) {
  22197. for (const [key, value] of Object.entries(properties)) {
  22198. if (value === null) {
  22199. element.removeAttribute(key);
  22200. } else {
  22201. element.setAttribute(key, value);
  22202. }
  22203. }
  22204. }
  22205. draw(properties, isPathUpdatable = false, hasClip = false) {
  22206. const id = DrawLayer.#id++;
  22207. const root = this.#createSVG();
  22208. const defs = DrawLayer._svgFactory.createElement("defs");
  22209. root.append(defs);
  22210. const path = DrawLayer._svgFactory.createElement("path");
  22211. defs.append(path);
  22212. const pathId = `path_p${this.pageIndex}_${id}`;
  22213. path.setAttribute("id", pathId);
  22214. path.setAttribute("vector-effect", "non-scaling-stroke");
  22215. if (isPathUpdatable) {
  22216. this.#toUpdate.set(id, path);
  22217. }
  22218. const clipPathId = hasClip ? this.#createClipPath(defs, pathId) : null;
  22219. const use = DrawLayer._svgFactory.createElement("use");
  22220. root.append(use);
  22221. use.setAttribute("href", `#${pathId}`);
  22222. this.updateProperties(root, properties);
  22223. this.#mapping.set(id, root);
  22224. return {
  22225. id,
  22226. clipPathId: `url(#${clipPathId})`
  22227. };
  22228. }
  22229. drawOutline(properties, mustRemoveSelfIntersections) {
  22230. const id = DrawLayer.#id++;
  22231. const root = this.#createSVG();
  22232. const defs = DrawLayer._svgFactory.createElement("defs");
  22233. root.append(defs);
  22234. const path = DrawLayer._svgFactory.createElement("path");
  22235. defs.append(path);
  22236. const pathId = `path_p${this.pageIndex}_${id}`;
  22237. path.setAttribute("id", pathId);
  22238. path.setAttribute("vector-effect", "non-scaling-stroke");
  22239. let maskId;
  22240. if (mustRemoveSelfIntersections) {
  22241. const mask = DrawLayer._svgFactory.createElement("mask");
  22242. defs.append(mask);
  22243. maskId = `mask_p${this.pageIndex}_${id}`;
  22244. mask.setAttribute("id", maskId);
  22245. mask.setAttribute("maskUnits", "objectBoundingBox");
  22246. const rect = DrawLayer._svgFactory.createElement("rect");
  22247. mask.append(rect);
  22248. rect.setAttribute("width", "1");
  22249. rect.setAttribute("height", "1");
  22250. rect.setAttribute("fill", "white");
  22251. const use = DrawLayer._svgFactory.createElement("use");
  22252. mask.append(use);
  22253. use.setAttribute("href", `#${pathId}`);
  22254. use.setAttribute("stroke", "none");
  22255. use.setAttribute("fill", "black");
  22256. use.setAttribute("fill-rule", "nonzero");
  22257. use.classList.add("mask");
  22258. }
  22259. const use1 = DrawLayer._svgFactory.createElement("use");
  22260. root.append(use1);
  22261. use1.setAttribute("href", `#${pathId}`);
  22262. if (maskId) {
  22263. use1.setAttribute("mask", `url(#${maskId})`);
  22264. }
  22265. const use2 = use1.cloneNode();
  22266. root.append(use2);
  22267. use1.classList.add("mainOutline");
  22268. use2.classList.add("secondaryOutline");
  22269. this.updateProperties(root, properties);
  22270. this.#mapping.set(id, root);
  22271. return id;
  22272. }
  22273. finalizeDraw(id, properties) {
  22274. this.#toUpdate.delete(id);
  22275. this.updateProperties(id, properties);
  22276. }
  22277. updateProperties(elementOrId, properties) {
  22278. if (!properties) {
  22279. return;
  22280. }
  22281. const {
  22282. root,
  22283. bbox,
  22284. rootClass,
  22285. path
  22286. } = properties;
  22287. const element = typeof elementOrId === "number" ? this.#mapping.get(elementOrId) : elementOrId;
  22288. if (!element) {
  22289. return;
  22290. }
  22291. if (root) {
  22292. this.#updateProperties(element, root);
  22293. }
  22294. if (bbox) {
  22295. DrawLayer.#setBox(element, bbox);
  22296. }
  22297. if (rootClass) {
  22298. const {
  22299. classList
  22300. } = element;
  22301. for (const [className, value] of Object.entries(rootClass)) {
  22302. classList.toggle(className, value);
  22303. }
  22304. }
  22305. if (path) {
  22306. const defs = element.firstChild;
  22307. const pathElement = defs.firstChild;
  22308. this.#updateProperties(pathElement, path);
  22309. }
  22310. }
  22311. updateParent(id, layer) {
  22312. if (layer === this) {
  22313. return;
  22314. }
  22315. const root = this.#mapping.get(id);
  22316. if (!root) {
  22317. return;
  22318. }
  22319. layer.#parent.append(root);
  22320. this.#mapping.delete(id);
  22321. layer.#mapping.set(id, root);
  22322. }
  22323. remove(id) {
  22324. this.#toUpdate.delete(id);
  22325. if (this.#parent === null) {
  22326. return;
  22327. }
  22328. this.#mapping.get(id).remove();
  22329. this.#mapping.delete(id);
  22330. }
  22331. destroy() {
  22332. this.#parent = null;
  22333. for (const root of this.#mapping.values()) {
  22334. root.remove();
  22335. }
  22336. this.#mapping.clear();
  22337. this.#toUpdate.clear();
  22338. }
  22339. }
  22340. ;// ./src/pdf.js
  22341. const pdfjsVersion = "5.2.133";
  22342. const pdfjsBuild = "4f7761353";
  22343. {
  22344. globalThis.pdfjsTestingUtils = {
  22345. HighlightOutliner: HighlightOutliner
  22346. };
  22347. }
  22348. globalThis.pdfjsLib = {
  22349. AbortException: AbortException,
  22350. AnnotationEditorLayer: AnnotationEditorLayer,
  22351. AnnotationEditorParamsType: AnnotationEditorParamsType,
  22352. AnnotationEditorType: AnnotationEditorType,
  22353. AnnotationEditorUIManager: AnnotationEditorUIManager,
  22354. AnnotationLayer: AnnotationLayer,
  22355. AnnotationMode: AnnotationMode,
  22356. AnnotationType: AnnotationType,
  22357. build: build,
  22358. ColorPicker: ColorPicker,
  22359. createValidAbsoluteUrl: createValidAbsoluteUrl,
  22360. DOMSVGFactory: DOMSVGFactory,
  22361. DrawLayer: DrawLayer,
  22362. FeatureTest: util_FeatureTest,
  22363. fetchData: fetchData,
  22364. getDocument: getDocument,
  22365. getFilenameFromUrl: getFilenameFromUrl,
  22366. getPdfFilenameFromUrl: getPdfFilenameFromUrl,
  22367. getUuid: getUuid,
  22368. getXfaPageViewport: getXfaPageViewport,
  22369. GlobalWorkerOptions: GlobalWorkerOptions,
  22370. ImageKind: util_ImageKind,
  22371. InvalidPDFException: InvalidPDFException,
  22372. isDataScheme: isDataScheme,
  22373. isPdfFile: isPdfFile,
  22374. isValidExplicitDest: isValidExplicitDest,
  22375. MathClamp: MathClamp,
  22376. noContextMenu: noContextMenu,
  22377. normalizeUnicode: normalizeUnicode,
  22378. OPS: OPS,
  22379. OutputScale: OutputScale,
  22380. PasswordResponses: PasswordResponses,
  22381. PDFDataRangeTransport: PDFDataRangeTransport,
  22382. PDFDateString: PDFDateString,
  22383. PDFWorker: PDFWorker,
  22384. PermissionFlag: PermissionFlag,
  22385. PixelsPerInch: PixelsPerInch,
  22386. RenderingCancelledException: RenderingCancelledException,
  22387. ResponseException: ResponseException,
  22388. setLayerDimensions: setLayerDimensions,
  22389. shadow: shadow,
  22390. SignatureExtractor: SignatureExtractor,
  22391. stopEvent: stopEvent,
  22392. SupportedImageMimeTypes: SupportedImageMimeTypes,
  22393. TextLayer: TextLayer,
  22394. TouchManager: TouchManager,
  22395. updateUrlHash: updateUrlHash,
  22396. Util: Util,
  22397. VerbosityLevel: VerbosityLevel,
  22398. version: version,
  22399. XfaLayer: XfaLayer
  22400. };
  22401. export { AbortException, AnnotationEditorLayer, AnnotationEditorParamsType, AnnotationEditorType, AnnotationEditorUIManager, AnnotationLayer, AnnotationMode, AnnotationType, ColorPicker, DOMSVGFactory, DrawLayer, util_FeatureTest as FeatureTest, GlobalWorkerOptions, util_ImageKind as ImageKind, InvalidPDFException, MathClamp, OPS, OutputScale, PDFDataRangeTransport, PDFDateString, PDFWorker, PasswordResponses, PermissionFlag, PixelsPerInch, RenderingCancelledException, ResponseException, SignatureExtractor, SupportedImageMimeTypes, TextLayer, TouchManager, Util, VerbosityLevel, XfaLayer, build, createValidAbsoluteUrl, fetchData, getDocument, getFilenameFromUrl, getPdfFilenameFromUrl, getUuid, getXfaPageViewport, isDataScheme, isPdfFile, isValidExplicitDest, noContextMenu, normalizeUnicode, setLayerDimensions, shadow, stopEvent, updateUrlHash, version };
  22402. //# sourceMappingURL=pdf.mjs.map