browser.c 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836
  1. /* browser: a small HTML/CSS browser built on ngui, the image library and the
  2. * shared uhttp client.
  3. *
  4. * Inline JavaScript runs through the vendored MuJS engine (console.log,
  5. * alert, document.getElementById(...).textContent and location.href); forms
  6. * are not supported. External CSS is loaded over uhttp, and images are
  7. * decoded with img.c and drawn inline. Page-scoped buffers (page body,
  8. * rules and image bitmaps) come from the ring-3 libc heap; each image gets an
  9. * exact-size decode arena that is freed as soon as the scaled bitmap exists.
  10. * The DOM and the image/CSS I/O scratch use fixed static arenas.
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <math.h>
  16. #include <ucmd_heap.h>
  17. #include "ucmd_lib.h"
  18. #include "ngui.h"
  19. #include "browser_html.h"
  20. #include "js_browser.h"
  21. #include "uhttp.h"
  22. #include "img.h"
  23. #include "inflate.h"
  24. #include "ufont.h"
  25. /* Start wide enough for the desktop nav; smaller screens fall back to
  26. * 960x640 and 780x540, which still fit a 1024x768 panel. */
  27. #define BR_W 1100
  28. #define BR_H 700
  29. #define BR_TOOLBAR_H 30
  30. #define BR_STATUS_H 18
  31. #define BR_PAGE_MAX (2u * 1024u * 1024u)
  32. #define BR_IMG_FILE_MAX (512u * 1024u)
  33. #define BR_IMG_PIX_MAX (8u * 1024u * 1024u)
  34. #define BR_MAX_IMAGES 16
  35. #define BR_IMG_ARENA_MAX (24u * 1024u * 1024u)
  36. #define BR_IMG_LAYOUT_W 1000
  37. #define BR_CSS_MAX (832u * 1024u)
  38. #define BR_CSS_COMP_MAX (192u * 1024u)
  39. #define BR_SCRIPT_MAX (512u * 1024u)
  40. #define FETCH_ABORT (-2)
  41. /* Session cache for remote stylesheets (per launch; cleared at startup). */
  42. #define CSS_CACHE_MAX 16
  43. #define CSS_CACHE_FILE_MAX (256u * 1024u)
  44. #define CSS_CACHE_PREFIX "bcss-"
  45. #define SCRIPT_CACHE_MAX 16
  46. #define SCRIPT_CACHE_FILE_MAX (512u * 1024u)
  47. #define SCRIPT_CACHE_PREFIX "bjs-"
  48. #define BR_MAX_SHEETS 6
  49. #define BR_HISTORY 16
  50. #define BR_URL_MAX 512
  51. #define BR_STATUS_MAX 200
  52. #define BR_IMG_RGB565(r, g, b) \
  53. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  54. #define BR_GRAY 0x8410
  55. enum {
  56. ID_URL = 1,
  57. ID_GO,
  58. ID_BACK,
  59. ID_FORWARD,
  60. ID_RELOAD,
  61. ID_HOME
  62. };
  63. #define ACT_NAV (1u << 0)
  64. #define ACT_BACK (1u << 1)
  65. #define ACT_FORWARD (1u << 2)
  66. #define ACT_RELOAD (1u << 3)
  67. #define ACT_HOME (1u << 4)
  68. #define ACT_CLOSE (1u << 5)
  69. #define ACT_WAIT_NAV (ACT_NAV | ACT_BACK | ACT_FORWARD | ACT_RELOAD | \
  70. ACT_HOME | ACT_CLOSE)
  71. struct br_image {
  72. int state; /* 0 empty, 1 ready */
  73. struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */
  74. int native_w, native_h;
  75. char err[32];
  76. };
  77. static struct ngui ui;
  78. static struct ngui_input url_in;
  79. static struct ngui_scroll page_scroll;
  80. static struct br_doc doc;
  81. static struct br_image images[BR_MAX_IMAGES];
  82. static int image_count;
  83. /* Painted border boxes, in page/screen coordinates (layout starts at
  84. * page_rect.y + 4 - scroll). getBoundingClientRect() subtracts page_rect. */
  85. struct br_layout_box {
  86. int x, y;
  87. int w, h;
  88. unsigned char valid;
  89. };
  90. static struct br_layout_box layout_boxes[BR_MAX_NODES];
  91. /* Images are loaded one per main-loop iteration so the parsed page stays
  92. * interactive while a slow network/TLS fetch or 4K decode is in progress. */
  93. static int preload_active;
  94. static int preload_pending;
  95. static int preload_wait;
  96. static unsigned int preload_node;
  97. static int preload_loaded;
  98. static int preload_scroll;
  99. static char post_status_text[BR_STATUS_MAX];
  100. static int post_status_error;
  101. static unsigned int post_status_ttl;
  102. /* Content-relative position/height of each <img>, recorded by the measure
  103. * pass so image loading can be deferred until it is near the viewport. */
  104. static unsigned short img_top[BR_MAX_NODES];
  105. static unsigned short img_h[BR_MAX_NODES];
  106. /* One reusable I/O buffer: the two halves double as the gzip input scratch
  107. * for page/CSS fetches, where the heap has no room for another multi-megabyte
  108. * body. Decode arenas are allocated per image instead of living here. */
  109. static unsigned char *image_io_buf;
  110. static struct ufont_face *font_faces[BR_MAX_FACES];
  111. #define image_file_buf (image_io_buf)
  112. #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX)
  113. #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX)
  114. static struct inflate image_inflate_ws;
  115. #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX
  116. #error "CSS scratch must fit in image_io_buf"
  117. #endif
  118. static char page_url[BR_URL_MAX];
  119. static char page_title[BR_TITLE_MAX];
  120. static char cwd[64];
  121. static int page_is_local;
  122. static int page_loading;
  123. static char status_text[BR_STATUS_MAX];
  124. static unsigned int status_until;
  125. static int status_error;
  126. static char history[BR_HISTORY][BR_URL_MAX];
  127. static int hist_len, hist_pos;
  128. static char click_url[BR_URL_MAX];
  129. static char pending_url[BR_URL_MAX];
  130. #define BR_POST_MAX 4096
  131. static char pending_post[BR_POST_MAX];
  132. static unsigned int pending_post_len;
  133. static unsigned int pending_action;
  134. static int cancel_fetch;
  135. static int quit;
  136. static struct ngui_rect page_rect;
  137. static unsigned short *page_backbuf;
  138. static int pagebuf_w, pagebuf_h;
  139. static int page_cache_valid;
  140. static int page_cache_scroll;
  141. static unsigned int page_cache_nnode;
  142. static int page_cache_x, page_cache_y, page_cache_w, page_cache_h;
  143. static int layout_valid, layout_left, layout_right;
  144. static unsigned int layout_nnode;
  145. static int layout_content_h;
  146. /* layout output */
  147. struct lay_run {
  148. unsigned short off, len;
  149. unsigned short gap;
  150. unsigned short color;
  151. unsigned short href;
  152. char lit[28];
  153. short img;
  154. signed short dy; /* inline image vertical offset */
  155. unsigned short iw, ih;
  156. unsigned short w;
  157. unsigned char font_px, bold, italic, mono, underline, font_face;
  158. unsigned char transform_scale;
  159. unsigned char origin_x_kind, origin_y_kind;
  160. signed short scale_x, scale_y, rotate_x100;
  161. signed short origin_x, origin_y;
  162. unsigned char has_lit;
  163. unsigned int form; /* node + 1, 0 = not a control */
  164. unsigned char form_kind;
  165. unsigned char form_disabled;
  166. };
  167. struct lay {
  168. int draw;
  169. struct ngui *ui;
  170. int left, right;
  171. int y;
  172. int line_x, line_y, line_h;
  173. int line_left, line_right;
  174. int parent_top;
  175. int run_x;
  176. int align;
  177. int depth;
  178. int space_pending;
  179. unsigned char float_active;
  180. int float_left, float_right, float_bottom;
  181. struct lay_run runs[96];
  182. int nruns;
  183. };
  184. struct hit {
  185. int x, y, w, h;
  186. unsigned short node;
  187. };
  188. /* Live form state; the DOM attributes stay the initial/default value. */
  189. #define FORM_MAX 64
  190. #define FORM_VALUE_MAX 63
  191. enum {
  192. FORM_TEXT, FORM_PASSWORD, FORM_CHECKBOX, FORM_RADIO,
  193. FORM_BUTTON, FORM_SUBMIT, FORM_RESET, FORM_SELECT, FORM_TEXTAREA
  194. };
  195. struct form_state {
  196. unsigned int node;
  197. char value[FORM_VALUE_MAX + 1];
  198. unsigned char len, caret, checked, selected, used;
  199. };
  200. static struct form_state form_states[FORM_MAX];
  201. static unsigned int form_focus;
  202. static unsigned int form_dirty;
  203. static int form_descendant(unsigned int node, unsigned int form);
  204. static int form_submit_form(unsigned int node, int fire_event);
  205. static void form_reset_event(unsigned int form);
  206. static unsigned int s_len(const char *s);
  207. static int s_eq(const char *a, const char *b);
  208. static void form_key(int k);
  209. static void form_click(unsigned int node);
  210. static void style_for_node(unsigned int node, struct br_style *out);
  211. static int form_disabled(unsigned int node)
  212. {
  213. struct br_node *n = &doc.nodes[node];
  214. if (n->attr_flags & BR_ATTR_DISABLED)
  215. return 1;
  216. return br_find_attr(&doc, node, "disabled") != 0;
  217. }
  218. static int form_kind_of(unsigned int node)
  219. {
  220. const char *type = br_type(&doc, node);
  221. if (doc.nodes[node].tag == BR_TAG_TEXTAREA)
  222. return FORM_TEXTAREA;
  223. if (doc.nodes[node].tag == BR_TAG_SELECT)
  224. return FORM_SELECT;
  225. if (doc.nodes[node].tag == BR_TAG_BUTTON) {
  226. if (s_eq(type, "reset"))
  227. return FORM_RESET;
  228. if (s_eq(type, "button"))
  229. return FORM_BUTTON;
  230. return FORM_SUBMIT;
  231. }
  232. if (s_eq(type, "password"))
  233. return FORM_PASSWORD;
  234. if (s_eq(type, "checkbox"))
  235. return FORM_CHECKBOX;
  236. if (s_eq(type, "radio"))
  237. return FORM_RADIO;
  238. if (s_eq(type, "submit"))
  239. return FORM_SUBMIT;
  240. if (s_eq(type, "reset"))
  241. return FORM_RESET;
  242. if (s_eq(type, "button"))
  243. return FORM_BUTTON;
  244. return FORM_TEXT;
  245. }
  246. static unsigned int form_option_count(unsigned int node)
  247. {
  248. unsigned int c, n = 0;
  249. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next)
  250. if (doc.nodes[c].type == BR_NODE_ELEM &&
  251. doc.nodes[c].tag == BR_TAG_OPTION)
  252. n++;
  253. return n;
  254. }
  255. static unsigned int form_option_selected(unsigned int node)
  256. {
  257. unsigned int c, i = 0;
  258. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  259. if (doc.nodes[c].type != BR_NODE_ELEM ||
  260. doc.nodes[c].tag != BR_TAG_OPTION)
  261. continue;
  262. if (br_find_attr(&doc, c, "selected"))
  263. return i;
  264. i++;
  265. }
  266. return 0;
  267. }
  268. static unsigned int form_option_text(unsigned int node, unsigned int idx,
  269. char *out, unsigned int cap)
  270. {
  271. unsigned int c, i = 0;
  272. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  273. if (doc.nodes[c].type != BR_NODE_ELEM ||
  274. doc.nodes[c].tag != BR_TAG_OPTION)
  275. continue;
  276. if (i == idx)
  277. return br_text_content(&doc, c, out, cap);
  278. i++;
  279. }
  280. return 0;
  281. }
  282. static struct form_state *form_get(unsigned int node, int create)
  283. {
  284. struct form_state *slot = 0;
  285. unsigned int i;
  286. for (i = 0; i < FORM_MAX; i++) {
  287. if (form_states[i].used) {
  288. if (form_states[i].node == node)
  289. return &form_states[i];
  290. } else if (!slot) {
  291. slot = &form_states[i];
  292. }
  293. }
  294. if (!create || !slot)
  295. return 0;
  296. slot->node = node;
  297. slot->len = 0;
  298. slot->caret = 0;
  299. slot->checked = br_find_attr(&doc, node, "checked") != 0;
  300. slot->selected = form_option_selected(node);
  301. slot->used = 1;
  302. {
  303. unsigned int n = 0;
  304. if (doc.nodes[node].tag == BR_TAG_TEXTAREA) {
  305. n = br_text_content(&doc, node, slot->value,
  306. FORM_VALUE_MAX + 1);
  307. } else {
  308. const char *v = br_value(&doc, node);
  309. if (v) {
  310. n = s_len(v);
  311. if (n > FORM_VALUE_MAX)
  312. n = FORM_VALUE_MAX;
  313. for (unsigned int j = 0; j < n; j++)
  314. slot->value[j] = v[j];
  315. }
  316. if (doc.nodes[node].tag == BR_TAG_BUTTON && n == 0)
  317. n = br_text_content(&doc, node, slot->value,
  318. FORM_VALUE_MAX + 1);
  319. }
  320. if (n > FORM_VALUE_MAX)
  321. n = FORM_VALUE_MAX;
  322. slot->value[n] = '\0';
  323. slot->len = (unsigned char)n;
  324. slot->caret = (unsigned char)n;
  325. }
  326. return slot;
  327. }
  328. static void form_reset_state(void)
  329. {
  330. unsigned int i;
  331. for (i = 0; i < FORM_MAX; i++)
  332. form_states[i].used = 0;
  333. form_focus = 0;
  334. }
  335. static int form_is_control(unsigned int node)
  336. {
  337. unsigned int tag;
  338. if (node >= doc.nnode || doc.nodes[node].type != BR_NODE_ELEM)
  339. return 0;
  340. tag = doc.nodes[node].tag;
  341. return tag == BR_TAG_INPUT || tag == BR_TAG_TEXTAREA ||
  342. tag == BR_TAG_SELECT || tag == BR_TAG_BUTTON;
  343. }
  344. #define HIT_MAX 96
  345. static struct hit hits[HIT_MAX];
  346. static int nhits;
  347. static unsigned short block_h[BR_MAX_NODES];
  348. static char li_marker[BR_MAX_NODES][6];
  349. /* one connection object; the browser fetches one resource at a time */
  350. /* ---- tiny strings ---- */
  351. static unsigned int s_len(const char *s)
  352. {
  353. unsigned int n = 0;
  354. while (s && s[n])
  355. n++;
  356. return n;
  357. }
  358. static void s_copy(char *d, const char *s, unsigned int cap)
  359. {
  360. unsigned int i = 0;
  361. if (!cap)
  362. return;
  363. while (s && s[i] && i + 1 < cap) {
  364. d[i] = s[i];
  365. i++;
  366. }
  367. d[i] = '\0';
  368. }
  369. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  370. {
  371. while (s && *s && *n + 1 < cap)
  372. d[(*n)++] = *s++;
  373. d[*n] = '\0';
  374. }
  375. static void s_append_dec(char *d, unsigned int *n, unsigned int v,
  376. unsigned int cap)
  377. {
  378. char tmp[12];
  379. unsigned int i = 0, j;
  380. do {
  381. tmp[i++] = (char)('0' + v % 10);
  382. v /= 10;
  383. } while (v && i < sizeof(tmp));
  384. for (j = 0; j < i && *n + 1 < cap; j++)
  385. d[(*n)++] = tmp[i - 1 - j];
  386. d[*n] = '\0';
  387. }
  388. static int s_eq(const char *a, const char *b)
  389. {
  390. while (*a && *b) {
  391. if (*a != *b)
  392. return 0;
  393. a++;
  394. b++;
  395. }
  396. return *a == *b;
  397. }
  398. static int starts_with(const char *s, const char *p)
  399. {
  400. unsigned int i = 0;
  401. while (p[i]) {
  402. if (s[i] != p[i])
  403. return 0;
  404. i++;
  405. }
  406. return 1;
  407. }
  408. static int contains_ci(const char *s, const char *p)
  409. {
  410. unsigned int i, j;
  411. for (i = 0; s[i]; i++) {
  412. for (j = 0; p[j]; j++) {
  413. char a = s[i + j];
  414. if (a >= 'A' && a <= 'Z')
  415. a = (char)(a - 'A' + 'a');
  416. if (a != p[j])
  417. break;
  418. }
  419. if (!p[j])
  420. return 1;
  421. }
  422. return 0;
  423. }
  424. static char *trim(char *s)
  425. {
  426. unsigned int n;
  427. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
  428. s++;
  429. n = s_len(s);
  430. while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
  431. s[n - 1] == '\r' || s[n - 1] == '\n'))
  432. s[--n] = '\0';
  433. return s;
  434. }
  435. /* ---- per-process heap ---- */
  436. static int heap_start(void)
  437. {
  438. image_io_buf = (unsigned char *)malloc(2u * BR_IMG_FILE_MAX);
  439. if (!image_io_buf)
  440. return -1;
  441. page_backbuf = (unsigned short *)malloc(
  442. (unsigned int)ui.w * (unsigned int)ui.h * 2u);
  443. pagebuf_w = page_backbuf ? ui.w : 0;
  444. pagebuf_h = page_backbuf ? ui.h : 0;
  445. page_cache_valid = 0;
  446. return 0;
  447. }
  448. static void heap_clear(void)
  449. {
  450. memset(font_faces, 0, sizeof(font_faces));
  451. ngui_font_detach(&ui);
  452. heap_reset();
  453. image_io_buf = 0;
  454. page_backbuf = 0;
  455. pagebuf_w = pagebuf_h = 0;
  456. page_cache_valid = 0;
  457. }
  458. static void browser_rules_alloc(void)
  459. {
  460. struct br_rule *rules;
  461. rules = (struct br_rule *)malloc(
  462. (unsigned int)(BR_MAX_RULES * sizeof(struct br_rule)));
  463. br_set_rule_store(&doc, rules, rules ? BR_MAX_RULES : 0);
  464. }
  465. /* ---- status ---- */
  466. static void set_status(const char *s, int err, unsigned int ttl_ms)
  467. {
  468. s_copy(status_text, s, sizeof(status_text));
  469. status_error = err;
  470. status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0;
  471. ngui_invalidate(&ui);
  472. }
  473. /* ---- JavaScript ---- */
  474. static int resolve_link(const char *base, const char *href, char *out,
  475. unsigned int cap);
  476. static char js_last_status[BR_STATUS_MAX];
  477. static int js_last_error;
  478. static void js_host_status(const char *msg, int error)
  479. {
  480. s_copy(js_last_status, msg, sizeof(js_last_status));
  481. js_last_error = error;
  482. set_status(msg, error, 6000);
  483. }
  484. static void js_host_report(const char *msg)
  485. {
  486. u_puts("[js] ");
  487. u_puts(msg);
  488. u_putc('\n');
  489. s_copy(js_last_status, msg, sizeof(js_last_status));
  490. js_last_error = 1;
  491. set_status(msg, 1, 8000);
  492. }
  493. static void js_host_navigate(const char *url)
  494. {
  495. char resolved[BR_URL_MAX];
  496. if (!url || !url[0])
  497. return;
  498. if (resolve_link(page_url, url, resolved, sizeof(resolved)) != 0)
  499. return;
  500. s_copy(pending_url, resolved, sizeof(pending_url));
  501. pending_action |= ACT_NAV;
  502. cancel_fetch = 1;
  503. }
  504. static void js_host_changed(void)
  505. {
  506. /* Class/style/attribute mutations change computed styles, not just the
  507. * text: drop the per-node style cache before the next layout. */
  508. br_reset_styles(&doc);
  509. layout_valid = 0;
  510. ngui_invalidate(&ui);
  511. }
  512. static int js_host_rect(unsigned int node, int *x, int *y, int *w, int *h)
  513. {
  514. if (node >= BR_MAX_NODES || !layout_boxes[node].valid)
  515. return 0;
  516. *x = layout_boxes[node].x - page_rect.x;
  517. *y = layout_boxes[node].y - page_rect.y;
  518. *w = layout_boxes[node].w;
  519. *h = layout_boxes[node].h;
  520. return 1;
  521. }
  522. static void form_copy_out(const char *s, char *buf, int cap)
  523. {
  524. int i = 0;
  525. if (!buf || cap <= 0)
  526. return;
  527. while (s && s[i] && i + 1 < cap) {
  528. buf[i] = s[i];
  529. i++;
  530. }
  531. buf[i] = '\0';
  532. }
  533. static int js_host_form_value(unsigned int node, char *buf, int cap)
  534. {
  535. struct form_state *fs;
  536. int kind;
  537. if (!form_is_control(node))
  538. return 0;
  539. kind = form_kind_of(node);
  540. if (kind == FORM_CHECKBOX || kind == FORM_RADIO)
  541. return 0;
  542. if (kind == FORM_SELECT) {
  543. char opt[64] = "";
  544. unsigned int c, i = 0;
  545. fs = form_get(node, 1);
  546. if (!fs)
  547. return 0;
  548. form_option_text(node, fs->selected, opt, sizeof(opt));
  549. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  550. if (doc.nodes[c].type != BR_NODE_ELEM ||
  551. doc.nodes[c].tag != BR_TAG_OPTION)
  552. continue;
  553. if (i == fs->selected) {
  554. const char *v = br_value(&doc, c);
  555. if (v && v[0])
  556. form_copy_out(v, buf, cap);
  557. else
  558. form_copy_out(opt, buf, cap);
  559. return 1;
  560. }
  561. i++;
  562. }
  563. form_copy_out(opt, buf, cap);
  564. return 1;
  565. }
  566. fs = form_get(node, 1);
  567. if (!fs)
  568. return 0;
  569. form_copy_out(fs->value, buf, cap);
  570. return 1;
  571. }
  572. static int js_host_form_checked(unsigned int node)
  573. {
  574. struct form_state *fs;
  575. int kind;
  576. if (!form_is_control(node))
  577. return -1;
  578. kind = form_kind_of(node);
  579. if (kind != FORM_CHECKBOX && kind != FORM_RADIO)
  580. return -1;
  581. fs = form_get(node, 1);
  582. return fs ? fs->checked : 0;
  583. }
  584. static int js_host_form_selected(unsigned int node)
  585. {
  586. struct form_state *fs;
  587. if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT)
  588. return -1;
  589. fs = form_get(node, 1);
  590. return fs ? fs->selected : 0;
  591. }
  592. static int js_host_form_set_value(unsigned int node, const char *value)
  593. {
  594. struct form_state *fs;
  595. unsigned int n;
  596. if (!form_is_control(node))
  597. return 0;
  598. fs = form_get(node, 1);
  599. if (!fs)
  600. return 0;
  601. n = s_len(value ? value : "");
  602. if (n > FORM_VALUE_MAX)
  603. n = FORM_VALUE_MAX;
  604. for (unsigned int i = 0; i < n; i++)
  605. fs->value[i] = value[i];
  606. fs->value[n] = '\0';
  607. fs->len = (unsigned char)n;
  608. fs->caret = (unsigned char)n;
  609. layout_valid = 0;
  610. ngui_invalidate(&ui);
  611. return 1;
  612. }
  613. static int js_host_form_set_checked(unsigned int node, int checked)
  614. {
  615. struct form_state *fs;
  616. int kind;
  617. if (!form_is_control(node))
  618. return 0;
  619. kind = form_kind_of(node);
  620. if (kind != FORM_CHECKBOX && kind != FORM_RADIO)
  621. return 0;
  622. fs = form_get(node, 1);
  623. if (!fs)
  624. return 0;
  625. fs->checked = (unsigned char)(checked != 0);
  626. layout_valid = 0;
  627. ngui_invalidate(&ui);
  628. return 1;
  629. }
  630. static int js_host_form_set_selected(unsigned int node, int index)
  631. {
  632. struct form_state *fs;
  633. unsigned int n;
  634. if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT)
  635. return 0;
  636. fs = form_get(node, 1);
  637. if (!fs)
  638. return 0;
  639. n = form_option_count(node);
  640. if (index < 0)
  641. index = 0;
  642. if (n && (unsigned int)index >= n)
  643. index = (int)n - 1;
  644. fs->selected = (unsigned char)index;
  645. layout_valid = 0;
  646. ngui_invalidate(&ui);
  647. return 1;
  648. }
  649. static int js_host_form_elements(unsigned int form, unsigned int *out, int cap)
  650. {
  651. unsigned int i;
  652. int n = 0;
  653. if (!form || form >= doc.nnode || doc.nodes[form].type != BR_NODE_ELEM ||
  654. doc.nodes[form].tag != BR_TAG_FORM)
  655. return 0;
  656. for (i = 0; i < doc.nnode && n < cap; i++) {
  657. struct br_node *nd = &doc.nodes[i];
  658. if (i == form || nd->type != BR_NODE_ELEM)
  659. continue;
  660. if (nd->tag != BR_TAG_INPUT && nd->tag != BR_TAG_SELECT &&
  661. nd->tag != BR_TAG_TEXTAREA && nd->tag != BR_TAG_BUTTON)
  662. continue;
  663. if (!form_descendant(i, form))
  664. continue;
  665. out[n++] = i;
  666. }
  667. return n;
  668. }
  669. static int js_host_form_submit(unsigned int form)
  670. {
  671. form_submit_form(form, 0);
  672. return 1;
  673. }
  674. static int js_host_form_reset(unsigned int form)
  675. {
  676. form_reset_event(form);
  677. return 1;
  678. }
  679. static void css_computed_name(const char *p, char *out, int cap)
  680. {
  681. int n = 0;
  682. for (; p && *p && n + 2 < cap; p++) {
  683. char c = *p;
  684. if (c >= 'A' && c <= 'Z') {
  685. out[n++] = '-';
  686. c = (char)(c - 'A' + 'a');
  687. } else if (c == '_') {
  688. c = '-';
  689. } else if (c >= 'a' && c <= 'z') {
  690. /* keep */
  691. }
  692. out[n++] = c;
  693. }
  694. out[n] = '\0';
  695. if (!strcmp(out, "css-float"))
  696. strcpy(out, "float");
  697. }
  698. static void css_rgb(unsigned short c, char *buf, int cap)
  699. {
  700. int r = ((c >> 11) & 31) * 255 / 31;
  701. int g = ((c >> 5) & 63) * 255 / 63;
  702. int b = (c & 31) * 255 / 31;
  703. snprintf(buf, cap, "rgb(%d, %d, %d)", r, g, b);
  704. }
  705. static void css_mp_value(char *buf, int cap, int raw, int unit, int px)
  706. {
  707. if (unit == 4) {
  708. snprintf(buf, cap, "auto");
  709. } else if (unit == 0) {
  710. snprintf(buf, cap, "%dpx", raw);
  711. } else if (unit == 1) {
  712. snprintf(buf, cap, "%d.%02d%%", raw / 100, raw % 100);
  713. } else if (unit == 2) {
  714. snprintf(buf, cap, "%d.%02dem", raw / 100, raw % 100);
  715. } else if (unit == 3) {
  716. snprintf(buf, cap, "%d.%02drem", raw / 100, raw % 100);
  717. } else {
  718. snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
  719. raw / 100, raw % 100, px);
  720. }
  721. }
  722. static int js_host_computed(unsigned int node, const char *property,
  723. char *buf, int cap)
  724. {
  725. struct br_style st;
  726. char name[40];
  727. if (!buf || cap <= 0 || node >= doc.nnode || node == 0)
  728. return 0;
  729. buf[0] = '\0';
  730. css_computed_name(property, name, sizeof(name));
  731. style_for_node(node, &st);
  732. if (!strcmp(name, "display")) {
  733. const char *v = "inline";
  734. if (st.display == BR_DISPLAY_BLOCK)
  735. v = "block";
  736. else if (st.display == BR_DISPLAY_NONE)
  737. v = "none";
  738. else if (st.display == BR_DISPLAY_FLEX)
  739. v = "flex";
  740. else if (st.display == BR_DISPLAY_GRID)
  741. v = "grid";
  742. else if (st.display == BR_DISPLAY_INLINE_BLOCK)
  743. v = "inline-block";
  744. snprintf(buf, cap, "%s", v);
  745. } else if (!strcmp(name, "position")) {
  746. static const char *pos[] = {
  747. "static", "absolute", "fixed", "relative", "sticky"
  748. };
  749. snprintf(buf, cap, "%s", pos[st.pos < 5 ? st.pos : 0]);
  750. } else if (!strcmp(name, "width") || !strcmp(name, "height")) {
  751. int is_w = name[0] == 'w';
  752. int kind = is_w ? st.width_kind : st.height_kind;
  753. int val = is_w ? st.width : st.height;
  754. if (kind == 3) {
  755. if (is_w)
  756. snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
  757. st.width_calc_pct / 100,
  758. st.width_calc_pct % 100,
  759. st.width_calc_px);
  760. else
  761. snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
  762. st.height_calc_pct / 100,
  763. st.height_calc_pct % 100,
  764. st.height_calc_px);
  765. } else if (kind == 1) {
  766. snprintf(buf, cap, "%dpx", val);
  767. } else if (kind == 2) {
  768. snprintf(buf, cap, "%d.%02d%%", val / 100, val % 100);
  769. } else {
  770. snprintf(buf, cap, "auto");
  771. }
  772. } else if (!strcmp(name, "color") ||
  773. !strcmp(name, "background-color")) {
  774. if (!strcmp(name, "background-color") && !st.has_bg)
  775. snprintf(buf, cap, "rgba(0, 0, 0, 0)");
  776. else
  777. css_rgb(name[0] == 'c' ? st.color : st.bg, buf, cap);
  778. } else if (!strcmp(name, "font-size")) {
  779. snprintf(buf, cap, "%dpx", st.font_px);
  780. } else if (!strcmp(name, "font-weight")) {
  781. snprintf(buf, cap, "%s", st.bold ? "700" : "400");
  782. } else if (!strcmp(name, "font-style")) {
  783. snprintf(buf, cap, "%s", st.italic ? "italic" : "normal");
  784. } else if (!strcmp(name, "font-family")) {
  785. if (st.font_face > 0 && st.font_face <= BR_MAX_FACES &&
  786. br_font_face_family(st.font_face)[0])
  787. snprintf(buf, cap, "%s",
  788. br_font_face_family(st.font_face));
  789. else
  790. snprintf(buf, cap, "%s",
  791. st.mono ? "monospace" : "sans-serif");
  792. } else if (!strcmp(name, "text-align")) {
  793. static const char *al[] = { "left", "center", "right", "justify" };
  794. snprintf(buf, cap, "%s", al[st.align < 4 ? st.align : 0]);
  795. } else if (!strcmp(name, "float")) {
  796. snprintf(buf, cap, "%s", st.float_left == 1 ? "left" :
  797. st.float_left == 2 ? "right" : "none");
  798. } else if (!strcmp(name, "clear")) {
  799. static const char *cl[] = { "none", "left", "right", "both" };
  800. snprintf(buf, cap, "%s", cl[st.clear < 4 ? st.clear : 0]);
  801. } else if (!strcmp(name, "opacity")) {
  802. snprintf(buf, cap, "%s", st.opacity_hidden ? "0" : "1");
  803. } else if (!strcmp(name, "visibility")) {
  804. snprintf(buf, cap, "%s", st.vis_hidden ? "hidden" : "visible");
  805. } else if (!strcmp(name, "overflow")) {
  806. snprintf(buf, cap, "%s",
  807. st.overflow_hidden ? "hidden" : "visible");
  808. } else if (!strcmp(name, "white-space")) {
  809. snprintf(buf, cap, "%s", st.nowrap ? "nowrap" : "normal");
  810. } else if (!strcmp(name, "text-transform")) {
  811. snprintf(buf, cap, "%s", st.uppercase ? "uppercase" : "none");
  812. } else if (!strcmp(name, "box-sizing")) {
  813. snprintf(buf, cap, "%s",
  814. st.box_sizing ? "border-box" : "content-box");
  815. } else if (!strcmp(name, "line-height")) {
  816. if (st.line_h_px)
  817. snprintf(buf, cap, "%dpx", st.line_h_px);
  818. else if (st.line_h_pct)
  819. snprintf(buf, cap, "%d%%", st.line_h_pct);
  820. else
  821. snprintf(buf, cap, "normal");
  822. } else if (!strcmp(name, "z-index")) {
  823. if (st.z_set)
  824. snprintf(buf, cap, "%d", st.z_index);
  825. else
  826. snprintf(buf, cap, "auto");
  827. } else if (!strcmp(name, "top") || !strcmp(name, "left") ||
  828. !strcmp(name, "right") || !strcmp(name, "bottom")) {
  829. int set = name[0] == 't' ? st.top_set :
  830. name[0] == 'l' ? st.left_set :
  831. name[0] == 'r' ? st.right_set : st.bottom_set;
  832. int v = name[0] == 't' ? st.top :
  833. name[0] == 'l' ? st.left :
  834. name[0] == 'r' ? st.right : st.bottom;
  835. if (set)
  836. snprintf(buf, cap, "%dpx", v);
  837. else
  838. snprintf(buf, cap, "auto");
  839. } else if (!strcmp(name, "margin-top") || !strcmp(name, "margin-right") ||
  840. !strcmp(name, "margin-bottom") ||
  841. !strcmp(name, "margin-left") ||
  842. !strcmp(name, "padding-top") ||
  843. !strcmp(name, "padding-right") ||
  844. !strcmp(name, "padding-bottom") ||
  845. !strcmp(name, "padding-left")) {
  846. int is_pad = name[0] == 'p';
  847. int side = name[strlen(name) - 1] == 'p' ? 0 :
  848. name[strlen(name) - 1] == 't' ? 1 :
  849. name[strlen(name) - 1] == 'm' ? 2 : 3;
  850. /* last letter: top p, right t, bottom m, left t (ambiguity). */
  851. if (!strcmp(name, "margin-right") ||
  852. !strcmp(name, "padding-right"))
  853. side = 1;
  854. else if (!strcmp(name, "margin-bottom") ||
  855. !strcmp(name, "padding-bottom"))
  856. side = 2;
  857. else if (!strcmp(name, "margin-left") ||
  858. !strcmp(name, "padding-left"))
  859. side = 3;
  860. else
  861. side = 0;
  862. side += is_pad ? 4 : 0;
  863. css_mp_value(buf, cap,
  864. side == 0 ? st.mt : side == 1 ? st.mr :
  865. side == 2 ? st.mb : side == 3 ? st.ml :
  866. side == 4 ? st.pt : side == 5 ? st.pr :
  867. side == 6 ? st.pb : st.pl,
  868. st.mp_unit[side], st.mp_calc_px[side]);
  869. } else if (!strcmp(name, "border-top-width") ||
  870. !strcmp(name, "border-right-width") ||
  871. !strcmp(name, "border-bottom-width") ||
  872. !strcmp(name, "border-left-width")) {
  873. int side = name[7] == 't' ? 0 : name[7] == 'r' ? 1 :
  874. name[7] == 'b' ? 2 : 3;
  875. snprintf(buf, cap, "%dpx", st.border_w[side]);
  876. } else if (!strcmp(name, "border-top-color") ||
  877. !strcmp(name, "border-right-color") ||
  878. !strcmp(name, "border-bottom-color") ||
  879. !strcmp(name, "border-left-color")) {
  880. int side = name[7] == 't' ? 0 : name[7] == 'r' ? 1 :
  881. name[7] == 'b' ? 2 : 3;
  882. if (st.border_w[side])
  883. css_rgb(st.border_c[side], buf, cap);
  884. else
  885. snprintf(buf, cap, "rgba(0, 0, 0, 0)");
  886. } else if (!strcmp(name, "background-image")) {
  887. if (st.bg_image)
  888. snprintf(buf, cap, "url(\"%s\")",
  889. br_bg_image_url(st.bg_image - 1));
  890. else
  891. snprintf(buf, cap, "none");
  892. } else if (!strcmp(name, "background-repeat")) {
  893. static const char *rp[] = {
  894. "repeat", "no-repeat", "repeat-x", "repeat-y"
  895. };
  896. snprintf(buf, cap, "%s", rp[st.bg_repeat < 4 ?
  897. st.bg_repeat : 0]);
  898. } else if (!strcmp(name, "background-position")) {
  899. if (st.bg_pos_x_kind == 1)
  900. snprintf(buf, cap, "%d%% %d%%", st.bg_pos_x,
  901. st.bg_pos_y);
  902. else
  903. snprintf(buf, cap, "%dpx %dpx", st.bg_pos_x,
  904. st.bg_pos_y);
  905. } else if (!strcmp(name, "background-size")) {
  906. if (st.bg_size_kind == 3)
  907. snprintf(buf, cap, "cover");
  908. else if (st.bg_size_kind == 4)
  909. snprintf(buf, cap, "contain");
  910. else if (st.bg_size_kind == 1)
  911. snprintf(buf, cap, "%dpx %dpx", st.bg_size_w,
  912. st.bg_size_h);
  913. else if (st.bg_size_kind == 2)
  914. snprintf(buf, cap, "%d%% %d%%", st.bg_size_w,
  915. st.bg_size_h);
  916. else
  917. snprintf(buf, cap, "auto");
  918. } else if (!strcmp(name, "transform")) {
  919. char tmp[128];
  920. unsigned int n = 0;
  921. if (!st.transform_set) {
  922. snprintf(buf, cap, "none");
  923. } else {
  924. tmp[0] = '\0';
  925. if (st.tx || st.ty) {
  926. snprintf(tmp + n, sizeof(tmp) - n,
  927. "translate(%dpx, %dpx)", st.tx, st.ty);
  928. n = (unsigned int)strlen(tmp);
  929. }
  930. if (st.transform_scale) {
  931. snprintf(tmp + n, sizeof(tmp) - n,
  932. "%sscale(%d.%02d, %d.%02d)",
  933. n ? " " : "", st.scale_x / 100,
  934. st.scale_x % 100, st.scale_y / 100,
  935. st.scale_y % 100);
  936. n = (unsigned int)strlen(tmp);
  937. if (st.rotate_x100) {
  938. snprintf(tmp + n, sizeof(tmp) - n,
  939. " rotate(%d.%02ddeg)",
  940. st.rotate_x100 / 100,
  941. st.rotate_x100 % 100);
  942. }
  943. } else if (st.rotate_x100) {
  944. snprintf(tmp + n, sizeof(tmp) - n,
  945. "%srotate(%d.%02ddeg)",
  946. n ? " " : "", st.rotate_x100 / 100,
  947. st.rotate_x100 % 100);
  948. }
  949. snprintf(buf, cap, "%s", tmp[0] ? tmp : "none");
  950. }
  951. } else {
  952. return 0;
  953. }
  954. return buf[0] != '\0';
  955. }
  956. static int js_host_click(unsigned int node)
  957. {
  958. char url[BR_URL_MAX];
  959. const char *href;
  960. if (node >= doc.nnode)
  961. return 0;
  962. if (form_is_control(node)) {
  963. form_click(node);
  964. return 1;
  965. }
  966. href = br_href(&doc, node);
  967. if (resolve_link(page_url, href, url, sizeof(url)) != 0)
  968. return 0;
  969. s_copy(pending_url, url, sizeof(pending_url));
  970. pending_action |= ACT_NAV;
  971. cancel_fetch = 1;
  972. ngui_invalidate(&ui);
  973. return 1;
  974. }
  975. static int fetch_script_text(const char *url, unsigned char **out,
  976. unsigned int *outlen, char *err,
  977. unsigned int errcap);
  978. static void script_error(const char *what, const char *url, const char *why)
  979. {
  980. char msg[192];
  981. unsigned int n = 0;
  982. s_append(msg, &n, what, sizeof(msg));
  983. if (url && url[0]) {
  984. s_append(msg, &n, " ", sizeof(msg));
  985. s_append(msg, &n, url, sizeof(msg));
  986. }
  987. if (why && why[0]) {
  988. s_append(msg, &n, ": ", sizeof(msg));
  989. s_append(msg, &n, why, sizeof(msg));
  990. }
  991. js_host_report(msg);
  992. }
  993. /* Run the page's scripts in document order: external <script src> bodies are
  994. * fetched, inline bodies are copied because MuJS needs NUL-terminated text. */
  995. static void run_page_scripts(void)
  996. {
  997. struct js_browser_host host;
  998. unsigned int i;
  999. if (!doc.nscripts)
  1000. return;
  1001. host.status = js_host_status;
  1002. host.report = js_host_report;
  1003. host.navigate = js_host_navigate;
  1004. host.changed = js_host_changed;
  1005. host.rect = js_host_rect;
  1006. host.form_value = js_host_form_value;
  1007. host.form_checked = js_host_form_checked;
  1008. host.form_selected = js_host_form_selected;
  1009. host.form_set_value = js_host_form_set_value;
  1010. host.form_set_checked = js_host_form_set_checked;
  1011. host.form_set_selected = js_host_form_set_selected;
  1012. host.form_elements = js_host_form_elements;
  1013. host.form_submit = js_host_form_submit;
  1014. host.form_reset = js_host_form_reset;
  1015. host.click = js_host_click;
  1016. host.computed = js_host_computed;
  1017. host.page_url = page_url;
  1018. host.width = ui.w;
  1019. host.height = ui.h;
  1020. if (js_browser_start(&doc, &host) != 0) {
  1021. js_host_status("JavaScript: out of memory", 1);
  1022. return;
  1023. }
  1024. for (i = 0; i < doc.nscripts; i++) {
  1025. struct br_script *script = &doc.scripts[i];
  1026. if (script->src_off) {
  1027. const char *src_attr = doc.attrs + script->src_off;
  1028. char url[BR_URL_MAX];
  1029. unsigned char *data;
  1030. unsigned int len;
  1031. char err[64];
  1032. int rv;
  1033. if (!src_attr[0])
  1034. continue;
  1035. if (resolve_link(page_url, src_attr, url,
  1036. sizeof(url)) != 0) {
  1037. script_error("Cannot load script", src_attr,
  1038. "unsupported URL");
  1039. continue;
  1040. }
  1041. set_status("Loading script...", 0, 0);
  1042. rv = fetch_script_text(url, &data, &len, err,
  1043. sizeof(err));
  1044. if (rv == FETCH_ABORT)
  1045. break;
  1046. if (rv != 0) {
  1047. script_error("Cannot load script", url, err);
  1048. continue;
  1049. }
  1050. js_browser_run((const char *)data, url);
  1051. free(data);
  1052. } else {
  1053. unsigned int off = script->off;
  1054. unsigned int len = script->len;
  1055. unsigned int k;
  1056. char *src = (char *)malloc(len + 1);
  1057. if (!src) {
  1058. js_host_status("JavaScript: out of memory", 1);
  1059. break;
  1060. }
  1061. for (k = 0; k < len; k++)
  1062. src[k] = doc.script_src[off + k];
  1063. src[len] = '\0';
  1064. js_browser_run(src, page_url);
  1065. free(src);
  1066. }
  1067. if (pending_action)
  1068. break;
  1069. }
  1070. /* Keep the state alive for setTimeout/ready callbacks; load_page() and
  1071. * load_home() stop it before the next navigation. */
  1072. if (doc.scripts_truncated && !js_last_error)
  1073. js_host_status("Page has more than 32 scripts; extra scripts skipped",
  1074. 1);
  1075. }
  1076. /* ---- images ---- */
  1077. static void image_reset(void)
  1078. {
  1079. int i;
  1080. for (i = 0; i < BR_MAX_IMAGES; i++) {
  1081. images[i].state = 0;
  1082. images[i].native_w = images[i].native_h = 0;
  1083. images[i].err[0] = '\0';
  1084. }
  1085. image_count = 0;
  1086. preload_active = 0;
  1087. preload_pending = 0;
  1088. preload_wait = 0;
  1089. preload_node = 0;
  1090. preload_loaded = 0;
  1091. }
  1092. /* Decode into the shared arena, then copy/scale the result into a display
  1093. * bitmap of at most BR_IMG_LAYOUT_W pixels wide. The arena is reused by the
  1094. * next image, so the per-image cost is its scaled bitmap, not full 4K RGBA
  1095. * pixels. Transparent edges are composited over white. */
  1096. static int decode_image_data(const unsigned char *data, unsigned int len,
  1097. struct br_image *bi)
  1098. {
  1099. struct img_info info;
  1100. struct img_options opt;
  1101. struct img_mem mem;
  1102. struct img_image decoded;
  1103. unsigned char *arena;
  1104. unsigned int est, bytes, i;
  1105. unsigned short *px;
  1106. unsigned char *alpha = 0;
  1107. int dw, dh, r;
  1108. if (img_sniff(data, len, &info) != 0) {
  1109. s_copy(bi->err, "unknown image format", sizeof(bi->err));
  1110. return -1;
  1111. }
  1112. est = img_estimate(&info);
  1113. if (est == 0xFFFFFFFFu || est > BR_IMG_ARENA_MAX) {
  1114. s_copy(bi->err, "image too large", sizeof(bi->err));
  1115. return -1;
  1116. }
  1117. arena = (unsigned char *)malloc(est);
  1118. if (!arena) {
  1119. s_copy(bi->err, "out of memory", sizeof(bi->err));
  1120. return -1;
  1121. }
  1122. img_mem_init(&mem, arena, est);
  1123. opt.max_pixels = BR_IMG_PIX_MAX;
  1124. r = img_decode(data, len, &opt, &mem, &decoded);
  1125. if (r != 0) {
  1126. s_copy(bi->err, img_error_string(r), sizeof(bi->err));
  1127. goto fail;
  1128. }
  1129. if (decoded.w == 0 || decoded.h == 0) {
  1130. s_copy(bi->err, "empty image", sizeof(bi->err));
  1131. goto fail;
  1132. }
  1133. bi->native_w = (int)decoded.w;
  1134. bi->native_h = (int)decoded.h;
  1135. dw = (int)decoded.w;
  1136. dh = (int)decoded.h;
  1137. if (dw > BR_IMG_LAYOUT_W) {
  1138. dh = (int)((unsigned int)decoded.h * BR_IMG_LAYOUT_W /
  1139. decoded.w);
  1140. dw = BR_IMG_LAYOUT_W;
  1141. }
  1142. if (dh < 1)
  1143. dh = 1;
  1144. if (dw < 1)
  1145. dw = 1;
  1146. bytes = (unsigned int)dw * (unsigned int)dh * 2u;
  1147. px = (unsigned short *)malloc(bytes);
  1148. if (!px) {
  1149. s_copy(bi->err, "out of memory", sizeof(bi->err));
  1150. goto fail;
  1151. }
  1152. /* Keep the alpha plane for the scaled image so transparent logos and
  1153. * icons composite over the page instead of the browser's dark theme
  1154. * background. Very large alpha images stay flattened to bound the
  1155. * heap use. */
  1156. if (decoded.alpha && (unsigned int)dw * (unsigned int)dh <= (1u << 20)) {
  1157. alpha = (unsigned char *)malloc((unsigned int)dw *
  1158. (unsigned int)dh);
  1159. if (alpha) {
  1160. for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
  1161. alpha[i] = 255;
  1162. }
  1163. }
  1164. for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
  1165. px[i] = ui.theme.bg;
  1166. {
  1167. struct img_blit b;
  1168. b.dst = px;
  1169. b.dst_w = dw;
  1170. b.dst_h = dh;
  1171. b.clip_x = 0;
  1172. b.clip_y = 0;
  1173. b.clip_w = dw;
  1174. b.clip_h = dh;
  1175. b.x = 0;
  1176. b.y = 0;
  1177. b.scale = ((unsigned int)dw << 16) / decoded.w;
  1178. b.bg = ui.theme.bg;
  1179. b.bg2 = ui.theme.bg;
  1180. b.dst_a = alpha;
  1181. b.flags = alpha ? IMG_BLIT_SAVE_ALPHA : IMG_BLIT_OVER_DST;
  1182. img_draw(&decoded, &b);
  1183. }
  1184. bi->img.format = decoded.format;
  1185. bi->img.w = (unsigned int)dw;
  1186. bi->img.h = (unsigned int)dh;
  1187. bi->img.px = px;
  1188. bi->img.alpha = alpha;
  1189. bi->img.flags = 0;
  1190. bi->state = 1;
  1191. free(arena);
  1192. return 0;
  1193. fail:
  1194. free(arena);
  1195. return -1;
  1196. }
  1197. /* ---- URL / local path handling ---- */
  1198. static int is_http(const char *s)
  1199. {
  1200. return starts_with(s, "http://") || starts_with(s, "https://");
  1201. }
  1202. /* Collapse "/./", "/../" and duplicate slashes in an absolute path. */
  1203. static int normalize_abs(const char *in, char *out, unsigned int cap)
  1204. {
  1205. char seg[64];
  1206. unsigned int i = 0, o = 1;
  1207. int depth = 0;
  1208. int trailing;
  1209. if (!in || in[0] != '/' || cap < 2)
  1210. return -1;
  1211. trailing = 0;
  1212. {
  1213. unsigned int n = s_len(in);
  1214. if (n > 1 && in[n - 1] == '/')
  1215. trailing = 1;
  1216. }
  1217. out[0] = '/';
  1218. while (in[i]) {
  1219. unsigned int n = 0;
  1220. while (in[i] == '/')
  1221. i++;
  1222. while (in[i] && in[i] != '/' && in[i] != '?' && in[i] != '#') {
  1223. if (n + 1 < sizeof(seg))
  1224. seg[n++] = in[i];
  1225. i++;
  1226. }
  1227. seg[n] = '\0';
  1228. if (n == 0)
  1229. break;
  1230. if (s_eq(seg, "."))
  1231. continue;
  1232. if (s_eq(seg, "..")) {
  1233. if (depth > 0) {
  1234. while (o > 1 && out[o - 1] != '/')
  1235. o--;
  1236. if (o > 1)
  1237. o--;
  1238. depth--;
  1239. }
  1240. continue;
  1241. }
  1242. if (o > 1) {
  1243. if (o + 1 >= cap)
  1244. return -1;
  1245. out[o++] = '/';
  1246. }
  1247. if (o + n + 1 >= cap)
  1248. return -1;
  1249. {
  1250. unsigned int j;
  1251. for (j = 0; j < n; j++)
  1252. out[o++] = seg[j];
  1253. }
  1254. depth++;
  1255. }
  1256. if (trailing && o > 1 && in[i] != '?' && in[i] != '#') {
  1257. if (o + 1 >= cap)
  1258. return -1;
  1259. out[o++] = '/';
  1260. }
  1261. /* Keep the query/fragment suffix for form GET and in-page anchors. */
  1262. if (in[i] == '?' || in[i] == '#') {
  1263. while (in[i]) {
  1264. if (o + 1 >= cap)
  1265. return -1;
  1266. out[o++] = in[i++];
  1267. }
  1268. }
  1269. out[o] = '\0';
  1270. return 0;
  1271. }
  1272. static void make_abs_local(const char *in, char *out, unsigned int cap)
  1273. {
  1274. char joined[BR_URL_MAX];
  1275. if (!in || !in[0]) {
  1276. s_copy(out, cwd[0] ? cwd : "/", cap);
  1277. return;
  1278. }
  1279. if (in[0] == '/') {
  1280. if (normalize_abs(in, out, cap) != 0)
  1281. s_copy(out, in, cap);
  1282. return;
  1283. }
  1284. {
  1285. unsigned int n = 0;
  1286. s_append(joined, &n, cwd[0] ? cwd : "/", sizeof(joined));
  1287. s_append(joined, &n, "/", sizeof(joined));
  1288. s_append(joined, &n, in, sizeof(joined));
  1289. }
  1290. if (normalize_abs(joined, out, cap) != 0)
  1291. s_copy(out, joined, cap);
  1292. }
  1293. /* ---- canonical origin cache ---- *
  1294. *
  1295. * Sites commonly redirect http://host to https://host and then to
  1296. * https://www.host. Remember the final scheme/host of the last navigation so
  1297. * a bare domain typed again goes straight there instead of paying for two
  1298. * more connections and TLS handshakes. Only default-port requests are
  1299. * rewritten; explicit ports are left alone. */
  1300. #define ORIGIN_CACHE_MAX 8
  1301. #define ORIGIN_FILE "/share/borigins"
  1302. #define ORIGIN_BUF_SIZE (ORIGIN_CACHE_MAX * (UHTTP_HOST_MAX + 16 + \
  1303. UHTTP_HOST_MAX + 2))
  1304. struct origin_cache_entry {
  1305. char host[UHTTP_HOST_MAX];
  1306. char origin[UHTTP_HOST_MAX + 16];
  1307. };
  1308. static struct origin_cache_entry origins[ORIGIN_CACHE_MAX];
  1309. static int origin_count;
  1310. static char origin_buf[ORIGIN_BUF_SIZE];
  1311. static int default_port(const struct uhttp_url *u)
  1312. {
  1313. return u->secure ? u->port == 443 : u->port == 80;
  1314. }
  1315. static void url_origin(const struct uhttp_url *u, char *out,
  1316. unsigned int cap)
  1317. {
  1318. unsigned int n = 0;
  1319. s_append(out, &n, u->secure ? "https://" : "http://", cap);
  1320. s_append(out, &n, u->host, cap);
  1321. if (!default_port(u)) {
  1322. s_append(out, &n, ":", cap);
  1323. s_append_dec(out, &n, u->port, cap);
  1324. }
  1325. }
  1326. static void origin_cache_apply(char *url, unsigned int cap)
  1327. {
  1328. struct uhttp_url u;
  1329. char out[BR_URL_MAX];
  1330. unsigned int n = 0;
  1331. int i;
  1332. if (uhttp_parse_url(url, &u) != 0 || !default_port(&u))
  1333. return;
  1334. for (i = 0; i < origin_count; i++) {
  1335. if (!s_eq(u.host, origins[i].host))
  1336. continue;
  1337. s_append(out, &n, origins[i].origin, sizeof(out));
  1338. s_append(out, &n, u.path, sizeof(out));
  1339. s_copy(url, out, cap);
  1340. return;
  1341. }
  1342. }
  1343. static void origin_cache_save(void)
  1344. {
  1345. unsigned int n = 0;
  1346. int i;
  1347. for (i = 0; i < origin_count; i++) {
  1348. s_append(origin_buf, &n, origins[i].host, sizeof(origin_buf));
  1349. s_append(origin_buf, &n, " ", sizeof(origin_buf));
  1350. s_append(origin_buf, &n, origins[i].origin, sizeof(origin_buf));
  1351. s_append(origin_buf, &n, "\n", sizeof(origin_buf));
  1352. }
  1353. if (n)
  1354. u_syscall3(SYS_WRITE_FILE, (int)ORIGIN_FILE,
  1355. (int)origin_buf, (int)n);
  1356. }
  1357. static void origin_cache_load(void)
  1358. {
  1359. char *p;
  1360. int rn;
  1361. rn = u_syscall3(SYS_READ_FILE, (int)ORIGIN_FILE, (int)origin_buf,
  1362. (int)(sizeof(origin_buf) - 1));
  1363. if (rn <= 0)
  1364. return;
  1365. origin_buf[rn] = '\0';
  1366. p = origin_buf;
  1367. while (*p && origin_count < ORIGIN_CACHE_MAX) {
  1368. char *line = p, *origin;
  1369. while (*p && *p != '\n')
  1370. p++;
  1371. if (*p)
  1372. *p++ = '\0';
  1373. if (!line[0])
  1374. continue;
  1375. origin = line;
  1376. while (*origin && *origin != ' ')
  1377. origin++;
  1378. if (!*origin)
  1379. continue;
  1380. *origin++ = '\0';
  1381. if (!line[0] || !is_http(origin))
  1382. continue;
  1383. {
  1384. struct uhttp_url uo;
  1385. if (uhttp_parse_url(origin, &uo) != 0 ||
  1386. !s_eq(uo.path, "/"))
  1387. continue;
  1388. }
  1389. s_copy(origins[origin_count].host, line,
  1390. sizeof(origins[origin_count].host));
  1391. s_copy(origins[origin_count].origin, origin,
  1392. sizeof(origins[origin_count].origin));
  1393. origin_count++;
  1394. }
  1395. }
  1396. static int host_is_www_of(const char *host, const char *base)
  1397. {
  1398. return host[0] == 'w' && host[1] == 'w' && host[2] == 'w' &&
  1399. host[3] == '.' && s_eq(host + 4, base);
  1400. }
  1401. static void origin_cache_learn(const char *requested, const char *final)
  1402. {
  1403. struct uhttp_url from, to;
  1404. char origin[UHTTP_HOST_MAX + 16];
  1405. int i;
  1406. if (!is_http(requested) || !is_http(final))
  1407. return;
  1408. if (uhttp_parse_url(requested, &from) != 0 ||
  1409. uhttp_parse_url(final, &to) != 0)
  1410. return;
  1411. if (!default_port(&from))
  1412. return;
  1413. if (s_eq(from.host, to.host) && from.secure == to.secure &&
  1414. from.port == to.port)
  1415. return;
  1416. /* Only remember origin-level canonicalisation: the same path, and
  1417. * either the same host or its www./apex sibling. A path- or
  1418. * host-specific redirect (a shortener, a login page) must not poison
  1419. * every request to the typed host. */
  1420. if (!s_eq(from.path, to.path))
  1421. return;
  1422. if (!s_eq(from.host, to.host) &&
  1423. !host_is_www_of(to.host, from.host) &&
  1424. !host_is_www_of(from.host, to.host))
  1425. return;
  1426. url_origin(&to, origin, sizeof(origin));
  1427. for (i = 0; i < origin_count; i++) {
  1428. if (s_eq(origins[i].host, from.host)) {
  1429. if (!s_eq(origins[i].origin, origin)) {
  1430. s_copy(origins[i].origin, origin,
  1431. sizeof(origins[i].origin));
  1432. origin_cache_save();
  1433. }
  1434. return;
  1435. }
  1436. }
  1437. i = origin_count < ORIGIN_CACHE_MAX ? origin_count++ : 0;
  1438. s_copy(origins[i].host, from.host, sizeof(origins[i].host));
  1439. s_copy(origins[i].origin, origin, sizeof(origins[i].origin));
  1440. origin_cache_save();
  1441. }
  1442. static int path_dir(const char *path, char *out, unsigned int cap)
  1443. {
  1444. unsigned int i, last = 0;
  1445. for (i = 0; path[i]; i++)
  1446. if (path[i] == '/')
  1447. last = i;
  1448. if (last == 0) {
  1449. if (cap < 2)
  1450. return -1;
  1451. out[0] = '/';
  1452. out[1] = '\0';
  1453. return 0;
  1454. }
  1455. if (last + 1 >= cap)
  1456. return -1;
  1457. {
  1458. unsigned int j;
  1459. for (j = 0; j < last; j++)
  1460. out[j] = path[j];
  1461. out[last] = '\0';
  1462. }
  1463. return 0;
  1464. }
  1465. /* Resolve an href against the current page. 0 = ok, -1 = ignore. */
  1466. static int resolve_link(const char *base, const char *href, char *out,
  1467. unsigned int cap)
  1468. {
  1469. char joined[BR_URL_MAX];
  1470. if (!href || !href[0] || href[0] == '#')
  1471. return -1;
  1472. if (starts_with(href, "mailto:") || starts_with(href, "javascript:") ||
  1473. starts_with(href, "data:") || starts_with(href, "tel:"))
  1474. return -1;
  1475. if (starts_with(href, "about:")) {
  1476. s_copy(out, href, cap);
  1477. return 0;
  1478. }
  1479. if (is_http(href)) {
  1480. s_copy(out, href, cap);
  1481. return 0;
  1482. }
  1483. if (starts_with(href, "//")) {
  1484. if (is_http(base)) {
  1485. unsigned int n = 0;
  1486. s_append(out, &n, starts_with(base, "https:") ?
  1487. "https:" : "http:", cap);
  1488. s_append(out, &n, href, cap);
  1489. return 0;
  1490. }
  1491. return -1;
  1492. }
  1493. if (is_http(base)) {
  1494. return uhttp_resolve(base, href, out, cap) == 0 ? 0 : -1;
  1495. }
  1496. /* local page */
  1497. if (href[0] == '/') {
  1498. if (normalize_abs(href, out, cap) != 0)
  1499. return -1;
  1500. return 0;
  1501. }
  1502. {
  1503. char dir[BR_URL_MAX];
  1504. unsigned int n = 0;
  1505. if (s_eq(base, "about:home"))
  1506. s_copy(dir, cwd[0] ? cwd : "/", sizeof(dir));
  1507. else if (path_dir(base, dir, sizeof(dir)) != 0)
  1508. s_copy(dir, "/", sizeof(dir));
  1509. s_append(joined, &n, dir, sizeof(joined));
  1510. if (n == 0 || joined[n - 1] != '/')
  1511. s_append(joined, &n, "/", sizeof(joined));
  1512. s_append(joined, &n, href, sizeof(joined));
  1513. }
  1514. return normalize_abs(joined, out, cap) == 0 ? 0 : -1;
  1515. }
  1516. /* Best-effort decompression: keep the first `cap` bytes and discard the
  1517. * rest instead of failing when a page or stylesheet is larger than the
  1518. * buffer. The stream is still walked to the end, so a normal page is
  1519. * verified; a truncated compressed input still returns whatever was decoded
  1520. * so far. */
  1521. struct trunc_buf {
  1522. unsigned char *dst;
  1523. unsigned int cap, pos;
  1524. int full;
  1525. };
  1526. static int trunc_write(void *ctx, const unsigned char *data, unsigned int len)
  1527. {
  1528. struct trunc_buf *m = (struct trunc_buf *)ctx;
  1529. if (len > m->cap - m->pos) {
  1530. unsigned int room = m->cap - m->pos;
  1531. if (room) {
  1532. unsigned int i;
  1533. for (i = 0; i < room; i++)
  1534. m->dst[m->pos + i] = data[i];
  1535. m->pos += room;
  1536. }
  1537. m->full = 1;
  1538. return 0;
  1539. }
  1540. {
  1541. unsigned int i;
  1542. for (i = 0; i < len; i++)
  1543. m->dst[m->pos + i] = data[i];
  1544. }
  1545. m->pos += len;
  1546. return 0;
  1547. }
  1548. static void worker_wait_ui(void);
  1549. static void inflate_ui_pump(void *ctx)
  1550. {
  1551. static unsigned int last;
  1552. unsigned int now = u_ticks_ms();
  1553. (void)ctx;
  1554. if ((int)(now - last) >= 50) {
  1555. last = now;
  1556. worker_wait_ui();
  1557. }
  1558. }
  1559. static int inflate_trunc(const unsigned char *src, unsigned int len,
  1560. unsigned char *dst, unsigned int cap,
  1561. unsigned int *outlen, int gzip, int *truncated)
  1562. {
  1563. struct inflate_src in;
  1564. struct inflate_sink sink;
  1565. struct trunc_buf m;
  1566. unsigned int got = 0;
  1567. int r;
  1568. in.p = src;
  1569. in.len = len;
  1570. in.more = 0;
  1571. in.ctx = 0;
  1572. m.dst = dst;
  1573. m.cap = cap;
  1574. m.pos = 0;
  1575. m.full = 0;
  1576. sink.write = trunc_write;
  1577. sink.ctx = &m;
  1578. inflate_init(&image_inflate_ws, &in, &sink);
  1579. image_inflate_ws.progress = inflate_ui_pump;
  1580. image_inflate_ws.progress_ctx = 0;
  1581. r = gzip ? inflate_gzip(&image_inflate_ws, &got) :
  1582. inflate_zlib(&image_inflate_ws, &got);
  1583. *outlen = m.pos;
  1584. *truncated = m.full || r != 0;
  1585. if (r != 0 && m.pos == 0)
  1586. return -1;
  1587. return 0;
  1588. }
  1589. /* ---- headless fetch helper ------------------------------------------- *
  1590. *
  1591. * Network I/O runs in a separate ring-3 process (`curl __fetch`): it writes
  1592. * the raw response and a small metadata file under /share. While it works,
  1593. * the browser keeps drawing and scrolling its own window, so a slow DNS/TLS
  1594. * handshake or a multi-megabyte body never freezes the UI.
  1595. */
  1596. #define FETCH_REQ "/share/bfreq"
  1597. #define FETCH_BODY "/share/bfbody"
  1598. #define FETCH_META "/share/bfmeta"
  1599. #define FETCH_DONE "/share/bfdone"
  1600. #define FETCH_POST "/share/bfpost"
  1601. struct fetch_meta {
  1602. int status;
  1603. int error;
  1604. int trunc;
  1605. unsigned int len;
  1606. char encoding[16];
  1607. char ctype[64];
  1608. char final[BR_URL_MAX];
  1609. };
  1610. static void draw(unsigned int *action);
  1611. static void handle_keys(unsigned int *action);
  1612. static void handle_page_click(unsigned int *action);
  1613. static int fetch_helper_tty = -2;
  1614. static int fetch_helper_pid = -1;
  1615. static unsigned int fetch_seq;
  1616. static struct fetch_meta fetch_meta;
  1617. static unsigned int meta_uint(char **pp)
  1618. {
  1619. char *p = *pp;
  1620. unsigned int v = 0;
  1621. while (*p >= '0' && *p <= '9') {
  1622. v = v * 10 + (unsigned int)(*p - '0');
  1623. p++;
  1624. }
  1625. while (*p && *p != '\n')
  1626. p++;
  1627. if (*p == '\n')
  1628. p++;
  1629. *pp = p;
  1630. return v;
  1631. }
  1632. static void meta_str(char **pp, char *out, unsigned int cap)
  1633. {
  1634. char *p = *pp;
  1635. unsigned int n = 0;
  1636. while (*p && *p != '\n' && n + 1 < cap)
  1637. out[n++] = *p++;
  1638. out[n] = '\0';
  1639. while (*p && *p != '\n')
  1640. p++;
  1641. if (*p == '\n')
  1642. p++;
  1643. *pp = p;
  1644. }
  1645. static void worker_wait_ui(void)
  1646. {
  1647. unsigned int action = 0;
  1648. if (!ngui_begin(&ui))
  1649. return;
  1650. draw(&action);
  1651. handle_keys(&action);
  1652. handle_page_click(&action);
  1653. if (action & ACT_WAIT_NAV) {
  1654. pending_action |= action & ACT_WAIT_NAV;
  1655. if ((action & ACT_NAV) && click_url[0])
  1656. s_copy(pending_url, click_url, sizeof(pending_url));
  1657. click_url[0] = '\0';
  1658. cancel_fetch = 1;
  1659. }
  1660. if (!ngui_focus(&ui) && ngui_scroll_keys(&ui, &page_scroll))
  1661. ngui_invalidate(&ui);
  1662. ngui_end(&ui);
  1663. }
  1664. static void fetch_helper_stop(void)
  1665. {
  1666. int i;
  1667. if (fetch_helper_pid > 0) {
  1668. u_kill((unsigned int)fetch_helper_pid);
  1669. for (i = 0; i < 40 && u_alive((unsigned int)fetch_helper_pid); i++)
  1670. u_sleep_ms(5);
  1671. /* Let the reaper release the NIC before the next request. */
  1672. u_sleep_ms(10);
  1673. }
  1674. fetch_helper_pid = -1;
  1675. }
  1676. static int fetch_helper_start(char *err, unsigned int errcap)
  1677. {
  1678. if (fetch_helper_pid > 0 &&
  1679. u_alive((unsigned int)fetch_helper_pid))
  1680. return 0;
  1681. fetch_helper_pid = -1;
  1682. if (fetch_helper_tty == -2) {
  1683. int t = u_tty_create();
  1684. fetch_helper_tty = t > 0 ? t : 0;
  1685. }
  1686. fetch_helper_pid = u_spawn("/ramfs/bin/curl", "__serve",
  1687. fetch_helper_tty, "/");
  1688. if (fetch_helper_pid <= 0) {
  1689. fetch_helper_pid = -1;
  1690. s_copy(err, "cannot start network helper", errcap);
  1691. return -1;
  1692. }
  1693. return 0;
  1694. }
  1695. static int worker_fetch(const char *url, unsigned int cap,
  1696. const char *post, unsigned int post_len,
  1697. struct fetch_meta *m, char *err, unsigned int errcap)
  1698. {
  1699. static char req[768], meta[1024], done[24];
  1700. char *p;
  1701. unsigned int n = 0, deadline;
  1702. int rn, mseq, retried = 0;
  1703. struct sys_stat st;
  1704. if (cancel_fetch || pending_action)
  1705. return FETCH_ABORT;
  1706. if (u_stat("/share", &st) != 0 || !st.is_dir ||
  1707. u_stat("/ramfs/bin/curl", &st) != 0) {
  1708. s_copy(err, "network helper unavailable", errcap);
  1709. return -1;
  1710. }
  1711. fetch_seq++;
  1712. if (!fetch_seq)
  1713. fetch_seq = 1;
  1714. s_append_dec(req, &n, fetch_seq, sizeof(req));
  1715. s_append(req, &n, " ", sizeof(req));
  1716. s_append_dec(req, &n, cap, sizeof(req));
  1717. s_append(req, &n, " ", sizeof(req));
  1718. if (post_len) {
  1719. s_append(req, &n, "POST ", sizeof(req));
  1720. s_append_dec(req, &n, post_len, sizeof(req));
  1721. s_append(req, &n, " ", sizeof(req));
  1722. }
  1723. s_append(req, &n, url, sizeof(req));
  1724. s_append(req, &n, "\n", sizeof(req));
  1725. if (post_len && u_syscall3(SYS_WRITE_FILE, (int)FETCH_POST,
  1726. (int)post, (int)post_len) < 0) {
  1727. s_copy(err, "cannot queue form body", errcap);
  1728. return -1;
  1729. }
  1730. u_syscall1(SYS_UNLINK, (int)FETCH_DONE);
  1731. u_syscall1(SYS_UNLINK, (int)FETCH_META);
  1732. if (u_syscall3(SYS_WRITE_FILE, (int)FETCH_REQ, (int)req, (int)n) < 0) {
  1733. s_copy(err, "cannot queue network request", errcap);
  1734. return -1;
  1735. }
  1736. if (fetch_helper_start(err, errcap) != 0)
  1737. return -1;
  1738. deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
  1739. for (;;) {
  1740. worker_wait_ui();
  1741. if (cancel_fetch || pending_action) {
  1742. fetch_helper_stop();
  1743. return FETCH_ABORT;
  1744. }
  1745. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_DONE, (int)done,
  1746. (int)(sizeof(done) - 1));
  1747. if (rn > 0) {
  1748. done[rn] = '\0';
  1749. p = done;
  1750. mseq = (int)meta_uint(&p);
  1751. if (mseq == (int)fetch_seq)
  1752. break;
  1753. }
  1754. if (!u_alive((unsigned int)fetch_helper_pid)) {
  1755. fetch_helper_pid = -1;
  1756. if (!retried && fetch_helper_start(err, errcap) == 0) {
  1757. retried = 1;
  1758. continue;
  1759. }
  1760. s_copy(err, "network helper stopped", errcap);
  1761. return -1;
  1762. }
  1763. if ((int)(u_ticks_ms() - deadline) >= 0) {
  1764. fetch_helper_stop();
  1765. s_copy(err, "network request timed out", errcap);
  1766. return -1;
  1767. }
  1768. u_sleep_ms(20);
  1769. }
  1770. if (cancel_fetch || pending_action)
  1771. return FETCH_ABORT;
  1772. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_META, (int)meta,
  1773. (int)(sizeof(meta) - 1));
  1774. if (rn <= 0) {
  1775. s_copy(err, "network helper failed", errcap);
  1776. return -1;
  1777. }
  1778. meta[rn] = '\0';
  1779. p = meta;
  1780. mseq = (int)meta_uint(&p);
  1781. if (mseq != (int)fetch_seq) {
  1782. s_copy(err, "stale network response", errcap);
  1783. return -1;
  1784. }
  1785. m->status = (int)meta_uint(&p);
  1786. m->error = (int)meta_uint(&p);
  1787. m->trunc = (int)meta_uint(&p);
  1788. meta_str(&p, m->encoding, sizeof(m->encoding));
  1789. meta_str(&p, m->ctype, sizeof(m->ctype));
  1790. meta_str(&p, m->final, sizeof(m->final));
  1791. m->len = meta_uint(&p);
  1792. if (m->error) {
  1793. s_copy(err, uhttp_error_string(m->error), errcap);
  1794. return -1;
  1795. }
  1796. return 0;
  1797. }
  1798. static int fetch_remote(const char *url, unsigned int cap,
  1799. const char *post, unsigned int post_len,
  1800. unsigned char *plain, unsigned int plaincap,
  1801. unsigned char *comp, unsigned int compcap,
  1802. struct fetch_meta *m, int *truncated,
  1803. char *err, unsigned int errcap)
  1804. {
  1805. int rv, zr, was_trunc;
  1806. rv = worker_fetch(url, cap, post, post_len, m, err, errcap);
  1807. if (rv == FETCH_ABORT)
  1808. return FETCH_ABORT;
  1809. if (rv != 0)
  1810. return -1;
  1811. if (contains_ci(m->encoding, "gzip") ||
  1812. contains_ci(m->encoding, "deflate")) {
  1813. unsigned int comp_len = 0, plen = 0;
  1814. rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)comp,
  1815. (int)compcap);
  1816. if (rv < 0) {
  1817. s_copy(err, "empty response", errcap);
  1818. return -1;
  1819. }
  1820. comp_len = (unsigned int)rv;
  1821. if (comp_len < m->len) {
  1822. s_copy(err, "compressed response is too large", errcap);
  1823. return -1;
  1824. }
  1825. if (contains_ci(m->encoding, "deflate"))
  1826. zr = inflate_trunc(comp, comp_len, plain, plaincap,
  1827. &plen, 0, &was_trunc);
  1828. else
  1829. zr = inflate_trunc(comp, comp_len, plain, plaincap,
  1830. &plen, 1, &was_trunc);
  1831. if (zr != 0) {
  1832. s_copy(err, "compressed response is corrupt", errcap);
  1833. return -1;
  1834. }
  1835. *truncated = m->trunc || was_trunc;
  1836. return (cancel_fetch || pending_action) ? FETCH_ABORT :
  1837. (int)plen;
  1838. }
  1839. rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)plain,
  1840. (int)plaincap);
  1841. if (rv < 0) {
  1842. s_copy(err, "empty response", errcap);
  1843. return -1;
  1844. }
  1845. *truncated = m->trunc || ((unsigned int)rv < m->len);
  1846. return (cancel_fetch || pending_action) ? FETCH_ABORT : rv;
  1847. }
  1848. /* ---- HTTP / file fetch ---- */
  1849. static int read_url(const char *url, unsigned int cap, unsigned char **out,
  1850. unsigned int *outlen, int *is_local, char *ctype,
  1851. unsigned int ctcap, char *final, unsigned int finalcap,
  1852. char *err, unsigned int errcap, int *truncated,
  1853. const char *post, unsigned int post_len)
  1854. {
  1855. unsigned char *buf;
  1856. unsigned int len;
  1857. int tr = 0;
  1858. int rv;
  1859. *out = 0;
  1860. *outlen = 0;
  1861. *truncated = 0;
  1862. if (final && finalcap)
  1863. final[0] = '\0';
  1864. if (ctype && ctcap)
  1865. ctype[0] = '\0';
  1866. if (url[0] == '/') {
  1867. char pathbuf[BR_URL_MAX];
  1868. const char *open_url = url;
  1869. unsigned int i = 0;
  1870. /* A local form GET keeps ?query in the address bar but the
  1871. * file lookup uses the path alone, like a real browser. */
  1872. while (url[i] && url[i] != '?' && url[i] != '#')
  1873. i++;
  1874. if (url[i]) {
  1875. s_copy(pathbuf, url, i + 1);
  1876. open_url = pathbuf;
  1877. }
  1878. buf = (unsigned char *)malloc(cap);
  1879. if (!buf) {
  1880. s_copy(err, "out of memory", errcap);
  1881. return -1;
  1882. }
  1883. rv = u_syscall3(SYS_READ_FILE, (int)open_url, (int)buf, (int)cap);
  1884. if (rv < 0) {
  1885. free(buf);
  1886. s_copy(err, "file not found", errcap);
  1887. return -1;
  1888. }
  1889. *out = buf;
  1890. *outlen = (unsigned int)rv;
  1891. *is_local = 1;
  1892. if (ctype)
  1893. s_copy(ctype, "text/html", ctcap);
  1894. if (final)
  1895. s_copy(final, url, finalcap);
  1896. if ((unsigned int)rv == cap)
  1897. *truncated = 1;
  1898. return 0;
  1899. }
  1900. buf = (unsigned char *)malloc(cap);
  1901. if (!buf) {
  1902. s_copy(err, "out of memory", errcap);
  1903. return -1;
  1904. }
  1905. rv = fetch_remote(url, cap, post, post_len, buf, cap, image_io_buf,
  1906. BR_PAGE_COMP_MAX, &fetch_meta, &tr, err, errcap);
  1907. if (rv == FETCH_ABORT) {
  1908. free(buf);
  1909. return FETCH_ABORT;
  1910. }
  1911. if (rv < 0) {
  1912. free(buf);
  1913. return -1;
  1914. }
  1915. len = (unsigned int)rv;
  1916. if (len == 0) {
  1917. free(buf);
  1918. s_copy(err, "empty response", errcap);
  1919. return -1;
  1920. }
  1921. if (ctype)
  1922. s_copy(ctype, fetch_meta.ctype, ctcap);
  1923. if (final && fetch_meta.final[0])
  1924. s_copy(final, fetch_meta.final, finalcap);
  1925. *is_local = 0;
  1926. *out = buf;
  1927. *outlen = len;
  1928. *truncated = tr;
  1929. return 0;
  1930. }
  1931. /* Load every @font-face the parser registered. Local files go through
  1932. * ufont_face_open_path(); remote files use the fetch helper (identity, the
  1933. * TTF parser has no gzip). A failed face stays NULL: layout then falls back
  1934. * to the built-in regular/bold/italic/mono set. */
  1935. static void load_font_faces(void)
  1936. {
  1937. unsigned int i;
  1938. memset(font_faces, 0, sizeof(font_faces));
  1939. for (i = 1; i <= (unsigned int)br_font_face_count() &&
  1940. i <= BR_MAX_FACES; i++) {
  1941. const char *url = br_font_face_url((int)i);
  1942. const char *base = br_font_face_base((int)i);
  1943. char abs[BR_URL_MAX];
  1944. if (!url || !url[0])
  1945. continue;
  1946. if (resolve_link(base && base[0] ? base : page_url, url, abs,
  1947. sizeof(abs)) != 0)
  1948. continue;
  1949. if (abs[0] == '/') {
  1950. font_faces[i - 1] = ufont_face_open_path(abs);
  1951. } else if (is_http(abs)) {
  1952. unsigned char *buf;
  1953. char err[64];
  1954. int rv, tr = 0;
  1955. buf = (unsigned char *)malloc(UFONT_MAX_FILE);
  1956. if (!buf)
  1957. continue;
  1958. rv = fetch_remote(abs, UFONT_MAX_FILE, 0, 0, buf,
  1959. UFONT_MAX_FILE, image_io_buf,
  1960. BR_PAGE_COMP_MAX, &fetch_meta, &tr,
  1961. err, sizeof(err));
  1962. if (rv > 0)
  1963. font_faces[i - 1] = ufont_face_open_mem(buf,
  1964. rv);
  1965. free(buf);
  1966. }
  1967. }
  1968. }
  1969. /* Remote scripts are cached under /share for the current boot, like the
  1970. * stylesheet cache, so reloads and later visits skip the network. */
  1971. static int script_cache_count;
  1972. static unsigned int script_cache_hash(const char *s)
  1973. {
  1974. unsigned int h = 2166136261u;
  1975. while (*s) {
  1976. h ^= (unsigned char)*s++;
  1977. h *= 16777619u;
  1978. }
  1979. return h;
  1980. }
  1981. static void script_cache_path(const char *url, char *out, unsigned int cap)
  1982. {
  1983. unsigned int n = 0;
  1984. s_append(out, &n, "/share/" SCRIPT_CACHE_PREFIX, cap);
  1985. s_append_dec(out, &n, script_cache_hash(url), cap);
  1986. }
  1987. static void script_cache_clear(void)
  1988. {
  1989. struct sys_dirent d;
  1990. char path[96];
  1991. for (;;) {
  1992. int i, found = 0;
  1993. for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
  1994. (int)&d) == 1; i++) {
  1995. unsigned int n = 0;
  1996. if (!starts_with(d.name, SCRIPT_CACHE_PREFIX))
  1997. continue;
  1998. s_append(path, &n, "/share/", sizeof(path));
  1999. s_append(path, &n, d.name, sizeof(path));
  2000. if (u_syscall1(SYS_UNLINK, (int)path) < 0)
  2001. return;
  2002. found = 1;
  2003. break;
  2004. }
  2005. if (!found)
  2006. break;
  2007. }
  2008. script_cache_count = 0;
  2009. }
  2010. static void script_cache_init(void)
  2011. {
  2012. struct sys_dirent d;
  2013. int i, count = 0;
  2014. for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
  2015. (int)&d) == 1; i++)
  2016. if (starts_with(d.name, SCRIPT_CACHE_PREFIX))
  2017. count++;
  2018. if (count >= SCRIPT_CACHE_MAX) {
  2019. script_cache_clear();
  2020. return;
  2021. }
  2022. script_cache_count = count;
  2023. }
  2024. static int script_cache_get(const char *url, unsigned char **out,
  2025. unsigned int *outlen)
  2026. {
  2027. char path[96];
  2028. struct sys_stat st;
  2029. unsigned char *buf;
  2030. int rn;
  2031. script_cache_path(url, path, sizeof(path));
  2032. if (u_stat(path, &st) != 0 || st.is_dir || st.size == 0 ||
  2033. st.size >= SCRIPT_CACHE_FILE_MAX)
  2034. return 0;
  2035. buf = (unsigned char *)malloc(st.size + 1);
  2036. if (!buf)
  2037. return 0;
  2038. rn = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)st.size);
  2039. if (rn != (int)st.size) {
  2040. free(buf);
  2041. return 0;
  2042. }
  2043. buf[st.size] = '\0';
  2044. *out = buf;
  2045. *outlen = st.size;
  2046. return 1;
  2047. }
  2048. static void script_cache_put(const char *url, const unsigned char *data,
  2049. unsigned int len)
  2050. {
  2051. char path[96];
  2052. if (script_cache_count >= SCRIPT_CACHE_MAX || len == 0 ||
  2053. len >= SCRIPT_CACHE_FILE_MAX)
  2054. return;
  2055. script_cache_path(url, path, sizeof(path));
  2056. if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)data,
  2057. (int)len) >= 0)
  2058. script_cache_count++;
  2059. }
  2060. /* Fetch one external script into a heap buffer and NUL-terminate it. */
  2061. static int fetch_script_text(const char *url, unsigned char **out,
  2062. unsigned int *outlen, char *err,
  2063. unsigned int errcap)
  2064. {
  2065. unsigned char *buf;
  2066. unsigned int len;
  2067. int local, truncated = 0;
  2068. char ctype[64], final[BR_URL_MAX];
  2069. int rv;
  2070. *out = 0;
  2071. *outlen = 0;
  2072. if (url[0] != '/' && script_cache_get(url, out, outlen))
  2073. return 0;
  2074. rv = read_url(url, BR_SCRIPT_MAX, &buf, &len, &local, ctype,
  2075. sizeof(ctype), final, sizeof(final), err, errcap,
  2076. &truncated, 0, 0);
  2077. if (rv != 0)
  2078. return rv;
  2079. if (truncated || len >= BR_SCRIPT_MAX) {
  2080. free(buf);
  2081. s_copy(err, "script is too large", errcap);
  2082. return -1;
  2083. }
  2084. buf[len] = '\0';
  2085. if (!local)
  2086. script_cache_put(url, buf, len);
  2087. *out = buf;
  2088. *outlen = len;
  2089. return 0;
  2090. }
  2091. /* Fetch an image into the fixed scratch buffers. The scaled bitmaps live on
  2092. * the heap, so images must not allocate their file body from it as well. */
  2093. static int fetch_image(const char *url, unsigned int cap,
  2094. unsigned char **out, unsigned int *outlen,
  2095. char *err, unsigned int errcap)
  2096. {
  2097. unsigned int len;
  2098. int tr = 0;
  2099. int rv;
  2100. *out = 0;
  2101. *outlen = 0;
  2102. if (cap > BR_IMG_FILE_MAX)
  2103. cap = BR_IMG_FILE_MAX;
  2104. if (url[0] == '/') {
  2105. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_file_buf,
  2106. (int)cap);
  2107. if (rv < 0) {
  2108. s_copy(err, "file not found", errcap);
  2109. return -1;
  2110. }
  2111. *out = image_file_buf;
  2112. *outlen = (unsigned int)rv;
  2113. return 0;
  2114. }
  2115. rv = fetch_remote(url, cap, 0, 0, image_file_buf, cap, image_comp_buf,
  2116. BR_IMG_FILE_MAX, &fetch_meta, &tr, err, errcap);
  2117. if (rv == FETCH_ABORT)
  2118. return FETCH_ABORT;
  2119. if (rv < 0)
  2120. return -1;
  2121. len = (unsigned int)rv;
  2122. if (len == 0) {
  2123. s_copy(err, "empty image", errcap);
  2124. return -1;
  2125. }
  2126. *out = image_file_buf;
  2127. *outlen = len;
  2128. return 0;
  2129. }
  2130. /* Remote stylesheets are immutable enough for the current boot: keep
  2131. * successful responses under /share so a reload, a second visit or a fresh
  2132. * browser launch does not fetch the same five sheets over the network again.
  2133. * /share is cleared by reboot/kexec; when the cache fills up it is reset. */
  2134. static int css_cache_count;
  2135. static unsigned int css_cache_hash(const char *s)
  2136. {
  2137. unsigned int h = 2166136261u;
  2138. while (*s) {
  2139. h ^= (unsigned char)*s++;
  2140. h *= 16777619u;
  2141. }
  2142. return h;
  2143. }
  2144. static void css_cache_path(const char *url, char *out, unsigned int cap)
  2145. {
  2146. unsigned int n = 0;
  2147. s_append(out, &n, "/share/" CSS_CACHE_PREFIX, cap);
  2148. s_append_dec(out, &n, css_cache_hash(url), cap);
  2149. }
  2150. static void css_cache_clear(void)
  2151. {
  2152. struct sys_dirent d;
  2153. char path[96];
  2154. for (;;) {
  2155. int i, found = 0;
  2156. for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
  2157. (int)&d) == 1; i++) {
  2158. unsigned int n = 0;
  2159. if (!starts_with(d.name, CSS_CACHE_PREFIX))
  2160. continue;
  2161. s_append(path, &n, "/share/", sizeof(path));
  2162. s_append(path, &n, d.name, sizeof(path));
  2163. if (u_syscall1(SYS_UNLINK, (int)path) < 0)
  2164. return;
  2165. found = 1;
  2166. break;
  2167. }
  2168. if (!found)
  2169. break;
  2170. }
  2171. css_cache_count = 0;
  2172. }
  2173. static void css_cache_init(void)
  2174. {
  2175. struct sys_dirent d;
  2176. int i, count = 0;
  2177. for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
  2178. (int)&d) == 1; i++)
  2179. if (starts_with(d.name, CSS_CACHE_PREFIX))
  2180. count++;
  2181. if (count >= CSS_CACHE_MAX) {
  2182. css_cache_clear();
  2183. return;
  2184. }
  2185. css_cache_count = count;
  2186. }
  2187. static int css_cache_get(const char *url, unsigned char **out,
  2188. unsigned int *outlen)
  2189. {
  2190. char path[96];
  2191. int rn;
  2192. css_cache_path(url, path, sizeof(path));
  2193. rn = u_syscall3(SYS_READ_FILE, (int)path, (int)image_io_buf,
  2194. (int)CSS_CACHE_FILE_MAX);
  2195. if (rn <= 0 || (unsigned int)rn >= CSS_CACHE_FILE_MAX)
  2196. return 0;
  2197. *out = image_io_buf;
  2198. *outlen = (unsigned int)rn;
  2199. return 1;
  2200. }
  2201. static void css_cache_put(const char *url, const unsigned char *data,
  2202. unsigned int len)
  2203. {
  2204. char path[96];
  2205. if (css_cache_count >= CSS_CACHE_MAX || len == 0 ||
  2206. len >= CSS_CACHE_FILE_MAX)
  2207. return;
  2208. css_cache_path(url, path, sizeof(path));
  2209. if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)data,
  2210. (int)len) >= 0)
  2211. css_cache_count++;
  2212. }
  2213. /* Read one stylesheet into the shared static buffer. CSS is parsed
  2214. * immediately, so it does not need the page heap; this leaves the heap for
  2215. * the 2 MB page body, the rules and the scaled image bitmaps. */
  2216. static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
  2217. char *err, unsigned int errcap)
  2218. {
  2219. unsigned int len;
  2220. int tr = 0;
  2221. int rv;
  2222. *out = 0;
  2223. *outlen = 0;
  2224. if (url[0] == '/') {
  2225. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_io_buf,
  2226. (int)BR_CSS_MAX);
  2227. if (rv < 0) {
  2228. s_copy(err, "stylesheet not found", errcap);
  2229. return -1;
  2230. }
  2231. *out = image_io_buf;
  2232. *outlen = (unsigned int)rv;
  2233. return 0;
  2234. }
  2235. if (css_cache_get(url, out, outlen))
  2236. return 0;
  2237. rv = fetch_remote(url, BR_CSS_MAX, 0, 0, image_io_buf, BR_CSS_MAX,
  2238. image_io_buf + BR_CSS_MAX, BR_CSS_COMP_MAX,
  2239. &fetch_meta, &tr, err, errcap);
  2240. if (rv == FETCH_ABORT)
  2241. return FETCH_ABORT;
  2242. if (rv < 0)
  2243. return -1;
  2244. len = (unsigned int)rv;
  2245. if (len == 0) {
  2246. s_copy(err, "empty stylesheet", errcap);
  2247. return -1;
  2248. }
  2249. *out = image_io_buf;
  2250. *outlen = len;
  2251. css_cache_put(url, image_io_buf, len);
  2252. return 0;
  2253. }
  2254. static void redraw_now(void);
  2255. static char error_page[1024];
  2256. static void show_error(const char *title, const char *detail)
  2257. {
  2258. char *page = error_page;
  2259. unsigned int n = 0;
  2260. s_append(page, &n, "<html><body><h2>", sizeof(error_page));
  2261. s_append(page, &n, title, sizeof(error_page));
  2262. s_append(page, &n, "</h2><p>", sizeof(error_page));
  2263. {
  2264. const char *p = detail;
  2265. while (*p && n + 1 < sizeof(error_page)) {
  2266. if (*p == '<') s_append(page, &n, "&lt;", sizeof(error_page));
  2267. else if (*p == '>') s_append(page, &n, "&gt;", sizeof(error_page));
  2268. else if (*p == '&') s_append(page, &n, "&amp;", sizeof(error_page));
  2269. else page[n++] = *p;
  2270. p++;
  2271. }
  2272. }
  2273. page[n] = '\0';
  2274. s_append(page, &n, "</p><p>", sizeof(error_page));
  2275. s_append(page, &n, page_url, sizeof(error_page));
  2276. s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
  2277. sizeof(error_page));
  2278. br_parse(&doc, page, n);
  2279. layout_valid = 0;
  2280. preload_active = 0;
  2281. preload_pending = 0;
  2282. preload_wait = 0;
  2283. s_copy(page_title, title, sizeof(page_title));
  2284. page_loading = 0;
  2285. set_status(title, 1, 0);
  2286. ngui_invalidate(&ui);
  2287. }
  2288. static int decode_direct_image(const unsigned char *data, unsigned int len,
  2289. unsigned int node)
  2290. {
  2291. if (image_count >= BR_MAX_IMAGES)
  2292. return -1;
  2293. if (decode_image_data(data, len, &images[0]) != 0)
  2294. return -1;
  2295. doc.nodes[node].img_index = 0;
  2296. image_count = 1;
  2297. return 0;
  2298. }
  2299. static int ancestor_is(unsigned int node, unsigned int tag)
  2300. {
  2301. unsigned int p = doc.nodes[node].parent;
  2302. while (p) {
  2303. if (doc.nodes[p].tag == tag)
  2304. return 1;
  2305. p = doc.nodes[p].parent;
  2306. }
  2307. return 0;
  2308. }
  2309. static int load_stylesheets(void)
  2310. {
  2311. static char seen[BR_MAX_SHEETS][BR_URL_MAX];
  2312. unsigned int i, sheets = 0, nseen = 0;
  2313. for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) {
  2314. struct br_node *nd = &doc.nodes[i];
  2315. const char *rel, *href;
  2316. char url[BR_URL_MAX];
  2317. unsigned char *data;
  2318. unsigned int len;
  2319. char err[64];
  2320. int rv;
  2321. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK)
  2322. continue;
  2323. rel = br_rel(&doc, i);
  2324. if (!contains_ci(rel, "stylesheet"))
  2325. continue;
  2326. href = br_href(&doc, i);
  2327. if (!href[0])
  2328. continue;
  2329. if (resolve_link(page_url, href, url, sizeof(url)) != 0)
  2330. continue;
  2331. {
  2332. unsigned int k;
  2333. int dup = 0;
  2334. for (k = 0; k < nseen; k++)
  2335. if (s_eq(seen[k], url)) {
  2336. dup = 1;
  2337. break;
  2338. }
  2339. if (dup)
  2340. continue;
  2341. if (nseen < BR_MAX_SHEETS) {
  2342. s_copy(seen[nseen], url, BR_URL_MAX);
  2343. nseen++;
  2344. }
  2345. }
  2346. rv = fetch_css(url, &data, &len, err, sizeof(err));
  2347. if (rv == FETCH_ABORT)
  2348. return FETCH_ABORT;
  2349. if (rv != 0)
  2350. continue;
  2351. if (len) {
  2352. br_set_font_base(url);
  2353. br_parse_css_text(&doc, (const char *)data, len);
  2354. }
  2355. sheets++;
  2356. }
  2357. br_set_font_base(page_url);
  2358. br_parse_inline_css(&doc);
  2359. load_font_faces();
  2360. br_reset_styles(&doc);
  2361. layout_valid = 0;
  2362. return 0;
  2363. }
  2364. static void preload_begin(void)
  2365. {
  2366. preload_active = 1;
  2367. preload_pending = 0;
  2368. preload_wait = 1;
  2369. preload_node = 0;
  2370. preload_loaded = 0;
  2371. preload_scroll = 0;
  2372. }
  2373. /* Styles are computed top-down during layout, but images inside a hidden
  2374. * menu/carousel are never visited and keep img_top == 0. Without this check
  2375. * the preloader walks the DOM in node order and spends the whole image
  2376. * budget on logos and off-canvas thumbnails the reader cannot see. */
  2377. static int node_hidden(unsigned int node)
  2378. {
  2379. unsigned int path[32], n = 0, p = node, i;
  2380. struct br_style st, parent;
  2381. while (p && n < sizeof(path) / sizeof(path[0])) {
  2382. path[n++] = p;
  2383. p = doc.nodes[p].parent;
  2384. }
  2385. parent = (struct br_style){0};
  2386. parent.font_px = 16;
  2387. parent.display = BR_DISPLAY_BLOCK;
  2388. for (i = n; i > 0; i--) {
  2389. br_style_for(&doc, path[i - 1], &parent, &st);
  2390. if (st.display == BR_DISPLAY_NONE || st.vis_hidden ||
  2391. st.opacity_hidden)
  2392. return 1;
  2393. parent = st;
  2394. }
  2395. return 0;
  2396. }
  2397. static struct br_style base_style(void);
  2398. static void style_for_node(unsigned int node, struct br_style *out)
  2399. {
  2400. unsigned int p = doc.nodes[node].parent;
  2401. struct br_style parent;
  2402. if (p) {
  2403. style_for_node(p, &parent);
  2404. br_style_for(&doc, node, &parent, out);
  2405. } else {
  2406. parent = base_style();
  2407. br_style_for(&doc, node, &parent, out);
  2408. }
  2409. }
  2410. /* Load at most one image per call. Images below the current viewport are
  2411. * left pending until the page is scrolled; this keeps a long article with
  2412. * many 4K screenshots from stalling on content the user cannot see yet. */
  2413. static void preload_step(void)
  2414. {
  2415. unsigned int batch = 0;
  2416. while (preload_active && preload_node < doc.nnode) {
  2417. unsigned int i = preload_node;
  2418. struct br_node *nd = &doc.nodes[i];
  2419. const char *src;
  2420. char url[BR_URL_MAX];
  2421. unsigned char *data;
  2422. unsigned int len;
  2423. char err[64];
  2424. char msg[64];
  2425. unsigned int n = 0;
  2426. int rv;
  2427. if (nd->type != BR_NODE_ELEM) {
  2428. preload_node++;
  2429. continue;
  2430. }
  2431. if (nd->tag != BR_TAG_IMG) {
  2432. if (nd->bg_img_index == -1) {
  2433. struct br_style bs;
  2434. style_for_node(i, &bs);
  2435. if (bs.display != BR_DISPLAY_NONE &&
  2436. !bs.vis_hidden && bs.bg_image) {
  2437. const char *bsrc =
  2438. br_bg_image_url(bs.bg_image - 1);
  2439. if (bsrc[0] &&
  2440. !contains_ci(bsrc, ".svg") &&
  2441. !starts_with(bsrc, "data:") &&
  2442. resolve_link(page_url, bsrc, url,
  2443. sizeof(url)) == 0) {
  2444. if (image_count >=
  2445. BR_MAX_IMAGES) {
  2446. preload_node =
  2447. doc.nnode;
  2448. break;
  2449. }
  2450. preload_node = i + 1;
  2451. data = 0;
  2452. len = 0;
  2453. rv = fetch_image(url,
  2454. BR_IMG_FILE_MAX,
  2455. &data, &len,
  2456. err, sizeof(err));
  2457. if (rv == FETCH_ABORT) {
  2458. preload_active = 0;
  2459. preload_pending = 0;
  2460. return;
  2461. }
  2462. if (rv == 0 &&
  2463. decode_image_data(
  2464. data, len,
  2465. &images[image_count]) == 0) {
  2466. nd->bg_img_index =
  2467. (short)image_count;
  2468. image_count++;
  2469. preload_loaded++;
  2470. layout_valid = 0;
  2471. } else {
  2472. nd->bg_img_index = -2;
  2473. }
  2474. ngui_invalidate(&ui);
  2475. if (++batch >= 4)
  2476. return;
  2477. continue;
  2478. }
  2479. }
  2480. nd->bg_img_index = -2;
  2481. }
  2482. preload_node++;
  2483. continue;
  2484. }
  2485. if (nd->img_index != -1) {
  2486. preload_node++;
  2487. continue;
  2488. }
  2489. if (ancestor_is(i, BR_TAG_NOSCRIPT) || node_hidden(i)) {
  2490. preload_node++;
  2491. continue;
  2492. }
  2493. src = br_src(&doc, i);
  2494. if (!src[0] || contains_ci(src, ".svg") ||
  2495. starts_with(src, "data:")) {
  2496. preload_node++;
  2497. continue;
  2498. }
  2499. if (resolve_link(page_url, src, url, sizeof(url)) != 0) {
  2500. preload_node++;
  2501. continue;
  2502. }
  2503. if (image_count >= BR_MAX_IMAGES) {
  2504. preload_node = doc.nnode;
  2505. break;
  2506. }
  2507. /* Do not stop at the first below-fold image: DOM order is
  2508. * not visual order, and a footer/ad image early in the DOM
  2509. * used to hide every later article image from the loader. */
  2510. if (img_top[i] >
  2511. (unsigned int)(page_scroll.offset + page_rect.h + 160)) {
  2512. preload_pending = 1;
  2513. preload_node++;
  2514. continue;
  2515. }
  2516. preload_node = i + 1;
  2517. s_append(msg, &n, "Loading images ", sizeof(msg));
  2518. s_append_dec(msg, &n, (unsigned int)preload_loaded + 1,
  2519. sizeof(msg));
  2520. s_append(msg, &n, "...", sizeof(msg));
  2521. set_status(msg, 0, 0);
  2522. ngui_invalidate(&ui);
  2523. data = 0;
  2524. len = 0;
  2525. rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
  2526. sizeof(err));
  2527. if (rv == FETCH_ABORT) {
  2528. preload_active = 0;
  2529. preload_pending = 0;
  2530. return;
  2531. }
  2532. if (rv == 0 &&
  2533. decode_image_data(data, len, &images[image_count]) == 0) {
  2534. nd->img_index = (short)image_count;
  2535. image_count++;
  2536. preload_loaded++;
  2537. layout_valid = 0;
  2538. } else {
  2539. /* Remember failures so a scroll does not retry an
  2540. * unreachable tracking/ad image with the full network
  2541. * timeout on every pass. */
  2542. nd->img_index = -2;
  2543. }
  2544. ngui_invalidate(&ui);
  2545. /* A full page layout after every image dominates loading on a
  2546. * long page. Decode a small batch, then let the main loop draw
  2547. * once for the whole batch. */
  2548. if (++batch >= 4)
  2549. return;
  2550. }
  2551. preload_active = 0;
  2552. if (preload_pending)
  2553. preload_scroll = page_scroll.offset;
  2554. else
  2555. preload_pending = 0;
  2556. set_status(post_status_text, post_status_error, post_status_ttl);
  2557. ngui_invalidate(&ui);
  2558. }
  2559. static void history_push(const char *url)
  2560. {
  2561. if (!url || !url[0])
  2562. return;
  2563. if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len &&
  2564. s_eq(history[hist_pos], url))
  2565. return;
  2566. if (hist_len > 0 && hist_pos + 1 < hist_len)
  2567. hist_len = hist_pos + 1;
  2568. if (hist_len < BR_HISTORY) {
  2569. s_copy(history[hist_len], url, BR_URL_MAX);
  2570. hist_pos = hist_len;
  2571. hist_len++;
  2572. } else {
  2573. int i;
  2574. for (i = 1; i < BR_HISTORY; i++)
  2575. s_copy(history[i - 1], history[i], BR_URL_MAX);
  2576. s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX);
  2577. hist_pos = BR_HISTORY - 1;
  2578. }
  2579. }
  2580. static void load_page(const char *url, int local, int push)
  2581. {
  2582. unsigned char *body = 0;
  2583. unsigned int len = 0;
  2584. int truncated = 0;
  2585. struct img_info image_info;
  2586. char ctype[64];
  2587. char final[BR_URL_MAX];
  2588. char err[80];
  2589. const char *detail = 0;
  2590. const char *post = pending_post_len ? pending_post : 0;
  2591. unsigned int post_len = pending_post_len;
  2592. int rv;
  2593. final[0] = '\0';
  2594. pending_post_len = 0;
  2595. cancel_fetch = 0;
  2596. preload_active = 0;
  2597. preload_pending = 0;
  2598. js_browser_stop();
  2599. js_last_status[0] = '\0';
  2600. js_last_error = 0;
  2601. heap_clear();
  2602. image_reset();
  2603. form_reset_state();
  2604. layout_valid = 0;
  2605. page_scroll.offset = 0;
  2606. page_scroll.dragging = 0;
  2607. br_parse_plain(&doc, "", 0); /* blank while loading */
  2608. page_title[0] = '\0';
  2609. page_loading = 1;
  2610. page_is_local = local;
  2611. s_copy(page_url, url, sizeof(page_url));
  2612. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  2613. url_in.len = s_len(url_in.text);
  2614. url_in.cursor = url_in.len;
  2615. url_in.scroll = 0;
  2616. ngui_set_focus(&ui, 0);
  2617. if (final[0])
  2618. final[0] = '\0';
  2619. if (push)
  2620. history_push(url);
  2621. set_status("Loading...", 0, 0);
  2622. ngui_invalidate(&ui);
  2623. /* heap_clear() detaches the fonts, so attach them again before the
  2624. * first frame or the URL bar flashes in the 8x16 bitmap fallback. */
  2625. if (heap_start() != 0) {
  2626. show_error("Out of memory", "Cannot allocate the page buffer");
  2627. return;
  2628. }
  2629. ngui_font_attach(&ui);
  2630. redraw_now();
  2631. browser_rules_alloc();
  2632. rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
  2633. sizeof(ctype), final, sizeof(final), err, sizeof(err),
  2634. &truncated, post, post_len);
  2635. if (rv == FETCH_ABORT)
  2636. return;
  2637. if (rv != 0) {
  2638. detail = err;
  2639. show_error("Cannot load this page", detail);
  2640. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  2641. url_in.len = s_len(url_in.text);
  2642. url_in.cursor = url_in.len;
  2643. url_in.scroll = 0;
  2644. return;
  2645. }
  2646. if (!local)
  2647. origin_cache_learn(url, final[0] ? final : url);
  2648. page_is_local = local;
  2649. if (final[0]) {
  2650. s_copy(page_url, final, sizeof(page_url));
  2651. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  2652. url_in.len = s_len(url_in.text);
  2653. url_in.cursor = url_in.len;
  2654. url_in.scroll = 0;
  2655. }
  2656. if (contains_ci(ctype, "text/plain")) {
  2657. br_parse_plain(&doc, (const char *)body, len);
  2658. } else if (contains_ci(ctype, "image/") ||
  2659. (!contains_ci(ctype, "text/html") &&
  2660. img_sniff(body, len, &image_info) == 0)) {
  2661. unsigned int node = br_parse_image_doc(&doc, page_url);
  2662. if (node)
  2663. decode_direct_image(body, len, node);
  2664. s_copy(page_title, "Image", sizeof(page_title));
  2665. } else {
  2666. br_set_viewport(ui.w);
  2667. br_parse(&doc, (const char *)body, len);
  2668. s_copy(page_title, doc.title[0] ? doc.title : page_url,
  2669. sizeof(page_title));
  2670. if (load_stylesheets() == FETCH_ABORT)
  2671. return;
  2672. if (pending_action)
  2673. return;
  2674. }
  2675. preload_begin();
  2676. page_loading = 0;
  2677. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  2678. url_in.len = s_len(url_in.text);
  2679. url_in.cursor = url_in.len;
  2680. url_in.scroll = 0;
  2681. ngui_set_focus(&ui, 0);
  2682. /* Paint the parsed page before fetching/running scripts. A slow
  2683. * remote <script src> then does not hide the page behind the loading
  2684. * screen; the fetch helper pumps this UI while it waits. */
  2685. set_status("Loading scripts...", 0, 0);
  2686. redraw_now();
  2687. run_page_scripts();
  2688. if (pending_action)
  2689. return;
  2690. post_status_text[0] = '\0';
  2691. post_status_error = 0;
  2692. post_status_ttl = 0;
  2693. if (js_last_status[0]) {
  2694. s_copy(post_status_text, js_last_status,
  2695. sizeof(post_status_text));
  2696. post_status_error = js_last_error;
  2697. post_status_ttl = 8000;
  2698. } else if (truncated) {
  2699. s_copy(post_status_text, "Page truncated at the buffer limit",
  2700. sizeof(post_status_text));
  2701. post_status_ttl = 6000;
  2702. } else if (doc.truncated) {
  2703. s_copy(post_status_text, "Page truncated by the parser",
  2704. sizeof(post_status_text));
  2705. post_status_ttl = 6000;
  2706. } else if (starts_with(page_url, "https://")) {
  2707. s_copy(post_status_text, "HTTPS: certificate NOT verified",
  2708. sizeof(post_status_text));
  2709. post_status_ttl = 6000;
  2710. }
  2711. set_status(post_status_text, post_status_error, post_status_ttl);
  2712. ngui_invalidate(&ui);
  2713. }
  2714. static void load_home(int push)
  2715. {
  2716. pending_post_len = 0;
  2717. cancel_fetch = 0;
  2718. preload_active = 0;
  2719. preload_pending = 0;
  2720. js_browser_stop();
  2721. js_last_status[0] = '\0';
  2722. js_last_error = 0;
  2723. heap_clear();
  2724. image_reset();
  2725. form_reset_state();
  2726. layout_valid = 0;
  2727. page_scroll.offset = 0;
  2728. page_scroll.dragging = 0;
  2729. br_parse_plain(&doc, "", 0);
  2730. page_loading = 1;
  2731. page_is_local = 0;
  2732. s_copy(page_url, "about:home", sizeof(page_url));
  2733. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  2734. url_in.len = s_len(url_in.text);
  2735. url_in.cursor = url_in.len;
  2736. url_in.scroll = 0;
  2737. ngui_set_focus(&ui, 0);
  2738. if (push)
  2739. history_push(page_url);
  2740. ngui_invalidate(&ui);
  2741. /* See load_page(): reattach the fonts before the first redraw. */
  2742. if (heap_start() != 0) {
  2743. show_error("Out of memory", "Cannot allocate the browser heap");
  2744. return;
  2745. }
  2746. ngui_font_attach(&ui);
  2747. redraw_now();
  2748. browser_rules_alloc();
  2749. {
  2750. static const char home[] =
  2751. "<html><head><title>NebOS Browser</title><style>"
  2752. "body { margin: 10px; color: #e4e6f0; }"
  2753. "h1 { color: #7fb0ff; }"
  2754. ".box { background-color: #262b3a; margin: 12px; "
  2755. "padding: 10px; }"
  2756. "a { color: #7fb0ff; }"
  2757. "pre { background-color: #32384a; }"
  2758. "</style></head><body>"
  2759. "<h1>NebOS Browser</h1>"
  2760. "<p>A small HTML/CSS browser built on <b>ngui</b>, the "
  2761. "shared HTTP client and the image library.</p>"
  2762. "<div class=\"box\"><b>Try:</b>"
  2763. "<ul><li><a href=\"/floppy1/DEMO.HTM\">/"
  2764. "floppy1/DEMO.HTM</a> on the root floppy</li>"
  2765. "<li><a href=\"/floppy1/JS.HTM\">/"
  2766. "floppy1/JS.HTM</a> inline JavaScript demo</li>"
  2767. "<li><a href=\"http://10.0.2.2:8000/\">"
  2768. "http://10.0.2.2:8000/</a> on the QEMU host</li>"
  2769. "<li><a href=\"https://example.com/\">"
  2770. "https://example.com/</a> (TLS, not verified)</li>"
  2771. "</ul></div>"
  2772. "<p>Type a URL in the bar above. Esc closes the "
  2773. "window, Ctrl+L focuses the address bar.</p>"
  2774. "<pre>javascript: inline + external scripts (limited DOM)\nforms: not supported"
  2775. "\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
  2776. "</body></html>";
  2777. br_set_viewport(ui.w);
  2778. br_parse(&doc, home, sizeof(home) - 1);
  2779. br_parse_inline_css(&doc);
  2780. br_reset_styles(&doc);
  2781. s_copy(page_title, doc.title, sizeof(page_title));
  2782. }
  2783. post_status_text[0] = '\0';
  2784. post_status_error = 0;
  2785. post_status_ttl = 0;
  2786. page_loading = 0;
  2787. set_status("", 0, 0);
  2788. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  2789. url_in.len = s_len(url_in.text);
  2790. url_in.cursor = url_in.len;
  2791. url_in.scroll = 0;
  2792. ngui_set_focus(&ui, 0);
  2793. ngui_invalidate(&ui);
  2794. }
  2795. /* Extensions the browser opens from the local filesystem. A dotted input
  2796. * with one of these still resolves relative to the cwd. */
  2797. static int local_file_ext(const char *s)
  2798. {
  2799. static const char *const exts[] = {
  2800. "htm", "html", "txt", "text", "md", "css", "js", "json", "xml",
  2801. "png", "jpg", "jpeg", "gif", "bmp", "tga", "ppm", "pgm", "pbm",
  2802. "ico", "cur", "pam", "svg", "elf", "bin", "ttf", "log", "ini",
  2803. "cfg", "conf", 0
  2804. };
  2805. unsigned int i = 0, start = 0, len = 0, e;
  2806. while (s[i] && s[i] != '/' && s[i] != ':' && s[i] != '?' &&
  2807. s[i] != '#') {
  2808. if (s[i] == '.') {
  2809. start = i + 1;
  2810. len = 0;
  2811. } else if (start) {
  2812. len++;
  2813. }
  2814. i++;
  2815. }
  2816. if (!len)
  2817. return 0;
  2818. for (e = 0; exts[e]; e++) {
  2819. const char *x = exts[e];
  2820. unsigned int xn = s_len(x), k;
  2821. if (xn != len)
  2822. continue;
  2823. for (k = 0; k < len; k++) {
  2824. char a = s[start + k];
  2825. if (a >= 'A' && a <= 'Z')
  2826. a = (char)(a - 'A' + 'a');
  2827. if (a != x[k])
  2828. break;
  2829. }
  2830. if (k == len)
  2831. return 1;
  2832. }
  2833. return 0;
  2834. }
  2835. /* A dotted first component (cloudron.io) is a hostname, not a local file.
  2836. * Skipping the VFS stat keeps a slow FAT/floppy lookup off the path from
  2837. * Enter to the first network request. */
  2838. static int looks_like_host(const char *s)
  2839. {
  2840. unsigned int i = 0;
  2841. int dot = 0;
  2842. while (s[i] && s[i] != '/' && s[i] != '?' && s[i] != '#' &&
  2843. s[i] != ':') {
  2844. char c = s[i];
  2845. if (c == '.') {
  2846. dot = 1;
  2847. } else if (!((c >= 'a' && c <= 'z') ||
  2848. (c >= 'A' && c <= 'Z') ||
  2849. (c >= '0' && c <= '9') ||
  2850. c == '-' || c == '_')) {
  2851. return 0;
  2852. }
  2853. i++;
  2854. }
  2855. return dot && i > 0 && !local_file_ext(s);
  2856. }
  2857. static void load_remote(const char *url, int push)
  2858. {
  2859. char canon[BR_URL_MAX];
  2860. s_copy(canon, url, sizeof(canon));
  2861. origin_cache_apply(canon, sizeof(canon));
  2862. load_page(canon, 0, push);
  2863. }
  2864. static void navigate(const char *input, int push)
  2865. {
  2866. char target[BR_URL_MAX];
  2867. char *t;
  2868. if (!input)
  2869. return;
  2870. s_copy(target, input, sizeof(target));
  2871. t = trim(target);
  2872. if (!t[0])
  2873. return;
  2874. if (s_eq(t, "about:home")) {
  2875. load_home(push);
  2876. return;
  2877. }
  2878. if (starts_with(t, "file://")) {
  2879. make_abs_local(t + 7, target, sizeof(target));
  2880. load_page(target, 1, push);
  2881. return;
  2882. }
  2883. if (is_http(t)) {
  2884. load_remote(t, push);
  2885. return;
  2886. }
  2887. if (t[0] == '/') {
  2888. make_abs_local(t, target, sizeof(target));
  2889. load_page(target, 1, push);
  2890. return;
  2891. }
  2892. if (!looks_like_host(t)) {
  2893. struct sys_stat st;
  2894. if (u_stat(t, &st) == 0 && !st.is_dir) {
  2895. make_abs_local(t, target, sizeof(target));
  2896. load_page(target, 1, push);
  2897. return;
  2898. }
  2899. }
  2900. {
  2901. char with_scheme[BR_URL_MAX];
  2902. unsigned int n = 0;
  2903. s_append(with_scheme, &n, "http://", sizeof(with_scheme));
  2904. s_append(with_scheme, &n, t, sizeof(with_scheme));
  2905. load_remote(with_scheme, push);
  2906. }
  2907. }
  2908. /* ---- layout ---- */
  2909. static struct ufont_face *style_face(const struct br_style *st)
  2910. {
  2911. if (st && st->font_face && st->font_face <= BR_MAX_FACES)
  2912. return font_faces[st->font_face - 1];
  2913. return 0;
  2914. }
  2915. static struct ufont_face *run_face(const struct lay_run *r)
  2916. {
  2917. if (r->font_face && r->font_face <= BR_MAX_FACES)
  2918. return font_faces[r->font_face - 1];
  2919. return 0;
  2920. }
  2921. static unsigned int face_text_width(struct ngui *gui,
  2922. const struct br_style *st,
  2923. const char *s, unsigned int n)
  2924. {
  2925. return ngui_text_width_face(gui, style_face(st), s, n, st->font_px,
  2926. st->bold, st->italic, st->mono);
  2927. }
  2928. static int face_line_height(struct ngui *gui, const struct br_style *st)
  2929. {
  2930. return ngui_text_line_height_face(gui, style_face(st), st->font_px,
  2931. st->mono);
  2932. }
  2933. static unsigned int run_text_width(struct ngui *gui,
  2934. const struct lay_run *r,
  2935. const char *s, unsigned int n)
  2936. {
  2937. return ngui_text_width_face(gui, run_face(r), s, n, r->font_px,
  2938. r->bold, r->italic, r->mono);
  2939. }
  2940. static int run_line_height(struct ngui *gui, const struct lay_run *r)
  2941. {
  2942. return ngui_text_line_height_face(gui, run_face(r), r->font_px,
  2943. r->mono);
  2944. }
  2945. static int run_ascent(struct ngui *gui, const struct lay_run *r)
  2946. {
  2947. return ngui_text_ascent_face(gui, run_face(r), r->font_px, r->mono);
  2948. }
  2949. static struct br_style base_style(void)
  2950. {
  2951. struct br_style s = { 0 };
  2952. s.color = ui.theme.text;
  2953. s.bg = ui.theme.bg;
  2954. s.has_bg = 0;
  2955. for (int i = 0; i < 4; i++) {
  2956. s.border_c[i] = 0;
  2957. s.border_w[i] = 0;
  2958. }
  2959. s.font_px = 16;
  2960. s.bold = 0;
  2961. s.italic = 0;
  2962. s.mono = 0;
  2963. s.underline = 0;
  2964. s.align = BR_ALIGN_LEFT;
  2965. s.display = BR_DISPLAY_BLOCK;
  2966. s.no_bullet = 0;
  2967. s.uppercase = 0;
  2968. s.width_kind = s.max_width_kind = s.min_width_kind = 0;
  2969. s.height_kind = s.min_height_kind = s.max_height_kind = 0;
  2970. s.width = s.max_width = s.min_width = 0;
  2971. s.height = s.min_height = s.max_height = 0;
  2972. s.box_sizing = 0;
  2973. s.ml_auto = s.mr_auto = 0;
  2974. s.line_h_pct = 0;
  2975. s.line_h_px = 0;
  2976. s.mt = s.mb = s.ml = s.mr = 0;
  2977. s.pt = s.pb = s.pl = s.pr = 0;
  2978. return s;
  2979. }
  2980. /* Resolve a margin/padding raw value. Percentages use the containing
  2981. * block width for all four sides, as CSS specifies. */
  2982. static int css_mp_px(const struct br_style *st, int v, int unit, int base)
  2983. {
  2984. if (unit == 1)
  2985. return base * v / 10000;
  2986. if (unit == 2)
  2987. return st->font_px * v / 100;
  2988. if (unit == 3)
  2989. return (int)br_root_font_px() * v / 100;
  2990. if (unit == 4)
  2991. return 0;
  2992. return v;
  2993. }
  2994. static int mp_px(const struct br_style *st, int side, int base)
  2995. {
  2996. int raw;
  2997. switch (side) {
  2998. case 0: raw = st->mt; break;
  2999. case 1: raw = st->mr; break;
  3000. case 2: raw = st->mb; break;
  3001. case 3: raw = st->ml; break;
  3002. case 4: raw = st->pt; break;
  3003. case 5: raw = st->pr; break;
  3004. case 6: raw = st->pb; break;
  3005. default: raw = st->pl; break;
  3006. }
  3007. if (st->mp_unit[side] == 5)
  3008. return base * raw / 100 + st->mp_calc_px[side];
  3009. return css_mp_px(st, raw, st->mp_unit[side], base);
  3010. }
  3011. static int style_width_px(const struct br_style *st, int base)
  3012. {
  3013. if (st->width_kind == 1)
  3014. return st->width;
  3015. if (st->width_kind == 2)
  3016. return base * (int)st->width / 100;
  3017. if (st->width_kind == 3)
  3018. return base * st->width_calc_pct / 100 + st->width_calc_px;
  3019. return 0;
  3020. }
  3021. static int style_height_px(const struct br_style *st, int base)
  3022. {
  3023. if (st->height_kind == 1)
  3024. return st->height;
  3025. if (st->height_kind == 2)
  3026. return base * (int)st->height / 100;
  3027. if (st->height_kind == 3)
  3028. return base * st->height_calc_pct / 100 + st->height_calc_px;
  3029. return 0;
  3030. }
  3031. static int css_size(unsigned char kind, int val, int base)
  3032. {
  3033. if (kind == 1)
  3034. return val;
  3035. if (kind == 2)
  3036. return base * val / 100;
  3037. return -1;
  3038. }
  3039. static void draw_border_box(struct ngui *ui, struct ngui_rect r,
  3040. const struct br_style *st)
  3041. {
  3042. int top = st->border_w[0], right = st->border_w[1];
  3043. int bottom = st->border_w[2], left = st->border_w[3];
  3044. if (r.w <= 0 || r.h <= 0)
  3045. return;
  3046. if (top >= r.h || bottom >= r.h || left >= r.w || right >= r.w) {
  3047. int i;
  3048. for (i = 0; i < 4; i++)
  3049. if (st->border_w[i]) {
  3050. ngui_fill_rect(ui, r, st->border_c[i]);
  3051. break;
  3052. }
  3053. return;
  3054. }
  3055. if (top)
  3056. ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y, r.w, top},
  3057. st->border_c[0]);
  3058. if (bottom)
  3059. ngui_fill_rect(ui, (struct ngui_rect){r.x,
  3060. r.y + r.h - bottom,
  3061. r.w, bottom},
  3062. st->border_c[2]);
  3063. if (left)
  3064. ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y + top, left,
  3065. r.h - top - bottom},
  3066. st->border_c[3]);
  3067. if (right)
  3068. ngui_fill_rect(ui, (struct ngui_rect){r.x + r.w - right,
  3069. r.y + top, right,
  3070. r.h - top - bottom},
  3071. st->border_c[1]);
  3072. }
  3073. static void transform_origin(const struct br_style *st, int w, int h,
  3074. int *ox, int *oy)
  3075. {
  3076. *ox = st->origin_x_kind ? w * st->origin_x / 100 : st->origin_x;
  3077. *oy = st->origin_y_kind ? h * st->origin_y / 100 : st->origin_y;
  3078. }
  3079. static unsigned short blend_rgb565(unsigned short d, unsigned short s,
  3080. unsigned int a)
  3081. {
  3082. unsigned int dr, dg, db, sr, sg, sb;
  3083. if (a >= 255)
  3084. return s;
  3085. dr = (d >> 11) & 31;
  3086. dg = (d >> 5) & 63;
  3087. db = d & 31;
  3088. sr = (s >> 11) & 31;
  3089. sg = (s >> 5) & 63;
  3090. sb = s & 31;
  3091. dr = (dr * (255 - a) + sr * a) / 255;
  3092. dg = (dg * (255 - a) + sg * a) / 255;
  3093. db = (db * (255 - a) + sb * a) / 255;
  3094. return (unsigned short)((dr << 11) | (dg << 5) | db);
  3095. }
  3096. /* Paint a solid box through scale()/rotate() around transform-origin. This
  3097. * transforms the element's own background box; text and descendants keep
  3098. * their layout (documented approximation). */
  3099. static void fill_transformed_rect(struct ngui *gui, struct ngui_rect box,
  3100. unsigned short color,
  3101. const struct br_style *st)
  3102. {
  3103. double sx = st->scale_x / 100.0;
  3104. double sy = st->scale_y / 100.0;
  3105. double th = st->rotate_x100 / 100.0 * 3.14159265358979 / 180.0;
  3106. double c = cos(th), sn = sin(th);
  3107. int ox, oy, minx, miny, maxx, maxy, x, y;
  3108. double ax, ay;
  3109. double px[4], py[4];
  3110. int cx1, cy1, cx2, cy2, i;
  3111. if (sx == 0)
  3112. sx = 0.01;
  3113. if (sy == 0)
  3114. sy = 0.01;
  3115. transform_origin(st, box.w, box.h, &ox, &oy);
  3116. ax = box.x + ox;
  3117. ay = box.y + oy;
  3118. px[0] = -ox; py[0] = -oy;
  3119. px[1] = box.w - ox; py[1] = -oy;
  3120. px[2] = box.w - ox; py[2] = box.h - oy;
  3121. px[3] = -ox; py[3] = box.h - oy;
  3122. minx = miny = 1000000;
  3123. maxx = maxy = -1000000;
  3124. for (i = 0; i < 4; i++) {
  3125. double rx = (px[i] * sx) * c - (py[i] * sy) * sn;
  3126. double ry = (px[i] * sx) * sn + (py[i] * sy) * c;
  3127. int dx = (int)(ax + rx), dy = (int)(ay + ry);
  3128. if (dx < minx) minx = dx;
  3129. if (dx > maxx) maxx = dx;
  3130. if (dy < miny) miny = dy;
  3131. if (dy > maxy) maxy = dy;
  3132. }
  3133. cx1 = page_rect.x > 0 ? page_rect.x : 0;
  3134. cy1 = page_rect.y > 0 ? page_rect.y : 0;
  3135. cx2 = page_rect.x + page_rect.w - 12;
  3136. cy2 = page_rect.y + page_rect.h;
  3137. if (cx2 > gui->w) cx2 = gui->w;
  3138. if (cy2 > gui->h) cy2 = gui->h;
  3139. if (minx < cx1) minx = cx1;
  3140. if (miny < cy1) miny = cy1;
  3141. if (maxx > cx2 - 1) maxx = cx2 - 1;
  3142. if (maxy > cy2 - 1) maxy = cy2 - 1;
  3143. for (y = miny; y <= maxy; y++) {
  3144. for (x = minx; x <= maxx; x++) {
  3145. double dx = x + 0.5 - ax, dy = y + 0.5 - ay;
  3146. double rx = dx * c + dy * sn;
  3147. double ry = -dx * sn + dy * c;
  3148. double bx = rx / sx + ox;
  3149. double by = ry / sy + oy;
  3150. if (bx >= 0 && bx < box.w && by >= 0 && by < box.h)
  3151. gui->px[y * gui->w + x] = color;
  3152. }
  3153. }
  3154. }
  3155. static void draw_image_transformed(struct ngui *gui,
  3156. const struct img_image *im,
  3157. int x, int y, int w, int h,
  3158. int sx100, int sy100, int rot100,
  3159. int origin_x, int origin_y,
  3160. int origin_x_kind, int origin_y_kind)
  3161. {
  3162. double sx = sx100 / 100.0, sy = sy100 / 100.0;
  3163. double th = rot100 / 100.0 * 3.14159265358979 / 180.0;
  3164. double c = cos(th), sn = sin(th);
  3165. int ox, oy, minx, miny, maxx, maxy, px, py, i;
  3166. double ax, ay;
  3167. double bx[4], by[4];
  3168. int cx1, cy1, cx2, cy2;
  3169. if (!im || !im->px || w <= 0 || h <= 0 || sx == 0 || sy == 0)
  3170. return;
  3171. ox = origin_x_kind ? w * origin_x / 100 : origin_x;
  3172. oy = origin_y_kind ? h * origin_y / 100 : origin_y;
  3173. ax = x + ox;
  3174. ay = y + oy;
  3175. bx[0] = -ox; by[0] = -oy;
  3176. bx[1] = w - ox; by[1] = -oy;
  3177. bx[2] = w - ox; by[2] = h - oy;
  3178. bx[3] = -ox; by[3] = h - oy;
  3179. minx = miny = 1000000;
  3180. maxx = maxy = -1000000;
  3181. for (i = 0; i < 4; i++) {
  3182. double rx = (bx[i] * sx) * c - (by[i] * sy) * sn;
  3183. double ry = (bx[i] * sx) * sn + (by[i] * sy) * c;
  3184. int dx = (int)(ax + rx), dy = (int)(ay + ry);
  3185. if (dx < minx) minx = dx;
  3186. if (dx > maxx) maxx = dx;
  3187. if (dy < miny) miny = dy;
  3188. if (dy > maxy) maxy = dy;
  3189. }
  3190. cx1 = page_rect.x > 0 ? page_rect.x : 0;
  3191. cy1 = page_rect.y > 0 ? page_rect.y : 0;
  3192. cx2 = page_rect.x + page_rect.w - 12;
  3193. cy2 = page_rect.y + page_rect.h;
  3194. if (cx2 > gui->w) cx2 = gui->w;
  3195. if (cy2 > gui->h) cy2 = gui->h;
  3196. if (minx < cx1) minx = cx1;
  3197. if (miny < cy1) miny = cy1;
  3198. if (maxx > cx2 - 1) maxx = cx2 - 1;
  3199. if (maxy > cy2 - 1) maxy = cy2 - 1;
  3200. for (py = miny; py <= maxy; py++) {
  3201. for (px = minx; px <= maxx; px++) {
  3202. double dx = px + 0.5 - ax, dy = py + 0.5 - ay;
  3203. double rx = dx * c + dy * sn;
  3204. double ry = -dx * sn + dy * c;
  3205. double fx = rx / sx + ox;
  3206. double fy = ry / sy + oy;
  3207. int ix, iy;
  3208. unsigned int si, a;
  3209. unsigned short src;
  3210. if (fx < 0 || fx >= w || fy < 0 || fy >= h)
  3211. continue;
  3212. ix = (int)(fx * (double)im->w / w);
  3213. iy = (int)(fy * (double)im->h / h);
  3214. if (ix < 0 || ix >= (int)im->w ||
  3215. iy < 0 || iy >= (int)im->h)
  3216. continue;
  3217. si = (unsigned int)iy * im->w + (unsigned int)ix;
  3218. src = im->px[si];
  3219. a = im->alpha ? im->alpha[si] : 255;
  3220. gui->px[py * gui->w + px] =
  3221. a >= 255 ? src :
  3222. blend_rgb565(gui->px[py * gui->w + px],
  3223. src, a);
  3224. }
  3225. }
  3226. }
  3227. static void draw_bg_image(struct lay *l, struct ngui_rect box,
  3228. unsigned int node, const struct br_style *st)
  3229. {
  3230. struct br_image *bi;
  3231. struct img_blit b;
  3232. int tw, th, x, y, rep_x, rep_y, posx, posy;
  3233. unsigned int scale = 65536;
  3234. int legacy, tiles = 0, rows = 0;
  3235. int cx1, cy1, cx2, cy2;
  3236. if (!st->bg_image || box.w <= 0 || box.h <= 0)
  3237. return;
  3238. if (doc.nodes[node].bg_img_index < 0 ||
  3239. doc.nodes[node].bg_img_index >= image_count)
  3240. return;
  3241. bi = &images[doc.nodes[node].bg_img_index];
  3242. if (bi->state != 1 || bi->img.w <= 0 || bi->img.h <= 0)
  3243. return;
  3244. legacy = st->bg_size_kind == 0 && st->bg_repeat == 0 &&
  3245. st->bg_pos_x_kind == 0 && st->bg_pos_x == 0 &&
  3246. st->bg_pos_y_kind == 0 && st->bg_pos_y == 0;
  3247. if (legacy) {
  3248. scale = ((unsigned int)box.w << 16) / bi->img.w;
  3249. tw = box.w;
  3250. th = (int)(((unsigned int)bi->img.h * scale) >> 16);
  3251. x = box.x;
  3252. y = box.y;
  3253. rep_x = rep_y = 0;
  3254. } else {
  3255. if (st->bg_size_kind == 3 || st->bg_size_kind == 4) {
  3256. unsigned int sx = ((unsigned int)box.w << 16) /
  3257. bi->img.w;
  3258. unsigned int sy = ((unsigned int)box.h << 16) /
  3259. bi->img.h;
  3260. scale = st->bg_size_kind == 3 ?
  3261. (sx > sy ? sx : sy) : (sx < sy ? sx : sy);
  3262. } else if (st->bg_size_kind == 1 ||
  3263. st->bg_size_kind == 2) {
  3264. int w = st->bg_size_w;
  3265. int h = st->bg_size_h;
  3266. if (st->bg_size_kind == 2) {
  3267. if (w)
  3268. w = box.w * w / 100;
  3269. if (h)
  3270. h = box.h * h / 100;
  3271. }
  3272. if (w)
  3273. scale = ((unsigned int)w << 16) / bi->img.w;
  3274. else if (h)
  3275. scale = ((unsigned int)h << 16) / bi->img.h;
  3276. }
  3277. tw = (int)(((unsigned int)bi->img.w * scale) >> 16);
  3278. th = (int)(((unsigned int)bi->img.h * scale) >> 16);
  3279. if (tw < 1)
  3280. tw = 1;
  3281. if (th < 1)
  3282. th = 1;
  3283. posx = st->bg_pos_x_kind == 1 ?
  3284. (box.w - tw) * st->bg_pos_x / 100 : st->bg_pos_x;
  3285. posy = st->bg_pos_y_kind == 1 ?
  3286. (box.h - th) * st->bg_pos_y / 100 : st->bg_pos_y;
  3287. x = box.x + posx;
  3288. y = box.y + posy;
  3289. rep_x = st->bg_repeat == 0 || st->bg_repeat == 2;
  3290. rep_y = st->bg_repeat == 0 || st->bg_repeat == 3;
  3291. if (rep_x) {
  3292. int m = (x - box.x) % tw;
  3293. if (m > 0)
  3294. m -= tw;
  3295. x = box.x + m;
  3296. }
  3297. if (rep_y) {
  3298. int m = (y - box.y) % th;
  3299. if (m > 0)
  3300. m -= th;
  3301. y = box.y + m;
  3302. }
  3303. }
  3304. cx1 = box.x > page_rect.x ? box.x : page_rect.x;
  3305. cy1 = box.y > page_rect.y ? box.y : page_rect.y;
  3306. cx2 = box.x + box.w;
  3307. cy2 = box.y + box.h;
  3308. if (cx2 > page_rect.x + page_rect.w - 12)
  3309. cx2 = page_rect.x + page_rect.w - 12;
  3310. if (cy2 > page_rect.y + page_rect.h)
  3311. cy2 = page_rect.y + page_rect.h;
  3312. if (cx1 >= cx2 || cy1 >= cy2)
  3313. return;
  3314. b.dst = l->ui->px;
  3315. b.dst_w = l->ui->w;
  3316. b.dst_h = l->ui->h;
  3317. b.clip_x = cx1;
  3318. b.clip_y = cy1;
  3319. b.clip_w = cx2 - cx1;
  3320. b.clip_h = cy2 - cy1;
  3321. b.scale = scale;
  3322. b.bg = ui.theme.bg;
  3323. b.bg2 = ui.theme.bg;
  3324. b.flags = IMG_BLIT_OVER_DST;
  3325. {
  3326. int row_x = x;
  3327. for (;;) {
  3328. for (;;) {
  3329. b.x = x;
  3330. b.y = y;
  3331. img_draw(&bi->img, &b);
  3332. if (!rep_x || ++tiles > 512)
  3333. break;
  3334. x += tw;
  3335. if (x >= box.x + box.w)
  3336. break;
  3337. }
  3338. if (!rep_y || ++rows > 64)
  3339. break;
  3340. y += th;
  3341. if (y >= box.y + box.h)
  3342. break;
  3343. x = row_x;
  3344. }
  3345. }
  3346. }
  3347. static int run_width(const struct lay_run *r)
  3348. {
  3349. if (r->img >= 0)
  3350. return r->iw;
  3351. if (r->img == -2)
  3352. return r->iw;
  3353. return (int)r->w;
  3354. }
  3355. static void start_line(struct lay *l, const struct br_style *st)
  3356. {
  3357. (void)st;
  3358. if (l->nruns == 0) {
  3359. l->line_y = l->y;
  3360. l->line_h = 0;
  3361. }
  3362. l->line_left = l->left;
  3363. l->line_right = l->right;
  3364. if (l->float_active && l->line_y < l->float_bottom) {
  3365. if (l->float_left > l->line_left)
  3366. l->line_left = l->float_left;
  3367. if (l->float_right < l->line_right)
  3368. l->line_right = l->float_right;
  3369. }
  3370. if (l->line_right < l->line_left + 8)
  3371. l->line_right = l->line_left + 8;
  3372. l->line_x = l->line_left;
  3373. l->run_x = l->line_left;
  3374. }
  3375. static void flush_line(struct lay *l)
  3376. {
  3377. int total, x, i, vis;
  3378. if (!l->nruns && l->line_x == l->line_left) {
  3379. l->line_h = 0;
  3380. return;
  3381. }
  3382. total = l->line_x - l->line_left;
  3383. x = l->line_left;
  3384. vis = 1;
  3385. if (l->draw) {
  3386. int lh = l->line_h ? l->line_h : 16;
  3387. vis = l->line_y + lh >= page_rect.y &&
  3388. l->line_y <= page_rect.y + page_rect.h;
  3389. }
  3390. if (l->align == BR_ALIGN_CENTER)
  3391. x += (l->line_right - l->line_left - total) / 2;
  3392. else if (l->align == BR_ALIGN_RIGHT)
  3393. x = l->line_right - total;
  3394. if (x < l->line_left)
  3395. x = l->line_left;
  3396. for (i = 0; i < l->nruns; i++) {
  3397. struct lay_run *r = &l->runs[i];
  3398. int w = run_width(r);
  3399. int ry = l->line_y + r->dy;
  3400. x += r->gap;
  3401. if (l->draw && vis) {
  3402. if (r->form) {
  3403. unsigned int node = r->form - 1;
  3404. struct form_state *fs = form_get(node, 1);
  3405. int hh = r->ih ? r->ih : 20;
  3406. int focused = form_focus == node;
  3407. int disabled = r->form_disabled;
  3408. unsigned short bg = l->ui->theme.panel_alt;
  3409. unsigned short fg = disabled ? 0x8410 : r->color;
  3410. struct ngui_rect box = {x, ry, w, hh};
  3411. if (r->form_kind == FORM_CHECKBOX ||
  3412. r->form_kind == FORM_RADIO) {
  3413. ngui_fill_rect(l->ui, box, bg);
  3414. ngui_frame(l->ui, box,
  3415. focused ? l->ui->theme.accent :
  3416. l->ui->theme.border);
  3417. if (fs && fs->checked) {
  3418. struct ngui_rect dot = {
  3419. x + 4, ry + 4,
  3420. w > 8 ? w - 8 : 1,
  3421. hh > 8 ? hh - 8 : 1
  3422. };
  3423. if (r->form_kind == FORM_RADIO)
  3424. ngui_fill_round_rect(
  3425. l->ui, dot, 4,
  3426. 4, 4, 4,
  3427. l->ui->theme.accent);
  3428. else
  3429. ngui_fill_rect(l->ui,
  3430. dot,
  3431. l->ui->theme.accent);
  3432. }
  3433. } else {
  3434. ngui_fill_rect(l->ui, box, bg);
  3435. ngui_frame(l->ui, box,
  3436. focused ? l->ui->theme.accent :
  3437. l->ui->theme.border);
  3438. ngui_text_px_face(l->ui, run_face(r),
  3439. x + 6, ry + 2,
  3440. r->lit, s_len(r->lit),
  3441. fg, r->font_px, r->bold,
  3442. r->italic, r->mono);
  3443. if (focused && fs &&
  3444. (r->form_kind == FORM_TEXT ||
  3445. r->form_kind == FORM_PASSWORD ||
  3446. r->form_kind == FORM_TEXTAREA)) {
  3447. int cw = 0;
  3448. if (fs->len && fs->caret)
  3449. cw = (int)run_text_width(
  3450. l->ui, r, r->lit,
  3451. fs->caret < fs->len ?
  3452. fs->caret : fs->len);
  3453. ngui_fill_rect(l->ui,
  3454. (struct ngui_rect){
  3455. x + 6 + cw,
  3456. ry + 4, 1,
  3457. hh > 8 ? hh - 8 : 1
  3458. },
  3459. l->ui->theme.accent);
  3460. }
  3461. if (r->form_kind == FORM_SELECT) {
  3462. ngui_text_px(l->ui, x + w - 12,
  3463. ry + 2, "v", 1, fg,
  3464. r->font_px, 0, 0, 0);
  3465. }
  3466. }
  3467. } else if (r->img == -2) {
  3468. int hh = r->ih ? r->ih : 20;
  3469. ngui_fill_rect(l->ui,
  3470. (struct ngui_rect){x, ry, w, hh},
  3471. l->ui->theme.panel_alt);
  3472. ngui_frame(l->ui,
  3473. (struct ngui_rect){x, ry, w, hh},
  3474. l->ui->theme.border);
  3475. ngui_text_px_face(l->ui, run_face(r),
  3476. x + 6, ry + 2, r->lit,
  3477. s_len(r->lit), r->color,
  3478. r->font_px, r->bold, r->italic,
  3479. r->mono);
  3480. } else if (r->img >= 0) {
  3481. struct br_image *bi = &images[r->img];
  3482. if (bi->state == 1 && r->transform_scale) {
  3483. draw_image_transformed(l->ui, &bi->img,
  3484. x, ry, r->iw, r->ih,
  3485. r->scale_x, r->scale_y,
  3486. r->rotate_x100, r->origin_x,
  3487. r->origin_y, r->origin_x_kind,
  3488. r->origin_y_kind);
  3489. } else if (bi->state == 1) {
  3490. struct img_blit b;
  3491. b.dst = l->ui->px;
  3492. b.dst_w = l->ui->w;
  3493. b.dst_h = l->ui->h;
  3494. b.clip_x = page_rect.x;
  3495. b.clip_y = page_rect.y;
  3496. b.clip_w = page_rect.w - 12;
  3497. b.clip_h = page_rect.h;
  3498. b.x = x;
  3499. b.y = ry;
  3500. b.scale = ((unsigned int)r->iw << 16) /
  3501. bi->img.w;
  3502. b.bg = ui.theme.bg;
  3503. b.bg2 = ui.theme.bg;
  3504. b.flags = IMG_BLIT_OVER_DST;
  3505. img_draw(&bi->img, &b);
  3506. }
  3507. } else {
  3508. const char *s = r->has_lit ? r->lit :
  3509. doc.text + r->off;
  3510. unsigned int n = r->has_lit ? s_len(r->lit) :
  3511. r->len;
  3512. ngui_text_px_face(l->ui, run_face(r), x, ry,
  3513. s, n, r->color, r->font_px,
  3514. r->bold, r->italic, r->mono);
  3515. if (r->underline)
  3516. ngui_fill_rect(l->ui,
  3517. (struct ngui_rect){x,
  3518. ry + run_ascent(l->ui, r) + 2,
  3519. w, 1}, r->color);
  3520. }
  3521. if (r->form || r->href) {
  3522. if (nhits < HIT_MAX) {
  3523. struct hit *h = &hits[nhits];
  3524. unsigned int target = r->form ?
  3525. r->form - 1 : r->href;
  3526. int hh = (r->form || r->img >= 0) ?
  3527. r->ih :
  3528. run_line_height(l->ui, r);
  3529. int merge = nhits > 0 &&
  3530. hits[nhits - 1].node == target &&
  3531. hits[nhits - 1].y == ry &&
  3532. hits[nhits - 1].h == hh &&
  3533. x <= hits[nhits - 1].x +
  3534. hits[nhits - 1].w + 2;
  3535. if (merge) {
  3536. struct hit *p = &hits[nhits - 1];
  3537. int end = x + w;
  3538. if (end > p->x + p->w)
  3539. p->w = end - p->x;
  3540. } else {
  3541. h->x = x;
  3542. h->y = ry;
  3543. h->w = w;
  3544. h->h = hh;
  3545. h->node = (unsigned short)target;
  3546. nhits++;
  3547. }
  3548. }
  3549. }
  3550. }
  3551. x += w;
  3552. }
  3553. l->y += l->line_h + 2;
  3554. if (l->float_active && l->y >= l->float_bottom)
  3555. l->float_active = 0;
  3556. l->nruns = 0;
  3557. l->line_x = l->left;
  3558. l->run_x = l->left;
  3559. l->line_left = l->left;
  3560. l->line_right = l->right;
  3561. l->line_h = 0;
  3562. l->space_pending = 0;
  3563. }
  3564. static void add_run(struct lay *l, unsigned short off, unsigned short len,
  3565. const struct br_style *st, unsigned short href,
  3566. const char *lit)
  3567. {
  3568. unsigned int n = lit ? s_len(lit) : len;
  3569. const char *tp = lit ? lit : doc.text + off;
  3570. int w = (int)face_text_width(l->ui, st, tp, n);
  3571. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  3572. flush_line(l);
  3573. if (l->nruns == 0) {
  3574. l->line_y = l->y;
  3575. l->line_h = 0;
  3576. start_line(l, st);
  3577. }
  3578. {
  3579. struct lay_run *r = &l->runs[l->nruns++];
  3580. int gap = l->line_x - l->run_x;
  3581. if (gap < 0)
  3582. gap = 0;
  3583. r->gap = (unsigned short)gap;
  3584. r->off = off;
  3585. r->len = len;
  3586. r->color = st->color;
  3587. r->href = href;
  3588. r->dy = 0;
  3589. r->has_lit = 0;
  3590. r->form = 0;
  3591. r->form_kind = 0;
  3592. r->form_disabled = 0;
  3593. r->lit[0] = '\0';
  3594. if (lit && lit[0]) {
  3595. unsigned int i;
  3596. for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++)
  3597. r->lit[i] = lit[i];
  3598. r->lit[i] = '\0';
  3599. r->has_lit = 1;
  3600. }
  3601. r->img = -1;
  3602. r->iw = r->ih = 0;
  3603. r->font_px = st->font_px;
  3604. r->bold = st->bold;
  3605. r->italic = st->italic;
  3606. r->mono = st->mono;
  3607. r->font_face = st->font_face;
  3608. /* A control with its own background is a button, not body copy;
  3609. * an inherited link underline would strike through the fill. */
  3610. r->underline = st->underline && !st->has_bg;
  3611. r->w = (unsigned short)w;
  3612. if (r->has_lit)
  3613. r->w = (unsigned short)run_text_width(
  3614. l->ui, r, r->lit, s_len(r->lit));
  3615. w = r->w;
  3616. }
  3617. l->line_x += w;
  3618. l->run_x = l->line_x;
  3619. {
  3620. int lh = face_line_height(l->ui, st);
  3621. if (st->line_h_px)
  3622. lh = st->line_h_px;
  3623. else if (st->line_h_pct)
  3624. lh = st->font_px * st->line_h_pct / 100;
  3625. if (lh < face_line_height(l->ui, st))
  3626. lh = face_line_height(l->ui, st);
  3627. if (l->line_h < lh)
  3628. l->line_h = lh;
  3629. }
  3630. }
  3631. static void add_image_run(struct lay *l, unsigned int node,
  3632. const struct br_style *st, unsigned short href)
  3633. {
  3634. struct br_image *bi = 0;
  3635. int idx = doc.nodes[node].img_index;
  3636. int dw, dh, maxw;
  3637. if (idx >= 0 && idx < image_count && images[idx].state == 1)
  3638. bi = &images[idx];
  3639. if (l->nruns == 0)
  3640. start_line(l, st);
  3641. if (l->space_pending &&
  3642. (l->nruns > 0 || l->line_x > l->line_left)) {
  3643. int sp = (int)face_text_width(l->ui, st, " ", 1);
  3644. if (l->line_x + sp <= l->line_right)
  3645. l->line_x += sp;
  3646. }
  3647. l->space_pending = 0;
  3648. if (!bi) {
  3649. const char *alt = br_alt(&doc, node);
  3650. struct br_style gs = *st;
  3651. char tmp[96];
  3652. unsigned int n = 0;
  3653. int w;
  3654. gs.color = BR_GRAY;
  3655. gs.underline = 0;
  3656. s_append(tmp, &n, "[", sizeof(tmp));
  3657. s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp));
  3658. s_append(tmp, &n, "]", sizeof(tmp));
  3659. w = (int)face_text_width(l->ui, &gs, tmp, n);
  3660. if (l->line_x + w > l->line_right &&
  3661. (l->nruns > 0 || l->line_x > l->line_left))
  3662. flush_line(l);
  3663. add_run(l, 0, 0, &gs, href, tmp);
  3664. if (!l->draw) {
  3665. int t = l->line_y < 0 ? 0 : l->line_y;
  3666. int hh = face_line_height(l->ui, &gs);
  3667. if (t > 65535)
  3668. t = 65535;
  3669. if (hh > 65535)
  3670. hh = 65535;
  3671. img_top[node] = (unsigned short)t;
  3672. img_h[node] = (unsigned short)hh;
  3673. }
  3674. return;
  3675. }
  3676. maxw = l->line_right - l->line_left;
  3677. if (maxw < 8)
  3678. maxw = 8;
  3679. {
  3680. int iw = bi->native_w, ih = bi->native_h;
  3681. int have_w = 0, have_h = 0, css_w = 0, css_h = 0;
  3682. if (iw <= 0 || ih <= 0) {
  3683. iw = (int)bi->img.w;
  3684. ih = (int)bi->img.h;
  3685. }
  3686. dw = iw;
  3687. dh = ih;
  3688. if (doc.nodes[node].width_attr) {
  3689. int want = doc.nodes[node].width_attr;
  3690. dw = doc.nodes[node].width_pct ? maxw * want / 100 : want;
  3691. have_w = 1;
  3692. }
  3693. if (doc.nodes[node].height_attr) {
  3694. int want = doc.nodes[node].height_attr;
  3695. dh = doc.nodes[node].height_pct ? maxw * want / 100 : want;
  3696. have_h = 1;
  3697. }
  3698. if (have_w && !have_h)
  3699. dh = ih ? ih * dw / iw : 0;
  3700. if (have_h && !have_w)
  3701. dw = iw ? iw * dh / ih : 0;
  3702. if (st->width_kind) {
  3703. css_w = 1;
  3704. if (st->width_kind == 3)
  3705. dw = maxw * st->width_calc_pct / 100 +
  3706. st->width_calc_px;
  3707. else
  3708. dw = st->width_kind == 2 ?
  3709. maxw * (int)st->width / 100 :
  3710. (int)st->width;
  3711. }
  3712. if (st->height_kind) {
  3713. css_h = 1;
  3714. if (st->height_kind == 3)
  3715. dh = maxw * st->height_calc_pct / 100 +
  3716. st->height_calc_px;
  3717. else
  3718. dh = st->height_kind == 2 ?
  3719. maxw * (int)st->height / 100 :
  3720. (int)st->height;
  3721. }
  3722. if (css_w && !css_h)
  3723. dh = ih ? ih * dw / iw : 0;
  3724. if (css_h && !css_w)
  3725. dw = iw ? iw * dh / ih : 0;
  3726. if (st->max_width_kind) {
  3727. int mw = st->max_width_kind == 3 ?
  3728. maxw * st->max_width_calc_pct / 100 +
  3729. st->max_width_calc_px :
  3730. st->max_width_kind == 2 ?
  3731. maxw * (int)st->max_width / 100 :
  3732. (int)st->max_width;
  3733. if (mw > 0 && dw > mw) {
  3734. dh = iw ? ih * mw / iw : 0;
  3735. dw = mw;
  3736. }
  3737. }
  3738. }
  3739. if (dw < 8)
  3740. dw = 8;
  3741. if (dh < 8)
  3742. dh = 8;
  3743. if (dw > maxw) {
  3744. dh = dw ? dh * maxw / dw : dh;
  3745. dw = maxw;
  3746. }
  3747. if (!l->draw) {
  3748. int t = l->line_y < 0 ? 0 : l->line_y;
  3749. if (t > 65535)
  3750. t = 65535;
  3751. if (dh > 65535)
  3752. dh = 65535;
  3753. img_top[node] = (unsigned short)t;
  3754. img_h[node] = (unsigned short)dh;
  3755. }
  3756. if (l->line_x + dw > l->line_right && (l->nruns > 0 ||
  3757. l->line_x > l->line_left))
  3758. flush_line(l);
  3759. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  3760. flush_line(l);
  3761. if (l->nruns == 0) {
  3762. l->line_y = l->y;
  3763. l->line_h = 0;
  3764. start_line(l, st);
  3765. }
  3766. {
  3767. struct lay_run *r = &l->runs[l->nruns++];
  3768. int gap = l->line_x - l->run_x;
  3769. if (gap < 0)
  3770. gap = 0;
  3771. r->gap = (unsigned short)gap;
  3772. r->off = 0;
  3773. r->len = 0;
  3774. r->color = 0;
  3775. r->href = href;
  3776. r->form = 0;
  3777. r->form_kind = 0;
  3778. r->form_disabled = 0;
  3779. /* Inline replaced elements honour their vertical margins; this
  3780. * is how Bootstrap's negative logo margins centre the wordmark. */
  3781. {
  3782. int idy = mp_px(st, 0, l->right - l->left);
  3783. if (idy < -2048)
  3784. idy = -2048;
  3785. if (idy > 2048)
  3786. idy = 2048;
  3787. r->dy = (signed short)idy;
  3788. }
  3789. r->has_lit = 0;
  3790. r->lit[0] = '\0';
  3791. r->img = (short)idx;
  3792. r->iw = (unsigned short)dw;
  3793. r->ih = (unsigned short)dh;
  3794. r->w = (unsigned short)dw;
  3795. r->font_px = st->font_px;
  3796. r->bold = st->bold;
  3797. r->italic = st->italic;
  3798. r->mono = st->mono;
  3799. r->font_face = st->font_face;
  3800. r->transform_scale = st->transform_scale;
  3801. r->scale_x = st->scale_x;
  3802. r->scale_y = st->scale_y;
  3803. r->rotate_x100 = st->rotate_x100;
  3804. r->origin_x = st->origin_x;
  3805. r->origin_y = st->origin_y;
  3806. r->origin_x_kind = st->origin_x_kind;
  3807. r->origin_y_kind = st->origin_y_kind;
  3808. r->underline = 0;
  3809. }
  3810. if (node < BR_MAX_NODES) {
  3811. layout_boxes[node].x = l->line_x;
  3812. layout_boxes[node].y = l->line_y +
  3813. mp_px(st, 0, l->right - l->left);
  3814. layout_boxes[node].w = dw;
  3815. layout_boxes[node].h = dh;
  3816. layout_boxes[node].valid = 1;
  3817. }
  3818. l->line_x += dw;
  3819. l->run_x = l->line_x;
  3820. if (l->line_h < dh)
  3821. l->line_h = dh;
  3822. }
  3823. static int text_width(struct lay *l, const char *s, unsigned int n,
  3824. const struct br_style *st)
  3825. {
  3826. return (int)face_text_width(l->ui, st, s, n);
  3827. }
  3828. /* Longest prefix that fits in maxw, at least one byte. */
  3829. static unsigned int fit_prefix(struct lay *l, const char *s, unsigned int n,
  3830. const struct br_style *st, int maxw)
  3831. {
  3832. unsigned int take = 0;
  3833. while (take < n) {
  3834. if (text_width(l, s, take + 1, st) > maxw)
  3835. break;
  3836. take++;
  3837. }
  3838. return take ? take : 1;
  3839. }
  3840. static void emit_word(struct lay *l, const char *s, unsigned int start,
  3841. unsigned int n, unsigned int base,
  3842. const struct br_style *st, unsigned short href)
  3843. {
  3844. int avail;
  3845. int w = text_width(l, s + start, n, st);
  3846. unsigned int off = start;
  3847. if (l->nruns == 0)
  3848. start_line(l, st);
  3849. avail = l->line_right - l->line_left;
  3850. if (st->nowrap) {
  3851. if (n)
  3852. add_run(l, (unsigned short)(base + start),
  3853. (unsigned short)n, st, href, 0);
  3854. return;
  3855. }
  3856. if (l->line_x + w > l->line_right &&
  3857. (l->nruns > 0 || l->line_x > l->line_left)) {
  3858. flush_line(l);
  3859. start_line(l, st);
  3860. avail = l->line_right - l->line_left;
  3861. }
  3862. if (w <= avail) {
  3863. if (n)
  3864. add_run(l, (unsigned short)(base + start),
  3865. (unsigned short)n, st, href, 0);
  3866. return;
  3867. }
  3868. while (n > 0) {
  3869. unsigned int take = fit_prefix(l, s + off, n, st, avail);
  3870. add_run(l, (unsigned short)(base + off),
  3871. (unsigned short)take, st, href, 0);
  3872. off += take;
  3873. n -= take;
  3874. if (n) {
  3875. flush_line(l);
  3876. start_line(l, st);
  3877. avail = l->line_right - l->line_left;
  3878. }
  3879. }
  3880. }
  3881. static void emit_text(struct lay *l, const char *s, unsigned int len,
  3882. const struct br_style *st, unsigned short href, int pre)
  3883. {
  3884. unsigned int i = 0;
  3885. unsigned int base;
  3886. int space_w = (int)face_text_width(l->ui, st, " ", 1);
  3887. if (!s || !len)
  3888. return;
  3889. if (space_w <= 0)
  3890. space_w = 8;
  3891. if (l->nruns == 0)
  3892. start_line(l, st);
  3893. base = (unsigned int)(s - doc.text);
  3894. if (st->uppercase) {
  3895. char *p = (char *)s;
  3896. unsigned int j;
  3897. for (j = 0; j < len; j++)
  3898. if (p[j] >= 'a' && p[j] <= 'z')
  3899. p[j] = (char)(p[j] - 'a' + 'A');
  3900. }
  3901. if (pre) {
  3902. while (i < len) {
  3903. unsigned int start;
  3904. if (s[i] == '\n') {
  3905. flush_line(l);
  3906. start_line(l, st);
  3907. i++;
  3908. continue;
  3909. }
  3910. if (s[i] == ' ') {
  3911. if (l->line_x + space_w > l->line_right &&
  3912. (l->nruns > 0 || l->line_x > l->line_left)) {
  3913. flush_line(l);
  3914. start_line(l, st);
  3915. } else {
  3916. l->line_x += space_w;
  3917. }
  3918. i++;
  3919. continue;
  3920. }
  3921. start = i;
  3922. while (i < len && s[i] != ' ' && s[i] != '\n')
  3923. i++;
  3924. emit_word(l, s, start, i - start, base, st, href);
  3925. }
  3926. return;
  3927. }
  3928. while (i < len) {
  3929. unsigned int start, n;
  3930. if (s[i] == ' ') {
  3931. l->space_pending = 1;
  3932. i++;
  3933. continue;
  3934. }
  3935. start = i;
  3936. while (i < len && s[i] != ' ')
  3937. i++;
  3938. n = i - start;
  3939. if (l->space_pending &&
  3940. (l->nruns > 0 || l->line_x > l->line_left)) {
  3941. if (st->nowrap ||
  3942. l->line_x + space_w <= l->line_right)
  3943. l->line_x += space_w;
  3944. }
  3945. l->space_pending = 0;
  3946. emit_word(l, s, start, n, base, st, href);
  3947. }
  3948. }
  3949. static int list_index(unsigned int parent, unsigned int node)
  3950. {
  3951. unsigned int n = 0, i;
  3952. for (i = doc.nodes[parent].first; i && i != node;
  3953. i = doc.nodes[i].next)
  3954. if (doc.nodes[i].tag == BR_TAG_LI)
  3955. n++;
  3956. return (int)n + 1;
  3957. }
  3958. static void add_form_run(struct lay *l, unsigned int node,
  3959. const struct br_style *st, unsigned short href)
  3960. {
  3961. struct form_state *fs = form_get(node, 1);
  3962. char shown[FORM_VALUE_MAX + 1];
  3963. char opt[64];
  3964. const char *text = "";
  3965. struct lay_run *r;
  3966. int kind = form_kind_of(node);
  3967. int disabled = form_disabled(node);
  3968. int tw, w = 120, h = 20, i;
  3969. if (!fs)
  3970. return;
  3971. if (l->nruns == 0)
  3972. start_line(l, st);
  3973. if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
  3974. text = "";
  3975. w = 16;
  3976. h = 16;
  3977. } else if (kind == FORM_SELECT) {
  3978. if (!form_option_text(node, fs->selected, opt, sizeof(opt)))
  3979. s_copy(opt, "Select", sizeof(opt));
  3980. text = opt;
  3981. w = 120;
  3982. } else if (kind == FORM_BUTTON || kind == FORM_SUBMIT ||
  3983. kind == FORM_RESET) {
  3984. if (fs->len)
  3985. text = fs->value;
  3986. else
  3987. text = kind == FORM_SUBMIT ? "Submit" :
  3988. kind == FORM_RESET ? "Reset" : "Button";
  3989. } else if (kind == FORM_PASSWORD) {
  3990. for (i = 0; i < fs->len; i++)
  3991. shown[i] = '*';
  3992. shown[fs->len] = '\0';
  3993. text = fs->len ? shown : br_placeholder(&doc, node);
  3994. } else {
  3995. if (fs->len)
  3996. text = fs->value;
  3997. else
  3998. text = br_placeholder(&doc, node);
  3999. if (kind == FORM_TEXTAREA) {
  4000. w = 180;
  4001. h = 40;
  4002. }
  4003. }
  4004. tw = (int)face_text_width(l->ui, st, text, s_len(text));
  4005. if (kind == FORM_BUTTON || kind == FORM_SUBMIT || kind == FORM_RESET) {
  4006. w = tw + 18;
  4007. if (w < 64)
  4008. w = 64;
  4009. h = 22;
  4010. } else if (kind == FORM_TEXT || kind == FORM_PASSWORD) {
  4011. w = st->width_kind == 1 ? st->width : 120;
  4012. } else if (kind == FORM_SELECT) {
  4013. w = st->width_kind == 1 ? st->width : 120;
  4014. }
  4015. if (w > l->line_right - l->line_left)
  4016. w = l->line_right - l->line_left;
  4017. if (w < 8)
  4018. w = 8;
  4019. if (l->line_x + w > l->line_right &&
  4020. (l->nruns > 0 || l->line_x > l->line_left))
  4021. flush_line(l);
  4022. if (l->nruns >= 96)
  4023. return;
  4024. if (l->nruns == 0) {
  4025. l->line_y = l->y;
  4026. l->line_h = 0;
  4027. start_line(l, st);
  4028. }
  4029. r = &l->runs[l->nruns++];
  4030. r->gap = (unsigned short)(l->line_x - l->run_x);
  4031. r->off = r->len = 0;
  4032. r->color = st->color;
  4033. r->href = href;
  4034. r->dy = 0;
  4035. r->has_lit = 1;
  4036. for (i = 0; i + 1 < (int)sizeof(r->lit) && text[i]; i++)
  4037. r->lit[i] = text[i];
  4038. r->lit[i] = '\0';
  4039. r->img = -2;
  4040. r->iw = (unsigned short)w;
  4041. r->ih = (unsigned short)h;
  4042. r->w = (unsigned short)w;
  4043. r->font_px = st->font_px;
  4044. r->bold = st->bold;
  4045. r->italic = st->italic;
  4046. r->mono = st->mono;
  4047. r->font_face = st->font_face;
  4048. r->underline = 0;
  4049. r->form = node + 1;
  4050. r->form_kind = (unsigned char)kind;
  4051. r->form_disabled = (unsigned char)disabled;
  4052. if (node < BR_MAX_NODES) {
  4053. layout_boxes[node].x = l->line_x;
  4054. layout_boxes[node].y = l->line_y;
  4055. layout_boxes[node].w = w;
  4056. layout_boxes[node].h = h;
  4057. layout_boxes[node].valid = 1;
  4058. }
  4059. l->line_x += w;
  4060. l->run_x = l->line_x;
  4061. if (l->line_h < h)
  4062. l->line_h = h;
  4063. }
  4064. /* A poster wrapper is absolutely positioned over the media slot; the
  4065. * measured height of its picture is normally added back to the flow. When a
  4066. * loading-skeleton sibling already reserves that slot, adding both doubles
  4067. * the media height and pushes the article text a full screen down. */
  4068. static int has_skeleton_sibling(unsigned int node)
  4069. {
  4070. unsigned int p = doc.nodes[node].parent, c;
  4071. for (c = doc.nodes[p].first; c; c = doc.nodes[c].next) {
  4072. if (c == node || doc.nodes[c].type != BR_NODE_ELEM)
  4073. continue;
  4074. if (doc.nodes[c].class_off &&
  4075. contains_ci(doc.attrs + doc.nodes[c].class_off,
  4076. "skeleton"))
  4077. return 1;
  4078. }
  4079. return 0;
  4080. }
  4081. static void layout_children(struct lay *l, unsigned int parent,
  4082. const struct br_style *pst, int depth,
  4083. unsigned short link);
  4084. static void layout_flex_children(struct lay *l, unsigned int parent,
  4085. const struct br_style *pst, int depth,
  4086. unsigned short link);
  4087. static void layout_grid_children(struct lay *l, unsigned int parent,
  4088. const struct br_style *pst, int depth,
  4089. unsigned short link);
  4090. static void layout_grid_auto(struct lay *l, unsigned int parent,
  4091. const struct br_style *pst, int depth,
  4092. unsigned short link);
  4093. static int flex_item_width(struct lay *l, unsigned int n,
  4094. const struct br_style *cs, int avail);
  4095. static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
  4096. const struct br_style *pst, int depth,
  4097. unsigned short link);
  4098. static void layout_table_row(struct lay *l, unsigned int row,
  4099. const struct br_style *pst, int depth,
  4100. unsigned short link);
  4101. static int table_has_rowspan(unsigned int table);
  4102. static void layout_table(struct lay *l, unsigned int table,
  4103. const struct br_style *st, int depth,
  4104. unsigned short link);
  4105. /* A flex container whose items are only inline spans is usually just a
  4106. * heading/icon pair. Our flex row positions such items without emitting
  4107. * their inline text in some deep page contexts; normal inline flow renders
  4108. * the same content correctly. */
  4109. static int flex_wants_row(unsigned int parent, const struct br_style *pst)
  4110. {
  4111. unsigned int c;
  4112. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  4113. struct br_node *nd = &doc.nodes[c];
  4114. struct br_style cs;
  4115. if (nd->type == BR_NODE_TEXT)
  4116. continue;
  4117. br_style_for(&doc, c, pst, &cs);
  4118. if (cs.display == BR_DISPLAY_NONE)
  4119. continue;
  4120. if (cs.display != BR_DISPLAY_INLINE || cs.flex_grow ||
  4121. cs.width_kind || cs.has_bg ||
  4122. cs.border_w[0] || cs.border_w[1] ||
  4123. cs.border_w[2] || cs.border_w[3] ||
  4124. nd->tag == BR_TAG_IMG)
  4125. return 1;
  4126. }
  4127. return 0;
  4128. }
  4129. static int css_radius(const struct br_style *st, int corner,
  4130. int w, int h)
  4131. {
  4132. int r = st->radius[corner];
  4133. if (r <= 0)
  4134. return 0;
  4135. if (r > w / 2)
  4136. r = w / 2;
  4137. if (r > h / 2)
  4138. r = h / 2;
  4139. return r;
  4140. }
  4141. static void svg_viewbox(unsigned node, int *vw, int *vh)
  4142. {
  4143. struct br_node *nd = &doc.nodes[node];
  4144. if (nd->width_attr && nd->height_attr) {
  4145. *vw = nd->width_attr;
  4146. *vh = nd->height_attr;
  4147. } else {
  4148. *vw = 300;
  4149. *vh = 150;
  4150. }
  4151. if (*vw < 1)
  4152. *vw = 1;
  4153. if (*vh < 1)
  4154. *vh = 1;
  4155. }
  4156. static int svg_num(const char **pp)
  4157. {
  4158. const char *p = *pp;
  4159. int neg = 0, v = 0, frac = 0, scale = 1;
  4160. while (*p == ' ' || *p == ',' || *p == '\t' ||
  4161. *p == '\n' || *p == '\r')
  4162. p++;
  4163. if (*p == '-') {
  4164. neg = 1;
  4165. p++;
  4166. } else if (*p == '+') {
  4167. p++;
  4168. }
  4169. while (*p >= '0' && *p <= '9') {
  4170. v = v * 10 + (*p - '0');
  4171. p++;
  4172. }
  4173. if (*p == '.') {
  4174. p++;
  4175. while (*p >= '0' && *p <= '9' && scale < 1000) {
  4176. frac = frac * 10 + (*p - '0');
  4177. scale *= 10;
  4178. p++;
  4179. }
  4180. }
  4181. v += frac / scale;
  4182. *pp = p;
  4183. return neg ? -v : v;
  4184. }
  4185. static void svg_path(struct ngui *ui, const char *d,
  4186. int ox, int oy, int dw, int dh, int vw, int vh,
  4187. unsigned short color)
  4188. {
  4189. int cx = 0, cy = 0, sx = 0, sy = 0;
  4190. char cmd = 0;
  4191. const char *p = d;
  4192. while (p && *p) {
  4193. int rel, nx, ny;
  4194. char c;
  4195. while (*p == ' ' || *p == ',' || *p == '\t' ||
  4196. *p == '\n' || *p == '\r')
  4197. p++;
  4198. if (!*p)
  4199. break;
  4200. if ((*p >= 'A' && *p <= 'Z') ||
  4201. (*p >= 'a' && *p <= 'z'))
  4202. cmd = *p++;
  4203. else if (!cmd) {
  4204. p++;
  4205. continue;
  4206. }
  4207. rel = cmd >= 'a' && cmd <= 'z';
  4208. c = rel ? (char)(cmd - 'a' + 'A') : cmd;
  4209. if (c == 'Z') {
  4210. ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
  4211. ox + sx * dw / vw, oy + sy * dh / vh,
  4212. color);
  4213. cx = sx;
  4214. cy = sy;
  4215. cmd = 0;
  4216. continue;
  4217. }
  4218. if (c == 'H' || c == 'V') {
  4219. const char *save = p;
  4220. int a = svg_num(&p);
  4221. if (p == save) {
  4222. p++;
  4223. continue;
  4224. }
  4225. nx = cx;
  4226. ny = cy;
  4227. if (c == 'H')
  4228. nx = rel ? cx + a : a;
  4229. else
  4230. ny = rel ? cy + a : a;
  4231. } else {
  4232. const char *save = p;
  4233. int a = svg_num(&p);
  4234. int b = svg_num(&p);
  4235. if (p == save) {
  4236. p++;
  4237. continue;
  4238. }
  4239. if (c == 'M') {
  4240. nx = rel ? cx + a : a;
  4241. ny = rel ? cy + b : b;
  4242. cx = sx = nx;
  4243. cy = sy = ny;
  4244. cmd = rel ? 'l' : 'L';
  4245. continue;
  4246. }
  4247. if (c != 'L')
  4248. break;
  4249. nx = rel ? cx + a : a;
  4250. ny = rel ? cy + b : b;
  4251. }
  4252. ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
  4253. ox + nx * dw / vw, oy + ny * dh / vh, color);
  4254. cx = nx;
  4255. cy = ny;
  4256. }
  4257. }
  4258. static void draw_svg(struct ngui *ui, unsigned node,
  4259. int ox, int oy, int dw, int dh, int vw, int vh,
  4260. const struct br_style *pst)
  4261. {
  4262. unsigned int c;
  4263. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  4264. struct br_node *nd = &doc.nodes[c];
  4265. struct br_style st;
  4266. const char *nm;
  4267. if (nd->type != BR_NODE_ELEM || !nd->name_off)
  4268. continue;
  4269. nm = doc.attrs + nd->name_off;
  4270. br_style_for(&doc, c, pst, &st);
  4271. if (s_eq(nm, "rect")) {
  4272. struct ngui_rect r;
  4273. int rx = (int)nd->svg_rx * dw / vw;
  4274. r.x = ox + (int)nd->svg_x * dw / vw;
  4275. r.y = oy + (int)nd->svg_y * dh / vh;
  4276. r.w = (int)nd->width_attr * dw / vw;
  4277. r.h = (int)nd->height_attr * dh / vh;
  4278. if (r.w < 1)
  4279. r.w = 1;
  4280. if (r.h < 1)
  4281. r.h = 1;
  4282. if (rx > r.w / 2)
  4283. rx = r.w / 2;
  4284. if (rx > r.h / 2)
  4285. rx = r.h / 2;
  4286. if (st.has_bg) {
  4287. if (rx)
  4288. ngui_fill_round_rect(ui, r, rx, rx,
  4289. rx, rx, st.bg);
  4290. else
  4291. ngui_fill_rect(ui, r, st.bg);
  4292. }
  4293. if (st.border_w[0]) {
  4294. if (rx)
  4295. ngui_frame_round(ui, r, rx, rx, rx, rx,
  4296. st.border_w[0],
  4297. st.border_c[0]);
  4298. else
  4299. draw_border_box(ui, r, &st);
  4300. }
  4301. } else if (s_eq(nm, "line")) {
  4302. if (st.border_w[0])
  4303. ngui_line(ui,
  4304. ox + (int)nd->svg_x * dw / vw,
  4305. oy + (int)nd->svg_y * dh / vh,
  4306. ox + (int)nd->width_attr * dw / vw,
  4307. oy + (int)nd->height_attr * dh / vh,
  4308. st.border_c[0]);
  4309. } else if (s_eq(nm, "path")) {
  4310. if (st.border_w[0] && nd->src_off)
  4311. svg_path(ui, doc.attrs + nd->src_off,
  4312. ox, oy, dw, dh, vw, vh,
  4313. st.border_c[0]);
  4314. } else if (s_eq(nm, "text")) {
  4315. unsigned int t = nd->first;
  4316. const char *s;
  4317. unsigned int n, tw;
  4318. int px = st.font_px ? st.font_px : 16;
  4319. int tx, ty, anchor;
  4320. if (!t || doc.nodes[t].type != BR_NODE_TEXT)
  4321. continue;
  4322. s = doc.text + doc.nodes[t].text_off;
  4323. n = doc.nodes[t].text_len;
  4324. tw = face_text_width(ui, &st, s, n);
  4325. tx = ox + (int)nd->svg_x * dw / vw;
  4326. ty = oy + (int)nd->svg_y * dh / vh -
  4327. ngui_text_ascent_face(ui, style_face(&st), px,
  4328. st.mono);
  4329. anchor = st.align;
  4330. if (nd->attr_flags & BR_ATTR_ANCHOR_MIDDLE)
  4331. anchor = BR_ALIGN_CENTER;
  4332. else if (nd->attr_flags & BR_ATTR_ANCHOR_END)
  4333. anchor = BR_ALIGN_RIGHT;
  4334. if (anchor == BR_ALIGN_CENTER)
  4335. tx -= (int)tw / 2;
  4336. else if (anchor == BR_ALIGN_RIGHT)
  4337. tx -= (int)tw;
  4338. ngui_text_px_face(ui, style_face(&st), tx, ty, s, n,
  4339. st.color, px, st.bold, st.italic,
  4340. st.mono);
  4341. }
  4342. }
  4343. }
  4344. static void layout_block(struct lay *l, unsigned int node,
  4345. const struct br_style *st, int depth,
  4346. unsigned short link)
  4347. {
  4348. int old_left = l->left, old_right = l->right, old_align = l->align;
  4349. int saved_parent_top = l->parent_top;
  4350. int top, l0, r0;
  4351. int flow_y, flow_left, flow_right, positioned, relative, sticky, shifted;
  4352. int cbw, ml, mr, bl, br, pl, pr, bt, bb, pt, pb;
  4353. int deco_w, deco_h, content_w, content_h, box_h;
  4354. int definite_w = 0;
  4355. int normal_top = 0;
  4356. int rel_dx = 0, rel_dy = 0;
  4357. int clip_pushed = 0;
  4358. struct ngui_rect clip_rect;
  4359. if (depth > 24 || st->display == BR_DISPLAY_NONE)
  4360. return;
  4361. flow_y = l->y;
  4362. flow_left = l->left;
  4363. flow_right = l->right;
  4364. positioned = st->pos == 1 || st->pos == 2;
  4365. relative = st->pos == 3;
  4366. sticky = st->pos == 4;
  4367. shifted = relative || sticky || st->transform_set;
  4368. if (positioned) {
  4369. int def_w = st->width_kind == 1 ? st->width : -1;
  4370. if (st->pos == 2) {
  4371. int avail_w = page_rect.w - 12;
  4372. if (st->right_set && def_w >= 0)
  4373. l->left = page_rect.x + avail_w - st->right -
  4374. def_w;
  4375. else
  4376. l->left = page_rect.x +
  4377. (st->left_set ? st->left : 0);
  4378. l->right = def_w >= 0 ? l->left + def_w :
  4379. page_rect.x + avail_w;
  4380. if (st->bottom_set && st->height_kind)
  4381. l->y = page_rect.y + page_rect.h -
  4382. st->bottom -
  4383. style_height_px(st, page_rect.w);
  4384. else
  4385. l->y = page_rect.y +
  4386. (st->top_set ? st->top : 0);
  4387. } else {
  4388. int left = flow_left + (st->left_set ? st->left : 0);
  4389. unsigned int parent = doc.nodes[node].parent;
  4390. if (st->right_set && def_w >= 0)
  4391. left = flow_right - st->right - def_w;
  4392. l->left = left;
  4393. l->right = def_w >= 0 ? left + def_w :
  4394. flow_right +
  4395. (st->left_set ? st->left : 0);
  4396. l->y = flow_y + (st->top_set ? st->top : 0);
  4397. if (st->bottom_set && st->height_kind &&
  4398. parent < BR_MAX_NODES && block_h[parent] > 0)
  4399. l->y = flow_y + block_h[parent] - st->bottom -
  4400. style_height_px(st, old_right - old_left);
  4401. }
  4402. old_left = l->left;
  4403. old_right = l->right;
  4404. } else if (relative) {
  4405. rel_dx = st->left_set ? st->left :
  4406. (st->right_set ? -st->right : 0);
  4407. rel_dy = st->top_set ? st->top :
  4408. (st->bottom_set ? -st->bottom : 0);
  4409. }
  4410. if (st->transform_set) {
  4411. rel_dx += st->tx;
  4412. rel_dy += st->ty;
  4413. }
  4414. if (doc.nodes[node].tag == BR_TAG_INPUT ||
  4415. doc.nodes[node].tag == BR_TAG_TEXTAREA ||
  4416. doc.nodes[node].tag == BR_TAG_SELECT ||
  4417. doc.nodes[node].tag == BR_TAG_BUTTON) {
  4418. if (doc.nodes[node].tag == BR_TAG_INPUT &&
  4419. s_eq(br_type(&doc, node), "hidden"))
  4420. return;
  4421. add_form_run(l, node, st, link);
  4422. l->y += doc.nodes[node].tag == BR_TAG_TEXTAREA ? 40 :
  4423. doc.nodes[node].tag == BR_TAG_BUTTON ? 22 : 20;
  4424. return;
  4425. }
  4426. flush_line(l);
  4427. cbw = old_right - old_left;
  4428. if (cbw < 8)
  4429. cbw = 8;
  4430. l->y += mp_px(st, 0, cbw);
  4431. top = l->y;
  4432. ml = mp_px(st, 3, cbw);
  4433. mr = mp_px(st, 1, cbw);
  4434. bl = st->border_w[3];
  4435. br = st->border_w[1];
  4436. pl = mp_px(st, 7, cbw);
  4437. pr = mp_px(st, 5, cbw);
  4438. bt = st->border_w[0];
  4439. bb = st->border_w[2];
  4440. pt = mp_px(st, 4, cbw);
  4441. pb = mp_px(st, 6, cbw);
  4442. deco_w = bl + br + pl + pr;
  4443. deco_h = bt + bb + pt + pb;
  4444. if (st->width_kind) {
  4445. int w;
  4446. if (st->width_kind == 3)
  4447. w = cbw * st->width_calc_pct / 100 +
  4448. st->width_calc_px;
  4449. else
  4450. w = css_size(st->width_kind, st->width, cbw);
  4451. if (w < 0)
  4452. w = 0;
  4453. content_w = st->box_sizing ? w - deco_w : w;
  4454. definite_w = 1;
  4455. } else {
  4456. content_w = cbw - ml - mr - deco_w;
  4457. }
  4458. if (content_w < 0)
  4459. content_w = 0;
  4460. if (st->max_width_kind) {
  4461. int mw = st->max_width_kind == 3 ?
  4462. cbw * st->max_width_calc_pct / 100 +
  4463. st->max_width_calc_px :
  4464. css_size(st->max_width_kind, st->max_width, cbw);
  4465. int max_box = st->box_sizing ? mw : mw + deco_w;
  4466. if (content_w + deco_w > max_box)
  4467. content_w = max_box - deco_w;
  4468. }
  4469. if (st->min_width_kind) {
  4470. int mw = st->min_width_kind == 3 ?
  4471. cbw * st->min_width_calc_pct / 100 +
  4472. st->min_width_calc_px :
  4473. css_size(st->min_width_kind, st->min_width, cbw);
  4474. int min_box = st->box_sizing ? mw : mw + deco_w;
  4475. if (content_w + deco_w < min_box) {
  4476. content_w = min_box - deco_w;
  4477. definite_w = 1;
  4478. }
  4479. }
  4480. if (content_w < 8)
  4481. content_w = 8;
  4482. if (definite_w) {
  4483. int free_w = cbw - ml - mr - deco_w - content_w;
  4484. if (free_w < 0)
  4485. free_w = 0;
  4486. if (st->ml_auto && st->mr_auto) {
  4487. ml = free_w / 2;
  4488. mr = free_w - ml;
  4489. } else if (st->ml_auto) {
  4490. ml = free_w;
  4491. } else if (st->mr_auto) {
  4492. mr = free_w;
  4493. }
  4494. }
  4495. l0 = old_left + ml;
  4496. r0 = l0 + deco_w + content_w;
  4497. if (r0 < l0 + 8)
  4498. r0 = l0 + 8;
  4499. normal_top = top;
  4500. if (sticky) {
  4501. if (st->top_set) {
  4502. int stick = page_rect.y + 4 + st->top;
  4503. if (normal_top < stick)
  4504. rel_dy += stick - normal_top;
  4505. } else if (st->bottom_set && block_h[node]) {
  4506. int stick = page_rect.y + page_rect.h - st->bottom -
  4507. block_h[node];
  4508. if (normal_top > stick)
  4509. rel_dy += stick - normal_top;
  4510. }
  4511. if (l->draw && doc.nodes[node].parent < BR_MAX_NODES &&
  4512. block_h[doc.nodes[node].parent]) {
  4513. unsigned int p = doc.nodes[node].parent;
  4514. int ptop = l->parent_top;
  4515. int pbottom = ptop + block_h[p];
  4516. int eh = block_h[node];
  4517. int moved_top = normal_top + rel_dy;
  4518. int moved_bottom = moved_top + eh;
  4519. if (moved_bottom > pbottom)
  4520. rel_dy -= moved_bottom - pbottom;
  4521. moved_top = normal_top + rel_dy;
  4522. if (moved_top < ptop)
  4523. rel_dy += ptop - moved_top;
  4524. }
  4525. }
  4526. if (shifted && l->draw) {
  4527. top += rel_dy;
  4528. l0 += rel_dx;
  4529. r0 += rel_dx;
  4530. }
  4531. l->parent_top = top;
  4532. if (l->draw && block_h[node] &&
  4533. top < page_rect.y + page_rect.h &&
  4534. top + block_h[node] > page_rect.y) {
  4535. struct ngui_rect box = {l0, top, r0 - l0, block_h[node]};
  4536. int rtl = css_radius(st, 0, box.w, box.h);
  4537. int rtr = css_radius(st, 1, box.w, box.h);
  4538. int rbr = css_radius(st, 2, box.w, box.h);
  4539. int rbl = css_radius(st, 3, box.w, box.h);
  4540. if (st->transform_scale) {
  4541. if (st->has_bg)
  4542. fill_transformed_rect(l->ui, box, st->bg, st);
  4543. } else if (rtl || rtr || rbr || rbl) {
  4544. if (st->has_bg) {
  4545. int bw = st->border_w[0];
  4546. if (bw > 0 && st->border_c[0] != st->bg &&
  4547. box.w > bw * 2 && box.h > bw * 2) {
  4548. struct ngui_rect inner = {
  4549. box.x + bw, box.y + bw,
  4550. box.w - bw * 2, box.h - bw * 2
  4551. };
  4552. int itl = rtl > bw ? rtl - bw : 0;
  4553. int itr = rtr > bw ? rtr - bw : 0;
  4554. int ibr = rbr > bw ? rbr - bw : 0;
  4555. int ibl = rbl > bw ? rbl - bw : 0;
  4556. ngui_fill_round_rect(l->ui, box, rtl,
  4557. rtr, rbr, rbl,
  4558. st->border_c[0]);
  4559. ngui_fill_round_rect(l->ui, inner,
  4560. itl, itr, ibr,
  4561. ibl, st->bg);
  4562. } else {
  4563. ngui_fill_round_rect(l->ui, box, rtl,
  4564. rtr, rbr, rbl,
  4565. st->bg);
  4566. }
  4567. } else if (st->border_w[0]) {
  4568. ngui_frame_round(l->ui, box, rtl, rtr, rbr,
  4569. rbl, st->border_w[0],
  4570. st->border_c[0]);
  4571. }
  4572. } else {
  4573. if (st->has_bg)
  4574. ngui_fill_rect(l->ui, box, st->bg);
  4575. if (st->bg_image)
  4576. draw_bg_image(l, box, node, st);
  4577. if (st->border_w[0] || st->border_w[1] ||
  4578. st->border_w[2] || st->border_w[3])
  4579. draw_border_box(l->ui, box, st);
  4580. }
  4581. }
  4582. if (l->draw && st->overflow_hidden && block_h[node] > 0 &&
  4583. l->ui && l->ui->nclip < 4) {
  4584. clip_rect.x = l0 + bl;
  4585. clip_rect.y = top + bt;
  4586. clip_rect.w = (r0 - l0) - bl - br;
  4587. clip_rect.h = (int)block_h[node] - bt - bb;
  4588. if (clip_rect.w > 0 && clip_rect.h > 0) {
  4589. ngui_push_clip(l->ui, clip_rect);
  4590. clip_pushed = 1;
  4591. }
  4592. }
  4593. l->left = l0 + bl + pl;
  4594. l->right = l->left + content_w;
  4595. if (l->right < l->left + 8)
  4596. l->right = l->left + 8;
  4597. l->align = st->align;
  4598. l->line_left = l->left;
  4599. l->line_right = l->right;
  4600. l->y = top + bt + pt;
  4601. if (doc.nodes[node].tag == BR_TAG_HR) {
  4602. if (l->draw)
  4603. ngui_fill_rect(l->ui, (struct ngui_rect){l->left,
  4604. l->y + 2, l->right - l->left, 1}, BR_GRAY);
  4605. l->y += 8;
  4606. } else if (doc.nodes[node].tag == BR_TAG_VIDEO) {
  4607. int vw = l->right - l->left;
  4608. int vh;
  4609. if (doc.nodes[node].width_pct)
  4610. vw = vw * doc.nodes[node].width_attr / 100;
  4611. else if (doc.nodes[node].width_attr)
  4612. vw = doc.nodes[node].width_attr;
  4613. if (vw > l->right - l->left)
  4614. vw = l->right - l->left;
  4615. if (vw < 8)
  4616. vw = 8;
  4617. vh = vw * 9 / 16;
  4618. if (vh > 240)
  4619. vh = 240;
  4620. if (vh < 80)
  4621. vh = 80;
  4622. if (l->draw) {
  4623. ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y,
  4624. vw, vh}, 0xD69A);
  4625. ngui_frame(l->ui, (struct ngui_rect){l->left, l->y,
  4626. vw, vh}, 0x8410);
  4627. ngui_text(l->ui, l->left + 8, l->y + 8, "[video]",
  4628. 0x4208);
  4629. }
  4630. l->y += vh + 4;
  4631. } else if (doc.nodes[node].tag == BR_TAG_SVG) {
  4632. int vw, vh, dw = l->right - l->left;
  4633. int dh;
  4634. svg_viewbox(node, &vw, &vh);
  4635. if (dw < 8)
  4636. dw = 8;
  4637. dh = dw * vh / vw;
  4638. if (dh < 8)
  4639. dh = 8;
  4640. if (dh > 1400)
  4641. dh = 1400;
  4642. if (l->draw) {
  4643. struct ngui_rect clip = {l->left, l->y, dw, dh};
  4644. ngui_push_clip(l->ui, clip);
  4645. draw_svg(l->ui, node, l->left, l->y, dw, dh,
  4646. vw, vh, st);
  4647. ngui_pop_clip(l->ui);
  4648. }
  4649. l->y += dh;
  4650. } else {
  4651. if (doc.nodes[node].tag == BR_TAG_LI && st->list_item &&
  4652. !st->no_bullet) {
  4653. char *m = li_marker[node];
  4654. if (!m[0]) {
  4655. unsigned int parent = doc.nodes[node].parent;
  4656. if (parent &&
  4657. doc.nodes[parent].tag == BR_TAG_OL) {
  4658. int idx = list_index(parent, node);
  4659. unsigned int n = 0;
  4660. s_append_dec(m, &n, (unsigned int)idx, 6);
  4661. s_append(m, &n, ".", 6);
  4662. } else {
  4663. s_copy(m, "-", 6);
  4664. }
  4665. }
  4666. if (l->draw) {
  4667. int mw = (int)ngui_text_width_ui(l->ui, m);
  4668. ngui_text(l->ui, l->left - mw - 6, l->y, m,
  4669. st->color);
  4670. }
  4671. }
  4672. if (doc.nodes[node].tag == BR_TAG_TR)
  4673. layout_table_row(l, node, st, depth + 1, link);
  4674. else if (doc.nodes[node].tag == BR_TAG_TABLE &&
  4675. table_has_rowspan(node))
  4676. layout_table(l, node, st, depth + 1, link);
  4677. else if (st->grid_auto)
  4678. layout_grid_auto(l, node, st, depth + 1, link);
  4679. else if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 &&
  4680. st->grid_gap > 0)
  4681. layout_grid_children(l, node, st, depth + 1, link);
  4682. else if (st->display == BR_DISPLAY_FLEX && !st->flex_col &&
  4683. flex_wants_row(node, st))
  4684. layout_flex_children(l, node, st, depth + 1, link);
  4685. else
  4686. layout_children(l, node, st, depth + 1, link);
  4687. }
  4688. flush_line(l);
  4689. if (clip_pushed)
  4690. ngui_pop_clip(l->ui);
  4691. if (l->float_active) {
  4692. if (l->y < l->float_bottom)
  4693. l->y = l->float_bottom;
  4694. l->float_active = 0;
  4695. }
  4696. if (st->height_kind) {
  4697. content_h = style_height_px(st, cbw);
  4698. if (st->box_sizing)
  4699. content_h -= deco_h;
  4700. } else {
  4701. content_h = l->y - (top + bt + pt);
  4702. }
  4703. if (content_h < 0)
  4704. content_h = 0;
  4705. if (st->max_height_kind) {
  4706. int mh = st->max_height_kind == 3 ?
  4707. cbw * st->max_height_calc_pct / 100 +
  4708. st->max_height_calc_px :
  4709. css_size(st->max_height_kind, st->max_height, cbw);
  4710. int max_box = st->box_sizing ? mh : mh + deco_h;
  4711. if (content_h + deco_h > max_box)
  4712. content_h = max_box - deco_h;
  4713. }
  4714. if (st->min_height_kind) {
  4715. int mh = st->min_height_kind == 3 ?
  4716. cbw * st->min_height_calc_pct / 100 +
  4717. st->min_height_calc_px :
  4718. css_size(st->min_height_kind, st->min_height, cbw);
  4719. int min_box = st->box_sizing ? mh : mh + deco_h;
  4720. if (content_h + deco_h < min_box)
  4721. content_h = min_box - deco_h;
  4722. }
  4723. if (content_h < 0)
  4724. content_h = 0;
  4725. box_h = deco_h + content_h;
  4726. if (box_h < 0)
  4727. box_h = 0;
  4728. l->y = (shifted ? normal_top : top) + box_h;
  4729. if (node < BR_MAX_NODES) {
  4730. layout_boxes[node].x = l0;
  4731. layout_boxes[node].y = top;
  4732. layout_boxes[node].w = r0 - l0;
  4733. layout_boxes[node].h = box_h;
  4734. layout_boxes[node].valid = r0 > l0;
  4735. }
  4736. l->left = old_left;
  4737. l->right = old_right;
  4738. l->align = old_align;
  4739. l->y += mp_px(st, 2, cbw);
  4740. if (!l->draw) {
  4741. if (box_h > 65535)
  4742. box_h = 65535;
  4743. block_h[node] = (unsigned short)box_h;
  4744. }
  4745. if (positioned) {
  4746. l->y = flow_y;
  4747. l->left = flow_left;
  4748. l->right = flow_right;
  4749. }
  4750. l->parent_top = saved_parent_top;
  4751. }
  4752. static int text_node_blank(const struct br_node *nd)
  4753. {
  4754. const char *t = doc.text + nd->text_off;
  4755. unsigned int i;
  4756. for (i = 0; i < nd->text_len; i++)
  4757. if (t[i] != ' ' && t[i] != '\t' && t[i] != '\n' &&
  4758. t[i] != '\r')
  4759. return 0;
  4760. return 1;
  4761. }
  4762. /* Rough shrink-to-fit width for a float without an explicit width: sum the
  4763. * text/image widths in the subtree, plus padding and margins. This keeps
  4764. * Bootstrap's floated nav items on one line instead of giving each the whole
  4765. * row. */
  4766. static int text_space(char c)
  4767. {
  4768. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  4769. }
  4770. static int intrinsic_width(struct ngui *ui, unsigned int node,
  4771. const struct br_style *pst, int depth, int base)
  4772. {
  4773. unsigned int c;
  4774. int w = 0;
  4775. if (depth > 12)
  4776. return 0;
  4777. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  4778. struct br_node *nd = &doc.nodes[c];
  4779. struct br_style cs;
  4780. if (nd->type == BR_NODE_TEXT) {
  4781. const char *t = doc.text + nd->text_off;
  4782. unsigned int tn = nd->text_len;
  4783. if (doc.nodes[node].tag == BR_TAG_PRE) {
  4784. w += (int)face_text_width(ui, pst, t, tn);
  4785. } else {
  4786. unsigned int ti = 0;
  4787. int first = 1;
  4788. int lead = w > 0 && tn > 0 && text_space(t[0]);
  4789. int sp = (int)face_text_width(ui, pst, " ", 1);
  4790. while (ti < tn) {
  4791. unsigned int ts;
  4792. while (ti < tn && text_space(t[ti]))
  4793. ti++;
  4794. ts = ti;
  4795. while (ti < tn && !text_space(t[ti]))
  4796. ti++;
  4797. if (ti == ts)
  4798. break;
  4799. if (!first || lead)
  4800. w += sp;
  4801. lead = 0;
  4802. w += (int)face_text_width(ui, pst,
  4803. t + ts, ti - ts);
  4804. first = 0;
  4805. }
  4806. }
  4807. continue;
  4808. }
  4809. br_style_for(&doc, c, pst, &cs);
  4810. if (cs.display == BR_DISPLAY_NONE)
  4811. continue;
  4812. if (nd->tag == BR_TAG_BR) {
  4813. w += 8;
  4814. continue;
  4815. }
  4816. if (nd->tag == BR_TAG_IMG) {
  4817. /* Measure the image as layout will draw it: a CSS or
  4818. * attribute height keeps the intrinsic aspect ratio, and
  4819. * a CSS width overrides the native pixel width. Bootstrap
  4820. * sizes the Cloudron logo with height:40px only. */
  4821. int idx = nd->img_index;
  4822. int iw = 0, ih = 0;
  4823. if (idx >= 0 && idx < image_count &&
  4824. images[idx].state == 1) {
  4825. iw = (int)images[idx].native_w;
  4826. ih = (int)images[idx].native_h;
  4827. if (iw <= 0 || ih <= 0) {
  4828. iw = (int)images[idx].img.w;
  4829. ih = (int)images[idx].img.h;
  4830. }
  4831. }
  4832. if (cs.width_kind) {
  4833. int ww = style_width_px(&cs, base);
  4834. w += ww > 0 ? ww : 64;
  4835. } else if (nd->width_attr) {
  4836. w += nd->width_attr;
  4837. } else if (nd->height_attr && iw && ih) {
  4838. w += iw * (int)nd->height_attr / ih;
  4839. } else if (cs.height_kind && iw && ih) {
  4840. int hh = style_height_px(&cs, base);
  4841. w += hh > 0 ? iw * hh / ih : iw;
  4842. } else if (iw) {
  4843. w += iw;
  4844. } else {
  4845. w += 64;
  4846. }
  4847. continue;
  4848. }
  4849. w += mp_px(&cs, 7, base) +
  4850. mp_px(&cs, 5, base) +
  4851. mp_px(&cs, 3, base) +
  4852. mp_px(&cs, 1, base);
  4853. w += intrinsic_width(ui, c, &cs, depth + 1, base);
  4854. if (w > 4096)
  4855. break;
  4856. }
  4857. return w;
  4858. }
  4859. /* Lay consecutive floated block children side by side. Bootstrap's grid is
  4860. * the main user: .row > .col-md-* with float:left and a percentage width.
  4861. * Each column is laid out with its own left/right edge and the row advances
  4862. * to the tallest column. Returns the first child not consumed. */
  4863. static unsigned int layout_float_group(struct lay *l, unsigned int first,
  4864. const struct br_style *pst, int depth,
  4865. unsigned short link)
  4866. {
  4867. unsigned int n = first;
  4868. int row_left = l->left, row_right = l->right;
  4869. int row_top, row_bottom, xl, xr, any = 0;
  4870. int fl_left = row_left, fl_right = row_right, fl_bottom;
  4871. flush_line(l);
  4872. row_top = l->y;
  4873. row_bottom = l->y;
  4874. fl_bottom = row_top;
  4875. xl = row_left;
  4876. xr = row_right;
  4877. while (n) {
  4878. struct br_node *nd = &doc.nodes[n];
  4879. struct br_style cs;
  4880. unsigned short lk = link;
  4881. int colw, right;
  4882. if (nd->type == BR_NODE_TEXT) {
  4883. if (text_node_blank(nd)) {
  4884. n = nd->next;
  4885. continue;
  4886. }
  4887. break;
  4888. }
  4889. br_style_for(&doc, n, pst, &cs);
  4890. if (cs.display == BR_DISPLAY_NONE) {
  4891. n = nd->next;
  4892. continue;
  4893. }
  4894. /* A float blockifies its box: Bootstrap floats inline
  4895. * anchors too (the navbar brand and its text links), and
  4896. * their padding must be honoured. */
  4897. if (!cs.float_left)
  4898. break;
  4899. if (nd->tag == BR_TAG_A && nd->href_off)
  4900. lk = (unsigned short)n;
  4901. right = cs.float_left == 2;
  4902. if (cs.width_kind == 2 || cs.width_kind == 3) {
  4903. colw = style_width_px(&cs, row_right - row_left);
  4904. } else if (cs.width_kind == 1) {
  4905. colw = (int)cs.width;
  4906. } else {
  4907. int base = row_right - row_left;
  4908. colw = intrinsic_width(l->ui, n, &cs, 0, base) +
  4909. mp_px(&cs, 7, base) +
  4910. mp_px(&cs, 5, base) +
  4911. mp_px(&cs, 3, base) +
  4912. mp_px(&cs, 1, base);
  4913. if (colw < 8)
  4914. colw = 8;
  4915. }
  4916. if (colw < 8)
  4917. colw = 8;
  4918. if (colw > row_right - row_left)
  4919. colw = row_right - row_left;
  4920. /* The float column width is already resolved. */
  4921. cs.width_kind = 0;
  4922. cs.max_width_kind = 0;
  4923. if (right) {
  4924. if (any && xr - colw < xl) {
  4925. row_top = row_bottom;
  4926. row_bottom = row_top;
  4927. xl = row_left;
  4928. xr = row_right;
  4929. }
  4930. if (xr - colw < xl)
  4931. colw = xr - xl;
  4932. if (colw < 8)
  4933. colw = 8;
  4934. l->left = xr - colw;
  4935. if (l->left < row_left)
  4936. l->left = row_left;
  4937. l->right = xr;
  4938. xr = l->left;
  4939. if (xr < fl_right)
  4940. fl_right = xr;
  4941. } else {
  4942. if (any && xl + colw > xr) {
  4943. row_top = row_bottom;
  4944. row_bottom = row_top;
  4945. xl = row_left;
  4946. xr = row_right;
  4947. }
  4948. l->left = xl;
  4949. l->right = xl + colw;
  4950. xl += colw;
  4951. if (xl > fl_left)
  4952. fl_left = xl;
  4953. }
  4954. if (l->right < l->left + 8)
  4955. l->right = l->left + 8;
  4956. l->y = row_top;
  4957. l->line_x = l->left;
  4958. l->run_x = l->left;
  4959. l->line_y = l->y;
  4960. l->line_h = 0;
  4961. l->nruns = 0;
  4962. l->space_pending = 0;
  4963. l->align = cs.align;
  4964. layout_block(l, n, &cs, depth + 1, lk);
  4965. if (l->y > row_bottom)
  4966. row_bottom = l->y;
  4967. if (l->y > fl_bottom)
  4968. fl_bottom = l->y;
  4969. any = 1;
  4970. n = nd->next;
  4971. }
  4972. l->left = row_left;
  4973. l->right = row_right;
  4974. if (n && any) {
  4975. struct br_node *nd = &doc.nodes[n];
  4976. int inline_next = 0;
  4977. if (nd->type == BR_NODE_TEXT) {
  4978. inline_next = 1;
  4979. } else if (nd->type == BR_NODE_ELEM) {
  4980. struct br_style cs;
  4981. br_style_for(&doc, n, pst, &cs);
  4982. inline_next = cs.display != BR_DISPLAY_BLOCK &&
  4983. cs.display != BR_DISPLAY_NONE &&
  4984. !br_tag_is_block(nd->tag);
  4985. }
  4986. if (inline_next) {
  4987. if (l->float_active) {
  4988. if (l->float_left > fl_left)
  4989. fl_left = l->float_left;
  4990. if (l->float_right < fl_right)
  4991. fl_right = l->float_right;
  4992. if (l->float_bottom > fl_bottom)
  4993. fl_bottom = l->float_bottom;
  4994. }
  4995. l->float_active = 1;
  4996. l->float_left = fl_left;
  4997. l->float_right = fl_right;
  4998. l->float_bottom = fl_bottom;
  4999. l->y = row_top;
  5000. } else {
  5001. l->float_active = 0;
  5002. l->y = row_bottom;
  5003. }
  5004. } else {
  5005. l->float_active = 0;
  5006. l->y = row_bottom;
  5007. }
  5008. l->line_x = l->left;
  5009. l->run_x = l->left;
  5010. l->line_left = l->left;
  5011. l->line_right = l->right;
  5012. l->line_y = l->y;
  5013. l->line_h = 0;
  5014. l->nruns = 0;
  5015. l->space_pending = 0;
  5016. return n;
  5017. }
  5018. static int flex_item_width(struct lay *l, unsigned int n,
  5019. const struct br_style *cs, int avail)
  5020. {
  5021. struct br_node *nd = &doc.nodes[n];
  5022. (void)l;
  5023. int w;
  5024. if (cs->width_kind == 2 || cs->width_kind == 3)
  5025. w = style_width_px(cs, avail);
  5026. else if (cs->width_kind == 1)
  5027. w = (int)cs->width;
  5028. else if (nd->tag == BR_TAG_IMG) {
  5029. int idx = nd->img_index;
  5030. if (idx >= 0 && idx < image_count &&
  5031. images[idx].state == 1)
  5032. w = (int)images[idx].img.w;
  5033. else if (nd->width_attr)
  5034. w = (int)nd->width_attr;
  5035. else
  5036. w = 64;
  5037. } else {
  5038. w = intrinsic_width(l->ui, n, cs, 0, avail);
  5039. w += mp_px(cs, 7, avail);
  5040. w += mp_px(cs, 5, avail);
  5041. w += mp_px(cs, 3, avail);
  5042. w += mp_px(cs, 1, avail);
  5043. }
  5044. if (w < 24)
  5045. w = 24;
  5046. if (w > avail)
  5047. w = avail;
  5048. return w;
  5049. }
  5050. /* A run of display:inline-block siblings (file tags, small buttons) belongs
  5051. * on one line, not one full-width block each. Place intrinsic widths from
  5052. * left to right and wrap when the row is full. */
  5053. static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
  5054. const struct br_style *pst, int depth,
  5055. unsigned short link)
  5056. {
  5057. unsigned int n = first;
  5058. int row_left = l->left, row_right = l->right;
  5059. int avail = row_right - row_left;
  5060. int x = row_left, y = l->y, row_h = 0, any = 0;
  5061. flush_line(l);
  5062. y = l->y;
  5063. while (n) {
  5064. struct br_node *nd = &doc.nodes[n];
  5065. struct br_style cs;
  5066. unsigned short lk = link;
  5067. int colw, h;
  5068. if (nd->type == BR_NODE_TEXT) {
  5069. if (!text_node_blank(nd))
  5070. break;
  5071. if (any)
  5072. x += 4;
  5073. n = nd->next;
  5074. continue;
  5075. }
  5076. br_style_for(&doc, n, pst, &cs);
  5077. if (cs.display == BR_DISPLAY_NONE) {
  5078. n = nd->next;
  5079. continue;
  5080. }
  5081. if (cs.display != BR_DISPLAY_INLINE_BLOCK)
  5082. break;
  5083. if (nd->tag == BR_TAG_A && nd->href_off)
  5084. lk = (unsigned short)n;
  5085. colw = flex_item_width(l, n, &cs, avail);
  5086. /* A few pixels of slack keep a single long token (boot.S)
  5087. * from wrapping at the exact content width. */
  5088. colw += 8;
  5089. if (colw > avail)
  5090. colw = avail;
  5091. cs.width_kind = 0;
  5092. cs.max_width_kind = 0;
  5093. if (any && x + colw > row_right) {
  5094. y += row_h + 2;
  5095. x = row_left;
  5096. row_h = 0;
  5097. any = 0;
  5098. }
  5099. if (any && x > row_left)
  5100. x += 4;
  5101. l->left = x;
  5102. l->right = x + colw;
  5103. if (l->right < l->left + 8)
  5104. l->right = l->left + 8;
  5105. l->y = y;
  5106. l->line_x = l->left;
  5107. l->run_x = l->left;
  5108. l->line_y = l->y;
  5109. l->line_h = 0;
  5110. l->nruns = 0;
  5111. l->space_pending = 0;
  5112. l->align = cs.align;
  5113. layout_block(l, n, &cs, depth + 1, lk);
  5114. h = l->y - y;
  5115. if (h > row_h)
  5116. row_h = h;
  5117. x += colw;
  5118. any = 1;
  5119. n = nd->next;
  5120. }
  5121. if (any)
  5122. y += row_h;
  5123. l->left = row_left;
  5124. l->right = row_right;
  5125. l->y = y;
  5126. l->line_x = l->left;
  5127. l->run_x = l->left;
  5128. l->line_y = l->y;
  5129. l->line_h = 0;
  5130. l->nruns = 0;
  5131. l->space_pending = 0;
  5132. return n;
  5133. }
  5134. struct flex_item {
  5135. unsigned int node;
  5136. unsigned short link;
  5137. int w;
  5138. };
  5139. static void layout_flex_children(struct lay *l, unsigned int parent,
  5140. const struct br_style *pst, int depth,
  5141. unsigned short link)
  5142. {
  5143. struct flex_item it[32];
  5144. unsigned int c;
  5145. int nitem = 0, grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
  5146. int row_left = l->left, row_right = l->right;
  5147. int avail = row_right - row_left;
  5148. int idx, row_top;
  5149. if (avail < 160 || depth > 24) {
  5150. layout_children(l, parent, pst, depth, link);
  5151. return;
  5152. }
  5153. /* If direct text is not just whitespace, normal block flow handles it
  5154. * better than pretending it is a flex item. */
  5155. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  5156. struct br_node *nd = &doc.nodes[c];
  5157. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  5158. layout_children(l, parent, pst, depth, link);
  5159. return;
  5160. }
  5161. }
  5162. for (c = doc.nodes[parent].first;
  5163. c && nitem < (int)(sizeof(it) / sizeof(it[0]));
  5164. c = doc.nodes[c].next) {
  5165. struct br_node *nd = &doc.nodes[c];
  5166. struct br_style cs;
  5167. if (nd->type == BR_NODE_TEXT)
  5168. continue;
  5169. br_style_for(&doc, c, pst, &cs);
  5170. if (cs.display == BR_DISPLAY_NONE)
  5171. continue;
  5172. it[nitem].node = c;
  5173. it[nitem].link = (nd->tag == BR_TAG_A && nd->href_off) ?
  5174. (unsigned short)c : link;
  5175. it[nitem].w = 0;
  5176. if (cs.flex_grow && !cs.width_kind)
  5177. grow_count++;
  5178. else
  5179. fixed += flex_item_width(l, c, &cs, avail);
  5180. nitem++;
  5181. }
  5182. grow_w = grow_count ? (avail - fixed) / grow_count : 0;
  5183. if (grow_w < 96)
  5184. grow_w = 96;
  5185. for (idx = 0; idx < nitem; idx++) {
  5186. struct br_style cs;
  5187. br_style_for(&doc, it[idx].node, pst, &cs);
  5188. if (cs.flex_grow && !cs.width_kind)
  5189. it[idx].w = grow_w;
  5190. else
  5191. it[idx].w = flex_item_width(l, it[idx].node, &cs, avail);
  5192. if (it[idx].w > avail)
  5193. it[idx].w = avail;
  5194. if (it[idx].w < 8)
  5195. it[idx].w = 8;
  5196. }
  5197. row_top = l->y;
  5198. idx = 0;
  5199. while (idx < nitem) {
  5200. int start = idx, count = 0, row_w = 0;
  5201. int free_w, x, extra = 0, row_h = 0, k;
  5202. while (idx < nitem) {
  5203. int add = it[idx].w + (count ? gap : 0);
  5204. if (count && pst->flex_wrap != 2 &&
  5205. row_w + add > avail)
  5206. break;
  5207. row_w += add;
  5208. count++;
  5209. idx++;
  5210. }
  5211. if (!count) {
  5212. count = 1;
  5213. row_w = it[start].w;
  5214. idx = start + 1;
  5215. }
  5216. free_w = avail - row_w;
  5217. if (free_w < 0)
  5218. free_w = 0;
  5219. x = row_left;
  5220. if (pst->justify == 1) {
  5221. x += free_w / 2;
  5222. } else if (pst->justify == 2) {
  5223. x += free_w;
  5224. } else if (pst->justify == 3) {
  5225. extra = count > 1 ? free_w / (count - 1) : 0;
  5226. } else if (pst->justify == 4) {
  5227. extra = count ? free_w / count : 0;
  5228. x += extra / 2;
  5229. }
  5230. for (k = 0; k < count; k++) {
  5231. int node = (int)it[start + k].node;
  5232. int h = block_h[node];
  5233. if (h > row_h)
  5234. row_h = h;
  5235. }
  5236. if (pst->height_kind) {
  5237. int hh = style_height_px(pst, avail);
  5238. if (hh > row_h)
  5239. row_h = hh;
  5240. }
  5241. for (k = 0; k < count; k++) {
  5242. struct br_style cs;
  5243. int node = (int)it[start + k].node;
  5244. int yoff = 0;
  5245. br_style_for(&doc, node, pst, &cs);
  5246. cs.width_kind = 0;
  5247. cs.max_width_kind = 0;
  5248. if (l->draw && pst->align_items &&
  5249. row_h > block_h[node]) {
  5250. if (pst->align_items == 1)
  5251. yoff = (row_h - block_h[node]) / 2;
  5252. else if (pst->align_items == 2)
  5253. yoff = row_h - block_h[node];
  5254. }
  5255. l->left = x;
  5256. l->right = x + it[start + k].w;
  5257. if (l->right < l->left + 8)
  5258. l->right = l->left + 8;
  5259. l->y = row_top + yoff;
  5260. l->line_x = l->left;
  5261. l->run_x = l->left;
  5262. l->line_y = l->y;
  5263. l->line_h = 0;
  5264. l->nruns = 0;
  5265. l->space_pending = 0;
  5266. l->align = cs.align;
  5267. layout_block(l, node, &cs, depth + 1, it[start + k].link);
  5268. if (!l->draw) {
  5269. int h = l->y - row_top;
  5270. if (h > row_h)
  5271. row_h = h;
  5272. }
  5273. x += it[start + k].w;
  5274. if (k + 1 < count)
  5275. x += gap + extra;
  5276. }
  5277. row_top += row_h;
  5278. }
  5279. l->left = row_left;
  5280. l->right = row_right;
  5281. l->y = row_top;
  5282. l->line_x = l->left;
  5283. l->run_x = l->left;
  5284. l->line_y = l->y;
  5285. l->line_h = 0;
  5286. l->nruns = 0;
  5287. l->space_pending = 0;
  5288. }
  5289. /* Two-column CSS grid: track sizes from grid-template-columns (px, %, fr)
  5290. * plus the grid-area hints used by site shells (site notice/footer full
  5291. * width, column-start left, page-content right). Anything more elaborate
  5292. * falls back to normal block flow. */
  5293. static int grid_track_width(unsigned char kind, unsigned short val,
  5294. int avail, int other)
  5295. {
  5296. if (kind == 2)
  5297. return avail * (int)val / 100;
  5298. if (kind == 1)
  5299. return val;
  5300. return other;
  5301. }
  5302. static void layout_grid_children(struct lay *l, unsigned int parent,
  5303. const struct br_style *pst, int depth,
  5304. unsigned short link)
  5305. {
  5306. unsigned int n;
  5307. int row_left = l->left, row_right = l->right;
  5308. int avail = row_right - row_left;
  5309. int gap = pst->grid_gap;
  5310. int track_total = avail - gap;
  5311. int col0, col1, x0, x1;
  5312. int row_y, row_bottom, have_left = 0, have_right = 0;
  5313. unsigned int c;
  5314. if (avail < 32 || depth > 24)
  5315. return;
  5316. if (track_total < 16)
  5317. track_total = 16;
  5318. col0 = grid_track_width((unsigned char)(pst->width_kind),
  5319. pst->width, track_total, 0);
  5320. col1 = grid_track_width((unsigned char)(pst->max_width_kind),
  5321. pst->max_width, track_total, 0);
  5322. if (pst->width_kind == 3 && pst->max_width_kind == 3) {
  5323. int w0 = pst->width ? pst->width : 1;
  5324. int w1 = pst->max_width ? pst->max_width : 1;
  5325. col0 = track_total * w0 / (w0 + w1);
  5326. col1 = track_total - col0;
  5327. } else if (pst->width_kind == 3) {
  5328. col0 = track_total - col1;
  5329. if (col0 < 24)
  5330. col0 = 24;
  5331. } else if (pst->max_width_kind == 3) {
  5332. col1 = track_total - col0;
  5333. if (col1 < 24)
  5334. col1 = 24;
  5335. }
  5336. if (col0 < 24)
  5337. col0 = 24;
  5338. if (col1 < 24)
  5339. col1 = 24;
  5340. if (col0 + col1 > track_total) {
  5341. col1 = track_total - col0;
  5342. if (col1 < 24)
  5343. col1 = 24;
  5344. }
  5345. x0 = row_left;
  5346. x1 = row_left + col0 + gap;
  5347. /* Direct text makes a grid ambiguous; use block flow instead. */
  5348. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  5349. struct br_node *nd = &doc.nodes[c];
  5350. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  5351. layout_children(l, parent, pst, depth, link);
  5352. return;
  5353. }
  5354. }
  5355. row_y = row_bottom = l->y;
  5356. n = doc.nodes[parent].first;
  5357. while (n) {
  5358. struct br_node *nd = &doc.nodes[n];
  5359. struct br_style cs;
  5360. unsigned short lk = link;
  5361. int slot, x, w;
  5362. if (nd->type == BR_NODE_TEXT) {
  5363. n = nd->next;
  5364. continue;
  5365. }
  5366. br_style_for(&doc, n, pst, &cs);
  5367. if (cs.display == BR_DISPLAY_NONE) {
  5368. n = nd->next;
  5369. continue;
  5370. }
  5371. if (nd->tag == BR_TAG_A && nd->href_off)
  5372. lk = (unsigned short)n;
  5373. slot = cs.grid_slot;
  5374. if (!slot) {
  5375. if (!have_left)
  5376. slot = 2;
  5377. else if (!have_right)
  5378. slot = 3;
  5379. else
  5380. slot = 2;
  5381. }
  5382. if (slot == 1) {
  5383. if (have_left || have_right) {
  5384. row_y = row_bottom;
  5385. have_left = have_right = 0;
  5386. }
  5387. x = row_left;
  5388. w = avail;
  5389. } else if (slot == 2) {
  5390. if (have_left) {
  5391. row_y = row_bottom;
  5392. have_left = have_right = 0;
  5393. }
  5394. x = x0;
  5395. w = col0;
  5396. have_left = 1;
  5397. } else {
  5398. if (have_right) {
  5399. row_y = row_bottom;
  5400. have_left = have_right = 0;
  5401. }
  5402. x = x1;
  5403. w = col1;
  5404. have_right = 1;
  5405. }
  5406. cs.width_kind = 0;
  5407. cs.max_width_kind = 0;
  5408. l->left = x;
  5409. l->right = x + w;
  5410. if (l->right < l->left + 8)
  5411. l->right = l->left + 8;
  5412. l->y = row_y;
  5413. l->line_x = l->left;
  5414. l->run_x = l->left;
  5415. l->line_y = l->y;
  5416. l->line_h = 0;
  5417. l->nruns = 0;
  5418. l->space_pending = 0;
  5419. l->align = cs.align;
  5420. layout_block(l, n, &cs, depth + 1, lk);
  5421. if (l->y > row_bottom)
  5422. row_bottom = l->y;
  5423. n = nd->next;
  5424. }
  5425. l->left = row_left;
  5426. l->right = row_right;
  5427. l->y = row_bottom;
  5428. l->line_x = l->left;
  5429. l->run_x = l->left;
  5430. l->line_y = l->y;
  5431. l->line_h = 0;
  5432. l->nruns = 0;
  5433. l->space_pending = 0;
  5434. }
  5435. /* repeat(auto-fit, minmax(<min>, 1fr)): pick as many equal columns as fit,
  5436. * capped at four. Used by the project docs' card grids. */
  5437. static void layout_grid_auto(struct lay *l, unsigned int parent,
  5438. const struct br_style *pst, int depth,
  5439. unsigned short link)
  5440. {
  5441. unsigned int n, c;
  5442. int row_left = l->left, row_right = l->right;
  5443. int avail = row_right - row_left;
  5444. int gap = pst->grid_gap;
  5445. int minw = pst->width_kind == 1 ? pst->width : 200;
  5446. int ncols, colw, x, y = l->y, start_y = l->y, row_h = 0, col = 0;
  5447. if (avail < 32 || depth > 24) {
  5448. layout_children(l, parent, pst, depth, link);
  5449. return;
  5450. }
  5451. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  5452. struct br_node *nd = &doc.nodes[c];
  5453. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  5454. layout_children(l, parent, pst, depth, link);
  5455. return;
  5456. }
  5457. }
  5458. if (minw < 80)
  5459. minw = 80;
  5460. ncols = (avail + gap) / (minw + gap);
  5461. if (ncols < 1)
  5462. ncols = 1;
  5463. if (ncols > 4)
  5464. ncols = 4;
  5465. colw = (avail - gap * (ncols - 1)) / ncols;
  5466. if (colw < 24)
  5467. colw = 24;
  5468. x = row_left;
  5469. n = doc.nodes[parent].first;
  5470. while (n) {
  5471. struct br_node *nd = &doc.nodes[n];
  5472. struct br_style cs;
  5473. unsigned short lk = link;
  5474. int h;
  5475. if (nd->type == BR_NODE_TEXT) {
  5476. n = nd->next;
  5477. continue;
  5478. }
  5479. br_style_for(&doc, n, pst, &cs);
  5480. if (cs.display == BR_DISPLAY_NONE) {
  5481. n = nd->next;
  5482. continue;
  5483. }
  5484. if (nd->tag == BR_TAG_A && nd->href_off)
  5485. lk = (unsigned short)n;
  5486. cs.width_kind = 0;
  5487. cs.max_width_kind = 0;
  5488. l->left = x;
  5489. l->right = x + colw;
  5490. if (l->right < l->left + 8)
  5491. l->right = l->left + 8;
  5492. l->y = y;
  5493. l->line_x = l->left;
  5494. l->run_x = l->left;
  5495. l->line_y = l->y;
  5496. l->line_h = 0;
  5497. l->nruns = 0;
  5498. l->space_pending = 0;
  5499. l->align = cs.align;
  5500. layout_block(l, n, &cs, depth + 1, lk);
  5501. h = l->y - y;
  5502. if (h > row_h)
  5503. row_h = h;
  5504. col++;
  5505. n = nd->next;
  5506. if (col == ncols) {
  5507. y += row_h + gap;
  5508. x = row_left;
  5509. row_h = 0;
  5510. col = 0;
  5511. } else {
  5512. x += colw + gap;
  5513. }
  5514. }
  5515. if (col)
  5516. y += row_h;
  5517. else if (y > start_y)
  5518. y -= gap;
  5519. l->left = row_left;
  5520. l->right = row_right;
  5521. l->y = y;
  5522. l->line_x = l->left;
  5523. l->run_x = l->left;
  5524. l->line_y = l->y;
  5525. l->line_h = 0;
  5526. l->nruns = 0;
  5527. l->space_pending = 0;
  5528. }
  5529. /* Column widths are the widest cell in each column across every row, so all
  5530. * rows line up. The table model is still deliberately basic: no colspan,
  5531. * border-collapse or percentage columns. */
  5532. static void table_columns(struct lay *l, unsigned int table,
  5533. const struct br_style *pst, int avail,
  5534. int *raw, int maxcols, int *out_cols,
  5535. int *out_total)
  5536. {
  5537. unsigned int r, c;
  5538. int cols = 0, total = 0, fixed_sum = 0, free_raw = 0, free_cols = 0;
  5539. int fixed[12];
  5540. for (c = 0; c < (unsigned int)maxcols; c++) {
  5541. raw[c] = 0;
  5542. fixed[c] = 0;
  5543. }
  5544. /* Intrinsic widths of single-column cells. */
  5545. for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
  5546. struct br_node *rn = &doc.nodes[r];
  5547. int col = 0;
  5548. if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
  5549. continue;
  5550. for (c = rn->first; c; c = doc.nodes[c].next) {
  5551. struct br_node *cn = &doc.nodes[c];
  5552. struct br_style cs;
  5553. int w, span;
  5554. if (cn->type != BR_NODE_ELEM ||
  5555. (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
  5556. continue;
  5557. span = cn->colspan ? cn->colspan : 1;
  5558. br_style_for(&doc, c, pst, &cs);
  5559. if (cs.display == BR_DISPLAY_NONE)
  5560. continue;
  5561. if (col + span > maxcols)
  5562. span = maxcols - col;
  5563. if (span < 1)
  5564. break;
  5565. w = intrinsic_width(l->ui, c, &cs, 0, avail) +
  5566. mp_px(&cs, 7, avail) +
  5567. mp_px(&cs, 5, avail) +
  5568. mp_px(&cs, 3, avail) +
  5569. mp_px(&cs, 1, avail) +
  5570. cs.border_w[0] + cs.border_w[1] +
  5571. cs.border_w[2] + cs.border_w[3];
  5572. if (w < 24)
  5573. w = 24;
  5574. if (span == 1) {
  5575. if (w > raw[col])
  5576. raw[col] = w;
  5577. }
  5578. if (col + span > cols)
  5579. cols = col + span;
  5580. col += span;
  5581. }
  5582. }
  5583. /* A colspan cell only has to be as wide as the sum of its columns. */
  5584. for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
  5585. struct br_node *rn = &doc.nodes[r];
  5586. int col = 0;
  5587. if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
  5588. continue;
  5589. for (c = rn->first; c; c = doc.nodes[c].next) {
  5590. struct br_node *cn = &doc.nodes[c];
  5591. struct br_style cs;
  5592. int span, w, sum, i;
  5593. if (cn->type != BR_NODE_ELEM ||
  5594. (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
  5595. continue;
  5596. span = cn->colspan ? cn->colspan : 1;
  5597. if (col + span > maxcols)
  5598. span = maxcols - col;
  5599. if (span < 1)
  5600. break;
  5601. if (span > 1) {
  5602. br_style_for(&doc, c, pst, &cs);
  5603. if (cs.display != BR_DISPLAY_NONE) {
  5604. w = intrinsic_width(l->ui, c, &cs,
  5605. 0, avail) +
  5606. mp_px(&cs, 7, avail) +
  5607. mp_px(&cs, 5, avail) +
  5608. mp_px(&cs, 3, avail) +
  5609. mp_px(&cs, 1, avail) +
  5610. cs.border_w[0] + cs.border_w[1] +
  5611. cs.border_w[2] + cs.border_w[3];
  5612. sum = 0;
  5613. for (i = 0; i < span; i++)
  5614. sum += raw[col + i];
  5615. if (w > sum)
  5616. raw[col + span - 1] += w - sum;
  5617. }
  5618. }
  5619. col += span;
  5620. }
  5621. }
  5622. /* Explicit and percentage widths override intrinsic widths. */
  5623. for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
  5624. struct br_node *rn = &doc.nodes[r];
  5625. int col = 0;
  5626. if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
  5627. continue;
  5628. for (c = rn->first; c; c = doc.nodes[c].next) {
  5629. struct br_node *cn = &doc.nodes[c];
  5630. struct br_style cs;
  5631. int span;
  5632. if (cn->type != BR_NODE_ELEM ||
  5633. (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
  5634. continue;
  5635. span = cn->colspan ? cn->colspan : 1;
  5636. if (col + span > maxcols)
  5637. span = maxcols - col;
  5638. if (span < 1)
  5639. break;
  5640. if (span == 1) {
  5641. br_style_for(&doc, c, pst, &cs);
  5642. if (cs.display != BR_DISPLAY_NONE) {
  5643. if (cs.width_kind == 1) {
  5644. raw[col] = cs.width;
  5645. fixed[col] = 1;
  5646. } else if (cs.width_kind == 2 ||
  5647. cs.width_kind == 3) {
  5648. raw[col] = style_width_px(&cs,
  5649. avail);
  5650. fixed[col] = 1;
  5651. }
  5652. }
  5653. }
  5654. col += span;
  5655. }
  5656. }
  5657. for (c = 0; c < (unsigned int)cols; c++) {
  5658. if (raw[c] < 8)
  5659. raw[c] = 8;
  5660. total += raw[c];
  5661. if (fixed[c])
  5662. fixed_sum += raw[c];
  5663. else {
  5664. free_raw += raw[c];
  5665. free_cols++;
  5666. }
  5667. }
  5668. if (fixed_sum > 0 && fixed_sum < avail) {
  5669. int free_avail = avail - fixed_sum;
  5670. int i;
  5671. if (free_raw > 0) {
  5672. for (i = 0; i < cols; i++)
  5673. if (!fixed[i])
  5674. raw[i] = free_avail * raw[i] /
  5675. free_raw;
  5676. } else if (free_cols > 0) {
  5677. for (i = 0; i < cols; i++)
  5678. if (!fixed[i])
  5679. raw[i] = free_avail / free_cols;
  5680. }
  5681. total = 0;
  5682. for (i = 0; i < cols; i++)
  5683. total += raw[i];
  5684. } else if (total < avail && total > 0) {
  5685. int i;
  5686. for (i = 0; i < cols; i++)
  5687. raw[i] = raw[i] * avail / total;
  5688. total = 0;
  5689. for (i = 0; i < cols; i++)
  5690. total += raw[i];
  5691. }
  5692. *out_cols = cols;
  5693. *out_total = total;
  5694. }
  5695. static void layout_table_row(struct lay *l, unsigned int row,
  5696. const struct br_style *pst, int depth,
  5697. unsigned short link)
  5698. {
  5699. unsigned int n, cell[12];
  5700. int raw[12], colstart[12], colspan[12];
  5701. int prefix[13];
  5702. int ncell = 0, total = 0, cols = 0, col = 0;
  5703. int row_left = l->left, row_right = l->right;
  5704. int avail = row_right - row_left;
  5705. int y = l->y, bottom = l->y;
  5706. for (n = doc.nodes[row].first; n; n = doc.nodes[n].next) {
  5707. struct br_node *nd = &doc.nodes[n];
  5708. struct br_style cs;
  5709. int span;
  5710. if (nd->type == BR_NODE_TEXT) {
  5711. if (text_node_blank(nd))
  5712. continue;
  5713. layout_children(l, row, pst, depth, link);
  5714. return;
  5715. }
  5716. if (nd->tag != BR_TAG_TD && nd->tag != BR_TAG_TH) {
  5717. layout_children(l, row, pst, depth, link);
  5718. return;
  5719. }
  5720. br_style_for(&doc, n, pst, &cs);
  5721. if (cs.display == BR_DISPLAY_NONE)
  5722. continue;
  5723. if (ncell >= 12) {
  5724. layout_children(l, row, pst, depth, link);
  5725. return;
  5726. }
  5727. span = nd->colspan ? nd->colspan : 1;
  5728. if (span > 12 - col)
  5729. span = 12 - col;
  5730. if (span < 1)
  5731. continue;
  5732. cell[ncell] = n;
  5733. colstart[ncell] = col;
  5734. colspan[ncell] = span;
  5735. col += span;
  5736. ncell++;
  5737. }
  5738. if (ncell < 1 || avail < 32) {
  5739. layout_children(l, row, pst, depth, link);
  5740. return;
  5741. }
  5742. table_columns(l, doc.nodes[row].parent, pst, avail, raw, 12, &cols,
  5743. &total);
  5744. if (cols < 2 || total <= 0) {
  5745. int c;
  5746. for (c = 0; c < ncell; c++) {
  5747. struct br_style cs;
  5748. br_style_for(&doc, cell[c], pst, &cs);
  5749. raw[c] = intrinsic_width(l->ui, cell[c], &cs, 0,
  5750. avail) +
  5751. mp_px(&cs, 7, avail) +
  5752. mp_px(&cs, 5, avail) +
  5753. mp_px(&cs, 3, avail) +
  5754. mp_px(&cs, 1, avail);
  5755. if (raw[c] < 24)
  5756. raw[c] = 24;
  5757. total += raw[c];
  5758. }
  5759. }
  5760. prefix[0] = row_left;
  5761. for (n = 0; n < (unsigned int)cols && n < 12; n++)
  5762. prefix[n + 1] = prefix[n] + raw[n];
  5763. for (n = 0; n < (unsigned int)ncell; n++) {
  5764. struct br_style cs;
  5765. int start = colstart[n];
  5766. int end = start + colspan[n];
  5767. int w;
  5768. if (end > cols)
  5769. end = cols;
  5770. if (start >= cols)
  5771. continue;
  5772. w = prefix[end] - prefix[start];
  5773. br_style_for(&doc, cell[n], pst, &cs);
  5774. /* The column width already reflects the cell's width property. */
  5775. cs.width_kind = 0;
  5776. cs.max_width_kind = 0;
  5777. if (w < 24)
  5778. w = 24;
  5779. if (w < 8)
  5780. w = 8;
  5781. l->left = prefix[start];
  5782. l->right = l->left + w;
  5783. l->y = y;
  5784. l->line_x = l->left;
  5785. l->run_x = l->left;
  5786. l->line_y = l->y;
  5787. l->line_h = 0;
  5788. l->nruns = 0;
  5789. l->space_pending = 0;
  5790. l->align = cs.align;
  5791. layout_block(l, cell[n], &cs, depth + 1, link);
  5792. if (l->y > bottom)
  5793. bottom = l->y;
  5794. }
  5795. l->left = row_left;
  5796. l->right = row_right;
  5797. l->y = bottom;
  5798. l->line_x = l->left;
  5799. l->run_x = l->left;
  5800. l->line_y = l->y;
  5801. l->line_h = 0;
  5802. l->nruns = 0;
  5803. l->space_pending = 0;
  5804. }
  5805. #define TBL_MAX_ROWS 32
  5806. #define TBL_MAX_COLS 12
  5807. #define TBL_MAX_CELLS 128
  5808. struct tbl_cell {
  5809. unsigned int node;
  5810. unsigned char row, col, rowspan, colspan;
  5811. int height;
  5812. };
  5813. static struct tbl_cell tbl_cells[TBL_MAX_CELLS];
  5814. static unsigned short tbl_at[TBL_MAX_ROWS][TBL_MAX_COLS];
  5815. static int table_active;
  5816. static int table_has_rowspan(unsigned int table)
  5817. {
  5818. unsigned int r, c;
  5819. for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
  5820. struct br_node *rn = &doc.nodes[r];
  5821. if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
  5822. continue;
  5823. for (c = rn->first; c; c = doc.nodes[c].next) {
  5824. struct br_node *cn = &doc.nodes[c];
  5825. if (cn->type == BR_NODE_ELEM &&
  5826. (cn->tag == BR_TAG_TD || cn->tag == BR_TAG_TH) &&
  5827. cn->rowspan > 1)
  5828. return 1;
  5829. }
  5830. }
  5831. return 0;
  5832. }
  5833. static void table_cell_style(unsigned int row, unsigned int cell,
  5834. const struct br_style *st, struct br_style *out)
  5835. {
  5836. struct br_style trs;
  5837. br_style_for(&doc, row, st, &trs);
  5838. br_style_for(&doc, cell, &trs, out);
  5839. }
  5840. /* Tables that use rowspan need row heights before anything is painted. This
  5841. * path builds a small occupancy grid, measures the natural cell heights, then
  5842. * lays every cell out at its row/column position. Tables without rowspan
  5843. * keep the lighter per-row path in layout_table_row(). */
  5844. static void layout_table(struct lay *l, unsigned int table,
  5845. const struct br_style *st, int depth,
  5846. unsigned short link)
  5847. {
  5848. unsigned int rows[TBL_MAX_ROWS];
  5849. int rowh[TBL_MAX_ROWS], rowy[TBL_MAX_ROWS];
  5850. int raw[TBL_MAX_COLS], prefix[TBL_MAX_COLS + 1], fixed[TBL_MAX_COLS];
  5851. int nrows = 0, ncells = 0, cols = 0, total = 0, fixed_sum = 0;
  5852. int free_raw = 0, free_cols = 0;
  5853. int table_left = l->left, table_right = l->right;
  5854. int top = l->y, avail = table_right - table_left;
  5855. unsigned int r, c;
  5856. int i, rr, cc;
  5857. if (depth > 24 || table_active) {
  5858. layout_children(l, table, st, depth, link);
  5859. return;
  5860. }
  5861. table_active = 1;
  5862. for (r = doc.nodes[table].first;
  5863. r && nrows < TBL_MAX_ROWS; r = doc.nodes[r].next) {
  5864. struct br_node *rn = &doc.nodes[r];
  5865. if (rn->type == BR_NODE_ELEM && rn->tag == BR_TAG_TR)
  5866. rows[nrows++] = r;
  5867. }
  5868. if (!nrows) {
  5869. table_active = 0;
  5870. layout_children(l, table, st, depth, link);
  5871. return;
  5872. }
  5873. for (rr = 0; rr < nrows; rr++)
  5874. for (cc = 0; cc < TBL_MAX_COLS; cc++)
  5875. tbl_at[rr][cc] = 0;
  5876. for (rr = 0; rr < nrows; rr++) {
  5877. int col = 0;
  5878. for (c = doc.nodes[rows[rr]].first; c;
  5879. c = doc.nodes[c].next) {
  5880. struct br_node *cn = &doc.nodes[c];
  5881. struct br_style cs;
  5882. int rs, csn;
  5883. if (cn->type != BR_NODE_ELEM ||
  5884. (cn->tag != BR_TAG_TD &&
  5885. cn->tag != BR_TAG_TH))
  5886. continue;
  5887. table_cell_style(rows[rr], c, st, &cs);
  5888. if (cs.display == BR_DISPLAY_NONE)
  5889. continue;
  5890. while (col < TBL_MAX_COLS && tbl_at[rr][col])
  5891. col++;
  5892. if (col >= TBL_MAX_COLS)
  5893. break;
  5894. rs = cn->rowspan ? cn->rowspan : 1;
  5895. csn = cn->colspan ? cn->colspan : 1;
  5896. if (rr + rs > nrows)
  5897. rs = nrows - rr;
  5898. if (col + csn > TBL_MAX_COLS)
  5899. csn = TBL_MAX_COLS - col;
  5900. if (rs < 1 || csn < 1)
  5901. continue;
  5902. if (ncells >= TBL_MAX_CELLS)
  5903. break;
  5904. tbl_cells[ncells].node = c;
  5905. tbl_cells[ncells].row = (unsigned char)rr;
  5906. tbl_cells[ncells].col = (unsigned char)col;
  5907. tbl_cells[ncells].rowspan = (unsigned char)rs;
  5908. tbl_cells[ncells].colspan = (unsigned char)csn;
  5909. tbl_cells[ncells].height = 0;
  5910. for (i = rr; i < rr + rs; i++)
  5911. for (int j = col; j < col + csn; j++)
  5912. tbl_at[i][j] =
  5913. (unsigned short)(ncells + 1);
  5914. ncells++;
  5915. col += csn;
  5916. if (col > cols)
  5917. cols = col;
  5918. }
  5919. }
  5920. if (!ncells || cols < 1) {
  5921. table_active = 0;
  5922. layout_children(l, table, st, depth, link);
  5923. return;
  5924. }
  5925. for (i = 0; i < cols; i++) {
  5926. raw[i] = 0;
  5927. fixed[i] = 0;
  5928. }
  5929. /* Intrinsic widths of single-column cells. */
  5930. for (i = 0; i < ncells; i++) {
  5931. struct br_style cs;
  5932. int w;
  5933. table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
  5934. st, &cs);
  5935. w = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0, avail) +
  5936. mp_px(&cs, 7, avail) +
  5937. mp_px(&cs, 5, avail) +
  5938. mp_px(&cs, 3, avail) +
  5939. mp_px(&cs, 1, avail) +
  5940. cs.border_w[0] + cs.border_w[1] +
  5941. cs.border_w[2] + cs.border_w[3];
  5942. if (w < 24)
  5943. w = 24;
  5944. if (tbl_cells[i].colspan == 1) {
  5945. int col = tbl_cells[i].col;
  5946. if (w > raw[col])
  5947. raw[col] = w;
  5948. }
  5949. }
  5950. /* A spanning cell only needs the sum of its columns to be large
  5951. * enough; put the deficit in the last column. */
  5952. for (i = 0; i < ncells; i++) {
  5953. struct br_style cs;
  5954. int w, sum, col, span;
  5955. if (tbl_cells[i].colspan < 2)
  5956. continue;
  5957. table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
  5958. st, &cs);
  5959. w = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0, avail) +
  5960. mp_px(&cs, 7, avail) +
  5961. mp_px(&cs, 5, avail) +
  5962. mp_px(&cs, 3, avail) +
  5963. mp_px(&cs, 1, avail) +
  5964. cs.border_w[0] + cs.border_w[1] +
  5965. cs.border_w[2] + cs.border_w[3];
  5966. col = tbl_cells[i].col;
  5967. span = tbl_cells[i].colspan;
  5968. sum = 0;
  5969. for (int j = 0; j < span; j++)
  5970. sum += raw[col + j];
  5971. if (w > sum)
  5972. raw[col + span - 1] += w - sum;
  5973. }
  5974. /* Explicit/percentage widths, as in table_columns(). */
  5975. for (i = 0; i < ncells; i++) {
  5976. struct br_style cs;
  5977. int col;
  5978. if (tbl_cells[i].colspan != 1)
  5979. continue;
  5980. table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
  5981. st, &cs);
  5982. col = tbl_cells[i].col;
  5983. if (cs.width_kind == 1) {
  5984. raw[col] = cs.width;
  5985. fixed[col] = 1;
  5986. } else if (cs.width_kind == 2 || cs.width_kind == 3) {
  5987. raw[col] = style_width_px(&cs, avail);
  5988. fixed[col] = 1;
  5989. }
  5990. }
  5991. for (i = 0; i < cols; i++) {
  5992. if (raw[i] < 8)
  5993. raw[i] = 8;
  5994. total += raw[i];
  5995. if (fixed[i])
  5996. fixed_sum += raw[i];
  5997. else {
  5998. free_raw += raw[i];
  5999. free_cols++;
  6000. }
  6001. }
  6002. if (fixed_sum > 0 && fixed_sum < avail) {
  6003. int free_avail = avail - fixed_sum;
  6004. if (free_raw > 0) {
  6005. for (i = 0; i < cols; i++)
  6006. if (!fixed[i])
  6007. raw[i] = free_avail * raw[i] /
  6008. free_raw;
  6009. } else if (free_cols > 0) {
  6010. for (i = 0; i < cols; i++)
  6011. if (!fixed[i])
  6012. raw[i] = free_avail / free_cols;
  6013. }
  6014. } else if (total < avail && total > 0) {
  6015. for (i = 0; i < cols; i++)
  6016. raw[i] = raw[i] * avail / total;
  6017. }
  6018. prefix[0] = table_left;
  6019. for (i = 0; i < cols; i++)
  6020. prefix[i + 1] = prefix[i] + raw[i];
  6021. /* Measure natural cell heights with the column widths. */
  6022. for (i = 0; i < ncells; i++) {
  6023. struct br_style cs;
  6024. int col = tbl_cells[i].col;
  6025. int span = tbl_cells[i].colspan;
  6026. int w = prefix[col + span] - prefix[col];
  6027. int sl = l->left, sr = l->right, sy = l->y;
  6028. int slx = l->line_x, srx = l->run_x, sly = l->line_y;
  6029. int slh = l->line_h, snr = l->nruns, ssp = l->space_pending;
  6030. int sal = l->align, sdraw = l->draw;
  6031. struct ngui *sui = l->ui;
  6032. if (w < 8)
  6033. w = 8;
  6034. table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
  6035. st, &cs);
  6036. cs.width_kind = 0;
  6037. cs.max_width_kind = 0;
  6038. l->draw = 0;
  6039. l->ui = 0;
  6040. l->left = prefix[col];
  6041. l->right = l->left + w;
  6042. l->y = 0;
  6043. l->line_x = l->left;
  6044. l->run_x = l->left;
  6045. l->line_y = 0;
  6046. l->line_h = 0;
  6047. l->nruns = 0;
  6048. l->space_pending = 0;
  6049. l->align = cs.align;
  6050. layout_block(l, tbl_cells[i].node, &cs, depth + 1, link);
  6051. tbl_cells[i].height = block_h[tbl_cells[i].node];
  6052. l->left = sl;
  6053. l->right = sr;
  6054. l->y = sy;
  6055. l->line_x = slx;
  6056. l->run_x = srx;
  6057. l->line_y = sly;
  6058. l->line_h = slh;
  6059. l->nruns = snr;
  6060. l->space_pending = ssp;
  6061. l->align = sal;
  6062. l->draw = sdraw;
  6063. l->ui = sui;
  6064. }
  6065. for (i = 0; i < nrows; i++)
  6066. rowh[i] = 0;
  6067. for (i = 0; i < ncells; i++) {
  6068. int row = tbl_cells[i].row;
  6069. if (tbl_cells[i].rowspan == 1) {
  6070. if (tbl_cells[i].height > rowh[row])
  6071. rowh[row] = tbl_cells[i].height;
  6072. }
  6073. }
  6074. for (i = 0; i < ncells; i++) {
  6075. int row = tbl_cells[i].row;
  6076. int span = tbl_cells[i].rowspan;
  6077. int sum = 0;
  6078. if (span < 2)
  6079. continue;
  6080. for (int j = 0; j < span; j++)
  6081. sum += rowh[row + j];
  6082. if (sum < tbl_cells[i].height)
  6083. rowh[row + span - 1] +=
  6084. tbl_cells[i].height - sum;
  6085. }
  6086. for (i = 0; i < nrows; i++)
  6087. if (rowh[i] < 8)
  6088. rowh[i] = 8;
  6089. rowy[0] = top;
  6090. for (i = 1; i < nrows; i++)
  6091. rowy[i] = rowy[i - 1] + rowh[i - 1];
  6092. if (!l->draw) {
  6093. l->y = rowy[nrows - 1] + rowh[nrows - 1];
  6094. table_active = 0;
  6095. return;
  6096. }
  6097. for (i = 0; i < ncells; i++) {
  6098. struct br_style cs;
  6099. int col = tbl_cells[i].col;
  6100. int span = tbl_cells[i].colspan;
  6101. int row = tbl_cells[i].row;
  6102. int rspan = tbl_cells[i].rowspan;
  6103. int w = prefix[col + span] - prefix[col];
  6104. int h = 0;
  6105. int sl = l->left, sr = l->right, sy = l->y;
  6106. int slx = l->line_x, srx = l->run_x, sly = l->line_y;
  6107. int slh = l->line_h, snr = l->nruns, ssp = l->space_pending;
  6108. int sal = l->align;
  6109. int deco;
  6110. for (int j = 0; j < rspan; j++)
  6111. h += rowh[row + j];
  6112. table_cell_style(rows[row], tbl_cells[i].node, st, &cs);
  6113. cs.width_kind = 0;
  6114. cs.max_width_kind = 0;
  6115. cs.box_sizing = 0;
  6116. deco = mp_px(&cs, 4, avail) +
  6117. mp_px(&cs, 6, avail) +
  6118. cs.border_w[0] + cs.border_w[2];
  6119. cs.height_kind = 1;
  6120. cs.height = h > deco ? h - deco : 0;
  6121. /* The block painter reads block_h[] from the measure pass; make
  6122. * the table cell fill its row/rowspan, not just its content. */
  6123. if (h > 0 && h < 65536)
  6124. block_h[tbl_cells[i].node] = (unsigned short)h;
  6125. l->left = prefix[col];
  6126. l->right = l->left + w;
  6127. l->y = rowy[row];
  6128. l->line_x = l->left;
  6129. l->run_x = l->left;
  6130. l->line_y = l->y;
  6131. l->line_h = 0;
  6132. l->nruns = 0;
  6133. l->space_pending = 0;
  6134. l->align = cs.align;
  6135. layout_block(l, tbl_cells[i].node, &cs, depth + 1, link);
  6136. l->left = sl;
  6137. l->right = sr;
  6138. l->y = sy;
  6139. l->line_x = slx;
  6140. l->run_x = srx;
  6141. l->line_y = sly;
  6142. l->line_h = slh;
  6143. l->nruns = snr;
  6144. l->space_pending = ssp;
  6145. l->align = sal;
  6146. }
  6147. l->y = rowy[nrows - 1] + rowh[nrows - 1];
  6148. table_active = 0;
  6149. }
  6150. /* Positioned elements (fixed headers, overlays) are drawn after the normal
  6151. * flow so they sit above it, like CSS painting order. Their flow origin is
  6152. * saved for absolute boxes; fixed boxes only need the viewport rect. */
  6153. struct br_deferred {
  6154. unsigned int node;
  6155. int flow_y, flow_left, flow_right;
  6156. unsigned short link;
  6157. signed short z_index;
  6158. unsigned char z_set;
  6159. unsigned int order;
  6160. };
  6161. static struct br_deferred deferred[16];
  6162. static int ndeferred;
  6163. static void layout_children(struct lay *l, unsigned int parent,
  6164. const struct br_style *pst, int depth,
  6165. unsigned short link)
  6166. {
  6167. unsigned int n;
  6168. if (depth > 24)
  6169. return;
  6170. n = doc.nodes[parent].first;
  6171. while (n) {
  6172. struct br_node *nd = &doc.nodes[n];
  6173. struct br_style cs;
  6174. unsigned short lk = link;
  6175. if (nd->type == BR_NODE_TEXT) {
  6176. emit_text(l, doc.text + nd->text_off, nd->text_len, pst,
  6177. link, (nd->flags & BR_NODE_PREFORMAT) != 0);
  6178. n = nd->next;
  6179. continue;
  6180. }
  6181. br_style_for(&doc, n, pst, &cs);
  6182. if (cs.display == BR_DISPLAY_NONE) {
  6183. n = nd->next;
  6184. continue;
  6185. }
  6186. if (cs.clear && l->float_active) {
  6187. flush_line(l);
  6188. if (l->y < l->float_bottom)
  6189. l->y = l->float_bottom;
  6190. l->float_active = 0;
  6191. l->line_x = l->left;
  6192. l->run_x = l->left;
  6193. l->line_left = l->left;
  6194. l->line_right = l->right;
  6195. l->line_y = l->y;
  6196. l->line_h = 0;
  6197. l->nruns = 0;
  6198. l->space_pending = 0;
  6199. }
  6200. if (nd->tag == BR_TAG_A && nd->href_off)
  6201. lk = (unsigned short)n;
  6202. if (cs.pos == 1 || cs.pos == 2 ||
  6203. (cs.pos == 3 && cs.z_set)) {
  6204. if (cs.pos == 3 && l->draw && ndeferred < 16) {
  6205. /* Relative z-index: measure and advance the flow
  6206. * without painting, then paint it in the
  6207. * deferred (stacked) pass. */
  6208. int fy = l->y, fl = l->left, fr = l->right;
  6209. int save_draw = l->draw;
  6210. l->draw = 0;
  6211. layout_block(l, n, &cs, depth + 1, lk);
  6212. l->draw = save_draw;
  6213. deferred[ndeferred].node = n;
  6214. deferred[ndeferred].flow_y = fy;
  6215. deferred[ndeferred].flow_left = fl;
  6216. deferred[ndeferred].flow_right = fr;
  6217. deferred[ndeferred].link = lk;
  6218. deferred[ndeferred].z_index = cs.z_index;
  6219. deferred[ndeferred].z_set = cs.z_set;
  6220. deferred[ndeferred].order = (unsigned int)ndeferred;
  6221. ndeferred++;
  6222. } else if (l->draw && ndeferred < 16) {
  6223. deferred[ndeferred].node = n;
  6224. deferred[ndeferred].flow_y = l->y;
  6225. deferred[ndeferred].flow_left = l->left;
  6226. deferred[ndeferred].flow_right = l->right;
  6227. deferred[ndeferred].link = lk;
  6228. deferred[ndeferred].z_index = cs.z_index;
  6229. deferred[ndeferred].z_set = cs.z_set;
  6230. deferred[ndeferred].order = (unsigned int)ndeferred;
  6231. ndeferred++;
  6232. } else {
  6233. layout_block(l, n, &cs, depth + 1, lk);
  6234. }
  6235. if (cs.pos == 1 && block_h[n] &&
  6236. doc.nodes[n].class_off) {
  6237. const char *cl = doc.attrs +
  6238. doc.nodes[n].class_off;
  6239. if ((contains_ci(cl, "poster-wrapper") ||
  6240. contains_ci(cl, "ts-picture")) &&
  6241. !has_skeleton_sibling(n))
  6242. l->y += block_h[n];
  6243. }
  6244. n = nd->next;
  6245. continue;
  6246. }
  6247. if (nd->tag == BR_TAG_BR) {
  6248. flush_line(l);
  6249. n = nd->next;
  6250. continue;
  6251. }
  6252. if (nd->tag == BR_TAG_HR) {
  6253. flush_line(l);
  6254. if (l->draw)
  6255. ngui_fill_rect(l->ui,
  6256. (struct ngui_rect){l->left, l->y + 2,
  6257. l->right - l->left, 1}, BR_GRAY);
  6258. l->y += 8;
  6259. n = nd->next;
  6260. continue;
  6261. }
  6262. if (nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_TEXTAREA ||
  6263. nd->tag == BR_TAG_SELECT || nd->tag == BR_TAG_BUTTON) {
  6264. if (!(nd->tag == BR_TAG_INPUT &&
  6265. s_eq(br_type(&doc, n), "hidden")))
  6266. add_form_run(l, n, &cs, lk);
  6267. n = nd->next;
  6268. continue;
  6269. }
  6270. if (nd->tag == BR_TAG_IMG) {
  6271. add_image_run(l, n, &cs, lk);
  6272. n = nd->next;
  6273. continue;
  6274. }
  6275. if (nd->tag == BR_TAG_VIDEO) {
  6276. layout_block(l, n, &cs, depth + 1, lk);
  6277. n = nd->next;
  6278. continue;
  6279. }
  6280. if (cs.float_left) {
  6281. n = layout_float_group(l, n, pst, depth, lk);
  6282. continue;
  6283. }
  6284. if (cs.display == BR_DISPLAY_INLINE_BLOCK) {
  6285. n = layout_inline_blocks(l, n, pst, depth, link);
  6286. continue;
  6287. }
  6288. if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag))
  6289. layout_block(l, n, &cs, depth + 1, lk);
  6290. else {
  6291. layout_children(l, n, &cs, depth + 1, lk);
  6292. if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
  6293. l->space_pending = 1;
  6294. }
  6295. n = nd->next;
  6296. }
  6297. }
  6298. /* One layout state, kept out of the 8 KB user stack: it holds 96 line
  6299. * runs and a recursive page can otherwise overflow the stack. */
  6300. static struct lay layout_state;
  6301. static int measure_page(int left, int right)
  6302. {
  6303. struct lay *l = &layout_state;
  6304. struct br_style base = base_style();
  6305. unsigned int i;
  6306. for (i = 0; i < BR_MAX_NODES; i++) {
  6307. block_h[i] = 0;
  6308. img_top[i] = 0;
  6309. img_h[i] = 0;
  6310. layout_boxes[i].valid = 0;
  6311. }
  6312. l->draw = 0;
  6313. l->ui = 0;
  6314. l->left = left;
  6315. l->right = right;
  6316. l->y = 0;
  6317. l->line_x = left;
  6318. l->run_x = left;
  6319. l->line_left = left;
  6320. l->line_right = right;
  6321. l->line_y = 0;
  6322. l->line_h = 0;
  6323. l->align = BR_ALIGN_LEFT;
  6324. l->float_active = 0;
  6325. l->parent_top = 0;
  6326. l->depth = 0;
  6327. l->nruns = 0;
  6328. l->space_pending = 0;
  6329. layout_children(l, doc.root, &base, 0, 0);
  6330. return l->y + 8;
  6331. }
  6332. static int deferred_cmp(const struct br_deferred *a,
  6333. const struct br_deferred *b)
  6334. {
  6335. int az = a->z_set ? a->z_index : 0;
  6336. int bz = b->z_set ? b->z_index : 0;
  6337. if (az != bz)
  6338. return az < bz ? -1 : 1;
  6339. if (a->order != b->order)
  6340. return a->order < b->order ? -1 : 1;
  6341. return 0;
  6342. }
  6343. static void sort_deferred(void)
  6344. {
  6345. int i;
  6346. for (i = 1; i < ndeferred; i++) {
  6347. struct br_deferred key = deferred[i];
  6348. int j = i;
  6349. while (j > 0 && deferred_cmp(&deferred[j - 1], &key) > 0) {
  6350. deferred[j] = deferred[j - 1];
  6351. j--;
  6352. }
  6353. deferred[j] = key;
  6354. }
  6355. }
  6356. static void render_page(int left, int right, int y0)
  6357. {
  6358. struct lay *l = &layout_state;
  6359. struct br_style base = base_style();
  6360. l->draw = 1;
  6361. l->ui = &ui;
  6362. l->left = left;
  6363. l->right = right;
  6364. l->y = y0;
  6365. l->line_x = left;
  6366. l->run_x = left;
  6367. l->line_left = left;
  6368. l->line_right = right;
  6369. l->line_y = y0;
  6370. l->line_h = 0;
  6371. l->align = BR_ALIGN_LEFT;
  6372. l->float_active = 0;
  6373. l->parent_top = 0;
  6374. l->depth = 0;
  6375. l->nruns = 0;
  6376. l->space_pending = 0;
  6377. nhits = 0;
  6378. ndeferred = 0;
  6379. layout_children(l, doc.root, &base, 0, 0);
  6380. sort_deferred();
  6381. {
  6382. int i;
  6383. for (i = 0; i < ndeferred; i++) {
  6384. struct br_style st;
  6385. br_style_for(&doc, deferred[i].node, &base, &st);
  6386. if (st.display == BR_DISPLAY_NONE)
  6387. continue;
  6388. l->draw = 1;
  6389. l->left = deferred[i].flow_left;
  6390. l->right = deferred[i].flow_right;
  6391. l->y = deferred[i].flow_y;
  6392. l->line_x = l->left;
  6393. l->run_x = l->left;
  6394. l->line_y = l->y;
  6395. l->line_h = 0;
  6396. l->nruns = 0;
  6397. l->space_pending = 0;
  6398. l->align = st.align;
  6399. if (st.pos == 2 && block_h[deferred[i].node]) {
  6400. struct ngui_rect clip;
  6401. int ch = block_h[deferred[i].node] + 24;
  6402. clip.x = page_rect.x;
  6403. clip.y = page_rect.y;
  6404. clip.w = page_rect.w - 12;
  6405. if (ch > page_rect.h)
  6406. ch = page_rect.h;
  6407. clip.h = ch;
  6408. ngui_push_clip(&ui, clip);
  6409. if (st.has_bg)
  6410. ngui_fill_rect(&ui, clip, st.bg);
  6411. layout_block(l, deferred[i].node, &st, 0,
  6412. deferred[i].link);
  6413. ngui_pop_clip(&ui);
  6414. } else {
  6415. layout_block(l, deferred[i].node, &st, 0,
  6416. deferred[i].link);
  6417. }
  6418. }
  6419. }
  6420. }
  6421. /* ---- UI ---- */
  6422. static int find_hit(int x, int y)
  6423. {
  6424. int i;
  6425. for (i = nhits - 1; i >= 0; i--) {
  6426. struct hit *h = &hits[i];
  6427. if (x >= h->x && y >= h->y && x < h->x + h->w &&
  6428. y < h->y + h->h)
  6429. return (int)h->node;
  6430. }
  6431. return 0;
  6432. }
  6433. static void draw_toolbar(struct ngui_rect r, unsigned int *action)
  6434. {
  6435. int x = r.x + 4, y = r.y + 4, h = 22;
  6436. int compact = r.w < 560;
  6437. int go_w = r.w < 420 ? 32 : 42;
  6438. int in_x, in_w, res;
  6439. ngui_fill_rect(&ui, r, ui.theme.panel);
  6440. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  6441. ui.theme.border);
  6442. if (ngui_icon_button(&ui, ID_BACK, (struct ngui_rect){x, y, 28, h},
  6443. NGUI_ICON_CHEVRON_LEFT))
  6444. *action |= ACT_BACK;
  6445. x += 32;
  6446. if (!compact) {
  6447. if (ngui_icon_button(&ui, ID_FORWARD,
  6448. (struct ngui_rect){x, y, 28, h},
  6449. NGUI_ICON_CHEVRON_RIGHT))
  6450. *action |= ACT_FORWARD;
  6451. x += 32;
  6452. }
  6453. if (ngui_icon_button(&ui, ID_RELOAD, (struct ngui_rect){x, y, 28, h},
  6454. NGUI_ICON_REFRESH))
  6455. *action |= ACT_RELOAD;
  6456. x += 32;
  6457. if (!compact) {
  6458. if (ngui_icon_button(&ui, ID_HOME,
  6459. (struct ngui_rect){x, y, 28, h},
  6460. NGUI_ICON_HOME))
  6461. *action |= ACT_HOME;
  6462. x += 32;
  6463. }
  6464. in_x = x + 2;
  6465. in_w = r.x + r.w - 6 - go_w - 4 - in_x;
  6466. if (in_w < 60)
  6467. in_w = 60;
  6468. res = ngui_text_input(&ui, ID_URL,
  6469. (struct ngui_rect){in_x, y, in_w, h}, &url_in);
  6470. if (res & NGUI_INPUT_SUBMIT)
  6471. *action |= ACT_NAV;
  6472. if (res & NGUI_INPUT_CANCEL)
  6473. ngui_set_focus(&ui, 0);
  6474. if (ngui_button(&ui, ID_GO,
  6475. (struct ngui_rect){r.x + r.w - 4 - go_w, y, go_w, h},
  6476. "Go"))
  6477. *action |= ACT_NAV;
  6478. }
  6479. static int page_hover_node(void)
  6480. {
  6481. if (!ui.inside)
  6482. return 0;
  6483. if (ui.mouse_x < page_rect.x ||
  6484. ui.mouse_x >= page_rect.x + page_rect.w - 12 ||
  6485. ui.mouse_y < page_rect.y ||
  6486. ui.mouse_y >= page_rect.y + page_rect.h)
  6487. return 0;
  6488. return find_hit(ui.mouse_x, ui.mouse_y);
  6489. }
  6490. static void blit_page(struct ngui_rect page, unsigned short *src, int sw)
  6491. {
  6492. int x, y, w = page.w, h = page.h;
  6493. if (!src || w <= 0 || h <= 0)
  6494. return;
  6495. for (y = 0; y < h; y++) {
  6496. unsigned short *s = src + (page.y + y) * sw + page.x;
  6497. unsigned short *d = ui.px + (page.y + y) * ui.w + page.x;
  6498. for (x = 0; x + 1 < w; x += 2) {
  6499. *(unsigned int *)(d + x) = *(unsigned int *)(s + x);
  6500. }
  6501. if (x < w)
  6502. d[x] = s[x];
  6503. }
  6504. }
  6505. static void draw_page(struct ngui_rect page)
  6506. {
  6507. int left, right, ch;
  6508. int old_panel, old_dim;
  6509. unsigned short *saved_px;
  6510. int saved_w, saved_h, use_buf;
  6511. page_rect = page;
  6512. if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&
  6513. page_cache_valid && layout_valid &&
  6514. !ui.pressed && !(ui.buttons & 1) && !ui.released &&
  6515. page_scroll.offset == page_cache_scroll &&
  6516. page_cache_nnode == doc.nnode &&
  6517. page_cache_x == page.x && page_cache_y == page.y &&
  6518. page_cache_w == page.w && page_cache_h == page.h) {
  6519. blit_page(page, page_backbuf, pagebuf_w);
  6520. return;
  6521. }
  6522. ngui_fill_rect(&ui, page, ui.theme.bg);
  6523. if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
  6524. const char *s = page_loading ? "Loading..." : "No page";
  6525. int w = (int)ngui_text_width_ui(&ui, s);
  6526. ngui_text(&ui, page.x + (page.w - w) / 2,
  6527. page.y + page.h / 2 - 8, s, ui.theme.text_dim);
  6528. page_scroll.content_h = 0;
  6529. page_scroll.view_h = page.h;
  6530. page_cache_valid = 0;
  6531. return;
  6532. }
  6533. if (page.w < 40 || page.h < 40) {
  6534. page_cache_valid = 0;
  6535. return;
  6536. }
  6537. saved_px = ui.px;
  6538. saved_w = ui.w;
  6539. saved_h = ui.h;
  6540. use_buf = page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h;
  6541. if (use_buf) {
  6542. ui.px = page_backbuf;
  6543. ui.w = pagebuf_w;
  6544. ui.h = pagebuf_h;
  6545. ngui_fill_rect(&ui, page, ui.theme.bg);
  6546. }
  6547. left = page.x + 8;
  6548. right = page.x + page.w - 16;
  6549. if (!layout_valid || left != layout_left || right != layout_right ||
  6550. layout_nnode != doc.nnode) {
  6551. layout_content_h = measure_page(left, right);
  6552. layout_valid = 1;
  6553. layout_left = left;
  6554. layout_right = right;
  6555. layout_nnode = doc.nnode;
  6556. }
  6557. ch = layout_content_h;
  6558. page_scroll.content_h = ch;
  6559. page_scroll.view_h = page.h;
  6560. ngui_scroll_begin(&ui, page, &page_scroll);
  6561. render_page(left, right, page.y + 4 - page_scroll.offset);
  6562. old_panel = ui.theme.panel_alt;
  6563. old_dim = ui.theme.text_dim;
  6564. ui.theme.panel_alt = 0xC618;
  6565. ui.theme.text_dim = 0x8410;
  6566. ngui_scroll_end(&ui, page, &page_scroll);
  6567. ui.theme.panel_alt = old_panel;
  6568. ui.theme.text_dim = old_dim;
  6569. if (use_buf) {
  6570. ui.px = saved_px;
  6571. ui.w = saved_w;
  6572. ui.h = saved_h;
  6573. blit_page(page, page_backbuf, pagebuf_w);
  6574. page_cache_valid = 1;
  6575. page_cache_scroll = page_scroll.offset;
  6576. page_cache_nnode = doc.nnode;
  6577. page_cache_x = page.x;
  6578. page_cache_y = page.y;
  6579. page_cache_w = page.w;
  6580. page_cache_h = page.h;
  6581. } else {
  6582. page_cache_valid = 0;
  6583. }
  6584. }
  6585. static void draw_status(struct ngui_rect r)
  6586. {
  6587. char msg[BR_URL_MAX + 32];
  6588. unsigned int n = 0;
  6589. int hover = page_hover_node();
  6590. unsigned short color;
  6591. const char *kind;
  6592. ngui_fill_rect(&ui, r, ui.theme.panel);
  6593. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
  6594. ui.theme.border);
  6595. if (hover) {
  6596. const char *href = br_href(&doc, (unsigned int)hover);
  6597. char url[BR_URL_MAX];
  6598. if (resolve_link(page_url, href, url, sizeof(url)) == 0) {
  6599. s_append(msg, &n, "Link: ", sizeof(msg));
  6600. s_append(msg, &n, url, sizeof(msg));
  6601. } else {
  6602. s_copy(msg, "Link", sizeof(msg));
  6603. }
  6604. color = ui.theme.accent;
  6605. } else if (status_text[0]) {
  6606. s_copy(msg, status_text, sizeof(msg));
  6607. color = status_error ? ui.theme.error : ui.theme.text;
  6608. } else if (page_title[0]) {
  6609. s_copy(msg, page_title, sizeof(msg));
  6610. color = ui.theme.text;
  6611. } else {
  6612. s_copy(msg, "Ready", sizeof(msg));
  6613. color = ui.theme.text_dim;
  6614. }
  6615. kind = s_eq(page_url, "about:home") ? "HOME" :
  6616. page_is_local ? "FILE" :
  6617. (starts_with(page_url, "https://") ? "HTTPS" : "HTTP");
  6618. ngui_text_ellipsis(&ui, r, r.x + 6, r.y + 1, msg, color);
  6619. {
  6620. int kw = (int)ngui_text_width_ui(&ui, kind);
  6621. ngui_text(&ui, r.x + r.w - 6 - kw, r.y + 1, kind,
  6622. ui.theme.text_dim);
  6623. }
  6624. }
  6625. static void draw(unsigned int *action)
  6626. {
  6627. struct ngui_rect r = {0, 0, ui.w, ui.h};
  6628. struct ngui_rect toolbar, status, page;
  6629. toolbar = ngui_cut_top(&r, BR_TOOLBAR_H);
  6630. status = ngui_cut_bottom(&r, BR_STATUS_H);
  6631. page = r;
  6632. draw_toolbar(toolbar, action);
  6633. draw_page(page);
  6634. draw_status(status);
  6635. }
  6636. static void redraw_now(void)
  6637. {
  6638. unsigned int action = 0;
  6639. ngui_begin(&ui);
  6640. draw(&action);
  6641. ngui_end(&ui);
  6642. }
  6643. static void handle_keys(unsigned int *action)
  6644. {
  6645. int k, scrolled = 0;
  6646. if (form_focus >= doc.nnode)
  6647. form_focus = 0;
  6648. if (form_focus && !ngui_focus(&ui)) {
  6649. while ((k = ngui_key(&ui)) != 0)
  6650. form_key(k);
  6651. return;
  6652. }
  6653. /* Batch queued page keys: a slow full-page render must not make a
  6654. * held Page Down advance only once every few frames. */
  6655. while (scrolled < 3 && ngui_scroll_keys(&ui, &page_scroll))
  6656. scrolled++;
  6657. if (scrolled)
  6658. ngui_invalidate(&ui);
  6659. if (ngui_focus(&ui))
  6660. return;
  6661. while ((k = ngui_key(&ui)) != 0) {
  6662. switch (k) {
  6663. case NGUI_KEY_ESC:
  6664. *action |= ACT_CLOSE;
  6665. break;
  6666. case NGUI_KEY_TAB:
  6667. case 0x0C: /* Ctrl+L */
  6668. ngui_set_focus(&ui, ID_URL);
  6669. break;
  6670. case 0x12: /* Ctrl+R */
  6671. *action |= ACT_RELOAD;
  6672. break;
  6673. case NGUI_KEY_BACKSPACE:
  6674. *action |= ACT_BACK;
  6675. break;
  6676. case NGUI_KEY_UP:
  6677. ngui_scroll_to(&page_scroll,
  6678. page_scroll.offset - 24);
  6679. ngui_invalidate(&ui);
  6680. break;
  6681. case NGUI_KEY_DOWN:
  6682. ngui_scroll_to(&page_scroll,
  6683. page_scroll.offset + 24);
  6684. ngui_invalidate(&ui);
  6685. break;
  6686. case NGUI_KEY_HOME:
  6687. ngui_scroll_to(&page_scroll, 0);
  6688. ngui_invalidate(&ui);
  6689. break;
  6690. case NGUI_KEY_END:
  6691. ngui_scroll_to(&page_scroll,
  6692. page_scroll.content_h -
  6693. page_scroll.view_h);
  6694. ngui_invalidate(&ui);
  6695. break;
  6696. default:
  6697. break;
  6698. }
  6699. }
  6700. }
  6701. static unsigned int form_ancestor(unsigned int node)
  6702. {
  6703. unsigned int p = node;
  6704. while (p && p < doc.nnode) {
  6705. if (doc.nodes[p].tag == BR_TAG_FORM &&
  6706. doc.nodes[p].type == BR_NODE_ELEM)
  6707. return p;
  6708. p = doc.nodes[p].parent;
  6709. }
  6710. return 0;
  6711. }
  6712. static int form_descendant(unsigned int node, unsigned int form)
  6713. {
  6714. unsigned int p = node;
  6715. while (p && p < doc.nnode) {
  6716. if (p == form)
  6717. return 1;
  6718. p = doc.nodes[p].parent;
  6719. }
  6720. return 0;
  6721. }
  6722. static void form_reset_form(unsigned int form)
  6723. {
  6724. unsigned int i;
  6725. for (i = 0; i < FORM_MAX; i++)
  6726. if (form_states[i].used &&
  6727. form_descendant(form_states[i].node, form))
  6728. form_states[i].used = 0;
  6729. form_focus = 0;
  6730. }
  6731. static void form_url_append(char *dst, unsigned int *n, unsigned int cap,
  6732. const char *s)
  6733. {
  6734. static const char hex[] = "0123456789ABCDEF";
  6735. while (s && *s && *n + 1 < cap) {
  6736. unsigned char c = (unsigned char)*s++;
  6737. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  6738. (c >= '0' && c <= '9') || c == '-' || c == '_' ||
  6739. c == '.' || c == '~') {
  6740. dst[(*n)++] = (char)c;
  6741. } else if (c == ' ') {
  6742. dst[(*n)++] = '+';
  6743. } else if (*n + 3 < cap) {
  6744. dst[(*n)++] = '%';
  6745. dst[(*n)++] = hex[c >> 4];
  6746. dst[(*n)++] = hex[c & 15];
  6747. }
  6748. }
  6749. dst[*n] = '\0';
  6750. }
  6751. static void form_add_param(char *dst, unsigned int *n, unsigned int cap,
  6752. const char *name, const char *value, int *first)
  6753. {
  6754. if (!name || !name[0])
  6755. return;
  6756. if (!*first && *n + 1 < cap)
  6757. dst[(*n)++] = '&';
  6758. *first = 0;
  6759. form_url_append(dst, n, cap, name);
  6760. if (*n + 1 < cap)
  6761. dst[(*n)++] = '=';
  6762. form_url_append(dst, n, cap, value ? value : "");
  6763. dst[*n] = '\0';
  6764. }
  6765. static int form_submit_form(unsigned int node, int fire_event)
  6766. {
  6767. unsigned int form = form_ancestor(node);
  6768. char action[BR_URL_MAX], query[2048];
  6769. unsigned int qn = 0, i;
  6770. int first = 1, is_post = 0;
  6771. const char *a, *method;
  6772. if (!form)
  6773. return 0;
  6774. if (fire_event && !js_browser_event(form, "submit", 1, 1))
  6775. return 0;
  6776. method = br_find_attr(&doc, form, "method");
  6777. if (method && (s_eq(method, "post") || s_eq(method, "POST")))
  6778. is_post = 1;
  6779. a = br_find_attr(&doc, form, "action");
  6780. if (a && a[0]) {
  6781. if (resolve_link(page_url, a, action, sizeof(action)) != 0)
  6782. return 0;
  6783. } else {
  6784. s_copy(action, page_url, sizeof(action));
  6785. }
  6786. query[0] = '\0';
  6787. for (i = 0; i < doc.nnode; i++) {
  6788. struct br_node *nd = &doc.nodes[i];
  6789. struct form_state *fs;
  6790. const char *name;
  6791. const char *value = "";
  6792. if (nd->type != BR_NODE_ELEM || i == form ||
  6793. !form_descendant(i, form))
  6794. continue;
  6795. if (nd->tag != BR_TAG_INPUT && nd->tag != BR_TAG_TEXTAREA &&
  6796. nd->tag != BR_TAG_SELECT)
  6797. continue;
  6798. if (form_disabled(i))
  6799. continue;
  6800. name = br_find_attr(&doc, i, "name");
  6801. if (!name || !name[0])
  6802. continue;
  6803. fs = form_get(i, 1);
  6804. if (!fs)
  6805. continue;
  6806. if (nd->tag == BR_TAG_SELECT) {
  6807. char opt[64];
  6808. if (!form_option_text(i, fs->selected, opt,
  6809. sizeof(opt)))
  6810. s_copy(opt, "", sizeof(opt));
  6811. value = opt;
  6812. {
  6813. unsigned int c, oi = 0;
  6814. for (c = nd->first; c;
  6815. c = doc.nodes[c].next) {
  6816. if (doc.nodes[c].type != BR_NODE_ELEM ||
  6817. doc.nodes[c].tag != BR_TAG_OPTION)
  6818. continue;
  6819. if (oi == fs->selected) {
  6820. const char *v =
  6821. br_value(&doc, c);
  6822. if (v && v[0])
  6823. value = v;
  6824. break;
  6825. }
  6826. oi++;
  6827. }
  6828. }
  6829. } else if (nd->tag == BR_TAG_INPUT) {
  6830. int kind = form_kind_of(i);
  6831. if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
  6832. if (!fs->checked)
  6833. continue;
  6834. value = br_value(&doc, i);
  6835. if (!value || !value[0])
  6836. value = "on";
  6837. } else if (kind == FORM_SUBMIT || kind == FORM_RESET ||
  6838. kind == FORM_BUTTON) {
  6839. if (i != node)
  6840. continue;
  6841. value = fs->value;
  6842. } else {
  6843. value = fs->value;
  6844. }
  6845. } else {
  6846. value = fs->value;
  6847. }
  6848. form_add_param(query, &qn, sizeof(query), name, value, &first);
  6849. }
  6850. if (is_post) {
  6851. if (action[0] == '/') {
  6852. set_status("POST forms need an HTTP action URL", 1, 4000);
  6853. ngui_invalidate(&ui);
  6854. return 0;
  6855. }
  6856. if (qn >= sizeof(pending_post))
  6857. qn = sizeof(pending_post) - 1;
  6858. if (qn)
  6859. memcpy(pending_post, query, qn);
  6860. pending_post[qn] = '\0';
  6861. pending_post_len = qn;
  6862. } else {
  6863. pending_post_len = 0;
  6864. if (qn) {
  6865. unsigned int an = s_len(action);
  6866. if (an + qn + 2 < sizeof(action)) {
  6867. action[an++] = strchr(action, '?') ? '&' : '?';
  6868. action[an] = '\0';
  6869. s_copy(action + an, query,
  6870. sizeof(action) - an);
  6871. }
  6872. }
  6873. }
  6874. s_copy(pending_url, action, sizeof(pending_url));
  6875. pending_action |= ACT_NAV;
  6876. cancel_fetch = 1;
  6877. ngui_invalidate(&ui);
  6878. return 1;
  6879. }
  6880. static void form_reset_event(unsigned int form)
  6881. {
  6882. if (form && !js_browser_event(form, "reset", 1, 1))
  6883. return;
  6884. if (form)
  6885. form_reset_form(form);
  6886. else
  6887. form_reset_state();
  6888. form_dirty = 0;
  6889. layout_valid = 0;
  6890. ngui_invalidate(&ui);
  6891. }
  6892. static void form_blur(void)
  6893. {
  6894. unsigned int node = form_focus;
  6895. form_focus = 0;
  6896. if (node && form_dirty) {
  6897. form_dirty = 0;
  6898. js_browser_event(node, "change", 1, 0);
  6899. }
  6900. }
  6901. static void form_click(unsigned int node)
  6902. {
  6903. struct form_state *fs;
  6904. int kind, changed = 0;
  6905. if (!form_is_control(node) || form_disabled(node))
  6906. return;
  6907. if (form_focus != node)
  6908. form_blur();
  6909. if (!js_browser_event(node, "click", 1, 1))
  6910. return;
  6911. fs = form_get(node, 1);
  6912. if (!fs)
  6913. return;
  6914. kind = form_kind_of(node);
  6915. form_focus = node;
  6916. form_dirty = 0;
  6917. if (kind == FORM_CHECKBOX) {
  6918. fs->checked = !fs->checked;
  6919. changed = 1;
  6920. } else if (kind == FORM_RADIO) {
  6921. const char *name = br_find_attr(&doc, node, "name");
  6922. unsigned int i;
  6923. fs->checked = 1;
  6924. changed = 1;
  6925. if (name) {
  6926. for (i = 0; i < FORM_MAX; i++) {
  6927. struct form_state *o = &form_states[i];
  6928. if (!o->used || o->node == node)
  6929. continue;
  6930. if (form_kind_of(o->node) == FORM_RADIO) {
  6931. const char *on = br_find_attr(&doc,
  6932. o->node,
  6933. "name");
  6934. if (on && s_eq(on, name))
  6935. o->checked = 0;
  6936. }
  6937. }
  6938. }
  6939. } else if (kind == FORM_SELECT) {
  6940. unsigned int n = form_option_count(node);
  6941. if (n) {
  6942. fs->selected = (unsigned char)
  6943. ((fs->selected + 1) % n);
  6944. changed = 1;
  6945. }
  6946. } else if (kind == FORM_SUBMIT) {
  6947. form_submit_form(node, 1);
  6948. return;
  6949. } else if (kind == FORM_RESET) {
  6950. form_reset_event(form_ancestor(node));
  6951. return;
  6952. } else if (kind == FORM_TEXT || kind == FORM_PASSWORD ||
  6953. kind == FORM_TEXTAREA) {
  6954. fs->caret = fs->len;
  6955. }
  6956. if (changed) {
  6957. js_browser_event(node, "input", 1, 0);
  6958. js_browser_event(node, "change", 1, 0);
  6959. }
  6960. layout_valid = 0;
  6961. ngui_invalidate(&ui);
  6962. }
  6963. static void form_focus_next(void)
  6964. {
  6965. unsigned int start = form_focus ? form_focus + 1 : 0;
  6966. unsigned int i;
  6967. form_blur();
  6968. for (i = start; i < doc.nnode; i++) {
  6969. if (form_is_control(i) && !form_disabled(i) &&
  6970. !s_eq(br_type(&doc, i), "hidden")) {
  6971. form_focus = i;
  6972. layout_valid = 0;
  6973. ngui_invalidate(&ui);
  6974. return;
  6975. }
  6976. }
  6977. form_focus = 0;
  6978. layout_valid = 0;
  6979. ngui_invalidate(&ui);
  6980. }
  6981. static void form_key(int k)
  6982. {
  6983. struct form_state *fs;
  6984. int kind;
  6985. if (!form_focus || form_focus >= doc.nnode) {
  6986. form_focus = 0;
  6987. return;
  6988. }
  6989. fs = form_get(form_focus, 1);
  6990. if (!fs || form_disabled(form_focus))
  6991. return;
  6992. kind = form_kind_of(form_focus);
  6993. if (kind == FORM_SELECT) {
  6994. unsigned int n = form_option_count(form_focus);
  6995. int changed = 0;
  6996. if (k == NGUI_KEY_UP && n) {
  6997. fs->selected = (unsigned char)
  6998. ((fs->selected + n - 1) % n);
  6999. changed = 1;
  7000. } else if (k == NGUI_KEY_DOWN && n) {
  7001. fs->selected = (unsigned char)
  7002. ((fs->selected + 1) % n);
  7003. changed = 1;
  7004. } else if (k == NGUI_KEY_ESC || k == '\r' || k == '\n') {
  7005. form_blur();
  7006. layout_valid = 0;
  7007. ngui_invalidate(&ui);
  7008. return;
  7009. } else {
  7010. return;
  7011. }
  7012. if (changed) {
  7013. js_browser_event(form_focus, "input", 1, 0);
  7014. js_browser_event(form_focus, "change", 1, 0);
  7015. }
  7016. } else if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
  7017. if (k == ' ') {
  7018. form_click(form_focus);
  7019. return;
  7020. }
  7021. if (k == NGUI_KEY_ESC || k == '\r' || k == '\n') {
  7022. form_blur();
  7023. layout_valid = 0;
  7024. ngui_invalidate(&ui);
  7025. }
  7026. return;
  7027. } else if (kind == FORM_BUTTON || kind == FORM_SUBMIT ||
  7028. kind == FORM_RESET) {
  7029. if (k == NGUI_KEY_ESC) {
  7030. form_blur();
  7031. layout_valid = 0;
  7032. ngui_invalidate(&ui);
  7033. } else if (k == '\r' || k == '\n' || k == ' ') {
  7034. form_click(form_focus);
  7035. }
  7036. return;
  7037. } else if (k == NGUI_KEY_TAB) {
  7038. form_focus_next();
  7039. return;
  7040. } else if (k == NGUI_KEY_ESC) {
  7041. form_blur();
  7042. layout_valid = 0;
  7043. ngui_invalidate(&ui);
  7044. return;
  7045. } else if (k == '\r' || k == '\n') {
  7046. unsigned int node = form_focus;
  7047. form_blur();
  7048. if (form_ancestor(node))
  7049. form_submit_form(node, 1);
  7050. layout_valid = 0;
  7051. ngui_invalidate(&ui);
  7052. return;
  7053. } else if (k == NGUI_KEY_BACKSPACE) {
  7054. if (fs->caret) {
  7055. unsigned int i = --fs->caret;
  7056. for (; i < fs->len; i++)
  7057. fs->value[i] = fs->value[i + 1];
  7058. if (fs->len)
  7059. fs->len--;
  7060. form_dirty = 1;
  7061. js_browser_event(form_focus, "input", 1, 0);
  7062. }
  7063. } else if (k == NGUI_KEY_DELETE) {
  7064. if (fs->caret < fs->len) {
  7065. unsigned int i;
  7066. for (i = fs->caret; i < fs->len; i++)
  7067. fs->value[i] = fs->value[i + 1];
  7068. fs->len--;
  7069. form_dirty = 1;
  7070. js_browser_event(form_focus, "input", 1, 0);
  7071. }
  7072. } else if (k == NGUI_KEY_LEFT) {
  7073. if (fs->caret)
  7074. fs->caret--;
  7075. } else if (k == NGUI_KEY_RIGHT) {
  7076. if (fs->caret < fs->len)
  7077. fs->caret++;
  7078. } else if (k == NGUI_KEY_HOME) {
  7079. fs->caret = 0;
  7080. } else if (k == NGUI_KEY_END) {
  7081. fs->caret = fs->len;
  7082. } else if (k >= 32 && k < 127) {
  7083. if (fs->len < FORM_VALUE_MAX) {
  7084. unsigned int i;
  7085. for (i = fs->len; i > fs->caret; i--)
  7086. fs->value[i] = fs->value[i - 1];
  7087. fs->value[fs->caret++] = (char)k;
  7088. fs->len++;
  7089. form_dirty = 1;
  7090. js_browser_event(form_focus, "input", 1, 0);
  7091. }
  7092. } else {
  7093. return;
  7094. }
  7095. fs->value[fs->len] = '\0';
  7096. layout_valid = 0;
  7097. ngui_invalidate(&ui);
  7098. }
  7099. static void handle_page_click(unsigned int *action)
  7100. {
  7101. int node;
  7102. char url[BR_URL_MAX];
  7103. if (!ui.pressed)
  7104. return;
  7105. if (ui.press_x < page_rect.x ||
  7106. ui.press_x >= page_rect.x + page_rect.w - 12 ||
  7107. ui.press_y < page_rect.y ||
  7108. ui.press_y >= page_rect.y + page_rect.h)
  7109. return;
  7110. node = find_hit(ui.press_x, ui.press_y);
  7111. if (!node)
  7112. return;
  7113. if (form_is_control((unsigned int)node)) {
  7114. form_click((unsigned int)node);
  7115. return;
  7116. }
  7117. form_blur();
  7118. if (!js_browser_event((unsigned int)node, "click", 1, 1))
  7119. return;
  7120. if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
  7121. sizeof(url)) != 0)
  7122. return;
  7123. s_copy(click_url, url, sizeof(click_url));
  7124. *action |= ACT_NAV;
  7125. }
  7126. void _start(const char *arg)
  7127. {
  7128. struct win_info wi;
  7129. if (u_win_create(BR_W, BR_H, "Browser", &wi) != 0 &&
  7130. u_win_create(960, 640, "Browser", &wi) != 0 &&
  7131. u_win_create(780, 540, "Browser", &wi) != 0) {
  7132. u_puts("browser: no graphics window\n");
  7133. u_exit(1);
  7134. }
  7135. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  7136. url_in.placeholder = "http://...";
  7137. br_doc_init(&doc);
  7138. br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
  7139. ui.theme.accent);
  7140. br_set_viewport(wi.width);
  7141. if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
  7142. s_copy(cwd, "/", sizeof(cwd));
  7143. heap_clear();
  7144. origin_cache_load();
  7145. css_cache_init();
  7146. script_cache_init();
  7147. page_scroll.offset = 0;
  7148. page_scroll.content_h = 0;
  7149. if (arg && arg[0]) {
  7150. navigate(arg, 1);
  7151. } else {
  7152. load_home(1);
  7153. /* Start ready to type: keep the home page, but leave the address
  7154. * bar empty instead of showing "about:home". */
  7155. ngui_input_set(&url_in, "");
  7156. ngui_set_focus(&ui, ID_URL);
  7157. }
  7158. for (;;) {
  7159. unsigned int action = 0;
  7160. unsigned int now;
  7161. click_url[0] = '\0';
  7162. if (pending_action) {
  7163. action |= pending_action & ACT_WAIT_NAV;
  7164. if (pending_url[0])
  7165. s_copy(click_url, pending_url, sizeof(click_url));
  7166. pending_action = 0;
  7167. pending_url[0] = '\0';
  7168. cancel_fetch = 0;
  7169. } else if (preload_active) {
  7170. if (preload_wait)
  7171. preload_wait = 0;
  7172. else
  7173. preload_step();
  7174. } else if (preload_pending &&
  7175. page_scroll.offset != preload_scroll) {
  7176. preload_scroll = page_scroll.offset;
  7177. preload_pending = 0;
  7178. preload_node = 0;
  7179. preload_active = 1;
  7180. }
  7181. js_browser_tick();
  7182. if (pending_action)
  7183. continue;
  7184. now = u_ticks_ms();
  7185. if (status_until && (int)(now - status_until) >= 0) {
  7186. status_text[0] = '\0';
  7187. status_until = 0;
  7188. status_error = 0;
  7189. ngui_invalidate(&ui);
  7190. }
  7191. if (!ngui_begin(&ui)) {
  7192. /* A queued navigation (e.g. Enter in the URL bar during
  7193. * a slow fetch) must be handled even when this frame
  7194. * needs no redraw. */
  7195. if (!action) {
  7196. u_sleep_ms(10);
  7197. continue;
  7198. }
  7199. } else {
  7200. draw(&action);
  7201. handle_keys(&action);
  7202. handle_page_click(&action);
  7203. ngui_end(&ui);
  7204. }
  7205. if (action & ACT_NAV) {
  7206. if (click_url[0]) {
  7207. navigate(click_url, 1);
  7208. click_url[0] = '\0';
  7209. } else {
  7210. navigate(url_in.text, 1);
  7211. }
  7212. }
  7213. if (action & ACT_BACK) {
  7214. if (hist_pos > 0) {
  7215. hist_pos--;
  7216. navigate(history[hist_pos], 0);
  7217. }
  7218. }
  7219. if (action & ACT_FORWARD) {
  7220. if (hist_pos + 1 < hist_len) {
  7221. hist_pos++;
  7222. navigate(history[hist_pos], 0);
  7223. }
  7224. }
  7225. if (action & ACT_RELOAD) {
  7226. if (s_eq(page_url, "about:home"))
  7227. load_home(0);
  7228. else
  7229. navigate(page_url, 0);
  7230. }
  7231. if (action & ACT_HOME)
  7232. load_home(1);
  7233. if (action & ACT_CLOSE)
  7234. quit = 1;
  7235. if (quit)
  7236. break;
  7237. if (pending_action)
  7238. continue;
  7239. u_sleep_ms(10);
  7240. }
  7241. fetch_helper_stop();
  7242. u_win_close();
  7243. u_exit(0);
  7244. }