browser.c 181 KB

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