browser.c 208 KB

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