browser.c 180 KB

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