| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801 |
- /* browser: a small HTML/CSS browser built on ngui, the image library and the
- * shared uhttp client.
- *
- * Inline JavaScript runs through the vendored MuJS engine (console.log,
- * alert, document.getElementById(...).textContent and location.href); forms
- * are not supported. External CSS is loaded over uhttp, and images are
- * decoded with img.c and drawn inline. Page-scoped buffers (page body,
- * rules and image bitmaps) come from the ring-3 libc heap; each image gets an
- * exact-size decode arena that is freed as soon as the scaled bitmap exists.
- * The DOM and the image/CSS I/O scratch use fixed static arenas.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <ucmd_heap.h>
- #include "ucmd_lib.h"
- #include "ngui.h"
- #include "browser_html.h"
- #include "js_browser.h"
- #include "uhttp.h"
- #include "img.h"
- #include "inflate.h"
- #include "ufont.h"
- /* Start wide enough for the desktop nav; smaller screens fall back to
- * 960x640 and 780x540, which still fit a 1024x768 panel. */
- #define BR_W 1100
- #define BR_H 700
- #define BR_TOOLBAR_H 30
- #define BR_STATUS_H 18
- #define BR_PAGE_MAX (2u * 1024u * 1024u)
- #define BR_IMG_FILE_MAX (512u * 1024u)
- #define BR_IMG_PIX_MAX (8u * 1024u * 1024u)
- #define BR_MAX_IMAGES 16
- #define BR_IMG_ARENA_MAX (24u * 1024u * 1024u)
- #define BR_IMG_LAYOUT_W 1000
- #define BR_CSS_MAX (832u * 1024u)
- #define BR_CSS_COMP_MAX (192u * 1024u)
- #define BR_SCRIPT_MAX (512u * 1024u)
- #define FETCH_ABORT (-2)
- /* Session cache for remote stylesheets (per launch; cleared at startup). */
- #define CSS_CACHE_MAX 16
- #define CSS_CACHE_FILE_MAX (256u * 1024u)
- #define CSS_CACHE_PREFIX "bcss-"
- #define SCRIPT_CACHE_MAX 16
- #define SCRIPT_CACHE_FILE_MAX (512u * 1024u)
- #define SCRIPT_CACHE_PREFIX "bjs-"
- #define BR_MAX_SHEETS 6
- #define BR_HISTORY 16
- #define BR_URL_MAX 512
- #define BR_STATUS_MAX 200
- #define BR_IMG_RGB565(r, g, b) \
- ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
- #define BR_GRAY 0x8410
- enum {
- ID_URL = 1,
- ID_GO,
- ID_BACK,
- ID_FORWARD,
- ID_RELOAD,
- ID_HOME
- };
- #define ACT_NAV (1u << 0)
- #define ACT_BACK (1u << 1)
- #define ACT_FORWARD (1u << 2)
- #define ACT_RELOAD (1u << 3)
- #define ACT_HOME (1u << 4)
- #define ACT_CLOSE (1u << 5)
- #define ACT_WAIT_NAV (ACT_NAV | ACT_BACK | ACT_FORWARD | ACT_RELOAD | \
- ACT_HOME | ACT_CLOSE)
- struct br_image {
- int state; /* 0 empty, 1 ready */
- struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */
- int native_w, native_h;
- char err[32];
- };
- static struct ngui ui;
- static struct ngui_input url_in;
- static struct ngui_scroll page_scroll;
- static struct br_doc doc;
- static struct br_image images[BR_MAX_IMAGES];
- static int image_count;
- /* Painted border boxes, in page/screen coordinates (layout starts at
- * page_rect.y + 4 - scroll). getBoundingClientRect() subtracts page_rect. */
- struct br_layout_box {
- int x, y;
- int w, h;
- unsigned char valid;
- };
- static struct br_layout_box layout_boxes[BR_MAX_NODES];
- /* Images are loaded one per main-loop iteration so the parsed page stays
- * interactive while a slow network/TLS fetch or 4K decode is in progress. */
- static int preload_active;
- static int preload_pending;
- static int preload_wait;
- static unsigned int preload_node;
- static int preload_loaded;
- static int preload_scroll;
- static char post_status_text[BR_STATUS_MAX];
- static int post_status_error;
- static unsigned int post_status_ttl;
- /* Content-relative position/height of each <img>, recorded by the measure
- * pass so image loading can be deferred until it is near the viewport. */
- static unsigned short img_top[BR_MAX_NODES];
- static unsigned short img_h[BR_MAX_NODES];
- /* One reusable I/O buffer: the two halves double as the gzip input scratch
- * for page/CSS fetches, where the heap has no room for another multi-megabyte
- * body. Decode arenas are allocated per image instead of living here. */
- static unsigned char *image_io_buf;
- static struct ufont_face *font_faces[BR_MAX_FACES];
- #define image_file_buf (image_io_buf)
- #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX)
- #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX)
- static struct inflate image_inflate_ws;
- #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX
- #error "CSS scratch must fit in image_io_buf"
- #endif
- static char page_url[BR_URL_MAX];
- static char page_title[BR_TITLE_MAX];
- static char cwd[64];
- static int page_is_local;
- static int page_loading;
- static char status_text[BR_STATUS_MAX];
- static unsigned int status_until;
- static int status_error;
- static char history[BR_HISTORY][BR_URL_MAX];
- static int hist_len, hist_pos;
- static char click_url[BR_URL_MAX];
- static char pending_url[BR_URL_MAX];
- #define BR_POST_MAX 4096
- static char pending_post[BR_POST_MAX];
- static unsigned int pending_post_len;
- static unsigned int pending_action;
- static int cancel_fetch;
- static int quit;
- static struct ngui_rect page_rect;
- static unsigned short *page_backbuf;
- static int pagebuf_w, pagebuf_h;
- static int page_cache_valid;
- static int page_cache_scroll;
- static unsigned int page_cache_nnode;
- static int page_cache_x, page_cache_y, page_cache_w, page_cache_h;
- static int layout_valid, layout_left, layout_right;
- static unsigned int layout_nnode;
- static int layout_content_h;
- /* layout output */
- struct lay_run {
- unsigned short off, len;
- unsigned short gap;
- unsigned short color;
- unsigned short href;
- char lit[28];
- short img;
- signed short dy; /* inline image vertical offset */
- unsigned short iw, ih;
- unsigned short w;
- unsigned char font_px, bold, italic, mono, underline, font_face;
- unsigned char transform_scale;
- unsigned char origin_x_kind, origin_y_kind;
- signed short scale_x, scale_y, rotate_x100;
- signed short origin_x, origin_y;
- unsigned char has_lit;
- unsigned int form; /* node + 1, 0 = not a control */
- unsigned char form_kind;
- unsigned char form_disabled;
- };
- struct lay {
- int draw;
- struct ngui *ui;
- int left, right;
- int y;
- int line_x, line_y, line_h;
- int line_left, line_right;
- int parent_top;
- int run_x;
- int align;
- int depth;
- int space_pending;
- unsigned char float_active;
- int float_left, float_right, float_bottom;
- struct lay_run runs[96];
- int nruns;
- };
- struct hit {
- int x, y, w, h;
- unsigned short node;
- };
- /* Live form state; the DOM attributes stay the initial/default value. */
- #define FORM_MAX 64
- #define FORM_VALUE_MAX 63
- enum {
- FORM_TEXT, FORM_PASSWORD, FORM_CHECKBOX, FORM_RADIO,
- FORM_BUTTON, FORM_SUBMIT, FORM_RESET, FORM_SELECT, FORM_TEXTAREA
- };
- struct form_state {
- unsigned int node;
- char value[FORM_VALUE_MAX + 1];
- unsigned char len, caret, checked, selected, used;
- };
- static struct form_state form_states[FORM_MAX];
- static unsigned int form_focus;
- static unsigned int form_dirty;
- static int form_descendant(unsigned int node, unsigned int form);
- static int form_submit_form(unsigned int node, int fire_event);
- static void form_reset_event(unsigned int form);
- static unsigned int s_len(const char *s);
- static int s_eq(const char *a, const char *b);
- static void form_key(int k);
- static void form_click(unsigned int node);
- static void style_for_node(unsigned int node, struct br_style *out);
- static int form_disabled(unsigned int node)
- {
- struct br_node *n = &doc.nodes[node];
- if (n->attr_flags & BR_ATTR_DISABLED)
- return 1;
- return br_find_attr(&doc, node, "disabled") != 0;
- }
- static int form_kind_of(unsigned int node)
- {
- const char *type = br_type(&doc, node);
- if (doc.nodes[node].tag == BR_TAG_TEXTAREA)
- return FORM_TEXTAREA;
- if (doc.nodes[node].tag == BR_TAG_SELECT)
- return FORM_SELECT;
- if (doc.nodes[node].tag == BR_TAG_BUTTON) {
- if (s_eq(type, "reset"))
- return FORM_RESET;
- if (s_eq(type, "button"))
- return FORM_BUTTON;
- return FORM_SUBMIT;
- }
- if (s_eq(type, "password"))
- return FORM_PASSWORD;
- if (s_eq(type, "checkbox"))
- return FORM_CHECKBOX;
- if (s_eq(type, "radio"))
- return FORM_RADIO;
- if (s_eq(type, "submit"))
- return FORM_SUBMIT;
- if (s_eq(type, "reset"))
- return FORM_RESET;
- if (s_eq(type, "button"))
- return FORM_BUTTON;
- return FORM_TEXT;
- }
- static unsigned int form_option_count(unsigned int node)
- {
- unsigned int c, n = 0;
- for (c = doc.nodes[node].first; c; c = doc.nodes[c].next)
- if (doc.nodes[c].type == BR_NODE_ELEM &&
- doc.nodes[c].tag == BR_TAG_OPTION)
- n++;
- return n;
- }
- static unsigned int form_option_selected(unsigned int node)
- {
- unsigned int c, i = 0;
- for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
- if (doc.nodes[c].type != BR_NODE_ELEM ||
- doc.nodes[c].tag != BR_TAG_OPTION)
- continue;
- if (br_find_attr(&doc, c, "selected"))
- return i;
- i++;
- }
- return 0;
- }
- static unsigned int form_option_text(unsigned int node, unsigned int idx,
- char *out, unsigned int cap)
- {
- unsigned int c, i = 0;
- for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
- if (doc.nodes[c].type != BR_NODE_ELEM ||
- doc.nodes[c].tag != BR_TAG_OPTION)
- continue;
- if (i == idx)
- return br_text_content(&doc, c, out, cap);
- i++;
- }
- return 0;
- }
- static struct form_state *form_get(unsigned int node, int create)
- {
- struct form_state *slot = 0;
- unsigned int i;
- for (i = 0; i < FORM_MAX; i++) {
- if (form_states[i].used) {
- if (form_states[i].node == node)
- return &form_states[i];
- } else if (!slot) {
- slot = &form_states[i];
- }
- }
- if (!create || !slot)
- return 0;
- slot->node = node;
- slot->len = 0;
- slot->caret = 0;
- slot->checked = br_find_attr(&doc, node, "checked") != 0;
- slot->selected = form_option_selected(node);
- slot->used = 1;
- {
- unsigned int n = 0;
- if (doc.nodes[node].tag == BR_TAG_TEXTAREA) {
- n = br_text_content(&doc, node, slot->value,
- FORM_VALUE_MAX + 1);
- } else {
- const char *v = br_value(&doc, node);
- if (v) {
- n = s_len(v);
- if (n > FORM_VALUE_MAX)
- n = FORM_VALUE_MAX;
- for (unsigned int j = 0; j < n; j++)
- slot->value[j] = v[j];
- }
- if (doc.nodes[node].tag == BR_TAG_BUTTON && n == 0)
- n = br_text_content(&doc, node, slot->value,
- FORM_VALUE_MAX + 1);
- }
- if (n > FORM_VALUE_MAX)
- n = FORM_VALUE_MAX;
- slot->value[n] = '\0';
- slot->len = (unsigned char)n;
- slot->caret = (unsigned char)n;
- }
- return slot;
- }
- static void form_reset_state(void)
- {
- unsigned int i;
- for (i = 0; i < FORM_MAX; i++)
- form_states[i].used = 0;
- form_focus = 0;
- }
- static int form_is_control(unsigned int node)
- {
- unsigned int tag;
- if (node >= doc.nnode || doc.nodes[node].type != BR_NODE_ELEM)
- return 0;
- tag = doc.nodes[node].tag;
- return tag == BR_TAG_INPUT || tag == BR_TAG_TEXTAREA ||
- tag == BR_TAG_SELECT || tag == BR_TAG_BUTTON;
- }
- #define HIT_MAX 96
- static struct hit hits[HIT_MAX];
- static int nhits;
- static unsigned short block_h[BR_MAX_NODES];
- static char li_marker[BR_MAX_NODES][6];
- /* one connection object; the browser fetches one resource at a time */
- /* ---- tiny strings ---- */
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (s && s[i] && i + 1 < cap) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
- {
- while (s && *s && *n + 1 < cap)
- d[(*n)++] = *s++;
- d[*n] = '\0';
- }
- static void s_append_dec(char *d, unsigned int *n, unsigned int v,
- unsigned int cap)
- {
- char tmp[12];
- unsigned int i = 0, j;
- do {
- tmp[i++] = (char)('0' + v % 10);
- v /= 10;
- } while (v && i < sizeof(tmp));
- for (j = 0; j < i && *n + 1 < cap; j++)
- d[(*n)++] = tmp[i - 1 - j];
- d[*n] = '\0';
- }
- static int s_eq(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static int starts_with(const char *s, const char *p)
- {
- unsigned int i = 0;
- while (p[i]) {
- if (s[i] != p[i])
- return 0;
- i++;
- }
- return 1;
- }
- static int contains_ci(const char *s, const char *p)
- {
- unsigned int i, j;
- for (i = 0; s[i]; i++) {
- for (j = 0; p[j]; j++) {
- char a = s[i + j];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (a != p[j])
- break;
- }
- if (!p[j])
- return 1;
- }
- return 0;
- }
- static char *trim(char *s)
- {
- unsigned int n;
- while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
- s++;
- n = s_len(s);
- while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
- s[n - 1] == '\r' || s[n - 1] == '\n'))
- s[--n] = '\0';
- return s;
- }
- /* ---- per-process heap ---- */
- static int heap_start(void)
- {
- image_io_buf = (unsigned char *)malloc(2u * BR_IMG_FILE_MAX);
- if (!image_io_buf)
- return -1;
- page_backbuf = (unsigned short *)malloc(
- (unsigned int)ui.w * (unsigned int)ui.h * 2u);
- pagebuf_w = page_backbuf ? ui.w : 0;
- pagebuf_h = page_backbuf ? ui.h : 0;
- page_cache_valid = 0;
- return 0;
- }
- static void heap_clear(void)
- {
- memset(font_faces, 0, sizeof(font_faces));
- ngui_font_detach(&ui);
- heap_reset();
- image_io_buf = 0;
- page_backbuf = 0;
- pagebuf_w = pagebuf_h = 0;
- page_cache_valid = 0;
- }
- static void browser_rules_alloc(void)
- {
- struct br_rule *rules;
- rules = (struct br_rule *)malloc(
- (unsigned int)(BR_MAX_RULES * sizeof(struct br_rule)));
- br_set_rule_store(&doc, rules, rules ? BR_MAX_RULES : 0);
- }
- /* ---- status ---- */
- static void set_status(const char *s, int err, unsigned int ttl_ms)
- {
- s_copy(status_text, s, sizeof(status_text));
- status_error = err;
- status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0;
- ngui_invalidate(&ui);
- }
- /* ---- JavaScript ---- */
- static int resolve_link(const char *base, const char *href, char *out,
- unsigned int cap);
- static char js_last_status[BR_STATUS_MAX];
- static int js_last_error;
- static void js_host_status(const char *msg, int error)
- {
- s_copy(js_last_status, msg, sizeof(js_last_status));
- js_last_error = error;
- set_status(msg, error, 6000);
- }
- static void js_host_report(const char *msg)
- {
- u_puts("[js] ");
- u_puts(msg);
- u_putc('\n');
- s_copy(js_last_status, msg, sizeof(js_last_status));
- js_last_error = 1;
- set_status(msg, 1, 8000);
- }
- static void js_host_navigate(const char *url)
- {
- char resolved[BR_URL_MAX];
- if (!url || !url[0])
- return;
- if (resolve_link(page_url, url, resolved, sizeof(resolved)) != 0)
- return;
- s_copy(pending_url, resolved, sizeof(pending_url));
- pending_action |= ACT_NAV;
- cancel_fetch = 1;
- }
- static void js_host_changed(void)
- {
- /* Class/style/attribute mutations change computed styles, not just the
- * text: drop the per-node style cache before the next layout. */
- br_reset_styles(&doc);
- layout_valid = 0;
- ngui_invalidate(&ui);
- }
- static int js_host_rect(unsigned int node, int *x, int *y, int *w, int *h)
- {
- if (node >= BR_MAX_NODES || !layout_boxes[node].valid)
- return 0;
- *x = layout_boxes[node].x - page_rect.x;
- *y = layout_boxes[node].y - page_rect.y;
- *w = layout_boxes[node].w;
- *h = layout_boxes[node].h;
- return 1;
- }
- static void form_copy_out(const char *s, char *buf, int cap)
- {
- int i = 0;
- if (!buf || cap <= 0)
- return;
- while (s && s[i] && i + 1 < cap) {
- buf[i] = s[i];
- i++;
- }
- buf[i] = '\0';
- }
- static int js_host_form_value(unsigned int node, char *buf, int cap)
- {
- struct form_state *fs;
- int kind;
- if (!form_is_control(node))
- return 0;
- kind = form_kind_of(node);
- if (kind == FORM_CHECKBOX || kind == FORM_RADIO)
- return 0;
- if (kind == FORM_SELECT) {
- char opt[64] = "";
- unsigned int c, i = 0;
- fs = form_get(node, 1);
- if (!fs)
- return 0;
- form_option_text(node, fs->selected, opt, sizeof(opt));
- for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
- if (doc.nodes[c].type != BR_NODE_ELEM ||
- doc.nodes[c].tag != BR_TAG_OPTION)
- continue;
- if (i == fs->selected) {
- const char *v = br_value(&doc, c);
- if (v && v[0])
- form_copy_out(v, buf, cap);
- else
- form_copy_out(opt, buf, cap);
- return 1;
- }
- i++;
- }
- form_copy_out(opt, buf, cap);
- return 1;
- }
- fs = form_get(node, 1);
- if (!fs)
- return 0;
- form_copy_out(fs->value, buf, cap);
- return 1;
- }
- static int js_host_form_checked(unsigned int node)
- {
- struct form_state *fs;
- int kind;
- if (!form_is_control(node))
- return -1;
- kind = form_kind_of(node);
- if (kind != FORM_CHECKBOX && kind != FORM_RADIO)
- return -1;
- fs = form_get(node, 1);
- return fs ? fs->checked : 0;
- }
- static int js_host_form_selected(unsigned int node)
- {
- struct form_state *fs;
- if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT)
- return -1;
- fs = form_get(node, 1);
- return fs ? fs->selected : 0;
- }
- static int js_host_form_set_value(unsigned int node, const char *value)
- {
- struct form_state *fs;
- unsigned int n;
- if (!form_is_control(node))
- return 0;
- fs = form_get(node, 1);
- if (!fs)
- return 0;
- n = s_len(value ? value : "");
- if (n > FORM_VALUE_MAX)
- n = FORM_VALUE_MAX;
- for (unsigned int i = 0; i < n; i++)
- fs->value[i] = value[i];
- fs->value[n] = '\0';
- fs->len = (unsigned char)n;
- fs->caret = (unsigned char)n;
- layout_valid = 0;
- ngui_invalidate(&ui);
- return 1;
- }
- static int js_host_form_set_checked(unsigned int node, int checked)
- {
- struct form_state *fs;
- int kind;
- if (!form_is_control(node))
- return 0;
- kind = form_kind_of(node);
- if (kind != FORM_CHECKBOX && kind != FORM_RADIO)
- return 0;
- fs = form_get(node, 1);
- if (!fs)
- return 0;
- fs->checked = (unsigned char)(checked != 0);
- layout_valid = 0;
- ngui_invalidate(&ui);
- return 1;
- }
- static int js_host_form_set_selected(unsigned int node, int index)
- {
- struct form_state *fs;
- unsigned int n;
- if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT)
- return 0;
- fs = form_get(node, 1);
- if (!fs)
- return 0;
- n = form_option_count(node);
- if (index < 0)
- index = 0;
- if (n && (unsigned int)index >= n)
- index = (int)n - 1;
- fs->selected = (unsigned char)index;
- layout_valid = 0;
- ngui_invalidate(&ui);
- return 1;
- }
- static int js_host_form_elements(unsigned int form, unsigned int *out, int cap)
- {
- unsigned int i;
- int n = 0;
- if (!form || form >= doc.nnode || doc.nodes[form].type != BR_NODE_ELEM ||
- doc.nodes[form].tag != BR_TAG_FORM)
- return 0;
- for (i = 0; i < doc.nnode && n < cap; i++) {
- struct br_node *nd = &doc.nodes[i];
- if (i == form || nd->type != BR_NODE_ELEM)
- continue;
- if (nd->tag != BR_TAG_INPUT && nd->tag != BR_TAG_SELECT &&
- nd->tag != BR_TAG_TEXTAREA && nd->tag != BR_TAG_BUTTON)
- continue;
- if (!form_descendant(i, form))
- continue;
- out[n++] = i;
- }
- return n;
- }
- static int js_host_form_submit(unsigned int form)
- {
- form_submit_form(form, 0);
- return 1;
- }
- static int js_host_form_reset(unsigned int form)
- {
- form_reset_event(form);
- return 1;
- }
- static void css_computed_name(const char *p, char *out, int cap)
- {
- int n = 0;
- for (; p && *p && n + 2 < cap; p++) {
- char c = *p;
- if (c >= 'A' && c <= 'Z') {
- out[n++] = '-';
- c = (char)(c - 'A' + 'a');
- } else if (c == '_') {
- c = '-';
- } else if (c >= 'a' && c <= 'z') {
- /* keep */
- }
- out[n++] = c;
- }
- out[n] = '\0';
- if (!strcmp(out, "css-float"))
- strcpy(out, "float");
- }
- static void css_rgb(unsigned short c, char *buf, int cap)
- {
- int r = ((c >> 11) & 31) * 255 / 31;
- int g = ((c >> 5) & 63) * 255 / 63;
- int b = (c & 31) * 255 / 31;
- snprintf(buf, cap, "rgb(%d, %d, %d)", r, g, b);
- }
- static void css_mp_value(char *buf, int cap, int raw, int unit, int px)
- {
- if (unit == 4) {
- snprintf(buf, cap, "auto");
- } else if (unit == 0) {
- snprintf(buf, cap, "%dpx", raw);
- } else if (unit == 1) {
- snprintf(buf, cap, "%d.%02d%%", raw / 100, raw % 100);
- } else if (unit == 2) {
- snprintf(buf, cap, "%d.%02dem", raw / 100, raw % 100);
- } else if (unit == 3) {
- snprintf(buf, cap, "%d.%02drem", raw / 100, raw % 100);
- } else {
- snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
- raw / 100, raw % 100, px);
- }
- }
- static int js_host_computed(unsigned int node, const char *property,
- char *buf, int cap)
- {
- struct br_style st;
- char name[40];
- if (!buf || cap <= 0 || node >= doc.nnode || node == 0)
- return 0;
- buf[0] = '\0';
- css_computed_name(property, name, sizeof(name));
- style_for_node(node, &st);
- if (!strcmp(name, "display")) {
- const char *v = "inline";
- if (st.display == BR_DISPLAY_BLOCK)
- v = "block";
- else if (st.display == BR_DISPLAY_NONE)
- v = "none";
- else if (st.display == BR_DISPLAY_FLEX)
- v = "flex";
- else if (st.display == BR_DISPLAY_GRID)
- v = "grid";
- else if (st.display == BR_DISPLAY_INLINE_BLOCK)
- v = "inline-block";
- snprintf(buf, cap, "%s", v);
- } else if (!strcmp(name, "position")) {
- static const char *pos[] = {
- "static", "absolute", "fixed", "relative", "sticky"
- };
- snprintf(buf, cap, "%s", pos[st.pos < 5 ? st.pos : 0]);
- } else if (!strcmp(name, "width") || !strcmp(name, "height")) {
- int is_w = name[0] == 'w';
- int kind = is_w ? st.width_kind : st.height_kind;
- int val = is_w ? st.width : st.height;
- if (kind == 3) {
- if (is_w)
- snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
- st.width_calc_pct / 100,
- st.width_calc_pct % 100,
- st.width_calc_px);
- else
- snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
- st.height_calc_pct / 100,
- st.height_calc_pct % 100,
- st.height_calc_px);
- } else if (kind == 1) {
- snprintf(buf, cap, "%dpx", val);
- } else if (kind == 2) {
- snprintf(buf, cap, "%d.%02d%%", val / 100, val % 100);
- } else {
- snprintf(buf, cap, "auto");
- }
- } else if (!strcmp(name, "color") ||
- !strcmp(name, "background-color")) {
- if (!strcmp(name, "background-color") && !st.has_bg)
- snprintf(buf, cap, "rgba(0, 0, 0, 0)");
- else
- css_rgb(name[0] == 'c' ? st.color : st.bg, buf, cap);
- } else if (!strcmp(name, "font-size")) {
- snprintf(buf, cap, "%dpx", st.font_px);
- } else if (!strcmp(name, "font-weight")) {
- snprintf(buf, cap, "%s", st.bold ? "700" : "400");
- } else if (!strcmp(name, "font-style")) {
- snprintf(buf, cap, "%s", st.italic ? "italic" : "normal");
- } else if (!strcmp(name, "font-family")) {
- if (st.font_face > 0 && st.font_face <= BR_MAX_FACES &&
- br_font_face_family(st.font_face)[0])
- snprintf(buf, cap, "%s",
- br_font_face_family(st.font_face));
- else
- snprintf(buf, cap, "%s",
- st.mono ? "monospace" : "sans-serif");
- } else if (!strcmp(name, "text-align")) {
- static const char *al[] = { "left", "center", "right", "justify" };
- snprintf(buf, cap, "%s", al[st.align < 4 ? st.align : 0]);
- } else if (!strcmp(name, "float")) {
- snprintf(buf, cap, "%s", st.float_left == 1 ? "left" :
- st.float_left == 2 ? "right" : "none");
- } else if (!strcmp(name, "clear")) {
- static const char *cl[] = { "none", "left", "right", "both" };
- snprintf(buf, cap, "%s", cl[st.clear < 4 ? st.clear : 0]);
- } else if (!strcmp(name, "opacity")) {
- snprintf(buf, cap, "%s", st.opacity_hidden ? "0" : "1");
- } else if (!strcmp(name, "visibility")) {
- snprintf(buf, cap, "%s", st.vis_hidden ? "hidden" : "visible");
- } else if (!strcmp(name, "overflow")) {
- snprintf(buf, cap, "%s",
- st.overflow_hidden ? "hidden" : "visible");
- } else if (!strcmp(name, "white-space")) {
- snprintf(buf, cap, "%s", st.nowrap ? "nowrap" : "normal");
- } else if (!strcmp(name, "text-transform")) {
- snprintf(buf, cap, "%s", st.uppercase ? "uppercase" : "none");
- } else if (!strcmp(name, "box-sizing")) {
- snprintf(buf, cap, "%s",
- st.box_sizing ? "border-box" : "content-box");
- } else if (!strcmp(name, "line-height")) {
- if (st.line_h_px)
- snprintf(buf, cap, "%dpx", st.line_h_px);
- else if (st.line_h_pct)
- snprintf(buf, cap, "%d%%", st.line_h_pct);
- else
- snprintf(buf, cap, "normal");
- } else if (!strcmp(name, "z-index")) {
- if (st.z_set)
- snprintf(buf, cap, "%d", st.z_index);
- else
- snprintf(buf, cap, "auto");
- } else if (!strcmp(name, "top") || !strcmp(name, "left") ||
- !strcmp(name, "right") || !strcmp(name, "bottom")) {
- int set = name[0] == 't' ? st.top_set :
- name[0] == 'l' ? st.left_set :
- name[0] == 'r' ? st.right_set : st.bottom_set;
- int v = name[0] == 't' ? st.top :
- name[0] == 'l' ? st.left :
- name[0] == 'r' ? st.right : st.bottom;
- if (set)
- snprintf(buf, cap, "%dpx", v);
- else
- snprintf(buf, cap, "auto");
- } else if (!strcmp(name, "margin-top") || !strcmp(name, "margin-right") ||
- !strcmp(name, "margin-bottom") ||
- !strcmp(name, "margin-left") ||
- !strcmp(name, "padding-top") ||
- !strcmp(name, "padding-right") ||
- !strcmp(name, "padding-bottom") ||
- !strcmp(name, "padding-left")) {
- int is_pad = name[0] == 'p';
- int side = name[strlen(name) - 1] == 'p' ? 0 :
- name[strlen(name) - 1] == 't' ? 1 :
- name[strlen(name) - 1] == 'm' ? 2 : 3;
- /* last letter: top p, right t, bottom m, left t (ambiguity). */
- if (!strcmp(name, "margin-right") ||
- !strcmp(name, "padding-right"))
- side = 1;
- else if (!strcmp(name, "margin-bottom") ||
- !strcmp(name, "padding-bottom"))
- side = 2;
- else if (!strcmp(name, "margin-left") ||
- !strcmp(name, "padding-left"))
- side = 3;
- else
- side = 0;
- side += is_pad ? 4 : 0;
- css_mp_value(buf, cap,
- side == 0 ? st.mt : side == 1 ? st.mr :
- side == 2 ? st.mb : side == 3 ? st.ml :
- side == 4 ? st.pt : side == 5 ? st.pr :
- side == 6 ? st.pb : st.pl,
- st.mp_unit[side], st.mp_calc_px[side]);
- } else if (!strcmp(name, "border-top-width") ||
- !strcmp(name, "border-right-width") ||
- !strcmp(name, "border-bottom-width") ||
- !strcmp(name, "border-left-width")) {
- int side = name[7] == 't' ? 0 : name[7] == 'r' ? 1 :
- name[7] == 'b' ? 2 : 3;
- snprintf(buf, cap, "%dpx", st.border_w[side]);
- } else if (!strcmp(name, "border-top-color") ||
- !strcmp(name, "border-right-color") ||
- !strcmp(name, "border-bottom-color") ||
- !strcmp(name, "border-left-color")) {
- int side = name[7] == 't' ? 0 : name[7] == 'r' ? 1 :
- name[7] == 'b' ? 2 : 3;
- if (st.border_w[side])
- css_rgb(st.border_c[side], buf, cap);
- else
- snprintf(buf, cap, "rgba(0, 0, 0, 0)");
- } else if (!strcmp(name, "background-image")) {
- if (st.bg_image)
- snprintf(buf, cap, "url(\"%s\")",
- br_bg_image_url(st.bg_image - 1));
- else
- snprintf(buf, cap, "none");
- } else if (!strcmp(name, "background-repeat")) {
- static const char *rp[] = {
- "repeat", "no-repeat", "repeat-x", "repeat-y"
- };
- snprintf(buf, cap, "%s", rp[st.bg_repeat < 4 ?
- st.bg_repeat : 0]);
- } else if (!strcmp(name, "background-position")) {
- if (st.bg_pos_x_kind == 1)
- snprintf(buf, cap, "%d%% %d%%", st.bg_pos_x,
- st.bg_pos_y);
- else
- snprintf(buf, cap, "%dpx %dpx", st.bg_pos_x,
- st.bg_pos_y);
- } else if (!strcmp(name, "background-size")) {
- if (st.bg_size_kind == 3)
- snprintf(buf, cap, "cover");
- else if (st.bg_size_kind == 4)
- snprintf(buf, cap, "contain");
- else if (st.bg_size_kind == 1)
- snprintf(buf, cap, "%dpx %dpx", st.bg_size_w,
- st.bg_size_h);
- else if (st.bg_size_kind == 2)
- snprintf(buf, cap, "%d%% %d%%", st.bg_size_w,
- st.bg_size_h);
- else
- snprintf(buf, cap, "auto");
- } else if (!strcmp(name, "transform")) {
- char tmp[128];
- unsigned int n = 0;
- if (!st.transform_set) {
- snprintf(buf, cap, "none");
- } else {
- tmp[0] = '\0';
- if (st.tx || st.ty) {
- snprintf(tmp + n, sizeof(tmp) - n,
- "translate(%dpx, %dpx)", st.tx, st.ty);
- n = (unsigned int)strlen(tmp);
- }
- if (st.transform_scale) {
- snprintf(tmp + n, sizeof(tmp) - n,
- "%sscale(%d.%02d, %d.%02d)",
- n ? " " : "", st.scale_x / 100,
- st.scale_x % 100, st.scale_y / 100,
- st.scale_y % 100);
- n = (unsigned int)strlen(tmp);
- if (st.rotate_x100) {
- snprintf(tmp + n, sizeof(tmp) - n,
- " rotate(%d.%02ddeg)",
- st.rotate_x100 / 100,
- st.rotate_x100 % 100);
- }
- } else if (st.rotate_x100) {
- snprintf(tmp + n, sizeof(tmp) - n,
- "%srotate(%d.%02ddeg)",
- n ? " " : "", st.rotate_x100 / 100,
- st.rotate_x100 % 100);
- }
- snprintf(buf, cap, "%s", tmp[0] ? tmp : "none");
- }
- } else {
- return 0;
- }
- return buf[0] != '\0';
- }
- static int js_host_click(unsigned int node)
- {
- char url[BR_URL_MAX];
- const char *href;
- if (node >= doc.nnode)
- return 0;
- if (form_is_control(node)) {
- form_click(node);
- return 1;
- }
- href = br_href(&doc, node);
- if (resolve_link(page_url, href, url, sizeof(url)) != 0)
- return 0;
- s_copy(pending_url, url, sizeof(pending_url));
- pending_action |= ACT_NAV;
- cancel_fetch = 1;
- ngui_invalidate(&ui);
- return 1;
- }
- static int fetch_script_text(const char *url, unsigned char **out,
- unsigned int *outlen, char *err,
- unsigned int errcap);
- static void script_error(const char *what, const char *url, const char *why)
- {
- char msg[192];
- unsigned int n = 0;
- s_append(msg, &n, what, sizeof(msg));
- if (url && url[0]) {
- s_append(msg, &n, " ", sizeof(msg));
- s_append(msg, &n, url, sizeof(msg));
- }
- if (why && why[0]) {
- s_append(msg, &n, ": ", sizeof(msg));
- s_append(msg, &n, why, sizeof(msg));
- }
- js_host_report(msg);
- }
- /* Run the page's scripts in document order: external <script src> bodies are
- * fetched, inline bodies are copied because MuJS needs NUL-terminated text. */
- static void run_page_scripts(void)
- {
- struct js_browser_host host;
- unsigned int i;
- if (!doc.nscripts)
- return;
- host.status = js_host_status;
- host.report = js_host_report;
- host.navigate = js_host_navigate;
- host.changed = js_host_changed;
- host.rect = js_host_rect;
- host.form_value = js_host_form_value;
- host.form_checked = js_host_form_checked;
- host.form_selected = js_host_form_selected;
- host.form_set_value = js_host_form_set_value;
- host.form_set_checked = js_host_form_set_checked;
- host.form_set_selected = js_host_form_set_selected;
- host.form_elements = js_host_form_elements;
- host.form_submit = js_host_form_submit;
- host.form_reset = js_host_form_reset;
- host.click = js_host_click;
- host.computed = js_host_computed;
- host.page_url = page_url;
- host.width = ui.w;
- host.height = ui.h;
- if (js_browser_start(&doc, &host) != 0) {
- js_host_status("JavaScript: out of memory", 1);
- return;
- }
- for (i = 0; i < doc.nscripts; i++) {
- struct br_script *script = &doc.scripts[i];
- if (script->src_off) {
- const char *src_attr = doc.attrs + script->src_off;
- char url[BR_URL_MAX];
- unsigned char *data;
- unsigned int len;
- char err[64];
- int rv;
- if (!src_attr[0])
- continue;
- if (resolve_link(page_url, src_attr, url,
- sizeof(url)) != 0) {
- script_error("Cannot load script", src_attr,
- "unsupported URL");
- continue;
- }
- set_status("Loading script...", 0, 0);
- rv = fetch_script_text(url, &data, &len, err,
- sizeof(err));
- if (rv == FETCH_ABORT)
- break;
- if (rv != 0) {
- script_error("Cannot load script", url, err);
- continue;
- }
- js_browser_run((const char *)data, url);
- free(data);
- } else {
- unsigned int off = script->off;
- unsigned int len = script->len;
- unsigned int k;
- char *src = (char *)malloc(len + 1);
- if (!src) {
- js_host_status("JavaScript: out of memory", 1);
- break;
- }
- for (k = 0; k < len; k++)
- src[k] = doc.script_src[off + k];
- src[len] = '\0';
- js_browser_run(src, page_url);
- free(src);
- }
- if (pending_action)
- break;
- }
- /* Keep the state alive for setTimeout/ready callbacks; load_page() and
- * load_home() stop it before the next navigation. */
- if (doc.scripts_truncated && !js_last_error)
- js_host_status("Page has more than 32 scripts; extra scripts skipped",
- 1);
- }
- /* ---- images ---- */
- static void image_reset(void)
- {
- int i;
- for (i = 0; i < BR_MAX_IMAGES; i++) {
- images[i].state = 0;
- images[i].native_w = images[i].native_h = 0;
- images[i].err[0] = '\0';
- }
- image_count = 0;
- preload_active = 0;
- preload_pending = 0;
- preload_wait = 0;
- preload_node = 0;
- preload_loaded = 0;
- }
- /* Decode into the shared arena, then copy/scale the result into a display
- * bitmap of at most BR_IMG_LAYOUT_W pixels wide. The arena is reused by the
- * next image, so the per-image cost is its scaled bitmap, not full 4K RGBA
- * pixels. Transparent edges are composited over white. */
- static int decode_image_data(const unsigned char *data, unsigned int len,
- struct br_image *bi)
- {
- struct img_info info;
- struct img_options opt;
- struct img_mem mem;
- struct img_image decoded;
- unsigned char *arena;
- unsigned int est, bytes, i;
- unsigned short *px;
- unsigned char *alpha = 0;
- int dw, dh, r;
- if (img_sniff(data, len, &info) != 0) {
- s_copy(bi->err, "unknown image format", sizeof(bi->err));
- return -1;
- }
- est = img_estimate(&info);
- if (est == 0xFFFFFFFFu || est > BR_IMG_ARENA_MAX) {
- s_copy(bi->err, "image too large", sizeof(bi->err));
- return -1;
- }
- arena = (unsigned char *)malloc(est);
- if (!arena) {
- s_copy(bi->err, "out of memory", sizeof(bi->err));
- return -1;
- }
- img_mem_init(&mem, arena, est);
- opt.max_pixels = BR_IMG_PIX_MAX;
- r = img_decode(data, len, &opt, &mem, &decoded);
- if (r != 0) {
- s_copy(bi->err, img_error_string(r), sizeof(bi->err));
- goto fail;
- }
- if (decoded.w == 0 || decoded.h == 0) {
- s_copy(bi->err, "empty image", sizeof(bi->err));
- goto fail;
- }
- bi->native_w = (int)decoded.w;
- bi->native_h = (int)decoded.h;
- dw = (int)decoded.w;
- dh = (int)decoded.h;
- if (dw > BR_IMG_LAYOUT_W) {
- dh = (int)((unsigned int)decoded.h * BR_IMG_LAYOUT_W /
- decoded.w);
- dw = BR_IMG_LAYOUT_W;
- }
- if (dh < 1)
- dh = 1;
- if (dw < 1)
- dw = 1;
- bytes = (unsigned int)dw * (unsigned int)dh * 2u;
- px = (unsigned short *)malloc(bytes);
- if (!px) {
- s_copy(bi->err, "out of memory", sizeof(bi->err));
- goto fail;
- }
- /* Keep the alpha plane for the scaled image so transparent logos and
- * icons composite over the page instead of the browser's dark theme
- * background. Very large alpha images stay flattened to bound the
- * heap use. */
- if (decoded.alpha && (unsigned int)dw * (unsigned int)dh <= (1u << 20)) {
- alpha = (unsigned char *)malloc((unsigned int)dw *
- (unsigned int)dh);
- if (alpha) {
- for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
- alpha[i] = 255;
- }
- }
- for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
- px[i] = ui.theme.bg;
- {
- struct img_blit b;
- b.dst = px;
- b.dst_w = dw;
- b.dst_h = dh;
- b.clip_x = 0;
- b.clip_y = 0;
- b.clip_w = dw;
- b.clip_h = dh;
- b.x = 0;
- b.y = 0;
- b.scale = ((unsigned int)dw << 16) / decoded.w;
- b.bg = ui.theme.bg;
- b.bg2 = ui.theme.bg;
- b.dst_a = alpha;
- b.flags = alpha ? IMG_BLIT_SAVE_ALPHA : IMG_BLIT_OVER_DST;
- img_draw(&decoded, &b);
- }
- bi->img.format = decoded.format;
- bi->img.w = (unsigned int)dw;
- bi->img.h = (unsigned int)dh;
- bi->img.px = px;
- bi->img.alpha = alpha;
- bi->img.flags = 0;
- bi->state = 1;
- free(arena);
- return 0;
- fail:
- free(arena);
- return -1;
- }
- /* ---- URL / local path handling ---- */
- static int is_http(const char *s)
- {
- return starts_with(s, "http://") || starts_with(s, "https://");
- }
- /* Collapse "/./", "/../" and duplicate slashes in an absolute path. */
- static int normalize_abs(const char *in, char *out, unsigned int cap)
- {
- char seg[64];
- unsigned int i = 0, o = 1;
- int depth = 0;
- int trailing;
- if (!in || in[0] != '/' || cap < 2)
- return -1;
- trailing = 0;
- {
- unsigned int n = s_len(in);
- if (n > 1 && in[n - 1] == '/')
- trailing = 1;
- }
- out[0] = '/';
- while (in[i]) {
- unsigned int n = 0;
- while (in[i] == '/')
- i++;
- while (in[i] && in[i] != '/' && in[i] != '?' && in[i] != '#') {
- if (n + 1 < sizeof(seg))
- seg[n++] = in[i];
- i++;
- }
- seg[n] = '\0';
- if (n == 0)
- break;
- if (s_eq(seg, "."))
- continue;
- if (s_eq(seg, "..")) {
- if (depth > 0) {
- while (o > 1 && out[o - 1] != '/')
- o--;
- if (o > 1)
- o--;
- depth--;
- }
- continue;
- }
- if (o > 1) {
- if (o + 1 >= cap)
- return -1;
- out[o++] = '/';
- }
- if (o + n + 1 >= cap)
- return -1;
- {
- unsigned int j;
- for (j = 0; j < n; j++)
- out[o++] = seg[j];
- }
- depth++;
- }
- if (trailing && o > 1 && in[i] != '?' && in[i] != '#') {
- if (o + 1 >= cap)
- return -1;
- out[o++] = '/';
- }
- /* Keep the query/fragment suffix for form GET and in-page anchors. */
- if (in[i] == '?' || in[i] == '#') {
- while (in[i]) {
- if (o + 1 >= cap)
- return -1;
- out[o++] = in[i++];
- }
- }
- out[o] = '\0';
- return 0;
- }
- static void make_abs_local(const char *in, char *out, unsigned int cap)
- {
- char joined[BR_URL_MAX];
- if (!in || !in[0]) {
- s_copy(out, cwd[0] ? cwd : "/", cap);
- return;
- }
- if (in[0] == '/') {
- if (normalize_abs(in, out, cap) != 0)
- s_copy(out, in, cap);
- return;
- }
- {
- unsigned int n = 0;
- s_append(joined, &n, cwd[0] ? cwd : "/", sizeof(joined));
- s_append(joined, &n, "/", sizeof(joined));
- s_append(joined, &n, in, sizeof(joined));
- }
- if (normalize_abs(joined, out, cap) != 0)
- s_copy(out, joined, cap);
- }
- /* ---- canonical origin cache ---- *
- *
- * Sites commonly redirect http://host to https://host and then to
- * https://www.host. Remember the final scheme/host of the last navigation so
- * a bare domain typed again goes straight there instead of paying for two
- * more connections and TLS handshakes. Only default-port requests are
- * rewritten; explicit ports are left alone. */
- #define ORIGIN_CACHE_MAX 8
- #define ORIGIN_FILE "/share/borigins"
- #define ORIGIN_BUF_SIZE (ORIGIN_CACHE_MAX * (UHTTP_HOST_MAX + 16 + \
- UHTTP_HOST_MAX + 2))
- struct origin_cache_entry {
- char host[UHTTP_HOST_MAX];
- char origin[UHTTP_HOST_MAX + 16];
- };
- static struct origin_cache_entry origins[ORIGIN_CACHE_MAX];
- static int origin_count;
- static char origin_buf[ORIGIN_BUF_SIZE];
- static int default_port(const struct uhttp_url *u)
- {
- return u->secure ? u->port == 443 : u->port == 80;
- }
- static void url_origin(const struct uhttp_url *u, char *out,
- unsigned int cap)
- {
- unsigned int n = 0;
- s_append(out, &n, u->secure ? "https://" : "http://", cap);
- s_append(out, &n, u->host, cap);
- if (!default_port(u)) {
- s_append(out, &n, ":", cap);
- s_append_dec(out, &n, u->port, cap);
- }
- }
- static void origin_cache_apply(char *url, unsigned int cap)
- {
- struct uhttp_url u;
- char out[BR_URL_MAX];
- unsigned int n = 0;
- int i;
- if (uhttp_parse_url(url, &u) != 0 || !default_port(&u))
- return;
- for (i = 0; i < origin_count; i++) {
- if (!s_eq(u.host, origins[i].host))
- continue;
- s_append(out, &n, origins[i].origin, sizeof(out));
- s_append(out, &n, u.path, sizeof(out));
- s_copy(url, out, cap);
- return;
- }
- }
- static void origin_cache_save(void)
- {
- unsigned int n = 0;
- int i;
- for (i = 0; i < origin_count; i++) {
- s_append(origin_buf, &n, origins[i].host, sizeof(origin_buf));
- s_append(origin_buf, &n, " ", sizeof(origin_buf));
- s_append(origin_buf, &n, origins[i].origin, sizeof(origin_buf));
- s_append(origin_buf, &n, "\n", sizeof(origin_buf));
- }
- if (n)
- u_syscall3(SYS_WRITE_FILE, (int)ORIGIN_FILE,
- (int)origin_buf, (int)n);
- }
- static void origin_cache_load(void)
- {
- char *p;
- int rn;
- rn = u_syscall3(SYS_READ_FILE, (int)ORIGIN_FILE, (int)origin_buf,
- (int)(sizeof(origin_buf) - 1));
- if (rn <= 0)
- return;
- origin_buf[rn] = '\0';
- p = origin_buf;
- while (*p && origin_count < ORIGIN_CACHE_MAX) {
- char *line = p, *origin;
- while (*p && *p != '\n')
- p++;
- if (*p)
- *p++ = '\0';
- if (!line[0])
- continue;
- origin = line;
- while (*origin && *origin != ' ')
- origin++;
- if (!*origin)
- continue;
- *origin++ = '\0';
- if (!line[0] || !is_http(origin))
- continue;
- {
- struct uhttp_url uo;
- if (uhttp_parse_url(origin, &uo) != 0 ||
- !s_eq(uo.path, "/"))
- continue;
- }
- s_copy(origins[origin_count].host, line,
- sizeof(origins[origin_count].host));
- s_copy(origins[origin_count].origin, origin,
- sizeof(origins[origin_count].origin));
- origin_count++;
- }
- }
- static int host_is_www_of(const char *host, const char *base)
- {
- return host[0] == 'w' && host[1] == 'w' && host[2] == 'w' &&
- host[3] == '.' && s_eq(host + 4, base);
- }
- static void origin_cache_learn(const char *requested, const char *final)
- {
- struct uhttp_url from, to;
- char origin[UHTTP_HOST_MAX + 16];
- int i;
- if (!is_http(requested) || !is_http(final))
- return;
- if (uhttp_parse_url(requested, &from) != 0 ||
- uhttp_parse_url(final, &to) != 0)
- return;
- if (!default_port(&from))
- return;
- if (s_eq(from.host, to.host) && from.secure == to.secure &&
- from.port == to.port)
- return;
- /* Only remember origin-level canonicalisation: the same path, and
- * either the same host or its www./apex sibling. A path- or
- * host-specific redirect (a shortener, a login page) must not poison
- * every request to the typed host. */
- if (!s_eq(from.path, to.path))
- return;
- if (!s_eq(from.host, to.host) &&
- !host_is_www_of(to.host, from.host) &&
- !host_is_www_of(from.host, to.host))
- return;
- url_origin(&to, origin, sizeof(origin));
- for (i = 0; i < origin_count; i++) {
- if (s_eq(origins[i].host, from.host)) {
- if (!s_eq(origins[i].origin, origin)) {
- s_copy(origins[i].origin, origin,
- sizeof(origins[i].origin));
- origin_cache_save();
- }
- return;
- }
- }
- i = origin_count < ORIGIN_CACHE_MAX ? origin_count++ : 0;
- s_copy(origins[i].host, from.host, sizeof(origins[i].host));
- s_copy(origins[i].origin, origin, sizeof(origins[i].origin));
- origin_cache_save();
- }
- static int path_dir(const char *path, char *out, unsigned int cap)
- {
- unsigned int i, last = 0;
- for (i = 0; path[i]; i++)
- if (path[i] == '/')
- last = i;
- if (last == 0) {
- if (cap < 2)
- return -1;
- out[0] = '/';
- out[1] = '\0';
- return 0;
- }
- if (last + 1 >= cap)
- return -1;
- {
- unsigned int j;
- for (j = 0; j < last; j++)
- out[j] = path[j];
- out[last] = '\0';
- }
- return 0;
- }
- /* Resolve an href against the current page. 0 = ok, -1 = ignore. */
- static int resolve_link(const char *base, const char *href, char *out,
- unsigned int cap)
- {
- char joined[BR_URL_MAX];
- if (!href || !href[0] || href[0] == '#')
- return -1;
- if (starts_with(href, "mailto:") || starts_with(href, "javascript:") ||
- starts_with(href, "data:") || starts_with(href, "tel:"))
- return -1;
- if (starts_with(href, "about:")) {
- s_copy(out, href, cap);
- return 0;
- }
- if (is_http(href)) {
- s_copy(out, href, cap);
- return 0;
- }
- if (starts_with(href, "//")) {
- if (is_http(base)) {
- unsigned int n = 0;
- s_append(out, &n, starts_with(base, "https:") ?
- "https:" : "http:", cap);
- s_append(out, &n, href, cap);
- return 0;
- }
- return -1;
- }
- if (is_http(base)) {
- return uhttp_resolve(base, href, out, cap) == 0 ? 0 : -1;
- }
- /* local page */
- if (href[0] == '/') {
- if (normalize_abs(href, out, cap) != 0)
- return -1;
- return 0;
- }
- {
- char dir[BR_URL_MAX];
- unsigned int n = 0;
- if (s_eq(base, "about:home"))
- s_copy(dir, cwd[0] ? cwd : "/", sizeof(dir));
- else if (path_dir(base, dir, sizeof(dir)) != 0)
- s_copy(dir, "/", sizeof(dir));
- s_append(joined, &n, dir, sizeof(joined));
- if (n == 0 || joined[n - 1] != '/')
- s_append(joined, &n, "/", sizeof(joined));
- s_append(joined, &n, href, sizeof(joined));
- }
- return normalize_abs(joined, out, cap) == 0 ? 0 : -1;
- }
- /* Best-effort decompression: keep the first `cap` bytes and discard the
- * rest instead of failing when a page or stylesheet is larger than the
- * buffer. The stream is still walked to the end, so a normal page is
- * verified; a truncated compressed input still returns whatever was decoded
- * so far. */
- struct trunc_buf {
- unsigned char *dst;
- unsigned int cap, pos;
- int full;
- };
- static int trunc_write(void *ctx, const unsigned char *data, unsigned int len)
- {
- struct trunc_buf *m = (struct trunc_buf *)ctx;
- if (len > m->cap - m->pos) {
- unsigned int room = m->cap - m->pos;
- if (room) {
- unsigned int i;
- for (i = 0; i < room; i++)
- m->dst[m->pos + i] = data[i];
- m->pos += room;
- }
- m->full = 1;
- return 0;
- }
- {
- unsigned int i;
- for (i = 0; i < len; i++)
- m->dst[m->pos + i] = data[i];
- }
- m->pos += len;
- return 0;
- }
- static void worker_wait_ui(void);
- static void inflate_ui_pump(void *ctx)
- {
- static unsigned int last;
- unsigned int now = u_ticks_ms();
- (void)ctx;
- if ((int)(now - last) >= 50) {
- last = now;
- worker_wait_ui();
- }
- }
- static int inflate_trunc(const unsigned char *src, unsigned int len,
- unsigned char *dst, unsigned int cap,
- unsigned int *outlen, int gzip, int *truncated)
- {
- struct inflate_src in;
- struct inflate_sink sink;
- struct trunc_buf m;
- unsigned int got = 0;
- int r;
- in.p = src;
- in.len = len;
- in.more = 0;
- in.ctx = 0;
- m.dst = dst;
- m.cap = cap;
- m.pos = 0;
- m.full = 0;
- sink.write = trunc_write;
- sink.ctx = &m;
- inflate_init(&image_inflate_ws, &in, &sink);
- image_inflate_ws.progress = inflate_ui_pump;
- image_inflate_ws.progress_ctx = 0;
- r = gzip ? inflate_gzip(&image_inflate_ws, &got) :
- inflate_zlib(&image_inflate_ws, &got);
- *outlen = m.pos;
- *truncated = m.full || r != 0;
- if (r != 0 && m.pos == 0)
- return -1;
- return 0;
- }
- /* ---- headless fetch helper ------------------------------------------- *
- *
- * Network I/O runs in a separate ring-3 process (`curl __fetch`): it writes
- * the raw response and a small metadata file under /share. While it works,
- * the browser keeps drawing and scrolling its own window, so a slow DNS/TLS
- * handshake or a multi-megabyte body never freezes the UI.
- */
- #define FETCH_REQ "/share/bfreq"
- #define FETCH_BODY "/share/bfbody"
- #define FETCH_META "/share/bfmeta"
- #define FETCH_DONE "/share/bfdone"
- #define FETCH_POST "/share/bfpost"
- struct fetch_meta {
- int status;
- int error;
- int trunc;
- unsigned int len;
- char encoding[16];
- char ctype[64];
- char final[BR_URL_MAX];
- };
- static void draw(unsigned int *action);
- static void handle_keys(unsigned int *action);
- static void handle_page_click(unsigned int *action);
- static int fetch_helper_tty = -2;
- static int fetch_helper_pid = -1;
- static unsigned int fetch_seq;
- static struct fetch_meta fetch_meta;
- static unsigned int meta_uint(char **pp)
- {
- char *p = *pp;
- unsigned int v = 0;
- while (*p >= '0' && *p <= '9') {
- v = v * 10 + (unsigned int)(*p - '0');
- p++;
- }
- while (*p && *p != '\n')
- p++;
- if (*p == '\n')
- p++;
- *pp = p;
- return v;
- }
- static void meta_str(char **pp, char *out, unsigned int cap)
- {
- char *p = *pp;
- unsigned int n = 0;
- while (*p && *p != '\n' && n + 1 < cap)
- out[n++] = *p++;
- out[n] = '\0';
- while (*p && *p != '\n')
- p++;
- if (*p == '\n')
- p++;
- *pp = p;
- }
- static void worker_wait_ui(void)
- {
- unsigned int action = 0;
- if (!ngui_begin(&ui))
- return;
- draw(&action);
- handle_keys(&action);
- handle_page_click(&action);
- if (action & ACT_WAIT_NAV) {
- pending_action |= action & ACT_WAIT_NAV;
- if ((action & ACT_NAV) && click_url[0])
- s_copy(pending_url, click_url, sizeof(pending_url));
- click_url[0] = '\0';
- cancel_fetch = 1;
- }
- if (!ngui_focus(&ui) && ngui_scroll_keys(&ui, &page_scroll))
- ngui_invalidate(&ui);
- ngui_end(&ui);
- }
- static void fetch_helper_stop(void)
- {
- int i;
- if (fetch_helper_pid > 0) {
- u_kill((unsigned int)fetch_helper_pid);
- for (i = 0; i < 40 && u_alive((unsigned int)fetch_helper_pid); i++)
- u_sleep_ms(5);
- /* Let the reaper release the NIC before the next request. */
- u_sleep_ms(10);
- }
- fetch_helper_pid = -1;
- }
- static int fetch_helper_start(char *err, unsigned int errcap)
- {
- if (fetch_helper_pid > 0 &&
- u_alive((unsigned int)fetch_helper_pid))
- return 0;
- fetch_helper_pid = -1;
- if (fetch_helper_tty == -2) {
- int t = u_tty_create();
- fetch_helper_tty = t > 0 ? t : 0;
- }
- fetch_helper_pid = u_spawn("/ramfs/bin/curl", "__serve",
- fetch_helper_tty, "/");
- if (fetch_helper_pid <= 0) {
- fetch_helper_pid = -1;
- s_copy(err, "cannot start network helper", errcap);
- return -1;
- }
- return 0;
- }
- static int worker_fetch(const char *url, unsigned int cap,
- const char *post, unsigned int post_len,
- struct fetch_meta *m, char *err, unsigned int errcap)
- {
- static char req[768], meta[1024], done[24];
- char *p;
- unsigned int n = 0, deadline;
- int rn, mseq, retried = 0;
- struct sys_stat st;
- if (cancel_fetch || pending_action)
- return FETCH_ABORT;
- if (u_stat("/share", &st) != 0 || !st.is_dir ||
- u_stat("/ramfs/bin/curl", &st) != 0) {
- s_copy(err, "network helper unavailable", errcap);
- return -1;
- }
- fetch_seq++;
- if (!fetch_seq)
- fetch_seq = 1;
- s_append_dec(req, &n, fetch_seq, sizeof(req));
- s_append(req, &n, " ", sizeof(req));
- s_append_dec(req, &n, cap, sizeof(req));
- s_append(req, &n, " ", sizeof(req));
- if (post_len) {
- s_append(req, &n, "POST ", sizeof(req));
- s_append_dec(req, &n, post_len, sizeof(req));
- s_append(req, &n, " ", sizeof(req));
- }
- s_append(req, &n, url, sizeof(req));
- s_append(req, &n, "\n", sizeof(req));
- if (post_len && u_syscall3(SYS_WRITE_FILE, (int)FETCH_POST,
- (int)post, (int)post_len) < 0) {
- s_copy(err, "cannot queue form body", errcap);
- return -1;
- }
- u_syscall1(SYS_UNLINK, (int)FETCH_DONE);
- u_syscall1(SYS_UNLINK, (int)FETCH_META);
- if (u_syscall3(SYS_WRITE_FILE, (int)FETCH_REQ, (int)req, (int)n) < 0) {
- s_copy(err, "cannot queue network request", errcap);
- return -1;
- }
- if (fetch_helper_start(err, errcap) != 0)
- return -1;
- deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
- for (;;) {
- worker_wait_ui();
- if (cancel_fetch || pending_action) {
- fetch_helper_stop();
- return FETCH_ABORT;
- }
- rn = u_syscall3(SYS_READ_FILE, (int)FETCH_DONE, (int)done,
- (int)(sizeof(done) - 1));
- if (rn > 0) {
- done[rn] = '\0';
- p = done;
- mseq = (int)meta_uint(&p);
- if (mseq == (int)fetch_seq)
- break;
- }
- if (!u_alive((unsigned int)fetch_helper_pid)) {
- fetch_helper_pid = -1;
- if (!retried && fetch_helper_start(err, errcap) == 0) {
- retried = 1;
- continue;
- }
- s_copy(err, "network helper stopped", errcap);
- return -1;
- }
- if ((int)(u_ticks_ms() - deadline) >= 0) {
- fetch_helper_stop();
- s_copy(err, "network request timed out", errcap);
- return -1;
- }
- u_sleep_ms(20);
- }
- if (cancel_fetch || pending_action)
- return FETCH_ABORT;
- rn = u_syscall3(SYS_READ_FILE, (int)FETCH_META, (int)meta,
- (int)(sizeof(meta) - 1));
- if (rn <= 0) {
- s_copy(err, "network helper failed", errcap);
- return -1;
- }
- meta[rn] = '\0';
- p = meta;
- mseq = (int)meta_uint(&p);
- if (mseq != (int)fetch_seq) {
- s_copy(err, "stale network response", errcap);
- return -1;
- }
- m->status = (int)meta_uint(&p);
- m->error = (int)meta_uint(&p);
- m->trunc = (int)meta_uint(&p);
- meta_str(&p, m->encoding, sizeof(m->encoding));
- meta_str(&p, m->ctype, sizeof(m->ctype));
- meta_str(&p, m->final, sizeof(m->final));
- m->len = meta_uint(&p);
- if (m->error) {
- s_copy(err, uhttp_error_string(m->error), errcap);
- return -1;
- }
- return 0;
- }
- static int fetch_remote(const char *url, unsigned int cap,
- const char *post, unsigned int post_len,
- unsigned char *plain, unsigned int plaincap,
- unsigned char *comp, unsigned int compcap,
- struct fetch_meta *m, int *truncated,
- char *err, unsigned int errcap)
- {
- int rv, zr, was_trunc;
- rv = worker_fetch(url, cap, post, post_len, m, err, errcap);
- if (rv == FETCH_ABORT)
- return FETCH_ABORT;
- if (rv != 0)
- return -1;
- if (contains_ci(m->encoding, "gzip") ||
- contains_ci(m->encoding, "deflate")) {
- unsigned int comp_len = 0, plen = 0;
- rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)comp,
- (int)compcap);
- if (rv < 0) {
- s_copy(err, "empty response", errcap);
- return -1;
- }
- comp_len = (unsigned int)rv;
- if (comp_len < m->len) {
- s_copy(err, "compressed response is too large", errcap);
- return -1;
- }
- if (contains_ci(m->encoding, "deflate"))
- zr = inflate_trunc(comp, comp_len, plain, plaincap,
- &plen, 0, &was_trunc);
- else
- zr = inflate_trunc(comp, comp_len, plain, plaincap,
- &plen, 1, &was_trunc);
- if (zr != 0) {
- s_copy(err, "compressed response is corrupt", errcap);
- return -1;
- }
- *truncated = m->trunc || was_trunc;
- return (cancel_fetch || pending_action) ? FETCH_ABORT :
- (int)plen;
- }
- rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)plain,
- (int)plaincap);
- if (rv < 0) {
- s_copy(err, "empty response", errcap);
- return -1;
- }
- *truncated = m->trunc || ((unsigned int)rv < m->len);
- return (cancel_fetch || pending_action) ? FETCH_ABORT : rv;
- }
- /* ---- HTTP / file fetch ---- */
- static int read_url(const char *url, unsigned int cap, unsigned char **out,
- unsigned int *outlen, int *is_local, char *ctype,
- unsigned int ctcap, char *final, unsigned int finalcap,
- char *err, unsigned int errcap, int *truncated,
- const char *post, unsigned int post_len)
- {
- unsigned char *buf;
- unsigned int len;
- int tr = 0;
- int rv;
- *out = 0;
- *outlen = 0;
- *truncated = 0;
- if (final && finalcap)
- final[0] = '\0';
- if (ctype && ctcap)
- ctype[0] = '\0';
- if (url[0] == '/') {
- char pathbuf[BR_URL_MAX];
- const char *open_url = url;
- unsigned int i = 0;
- /* A local form GET keeps ?query in the address bar but the
- * file lookup uses the path alone, like a real browser. */
- while (url[i] && url[i] != '?' && url[i] != '#')
- i++;
- if (url[i]) {
- s_copy(pathbuf, url, i + 1);
- open_url = pathbuf;
- }
- buf = (unsigned char *)malloc(cap);
- if (!buf) {
- s_copy(err, "out of memory", errcap);
- return -1;
- }
- rv = u_syscall3(SYS_READ_FILE, (int)open_url, (int)buf, (int)cap);
- if (rv < 0) {
- free(buf);
- s_copy(err, "file not found", errcap);
- return -1;
- }
- *out = buf;
- *outlen = (unsigned int)rv;
- *is_local = 1;
- if (ctype)
- s_copy(ctype, "text/html", ctcap);
- if (final)
- s_copy(final, url, finalcap);
- if ((unsigned int)rv == cap)
- *truncated = 1;
- return 0;
- }
- buf = (unsigned char *)malloc(cap);
- if (!buf) {
- s_copy(err, "out of memory", errcap);
- return -1;
- }
- rv = fetch_remote(url, cap, post, post_len, buf, cap, image_io_buf,
- BR_PAGE_COMP_MAX, &fetch_meta, &tr, err, errcap);
- if (rv == FETCH_ABORT) {
- free(buf);
- return FETCH_ABORT;
- }
- if (rv < 0) {
- free(buf);
- return -1;
- }
- len = (unsigned int)rv;
- if (len == 0) {
- free(buf);
- s_copy(err, "empty response", errcap);
- return -1;
- }
- if (ctype)
- s_copy(ctype, fetch_meta.ctype, ctcap);
- if (final && fetch_meta.final[0])
- s_copy(final, fetch_meta.final, finalcap);
- *is_local = 0;
- *out = buf;
- *outlen = len;
- *truncated = tr;
- return 0;
- }
- /* Load every @font-face the parser registered. Local files go through
- * ufont_face_open_path(); remote files use the fetch helper (identity, the
- * TTF parser has no gzip). A failed face stays NULL: layout then falls back
- * to the built-in regular/bold/italic/mono set. */
- static void load_font_faces(void)
- {
- unsigned int i;
- memset(font_faces, 0, sizeof(font_faces));
- for (i = 1; i <= (unsigned int)br_font_face_count() &&
- i <= BR_MAX_FACES; i++) {
- const char *url = br_font_face_url((int)i);
- const char *base = br_font_face_base((int)i);
- char abs[BR_URL_MAX];
- if (!url || !url[0])
- continue;
- if (resolve_link(base && base[0] ? base : page_url, url, abs,
- sizeof(abs)) != 0)
- continue;
- if (abs[0] == '/') {
- font_faces[i - 1] = ufont_face_open_path(abs);
- } else if (is_http(abs)) {
- unsigned char *buf;
- char err[64];
- int rv, tr = 0;
- buf = (unsigned char *)malloc(UFONT_MAX_FILE);
- if (!buf)
- continue;
- rv = fetch_remote(abs, UFONT_MAX_FILE, 0, 0, buf,
- UFONT_MAX_FILE, image_io_buf,
- BR_PAGE_COMP_MAX, &fetch_meta, &tr,
- err, sizeof(err));
- if (rv > 0)
- font_faces[i - 1] = ufont_face_open_mem(buf,
- rv);
- free(buf);
- }
- }
- }
- /* Remote scripts are cached under /share for the current boot, like the
- * stylesheet cache, so reloads and later visits skip the network. */
- static int script_cache_count;
- static unsigned int script_cache_hash(const char *s)
- {
- unsigned int h = 2166136261u;
- while (*s) {
- h ^= (unsigned char)*s++;
- h *= 16777619u;
- }
- return h;
- }
- static void script_cache_path(const char *url, char *out, unsigned int cap)
- {
- unsigned int n = 0;
- s_append(out, &n, "/share/" SCRIPT_CACHE_PREFIX, cap);
- s_append_dec(out, &n, script_cache_hash(url), cap);
- }
- static void script_cache_clear(void)
- {
- struct sys_dirent d;
- char path[96];
- for (;;) {
- int i, found = 0;
- for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
- (int)&d) == 1; i++) {
- unsigned int n = 0;
- if (!starts_with(d.name, SCRIPT_CACHE_PREFIX))
- continue;
- s_append(path, &n, "/share/", sizeof(path));
- s_append(path, &n, d.name, sizeof(path));
- if (u_syscall1(SYS_UNLINK, (int)path) < 0)
- return;
- found = 1;
- break;
- }
- if (!found)
- break;
- }
- script_cache_count = 0;
- }
- static void script_cache_init(void)
- {
- struct sys_dirent d;
- int i, count = 0;
- for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
- (int)&d) == 1; i++)
- if (starts_with(d.name, SCRIPT_CACHE_PREFIX))
- count++;
- if (count >= SCRIPT_CACHE_MAX) {
- script_cache_clear();
- return;
- }
- script_cache_count = count;
- }
- static int script_cache_get(const char *url, unsigned char **out,
- unsigned int *outlen)
- {
- char path[96];
- struct sys_stat st;
- unsigned char *buf;
- int rn;
- script_cache_path(url, path, sizeof(path));
- if (u_stat(path, &st) != 0 || st.is_dir || st.size == 0 ||
- st.size >= SCRIPT_CACHE_FILE_MAX)
- return 0;
- buf = (unsigned char *)malloc(st.size + 1);
- if (!buf)
- return 0;
- rn = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)st.size);
- if (rn != (int)st.size) {
- free(buf);
- return 0;
- }
- buf[st.size] = '\0';
- *out = buf;
- *outlen = st.size;
- return 1;
- }
- static void script_cache_put(const char *url, const unsigned char *data,
- unsigned int len)
- {
- char path[96];
- if (script_cache_count >= SCRIPT_CACHE_MAX || len == 0 ||
- len >= SCRIPT_CACHE_FILE_MAX)
- return;
- script_cache_path(url, path, sizeof(path));
- if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)data,
- (int)len) >= 0)
- script_cache_count++;
- }
- /* Fetch one external script into a heap buffer and NUL-terminate it. */
- static int fetch_script_text(const char *url, unsigned char **out,
- unsigned int *outlen, char *err,
- unsigned int errcap)
- {
- unsigned char *buf;
- unsigned int len;
- int local, truncated = 0;
- char ctype[64], final[BR_URL_MAX];
- int rv;
- *out = 0;
- *outlen = 0;
- if (url[0] != '/' && script_cache_get(url, out, outlen))
- return 0;
- rv = read_url(url, BR_SCRIPT_MAX, &buf, &len, &local, ctype,
- sizeof(ctype), final, sizeof(final), err, errcap,
- &truncated, 0, 0);
- if (rv != 0)
- return rv;
- if (truncated || len >= BR_SCRIPT_MAX) {
- free(buf);
- s_copy(err, "script is too large", errcap);
- return -1;
- }
- buf[len] = '\0';
- if (!local)
- script_cache_put(url, buf, len);
- *out = buf;
- *outlen = len;
- return 0;
- }
- /* Fetch an image into the fixed scratch buffers. The scaled bitmaps live on
- * the heap, so images must not allocate their file body from it as well. */
- static int fetch_image(const char *url, unsigned int cap,
- unsigned char **out, unsigned int *outlen,
- char *err, unsigned int errcap)
- {
- unsigned int len;
- int tr = 0;
- int rv;
- *out = 0;
- *outlen = 0;
- if (cap > BR_IMG_FILE_MAX)
- cap = BR_IMG_FILE_MAX;
- if (url[0] == '/') {
- rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_file_buf,
- (int)cap);
- if (rv < 0) {
- s_copy(err, "file not found", errcap);
- return -1;
- }
- *out = image_file_buf;
- *outlen = (unsigned int)rv;
- return 0;
- }
- rv = fetch_remote(url, cap, 0, 0, image_file_buf, cap, image_comp_buf,
- BR_IMG_FILE_MAX, &fetch_meta, &tr, err, errcap);
- if (rv == FETCH_ABORT)
- return FETCH_ABORT;
- if (rv < 0)
- return -1;
- len = (unsigned int)rv;
- if (len == 0) {
- s_copy(err, "empty image", errcap);
- return -1;
- }
- *out = image_file_buf;
- *outlen = len;
- return 0;
- }
- /* Remote stylesheets are immutable enough for the current boot: keep
- * successful responses under /share so a reload, a second visit or a fresh
- * browser launch does not fetch the same five sheets over the network again.
- * /share is cleared by reboot/kexec; when the cache fills up it is reset. */
- static int css_cache_count;
- static unsigned int css_cache_hash(const char *s)
- {
- unsigned int h = 2166136261u;
- while (*s) {
- h ^= (unsigned char)*s++;
- h *= 16777619u;
- }
- return h;
- }
- static void css_cache_path(const char *url, char *out, unsigned int cap)
- {
- unsigned int n = 0;
- s_append(out, &n, "/share/" CSS_CACHE_PREFIX, cap);
- s_append_dec(out, &n, css_cache_hash(url), cap);
- }
- static void css_cache_clear(void)
- {
- struct sys_dirent d;
- char path[96];
- for (;;) {
- int i, found = 0;
- for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
- (int)&d) == 1; i++) {
- unsigned int n = 0;
- if (!starts_with(d.name, CSS_CACHE_PREFIX))
- continue;
- s_append(path, &n, "/share/", sizeof(path));
- s_append(path, &n, d.name, sizeof(path));
- if (u_syscall1(SYS_UNLINK, (int)path) < 0)
- return;
- found = 1;
- break;
- }
- if (!found)
- break;
- }
- css_cache_count = 0;
- }
- static void css_cache_init(void)
- {
- struct sys_dirent d;
- int i, count = 0;
- for (i = 0; u_syscall3(SYS_LIST_DIR, (int)"/share", i,
- (int)&d) == 1; i++)
- if (starts_with(d.name, CSS_CACHE_PREFIX))
- count++;
- if (count >= CSS_CACHE_MAX) {
- css_cache_clear();
- return;
- }
- css_cache_count = count;
- }
- static int css_cache_get(const char *url, unsigned char **out,
- unsigned int *outlen)
- {
- char path[96];
- int rn;
- css_cache_path(url, path, sizeof(path));
- rn = u_syscall3(SYS_READ_FILE, (int)path, (int)image_io_buf,
- (int)CSS_CACHE_FILE_MAX);
- if (rn <= 0 || (unsigned int)rn >= CSS_CACHE_FILE_MAX)
- return 0;
- *out = image_io_buf;
- *outlen = (unsigned int)rn;
- return 1;
- }
- static void css_cache_put(const char *url, const unsigned char *data,
- unsigned int len)
- {
- char path[96];
- if (css_cache_count >= CSS_CACHE_MAX || len == 0 ||
- len >= CSS_CACHE_FILE_MAX)
- return;
- css_cache_path(url, path, sizeof(path));
- if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)data,
- (int)len) >= 0)
- css_cache_count++;
- }
- /* Read one stylesheet into the shared static buffer. CSS is parsed
- * immediately, so it does not need the page heap; this leaves the heap for
- * the 2 MB page body, the rules and the scaled image bitmaps. */
- static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
- char *err, unsigned int errcap)
- {
- unsigned int len;
- int tr = 0;
- int rv;
- *out = 0;
- *outlen = 0;
- if (url[0] == '/') {
- rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_io_buf,
- (int)BR_CSS_MAX);
- if (rv < 0) {
- s_copy(err, "stylesheet not found", errcap);
- return -1;
- }
- *out = image_io_buf;
- *outlen = (unsigned int)rv;
- return 0;
- }
- if (css_cache_get(url, out, outlen))
- return 0;
- rv = fetch_remote(url, BR_CSS_MAX, 0, 0, image_io_buf, BR_CSS_MAX,
- image_io_buf + BR_CSS_MAX, BR_CSS_COMP_MAX,
- &fetch_meta, &tr, err, errcap);
- if (rv == FETCH_ABORT)
- return FETCH_ABORT;
- if (rv < 0)
- return -1;
- len = (unsigned int)rv;
- if (len == 0) {
- s_copy(err, "empty stylesheet", errcap);
- return -1;
- }
- *out = image_io_buf;
- *outlen = len;
- css_cache_put(url, image_io_buf, len);
- return 0;
- }
- static void redraw_now(void);
- static char error_page[1024];
- static void show_error(const char *title, const char *detail)
- {
- char *page = error_page;
- unsigned int n = 0;
- s_append(page, &n, "<html><body><h2>", sizeof(error_page));
- s_append(page, &n, title, sizeof(error_page));
- s_append(page, &n, "</h2><p>", sizeof(error_page));
- {
- const char *p = detail;
- while (*p && n + 1 < sizeof(error_page)) {
- if (*p == '<') s_append(page, &n, "<", sizeof(error_page));
- else if (*p == '>') s_append(page, &n, ">", sizeof(error_page));
- else if (*p == '&') s_append(page, &n, "&", sizeof(error_page));
- else page[n++] = *p;
- p++;
- }
- }
- page[n] = '\0';
- s_append(page, &n, "</p><p>", sizeof(error_page));
- s_append(page, &n, page_url, sizeof(error_page));
- s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
- sizeof(error_page));
- br_parse(&doc, page, n);
- layout_valid = 0;
- preload_active = 0;
- preload_pending = 0;
- preload_wait = 0;
- s_copy(page_title, title, sizeof(page_title));
- page_loading = 0;
- set_status(title, 1, 0);
- ngui_invalidate(&ui);
- }
- static int decode_direct_image(const unsigned char *data, unsigned int len,
- unsigned int node)
- {
- if (image_count >= BR_MAX_IMAGES)
- return -1;
- if (decode_image_data(data, len, &images[0]) != 0)
- return -1;
- doc.nodes[node].img_index = 0;
- image_count = 1;
- return 0;
- }
- static int ancestor_is(unsigned int node, unsigned int tag)
- {
- unsigned int p = doc.nodes[node].parent;
- while (p) {
- if (doc.nodes[p].tag == tag)
- return 1;
- p = doc.nodes[p].parent;
- }
- return 0;
- }
- static int load_stylesheets(void)
- {
- static char seen[BR_MAX_SHEETS][BR_URL_MAX];
- unsigned int i, sheets = 0, nseen = 0;
- for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) {
- struct br_node *nd = &doc.nodes[i];
- const char *rel, *href;
- char url[BR_URL_MAX];
- unsigned char *data;
- unsigned int len;
- char err[64];
- int rv;
- if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK)
- continue;
- rel = br_rel(&doc, i);
- if (!contains_ci(rel, "stylesheet"))
- continue;
- href = br_href(&doc, i);
- if (!href[0])
- continue;
- if (resolve_link(page_url, href, url, sizeof(url)) != 0)
- continue;
- {
- unsigned int k;
- int dup = 0;
- for (k = 0; k < nseen; k++)
- if (s_eq(seen[k], url)) {
- dup = 1;
- break;
- }
- if (dup)
- continue;
- if (nseen < BR_MAX_SHEETS) {
- s_copy(seen[nseen], url, BR_URL_MAX);
- nseen++;
- }
- }
- rv = fetch_css(url, &data, &len, err, sizeof(err));
- if (rv == FETCH_ABORT)
- return FETCH_ABORT;
- if (rv != 0)
- continue;
- if (len) {
- br_set_font_base(url);
- br_parse_css_text(&doc, (const char *)data, len);
- }
- sheets++;
- }
- br_set_font_base(page_url);
- br_parse_inline_css(&doc);
- load_font_faces();
- br_reset_styles(&doc);
- layout_valid = 0;
- return 0;
- }
- static void preload_begin(void)
- {
- preload_active = 1;
- preload_pending = 0;
- preload_wait = 1;
- preload_node = 0;
- preload_loaded = 0;
- preload_scroll = 0;
- }
- /* Styles are computed top-down during layout, but images inside a hidden
- * menu/carousel are never visited and keep img_top == 0. Without this check
- * the preloader walks the DOM in node order and spends the whole image
- * budget on logos and off-canvas thumbnails the reader cannot see. */
- static int node_hidden(unsigned int node)
- {
- unsigned int path[32], n = 0, p = node, i;
- struct br_style st, parent;
- while (p && n < sizeof(path) / sizeof(path[0])) {
- path[n++] = p;
- p = doc.nodes[p].parent;
- }
- parent = (struct br_style){0};
- parent.font_px = 16;
- parent.display = BR_DISPLAY_BLOCK;
- for (i = n; i > 0; i--) {
- br_style_for(&doc, path[i - 1], &parent, &st);
- if (st.display == BR_DISPLAY_NONE || st.vis_hidden ||
- st.opacity_hidden)
- return 1;
- parent = st;
- }
- return 0;
- }
- static struct br_style base_style(void);
- static void style_for_node(unsigned int node, struct br_style *out)
- {
- unsigned int p = doc.nodes[node].parent;
- struct br_style parent;
- if (p) {
- style_for_node(p, &parent);
- br_style_for(&doc, node, &parent, out);
- } else {
- parent = base_style();
- br_style_for(&doc, node, &parent, out);
- }
- }
- /* Load at most one image per call. Images below the current viewport are
- * left pending until the page is scrolled; this keeps a long article with
- * many 4K screenshots from stalling on content the user cannot see yet. */
- static void preload_step(void)
- {
- unsigned int batch = 0;
- while (preload_active && preload_node < doc.nnode) {
- unsigned int i = preload_node;
- struct br_node *nd = &doc.nodes[i];
- const char *src;
- char url[BR_URL_MAX];
- unsigned char *data;
- unsigned int len;
- char err[64];
- char msg[64];
- unsigned int n = 0;
- int rv;
- if (nd->type != BR_NODE_ELEM) {
- preload_node++;
- continue;
- }
- if (nd->tag != BR_TAG_IMG) {
- if (nd->bg_img_index == -1) {
- struct br_style bs;
- style_for_node(i, &bs);
- if (bs.display != BR_DISPLAY_NONE &&
- !bs.vis_hidden && bs.bg_image) {
- const char *bsrc =
- br_bg_image_url(bs.bg_image - 1);
- if (bsrc[0] &&
- !contains_ci(bsrc, ".svg") &&
- !starts_with(bsrc, "data:") &&
- resolve_link(page_url, bsrc, url,
- sizeof(url)) == 0) {
- if (image_count >=
- BR_MAX_IMAGES) {
- preload_node =
- doc.nnode;
- break;
- }
- preload_node = i + 1;
- data = 0;
- len = 0;
- rv = fetch_image(url,
- BR_IMG_FILE_MAX,
- &data, &len,
- err, sizeof(err));
- if (rv == FETCH_ABORT) {
- preload_active = 0;
- preload_pending = 0;
- return;
- }
- if (rv == 0 &&
- decode_image_data(
- data, len,
- &images[image_count]) == 0) {
- nd->bg_img_index =
- (short)image_count;
- image_count++;
- preload_loaded++;
- layout_valid = 0;
- } else {
- nd->bg_img_index = -2;
- }
- ngui_invalidate(&ui);
- if (++batch >= 4)
- return;
- continue;
- }
- }
- nd->bg_img_index = -2;
- }
- preload_node++;
- continue;
- }
- if (nd->img_index != -1) {
- preload_node++;
- continue;
- }
- if (ancestor_is(i, BR_TAG_NOSCRIPT) || node_hidden(i)) {
- preload_node++;
- continue;
- }
- src = br_src(&doc, i);
- if (!src[0] || contains_ci(src, ".svg") ||
- starts_with(src, "data:")) {
- preload_node++;
- continue;
- }
- if (resolve_link(page_url, src, url, sizeof(url)) != 0) {
- preload_node++;
- continue;
- }
- if (image_count >= BR_MAX_IMAGES) {
- preload_node = doc.nnode;
- break;
- }
- /* Do not stop at the first below-fold image: DOM order is
- * not visual order, and a footer/ad image early in the DOM
- * used to hide every later article image from the loader. */
- if (img_top[i] >
- (unsigned int)(page_scroll.offset + page_rect.h + 160)) {
- preload_pending = 1;
- preload_node++;
- continue;
- }
- preload_node = i + 1;
- s_append(msg, &n, "Loading images ", sizeof(msg));
- s_append_dec(msg, &n, (unsigned int)preload_loaded + 1,
- sizeof(msg));
- s_append(msg, &n, "...", sizeof(msg));
- set_status(msg, 0, 0);
- ngui_invalidate(&ui);
- data = 0;
- len = 0;
- rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
- sizeof(err));
- if (rv == FETCH_ABORT) {
- preload_active = 0;
- preload_pending = 0;
- return;
- }
- if (rv == 0 &&
- decode_image_data(data, len, &images[image_count]) == 0) {
- nd->img_index = (short)image_count;
- image_count++;
- preload_loaded++;
- layout_valid = 0;
- } else {
- /* Remember failures so a scroll does not retry an
- * unreachable tracking/ad image with the full network
- * timeout on every pass. */
- nd->img_index = -2;
- }
- ngui_invalidate(&ui);
- /* A full page layout after every image dominates loading on a
- * long page. Decode a small batch, then let the main loop draw
- * once for the whole batch. */
- if (++batch >= 4)
- return;
- }
- preload_active = 0;
- if (preload_pending)
- preload_scroll = page_scroll.offset;
- else
- preload_pending = 0;
- set_status(post_status_text, post_status_error, post_status_ttl);
- ngui_invalidate(&ui);
- }
- static void history_push(const char *url)
- {
- if (!url || !url[0])
- return;
- if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len &&
- s_eq(history[hist_pos], url))
- return;
- if (hist_len > 0 && hist_pos + 1 < hist_len)
- hist_len = hist_pos + 1;
- if (hist_len < BR_HISTORY) {
- s_copy(history[hist_len], url, BR_URL_MAX);
- hist_pos = hist_len;
- hist_len++;
- } else {
- int i;
- for (i = 1; i < BR_HISTORY; i++)
- s_copy(history[i - 1], history[i], BR_URL_MAX);
- s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX);
- hist_pos = BR_HISTORY - 1;
- }
- }
- static void load_page(const char *url, int local, int push)
- {
- unsigned char *body = 0;
- unsigned int len = 0;
- int truncated = 0;
- struct img_info image_info;
- char ctype[64];
- char final[BR_URL_MAX];
- char err[80];
- const char *detail = 0;
- const char *post = pending_post_len ? pending_post : 0;
- unsigned int post_len = pending_post_len;
- int rv;
- final[0] = '\0';
- pending_post_len = 0;
- cancel_fetch = 0;
- preload_active = 0;
- preload_pending = 0;
- js_browser_stop();
- js_last_status[0] = '\0';
- js_last_error = 0;
- heap_clear();
- image_reset();
- form_reset_state();
- layout_valid = 0;
- page_scroll.offset = 0;
- page_scroll.dragging = 0;
- br_parse_plain(&doc, "", 0); /* blank while loading */
- page_title[0] = '\0';
- page_loading = 1;
- page_is_local = local;
- s_copy(page_url, url, sizeof(page_url));
- s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
- url_in.len = s_len(url_in.text);
- url_in.cursor = url_in.len;
- url_in.scroll = 0;
- ngui_set_focus(&ui, 0);
- if (final[0])
- final[0] = '\0';
- if (push)
- history_push(url);
- set_status("Loading...", 0, 0);
- ngui_invalidate(&ui);
- /* heap_clear() detaches the fonts, so attach them again before the
- * first frame or the URL bar flashes in the 8x16 bitmap fallback. */
- if (heap_start() != 0) {
- show_error("Out of memory", "Cannot allocate the page buffer");
- return;
- }
- ngui_font_attach(&ui);
- redraw_now();
- browser_rules_alloc();
- rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
- sizeof(ctype), final, sizeof(final), err, sizeof(err),
- &truncated, post, post_len);
- if (rv == FETCH_ABORT)
- return;
- if (rv != 0) {
- detail = err;
- show_error("Cannot load this page", detail);
- s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
- url_in.len = s_len(url_in.text);
- url_in.cursor = url_in.len;
- url_in.scroll = 0;
- return;
- }
- if (!local)
- origin_cache_learn(url, final[0] ? final : url);
- page_is_local = local;
- if (final[0]) {
- s_copy(page_url, final, sizeof(page_url));
- s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
- url_in.len = s_len(url_in.text);
- url_in.cursor = url_in.len;
- url_in.scroll = 0;
- }
- if (contains_ci(ctype, "text/plain")) {
- br_parse_plain(&doc, (const char *)body, len);
- } else if (contains_ci(ctype, "image/") ||
- (!contains_ci(ctype, "text/html") &&
- img_sniff(body, len, &image_info) == 0)) {
- unsigned int node = br_parse_image_doc(&doc, page_url);
- if (node)
- decode_direct_image(body, len, node);
- s_copy(page_title, "Image", sizeof(page_title));
- } else {
- br_set_viewport(ui.w);
- br_parse(&doc, (const char *)body, len);
- s_copy(page_title, doc.title[0] ? doc.title : page_url,
- sizeof(page_title));
- if (load_stylesheets() == FETCH_ABORT)
- return;
- if (pending_action)
- return;
- }
- preload_begin();
- page_loading = 0;
- s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
- url_in.len = s_len(url_in.text);
- url_in.cursor = url_in.len;
- url_in.scroll = 0;
- ngui_set_focus(&ui, 0);
- /* Paint the parsed page before fetching/running scripts. A slow
- * remote <script src> then does not hide the page behind the loading
- * screen; the fetch helper pumps this UI while it waits. */
- set_status("Loading scripts...", 0, 0);
- redraw_now();
- run_page_scripts();
- if (pending_action)
- return;
- post_status_text[0] = '\0';
- post_status_error = 0;
- post_status_ttl = 0;
- if (js_last_status[0]) {
- s_copy(post_status_text, js_last_status,
- sizeof(post_status_text));
- post_status_error = js_last_error;
- post_status_ttl = 8000;
- } else if (truncated) {
- s_copy(post_status_text, "Page truncated at the buffer limit",
- sizeof(post_status_text));
- post_status_ttl = 6000;
- } else if (doc.truncated) {
- s_copy(post_status_text, "Page truncated by the parser",
- sizeof(post_status_text));
- post_status_ttl = 6000;
- } else if (starts_with(page_url, "https://")) {
- s_copy(post_status_text, "HTTPS: certificate NOT verified",
- sizeof(post_status_text));
- post_status_ttl = 6000;
- }
- set_status(post_status_text, post_status_error, post_status_ttl);
- ngui_invalidate(&ui);
- }
- static void load_home(int push)
- {
- pending_post_len = 0;
- cancel_fetch = 0;
- preload_active = 0;
- preload_pending = 0;
- js_browser_stop();
- js_last_status[0] = '\0';
- js_last_error = 0;
- heap_clear();
- image_reset();
- form_reset_state();
- layout_valid = 0;
- page_scroll.offset = 0;
- page_scroll.dragging = 0;
- br_parse_plain(&doc, "", 0);
- page_loading = 1;
- page_is_local = 0;
- s_copy(page_url, "about:home", sizeof(page_url));
- s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
- url_in.len = s_len(url_in.text);
- url_in.cursor = url_in.len;
- url_in.scroll = 0;
- ngui_set_focus(&ui, 0);
- if (push)
- history_push(page_url);
- ngui_invalidate(&ui);
- /* See load_page(): reattach the fonts before the first redraw. */
- if (heap_start() != 0) {
- show_error("Out of memory", "Cannot allocate the browser heap");
- return;
- }
- ngui_font_attach(&ui);
- redraw_now();
- browser_rules_alloc();
- {
- static const char home[] =
- "<html><head><title>NebOS Browser</title><style>"
- "body { margin: 10px; color: #e4e6f0; }"
- "h1 { color: #7fb0ff; }"
- ".box { background-color: #262b3a; margin: 12px; "
- "padding: 10px; }"
- "a { color: #7fb0ff; }"
- "pre { background-color: #32384a; }"
- "</style></head><body>"
- "<h1>NebOS Browser</h1>"
- "<p>A small HTML/CSS browser built on <b>ngui</b>, the "
- "shared HTTP client and the image library.</p>"
- "<div class=\"box\"><b>Try:</b>"
- "<ul><li><a href=\"/floppy1/DEMO.HTM\">/"
- "floppy1/DEMO.HTM</a> on the root floppy</li>"
- "<li><a href=\"/floppy1/JS.HTM\">/"
- "floppy1/JS.HTM</a> inline JavaScript demo</li>"
- "<li><a href=\"http://10.0.2.2:8000/\">"
- "http://10.0.2.2:8000/</a> on the QEMU host</li>"
- "<li><a href=\"https://example.com/\">"
- "https://example.com/</a> (TLS, not verified)</li>"
- "</ul></div>"
- "<p>Type a URL in the bar above. Esc closes the "
- "window, Ctrl+L focuses the address bar.</p>"
- "<pre>javascript: inline + external scripts (limited DOM)\nforms: not supported"
- "\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
- "</body></html>";
- br_set_viewport(ui.w);
- br_parse(&doc, home, sizeof(home) - 1);
- br_parse_inline_css(&doc);
- br_reset_styles(&doc);
- s_copy(page_title, doc.title, sizeof(page_title));
- }
- post_status_text[0] = '\0';
- post_status_error = 0;
- post_status_ttl = 0;
- page_loading = 0;
- set_status("", 0, 0);
- s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
- url_in.len = s_len(url_in.text);
- url_in.cursor = url_in.len;
- url_in.scroll = 0;
- ngui_set_focus(&ui, 0);
- ngui_invalidate(&ui);
- }
- /* Extensions the browser opens from the local filesystem. A dotted input
- * with one of these still resolves relative to the cwd. */
- static int local_file_ext(const char *s)
- {
- static const char *const exts[] = {
- "htm", "html", "txt", "text", "md", "css", "js", "json", "xml",
- "png", "jpg", "jpeg", "gif", "bmp", "tga", "ppm", "pgm", "pbm",
- "ico", "cur", "pam", "svg", "elf", "bin", "ttf", "log", "ini",
- "cfg", "conf", 0
- };
- unsigned int i = 0, start = 0, len = 0, e;
- while (s[i] && s[i] != '/' && s[i] != ':' && s[i] != '?' &&
- s[i] != '#') {
- if (s[i] == '.') {
- start = i + 1;
- len = 0;
- } else if (start) {
- len++;
- }
- i++;
- }
- if (!len)
- return 0;
- for (e = 0; exts[e]; e++) {
- const char *x = exts[e];
- unsigned int xn = s_len(x), k;
- if (xn != len)
- continue;
- for (k = 0; k < len; k++) {
- char a = s[start + k];
- if (a >= 'A' && a <= 'Z')
- a = (char)(a - 'A' + 'a');
- if (a != x[k])
- break;
- }
- if (k == len)
- return 1;
- }
- return 0;
- }
- /* A dotted first component (cloudron.io) is a hostname, not a local file.
- * Skipping the VFS stat keeps a slow FAT/floppy lookup off the path from
- * Enter to the first network request. */
- static int looks_like_host(const char *s)
- {
- unsigned int i = 0;
- int dot = 0;
- while (s[i] && s[i] != '/' && s[i] != '?' && s[i] != '#' &&
- s[i] != ':') {
- char c = s[i];
- if (c == '.') {
- dot = 1;
- } else if (!((c >= 'a' && c <= 'z') ||
- (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') ||
- c == '-' || c == '_')) {
- return 0;
- }
- i++;
- }
- return dot && i > 0 && !local_file_ext(s);
- }
- static void load_remote(const char *url, int push)
- {
- char canon[BR_URL_MAX];
- s_copy(canon, url, sizeof(canon));
- origin_cache_apply(canon, sizeof(canon));
- load_page(canon, 0, push);
- }
- static void navigate(const char *input, int push)
- {
- char target[BR_URL_MAX];
- char *t;
- if (!input)
- return;
- s_copy(target, input, sizeof(target));
- t = trim(target);
- if (!t[0])
- return;
- if (s_eq(t, "about:home")) {
- load_home(push);
- return;
- }
- if (starts_with(t, "file://")) {
- make_abs_local(t + 7, target, sizeof(target));
- load_page(target, 1, push);
- return;
- }
- if (is_http(t)) {
- load_remote(t, push);
- return;
- }
- if (t[0] == '/') {
- make_abs_local(t, target, sizeof(target));
- load_page(target, 1, push);
- return;
- }
- if (!looks_like_host(t)) {
- struct sys_stat st;
- if (u_stat(t, &st) == 0 && !st.is_dir) {
- make_abs_local(t, target, sizeof(target));
- load_page(target, 1, push);
- return;
- }
- }
- {
- char with_scheme[BR_URL_MAX];
- unsigned int n = 0;
- s_append(with_scheme, &n, "http://", sizeof(with_scheme));
- s_append(with_scheme, &n, t, sizeof(with_scheme));
- load_remote(with_scheme, push);
- }
- }
- /* ---- layout ---- */
- static struct ufont_face *style_face(const struct br_style *st)
- {
- if (st && st->font_face && st->font_face <= BR_MAX_FACES)
- return font_faces[st->font_face - 1];
- return 0;
- }
- static struct ufont_face *run_face(const struct lay_run *r)
- {
- if (r->font_face && r->font_face <= BR_MAX_FACES)
- return font_faces[r->font_face - 1];
- return 0;
- }
- static unsigned int face_text_width(struct ngui *gui,
- const struct br_style *st,
- const char *s, unsigned int n)
- {
- return ngui_text_width_face(gui, style_face(st), s, n, st->font_px,
- st->bold, st->italic, st->mono);
- }
- static int face_line_height(struct ngui *gui, const struct br_style *st)
- {
- return ngui_text_line_height_face(gui, style_face(st), st->font_px,
- st->mono);
- }
- static unsigned int run_text_width(struct ngui *gui,
- const struct lay_run *r,
- const char *s, unsigned int n)
- {
- return ngui_text_width_face(gui, run_face(r), s, n, r->font_px,
- r->bold, r->italic, r->mono);
- }
- static int run_line_height(struct ngui *gui, const struct lay_run *r)
- {
- return ngui_text_line_height_face(gui, run_face(r), r->font_px,
- r->mono);
- }
- static int run_ascent(struct ngui *gui, const struct lay_run *r)
- {
- return ngui_text_ascent_face(gui, run_face(r), r->font_px, r->mono);
- }
- static struct br_style base_style(void)
- {
- struct br_style s = { 0 };
- s.color = ui.theme.text;
- s.bg = ui.theme.bg;
- s.has_bg = 0;
- for (int i = 0; i < 4; i++) {
- s.border_c[i] = 0;
- s.border_w[i] = 0;
- }
- s.font_px = 16;
- s.bold = 0;
- s.italic = 0;
- s.mono = 0;
- s.underline = 0;
- s.align = BR_ALIGN_LEFT;
- s.display = BR_DISPLAY_BLOCK;
- s.no_bullet = 0;
- s.uppercase = 0;
- s.width_kind = s.max_width_kind = s.min_width_kind = 0;
- s.height_kind = s.min_height_kind = s.max_height_kind = 0;
- s.width = s.max_width = s.min_width = 0;
- s.height = s.min_height = s.max_height = 0;
- s.box_sizing = 0;
- s.ml_auto = s.mr_auto = 0;
- s.line_h_pct = 0;
- s.line_h_px = 0;
- s.mt = s.mb = s.ml = s.mr = 0;
- s.pt = s.pb = s.pl = s.pr = 0;
- return s;
- }
- /* Resolve a margin/padding raw value. Percentages use the containing
- * block width for all four sides, as CSS specifies. */
- static int css_mp_px(const struct br_style *st, int v, int unit, int base)
- {
- if (unit == 1)
- return base * v / 10000;
- if (unit == 2)
- return st->font_px * v / 100;
- if (unit == 3)
- return (int)br_root_font_px() * v / 100;
- if (unit == 4)
- return 0;
- return v;
- }
- static int mp_px(const struct br_style *st, int side, int base)
- {
- int raw;
- switch (side) {
- case 0: raw = st->mt; break;
- case 1: raw = st->mr; break;
- case 2: raw = st->mb; break;
- case 3: raw = st->ml; break;
- case 4: raw = st->pt; break;
- case 5: raw = st->pr; break;
- case 6: raw = st->pb; break;
- default: raw = st->pl; break;
- }
- if (st->mp_unit[side] == 5)
- return base * raw / 100 + st->mp_calc_px[side];
- return css_mp_px(st, raw, st->mp_unit[side], base);
- }
- static int style_width_px(const struct br_style *st, int base)
- {
- if (st->width_kind == 1)
- return st->width;
- if (st->width_kind == 2)
- return base * (int)st->width / 100;
- if (st->width_kind == 3)
- return base * st->width_calc_pct / 100 + st->width_calc_px;
- return 0;
- }
- static int style_height_px(const struct br_style *st, int base)
- {
- if (st->height_kind == 1)
- return st->height;
- if (st->height_kind == 2)
- return base * (int)st->height / 100;
- if (st->height_kind == 3)
- return base * st->height_calc_pct / 100 + st->height_calc_px;
- return 0;
- }
- static int css_size(unsigned char kind, int val, int base)
- {
- if (kind == 1)
- return val;
- if (kind == 2)
- return base * val / 100;
- return -1;
- }
- static void draw_border_box(struct ngui *ui, struct ngui_rect r,
- const struct br_style *st)
- {
- int top = st->border_w[0], right = st->border_w[1];
- int bottom = st->border_w[2], left = st->border_w[3];
- if (r.w <= 0 || r.h <= 0)
- return;
- if (top >= r.h || bottom >= r.h || left >= r.w || right >= r.w) {
- int i;
- for (i = 0; i < 4; i++)
- if (st->border_w[i]) {
- ngui_fill_rect(ui, r, st->border_c[i]);
- break;
- }
- return;
- }
- if (top)
- ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y, r.w, top},
- st->border_c[0]);
- if (bottom)
- ngui_fill_rect(ui, (struct ngui_rect){r.x,
- r.y + r.h - bottom,
- r.w, bottom},
- st->border_c[2]);
- if (left)
- ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y + top, left,
- r.h - top - bottom},
- st->border_c[3]);
- if (right)
- ngui_fill_rect(ui, (struct ngui_rect){r.x + r.w - right,
- r.y + top, right,
- r.h - top - bottom},
- st->border_c[1]);
- }
- static void transform_origin(const struct br_style *st, int w, int h,
- int *ox, int *oy)
- {
- *ox = st->origin_x_kind ? w * st->origin_x / 100 : st->origin_x;
- *oy = st->origin_y_kind ? h * st->origin_y / 100 : st->origin_y;
- }
- static unsigned short blend_rgb565(unsigned short d, unsigned short s,
- unsigned int a)
- {
- unsigned int dr, dg, db, sr, sg, sb;
- if (a >= 255)
- return s;
- dr = (d >> 11) & 31;
- dg = (d >> 5) & 63;
- db = d & 31;
- sr = (s >> 11) & 31;
- sg = (s >> 5) & 63;
- sb = s & 31;
- dr = (dr * (255 - a) + sr * a) / 255;
- dg = (dg * (255 - a) + sg * a) / 255;
- db = (db * (255 - a) + sb * a) / 255;
- return (unsigned short)((dr << 11) | (dg << 5) | db);
- }
- /* Paint a solid box through scale()/rotate() around transform-origin. This
- * transforms the element's own background box; text and descendants keep
- * their layout (documented approximation). */
- static void fill_transformed_rect(struct ngui *gui, struct ngui_rect box,
- unsigned short color,
- const struct br_style *st)
- {
- double sx = st->scale_x / 100.0;
- double sy = st->scale_y / 100.0;
- double th = st->rotate_x100 / 100.0 * 3.14159265358979 / 180.0;
- double c = cos(th), sn = sin(th);
- int ox, oy, minx, miny, maxx, maxy, x, y;
- double ax, ay;
- double px[4], py[4];
- int cx1, cy1, cx2, cy2, i;
- if (sx == 0)
- sx = 0.01;
- if (sy == 0)
- sy = 0.01;
- transform_origin(st, box.w, box.h, &ox, &oy);
- ax = box.x + ox;
- ay = box.y + oy;
- px[0] = -ox; py[0] = -oy;
- px[1] = box.w - ox; py[1] = -oy;
- px[2] = box.w - ox; py[2] = box.h - oy;
- px[3] = -ox; py[3] = box.h - oy;
- minx = miny = 1000000;
- maxx = maxy = -1000000;
- for (i = 0; i < 4; i++) {
- double rx = (px[i] * sx) * c - (py[i] * sy) * sn;
- double ry = (px[i] * sx) * sn + (py[i] * sy) * c;
- int dx = (int)(ax + rx), dy = (int)(ay + ry);
- if (dx < minx) minx = dx;
- if (dx > maxx) maxx = dx;
- if (dy < miny) miny = dy;
- if (dy > maxy) maxy = dy;
- }
- cx1 = page_rect.x > 0 ? page_rect.x : 0;
- cy1 = page_rect.y > 0 ? page_rect.y : 0;
- cx2 = page_rect.x + page_rect.w - 12;
- cy2 = page_rect.y + page_rect.h;
- if (cx2 > gui->w) cx2 = gui->w;
- if (cy2 > gui->h) cy2 = gui->h;
- if (minx < cx1) minx = cx1;
- if (miny < cy1) miny = cy1;
- if (maxx > cx2 - 1) maxx = cx2 - 1;
- if (maxy > cy2 - 1) maxy = cy2 - 1;
- for (y = miny; y <= maxy; y++) {
- for (x = minx; x <= maxx; x++) {
- double dx = x + 0.5 - ax, dy = y + 0.5 - ay;
- double rx = dx * c + dy * sn;
- double ry = -dx * sn + dy * c;
- double bx = rx / sx + ox;
- double by = ry / sy + oy;
- if (bx >= 0 && bx < box.w && by >= 0 && by < box.h)
- gui->px[y * gui->w + x] = color;
- }
- }
- }
- static void draw_image_transformed(struct ngui *gui,
- const struct img_image *im,
- int x, int y, int w, int h,
- int sx100, int sy100, int rot100,
- int origin_x, int origin_y,
- int origin_x_kind, int origin_y_kind)
- {
- double sx = sx100 / 100.0, sy = sy100 / 100.0;
- double th = rot100 / 100.0 * 3.14159265358979 / 180.0;
- double c = cos(th), sn = sin(th);
- int ox, oy, minx, miny, maxx, maxy, px, py, i;
- double ax, ay;
- double bx[4], by[4];
- int cx1, cy1, cx2, cy2;
- if (!im || !im->px || w <= 0 || h <= 0 || sx == 0 || sy == 0)
- return;
- ox = origin_x_kind ? w * origin_x / 100 : origin_x;
- oy = origin_y_kind ? h * origin_y / 100 : origin_y;
- ax = x + ox;
- ay = y + oy;
- bx[0] = -ox; by[0] = -oy;
- bx[1] = w - ox; by[1] = -oy;
- bx[2] = w - ox; by[2] = h - oy;
- bx[3] = -ox; by[3] = h - oy;
- minx = miny = 1000000;
- maxx = maxy = -1000000;
- for (i = 0; i < 4; i++) {
- double rx = (bx[i] * sx) * c - (by[i] * sy) * sn;
- double ry = (bx[i] * sx) * sn + (by[i] * sy) * c;
- int dx = (int)(ax + rx), dy = (int)(ay + ry);
- if (dx < minx) minx = dx;
- if (dx > maxx) maxx = dx;
- if (dy < miny) miny = dy;
- if (dy > maxy) maxy = dy;
- }
- cx1 = page_rect.x > 0 ? page_rect.x : 0;
- cy1 = page_rect.y > 0 ? page_rect.y : 0;
- cx2 = page_rect.x + page_rect.w - 12;
- cy2 = page_rect.y + page_rect.h;
- if (cx2 > gui->w) cx2 = gui->w;
- if (cy2 > gui->h) cy2 = gui->h;
- if (minx < cx1) minx = cx1;
- if (miny < cy1) miny = cy1;
- if (maxx > cx2 - 1) maxx = cx2 - 1;
- if (maxy > cy2 - 1) maxy = cy2 - 1;
- for (py = miny; py <= maxy; py++) {
- for (px = minx; px <= maxx; px++) {
- double dx = px + 0.5 - ax, dy = py + 0.5 - ay;
- double rx = dx * c + dy * sn;
- double ry = -dx * sn + dy * c;
- double fx = rx / sx + ox;
- double fy = ry / sy + oy;
- int ix, iy;
- unsigned int si, a;
- unsigned short src;
- if (fx < 0 || fx >= w || fy < 0 || fy >= h)
- continue;
- ix = (int)(fx * (double)im->w / w);
- iy = (int)(fy * (double)im->h / h);
- if (ix < 0 || ix >= (int)im->w ||
- iy < 0 || iy >= (int)im->h)
- continue;
- si = (unsigned int)iy * im->w + (unsigned int)ix;
- src = im->px[si];
- a = im->alpha ? im->alpha[si] : 255;
- gui->px[py * gui->w + px] =
- a >= 255 ? src :
- blend_rgb565(gui->px[py * gui->w + px],
- src, a);
- }
- }
- }
- static void draw_bg_image(struct lay *l, struct ngui_rect box,
- unsigned int node, const struct br_style *st)
- {
- struct br_image *bi;
- struct img_blit b;
- int tw, th, x, y, rep_x, rep_y, posx, posy;
- unsigned int scale = 65536;
- int legacy, tiles = 0, rows = 0;
- int cx1, cy1, cx2, cy2;
- if (!st->bg_image || box.w <= 0 || box.h <= 0)
- return;
- if (doc.nodes[node].bg_img_index < 0 ||
- doc.nodes[node].bg_img_index >= image_count)
- return;
- bi = &images[doc.nodes[node].bg_img_index];
- if (bi->state != 1 || bi->img.w <= 0 || bi->img.h <= 0)
- return;
- legacy = st->bg_size_kind == 0 && st->bg_repeat == 0 &&
- st->bg_pos_x_kind == 0 && st->bg_pos_x == 0 &&
- st->bg_pos_y_kind == 0 && st->bg_pos_y == 0;
- if (legacy) {
- scale = ((unsigned int)box.w << 16) / bi->img.w;
- tw = box.w;
- th = (int)(((unsigned int)bi->img.h * scale) >> 16);
- x = box.x;
- y = box.y;
- rep_x = rep_y = 0;
- } else {
- if (st->bg_size_kind == 3 || st->bg_size_kind == 4) {
- unsigned int sx = ((unsigned int)box.w << 16) /
- bi->img.w;
- unsigned int sy = ((unsigned int)box.h << 16) /
- bi->img.h;
- scale = st->bg_size_kind == 3 ?
- (sx > sy ? sx : sy) : (sx < sy ? sx : sy);
- } else if (st->bg_size_kind == 1 ||
- st->bg_size_kind == 2) {
- int w = st->bg_size_w;
- int h = st->bg_size_h;
- if (st->bg_size_kind == 2) {
- if (w)
- w = box.w * w / 100;
- if (h)
- h = box.h * h / 100;
- }
- if (w)
- scale = ((unsigned int)w << 16) / bi->img.w;
- else if (h)
- scale = ((unsigned int)h << 16) / bi->img.h;
- }
- tw = (int)(((unsigned int)bi->img.w * scale) >> 16);
- th = (int)(((unsigned int)bi->img.h * scale) >> 16);
- if (tw < 1)
- tw = 1;
- if (th < 1)
- th = 1;
- posx = st->bg_pos_x_kind == 1 ?
- (box.w - tw) * st->bg_pos_x / 100 : st->bg_pos_x;
- posy = st->bg_pos_y_kind == 1 ?
- (box.h - th) * st->bg_pos_y / 100 : st->bg_pos_y;
- x = box.x + posx;
- y = box.y + posy;
- rep_x = st->bg_repeat == 0 || st->bg_repeat == 2;
- rep_y = st->bg_repeat == 0 || st->bg_repeat == 3;
- if (rep_x) {
- int m = (x - box.x) % tw;
- if (m > 0)
- m -= tw;
- x = box.x + m;
- }
- if (rep_y) {
- int m = (y - box.y) % th;
- if (m > 0)
- m -= th;
- y = box.y + m;
- }
- }
- cx1 = box.x > page_rect.x ? box.x : page_rect.x;
- cy1 = box.y > page_rect.y ? box.y : page_rect.y;
- cx2 = box.x + box.w;
- cy2 = box.y + box.h;
- if (cx2 > page_rect.x + page_rect.w - 12)
- cx2 = page_rect.x + page_rect.w - 12;
- if (cy2 > page_rect.y + page_rect.h)
- cy2 = page_rect.y + page_rect.h;
- if (cx1 >= cx2 || cy1 >= cy2)
- return;
- b.dst = l->ui->px;
- b.dst_w = l->ui->w;
- b.dst_h = l->ui->h;
- b.clip_x = cx1;
- b.clip_y = cy1;
- b.clip_w = cx2 - cx1;
- b.clip_h = cy2 - cy1;
- b.scale = scale;
- b.bg = ui.theme.bg;
- b.bg2 = ui.theme.bg;
- b.flags = IMG_BLIT_OVER_DST;
- {
- int row_x = x;
- for (;;) {
- for (;;) {
- b.x = x;
- b.y = y;
- img_draw(&bi->img, &b);
- if (!rep_x || ++tiles > 512)
- break;
- x += tw;
- if (x >= box.x + box.w)
- break;
- }
- if (!rep_y || ++rows > 64)
- break;
- y += th;
- if (y >= box.y + box.h)
- break;
- x = row_x;
- }
- }
- }
- static int run_width(const struct lay_run *r)
- {
- if (r->img >= 0)
- return r->iw;
- if (r->img == -2)
- return r->iw;
- return (int)r->w;
- }
- static void start_line(struct lay *l, const struct br_style *st)
- {
- (void)st;
- if (l->nruns == 0) {
- l->line_y = l->y;
- l->line_h = 0;
- }
- l->line_left = l->left;
- l->line_right = l->right;
- if (l->float_active && l->line_y < l->float_bottom) {
- if (l->float_left > l->line_left)
- l->line_left = l->float_left;
- if (l->float_right < l->line_right)
- l->line_right = l->float_right;
- }
- if (l->line_right < l->line_left + 8)
- l->line_right = l->line_left + 8;
- l->line_x = l->line_left;
- l->run_x = l->line_left;
- }
- static void flush_line(struct lay *l)
- {
- int total, x, i, vis;
- if (!l->nruns && l->line_x == l->line_left) {
- l->line_h = 0;
- return;
- }
- total = l->line_x - l->line_left;
- x = l->line_left;
- vis = 1;
- if (l->draw) {
- int lh = l->line_h ? l->line_h : 16;
- vis = l->line_y + lh >= page_rect.y &&
- l->line_y <= page_rect.y + page_rect.h;
- }
- if (l->align == BR_ALIGN_CENTER)
- x += (l->line_right - l->line_left - total) / 2;
- else if (l->align == BR_ALIGN_RIGHT)
- x = l->line_right - total;
- if (x < l->line_left)
- x = l->line_left;
- for (i = 0; i < l->nruns; i++) {
- struct lay_run *r = &l->runs[i];
- int w = run_width(r);
- int ry = l->line_y + r->dy;
- x += r->gap;
- if (l->draw && vis) {
- if (r->form) {
- unsigned int node = r->form - 1;
- struct form_state *fs = form_get(node, 1);
- int hh = r->ih ? r->ih : 20;
- int focused = form_focus == node;
- int disabled = r->form_disabled;
- unsigned short bg = l->ui->theme.panel_alt;
- unsigned short fg = disabled ? 0x8410 : r->color;
- struct ngui_rect box = {x, ry, w, hh};
- if (r->form_kind == FORM_CHECKBOX ||
- r->form_kind == FORM_RADIO) {
- ngui_fill_rect(l->ui, box, bg);
- ngui_frame(l->ui, box,
- focused ? l->ui->theme.accent :
- l->ui->theme.border);
- if (fs && fs->checked) {
- struct ngui_rect dot = {
- x + 4, ry + 4,
- w > 8 ? w - 8 : 1,
- hh > 8 ? hh - 8 : 1
- };
- if (r->form_kind == FORM_RADIO)
- ngui_fill_round_rect(
- l->ui, dot, 4,
- 4, 4, 4,
- l->ui->theme.accent);
- else
- ngui_fill_rect(l->ui,
- dot,
- l->ui->theme.accent);
- }
- } else {
- ngui_fill_rect(l->ui, box, bg);
- ngui_frame(l->ui, box,
- focused ? l->ui->theme.accent :
- l->ui->theme.border);
- ngui_text_px_face(l->ui, run_face(r),
- x + 6, ry + 2,
- r->lit, s_len(r->lit),
- fg, r->font_px, r->bold,
- r->italic, r->mono);
- if (focused && fs &&
- (r->form_kind == FORM_TEXT ||
- r->form_kind == FORM_PASSWORD ||
- r->form_kind == FORM_TEXTAREA)) {
- int cw = 0;
- if (fs->len && fs->caret)
- cw = (int)run_text_width(
- l->ui, r, r->lit,
- fs->caret < fs->len ?
- fs->caret : fs->len);
- ngui_fill_rect(l->ui,
- (struct ngui_rect){
- x + 6 + cw,
- ry + 4, 1,
- hh > 8 ? hh - 8 : 1
- },
- l->ui->theme.accent);
- }
- if (r->form_kind == FORM_SELECT) {
- ngui_text_px(l->ui, x + w - 12,
- ry + 2, "v", 1, fg,
- r->font_px, 0, 0, 0);
- }
- }
- } else if (r->img == -2) {
- int hh = r->ih ? r->ih : 20;
- ngui_fill_rect(l->ui,
- (struct ngui_rect){x, ry, w, hh},
- l->ui->theme.panel_alt);
- ngui_frame(l->ui,
- (struct ngui_rect){x, ry, w, hh},
- l->ui->theme.border);
- ngui_text_px_face(l->ui, run_face(r),
- x + 6, ry + 2, r->lit,
- s_len(r->lit), r->color,
- r->font_px, r->bold, r->italic,
- r->mono);
- } else if (r->img >= 0) {
- struct br_image *bi = &images[r->img];
- if (bi->state == 1 && r->transform_scale) {
- draw_image_transformed(l->ui, &bi->img,
- x, ry, r->iw, r->ih,
- r->scale_x, r->scale_y,
- r->rotate_x100, r->origin_x,
- r->origin_y, r->origin_x_kind,
- r->origin_y_kind);
- } else if (bi->state == 1) {
- struct img_blit b;
- b.dst = l->ui->px;
- b.dst_w = l->ui->w;
- b.dst_h = l->ui->h;
- b.clip_x = page_rect.x;
- b.clip_y = page_rect.y;
- b.clip_w = page_rect.w - 12;
- b.clip_h = page_rect.h;
- b.x = x;
- b.y = ry;
- b.scale = ((unsigned int)r->iw << 16) /
- bi->img.w;
- b.bg = ui.theme.bg;
- b.bg2 = ui.theme.bg;
- b.flags = IMG_BLIT_OVER_DST;
- img_draw(&bi->img, &b);
- }
- } else {
- const char *s = r->has_lit ? r->lit :
- doc.text + r->off;
- unsigned int n = r->has_lit ? s_len(r->lit) :
- r->len;
- ngui_text_px_face(l->ui, run_face(r), x, ry,
- s, n, r->color, r->font_px,
- r->bold, r->italic, r->mono);
- if (r->underline)
- ngui_fill_rect(l->ui,
- (struct ngui_rect){x,
- ry + run_ascent(l->ui, r) + 2,
- w, 1}, r->color);
- }
- if (r->form || r->href) {
- if (nhits < HIT_MAX) {
- struct hit *h = &hits[nhits];
- unsigned int target = r->form ?
- r->form - 1 : r->href;
- int hh = (r->form || r->img >= 0) ?
- r->ih :
- run_line_height(l->ui, r);
- int merge = nhits > 0 &&
- hits[nhits - 1].node == target &&
- hits[nhits - 1].y == ry &&
- hits[nhits - 1].h == hh &&
- x <= hits[nhits - 1].x +
- hits[nhits - 1].w + 2;
- if (merge) {
- struct hit *p = &hits[nhits - 1];
- int end = x + w;
- if (end > p->x + p->w)
- p->w = end - p->x;
- } else {
- h->x = x;
- h->y = ry;
- h->w = w;
- h->h = hh;
- h->node = (unsigned short)target;
- nhits++;
- }
- }
- }
- }
- x += w;
- }
- l->y += l->line_h + 2;
- if (l->float_active && l->y >= l->float_bottom)
- l->float_active = 0;
- l->nruns = 0;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_left = l->left;
- l->line_right = l->right;
- l->line_h = 0;
- l->space_pending = 0;
- }
- static void add_run(struct lay *l, unsigned short off, unsigned short len,
- const struct br_style *st, unsigned short href,
- const char *lit)
- {
- unsigned int n = lit ? s_len(lit) : len;
- const char *tp = lit ? lit : doc.text + off;
- int w = (int)face_text_width(l->ui, st, tp, n);
- if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
- flush_line(l);
- if (l->nruns == 0) {
- l->line_y = l->y;
- l->line_h = 0;
- start_line(l, st);
- }
- {
- struct lay_run *r = &l->runs[l->nruns++];
- int gap = l->line_x - l->run_x;
- if (gap < 0)
- gap = 0;
- r->gap = (unsigned short)gap;
- r->off = off;
- r->len = len;
- r->color = st->color;
- r->href = href;
- r->dy = 0;
- r->has_lit = 0;
- r->form = 0;
- r->form_kind = 0;
- r->form_disabled = 0;
- r->lit[0] = '\0';
- if (lit && lit[0]) {
- unsigned int i;
- for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++)
- r->lit[i] = lit[i];
- r->lit[i] = '\0';
- r->has_lit = 1;
- }
- r->img = -1;
- r->iw = r->ih = 0;
- r->font_px = st->font_px;
- r->bold = st->bold;
- r->italic = st->italic;
- r->mono = st->mono;
- r->font_face = st->font_face;
- /* A control with its own background is a button, not body copy;
- * an inherited link underline would strike through the fill. */
- r->underline = st->underline && !st->has_bg;
- r->w = (unsigned short)w;
- if (r->has_lit)
- r->w = (unsigned short)run_text_width(
- l->ui, r, r->lit, s_len(r->lit));
- w = r->w;
- }
- l->line_x += w;
- l->run_x = l->line_x;
- {
- int lh = face_line_height(l->ui, st);
- if (st->line_h_px)
- lh = st->line_h_px;
- else if (st->line_h_pct)
- lh = st->font_px * st->line_h_pct / 100;
- if (lh < face_line_height(l->ui, st))
- lh = face_line_height(l->ui, st);
- if (l->line_h < lh)
- l->line_h = lh;
- }
- }
- static void add_image_run(struct lay *l, unsigned int node,
- const struct br_style *st, unsigned short href)
- {
- struct br_image *bi = 0;
- int idx = doc.nodes[node].img_index;
- int dw, dh, maxw;
- if (idx >= 0 && idx < image_count && images[idx].state == 1)
- bi = &images[idx];
- if (l->nruns == 0)
- start_line(l, st);
- if (l->space_pending &&
- (l->nruns > 0 || l->line_x > l->line_left)) {
- int sp = (int)face_text_width(l->ui, st, " ", 1);
- if (l->line_x + sp <= l->line_right)
- l->line_x += sp;
- }
- l->space_pending = 0;
- if (!bi) {
- const char *alt = br_alt(&doc, node);
- struct br_style gs = *st;
- char tmp[96];
- unsigned int n = 0;
- int w;
- gs.color = BR_GRAY;
- gs.underline = 0;
- s_append(tmp, &n, "[", sizeof(tmp));
- s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp));
- s_append(tmp, &n, "]", sizeof(tmp));
- w = (int)face_text_width(l->ui, &gs, tmp, n);
- if (l->line_x + w > l->line_right &&
- (l->nruns > 0 || l->line_x > l->line_left))
- flush_line(l);
- add_run(l, 0, 0, &gs, href, tmp);
- if (!l->draw) {
- int t = l->line_y < 0 ? 0 : l->line_y;
- int hh = face_line_height(l->ui, &gs);
- if (t > 65535)
- t = 65535;
- if (hh > 65535)
- hh = 65535;
- img_top[node] = (unsigned short)t;
- img_h[node] = (unsigned short)hh;
- }
- return;
- }
- maxw = l->line_right - l->line_left;
- if (maxw < 8)
- maxw = 8;
- {
- int iw = bi->native_w, ih = bi->native_h;
- int have_w = 0, have_h = 0, css_w = 0, css_h = 0;
- if (iw <= 0 || ih <= 0) {
- iw = (int)bi->img.w;
- ih = (int)bi->img.h;
- }
- dw = iw;
- dh = ih;
- if (doc.nodes[node].width_attr) {
- int want = doc.nodes[node].width_attr;
- dw = doc.nodes[node].width_pct ? maxw * want / 100 : want;
- have_w = 1;
- }
- if (doc.nodes[node].height_attr) {
- int want = doc.nodes[node].height_attr;
- dh = doc.nodes[node].height_pct ? maxw * want / 100 : want;
- have_h = 1;
- }
- if (have_w && !have_h)
- dh = ih ? ih * dw / iw : 0;
- if (have_h && !have_w)
- dw = iw ? iw * dh / ih : 0;
- if (st->width_kind) {
- css_w = 1;
- if (st->width_kind == 3)
- dw = maxw * st->width_calc_pct / 100 +
- st->width_calc_px;
- else
- dw = st->width_kind == 2 ?
- maxw * (int)st->width / 100 :
- (int)st->width;
- }
- if (st->height_kind) {
- css_h = 1;
- if (st->height_kind == 3)
- dh = maxw * st->height_calc_pct / 100 +
- st->height_calc_px;
- else
- dh = st->height_kind == 2 ?
- maxw * (int)st->height / 100 :
- (int)st->height;
- }
- if (css_w && !css_h)
- dh = ih ? ih * dw / iw : 0;
- if (css_h && !css_w)
- dw = iw ? iw * dh / ih : 0;
- if (st->max_width_kind) {
- int mw = st->max_width_kind == 3 ?
- maxw * st->max_width_calc_pct / 100 +
- st->max_width_calc_px :
- st->max_width_kind == 2 ?
- maxw * (int)st->max_width / 100 :
- (int)st->max_width;
- if (mw > 0 && dw > mw) {
- dh = iw ? ih * mw / iw : 0;
- dw = mw;
- }
- }
- }
- if (dw < 8)
- dw = 8;
- if (dh < 8)
- dh = 8;
- if (dw > maxw) {
- dh = dw ? dh * maxw / dw : dh;
- dw = maxw;
- }
- if (!l->draw) {
- int t = l->line_y < 0 ? 0 : l->line_y;
- if (t > 65535)
- t = 65535;
- if (dh > 65535)
- dh = 65535;
- img_top[node] = (unsigned short)t;
- img_h[node] = (unsigned short)dh;
- }
- if (l->line_x + dw > l->line_right && (l->nruns > 0 ||
- l->line_x > l->line_left))
- flush_line(l);
- if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
- flush_line(l);
- if (l->nruns == 0) {
- l->line_y = l->y;
- l->line_h = 0;
- start_line(l, st);
- }
- {
- struct lay_run *r = &l->runs[l->nruns++];
- int gap = l->line_x - l->run_x;
- if (gap < 0)
- gap = 0;
- r->gap = (unsigned short)gap;
- r->off = 0;
- r->len = 0;
- r->color = 0;
- r->href = href;
- r->form = 0;
- r->form_kind = 0;
- r->form_disabled = 0;
- /* Inline replaced elements honour their vertical margins; this
- * is how Bootstrap's negative logo margins centre the wordmark. */
- {
- int idy = mp_px(st, 0, l->right - l->left);
- if (idy < -2048)
- idy = -2048;
- if (idy > 2048)
- idy = 2048;
- r->dy = (signed short)idy;
- }
- r->has_lit = 0;
- r->lit[0] = '\0';
- r->img = (short)idx;
- r->iw = (unsigned short)dw;
- r->ih = (unsigned short)dh;
- r->w = (unsigned short)dw;
- r->font_px = st->font_px;
- r->bold = st->bold;
- r->italic = st->italic;
- r->mono = st->mono;
- r->font_face = st->font_face;
- r->transform_scale = st->transform_scale;
- r->scale_x = st->scale_x;
- r->scale_y = st->scale_y;
- r->rotate_x100 = st->rotate_x100;
- r->origin_x = st->origin_x;
- r->origin_y = st->origin_y;
- r->origin_x_kind = st->origin_x_kind;
- r->origin_y_kind = st->origin_y_kind;
- r->underline = 0;
- }
- if (node < BR_MAX_NODES) {
- layout_boxes[node].x = l->line_x;
- layout_boxes[node].y = l->line_y +
- mp_px(st, 0, l->right - l->left);
- layout_boxes[node].w = dw;
- layout_boxes[node].h = dh;
- layout_boxes[node].valid = 1;
- }
- l->line_x += dw;
- l->run_x = l->line_x;
- if (l->line_h < dh)
- l->line_h = dh;
- }
- static int text_width(struct lay *l, const char *s, unsigned int n,
- const struct br_style *st)
- {
- return (int)face_text_width(l->ui, st, s, n);
- }
- /* Longest prefix that fits in maxw, at least one byte. */
- static unsigned int fit_prefix(struct lay *l, const char *s, unsigned int n,
- const struct br_style *st, int maxw)
- {
- unsigned int take = 0;
- while (take < n) {
- if (text_width(l, s, take + 1, st) > maxw)
- break;
- take++;
- }
- return take ? take : 1;
- }
- static void emit_word(struct lay *l, const char *s, unsigned int start,
- unsigned int n, unsigned int base,
- const struct br_style *st, unsigned short href)
- {
- int avail;
- int w = text_width(l, s + start, n, st);
- unsigned int off = start;
- if (l->nruns == 0)
- start_line(l, st);
- avail = l->line_right - l->line_left;
- if (st->nowrap) {
- if (n)
- add_run(l, (unsigned short)(base + start),
- (unsigned short)n, st, href, 0);
- return;
- }
- if (l->line_x + w > l->line_right &&
- (l->nruns > 0 || l->line_x > l->line_left)) {
- flush_line(l);
- start_line(l, st);
- avail = l->line_right - l->line_left;
- }
- if (w <= avail) {
- if (n)
- add_run(l, (unsigned short)(base + start),
- (unsigned short)n, st, href, 0);
- return;
- }
- while (n > 0) {
- unsigned int take = fit_prefix(l, s + off, n, st, avail);
- add_run(l, (unsigned short)(base + off),
- (unsigned short)take, st, href, 0);
- off += take;
- n -= take;
- if (n) {
- flush_line(l);
- start_line(l, st);
- avail = l->line_right - l->line_left;
- }
- }
- }
- static void emit_text(struct lay *l, const char *s, unsigned int len,
- const struct br_style *st, unsigned short href, int pre)
- {
- unsigned int i = 0;
- unsigned int base;
- int space_w = (int)face_text_width(l->ui, st, " ", 1);
- if (!s || !len)
- return;
- if (space_w <= 0)
- space_w = 8;
- if (l->nruns == 0)
- start_line(l, st);
- base = (unsigned int)(s - doc.text);
- if (st->uppercase) {
- char *p = (char *)s;
- unsigned int j;
- for (j = 0; j < len; j++)
- if (p[j] >= 'a' && p[j] <= 'z')
- p[j] = (char)(p[j] - 'a' + 'A');
- }
- if (pre) {
- while (i < len) {
- unsigned int start;
- if (s[i] == '\n') {
- flush_line(l);
- start_line(l, st);
- i++;
- continue;
- }
- if (s[i] == ' ') {
- if (l->line_x + space_w > l->line_right &&
- (l->nruns > 0 || l->line_x > l->line_left)) {
- flush_line(l);
- start_line(l, st);
- } else {
- l->line_x += space_w;
- }
- i++;
- continue;
- }
- start = i;
- while (i < len && s[i] != ' ' && s[i] != '\n')
- i++;
- emit_word(l, s, start, i - start, base, st, href);
- }
- return;
- }
- while (i < len) {
- unsigned int start, n;
- if (s[i] == ' ') {
- l->space_pending = 1;
- i++;
- continue;
- }
- start = i;
- while (i < len && s[i] != ' ')
- i++;
- n = i - start;
- if (l->space_pending &&
- (l->nruns > 0 || l->line_x > l->line_left)) {
- if (st->nowrap ||
- l->line_x + space_w <= l->line_right)
- l->line_x += space_w;
- }
- l->space_pending = 0;
- emit_word(l, s, start, n, base, st, href);
- }
- }
- static int list_index(unsigned int parent, unsigned int node)
- {
- unsigned int n = 0, i;
- for (i = doc.nodes[parent].first; i && i != node;
- i = doc.nodes[i].next)
- if (doc.nodes[i].tag == BR_TAG_LI)
- n++;
- return (int)n + 1;
- }
- static void add_form_run(struct lay *l, unsigned int node,
- const struct br_style *st, unsigned short href)
- {
- struct form_state *fs = form_get(node, 1);
- char shown[FORM_VALUE_MAX + 1];
- char opt[64];
- const char *text = "";
- struct lay_run *r;
- int kind = form_kind_of(node);
- int disabled = form_disabled(node);
- int tw, w = 120, h = 20, i;
- if (!fs)
- return;
- if (l->nruns == 0)
- start_line(l, st);
- if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
- text = "";
- w = 16;
- h = 16;
- } else if (kind == FORM_SELECT) {
- if (!form_option_text(node, fs->selected, opt, sizeof(opt)))
- s_copy(opt, "Select", sizeof(opt));
- text = opt;
- w = 120;
- } else if (kind == FORM_BUTTON || kind == FORM_SUBMIT ||
- kind == FORM_RESET) {
- if (fs->len)
- text = fs->value;
- else
- text = kind == FORM_SUBMIT ? "Submit" :
- kind == FORM_RESET ? "Reset" : "Button";
- } else if (kind == FORM_PASSWORD) {
- for (i = 0; i < fs->len; i++)
- shown[i] = '*';
- shown[fs->len] = '\0';
- text = fs->len ? shown : br_placeholder(&doc, node);
- } else {
- if (fs->len)
- text = fs->value;
- else
- text = br_placeholder(&doc, node);
- if (kind == FORM_TEXTAREA) {
- w = 180;
- h = 40;
- }
- }
- tw = (int)face_text_width(l->ui, st, text, s_len(text));
- if (kind == FORM_BUTTON || kind == FORM_SUBMIT || kind == FORM_RESET) {
- w = tw + 18;
- if (w < 64)
- w = 64;
- h = 22;
- } else if (kind == FORM_TEXT || kind == FORM_PASSWORD) {
- w = st->width_kind == 1 ? st->width : 120;
- } else if (kind == FORM_SELECT) {
- w = st->width_kind == 1 ? st->width : 120;
- }
- if (w > l->line_right - l->line_left)
- w = l->line_right - l->line_left;
- if (w < 8)
- w = 8;
- if (l->line_x + w > l->line_right &&
- (l->nruns > 0 || l->line_x > l->line_left))
- flush_line(l);
- if (l->nruns >= 96)
- return;
- if (l->nruns == 0) {
- l->line_y = l->y;
- l->line_h = 0;
- start_line(l, st);
- }
- r = &l->runs[l->nruns++];
- r->gap = (unsigned short)(l->line_x - l->run_x);
- r->off = r->len = 0;
- r->color = st->color;
- r->href = href;
- r->dy = 0;
- r->has_lit = 1;
- for (i = 0; i + 1 < (int)sizeof(r->lit) && text[i]; i++)
- r->lit[i] = text[i];
- r->lit[i] = '\0';
- r->img = -2;
- r->iw = (unsigned short)w;
- r->ih = (unsigned short)h;
- r->w = (unsigned short)w;
- r->font_px = st->font_px;
- r->bold = st->bold;
- r->italic = st->italic;
- r->mono = st->mono;
- r->font_face = st->font_face;
- r->underline = 0;
- r->form = node + 1;
- r->form_kind = (unsigned char)kind;
- r->form_disabled = (unsigned char)disabled;
- if (node < BR_MAX_NODES) {
- layout_boxes[node].x = l->line_x;
- layout_boxes[node].y = l->line_y;
- layout_boxes[node].w = w;
- layout_boxes[node].h = h;
- layout_boxes[node].valid = 1;
- }
- l->line_x += w;
- l->run_x = l->line_x;
- if (l->line_h < h)
- l->line_h = h;
- }
- /* A poster wrapper is absolutely positioned over the media slot; the
- * measured height of its picture is normally added back to the flow. When a
- * loading-skeleton sibling already reserves that slot, adding both doubles
- * the media height and pushes the article text a full screen down. */
- static int has_skeleton_sibling(unsigned int node)
- {
- unsigned int p = doc.nodes[node].parent, c;
- for (c = doc.nodes[p].first; c; c = doc.nodes[c].next) {
- if (c == node || doc.nodes[c].type != BR_NODE_ELEM)
- continue;
- if (doc.nodes[c].class_off &&
- contains_ci(doc.attrs + doc.nodes[c].class_off,
- "skeleton"))
- return 1;
- }
- return 0;
- }
- static void layout_children(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link);
- static void layout_flex_children(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link);
- static void layout_grid_children(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link);
- static void layout_grid_auto(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link);
- static int flex_item_width(struct lay *l, unsigned int n,
- const struct br_style *cs, int avail);
- static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
- const struct br_style *pst, int depth,
- unsigned short link);
- static void layout_table_row(struct lay *l, unsigned int row,
- const struct br_style *pst, int depth,
- unsigned short link);
- static int table_has_rowspan(unsigned int table);
- static void layout_table(struct lay *l, unsigned int table,
- const struct br_style *st, int depth,
- unsigned short link);
- /* A flex container whose items are only inline spans is usually just a
- * heading/icon pair. Our flex row positions such items without emitting
- * their inline text in some deep page contexts; normal inline flow renders
- * the same content correctly. */
- static int flex_wants_row(unsigned int parent, const struct br_style *pst)
- {
- unsigned int c;
- for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- struct br_style cs;
- if (nd->type == BR_NODE_TEXT)
- continue;
- br_style_for(&doc, c, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE)
- continue;
- if (cs.display != BR_DISPLAY_INLINE || cs.flex_grow ||
- cs.width_kind || cs.has_bg ||
- cs.border_w[0] || cs.border_w[1] ||
- cs.border_w[2] || cs.border_w[3] ||
- nd->tag == BR_TAG_IMG)
- return 1;
- }
- return 0;
- }
- static int css_radius(const struct br_style *st, int corner,
- int w, int h)
- {
- int r = st->radius[corner];
- if (r <= 0)
- return 0;
- if (r > w / 2)
- r = w / 2;
- if (r > h / 2)
- r = h / 2;
- return r;
- }
- static void svg_viewbox(unsigned node, int *vw, int *vh)
- {
- struct br_node *nd = &doc.nodes[node];
- if (nd->width_attr && nd->height_attr) {
- *vw = nd->width_attr;
- *vh = nd->height_attr;
- } else {
- *vw = 300;
- *vh = 150;
- }
- if (*vw < 1)
- *vw = 1;
- if (*vh < 1)
- *vh = 1;
- }
- static int svg_num(const char **pp)
- {
- const char *p = *pp;
- int neg = 0, v = 0, frac = 0, scale = 1;
- while (*p == ' ' || *p == ',' || *p == '\t' ||
- *p == '\n' || *p == '\r')
- p++;
- if (*p == '-') {
- neg = 1;
- p++;
- } else if (*p == '+') {
- p++;
- }
- while (*p >= '0' && *p <= '9') {
- v = v * 10 + (*p - '0');
- p++;
- }
- if (*p == '.') {
- p++;
- while (*p >= '0' && *p <= '9' && scale < 1000) {
- frac = frac * 10 + (*p - '0');
- scale *= 10;
- p++;
- }
- }
- v += frac / scale;
- *pp = p;
- return neg ? -v : v;
- }
- static void svg_path(struct ngui *ui, const char *d,
- int ox, int oy, int dw, int dh, int vw, int vh,
- unsigned short color)
- {
- int cx = 0, cy = 0, sx = 0, sy = 0;
- char cmd = 0;
- const char *p = d;
- while (p && *p) {
- int rel, nx, ny;
- char c;
- while (*p == ' ' || *p == ',' || *p == '\t' ||
- *p == '\n' || *p == '\r')
- p++;
- if (!*p)
- break;
- if ((*p >= 'A' && *p <= 'Z') ||
- (*p >= 'a' && *p <= 'z'))
- cmd = *p++;
- else if (!cmd) {
- p++;
- continue;
- }
- rel = cmd >= 'a' && cmd <= 'z';
- c = rel ? (char)(cmd - 'a' + 'A') : cmd;
- if (c == 'Z') {
- ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
- ox + sx * dw / vw, oy + sy * dh / vh,
- color);
- cx = sx;
- cy = sy;
- cmd = 0;
- continue;
- }
- if (c == 'H' || c == 'V') {
- const char *save = p;
- int a = svg_num(&p);
- if (p == save) {
- p++;
- continue;
- }
- nx = cx;
- ny = cy;
- if (c == 'H')
- nx = rel ? cx + a : a;
- else
- ny = rel ? cy + a : a;
- } else {
- const char *save = p;
- int a = svg_num(&p);
- int b = svg_num(&p);
- if (p == save) {
- p++;
- continue;
- }
- if (c == 'M') {
- nx = rel ? cx + a : a;
- ny = rel ? cy + b : b;
- cx = sx = nx;
- cy = sy = ny;
- cmd = rel ? 'l' : 'L';
- continue;
- }
- if (c != 'L')
- break;
- nx = rel ? cx + a : a;
- ny = rel ? cy + b : b;
- }
- ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
- ox + nx * dw / vw, oy + ny * dh / vh, color);
- cx = nx;
- cy = ny;
- }
- }
- static void draw_svg(struct ngui *ui, unsigned node,
- int ox, int oy, int dw, int dh, int vw, int vh,
- const struct br_style *pst)
- {
- unsigned int c;
- for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- struct br_style st;
- const char *nm;
- if (nd->type != BR_NODE_ELEM || !nd->name_off)
- continue;
- nm = doc.attrs + nd->name_off;
- br_style_for(&doc, c, pst, &st);
- if (s_eq(nm, "rect")) {
- struct ngui_rect r;
- int rx = (int)nd->svg_rx * dw / vw;
- r.x = ox + (int)nd->svg_x * dw / vw;
- r.y = oy + (int)nd->svg_y * dh / vh;
- r.w = (int)nd->width_attr * dw / vw;
- r.h = (int)nd->height_attr * dh / vh;
- if (r.w < 1)
- r.w = 1;
- if (r.h < 1)
- r.h = 1;
- if (rx > r.w / 2)
- rx = r.w / 2;
- if (rx > r.h / 2)
- rx = r.h / 2;
- if (st.has_bg) {
- if (rx)
- ngui_fill_round_rect(ui, r, rx, rx,
- rx, rx, st.bg);
- else
- ngui_fill_rect(ui, r, st.bg);
- }
- if (st.border_w[0]) {
- if (rx)
- ngui_frame_round(ui, r, rx, rx, rx, rx,
- st.border_w[0],
- st.border_c[0]);
- else
- draw_border_box(ui, r, &st);
- }
- } else if (s_eq(nm, "line")) {
- if (st.border_w[0])
- ngui_line(ui,
- ox + (int)nd->svg_x * dw / vw,
- oy + (int)nd->svg_y * dh / vh,
- ox + (int)nd->width_attr * dw / vw,
- oy + (int)nd->height_attr * dh / vh,
- st.border_c[0]);
- } else if (s_eq(nm, "path")) {
- if (st.border_w[0] && nd->src_off)
- svg_path(ui, doc.attrs + nd->src_off,
- ox, oy, dw, dh, vw, vh,
- st.border_c[0]);
- } else if (s_eq(nm, "text")) {
- unsigned int t = nd->first;
- const char *s;
- unsigned int n, tw;
- int px = st.font_px ? st.font_px : 16;
- int tx, ty, anchor;
- if (!t || doc.nodes[t].type != BR_NODE_TEXT)
- continue;
- s = doc.text + doc.nodes[t].text_off;
- n = doc.nodes[t].text_len;
- tw = face_text_width(ui, &st, s, n);
- tx = ox + (int)nd->svg_x * dw / vw;
- ty = oy + (int)nd->svg_y * dh / vh -
- ngui_text_ascent_face(ui, style_face(&st), px,
- st.mono);
- anchor = st.align;
- if (nd->attr_flags & BR_ATTR_ANCHOR_MIDDLE)
- anchor = BR_ALIGN_CENTER;
- else if (nd->attr_flags & BR_ATTR_ANCHOR_END)
- anchor = BR_ALIGN_RIGHT;
- if (anchor == BR_ALIGN_CENTER)
- tx -= (int)tw / 2;
- else if (anchor == BR_ALIGN_RIGHT)
- tx -= (int)tw;
- ngui_text_px_face(ui, style_face(&st), tx, ty, s, n,
- st.color, px, st.bold, st.italic,
- st.mono);
- }
- }
- }
- static void layout_block(struct lay *l, unsigned int node,
- const struct br_style *st, int depth,
- unsigned short link)
- {
- int old_left = l->left, old_right = l->right, old_align = l->align;
- int saved_parent_top = l->parent_top;
- int top, l0, r0;
- int flow_y, flow_left, flow_right, positioned, relative, sticky, shifted;
- int cbw, ml, mr, bl, br, pl, pr, bt, bb, pt, pb;
- int deco_w, deco_h, content_w, content_h, box_h;
- int definite_w = 0;
- int normal_top = 0;
- int rel_dx = 0, rel_dy = 0;
- if (depth > 24 || st->display == BR_DISPLAY_NONE)
- return;
- flow_y = l->y;
- flow_left = l->left;
- flow_right = l->right;
- positioned = st->pos == 1 || st->pos == 2;
- relative = st->pos == 3;
- sticky = st->pos == 4;
- shifted = relative || sticky || st->transform_set;
- if (positioned) {
- int def_w = st->width_kind == 1 ? st->width : -1;
- if (st->pos == 2) {
- int avail_w = page_rect.w - 12;
- if (st->right_set && def_w >= 0)
- l->left = page_rect.x + avail_w - st->right -
- def_w;
- else
- l->left = page_rect.x +
- (st->left_set ? st->left : 0);
- l->right = def_w >= 0 ? l->left + def_w :
- page_rect.x + avail_w;
- if (st->bottom_set && st->height_kind)
- l->y = page_rect.y + page_rect.h -
- st->bottom -
- style_height_px(st, page_rect.w);
- else
- l->y = page_rect.y +
- (st->top_set ? st->top : 0);
- } else {
- int left = flow_left + (st->left_set ? st->left : 0);
- unsigned int parent = doc.nodes[node].parent;
- if (st->right_set && def_w >= 0)
- left = flow_right - st->right - def_w;
- l->left = left;
- l->right = def_w >= 0 ? left + def_w :
- flow_right +
- (st->left_set ? st->left : 0);
- l->y = flow_y + (st->top_set ? st->top : 0);
- if (st->bottom_set && st->height_kind &&
- parent < BR_MAX_NODES && block_h[parent] > 0)
- l->y = flow_y + block_h[parent] - st->bottom -
- style_height_px(st, old_right - old_left);
- }
- old_left = l->left;
- old_right = l->right;
- } else if (relative) {
- rel_dx = st->left_set ? st->left :
- (st->right_set ? -st->right : 0);
- rel_dy = st->top_set ? st->top :
- (st->bottom_set ? -st->bottom : 0);
- }
- if (st->transform_set) {
- rel_dx += st->tx;
- rel_dy += st->ty;
- }
- if (doc.nodes[node].tag == BR_TAG_INPUT ||
- doc.nodes[node].tag == BR_TAG_TEXTAREA ||
- doc.nodes[node].tag == BR_TAG_SELECT ||
- doc.nodes[node].tag == BR_TAG_BUTTON) {
- if (doc.nodes[node].tag == BR_TAG_INPUT &&
- s_eq(br_type(&doc, node), "hidden"))
- return;
- add_form_run(l, node, st, link);
- l->y += doc.nodes[node].tag == BR_TAG_TEXTAREA ? 40 :
- doc.nodes[node].tag == BR_TAG_BUTTON ? 22 : 20;
- return;
- }
- flush_line(l);
- cbw = old_right - old_left;
- if (cbw < 8)
- cbw = 8;
- l->y += mp_px(st, 0, cbw);
- top = l->y;
- ml = mp_px(st, 3, cbw);
- mr = mp_px(st, 1, cbw);
- bl = st->border_w[3];
- br = st->border_w[1];
- pl = mp_px(st, 7, cbw);
- pr = mp_px(st, 5, cbw);
- bt = st->border_w[0];
- bb = st->border_w[2];
- pt = mp_px(st, 4, cbw);
- pb = mp_px(st, 6, cbw);
- deco_w = bl + br + pl + pr;
- deco_h = bt + bb + pt + pb;
- if (st->width_kind) {
- int w;
- if (st->width_kind == 3)
- w = cbw * st->width_calc_pct / 100 +
- st->width_calc_px;
- else
- w = css_size(st->width_kind, st->width, cbw);
- if (w < 0)
- w = 0;
- content_w = st->box_sizing ? w - deco_w : w;
- definite_w = 1;
- } else {
- content_w = cbw - ml - mr - deco_w;
- }
- if (content_w < 0)
- content_w = 0;
- if (st->max_width_kind) {
- int mw = st->max_width_kind == 3 ?
- cbw * st->max_width_calc_pct / 100 +
- st->max_width_calc_px :
- css_size(st->max_width_kind, st->max_width, cbw);
- int max_box = st->box_sizing ? mw : mw + deco_w;
- if (content_w + deco_w > max_box)
- content_w = max_box - deco_w;
- }
- if (st->min_width_kind) {
- int mw = st->min_width_kind == 3 ?
- cbw * st->min_width_calc_pct / 100 +
- st->min_width_calc_px :
- css_size(st->min_width_kind, st->min_width, cbw);
- int min_box = st->box_sizing ? mw : mw + deco_w;
- if (content_w + deco_w < min_box) {
- content_w = min_box - deco_w;
- definite_w = 1;
- }
- }
- if (content_w < 8)
- content_w = 8;
- if (definite_w) {
- int free_w = cbw - ml - mr - deco_w - content_w;
- if (free_w < 0)
- free_w = 0;
- if (st->ml_auto && st->mr_auto) {
- ml = free_w / 2;
- mr = free_w - ml;
- } else if (st->ml_auto) {
- ml = free_w;
- } else if (st->mr_auto) {
- mr = free_w;
- }
- }
- l0 = old_left + ml;
- r0 = l0 + deco_w + content_w;
- if (r0 < l0 + 8)
- r0 = l0 + 8;
- normal_top = top;
- if (sticky) {
- if (st->top_set) {
- int stick = page_rect.y + 4 + st->top;
- if (normal_top < stick)
- rel_dy += stick - normal_top;
- } else if (st->bottom_set && block_h[node]) {
- int stick = page_rect.y + page_rect.h - st->bottom -
- block_h[node];
- if (normal_top > stick)
- rel_dy += stick - normal_top;
- }
- if (l->draw && doc.nodes[node].parent < BR_MAX_NODES &&
- block_h[doc.nodes[node].parent]) {
- unsigned int p = doc.nodes[node].parent;
- int ptop = l->parent_top;
- int pbottom = ptop + block_h[p];
- int eh = block_h[node];
- int moved_top = normal_top + rel_dy;
- int moved_bottom = moved_top + eh;
- if (moved_bottom > pbottom)
- rel_dy -= moved_bottom - pbottom;
- moved_top = normal_top + rel_dy;
- if (moved_top < ptop)
- rel_dy += ptop - moved_top;
- }
- }
- if (shifted && l->draw) {
- top += rel_dy;
- l0 += rel_dx;
- r0 += rel_dx;
- }
- l->parent_top = top;
- if (l->draw && block_h[node] &&
- top < page_rect.y + page_rect.h &&
- top + block_h[node] > page_rect.y) {
- struct ngui_rect box = {l0, top, r0 - l0, block_h[node]};
- int rtl = css_radius(st, 0, box.w, box.h);
- int rtr = css_radius(st, 1, box.w, box.h);
- int rbr = css_radius(st, 2, box.w, box.h);
- int rbl = css_radius(st, 3, box.w, box.h);
- if (st->transform_scale) {
- if (st->has_bg)
- fill_transformed_rect(l->ui, box, st->bg, st);
- } else if (rtl || rtr || rbr || rbl) {
- if (st->has_bg) {
- int bw = st->border_w[0];
- if (bw > 0 && st->border_c[0] != st->bg &&
- box.w > bw * 2 && box.h > bw * 2) {
- struct ngui_rect inner = {
- box.x + bw, box.y + bw,
- box.w - bw * 2, box.h - bw * 2
- };
- int itl = rtl > bw ? rtl - bw : 0;
- int itr = rtr > bw ? rtr - bw : 0;
- int ibr = rbr > bw ? rbr - bw : 0;
- int ibl = rbl > bw ? rbl - bw : 0;
- ngui_fill_round_rect(l->ui, box, rtl,
- rtr, rbr, rbl,
- st->border_c[0]);
- ngui_fill_round_rect(l->ui, inner,
- itl, itr, ibr,
- ibl, st->bg);
- } else {
- ngui_fill_round_rect(l->ui, box, rtl,
- rtr, rbr, rbl,
- st->bg);
- }
- } else if (st->border_w[0]) {
- ngui_frame_round(l->ui, box, rtl, rtr, rbr,
- rbl, st->border_w[0],
- st->border_c[0]);
- }
- } else {
- if (st->has_bg)
- ngui_fill_rect(l->ui, box, st->bg);
- if (st->bg_image)
- draw_bg_image(l, box, node, st);
- if (st->border_w[0] || st->border_w[1] ||
- st->border_w[2] || st->border_w[3])
- draw_border_box(l->ui, box, st);
- }
- }
- l->left = l0 + bl + pl;
- l->right = l->left + content_w;
- if (l->right < l->left + 8)
- l->right = l->left + 8;
- l->align = st->align;
- l->line_left = l->left;
- l->line_right = l->right;
- l->y = top + bt + pt;
- if (doc.nodes[node].tag == BR_TAG_HR) {
- if (l->draw)
- ngui_fill_rect(l->ui, (struct ngui_rect){l->left,
- l->y + 2, l->right - l->left, 1}, BR_GRAY);
- l->y += 8;
- } else if (doc.nodes[node].tag == BR_TAG_VIDEO) {
- int vw = l->right - l->left;
- int vh;
- if (doc.nodes[node].width_pct)
- vw = vw * doc.nodes[node].width_attr / 100;
- else if (doc.nodes[node].width_attr)
- vw = doc.nodes[node].width_attr;
- if (vw > l->right - l->left)
- vw = l->right - l->left;
- if (vw < 8)
- vw = 8;
- vh = vw * 9 / 16;
- if (vh > 240)
- vh = 240;
- if (vh < 80)
- vh = 80;
- if (l->draw) {
- ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y,
- vw, vh}, 0xD69A);
- ngui_frame(l->ui, (struct ngui_rect){l->left, l->y,
- vw, vh}, 0x8410);
- ngui_text(l->ui, l->left + 8, l->y + 8, "[video]",
- 0x4208);
- }
- l->y += vh + 4;
- } else if (doc.nodes[node].tag == BR_TAG_SVG) {
- int vw, vh, dw = l->right - l->left;
- int dh;
- svg_viewbox(node, &vw, &vh);
- if (dw < 8)
- dw = 8;
- dh = dw * vh / vw;
- if (dh < 8)
- dh = 8;
- if (dh > 1400)
- dh = 1400;
- if (l->draw) {
- struct ngui_rect clip = {l->left, l->y, dw, dh};
- ngui_push_clip(l->ui, clip);
- draw_svg(l->ui, node, l->left, l->y, dw, dh,
- vw, vh, st);
- ngui_pop_clip(l->ui);
- }
- l->y += dh;
- } else {
- if (doc.nodes[node].tag == BR_TAG_LI && st->list_item &&
- !st->no_bullet) {
- char *m = li_marker[node];
- if (!m[0]) {
- unsigned int parent = doc.nodes[node].parent;
- if (parent &&
- doc.nodes[parent].tag == BR_TAG_OL) {
- int idx = list_index(parent, node);
- unsigned int n = 0;
- s_append_dec(m, &n, (unsigned int)idx, 6);
- s_append(m, &n, ".", 6);
- } else {
- s_copy(m, "-", 6);
- }
- }
- if (l->draw) {
- int mw = (int)ngui_text_width_ui(l->ui, m);
- ngui_text(l->ui, l->left - mw - 6, l->y, m,
- st->color);
- }
- }
- if (doc.nodes[node].tag == BR_TAG_TR)
- layout_table_row(l, node, st, depth + 1, link);
- else if (doc.nodes[node].tag == BR_TAG_TABLE &&
- table_has_rowspan(node))
- layout_table(l, node, st, depth + 1, link);
- else if (st->grid_auto)
- layout_grid_auto(l, node, st, depth + 1, link);
- else if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 &&
- st->grid_gap > 0)
- layout_grid_children(l, node, st, depth + 1, link);
- else if (st->display == BR_DISPLAY_FLEX && !st->flex_col &&
- flex_wants_row(node, st))
- layout_flex_children(l, node, st, depth + 1, link);
- else
- layout_children(l, node, st, depth + 1, link);
- }
- flush_line(l);
- if (l->float_active) {
- if (l->y < l->float_bottom)
- l->y = l->float_bottom;
- l->float_active = 0;
- }
- if (st->height_kind) {
- content_h = style_height_px(st, cbw);
- if (st->box_sizing)
- content_h -= deco_h;
- } else {
- content_h = l->y - (top + bt + pt);
- }
- if (content_h < 0)
- content_h = 0;
- if (st->max_height_kind) {
- int mh = st->max_height_kind == 3 ?
- cbw * st->max_height_calc_pct / 100 +
- st->max_height_calc_px :
- css_size(st->max_height_kind, st->max_height, cbw);
- int max_box = st->box_sizing ? mh : mh + deco_h;
- if (content_h + deco_h > max_box)
- content_h = max_box - deco_h;
- }
- if (st->min_height_kind) {
- int mh = st->min_height_kind == 3 ?
- cbw * st->min_height_calc_pct / 100 +
- st->min_height_calc_px :
- css_size(st->min_height_kind, st->min_height, cbw);
- int min_box = st->box_sizing ? mh : mh + deco_h;
- if (content_h + deco_h < min_box)
- content_h = min_box - deco_h;
- }
- if (content_h < 0)
- content_h = 0;
- box_h = deco_h + content_h;
- if (box_h < 0)
- box_h = 0;
- l->y = (shifted ? normal_top : top) + box_h;
- if (node < BR_MAX_NODES) {
- layout_boxes[node].x = l0;
- layout_boxes[node].y = top;
- layout_boxes[node].w = r0 - l0;
- layout_boxes[node].h = box_h;
- layout_boxes[node].valid = r0 > l0;
- }
- l->left = old_left;
- l->right = old_right;
- l->align = old_align;
- l->y += mp_px(st, 2, cbw);
- if (!l->draw) {
- if (box_h > 65535)
- box_h = 65535;
- block_h[node] = (unsigned short)box_h;
- }
- if (positioned) {
- l->y = flow_y;
- l->left = flow_left;
- l->right = flow_right;
- }
- l->parent_top = saved_parent_top;
- }
- static int text_node_blank(const struct br_node *nd)
- {
- const char *t = doc.text + nd->text_off;
- unsigned int i;
- for (i = 0; i < nd->text_len; i++)
- if (t[i] != ' ' && t[i] != '\t' && t[i] != '\n' &&
- t[i] != '\r')
- return 0;
- return 1;
- }
- /* Rough shrink-to-fit width for a float without an explicit width: sum the
- * text/image widths in the subtree, plus padding and margins. This keeps
- * Bootstrap's floated nav items on one line instead of giving each the whole
- * row. */
- static int text_space(char c)
- {
- return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
- }
- static int intrinsic_width(struct ngui *ui, unsigned int node,
- const struct br_style *pst, int depth, int base)
- {
- unsigned int c;
- int w = 0;
- if (depth > 12)
- return 0;
- for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- struct br_style cs;
- if (nd->type == BR_NODE_TEXT) {
- const char *t = doc.text + nd->text_off;
- unsigned int tn = nd->text_len;
- if (doc.nodes[node].tag == BR_TAG_PRE) {
- w += (int)face_text_width(ui, pst, t, tn);
- } else {
- unsigned int ti = 0;
- int first = 1;
- int lead = w > 0 && tn > 0 && text_space(t[0]);
- int sp = (int)face_text_width(ui, pst, " ", 1);
- while (ti < tn) {
- unsigned int ts;
- while (ti < tn && text_space(t[ti]))
- ti++;
- ts = ti;
- while (ti < tn && !text_space(t[ti]))
- ti++;
- if (ti == ts)
- break;
- if (!first || lead)
- w += sp;
- lead = 0;
- w += (int)face_text_width(ui, pst,
- t + ts, ti - ts);
- first = 0;
- }
- }
- continue;
- }
- br_style_for(&doc, c, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE)
- continue;
- if (nd->tag == BR_TAG_BR) {
- w += 8;
- continue;
- }
- if (nd->tag == BR_TAG_IMG) {
- /* Measure the image as layout will draw it: a CSS or
- * attribute height keeps the intrinsic aspect ratio, and
- * a CSS width overrides the native pixel width. Bootstrap
- * sizes the Cloudron logo with height:40px only. */
- int idx = nd->img_index;
- int iw = 0, ih = 0;
- if (idx >= 0 && idx < image_count &&
- images[idx].state == 1) {
- iw = (int)images[idx].native_w;
- ih = (int)images[idx].native_h;
- if (iw <= 0 || ih <= 0) {
- iw = (int)images[idx].img.w;
- ih = (int)images[idx].img.h;
- }
- }
- if (cs.width_kind) {
- int ww = style_width_px(&cs, base);
- w += ww > 0 ? ww : 64;
- } else if (nd->width_attr) {
- w += nd->width_attr;
- } else if (nd->height_attr && iw && ih) {
- w += iw * (int)nd->height_attr / ih;
- } else if (cs.height_kind && iw && ih) {
- int hh = style_height_px(&cs, base);
- w += hh > 0 ? iw * hh / ih : iw;
- } else if (iw) {
- w += iw;
- } else {
- w += 64;
- }
- continue;
- }
- w += mp_px(&cs, 7, base) +
- mp_px(&cs, 5, base) +
- mp_px(&cs, 3, base) +
- mp_px(&cs, 1, base);
- w += intrinsic_width(ui, c, &cs, depth + 1, base);
- if (w > 4096)
- break;
- }
- return w;
- }
- /* Lay consecutive floated block children side by side. Bootstrap's grid is
- * the main user: .row > .col-md-* with float:left and a percentage width.
- * Each column is laid out with its own left/right edge and the row advances
- * to the tallest column. Returns the first child not consumed. */
- static unsigned int layout_float_group(struct lay *l, unsigned int first,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- unsigned int n = first;
- int row_left = l->left, row_right = l->right;
- int row_top, row_bottom, xl, xr, any = 0;
- int fl_left = row_left, fl_right = row_right, fl_bottom;
- flush_line(l);
- row_top = l->y;
- row_bottom = l->y;
- fl_bottom = row_top;
- xl = row_left;
- xr = row_right;
- while (n) {
- struct br_node *nd = &doc.nodes[n];
- struct br_style cs;
- unsigned short lk = link;
- int colw, right;
- if (nd->type == BR_NODE_TEXT) {
- if (text_node_blank(nd)) {
- n = nd->next;
- continue;
- }
- break;
- }
- br_style_for(&doc, n, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE) {
- n = nd->next;
- continue;
- }
- /* A float blockifies its box: Bootstrap floats inline
- * anchors too (the navbar brand and its text links), and
- * their padding must be honoured. */
- if (!cs.float_left)
- break;
- if (nd->tag == BR_TAG_A && nd->href_off)
- lk = (unsigned short)n;
- right = cs.float_left == 2;
- if (cs.width_kind == 2 || cs.width_kind == 3) {
- colw = style_width_px(&cs, row_right - row_left);
- } else if (cs.width_kind == 1) {
- colw = (int)cs.width;
- } else {
- int base = row_right - row_left;
- colw = intrinsic_width(l->ui, n, &cs, 0, base) +
- mp_px(&cs, 7, base) +
- mp_px(&cs, 5, base) +
- mp_px(&cs, 3, base) +
- mp_px(&cs, 1, base);
- if (colw < 8)
- colw = 8;
- }
- if (colw < 8)
- colw = 8;
- if (colw > row_right - row_left)
- colw = row_right - row_left;
- /* The float column width is already resolved. */
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- if (right) {
- if (any && xr - colw < xl) {
- row_top = row_bottom;
- row_bottom = row_top;
- xl = row_left;
- xr = row_right;
- }
- if (xr - colw < xl)
- colw = xr - xl;
- if (colw < 8)
- colw = 8;
- l->left = xr - colw;
- if (l->left < row_left)
- l->left = row_left;
- l->right = xr;
- xr = l->left;
- if (xr < fl_right)
- fl_right = xr;
- } else {
- if (any && xl + colw > xr) {
- row_top = row_bottom;
- row_bottom = row_top;
- xl = row_left;
- xr = row_right;
- }
- l->left = xl;
- l->right = xl + colw;
- xl += colw;
- if (xl > fl_left)
- fl_left = xl;
- }
- if (l->right < l->left + 8)
- l->right = l->left + 8;
- l->y = row_top;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, n, &cs, depth + 1, lk);
- if (l->y > row_bottom)
- row_bottom = l->y;
- if (l->y > fl_bottom)
- fl_bottom = l->y;
- any = 1;
- n = nd->next;
- }
- l->left = row_left;
- l->right = row_right;
- if (n && any) {
- struct br_node *nd = &doc.nodes[n];
- int inline_next = 0;
- if (nd->type == BR_NODE_TEXT) {
- inline_next = 1;
- } else if (nd->type == BR_NODE_ELEM) {
- struct br_style cs;
- br_style_for(&doc, n, pst, &cs);
- inline_next = cs.display != BR_DISPLAY_BLOCK &&
- cs.display != BR_DISPLAY_NONE &&
- !br_tag_is_block(nd->tag);
- }
- if (inline_next) {
- if (l->float_active) {
- if (l->float_left > fl_left)
- fl_left = l->float_left;
- if (l->float_right < fl_right)
- fl_right = l->float_right;
- if (l->float_bottom > fl_bottom)
- fl_bottom = l->float_bottom;
- }
- l->float_active = 1;
- l->float_left = fl_left;
- l->float_right = fl_right;
- l->float_bottom = fl_bottom;
- l->y = row_top;
- } else {
- l->float_active = 0;
- l->y = row_bottom;
- }
- } else {
- l->float_active = 0;
- l->y = row_bottom;
- }
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_left = l->left;
- l->line_right = l->right;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- return n;
- }
- static int flex_item_width(struct lay *l, unsigned int n,
- const struct br_style *cs, int avail)
- {
- struct br_node *nd = &doc.nodes[n];
- (void)l;
- int w;
- if (cs->width_kind == 2 || cs->width_kind == 3)
- w = style_width_px(cs, avail);
- else if (cs->width_kind == 1)
- w = (int)cs->width;
- else if (nd->tag == BR_TAG_IMG) {
- int idx = nd->img_index;
- if (idx >= 0 && idx < image_count &&
- images[idx].state == 1)
- w = (int)images[idx].img.w;
- else if (nd->width_attr)
- w = (int)nd->width_attr;
- else
- w = 64;
- } else {
- w = intrinsic_width(l->ui, n, cs, 0, avail);
- w += mp_px(cs, 7, avail);
- w += mp_px(cs, 5, avail);
- w += mp_px(cs, 3, avail);
- w += mp_px(cs, 1, avail);
- }
- if (w < 24)
- w = 24;
- if (w > avail)
- w = avail;
- return w;
- }
- /* A run of display:inline-block siblings (file tags, small buttons) belongs
- * on one line, not one full-width block each. Place intrinsic widths from
- * left to right and wrap when the row is full. */
- static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- unsigned int n = first;
- int row_left = l->left, row_right = l->right;
- int avail = row_right - row_left;
- int x = row_left, y = l->y, row_h = 0, any = 0;
- flush_line(l);
- y = l->y;
- while (n) {
- struct br_node *nd = &doc.nodes[n];
- struct br_style cs;
- unsigned short lk = link;
- int colw, h;
- if (nd->type == BR_NODE_TEXT) {
- if (!text_node_blank(nd))
- break;
- if (any)
- x += 4;
- n = nd->next;
- continue;
- }
- br_style_for(&doc, n, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE) {
- n = nd->next;
- continue;
- }
- if (cs.display != BR_DISPLAY_INLINE_BLOCK)
- break;
- if (nd->tag == BR_TAG_A && nd->href_off)
- lk = (unsigned short)n;
- colw = flex_item_width(l, n, &cs, avail);
- /* A few pixels of slack keep a single long token (boot.S)
- * from wrapping at the exact content width. */
- colw += 8;
- if (colw > avail)
- colw = avail;
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- if (any && x + colw > row_right) {
- y += row_h + 2;
- x = row_left;
- row_h = 0;
- any = 0;
- }
- if (any && x > row_left)
- x += 4;
- l->left = x;
- l->right = x + colw;
- if (l->right < l->left + 8)
- l->right = l->left + 8;
- l->y = y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, n, &cs, depth + 1, lk);
- h = l->y - y;
- if (h > row_h)
- row_h = h;
- x += colw;
- any = 1;
- n = nd->next;
- }
- if (any)
- y += row_h;
- l->left = row_left;
- l->right = row_right;
- l->y = y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- return n;
- }
- struct flex_item {
- unsigned int node;
- unsigned short link;
- int w;
- };
- static void layout_flex_children(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- struct flex_item it[32];
- unsigned int c;
- int nitem = 0, grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
- int row_left = l->left, row_right = l->right;
- int avail = row_right - row_left;
- int idx, row_top;
- if (avail < 160 || depth > 24) {
- layout_children(l, parent, pst, depth, link);
- return;
- }
- /* If direct text is not just whitespace, normal block flow handles it
- * better than pretending it is a flex item. */
- for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
- layout_children(l, parent, pst, depth, link);
- return;
- }
- }
- for (c = doc.nodes[parent].first;
- c && nitem < (int)(sizeof(it) / sizeof(it[0]));
- c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- struct br_style cs;
- if (nd->type == BR_NODE_TEXT)
- continue;
- br_style_for(&doc, c, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE)
- continue;
- it[nitem].node = c;
- it[nitem].link = (nd->tag == BR_TAG_A && nd->href_off) ?
- (unsigned short)c : link;
- it[nitem].w = 0;
- if (cs.flex_grow && !cs.width_kind)
- grow_count++;
- else
- fixed += flex_item_width(l, c, &cs, avail);
- nitem++;
- }
- grow_w = grow_count ? (avail - fixed) / grow_count : 0;
- if (grow_w < 96)
- grow_w = 96;
- for (idx = 0; idx < nitem; idx++) {
- struct br_style cs;
- br_style_for(&doc, it[idx].node, pst, &cs);
- if (cs.flex_grow && !cs.width_kind)
- it[idx].w = grow_w;
- else
- it[idx].w = flex_item_width(l, it[idx].node, &cs, avail);
- if (it[idx].w > avail)
- it[idx].w = avail;
- if (it[idx].w < 8)
- it[idx].w = 8;
- }
- row_top = l->y;
- idx = 0;
- while (idx < nitem) {
- int start = idx, count = 0, row_w = 0;
- int free_w, x, extra = 0, row_h = 0, k;
- while (idx < nitem) {
- int add = it[idx].w + (count ? gap : 0);
- if (count && pst->flex_wrap != 2 &&
- row_w + add > avail)
- break;
- row_w += add;
- count++;
- idx++;
- }
- if (!count) {
- count = 1;
- row_w = it[start].w;
- idx = start + 1;
- }
- free_w = avail - row_w;
- if (free_w < 0)
- free_w = 0;
- x = row_left;
- if (pst->justify == 1) {
- x += free_w / 2;
- } else if (pst->justify == 2) {
- x += free_w;
- } else if (pst->justify == 3) {
- extra = count > 1 ? free_w / (count - 1) : 0;
- } else if (pst->justify == 4) {
- extra = count ? free_w / count : 0;
- x += extra / 2;
- }
- for (k = 0; k < count; k++) {
- int node = (int)it[start + k].node;
- int h = block_h[node];
- if (h > row_h)
- row_h = h;
- }
- if (pst->height_kind) {
- int hh = style_height_px(pst, avail);
- if (hh > row_h)
- row_h = hh;
- }
- for (k = 0; k < count; k++) {
- struct br_style cs;
- int node = (int)it[start + k].node;
- int yoff = 0;
- br_style_for(&doc, node, pst, &cs);
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- if (l->draw && pst->align_items &&
- row_h > block_h[node]) {
- if (pst->align_items == 1)
- yoff = (row_h - block_h[node]) / 2;
- else if (pst->align_items == 2)
- yoff = row_h - block_h[node];
- }
- l->left = x;
- l->right = x + it[start + k].w;
- if (l->right < l->left + 8)
- l->right = l->left + 8;
- l->y = row_top + yoff;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, node, &cs, depth + 1, it[start + k].link);
- if (!l->draw) {
- int h = l->y - row_top;
- if (h > row_h)
- row_h = h;
- }
- x += it[start + k].w;
- if (k + 1 < count)
- x += gap + extra;
- }
- row_top += row_h;
- }
- l->left = row_left;
- l->right = row_right;
- l->y = row_top;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- }
- /* Two-column CSS grid: track sizes from grid-template-columns (px, %, fr)
- * plus the grid-area hints used by site shells (site notice/footer full
- * width, column-start left, page-content right). Anything more elaborate
- * falls back to normal block flow. */
- static int grid_track_width(unsigned char kind, unsigned short val,
- int avail, int other)
- {
- if (kind == 2)
- return avail * (int)val / 100;
- if (kind == 1)
- return val;
- return other;
- }
- static void layout_grid_children(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- unsigned int n;
- int row_left = l->left, row_right = l->right;
- int avail = row_right - row_left;
- int gap = pst->grid_gap;
- int track_total = avail - gap;
- int col0, col1, x0, x1;
- int row_y, row_bottom, have_left = 0, have_right = 0;
- unsigned int c;
- if (avail < 32 || depth > 24)
- return;
- if (track_total < 16)
- track_total = 16;
- col0 = grid_track_width((unsigned char)(pst->width_kind),
- pst->width, track_total, 0);
- col1 = grid_track_width((unsigned char)(pst->max_width_kind),
- pst->max_width, track_total, 0);
- if (pst->width_kind == 3 && pst->max_width_kind == 3) {
- int w0 = pst->width ? pst->width : 1;
- int w1 = pst->max_width ? pst->max_width : 1;
- col0 = track_total * w0 / (w0 + w1);
- col1 = track_total - col0;
- } else if (pst->width_kind == 3) {
- col0 = track_total - col1;
- if (col0 < 24)
- col0 = 24;
- } else if (pst->max_width_kind == 3) {
- col1 = track_total - col0;
- if (col1 < 24)
- col1 = 24;
- }
- if (col0 < 24)
- col0 = 24;
- if (col1 < 24)
- col1 = 24;
- if (col0 + col1 > track_total) {
- col1 = track_total - col0;
- if (col1 < 24)
- col1 = 24;
- }
- x0 = row_left;
- x1 = row_left + col0 + gap;
- /* Direct text makes a grid ambiguous; use block flow instead. */
- for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
- layout_children(l, parent, pst, depth, link);
- return;
- }
- }
- row_y = row_bottom = l->y;
- n = doc.nodes[parent].first;
- while (n) {
- struct br_node *nd = &doc.nodes[n];
- struct br_style cs;
- unsigned short lk = link;
- int slot, x, w;
- if (nd->type == BR_NODE_TEXT) {
- n = nd->next;
- continue;
- }
- br_style_for(&doc, n, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE) {
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_A && nd->href_off)
- lk = (unsigned short)n;
- slot = cs.grid_slot;
- if (!slot) {
- if (!have_left)
- slot = 2;
- else if (!have_right)
- slot = 3;
- else
- slot = 2;
- }
- if (slot == 1) {
- if (have_left || have_right) {
- row_y = row_bottom;
- have_left = have_right = 0;
- }
- x = row_left;
- w = avail;
- } else if (slot == 2) {
- if (have_left) {
- row_y = row_bottom;
- have_left = have_right = 0;
- }
- x = x0;
- w = col0;
- have_left = 1;
- } else {
- if (have_right) {
- row_y = row_bottom;
- have_left = have_right = 0;
- }
- x = x1;
- w = col1;
- have_right = 1;
- }
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- l->left = x;
- l->right = x + w;
- if (l->right < l->left + 8)
- l->right = l->left + 8;
- l->y = row_y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, n, &cs, depth + 1, lk);
- if (l->y > row_bottom)
- row_bottom = l->y;
- n = nd->next;
- }
- l->left = row_left;
- l->right = row_right;
- l->y = row_bottom;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- }
- /* repeat(auto-fit, minmax(<min>, 1fr)): pick as many equal columns as fit,
- * capped at four. Used by the project docs' card grids. */
- static void layout_grid_auto(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- unsigned int n, c;
- int row_left = l->left, row_right = l->right;
- int avail = row_right - row_left;
- int gap = pst->grid_gap;
- int minw = pst->width_kind == 1 ? pst->width : 200;
- int ncols, colw, x, y = l->y, start_y = l->y, row_h = 0, col = 0;
- if (avail < 32 || depth > 24) {
- layout_children(l, parent, pst, depth, link);
- return;
- }
- for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
- struct br_node *nd = &doc.nodes[c];
- if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
- layout_children(l, parent, pst, depth, link);
- return;
- }
- }
- if (minw < 80)
- minw = 80;
- ncols = (avail + gap) / (minw + gap);
- if (ncols < 1)
- ncols = 1;
- if (ncols > 4)
- ncols = 4;
- colw = (avail - gap * (ncols - 1)) / ncols;
- if (colw < 24)
- colw = 24;
- x = row_left;
- n = doc.nodes[parent].first;
- while (n) {
- struct br_node *nd = &doc.nodes[n];
- struct br_style cs;
- unsigned short lk = link;
- int h;
- if (nd->type == BR_NODE_TEXT) {
- n = nd->next;
- continue;
- }
- br_style_for(&doc, n, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE) {
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_A && nd->href_off)
- lk = (unsigned short)n;
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- l->left = x;
- l->right = x + colw;
- if (l->right < l->left + 8)
- l->right = l->left + 8;
- l->y = y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, n, &cs, depth + 1, lk);
- h = l->y - y;
- if (h > row_h)
- row_h = h;
- col++;
- n = nd->next;
- if (col == ncols) {
- y += row_h + gap;
- x = row_left;
- row_h = 0;
- col = 0;
- } else {
- x += colw + gap;
- }
- }
- if (col)
- y += row_h;
- else if (y > start_y)
- y -= gap;
- l->left = row_left;
- l->right = row_right;
- l->y = y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- }
- /* Column widths are the widest cell in each column across every row, so all
- * rows line up. The table model is still deliberately basic: no colspan,
- * border-collapse or percentage columns. */
- static void table_columns(struct lay *l, unsigned int table,
- const struct br_style *pst, int avail,
- int *raw, int maxcols, int *out_cols,
- int *out_total)
- {
- unsigned int r, c;
- int cols = 0, total = 0, fixed_sum = 0, free_raw = 0, free_cols = 0;
- int fixed[12];
- for (c = 0; c < (unsigned int)maxcols; c++) {
- raw[c] = 0;
- fixed[c] = 0;
- }
- /* Intrinsic widths of single-column cells. */
- for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
- struct br_node *rn = &doc.nodes[r];
- int col = 0;
- if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
- continue;
- for (c = rn->first; c; c = doc.nodes[c].next) {
- struct br_node *cn = &doc.nodes[c];
- struct br_style cs;
- int w, span;
- if (cn->type != BR_NODE_ELEM ||
- (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
- continue;
- span = cn->colspan ? cn->colspan : 1;
- br_style_for(&doc, c, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE)
- continue;
- if (col + span > maxcols)
- span = maxcols - col;
- if (span < 1)
- break;
- w = intrinsic_width(l->ui, c, &cs, 0, avail) +
- mp_px(&cs, 7, avail) +
- mp_px(&cs, 5, avail) +
- mp_px(&cs, 3, avail) +
- mp_px(&cs, 1, avail) +
- cs.border_w[0] + cs.border_w[1] +
- cs.border_w[2] + cs.border_w[3];
- if (w < 24)
- w = 24;
- if (span == 1) {
- if (w > raw[col])
- raw[col] = w;
- }
- if (col + span > cols)
- cols = col + span;
- col += span;
- }
- }
- /* A colspan cell only has to be as wide as the sum of its columns. */
- for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
- struct br_node *rn = &doc.nodes[r];
- int col = 0;
- if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
- continue;
- for (c = rn->first; c; c = doc.nodes[c].next) {
- struct br_node *cn = &doc.nodes[c];
- struct br_style cs;
- int span, w, sum, i;
- if (cn->type != BR_NODE_ELEM ||
- (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
- continue;
- span = cn->colspan ? cn->colspan : 1;
- if (col + span > maxcols)
- span = maxcols - col;
- if (span < 1)
- break;
- if (span > 1) {
- br_style_for(&doc, c, pst, &cs);
- if (cs.display != BR_DISPLAY_NONE) {
- w = intrinsic_width(l->ui, c, &cs,
- 0, avail) +
- mp_px(&cs, 7, avail) +
- mp_px(&cs, 5, avail) +
- mp_px(&cs, 3, avail) +
- mp_px(&cs, 1, avail) +
- cs.border_w[0] + cs.border_w[1] +
- cs.border_w[2] + cs.border_w[3];
- sum = 0;
- for (i = 0; i < span; i++)
- sum += raw[col + i];
- if (w > sum)
- raw[col + span - 1] += w - sum;
- }
- }
- col += span;
- }
- }
- /* Explicit and percentage widths override intrinsic widths. */
- for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
- struct br_node *rn = &doc.nodes[r];
- int col = 0;
- if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
- continue;
- for (c = rn->first; c; c = doc.nodes[c].next) {
- struct br_node *cn = &doc.nodes[c];
- struct br_style cs;
- int span;
- if (cn->type != BR_NODE_ELEM ||
- (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
- continue;
- span = cn->colspan ? cn->colspan : 1;
- if (col + span > maxcols)
- span = maxcols - col;
- if (span < 1)
- break;
- if (span == 1) {
- br_style_for(&doc, c, pst, &cs);
- if (cs.display != BR_DISPLAY_NONE) {
- if (cs.width_kind == 1) {
- raw[col] = cs.width;
- fixed[col] = 1;
- } else if (cs.width_kind == 2 ||
- cs.width_kind == 3) {
- raw[col] = style_width_px(&cs,
- avail);
- fixed[col] = 1;
- }
- }
- }
- col += span;
- }
- }
- for (c = 0; c < (unsigned int)cols; c++) {
- if (raw[c] < 8)
- raw[c] = 8;
- total += raw[c];
- if (fixed[c])
- fixed_sum += raw[c];
- else {
- free_raw += raw[c];
- free_cols++;
- }
- }
- if (fixed_sum > 0 && fixed_sum < avail) {
- int free_avail = avail - fixed_sum;
- int i;
- if (free_raw > 0) {
- for (i = 0; i < cols; i++)
- if (!fixed[i])
- raw[i] = free_avail * raw[i] /
- free_raw;
- } else if (free_cols > 0) {
- for (i = 0; i < cols; i++)
- if (!fixed[i])
- raw[i] = free_avail / free_cols;
- }
- total = 0;
- for (i = 0; i < cols; i++)
- total += raw[i];
- } else if (total < avail && total > 0) {
- int i;
- for (i = 0; i < cols; i++)
- raw[i] = raw[i] * avail / total;
- total = 0;
- for (i = 0; i < cols; i++)
- total += raw[i];
- }
- *out_cols = cols;
- *out_total = total;
- }
- static void layout_table_row(struct lay *l, unsigned int row,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- unsigned int n, cell[12];
- int raw[12], colstart[12], colspan[12];
- int prefix[13];
- int ncell = 0, total = 0, cols = 0, col = 0;
- int row_left = l->left, row_right = l->right;
- int avail = row_right - row_left;
- int y = l->y, bottom = l->y;
- for (n = doc.nodes[row].first; n; n = doc.nodes[n].next) {
- struct br_node *nd = &doc.nodes[n];
- struct br_style cs;
- int span;
- if (nd->type == BR_NODE_TEXT) {
- if (text_node_blank(nd))
- continue;
- layout_children(l, row, pst, depth, link);
- return;
- }
- if (nd->tag != BR_TAG_TD && nd->tag != BR_TAG_TH) {
- layout_children(l, row, pst, depth, link);
- return;
- }
- br_style_for(&doc, n, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE)
- continue;
- if (ncell >= 12) {
- layout_children(l, row, pst, depth, link);
- return;
- }
- span = nd->colspan ? nd->colspan : 1;
- if (span > 12 - col)
- span = 12 - col;
- if (span < 1)
- continue;
- cell[ncell] = n;
- colstart[ncell] = col;
- colspan[ncell] = span;
- col += span;
- ncell++;
- }
- if (ncell < 1 || avail < 32) {
- layout_children(l, row, pst, depth, link);
- return;
- }
- table_columns(l, doc.nodes[row].parent, pst, avail, raw, 12, &cols,
- &total);
- if (cols < 2 || total <= 0) {
- int c;
- for (c = 0; c < ncell; c++) {
- struct br_style cs;
- br_style_for(&doc, cell[c], pst, &cs);
- raw[c] = intrinsic_width(l->ui, cell[c], &cs, 0,
- avail) +
- mp_px(&cs, 7, avail) +
- mp_px(&cs, 5, avail) +
- mp_px(&cs, 3, avail) +
- mp_px(&cs, 1, avail);
- if (raw[c] < 24)
- raw[c] = 24;
- total += raw[c];
- }
- }
- prefix[0] = row_left;
- for (n = 0; n < (unsigned int)cols && n < 12; n++)
- prefix[n + 1] = prefix[n] + raw[n];
- for (n = 0; n < (unsigned int)ncell; n++) {
- struct br_style cs;
- int start = colstart[n];
- int end = start + colspan[n];
- int w;
- if (end > cols)
- end = cols;
- if (start >= cols)
- continue;
- w = prefix[end] - prefix[start];
- br_style_for(&doc, cell[n], pst, &cs);
- /* The column width already reflects the cell's width property. */
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- if (w < 24)
- w = 24;
- if (w < 8)
- w = 8;
- l->left = prefix[start];
- l->right = l->left + w;
- l->y = y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, cell[n], &cs, depth + 1, link);
- if (l->y > bottom)
- bottom = l->y;
- }
- l->left = row_left;
- l->right = row_right;
- l->y = bottom;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- }
- #define TBL_MAX_ROWS 32
- #define TBL_MAX_COLS 12
- #define TBL_MAX_CELLS 128
- struct tbl_cell {
- unsigned int node;
- unsigned char row, col, rowspan, colspan;
- int height;
- };
- static struct tbl_cell tbl_cells[TBL_MAX_CELLS];
- static unsigned short tbl_at[TBL_MAX_ROWS][TBL_MAX_COLS];
- static int table_active;
- static int table_has_rowspan(unsigned int table)
- {
- unsigned int r, c;
- for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
- struct br_node *rn = &doc.nodes[r];
- if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
- continue;
- for (c = rn->first; c; c = doc.nodes[c].next) {
- struct br_node *cn = &doc.nodes[c];
- if (cn->type == BR_NODE_ELEM &&
- (cn->tag == BR_TAG_TD || cn->tag == BR_TAG_TH) &&
- cn->rowspan > 1)
- return 1;
- }
- }
- return 0;
- }
- static void table_cell_style(unsigned int row, unsigned int cell,
- const struct br_style *st, struct br_style *out)
- {
- struct br_style trs;
- br_style_for(&doc, row, st, &trs);
- br_style_for(&doc, cell, &trs, out);
- }
- /* Tables that use rowspan need row heights before anything is painted. This
- * path builds a small occupancy grid, measures the natural cell heights, then
- * lays every cell out at its row/column position. Tables without rowspan
- * keep the lighter per-row path in layout_table_row(). */
- static void layout_table(struct lay *l, unsigned int table,
- const struct br_style *st, int depth,
- unsigned short link)
- {
- unsigned int rows[TBL_MAX_ROWS];
- int rowh[TBL_MAX_ROWS], rowy[TBL_MAX_ROWS];
- int raw[TBL_MAX_COLS], prefix[TBL_MAX_COLS + 1], fixed[TBL_MAX_COLS];
- int nrows = 0, ncells = 0, cols = 0, total = 0, fixed_sum = 0;
- int free_raw = 0, free_cols = 0;
- int table_left = l->left, table_right = l->right;
- int top = l->y, avail = table_right - table_left;
- unsigned int r, c;
- int i, rr, cc;
- if (depth > 24 || table_active) {
- layout_children(l, table, st, depth, link);
- return;
- }
- table_active = 1;
- for (r = doc.nodes[table].first;
- r && nrows < TBL_MAX_ROWS; r = doc.nodes[r].next) {
- struct br_node *rn = &doc.nodes[r];
- if (rn->type == BR_NODE_ELEM && rn->tag == BR_TAG_TR)
- rows[nrows++] = r;
- }
- if (!nrows) {
- table_active = 0;
- layout_children(l, table, st, depth, link);
- return;
- }
- for (rr = 0; rr < nrows; rr++)
- for (cc = 0; cc < TBL_MAX_COLS; cc++)
- tbl_at[rr][cc] = 0;
- for (rr = 0; rr < nrows; rr++) {
- int col = 0;
- for (c = doc.nodes[rows[rr]].first; c;
- c = doc.nodes[c].next) {
- struct br_node *cn = &doc.nodes[c];
- struct br_style cs;
- int rs, csn;
- if (cn->type != BR_NODE_ELEM ||
- (cn->tag != BR_TAG_TD &&
- cn->tag != BR_TAG_TH))
- continue;
- table_cell_style(rows[rr], c, st, &cs);
- if (cs.display == BR_DISPLAY_NONE)
- continue;
- while (col < TBL_MAX_COLS && tbl_at[rr][col])
- col++;
- if (col >= TBL_MAX_COLS)
- break;
- rs = cn->rowspan ? cn->rowspan : 1;
- csn = cn->colspan ? cn->colspan : 1;
- if (rr + rs > nrows)
- rs = nrows - rr;
- if (col + csn > TBL_MAX_COLS)
- csn = TBL_MAX_COLS - col;
- if (rs < 1 || csn < 1)
- continue;
- if (ncells >= TBL_MAX_CELLS)
- break;
- tbl_cells[ncells].node = c;
- tbl_cells[ncells].row = (unsigned char)rr;
- tbl_cells[ncells].col = (unsigned char)col;
- tbl_cells[ncells].rowspan = (unsigned char)rs;
- tbl_cells[ncells].colspan = (unsigned char)csn;
- tbl_cells[ncells].height = 0;
- for (i = rr; i < rr + rs; i++)
- for (int j = col; j < col + csn; j++)
- tbl_at[i][j] =
- (unsigned short)(ncells + 1);
- ncells++;
- col += csn;
- if (col > cols)
- cols = col;
- }
- }
- if (!ncells || cols < 1) {
- table_active = 0;
- layout_children(l, table, st, depth, link);
- return;
- }
- for (i = 0; i < cols; i++) {
- raw[i] = 0;
- fixed[i] = 0;
- }
- /* Intrinsic widths of single-column cells. */
- for (i = 0; i < ncells; i++) {
- struct br_style cs;
- int w;
- table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
- st, &cs);
- w = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0, avail) +
- mp_px(&cs, 7, avail) +
- mp_px(&cs, 5, avail) +
- mp_px(&cs, 3, avail) +
- mp_px(&cs, 1, avail) +
- cs.border_w[0] + cs.border_w[1] +
- cs.border_w[2] + cs.border_w[3];
- if (w < 24)
- w = 24;
- if (tbl_cells[i].colspan == 1) {
- int col = tbl_cells[i].col;
- if (w > raw[col])
- raw[col] = w;
- }
- }
- /* A spanning cell only needs the sum of its columns to be large
- * enough; put the deficit in the last column. */
- for (i = 0; i < ncells; i++) {
- struct br_style cs;
- int w, sum, col, span;
- if (tbl_cells[i].colspan < 2)
- continue;
- table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
- st, &cs);
- w = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0, avail) +
- mp_px(&cs, 7, avail) +
- mp_px(&cs, 5, avail) +
- mp_px(&cs, 3, avail) +
- mp_px(&cs, 1, avail) +
- cs.border_w[0] + cs.border_w[1] +
- cs.border_w[2] + cs.border_w[3];
- col = tbl_cells[i].col;
- span = tbl_cells[i].colspan;
- sum = 0;
- for (int j = 0; j < span; j++)
- sum += raw[col + j];
- if (w > sum)
- raw[col + span - 1] += w - sum;
- }
- /* Explicit/percentage widths, as in table_columns(). */
- for (i = 0; i < ncells; i++) {
- struct br_style cs;
- int col;
- if (tbl_cells[i].colspan != 1)
- continue;
- table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
- st, &cs);
- col = tbl_cells[i].col;
- if (cs.width_kind == 1) {
- raw[col] = cs.width;
- fixed[col] = 1;
- } else if (cs.width_kind == 2 || cs.width_kind == 3) {
- raw[col] = style_width_px(&cs, avail);
- fixed[col] = 1;
- }
- }
- for (i = 0; i < cols; i++) {
- if (raw[i] < 8)
- raw[i] = 8;
- total += raw[i];
- if (fixed[i])
- fixed_sum += raw[i];
- else {
- free_raw += raw[i];
- free_cols++;
- }
- }
- if (fixed_sum > 0 && fixed_sum < avail) {
- int free_avail = avail - fixed_sum;
- if (free_raw > 0) {
- for (i = 0; i < cols; i++)
- if (!fixed[i])
- raw[i] = free_avail * raw[i] /
- free_raw;
- } else if (free_cols > 0) {
- for (i = 0; i < cols; i++)
- if (!fixed[i])
- raw[i] = free_avail / free_cols;
- }
- } else if (total < avail && total > 0) {
- for (i = 0; i < cols; i++)
- raw[i] = raw[i] * avail / total;
- }
- prefix[0] = table_left;
- for (i = 0; i < cols; i++)
- prefix[i + 1] = prefix[i] + raw[i];
- /* Measure natural cell heights with the column widths. */
- for (i = 0; i < ncells; i++) {
- struct br_style cs;
- int col = tbl_cells[i].col;
- int span = tbl_cells[i].colspan;
- int w = prefix[col + span] - prefix[col];
- int sl = l->left, sr = l->right, sy = l->y;
- int slx = l->line_x, srx = l->run_x, sly = l->line_y;
- int slh = l->line_h, snr = l->nruns, ssp = l->space_pending;
- int sal = l->align, sdraw = l->draw;
- struct ngui *sui = l->ui;
- if (w < 8)
- w = 8;
- table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
- st, &cs);
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- l->draw = 0;
- l->ui = 0;
- l->left = prefix[col];
- l->right = l->left + w;
- l->y = 0;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = 0;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, tbl_cells[i].node, &cs, depth + 1, link);
- tbl_cells[i].height = block_h[tbl_cells[i].node];
- l->left = sl;
- l->right = sr;
- l->y = sy;
- l->line_x = slx;
- l->run_x = srx;
- l->line_y = sly;
- l->line_h = slh;
- l->nruns = snr;
- l->space_pending = ssp;
- l->align = sal;
- l->draw = sdraw;
- l->ui = sui;
- }
- for (i = 0; i < nrows; i++)
- rowh[i] = 0;
- for (i = 0; i < ncells; i++) {
- int row = tbl_cells[i].row;
- if (tbl_cells[i].rowspan == 1) {
- if (tbl_cells[i].height > rowh[row])
- rowh[row] = tbl_cells[i].height;
- }
- }
- for (i = 0; i < ncells; i++) {
- int row = tbl_cells[i].row;
- int span = tbl_cells[i].rowspan;
- int sum = 0;
- if (span < 2)
- continue;
- for (int j = 0; j < span; j++)
- sum += rowh[row + j];
- if (sum < tbl_cells[i].height)
- rowh[row + span - 1] +=
- tbl_cells[i].height - sum;
- }
- for (i = 0; i < nrows; i++)
- if (rowh[i] < 8)
- rowh[i] = 8;
- rowy[0] = top;
- for (i = 1; i < nrows; i++)
- rowy[i] = rowy[i - 1] + rowh[i - 1];
- if (!l->draw) {
- l->y = rowy[nrows - 1] + rowh[nrows - 1];
- table_active = 0;
- return;
- }
- for (i = 0; i < ncells; i++) {
- struct br_style cs;
- int col = tbl_cells[i].col;
- int span = tbl_cells[i].colspan;
- int row = tbl_cells[i].row;
- int rspan = tbl_cells[i].rowspan;
- int w = prefix[col + span] - prefix[col];
- int h = 0;
- int sl = l->left, sr = l->right, sy = l->y;
- int slx = l->line_x, srx = l->run_x, sly = l->line_y;
- int slh = l->line_h, snr = l->nruns, ssp = l->space_pending;
- int sal = l->align;
- int deco;
- for (int j = 0; j < rspan; j++)
- h += rowh[row + j];
- table_cell_style(rows[row], tbl_cells[i].node, st, &cs);
- cs.width_kind = 0;
- cs.max_width_kind = 0;
- cs.box_sizing = 0;
- deco = mp_px(&cs, 4, avail) +
- mp_px(&cs, 6, avail) +
- cs.border_w[0] + cs.border_w[2];
- cs.height_kind = 1;
- cs.height = h > deco ? h - deco : 0;
- /* The block painter reads block_h[] from the measure pass; make
- * the table cell fill its row/rowspan, not just its content. */
- if (h > 0 && h < 65536)
- block_h[tbl_cells[i].node] = (unsigned short)h;
- l->left = prefix[col];
- l->right = l->left + w;
- l->y = rowy[row];
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = cs.align;
- layout_block(l, tbl_cells[i].node, &cs, depth + 1, link);
- l->left = sl;
- l->right = sr;
- l->y = sy;
- l->line_x = slx;
- l->run_x = srx;
- l->line_y = sly;
- l->line_h = slh;
- l->nruns = snr;
- l->space_pending = ssp;
- l->align = sal;
- }
- l->y = rowy[nrows - 1] + rowh[nrows - 1];
- table_active = 0;
- }
- /* Positioned elements (fixed headers, overlays) are drawn after the normal
- * flow so they sit above it, like CSS painting order. Their flow origin is
- * saved for absolute boxes; fixed boxes only need the viewport rect. */
- struct br_deferred {
- unsigned int node;
- int flow_y, flow_left, flow_right;
- unsigned short link;
- signed short z_index;
- unsigned char z_set;
- unsigned int order;
- };
- static struct br_deferred deferred[16];
- static int ndeferred;
- static void layout_children(struct lay *l, unsigned int parent,
- const struct br_style *pst, int depth,
- unsigned short link)
- {
- unsigned int n;
- if (depth > 24)
- return;
- n = doc.nodes[parent].first;
- while (n) {
- struct br_node *nd = &doc.nodes[n];
- struct br_style cs;
- unsigned short lk = link;
- if (nd->type == BR_NODE_TEXT) {
- emit_text(l, doc.text + nd->text_off, nd->text_len, pst,
- link, (nd->flags & BR_NODE_PREFORMAT) != 0);
- n = nd->next;
- continue;
- }
- br_style_for(&doc, n, pst, &cs);
- if (cs.display == BR_DISPLAY_NONE) {
- n = nd->next;
- continue;
- }
- if (cs.clear && l->float_active) {
- flush_line(l);
- if (l->y < l->float_bottom)
- l->y = l->float_bottom;
- l->float_active = 0;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_left = l->left;
- l->line_right = l->right;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- }
- if (nd->tag == BR_TAG_A && nd->href_off)
- lk = (unsigned short)n;
- if (cs.pos == 1 || cs.pos == 2) {
- if (l->draw && ndeferred < 16) {
- deferred[ndeferred].node = n;
- deferred[ndeferred].flow_y = l->y;
- deferred[ndeferred].flow_left = l->left;
- deferred[ndeferred].flow_right = l->right;
- deferred[ndeferred].link = lk;
- deferred[ndeferred].z_index = cs.z_index;
- deferred[ndeferred].z_set = cs.z_set;
- deferred[ndeferred].order = (unsigned int)ndeferred;
- ndeferred++;
- } else {
- layout_block(l, n, &cs, depth + 1, lk);
- }
- if (cs.pos == 1 && block_h[n] &&
- doc.nodes[n].class_off) {
- const char *cl = doc.attrs +
- doc.nodes[n].class_off;
- if ((contains_ci(cl, "poster-wrapper") ||
- contains_ci(cl, "ts-picture")) &&
- !has_skeleton_sibling(n))
- l->y += block_h[n];
- }
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_BR) {
- flush_line(l);
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_HR) {
- flush_line(l);
- if (l->draw)
- ngui_fill_rect(l->ui,
- (struct ngui_rect){l->left, l->y + 2,
- l->right - l->left, 1}, BR_GRAY);
- l->y += 8;
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_TEXTAREA ||
- nd->tag == BR_TAG_SELECT || nd->tag == BR_TAG_BUTTON) {
- if (!(nd->tag == BR_TAG_INPUT &&
- s_eq(br_type(&doc, n), "hidden")))
- add_form_run(l, n, &cs, lk);
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_IMG) {
- add_image_run(l, n, &cs, lk);
- n = nd->next;
- continue;
- }
- if (nd->tag == BR_TAG_VIDEO) {
- layout_block(l, n, &cs, depth + 1, lk);
- n = nd->next;
- continue;
- }
- if (cs.float_left) {
- n = layout_float_group(l, n, pst, depth, lk);
- continue;
- }
- if (cs.display == BR_DISPLAY_INLINE_BLOCK) {
- n = layout_inline_blocks(l, n, pst, depth, link);
- continue;
- }
- if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag))
- layout_block(l, n, &cs, depth + 1, lk);
- else {
- layout_children(l, n, &cs, depth + 1, lk);
- if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
- l->space_pending = 1;
- }
- n = nd->next;
- }
- }
- /* One layout state, kept out of the 8 KB user stack: it holds 96 line
- * runs and a recursive page can otherwise overflow the stack. */
- static struct lay layout_state;
- static int measure_page(int left, int right)
- {
- struct lay *l = &layout_state;
- struct br_style base = base_style();
- unsigned int i;
- for (i = 0; i < BR_MAX_NODES; i++) {
- block_h[i] = 0;
- img_top[i] = 0;
- img_h[i] = 0;
- layout_boxes[i].valid = 0;
- }
- l->draw = 0;
- l->ui = 0;
- l->left = left;
- l->right = right;
- l->y = 0;
- l->line_x = left;
- l->run_x = left;
- l->line_left = left;
- l->line_right = right;
- l->line_y = 0;
- l->line_h = 0;
- l->align = BR_ALIGN_LEFT;
- l->float_active = 0;
- l->parent_top = 0;
- l->depth = 0;
- l->nruns = 0;
- l->space_pending = 0;
- layout_children(l, doc.root, &base, 0, 0);
- return l->y + 8;
- }
- static int deferred_cmp(const struct br_deferred *a,
- const struct br_deferred *b)
- {
- int az = a->z_set ? a->z_index : 0;
- int bz = b->z_set ? b->z_index : 0;
- if (az != bz)
- return az < bz ? -1 : 1;
- if (a->order != b->order)
- return a->order < b->order ? -1 : 1;
- return 0;
- }
- static void sort_deferred(void)
- {
- int i;
- for (i = 1; i < ndeferred; i++) {
- struct br_deferred key = deferred[i];
- int j = i;
- while (j > 0 && deferred_cmp(&deferred[j - 1], &key) > 0) {
- deferred[j] = deferred[j - 1];
- j--;
- }
- deferred[j] = key;
- }
- }
- static void render_page(int left, int right, int y0)
- {
- struct lay *l = &layout_state;
- struct br_style base = base_style();
- l->draw = 1;
- l->ui = &ui;
- l->left = left;
- l->right = right;
- l->y = y0;
- l->line_x = left;
- l->run_x = left;
- l->line_left = left;
- l->line_right = right;
- l->line_y = y0;
- l->line_h = 0;
- l->align = BR_ALIGN_LEFT;
- l->float_active = 0;
- l->parent_top = 0;
- l->depth = 0;
- l->nruns = 0;
- l->space_pending = 0;
- nhits = 0;
- ndeferred = 0;
- layout_children(l, doc.root, &base, 0, 0);
- sort_deferred();
- {
- int i;
- for (i = 0; i < ndeferred; i++) {
- struct br_style st;
- br_style_for(&doc, deferred[i].node, &base, &st);
- if (st.display == BR_DISPLAY_NONE)
- continue;
- l->draw = 1;
- l->left = deferred[i].flow_left;
- l->right = deferred[i].flow_right;
- l->y = deferred[i].flow_y;
- l->line_x = l->left;
- l->run_x = l->left;
- l->line_y = l->y;
- l->line_h = 0;
- l->nruns = 0;
- l->space_pending = 0;
- l->align = st.align;
- if (st.pos == 2 && block_h[deferred[i].node]) {
- struct ngui_rect clip;
- int ch = block_h[deferred[i].node] + 24;
- clip.x = page_rect.x;
- clip.y = page_rect.y;
- clip.w = page_rect.w - 12;
- if (ch > page_rect.h)
- ch = page_rect.h;
- clip.h = ch;
- ngui_push_clip(&ui, clip);
- if (st.has_bg)
- ngui_fill_rect(&ui, clip, st.bg);
- layout_block(l, deferred[i].node, &st, 0,
- deferred[i].link);
- ngui_pop_clip(&ui);
- } else {
- layout_block(l, deferred[i].node, &st, 0,
- deferred[i].link);
- }
- }
- }
- }
- /* ---- UI ---- */
- static int find_hit(int x, int y)
- {
- int i;
- for (i = nhits - 1; i >= 0; i--) {
- struct hit *h = &hits[i];
- if (x >= h->x && y >= h->y && x < h->x + h->w &&
- y < h->y + h->h)
- return (int)h->node;
- }
- return 0;
- }
- static void draw_toolbar(struct ngui_rect r, unsigned int *action)
- {
- int x = r.x + 4, y = r.y + 4, h = 22;
- int compact = r.w < 560;
- int go_w = r.w < 420 ? 32 : 42;
- int in_x, in_w, res;
- ngui_fill_rect(&ui, r, ui.theme.panel);
- ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
- ui.theme.border);
- if (ngui_icon_button(&ui, ID_BACK, (struct ngui_rect){x, y, 28, h},
- NGUI_ICON_CHEVRON_LEFT))
- *action |= ACT_BACK;
- x += 32;
- if (!compact) {
- if (ngui_icon_button(&ui, ID_FORWARD,
- (struct ngui_rect){x, y, 28, h},
- NGUI_ICON_CHEVRON_RIGHT))
- *action |= ACT_FORWARD;
- x += 32;
- }
- if (ngui_icon_button(&ui, ID_RELOAD, (struct ngui_rect){x, y, 28, h},
- NGUI_ICON_REFRESH))
- *action |= ACT_RELOAD;
- x += 32;
- if (!compact) {
- if (ngui_icon_button(&ui, ID_HOME,
- (struct ngui_rect){x, y, 28, h},
- NGUI_ICON_HOME))
- *action |= ACT_HOME;
- x += 32;
- }
- in_x = x + 2;
- in_w = r.x + r.w - 6 - go_w - 4 - in_x;
- if (in_w < 60)
- in_w = 60;
- res = ngui_text_input(&ui, ID_URL,
- (struct ngui_rect){in_x, y, in_w, h}, &url_in);
- if (res & NGUI_INPUT_SUBMIT)
- *action |= ACT_NAV;
- if (res & NGUI_INPUT_CANCEL)
- ngui_set_focus(&ui, 0);
- if (ngui_button(&ui, ID_GO,
- (struct ngui_rect){r.x + r.w - 4 - go_w, y, go_w, h},
- "Go"))
- *action |= ACT_NAV;
- }
- static int page_hover_node(void)
- {
- if (!ui.inside)
- return 0;
- if (ui.mouse_x < page_rect.x ||
- ui.mouse_x >= page_rect.x + page_rect.w - 12 ||
- ui.mouse_y < page_rect.y ||
- ui.mouse_y >= page_rect.y + page_rect.h)
- return 0;
- return find_hit(ui.mouse_x, ui.mouse_y);
- }
- static void blit_page(struct ngui_rect page, unsigned short *src, int sw)
- {
- int x, y, w = page.w, h = page.h;
- if (!src || w <= 0 || h <= 0)
- return;
- for (y = 0; y < h; y++) {
- unsigned short *s = src + (page.y + y) * sw + page.x;
- unsigned short *d = ui.px + (page.y + y) * ui.w + page.x;
- for (x = 0; x + 1 < w; x += 2) {
- *(unsigned int *)(d + x) = *(unsigned int *)(s + x);
- }
- if (x < w)
- d[x] = s[x];
- }
- }
- static void draw_page(struct ngui_rect page)
- {
- int left, right, ch;
- int old_panel, old_dim;
- unsigned short *saved_px;
- int saved_w, saved_h, use_buf;
- page_rect = page;
- if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&
- page_cache_valid && layout_valid &&
- !ui.pressed && !(ui.buttons & 1) && !ui.released &&
- page_scroll.offset == page_cache_scroll &&
- page_cache_nnode == doc.nnode &&
- page_cache_x == page.x && page_cache_y == page.y &&
- page_cache_w == page.w && page_cache_h == page.h) {
- blit_page(page, page_backbuf, pagebuf_w);
- return;
- }
- ngui_fill_rect(&ui, page, ui.theme.bg);
- if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
- const char *s = page_loading ? "Loading..." : "No page";
- int w = (int)ngui_text_width_ui(&ui, s);
- ngui_text(&ui, page.x + (page.w - w) / 2,
- page.y + page.h / 2 - 8, s, ui.theme.text_dim);
- page_scroll.content_h = 0;
- page_scroll.view_h = page.h;
- page_cache_valid = 0;
- return;
- }
- if (page.w < 40 || page.h < 40) {
- page_cache_valid = 0;
- return;
- }
- saved_px = ui.px;
- saved_w = ui.w;
- saved_h = ui.h;
- use_buf = page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h;
- if (use_buf) {
- ui.px = page_backbuf;
- ui.w = pagebuf_w;
- ui.h = pagebuf_h;
- ngui_fill_rect(&ui, page, ui.theme.bg);
- }
- left = page.x + 8;
- right = page.x + page.w - 16;
- if (!layout_valid || left != layout_left || right != layout_right ||
- layout_nnode != doc.nnode) {
- layout_content_h = measure_page(left, right);
- layout_valid = 1;
- layout_left = left;
- layout_right = right;
- layout_nnode = doc.nnode;
- }
- ch = layout_content_h;
- page_scroll.content_h = ch;
- page_scroll.view_h = page.h;
- ngui_scroll_begin(&ui, page, &page_scroll);
- render_page(left, right, page.y + 4 - page_scroll.offset);
- old_panel = ui.theme.panel_alt;
- old_dim = ui.theme.text_dim;
- ui.theme.panel_alt = 0xC618;
- ui.theme.text_dim = 0x8410;
- ngui_scroll_end(&ui, page, &page_scroll);
- ui.theme.panel_alt = old_panel;
- ui.theme.text_dim = old_dim;
- if (use_buf) {
- ui.px = saved_px;
- ui.w = saved_w;
- ui.h = saved_h;
- blit_page(page, page_backbuf, pagebuf_w);
- page_cache_valid = 1;
- page_cache_scroll = page_scroll.offset;
- page_cache_nnode = doc.nnode;
- page_cache_x = page.x;
- page_cache_y = page.y;
- page_cache_w = page.w;
- page_cache_h = page.h;
- } else {
- page_cache_valid = 0;
- }
- }
- static void draw_status(struct ngui_rect r)
- {
- char msg[BR_URL_MAX + 32];
- unsigned int n = 0;
- int hover = page_hover_node();
- unsigned short color;
- const char *kind;
- ngui_fill_rect(&ui, r, ui.theme.panel);
- ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
- ui.theme.border);
- if (hover) {
- const char *href = br_href(&doc, (unsigned int)hover);
- char url[BR_URL_MAX];
- if (resolve_link(page_url, href, url, sizeof(url)) == 0) {
- s_append(msg, &n, "Link: ", sizeof(msg));
- s_append(msg, &n, url, sizeof(msg));
- } else {
- s_copy(msg, "Link", sizeof(msg));
- }
- color = ui.theme.accent;
- } else if (status_text[0]) {
- s_copy(msg, status_text, sizeof(msg));
- color = status_error ? ui.theme.error : ui.theme.text;
- } else if (page_title[0]) {
- s_copy(msg, page_title, sizeof(msg));
- color = ui.theme.text;
- } else {
- s_copy(msg, "Ready", sizeof(msg));
- color = ui.theme.text_dim;
- }
- kind = s_eq(page_url, "about:home") ? "HOME" :
- page_is_local ? "FILE" :
- (starts_with(page_url, "https://") ? "HTTPS" : "HTTP");
- ngui_text_ellipsis(&ui, r, r.x + 6, r.y + 1, msg, color);
- {
- int kw = (int)ngui_text_width_ui(&ui, kind);
- ngui_text(&ui, r.x + r.w - 6 - kw, r.y + 1, kind,
- ui.theme.text_dim);
- }
- }
- static void draw(unsigned int *action)
- {
- struct ngui_rect r = {0, 0, ui.w, ui.h};
- struct ngui_rect toolbar, status, page;
- toolbar = ngui_cut_top(&r, BR_TOOLBAR_H);
- status = ngui_cut_bottom(&r, BR_STATUS_H);
- page = r;
- draw_toolbar(toolbar, action);
- draw_page(page);
- draw_status(status);
- }
- static void redraw_now(void)
- {
- unsigned int action = 0;
- ngui_begin(&ui);
- draw(&action);
- ngui_end(&ui);
- }
- static void handle_keys(unsigned int *action)
- {
- int k, scrolled = 0;
- if (form_focus >= doc.nnode)
- form_focus = 0;
- if (form_focus && !ngui_focus(&ui)) {
- while ((k = ngui_key(&ui)) != 0)
- form_key(k);
- return;
- }
- /* Batch queued page keys: a slow full-page render must not make a
- * held Page Down advance only once every few frames. */
- while (scrolled < 3 && ngui_scroll_keys(&ui, &page_scroll))
- scrolled++;
- if (scrolled)
- ngui_invalidate(&ui);
- if (ngui_focus(&ui))
- return;
- while ((k = ngui_key(&ui)) != 0) {
- switch (k) {
- case NGUI_KEY_ESC:
- *action |= ACT_CLOSE;
- break;
- case NGUI_KEY_TAB:
- case 0x0C: /* Ctrl+L */
- ngui_set_focus(&ui, ID_URL);
- break;
- case 0x12: /* Ctrl+R */
- *action |= ACT_RELOAD;
- break;
- case NGUI_KEY_BACKSPACE:
- *action |= ACT_BACK;
- break;
- case NGUI_KEY_UP:
- ngui_scroll_to(&page_scroll,
- page_scroll.offset - 24);
- ngui_invalidate(&ui);
- break;
- case NGUI_KEY_DOWN:
- ngui_scroll_to(&page_scroll,
- page_scroll.offset + 24);
- ngui_invalidate(&ui);
- break;
- case NGUI_KEY_HOME:
- ngui_scroll_to(&page_scroll, 0);
- ngui_invalidate(&ui);
- break;
- case NGUI_KEY_END:
- ngui_scroll_to(&page_scroll,
- page_scroll.content_h -
- page_scroll.view_h);
- ngui_invalidate(&ui);
- break;
- default:
- break;
- }
- }
- }
- static unsigned int form_ancestor(unsigned int node)
- {
- unsigned int p = node;
- while (p && p < doc.nnode) {
- if (doc.nodes[p].tag == BR_TAG_FORM &&
- doc.nodes[p].type == BR_NODE_ELEM)
- return p;
- p = doc.nodes[p].parent;
- }
- return 0;
- }
- static int form_descendant(unsigned int node, unsigned int form)
- {
- unsigned int p = node;
- while (p && p < doc.nnode) {
- if (p == form)
- return 1;
- p = doc.nodes[p].parent;
- }
- return 0;
- }
- static void form_reset_form(unsigned int form)
- {
- unsigned int i;
- for (i = 0; i < FORM_MAX; i++)
- if (form_states[i].used &&
- form_descendant(form_states[i].node, form))
- form_states[i].used = 0;
- form_focus = 0;
- }
- static void form_url_append(char *dst, unsigned int *n, unsigned int cap,
- const char *s)
- {
- static const char hex[] = "0123456789ABCDEF";
- while (s && *s && *n + 1 < cap) {
- unsigned char c = (unsigned char)*s++;
- if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
- (c >= '0' && c <= '9') || c == '-' || c == '_' ||
- c == '.' || c == '~') {
- dst[(*n)++] = (char)c;
- } else if (c == ' ') {
- dst[(*n)++] = '+';
- } else if (*n + 3 < cap) {
- dst[(*n)++] = '%';
- dst[(*n)++] = hex[c >> 4];
- dst[(*n)++] = hex[c & 15];
- }
- }
- dst[*n] = '\0';
- }
- static void form_add_param(char *dst, unsigned int *n, unsigned int cap,
- const char *name, const char *value, int *first)
- {
- if (!name || !name[0])
- return;
- if (!*first && *n + 1 < cap)
- dst[(*n)++] = '&';
- *first = 0;
- form_url_append(dst, n, cap, name);
- if (*n + 1 < cap)
- dst[(*n)++] = '=';
- form_url_append(dst, n, cap, value ? value : "");
- dst[*n] = '\0';
- }
- static int form_submit_form(unsigned int node, int fire_event)
- {
- unsigned int form = form_ancestor(node);
- char action[BR_URL_MAX], query[2048];
- unsigned int qn = 0, i;
- int first = 1, is_post = 0;
- const char *a, *method;
- if (!form)
- return 0;
- if (fire_event && !js_browser_event(form, "submit", 1, 1))
- return 0;
- method = br_find_attr(&doc, form, "method");
- if (method && (s_eq(method, "post") || s_eq(method, "POST")))
- is_post = 1;
- a = br_find_attr(&doc, form, "action");
- if (a && a[0]) {
- if (resolve_link(page_url, a, action, sizeof(action)) != 0)
- return 0;
- } else {
- s_copy(action, page_url, sizeof(action));
- }
- query[0] = '\0';
- for (i = 0; i < doc.nnode; i++) {
- struct br_node *nd = &doc.nodes[i];
- struct form_state *fs;
- const char *name;
- const char *value = "";
- if (nd->type != BR_NODE_ELEM || i == form ||
- !form_descendant(i, form))
- continue;
- if (nd->tag != BR_TAG_INPUT && nd->tag != BR_TAG_TEXTAREA &&
- nd->tag != BR_TAG_SELECT)
- continue;
- if (form_disabled(i))
- continue;
- name = br_find_attr(&doc, i, "name");
- if (!name || !name[0])
- continue;
- fs = form_get(i, 1);
- if (!fs)
- continue;
- if (nd->tag == BR_TAG_SELECT) {
- char opt[64];
- if (!form_option_text(i, fs->selected, opt,
- sizeof(opt)))
- s_copy(opt, "", sizeof(opt));
- value = opt;
- {
- unsigned int c, oi = 0;
- for (c = nd->first; c;
- c = doc.nodes[c].next) {
- if (doc.nodes[c].type != BR_NODE_ELEM ||
- doc.nodes[c].tag != BR_TAG_OPTION)
- continue;
- if (oi == fs->selected) {
- const char *v =
- br_value(&doc, c);
- if (v && v[0])
- value = v;
- break;
- }
- oi++;
- }
- }
- } else if (nd->tag == BR_TAG_INPUT) {
- int kind = form_kind_of(i);
- if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
- if (!fs->checked)
- continue;
- value = br_value(&doc, i);
- if (!value || !value[0])
- value = "on";
- } else if (kind == FORM_SUBMIT || kind == FORM_RESET ||
- kind == FORM_BUTTON) {
- if (i != node)
- continue;
- value = fs->value;
- } else {
- value = fs->value;
- }
- } else {
- value = fs->value;
- }
- form_add_param(query, &qn, sizeof(query), name, value, &first);
- }
- if (is_post) {
- if (action[0] == '/') {
- set_status("POST forms need an HTTP action URL", 1, 4000);
- ngui_invalidate(&ui);
- return 0;
- }
- if (qn >= sizeof(pending_post))
- qn = sizeof(pending_post) - 1;
- if (qn)
- memcpy(pending_post, query, qn);
- pending_post[qn] = '\0';
- pending_post_len = qn;
- } else {
- pending_post_len = 0;
- if (qn) {
- unsigned int an = s_len(action);
- if (an + qn + 2 < sizeof(action)) {
- action[an++] = strchr(action, '?') ? '&' : '?';
- action[an] = '\0';
- s_copy(action + an, query,
- sizeof(action) - an);
- }
- }
- }
- s_copy(pending_url, action, sizeof(pending_url));
- pending_action |= ACT_NAV;
- cancel_fetch = 1;
- ngui_invalidate(&ui);
- return 1;
- }
- static void form_reset_event(unsigned int form)
- {
- if (form && !js_browser_event(form, "reset", 1, 1))
- return;
- if (form)
- form_reset_form(form);
- else
- form_reset_state();
- form_dirty = 0;
- layout_valid = 0;
- ngui_invalidate(&ui);
- }
- static void form_blur(void)
- {
- unsigned int node = form_focus;
- form_focus = 0;
- if (node && form_dirty) {
- form_dirty = 0;
- js_browser_event(node, "change", 1, 0);
- }
- }
- static void form_click(unsigned int node)
- {
- struct form_state *fs;
- int kind, changed = 0;
- if (!form_is_control(node) || form_disabled(node))
- return;
- if (form_focus != node)
- form_blur();
- if (!js_browser_event(node, "click", 1, 1))
- return;
- fs = form_get(node, 1);
- if (!fs)
- return;
- kind = form_kind_of(node);
- form_focus = node;
- form_dirty = 0;
- if (kind == FORM_CHECKBOX) {
- fs->checked = !fs->checked;
- changed = 1;
- } else if (kind == FORM_RADIO) {
- const char *name = br_find_attr(&doc, node, "name");
- unsigned int i;
- fs->checked = 1;
- changed = 1;
- if (name) {
- for (i = 0; i < FORM_MAX; i++) {
- struct form_state *o = &form_states[i];
- if (!o->used || o->node == node)
- continue;
- if (form_kind_of(o->node) == FORM_RADIO) {
- const char *on = br_find_attr(&doc,
- o->node,
- "name");
- if (on && s_eq(on, name))
- o->checked = 0;
- }
- }
- }
- } else if (kind == FORM_SELECT) {
- unsigned int n = form_option_count(node);
- if (n) {
- fs->selected = (unsigned char)
- ((fs->selected + 1) % n);
- changed = 1;
- }
- } else if (kind == FORM_SUBMIT) {
- form_submit_form(node, 1);
- return;
- } else if (kind == FORM_RESET) {
- form_reset_event(form_ancestor(node));
- return;
- } else if (kind == FORM_TEXT || kind == FORM_PASSWORD ||
- kind == FORM_TEXTAREA) {
- fs->caret = fs->len;
- }
- if (changed) {
- js_browser_event(node, "input", 1, 0);
- js_browser_event(node, "change", 1, 0);
- }
- layout_valid = 0;
- ngui_invalidate(&ui);
- }
- static void form_focus_next(void)
- {
- unsigned int start = form_focus ? form_focus + 1 : 0;
- unsigned int i;
- form_blur();
- for (i = start; i < doc.nnode; i++) {
- if (form_is_control(i) && !form_disabled(i) &&
- !s_eq(br_type(&doc, i), "hidden")) {
- form_focus = i;
- layout_valid = 0;
- ngui_invalidate(&ui);
- return;
- }
- }
- form_focus = 0;
- layout_valid = 0;
- ngui_invalidate(&ui);
- }
- static void form_key(int k)
- {
- struct form_state *fs;
- int kind;
- if (!form_focus || form_focus >= doc.nnode) {
- form_focus = 0;
- return;
- }
- fs = form_get(form_focus, 1);
- if (!fs || form_disabled(form_focus))
- return;
- kind = form_kind_of(form_focus);
- if (kind == FORM_SELECT) {
- unsigned int n = form_option_count(form_focus);
- int changed = 0;
- if (k == NGUI_KEY_UP && n) {
- fs->selected = (unsigned char)
- ((fs->selected + n - 1) % n);
- changed = 1;
- } else if (k == NGUI_KEY_DOWN && n) {
- fs->selected = (unsigned char)
- ((fs->selected + 1) % n);
- changed = 1;
- } else if (k == NGUI_KEY_ESC || k == '\r' || k == '\n') {
- form_blur();
- layout_valid = 0;
- ngui_invalidate(&ui);
- return;
- } else {
- return;
- }
- if (changed) {
- js_browser_event(form_focus, "input", 1, 0);
- js_browser_event(form_focus, "change", 1, 0);
- }
- } else if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
- if (k == ' ') {
- form_click(form_focus);
- return;
- }
- if (k == NGUI_KEY_ESC || k == '\r' || k == '\n') {
- form_blur();
- layout_valid = 0;
- ngui_invalidate(&ui);
- }
- return;
- } else if (kind == FORM_BUTTON || kind == FORM_SUBMIT ||
- kind == FORM_RESET) {
- if (k == NGUI_KEY_ESC) {
- form_blur();
- layout_valid = 0;
- ngui_invalidate(&ui);
- } else if (k == '\r' || k == '\n' || k == ' ') {
- form_click(form_focus);
- }
- return;
- } else if (k == NGUI_KEY_TAB) {
- form_focus_next();
- return;
- } else if (k == NGUI_KEY_ESC) {
- form_blur();
- layout_valid = 0;
- ngui_invalidate(&ui);
- return;
- } else if (k == '\r' || k == '\n') {
- unsigned int node = form_focus;
- form_blur();
- if (form_ancestor(node))
- form_submit_form(node, 1);
- layout_valid = 0;
- ngui_invalidate(&ui);
- return;
- } else if (k == NGUI_KEY_BACKSPACE) {
- if (fs->caret) {
- unsigned int i = --fs->caret;
- for (; i < fs->len; i++)
- fs->value[i] = fs->value[i + 1];
- if (fs->len)
- fs->len--;
- form_dirty = 1;
- js_browser_event(form_focus, "input", 1, 0);
- }
- } else if (k == NGUI_KEY_DELETE) {
- if (fs->caret < fs->len) {
- unsigned int i;
- for (i = fs->caret; i < fs->len; i++)
- fs->value[i] = fs->value[i + 1];
- fs->len--;
- form_dirty = 1;
- js_browser_event(form_focus, "input", 1, 0);
- }
- } else if (k == NGUI_KEY_LEFT) {
- if (fs->caret)
- fs->caret--;
- } else if (k == NGUI_KEY_RIGHT) {
- if (fs->caret < fs->len)
- fs->caret++;
- } else if (k == NGUI_KEY_HOME) {
- fs->caret = 0;
- } else if (k == NGUI_KEY_END) {
- fs->caret = fs->len;
- } else if (k >= 32 && k < 127) {
- if (fs->len < FORM_VALUE_MAX) {
- unsigned int i;
- for (i = fs->len; i > fs->caret; i--)
- fs->value[i] = fs->value[i - 1];
- fs->value[fs->caret++] = (char)k;
- fs->len++;
- form_dirty = 1;
- js_browser_event(form_focus, "input", 1, 0);
- }
- } else {
- return;
- }
- fs->value[fs->len] = '\0';
- layout_valid = 0;
- ngui_invalidate(&ui);
- }
- static void handle_page_click(unsigned int *action)
- {
- int node;
- char url[BR_URL_MAX];
- if (!ui.pressed)
- return;
- if (ui.press_x < page_rect.x ||
- ui.press_x >= page_rect.x + page_rect.w - 12 ||
- ui.press_y < page_rect.y ||
- ui.press_y >= page_rect.y + page_rect.h)
- return;
- node = find_hit(ui.press_x, ui.press_y);
- if (!node)
- return;
- if (form_is_control((unsigned int)node)) {
- form_click((unsigned int)node);
- return;
- }
- form_blur();
- if (!js_browser_event((unsigned int)node, "click", 1, 1))
- return;
- if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
- sizeof(url)) != 0)
- return;
- s_copy(click_url, url, sizeof(click_url));
- *action |= ACT_NAV;
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- if (u_win_create(BR_W, BR_H, "Browser", &wi) != 0 &&
- u_win_create(960, 640, "Browser", &wi) != 0 &&
- u_win_create(780, 540, "Browser", &wi) != 0) {
- u_puts("browser: no graphics window\n");
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- url_in.placeholder = "http://...";
- br_doc_init(&doc);
- br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
- ui.theme.accent);
- br_set_viewport(wi.width);
- if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
- s_copy(cwd, "/", sizeof(cwd));
- heap_clear();
- origin_cache_load();
- css_cache_init();
- script_cache_init();
- page_scroll.offset = 0;
- page_scroll.content_h = 0;
- if (arg && arg[0]) {
- navigate(arg, 1);
- } else {
- load_home(1);
- /* Start ready to type: keep the home page, but leave the address
- * bar empty instead of showing "about:home". */
- ngui_input_set(&url_in, "");
- ngui_set_focus(&ui, ID_URL);
- }
- for (;;) {
- unsigned int action = 0;
- unsigned int now;
- click_url[0] = '\0';
- if (pending_action) {
- action |= pending_action & ACT_WAIT_NAV;
- if (pending_url[0])
- s_copy(click_url, pending_url, sizeof(click_url));
- pending_action = 0;
- pending_url[0] = '\0';
- cancel_fetch = 0;
- } else if (preload_active) {
- if (preload_wait)
- preload_wait = 0;
- else
- preload_step();
- } else if (preload_pending &&
- page_scroll.offset != preload_scroll) {
- preload_scroll = page_scroll.offset;
- preload_pending = 0;
- preload_node = 0;
- preload_active = 1;
- }
- js_browser_tick();
- if (pending_action)
- continue;
- now = u_ticks_ms();
- if (status_until && (int)(now - status_until) >= 0) {
- status_text[0] = '\0';
- status_until = 0;
- status_error = 0;
- ngui_invalidate(&ui);
- }
- if (!ngui_begin(&ui)) {
- /* A queued navigation (e.g. Enter in the URL bar during
- * a slow fetch) must be handled even when this frame
- * needs no redraw. */
- if (!action) {
- u_sleep_ms(10);
- continue;
- }
- } else {
- draw(&action);
- handle_keys(&action);
- handle_page_click(&action);
- ngui_end(&ui);
- }
- if (action & ACT_NAV) {
- if (click_url[0]) {
- navigate(click_url, 1);
- click_url[0] = '\0';
- } else {
- navigate(url_in.text, 1);
- }
- }
- if (action & ACT_BACK) {
- if (hist_pos > 0) {
- hist_pos--;
- navigate(history[hist_pos], 0);
- }
- }
- if (action & ACT_FORWARD) {
- if (hist_pos + 1 < hist_len) {
- hist_pos++;
- navigate(history[hist_pos], 0);
- }
- }
- if (action & ACT_RELOAD) {
- if (s_eq(page_url, "about:home"))
- load_home(0);
- else
- navigate(page_url, 0);
- }
- if (action & ACT_HOME)
- load_home(1);
- if (action & ACT_CLOSE)
- quit = 1;
- if (quit)
- break;
- if (pending_action)
- continue;
- u_sleep_ms(10);
- }
- fetch_helper_stop();
- u_win_close();
- u_exit(0);
- }
|