browser.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229
  1. /* browser: a small HTML/CSS browser built on ngui, the image library and the
  2. * shared uhttp client.
  3. *
  4. * No JavaScript and no forms; external CSS is loaded over uhttp, and images
  5. * are decoded with img.c and drawn inline. Page-scoped buffers (page body,
  6. * rules and image bitmaps) come from the ring-3 libc heap; each image gets an
  7. * exact-size decode arena that is freed as soon as the scaled bitmap exists.
  8. * The DOM and the image/CSS I/O scratch use fixed static arenas.
  9. */
  10. #include <stdlib.h>
  11. #include <ucmd_heap.h>
  12. #include "ucmd_lib.h"
  13. #include "ngui.h"
  14. #include "browser_html.h"
  15. #include "uhttp.h"
  16. #include "img.h"
  17. #include "inflate.h"
  18. /* Start wide enough for the desktop nav; smaller screens fall back to
  19. * 960x640 and 780x540, which still fit a 1024x768 panel. */
  20. #define BR_W 1100
  21. #define BR_H 700
  22. #define BR_TOOLBAR_H 30
  23. #define BR_STATUS_H 18
  24. #define BR_PAGE_MAX (2u * 1024u * 1024u)
  25. #define BR_IMG_FILE_MAX (512u * 1024u)
  26. #define BR_IMG_PIX_MAX (8u * 1024u * 1024u)
  27. #define BR_MAX_IMAGES 16
  28. #define BR_IMG_ARENA_MAX (24u * 1024u * 1024u)
  29. #define BR_IMG_LAYOUT_W 1000
  30. #define BR_CSS_MAX (832u * 1024u)
  31. #define BR_CSS_COMP_MAX (192u * 1024u)
  32. #define BR_MAX_SHEETS 6
  33. #define BR_HISTORY 16
  34. #define BR_URL_MAX 512
  35. #define BR_STATUS_MAX 200
  36. #define BR_IMG_RGB565(r, g, b) \
  37. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  38. #define BR_GRAY 0x8410
  39. enum {
  40. ID_URL = 1,
  41. ID_GO,
  42. ID_BACK,
  43. ID_FORWARD,
  44. ID_RELOAD,
  45. ID_HOME
  46. };
  47. #define ACT_NAV (1u << 0)
  48. #define ACT_BACK (1u << 1)
  49. #define ACT_FORWARD (1u << 2)
  50. #define ACT_RELOAD (1u << 3)
  51. #define ACT_HOME (1u << 4)
  52. #define ACT_CLOSE (1u << 5)
  53. #define ACT_WAIT_NAV (ACT_NAV | ACT_BACK | ACT_FORWARD | ACT_RELOAD | \
  54. ACT_HOME | ACT_CLOSE)
  55. struct br_image {
  56. int state; /* 0 empty, 1 ready */
  57. struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */
  58. int native_w, native_h;
  59. char err[32];
  60. };
  61. static struct ngui ui;
  62. static struct ngui_input url_in;
  63. static struct ngui_scroll page_scroll;
  64. static struct br_doc doc;
  65. static struct br_image images[BR_MAX_IMAGES];
  66. static int image_count;
  67. /* Images are loaded one per main-loop iteration so the parsed page stays
  68. * interactive while a slow network/TLS fetch or 4K decode is in progress. */
  69. static int preload_active;
  70. static int preload_pending;
  71. static int preload_wait;
  72. static unsigned int preload_node;
  73. static int preload_loaded;
  74. static int preload_scroll;
  75. static char post_status_text[BR_STATUS_MAX];
  76. static int post_status_error;
  77. static unsigned int post_status_ttl;
  78. /* Content-relative position/height of each <img>, recorded by the measure
  79. * pass so image loading can be deferred until it is near the viewport. */
  80. static unsigned short img_top[BR_MAX_NODES];
  81. static unsigned short img_h[BR_MAX_NODES];
  82. /* One reusable I/O buffer: the two halves double as the gzip input scratch
  83. * for page/CSS fetches, where the heap has no room for another multi-megabyte
  84. * body. Decode arenas are allocated per image instead of living here. */
  85. static unsigned char *image_io_buf;
  86. #define image_file_buf (image_io_buf)
  87. #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX)
  88. #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX)
  89. static struct inflate image_inflate_ws;
  90. #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX
  91. #error "CSS scratch must fit in image_io_buf"
  92. #endif
  93. static char page_url[BR_URL_MAX];
  94. static char page_title[BR_TITLE_MAX];
  95. static char cwd[64];
  96. static int page_is_local;
  97. static int page_loading;
  98. static char status_text[BR_STATUS_MAX];
  99. static unsigned int status_until;
  100. static int status_error;
  101. static char history[BR_HISTORY][BR_URL_MAX];
  102. static int hist_len, hist_pos;
  103. static char click_url[BR_URL_MAX];
  104. static char pending_url[BR_URL_MAX];
  105. static unsigned int pending_action;
  106. static int cancel_fetch;
  107. static int quit;
  108. static struct ngui_rect page_rect;
  109. static unsigned short *page_backbuf;
  110. static int pagebuf_w, pagebuf_h;
  111. static int page_cache_valid;
  112. static int page_cache_scroll;
  113. static unsigned int page_cache_nnode;
  114. static int page_cache_x, page_cache_y, page_cache_w, page_cache_h;
  115. static int layout_valid, layout_left, layout_right;
  116. static unsigned int layout_nnode;
  117. static int layout_content_h;
  118. /* layout output */
  119. struct lay_run {
  120. unsigned short off, len;
  121. unsigned short gap;
  122. unsigned short color;
  123. unsigned short href;
  124. char lit[28];
  125. short img;
  126. signed short dy; /* inline image vertical offset */
  127. unsigned short iw, ih;
  128. unsigned short w;
  129. unsigned char font_px, bold, italic, mono, underline;
  130. unsigned char has_lit;
  131. };
  132. struct lay {
  133. int draw;
  134. struct ngui *ui;
  135. int left, right;
  136. int y;
  137. int line_x, line_y, line_h;
  138. int run_x;
  139. int align;
  140. int depth;
  141. int space_pending;
  142. struct lay_run runs[96];
  143. int nruns;
  144. };
  145. struct hit {
  146. int x, y, w, h;
  147. unsigned short node;
  148. };
  149. #define HIT_MAX 96
  150. static struct hit hits[HIT_MAX];
  151. static int nhits;
  152. static unsigned short block_h[BR_MAX_NODES];
  153. static char li_marker[BR_MAX_NODES][6];
  154. /* one connection object; the browser fetches one resource at a time */
  155. /* ---- tiny strings ---- */
  156. static unsigned int s_len(const char *s)
  157. {
  158. unsigned int n = 0;
  159. while (s && s[n])
  160. n++;
  161. return n;
  162. }
  163. static void s_copy(char *d, const char *s, unsigned int cap)
  164. {
  165. unsigned int i = 0;
  166. if (!cap)
  167. return;
  168. while (s && s[i] && i + 1 < cap) {
  169. d[i] = s[i];
  170. i++;
  171. }
  172. d[i] = '\0';
  173. }
  174. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  175. {
  176. while (s && *s && *n + 1 < cap)
  177. d[(*n)++] = *s++;
  178. d[*n] = '\0';
  179. }
  180. static void s_append_dec(char *d, unsigned int *n, unsigned int v,
  181. unsigned int cap)
  182. {
  183. char tmp[12];
  184. unsigned int i = 0, j;
  185. do {
  186. tmp[i++] = (char)('0' + v % 10);
  187. v /= 10;
  188. } while (v && i < sizeof(tmp));
  189. for (j = 0; j < i && *n + 1 < cap; j++)
  190. d[(*n)++] = tmp[i - 1 - j];
  191. d[*n] = '\0';
  192. }
  193. static int s_eq(const char *a, const char *b)
  194. {
  195. while (*a && *b) {
  196. if (*a != *b)
  197. return 0;
  198. a++;
  199. b++;
  200. }
  201. return *a == *b;
  202. }
  203. static int starts_with(const char *s, const char *p)
  204. {
  205. unsigned int i = 0;
  206. while (p[i]) {
  207. if (s[i] != p[i])
  208. return 0;
  209. i++;
  210. }
  211. return 1;
  212. }
  213. static int contains_ci(const char *s, const char *p)
  214. {
  215. unsigned int i, j;
  216. for (i = 0; s[i]; i++) {
  217. for (j = 0; p[j]; j++) {
  218. char a = s[i + j];
  219. if (a >= 'A' && a <= 'Z')
  220. a = (char)(a - 'A' + 'a');
  221. if (a != p[j])
  222. break;
  223. }
  224. if (!p[j])
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. static char *trim(char *s)
  230. {
  231. unsigned int n;
  232. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
  233. s++;
  234. n = s_len(s);
  235. while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
  236. s[n - 1] == '\r' || s[n - 1] == '\n'))
  237. s[--n] = '\0';
  238. return s;
  239. }
  240. /* ---- per-process heap ---- */
  241. static int heap_start(void)
  242. {
  243. image_io_buf = (unsigned char *)malloc(2u * BR_IMG_FILE_MAX);
  244. if (!image_io_buf)
  245. return -1;
  246. page_backbuf = (unsigned short *)malloc(
  247. (unsigned int)ui.w * (unsigned int)ui.h * 2u);
  248. pagebuf_w = page_backbuf ? ui.w : 0;
  249. pagebuf_h = page_backbuf ? ui.h : 0;
  250. page_cache_valid = 0;
  251. return 0;
  252. }
  253. static void heap_clear(void)
  254. {
  255. ngui_font_detach(&ui);
  256. heap_reset();
  257. image_io_buf = 0;
  258. page_backbuf = 0;
  259. pagebuf_w = pagebuf_h = 0;
  260. page_cache_valid = 0;
  261. }
  262. static void browser_rules_alloc(void)
  263. {
  264. struct br_rule *rules;
  265. rules = (struct br_rule *)malloc(
  266. (unsigned int)(BR_MAX_RULES * sizeof(struct br_rule)));
  267. br_set_rule_store(&doc, rules, rules ? BR_MAX_RULES : 0);
  268. }
  269. /* ---- status ---- */
  270. static void set_status(const char *s, int err, unsigned int ttl_ms)
  271. {
  272. s_copy(status_text, s, sizeof(status_text));
  273. status_error = err;
  274. status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0;
  275. ngui_invalidate(&ui);
  276. }
  277. /* ---- images ---- */
  278. static void image_reset(void)
  279. {
  280. int i;
  281. for (i = 0; i < BR_MAX_IMAGES; i++) {
  282. images[i].state = 0;
  283. images[i].native_w = images[i].native_h = 0;
  284. images[i].err[0] = '\0';
  285. }
  286. image_count = 0;
  287. preload_active = 0;
  288. preload_pending = 0;
  289. preload_wait = 0;
  290. preload_node = 0;
  291. preload_loaded = 0;
  292. }
  293. /* Decode into the shared arena, then copy/scale the result into a display
  294. * bitmap of at most BR_IMG_LAYOUT_W pixels wide. The arena is reused by the
  295. * next image, so the per-image cost is its scaled bitmap, not full 4K RGBA
  296. * pixels. Transparent edges are composited over white. */
  297. static int decode_image_data(const unsigned char *data, unsigned int len,
  298. struct br_image *bi)
  299. {
  300. struct img_info info;
  301. struct img_options opt;
  302. struct img_mem mem;
  303. struct img_image decoded;
  304. unsigned char *arena;
  305. unsigned int est, bytes, i;
  306. unsigned short *px;
  307. unsigned char *alpha = 0;
  308. int dw, dh, r;
  309. if (img_sniff(data, len, &info) != 0) {
  310. s_copy(bi->err, "unknown image format", sizeof(bi->err));
  311. return -1;
  312. }
  313. est = img_estimate(&info);
  314. if (est == 0xFFFFFFFFu || est > BR_IMG_ARENA_MAX) {
  315. s_copy(bi->err, "image too large", sizeof(bi->err));
  316. return -1;
  317. }
  318. arena = (unsigned char *)malloc(est);
  319. if (!arena) {
  320. s_copy(bi->err, "out of memory", sizeof(bi->err));
  321. return -1;
  322. }
  323. img_mem_init(&mem, arena, est);
  324. opt.max_pixels = BR_IMG_PIX_MAX;
  325. r = img_decode(data, len, &opt, &mem, &decoded);
  326. if (r != 0) {
  327. s_copy(bi->err, img_error_string(r), sizeof(bi->err));
  328. goto fail;
  329. }
  330. if (decoded.w == 0 || decoded.h == 0) {
  331. s_copy(bi->err, "empty image", sizeof(bi->err));
  332. goto fail;
  333. }
  334. bi->native_w = (int)decoded.w;
  335. bi->native_h = (int)decoded.h;
  336. dw = (int)decoded.w;
  337. dh = (int)decoded.h;
  338. if (dw > BR_IMG_LAYOUT_W) {
  339. dh = (int)((unsigned int)decoded.h * BR_IMG_LAYOUT_W /
  340. decoded.w);
  341. dw = BR_IMG_LAYOUT_W;
  342. }
  343. if (dh < 1)
  344. dh = 1;
  345. if (dw < 1)
  346. dw = 1;
  347. bytes = (unsigned int)dw * (unsigned int)dh * 2u;
  348. px = (unsigned short *)malloc(bytes);
  349. if (!px) {
  350. s_copy(bi->err, "out of memory", sizeof(bi->err));
  351. goto fail;
  352. }
  353. /* Keep the alpha plane for the scaled image so transparent logos and
  354. * icons composite over the page instead of the browser's dark theme
  355. * background. Very large alpha images stay flattened to bound the
  356. * heap use. */
  357. if (decoded.alpha && (unsigned int)dw * (unsigned int)dh <= (1u << 20)) {
  358. alpha = (unsigned char *)malloc((unsigned int)dw *
  359. (unsigned int)dh);
  360. if (alpha) {
  361. for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
  362. alpha[i] = 255;
  363. }
  364. }
  365. for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
  366. px[i] = ui.theme.bg;
  367. {
  368. struct img_blit b;
  369. b.dst = px;
  370. b.dst_w = dw;
  371. b.dst_h = dh;
  372. b.clip_x = 0;
  373. b.clip_y = 0;
  374. b.clip_w = dw;
  375. b.clip_h = dh;
  376. b.x = 0;
  377. b.y = 0;
  378. b.scale = ((unsigned int)dw << 16) / decoded.w;
  379. b.bg = ui.theme.bg;
  380. b.bg2 = ui.theme.bg;
  381. b.dst_a = alpha;
  382. b.flags = alpha ? IMG_BLIT_SAVE_ALPHA : IMG_BLIT_OVER_DST;
  383. img_draw(&decoded, &b);
  384. }
  385. bi->img.format = decoded.format;
  386. bi->img.w = (unsigned int)dw;
  387. bi->img.h = (unsigned int)dh;
  388. bi->img.px = px;
  389. bi->img.alpha = alpha;
  390. bi->img.flags = 0;
  391. bi->state = 1;
  392. free(arena);
  393. return 0;
  394. fail:
  395. free(arena);
  396. return -1;
  397. }
  398. /* ---- URL / local path handling ---- */
  399. static int is_http(const char *s)
  400. {
  401. return starts_with(s, "http://") || starts_with(s, "https://");
  402. }
  403. /* Collapse "/./", "/../" and duplicate slashes in an absolute path. */
  404. static int normalize_abs(const char *in, char *out, unsigned int cap)
  405. {
  406. char seg[64];
  407. unsigned int i = 0, o = 1;
  408. int depth = 0;
  409. int trailing;
  410. if (!in || in[0] != '/' || cap < 2)
  411. return -1;
  412. trailing = 0;
  413. {
  414. unsigned int n = s_len(in);
  415. if (n > 1 && in[n - 1] == '/')
  416. trailing = 1;
  417. }
  418. out[0] = '/';
  419. while (in[i]) {
  420. unsigned int n = 0;
  421. while (in[i] == '/')
  422. i++;
  423. while (in[i] && in[i] != '/' && in[i] != '?' && in[i] != '#') {
  424. if (n + 1 < sizeof(seg))
  425. seg[n++] = in[i];
  426. i++;
  427. }
  428. seg[n] = '\0';
  429. if (n == 0)
  430. break;
  431. if (s_eq(seg, "."))
  432. continue;
  433. if (s_eq(seg, "..")) {
  434. if (depth > 0) {
  435. while (o > 1 && out[o - 1] != '/')
  436. o--;
  437. if (o > 1)
  438. o--;
  439. depth--;
  440. }
  441. continue;
  442. }
  443. if (o > 1) {
  444. if (o + 1 >= cap)
  445. return -1;
  446. out[o++] = '/';
  447. }
  448. if (o + n + 1 >= cap)
  449. return -1;
  450. {
  451. unsigned int j;
  452. for (j = 0; j < n; j++)
  453. out[o++] = seg[j];
  454. }
  455. depth++;
  456. }
  457. if (trailing && o > 1) {
  458. if (o + 1 >= cap)
  459. return -1;
  460. out[o++] = '/';
  461. }
  462. out[o] = '\0';
  463. return 0;
  464. }
  465. static void make_abs_local(const char *in, char *out, unsigned int cap)
  466. {
  467. char joined[BR_URL_MAX];
  468. if (!in || !in[0]) {
  469. s_copy(out, cwd[0] ? cwd : "/", cap);
  470. return;
  471. }
  472. if (in[0] == '/') {
  473. if (normalize_abs(in, out, cap) != 0)
  474. s_copy(out, in, cap);
  475. return;
  476. }
  477. {
  478. unsigned int n = 0;
  479. s_append(joined, &n, cwd[0] ? cwd : "/", sizeof(joined));
  480. s_append(joined, &n, "/", sizeof(joined));
  481. s_append(joined, &n, in, sizeof(joined));
  482. }
  483. if (normalize_abs(joined, out, cap) != 0)
  484. s_copy(out, joined, cap);
  485. }
  486. static int path_dir(const char *path, char *out, unsigned int cap)
  487. {
  488. unsigned int i, last = 0;
  489. for (i = 0; path[i]; i++)
  490. if (path[i] == '/')
  491. last = i;
  492. if (last == 0) {
  493. if (cap < 2)
  494. return -1;
  495. out[0] = '/';
  496. out[1] = '\0';
  497. return 0;
  498. }
  499. if (last + 1 >= cap)
  500. return -1;
  501. {
  502. unsigned int j;
  503. for (j = 0; j < last; j++)
  504. out[j] = path[j];
  505. out[last] = '\0';
  506. }
  507. return 0;
  508. }
  509. /* Resolve an href against the current page. 0 = ok, -1 = ignore. */
  510. static int resolve_link(const char *base, const char *href, char *out,
  511. unsigned int cap)
  512. {
  513. char joined[BR_URL_MAX];
  514. if (!href || !href[0] || href[0] == '#')
  515. return -1;
  516. if (starts_with(href, "mailto:") || starts_with(href, "javascript:") ||
  517. starts_with(href, "data:") || starts_with(href, "tel:"))
  518. return -1;
  519. if (starts_with(href, "about:")) {
  520. s_copy(out, href, cap);
  521. return 0;
  522. }
  523. if (is_http(href)) {
  524. s_copy(out, href, cap);
  525. return 0;
  526. }
  527. if (starts_with(href, "//")) {
  528. if (is_http(base)) {
  529. unsigned int n = 0;
  530. s_append(out, &n, starts_with(base, "https:") ?
  531. "https:" : "http:", cap);
  532. s_append(out, &n, href, cap);
  533. return 0;
  534. }
  535. return -1;
  536. }
  537. if (is_http(base)) {
  538. return uhttp_resolve(base, href, out, cap) == 0 ? 0 : -1;
  539. }
  540. /* local page */
  541. if (href[0] == '/') {
  542. if (normalize_abs(href, out, cap) != 0)
  543. return -1;
  544. return 0;
  545. }
  546. {
  547. char dir[BR_URL_MAX];
  548. unsigned int n = 0;
  549. if (s_eq(base, "about:home"))
  550. s_copy(dir, cwd[0] ? cwd : "/", sizeof(dir));
  551. else if (path_dir(base, dir, sizeof(dir)) != 0)
  552. s_copy(dir, "/", sizeof(dir));
  553. s_append(joined, &n, dir, sizeof(joined));
  554. if (n == 0 || joined[n - 1] != '/')
  555. s_append(joined, &n, "/", sizeof(joined));
  556. s_append(joined, &n, href, sizeof(joined));
  557. }
  558. return normalize_abs(joined, out, cap) == 0 ? 0 : -1;
  559. }
  560. /* Best-effort decompression: keep the first `cap` bytes and discard the
  561. * rest instead of failing when a page or stylesheet is larger than the
  562. * buffer. The stream is still walked to the end, so a normal page is
  563. * verified; a truncated compressed input still returns whatever was decoded
  564. * so far. */
  565. struct trunc_buf {
  566. unsigned char *dst;
  567. unsigned int cap, pos;
  568. int full;
  569. };
  570. static int trunc_write(void *ctx, const unsigned char *data, unsigned int len)
  571. {
  572. struct trunc_buf *m = (struct trunc_buf *)ctx;
  573. if (len > m->cap - m->pos) {
  574. unsigned int room = m->cap - m->pos;
  575. if (room) {
  576. unsigned int i;
  577. for (i = 0; i < room; i++)
  578. m->dst[m->pos + i] = data[i];
  579. m->pos += room;
  580. }
  581. m->full = 1;
  582. return 0;
  583. }
  584. {
  585. unsigned int i;
  586. for (i = 0; i < len; i++)
  587. m->dst[m->pos + i] = data[i];
  588. }
  589. m->pos += len;
  590. return 0;
  591. }
  592. static void worker_wait_ui(void);
  593. static void inflate_ui_pump(void *ctx)
  594. {
  595. static unsigned int last;
  596. unsigned int now = u_ticks_ms();
  597. (void)ctx;
  598. if ((int)(now - last) >= 50) {
  599. last = now;
  600. worker_wait_ui();
  601. }
  602. }
  603. static int inflate_trunc(const unsigned char *src, unsigned int len,
  604. unsigned char *dst, unsigned int cap,
  605. unsigned int *outlen, int gzip, int *truncated)
  606. {
  607. struct inflate_src in;
  608. struct inflate_sink sink;
  609. struct trunc_buf m;
  610. unsigned int got = 0;
  611. int r;
  612. in.p = src;
  613. in.len = len;
  614. in.more = 0;
  615. in.ctx = 0;
  616. m.dst = dst;
  617. m.cap = cap;
  618. m.pos = 0;
  619. m.full = 0;
  620. sink.write = trunc_write;
  621. sink.ctx = &m;
  622. inflate_init(&image_inflate_ws, &in, &sink);
  623. image_inflate_ws.progress = inflate_ui_pump;
  624. image_inflate_ws.progress_ctx = 0;
  625. r = gzip ? inflate_gzip(&image_inflate_ws, &got) :
  626. inflate_zlib(&image_inflate_ws, &got);
  627. *outlen = m.pos;
  628. *truncated = m.full || r != 0;
  629. if (r != 0 && m.pos == 0)
  630. return -1;
  631. return 0;
  632. }
  633. /* ---- headless fetch helper ------------------------------------------- *
  634. *
  635. * Network I/O runs in a separate ring-3 process (`curl __fetch`): it writes
  636. * the raw response and a small metadata file under /share. While it works,
  637. * the browser keeps drawing and scrolling its own window, so a slow DNS/TLS
  638. * handshake or a multi-megabyte body never freezes the UI.
  639. */
  640. #define FETCH_REQ "/share/bfreq"
  641. #define FETCH_BODY "/share/bfbody"
  642. #define FETCH_META "/share/bfmeta"
  643. #define FETCH_DONE "/share/bfdone"
  644. #define FETCH_ABORT (-2)
  645. struct fetch_meta {
  646. int status;
  647. int error;
  648. int trunc;
  649. unsigned int len;
  650. char encoding[16];
  651. char ctype[64];
  652. char final[BR_URL_MAX];
  653. };
  654. static void draw(unsigned int *action);
  655. static void handle_keys(unsigned int *action);
  656. static void handle_page_click(unsigned int *action);
  657. static int fetch_helper_tty = -2;
  658. static int fetch_helper_pid = -1;
  659. static unsigned int fetch_seq;
  660. static struct fetch_meta fetch_meta;
  661. static unsigned int meta_uint(char **pp)
  662. {
  663. char *p = *pp;
  664. unsigned int v = 0;
  665. while (*p >= '0' && *p <= '9') {
  666. v = v * 10 + (unsigned int)(*p - '0');
  667. p++;
  668. }
  669. while (*p && *p != '\n')
  670. p++;
  671. if (*p == '\n')
  672. p++;
  673. *pp = p;
  674. return v;
  675. }
  676. static void meta_str(char **pp, char *out, unsigned int cap)
  677. {
  678. char *p = *pp;
  679. unsigned int n = 0;
  680. while (*p && *p != '\n' && n + 1 < cap)
  681. out[n++] = *p++;
  682. out[n] = '\0';
  683. while (*p && *p != '\n')
  684. p++;
  685. if (*p == '\n')
  686. p++;
  687. *pp = p;
  688. }
  689. static void worker_wait_ui(void)
  690. {
  691. unsigned int action = 0;
  692. if (!ngui_begin(&ui))
  693. return;
  694. draw(&action);
  695. handle_keys(&action);
  696. handle_page_click(&action);
  697. if (action & ACT_WAIT_NAV) {
  698. pending_action |= action & ACT_WAIT_NAV;
  699. if ((action & ACT_NAV) && click_url[0])
  700. s_copy(pending_url, click_url, sizeof(pending_url));
  701. click_url[0] = '\0';
  702. cancel_fetch = 1;
  703. }
  704. if (!ngui_focus(&ui) && ngui_scroll_keys(&ui, &page_scroll))
  705. ngui_invalidate(&ui);
  706. ngui_end(&ui);
  707. }
  708. static void fetch_helper_stop(void)
  709. {
  710. int i;
  711. if (fetch_helper_pid > 0) {
  712. u_kill((unsigned int)fetch_helper_pid);
  713. for (i = 0; i < 40 && u_alive((unsigned int)fetch_helper_pid); i++)
  714. u_sleep_ms(5);
  715. /* Let the reaper release the NIC before the next request. */
  716. u_sleep_ms(10);
  717. }
  718. fetch_helper_pid = -1;
  719. }
  720. static int fetch_helper_start(char *err, unsigned int errcap)
  721. {
  722. if (fetch_helper_pid > 0 &&
  723. u_alive((unsigned int)fetch_helper_pid))
  724. return 0;
  725. fetch_helper_pid = -1;
  726. if (fetch_helper_tty == -2) {
  727. int t = u_tty_create();
  728. fetch_helper_tty = t > 0 ? t : 0;
  729. }
  730. fetch_helper_pid = u_spawn("/ramfs/bin/curl", "__serve",
  731. fetch_helper_tty, "/");
  732. if (fetch_helper_pid <= 0) {
  733. fetch_helper_pid = -1;
  734. s_copy(err, "cannot start network helper", errcap);
  735. return -1;
  736. }
  737. return 0;
  738. }
  739. static int worker_fetch(const char *url, unsigned int cap,
  740. struct fetch_meta *m, char *err, unsigned int errcap)
  741. {
  742. static char req[768], meta[1024], done[24];
  743. char *p;
  744. unsigned int n = 0, deadline;
  745. int rn, mseq, retried = 0;
  746. struct sys_stat st;
  747. if (cancel_fetch || pending_action)
  748. return FETCH_ABORT;
  749. if (u_stat("/share", &st) != 0 || !st.is_dir ||
  750. u_stat("/ramfs/bin/curl", &st) != 0) {
  751. s_copy(err, "network helper unavailable", errcap);
  752. return -1;
  753. }
  754. fetch_seq++;
  755. if (!fetch_seq)
  756. fetch_seq = 1;
  757. s_append_dec(req, &n, fetch_seq, sizeof(req));
  758. s_append(req, &n, " ", sizeof(req));
  759. s_append_dec(req, &n, cap, sizeof(req));
  760. s_append(req, &n, " ", sizeof(req));
  761. s_append(req, &n, url, sizeof(req));
  762. s_append(req, &n, "\n", sizeof(req));
  763. u_syscall1(SYS_UNLINK, (int)FETCH_DONE);
  764. u_syscall1(SYS_UNLINK, (int)FETCH_META);
  765. if (u_syscall3(SYS_WRITE_FILE, (int)FETCH_REQ, (int)req, (int)n) < 0) {
  766. s_copy(err, "cannot queue network request", errcap);
  767. return -1;
  768. }
  769. if (fetch_helper_start(err, errcap) != 0)
  770. return -1;
  771. deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
  772. for (;;) {
  773. worker_wait_ui();
  774. if (cancel_fetch || pending_action) {
  775. fetch_helper_stop();
  776. return FETCH_ABORT;
  777. }
  778. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_DONE, (int)done,
  779. (int)(sizeof(done) - 1));
  780. if (rn > 0) {
  781. done[rn] = '\0';
  782. p = done;
  783. mseq = (int)meta_uint(&p);
  784. if (mseq == (int)fetch_seq)
  785. break;
  786. }
  787. if (!u_alive((unsigned int)fetch_helper_pid)) {
  788. fetch_helper_pid = -1;
  789. if (!retried && fetch_helper_start(err, errcap) == 0) {
  790. retried = 1;
  791. continue;
  792. }
  793. s_copy(err, "network helper stopped", errcap);
  794. return -1;
  795. }
  796. if ((int)(u_ticks_ms() - deadline) >= 0) {
  797. fetch_helper_stop();
  798. s_copy(err, "network request timed out", errcap);
  799. return -1;
  800. }
  801. u_sleep_ms(20);
  802. }
  803. if (cancel_fetch || pending_action)
  804. return FETCH_ABORT;
  805. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_META, (int)meta,
  806. (int)(sizeof(meta) - 1));
  807. if (rn <= 0) {
  808. s_copy(err, "network helper failed", errcap);
  809. return -1;
  810. }
  811. meta[rn] = '\0';
  812. p = meta;
  813. mseq = (int)meta_uint(&p);
  814. if (mseq != (int)fetch_seq) {
  815. s_copy(err, "stale network response", errcap);
  816. return -1;
  817. }
  818. m->status = (int)meta_uint(&p);
  819. m->error = (int)meta_uint(&p);
  820. m->trunc = (int)meta_uint(&p);
  821. meta_str(&p, m->encoding, sizeof(m->encoding));
  822. meta_str(&p, m->ctype, sizeof(m->ctype));
  823. meta_str(&p, m->final, sizeof(m->final));
  824. m->len = meta_uint(&p);
  825. if (m->error) {
  826. s_copy(err, uhttp_error_string(m->error), errcap);
  827. return -1;
  828. }
  829. return 0;
  830. }
  831. static int fetch_remote(const char *url, unsigned int cap,
  832. unsigned char *plain, unsigned int plaincap,
  833. unsigned char *comp, unsigned int compcap,
  834. struct fetch_meta *m, int *truncated,
  835. char *err, unsigned int errcap)
  836. {
  837. int rv, zr, was_trunc;
  838. rv = worker_fetch(url, cap, m, err, errcap);
  839. if (rv == FETCH_ABORT)
  840. return FETCH_ABORT;
  841. if (rv != 0)
  842. return -1;
  843. if (contains_ci(m->encoding, "gzip") ||
  844. contains_ci(m->encoding, "deflate")) {
  845. unsigned int comp_len = 0, plen = 0;
  846. rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)comp,
  847. (int)compcap);
  848. if (rv < 0) {
  849. s_copy(err, "empty response", errcap);
  850. return -1;
  851. }
  852. comp_len = (unsigned int)rv;
  853. if (comp_len < m->len) {
  854. s_copy(err, "compressed response is too large", errcap);
  855. return -1;
  856. }
  857. if (contains_ci(m->encoding, "deflate"))
  858. zr = inflate_trunc(comp, comp_len, plain, plaincap,
  859. &plen, 0, &was_trunc);
  860. else
  861. zr = inflate_trunc(comp, comp_len, plain, plaincap,
  862. &plen, 1, &was_trunc);
  863. if (zr != 0) {
  864. s_copy(err, "compressed response is corrupt", errcap);
  865. return -1;
  866. }
  867. *truncated = m->trunc || was_trunc;
  868. return (cancel_fetch || pending_action) ? FETCH_ABORT :
  869. (int)plen;
  870. }
  871. rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)plain,
  872. (int)plaincap);
  873. if (rv < 0) {
  874. s_copy(err, "empty response", errcap);
  875. return -1;
  876. }
  877. *truncated = m->trunc || ((unsigned int)rv < m->len);
  878. return (cancel_fetch || pending_action) ? FETCH_ABORT : rv;
  879. }
  880. /* ---- HTTP / file fetch ---- */
  881. static int read_url(const char *url, unsigned int cap, unsigned char **out,
  882. unsigned int *outlen, int *is_local, char *ctype,
  883. unsigned int ctcap, char *final, unsigned int finalcap,
  884. char *err, unsigned int errcap, int *truncated)
  885. {
  886. unsigned char *buf;
  887. unsigned int len;
  888. int tr = 0;
  889. int rv;
  890. *out = 0;
  891. *outlen = 0;
  892. *truncated = 0;
  893. if (final && finalcap)
  894. final[0] = '\0';
  895. if (ctype && ctcap)
  896. ctype[0] = '\0';
  897. if (url[0] == '/') {
  898. buf = (unsigned char *)malloc(cap);
  899. if (!buf) {
  900. s_copy(err, "out of memory", errcap);
  901. return -1;
  902. }
  903. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)buf, (int)cap);
  904. if (rv < 0) {
  905. free(buf);
  906. s_copy(err, "file not found", errcap);
  907. return -1;
  908. }
  909. *out = buf;
  910. *outlen = (unsigned int)rv;
  911. *is_local = 1;
  912. if (ctype)
  913. s_copy(ctype, "text/html", ctcap);
  914. if (final)
  915. s_copy(final, url, finalcap);
  916. if ((unsigned int)rv == cap)
  917. *truncated = 1;
  918. return 0;
  919. }
  920. buf = (unsigned char *)malloc(cap);
  921. if (!buf) {
  922. s_copy(err, "out of memory", errcap);
  923. return -1;
  924. }
  925. rv = fetch_remote(url, cap, buf, cap, image_io_buf, BR_PAGE_COMP_MAX,
  926. &fetch_meta, &tr, err, errcap);
  927. if (rv == FETCH_ABORT) {
  928. free(buf);
  929. return FETCH_ABORT;
  930. }
  931. if (rv < 0) {
  932. free(buf);
  933. return -1;
  934. }
  935. len = (unsigned int)rv;
  936. if (len == 0) {
  937. free(buf);
  938. s_copy(err, "empty response", errcap);
  939. return -1;
  940. }
  941. if (ctype)
  942. s_copy(ctype, fetch_meta.ctype, ctcap);
  943. if (final && fetch_meta.final[0])
  944. s_copy(final, fetch_meta.final, finalcap);
  945. *is_local = 0;
  946. *out = buf;
  947. *outlen = len;
  948. *truncated = tr;
  949. return 0;
  950. }
  951. /* Fetch an image into the fixed scratch buffers. The scaled bitmaps live on
  952. * the heap, so images must not allocate their file body from it as well. */
  953. static int fetch_image(const char *url, unsigned int cap,
  954. unsigned char **out, unsigned int *outlen,
  955. char *err, unsigned int errcap)
  956. {
  957. unsigned int len;
  958. int tr = 0;
  959. int rv;
  960. *out = 0;
  961. *outlen = 0;
  962. if (cap > BR_IMG_FILE_MAX)
  963. cap = BR_IMG_FILE_MAX;
  964. if (url[0] == '/') {
  965. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_file_buf,
  966. (int)cap);
  967. if (rv < 0) {
  968. s_copy(err, "file not found", errcap);
  969. return -1;
  970. }
  971. *out = image_file_buf;
  972. *outlen = (unsigned int)rv;
  973. return 0;
  974. }
  975. rv = fetch_remote(url, cap, image_file_buf, cap, image_comp_buf,
  976. BR_IMG_FILE_MAX, &fetch_meta, &tr, err, errcap);
  977. if (rv == FETCH_ABORT)
  978. return FETCH_ABORT;
  979. if (rv < 0)
  980. return -1;
  981. len = (unsigned int)rv;
  982. if (len == 0) {
  983. s_copy(err, "empty image", errcap);
  984. return -1;
  985. }
  986. *out = image_file_buf;
  987. *outlen = len;
  988. return 0;
  989. }
  990. /* Read one stylesheet into the shared static buffer. CSS is parsed
  991. * immediately, so it does not need the page heap; this leaves the heap for
  992. * the 2 MB page body, the rules and the scaled image bitmaps. */
  993. static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
  994. char *err, unsigned int errcap)
  995. {
  996. unsigned int len;
  997. int tr = 0;
  998. int rv;
  999. *out = 0;
  1000. *outlen = 0;
  1001. if (url[0] == '/') {
  1002. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_io_buf,
  1003. (int)BR_CSS_MAX);
  1004. if (rv < 0) {
  1005. s_copy(err, "stylesheet not found", errcap);
  1006. return -1;
  1007. }
  1008. *out = image_io_buf;
  1009. *outlen = (unsigned int)rv;
  1010. return 0;
  1011. }
  1012. rv = fetch_remote(url, BR_CSS_MAX, image_io_buf, BR_CSS_MAX,
  1013. image_io_buf + BR_CSS_MAX, BR_CSS_COMP_MAX,
  1014. &fetch_meta, &tr, err, errcap);
  1015. if (rv == FETCH_ABORT)
  1016. return FETCH_ABORT;
  1017. if (rv < 0)
  1018. return -1;
  1019. len = (unsigned int)rv;
  1020. if (len == 0) {
  1021. s_copy(err, "empty stylesheet", errcap);
  1022. return -1;
  1023. }
  1024. *out = image_io_buf;
  1025. *outlen = len;
  1026. return 0;
  1027. }
  1028. static void redraw_now(void);
  1029. static char error_page[1024];
  1030. static void show_error(const char *title, const char *detail)
  1031. {
  1032. char *page = error_page;
  1033. unsigned int n = 0;
  1034. s_append(page, &n, "<html><body><h2>", sizeof(error_page));
  1035. s_append(page, &n, title, sizeof(error_page));
  1036. s_append(page, &n, "</h2><p>", sizeof(error_page));
  1037. {
  1038. const char *p = detail;
  1039. while (*p && n + 1 < sizeof(error_page)) {
  1040. if (*p == '<') s_append(page, &n, "&lt;", sizeof(error_page));
  1041. else if (*p == '>') s_append(page, &n, "&gt;", sizeof(error_page));
  1042. else if (*p == '&') s_append(page, &n, "&amp;", sizeof(error_page));
  1043. else page[n++] = *p;
  1044. p++;
  1045. }
  1046. }
  1047. page[n] = '\0';
  1048. s_append(page, &n, "</p><p>", sizeof(error_page));
  1049. s_append(page, &n, page_url, sizeof(error_page));
  1050. s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
  1051. sizeof(error_page));
  1052. br_parse(&doc, page, n);
  1053. layout_valid = 0;
  1054. preload_active = 0;
  1055. preload_pending = 0;
  1056. preload_wait = 0;
  1057. s_copy(page_title, title, sizeof(page_title));
  1058. page_loading = 0;
  1059. set_status(title, 1, 0);
  1060. ngui_invalidate(&ui);
  1061. }
  1062. static int decode_direct_image(const unsigned char *data, unsigned int len,
  1063. unsigned int node)
  1064. {
  1065. if (image_count >= BR_MAX_IMAGES)
  1066. return -1;
  1067. if (decode_image_data(data, len, &images[0]) != 0)
  1068. return -1;
  1069. doc.nodes[node].img_index = 0;
  1070. image_count = 1;
  1071. return 0;
  1072. }
  1073. static int ancestor_is(unsigned int node, unsigned int tag)
  1074. {
  1075. unsigned int p = doc.nodes[node].parent;
  1076. while (p) {
  1077. if (doc.nodes[p].tag == tag)
  1078. return 1;
  1079. p = doc.nodes[p].parent;
  1080. }
  1081. return 0;
  1082. }
  1083. static int load_stylesheets(void)
  1084. {
  1085. static char seen[BR_MAX_SHEETS][BR_URL_MAX];
  1086. unsigned int i, sheets = 0, nseen = 0;
  1087. for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) {
  1088. struct br_node *nd = &doc.nodes[i];
  1089. const char *rel, *href;
  1090. char url[BR_URL_MAX];
  1091. unsigned char *data;
  1092. unsigned int len;
  1093. char err[64];
  1094. int rv;
  1095. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK)
  1096. continue;
  1097. rel = br_rel(&doc, i);
  1098. if (!contains_ci(rel, "stylesheet"))
  1099. continue;
  1100. href = br_href(&doc, i);
  1101. if (!href[0])
  1102. continue;
  1103. if (resolve_link(page_url, href, url, sizeof(url)) != 0)
  1104. continue;
  1105. {
  1106. unsigned int k;
  1107. int dup = 0;
  1108. for (k = 0; k < nseen; k++)
  1109. if (s_eq(seen[k], url)) {
  1110. dup = 1;
  1111. break;
  1112. }
  1113. if (dup)
  1114. continue;
  1115. if (nseen < BR_MAX_SHEETS) {
  1116. s_copy(seen[nseen], url, BR_URL_MAX);
  1117. nseen++;
  1118. }
  1119. }
  1120. rv = fetch_css(url, &data, &len, err, sizeof(err));
  1121. if (rv == FETCH_ABORT)
  1122. return FETCH_ABORT;
  1123. if (rv != 0)
  1124. continue;
  1125. if (len)
  1126. br_parse_css_text(&doc, (const char *)data, len);
  1127. sheets++;
  1128. }
  1129. br_parse_inline_css(&doc);
  1130. br_reset_styles(&doc);
  1131. layout_valid = 0;
  1132. return 0;
  1133. }
  1134. static void preload_begin(void)
  1135. {
  1136. preload_active = 1;
  1137. preload_pending = 0;
  1138. preload_wait = 1;
  1139. preload_node = 0;
  1140. preload_loaded = 0;
  1141. preload_scroll = 0;
  1142. }
  1143. /* Styles are computed top-down during layout, but images inside a hidden
  1144. * menu/carousel are never visited and keep img_top == 0. Without this check
  1145. * the preloader walks the DOM in node order and spends the whole image
  1146. * budget on logos and off-canvas thumbnails the reader cannot see. */
  1147. static int node_hidden(unsigned int node)
  1148. {
  1149. unsigned int path[32], n = 0, p = node, i;
  1150. struct br_style st, parent;
  1151. while (p && n < sizeof(path) / sizeof(path[0])) {
  1152. path[n++] = p;
  1153. p = doc.nodes[p].parent;
  1154. }
  1155. parent = (struct br_style){0};
  1156. parent.font_px = 16;
  1157. parent.display = BR_DISPLAY_BLOCK;
  1158. for (i = n; i > 0; i--) {
  1159. br_style_for(&doc, path[i - 1], &parent, &st);
  1160. if (st.display == BR_DISPLAY_NONE || st.vis_hidden ||
  1161. st.opacity_hidden)
  1162. return 1;
  1163. parent = st;
  1164. }
  1165. return 0;
  1166. }
  1167. /* Load at most one image per call. Images below the current viewport are
  1168. * left pending until the page is scrolled; this keeps a long article with
  1169. * many 4K screenshots from stalling on content the user cannot see yet. */
  1170. static void preload_step(void)
  1171. {
  1172. unsigned int batch = 0;
  1173. while (preload_active && preload_node < doc.nnode) {
  1174. unsigned int i = preload_node;
  1175. struct br_node *nd = &doc.nodes[i];
  1176. const char *src;
  1177. char url[BR_URL_MAX];
  1178. unsigned char *data;
  1179. unsigned int len;
  1180. char err[64];
  1181. char msg[64];
  1182. unsigned int n = 0;
  1183. int rv;
  1184. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
  1185. nd->img_index != -1) {
  1186. preload_node++;
  1187. continue;
  1188. }
  1189. if (ancestor_is(i, BR_TAG_NOSCRIPT) || node_hidden(i)) {
  1190. preload_node++;
  1191. continue;
  1192. }
  1193. src = br_src(&doc, i);
  1194. if (!src[0] || contains_ci(src, ".svg") ||
  1195. starts_with(src, "data:")) {
  1196. preload_node++;
  1197. continue;
  1198. }
  1199. if (resolve_link(page_url, src, url, sizeof(url)) != 0) {
  1200. preload_node++;
  1201. continue;
  1202. }
  1203. if (image_count >= BR_MAX_IMAGES) {
  1204. preload_node = doc.nnode;
  1205. break;
  1206. }
  1207. /* Do not stop at the first below-fold image: DOM order is
  1208. * not visual order, and a footer/ad image early in the DOM
  1209. * used to hide every later article image from the loader. */
  1210. if (img_top[i] >
  1211. (unsigned int)(page_scroll.offset + page_rect.h + 160)) {
  1212. preload_pending = 1;
  1213. preload_node++;
  1214. continue;
  1215. }
  1216. preload_node = i + 1;
  1217. s_append(msg, &n, "Loading images ", sizeof(msg));
  1218. s_append_dec(msg, &n, (unsigned int)preload_loaded + 1,
  1219. sizeof(msg));
  1220. s_append(msg, &n, "...", sizeof(msg));
  1221. set_status(msg, 0, 0);
  1222. ngui_invalidate(&ui);
  1223. data = 0;
  1224. len = 0;
  1225. rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
  1226. sizeof(err));
  1227. if (rv == FETCH_ABORT) {
  1228. preload_active = 0;
  1229. preload_pending = 0;
  1230. return;
  1231. }
  1232. if (rv == 0 &&
  1233. decode_image_data(data, len, &images[image_count]) == 0) {
  1234. nd->img_index = (short)image_count;
  1235. image_count++;
  1236. preload_loaded++;
  1237. layout_valid = 0;
  1238. } else {
  1239. /* Remember failures so a scroll does not retry an
  1240. * unreachable tracking/ad image with the full network
  1241. * timeout on every pass. */
  1242. nd->img_index = -2;
  1243. }
  1244. ngui_invalidate(&ui);
  1245. /* A full page layout after every image dominates loading on a
  1246. * long page. Decode a small batch, then let the main loop draw
  1247. * once for the whole batch. */
  1248. if (++batch >= 4)
  1249. return;
  1250. }
  1251. preload_active = 0;
  1252. if (preload_pending)
  1253. preload_scroll = page_scroll.offset;
  1254. else
  1255. preload_pending = 0;
  1256. set_status(post_status_text, post_status_error, post_status_ttl);
  1257. ngui_invalidate(&ui);
  1258. }
  1259. static void history_push(const char *url)
  1260. {
  1261. if (!url || !url[0])
  1262. return;
  1263. if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len &&
  1264. s_eq(history[hist_pos], url))
  1265. return;
  1266. if (hist_len > 0 && hist_pos + 1 < hist_len)
  1267. hist_len = hist_pos + 1;
  1268. if (hist_len < BR_HISTORY) {
  1269. s_copy(history[hist_len], url, BR_URL_MAX);
  1270. hist_pos = hist_len;
  1271. hist_len++;
  1272. } else {
  1273. int i;
  1274. for (i = 1; i < BR_HISTORY; i++)
  1275. s_copy(history[i - 1], history[i], BR_URL_MAX);
  1276. s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX);
  1277. hist_pos = BR_HISTORY - 1;
  1278. }
  1279. }
  1280. static void load_page(const char *url, int local, int push)
  1281. {
  1282. unsigned char *body = 0;
  1283. unsigned int len = 0;
  1284. int truncated = 0;
  1285. struct img_info image_info;
  1286. char ctype[64];
  1287. char final[BR_URL_MAX];
  1288. char err[80];
  1289. const char *detail = 0;
  1290. int rv;
  1291. final[0] = '\0';
  1292. cancel_fetch = 0;
  1293. preload_active = 0;
  1294. preload_pending = 0;
  1295. heap_clear();
  1296. image_reset();
  1297. layout_valid = 0;
  1298. page_scroll.offset = 0;
  1299. page_scroll.dragging = 0;
  1300. br_parse_plain(&doc, "", 0); /* blank while loading */
  1301. page_title[0] = '\0';
  1302. page_loading = 1;
  1303. page_is_local = local;
  1304. s_copy(page_url, url, sizeof(page_url));
  1305. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1306. url_in.len = s_len(url_in.text);
  1307. url_in.cursor = url_in.len;
  1308. url_in.scroll = 0;
  1309. ngui_set_focus(&ui, 0);
  1310. if (final[0])
  1311. final[0] = '\0';
  1312. if (push)
  1313. history_push(url);
  1314. set_status("Loading...", 0, 0);
  1315. ngui_invalidate(&ui);
  1316. redraw_now();
  1317. if (heap_start() != 0) {
  1318. show_error("Out of memory", "Cannot allocate the page buffer");
  1319. return;
  1320. }
  1321. ngui_font_attach(&ui);
  1322. browser_rules_alloc();
  1323. rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
  1324. sizeof(ctype), final, sizeof(final), err, sizeof(err),
  1325. &truncated);
  1326. if (rv == FETCH_ABORT)
  1327. return;
  1328. if (rv != 0) {
  1329. detail = err;
  1330. show_error("Cannot load this page", detail);
  1331. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1332. url_in.len = s_len(url_in.text);
  1333. url_in.cursor = url_in.len;
  1334. url_in.scroll = 0;
  1335. return;
  1336. }
  1337. page_is_local = local;
  1338. if (final[0]) {
  1339. s_copy(page_url, final, sizeof(page_url));
  1340. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1341. url_in.len = s_len(url_in.text);
  1342. url_in.cursor = url_in.len;
  1343. url_in.scroll = 0;
  1344. }
  1345. if (contains_ci(ctype, "text/plain")) {
  1346. br_parse_plain(&doc, (const char *)body, len);
  1347. } else if (contains_ci(ctype, "image/") ||
  1348. (!contains_ci(ctype, "text/html") &&
  1349. img_sniff(body, len, &image_info) == 0)) {
  1350. unsigned int node = br_parse_image_doc(&doc, page_url);
  1351. if (node)
  1352. decode_direct_image(body, len, node);
  1353. s_copy(page_title, "Image", sizeof(page_title));
  1354. } else {
  1355. br_set_viewport(ui.w);
  1356. br_parse(&doc, (const char *)body, len);
  1357. s_copy(page_title, doc.title[0] ? doc.title : page_url,
  1358. sizeof(page_title));
  1359. if (load_stylesheets() == FETCH_ABORT)
  1360. return;
  1361. if (pending_action)
  1362. return;
  1363. }
  1364. post_status_text[0] = '\0';
  1365. post_status_error = 0;
  1366. post_status_ttl = 0;
  1367. if (truncated) {
  1368. s_copy(post_status_text, "Page truncated at the buffer limit",
  1369. sizeof(post_status_text));
  1370. post_status_ttl = 6000;
  1371. } else if (doc.truncated) {
  1372. s_copy(post_status_text, "Page truncated by the parser",
  1373. sizeof(post_status_text));
  1374. post_status_ttl = 6000;
  1375. } else if (starts_with(page_url, "https://")) {
  1376. s_copy(post_status_text, "HTTPS: certificate NOT verified",
  1377. sizeof(post_status_text));
  1378. post_status_ttl = 6000;
  1379. }
  1380. preload_begin();
  1381. page_loading = 0;
  1382. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1383. url_in.len = s_len(url_in.text);
  1384. url_in.cursor = url_in.len;
  1385. url_in.scroll = 0;
  1386. ngui_set_focus(&ui, 0);
  1387. ngui_invalidate(&ui);
  1388. }
  1389. static void load_home(int push)
  1390. {
  1391. cancel_fetch = 0;
  1392. preload_active = 0;
  1393. preload_pending = 0;
  1394. heap_clear();
  1395. image_reset();
  1396. layout_valid = 0;
  1397. page_scroll.offset = 0;
  1398. page_scroll.dragging = 0;
  1399. br_parse_plain(&doc, "", 0);
  1400. page_loading = 1;
  1401. page_is_local = 0;
  1402. s_copy(page_url, "about:home", sizeof(page_url));
  1403. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1404. url_in.len = s_len(url_in.text);
  1405. url_in.cursor = url_in.len;
  1406. url_in.scroll = 0;
  1407. ngui_set_focus(&ui, 0);
  1408. if (push)
  1409. history_push(page_url);
  1410. ngui_invalidate(&ui);
  1411. redraw_now();
  1412. if (heap_start() != 0) {
  1413. show_error("Out of memory", "Cannot allocate the browser heap");
  1414. return;
  1415. }
  1416. ngui_font_attach(&ui);
  1417. browser_rules_alloc();
  1418. {
  1419. static const char home[] =
  1420. "<html><head><title>NebOS Browser</title><style>"
  1421. "body { margin: 10px; color: #e4e6f0; }"
  1422. "h1 { color: #7fb0ff; }"
  1423. ".box { background-color: #262b3a; margin: 12px; "
  1424. "padding: 10px; }"
  1425. "a { color: #7fb0ff; }"
  1426. "pre { background-color: #32384a; }"
  1427. "</style></head><body>"
  1428. "<h1>NebOS Browser</h1>"
  1429. "<p>A small HTML/CSS browser built on <b>ngui</b>, the "
  1430. "shared HTTP client and the image library.</p>"
  1431. "<div class=\"box\"><b>Try:</b>"
  1432. "<ul><li><a href=\"/floppy1/DEMO.HTM\">/"
  1433. "floppy1/DEMO.HTM</a> on the root floppy</li>"
  1434. "<li><a href=\"http://10.0.2.2:8000/\">"
  1435. "http://10.0.2.2:8000/</a> on the QEMU host</li>"
  1436. "<li><a href=\"https://example.com/\">"
  1437. "https://example.com/</a> (TLS, not verified)</li>"
  1438. "</ul></div>"
  1439. "<p>Type a URL in the bar above. Esc closes the "
  1440. "window, Ctrl+L focuses the address bar.</p>"
  1441. "<pre>javascript: not supported\nforms: not supported"
  1442. "\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
  1443. "</body></html>";
  1444. br_set_viewport(ui.w);
  1445. br_parse(&doc, home, sizeof(home) - 1);
  1446. br_parse_inline_css(&doc);
  1447. br_reset_styles(&doc);
  1448. s_copy(page_title, doc.title, sizeof(page_title));
  1449. }
  1450. post_status_text[0] = '\0';
  1451. post_status_error = 0;
  1452. post_status_ttl = 0;
  1453. page_loading = 0;
  1454. set_status("", 0, 0);
  1455. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1456. url_in.len = s_len(url_in.text);
  1457. url_in.cursor = url_in.len;
  1458. url_in.scroll = 0;
  1459. ngui_set_focus(&ui, 0);
  1460. ngui_invalidate(&ui);
  1461. }
  1462. static void navigate(const char *input, int push)
  1463. {
  1464. char target[BR_URL_MAX];
  1465. char *t;
  1466. if (!input)
  1467. return;
  1468. s_copy(target, input, sizeof(target));
  1469. t = trim(target);
  1470. if (!t[0])
  1471. return;
  1472. if (s_eq(t, "about:home")) {
  1473. load_home(push);
  1474. return;
  1475. }
  1476. if (starts_with(t, "file://")) {
  1477. make_abs_local(t + 7, target, sizeof(target));
  1478. load_page(target, 1, push);
  1479. return;
  1480. }
  1481. if (is_http(t)) {
  1482. load_page(t, 0, push);
  1483. return;
  1484. }
  1485. {
  1486. struct sys_stat st;
  1487. if (t[0] == '/' || (u_stat(t, &st) == 0 && !st.is_dir)) {
  1488. make_abs_local(t, target, sizeof(target));
  1489. load_page(target, 1, push);
  1490. return;
  1491. }
  1492. }
  1493. {
  1494. char with_scheme[BR_URL_MAX];
  1495. unsigned int n = 0;
  1496. s_append(with_scheme, &n, "http://", sizeof(with_scheme));
  1497. s_append(with_scheme, &n, t, sizeof(with_scheme));
  1498. load_page(with_scheme, 0, push);
  1499. }
  1500. }
  1501. /* ---- layout ---- */
  1502. static struct br_style base_style(void)
  1503. {
  1504. struct br_style s = { 0 };
  1505. s.color = ui.theme.text;
  1506. s.bg = ui.theme.bg;
  1507. s.has_bg = 0;
  1508. s.border_color = 0;
  1509. s.font_px = 16;
  1510. s.bold = 0;
  1511. s.italic = 0;
  1512. s.mono = 0;
  1513. s.underline = 0;
  1514. s.align = BR_ALIGN_LEFT;
  1515. s.display = BR_DISPLAY_BLOCK;
  1516. s.no_bullet = 0;
  1517. s.uppercase = 0;
  1518. s.border_w = 0;
  1519. s.width_kind = s.max_width_kind = s.height_kind = 0;
  1520. s.width = s.max_width = s.height = 0;
  1521. s.line_h_pct = 0;
  1522. s.line_h_px = 0;
  1523. s.mt = s.mb = s.ml = s.mr = 0;
  1524. s.pt = s.pb = s.pl = s.pr = 0;
  1525. return s;
  1526. }
  1527. static int run_width(const struct lay_run *r)
  1528. {
  1529. if (r->img >= 0)
  1530. return r->iw;
  1531. if (r->img == -2)
  1532. return r->iw;
  1533. return (int)r->w;
  1534. }
  1535. static void flush_line(struct lay *l)
  1536. {
  1537. int total, x, i, vis;
  1538. if (!l->nruns && l->line_x == l->left) {
  1539. l->line_h = 0;
  1540. return;
  1541. }
  1542. total = l->line_x - l->left;
  1543. x = l->left;
  1544. vis = 1;
  1545. if (l->draw) {
  1546. int lh = l->line_h ? l->line_h : 16;
  1547. vis = l->line_y + lh >= page_rect.y &&
  1548. l->line_y <= page_rect.y + page_rect.h;
  1549. }
  1550. if (l->align == BR_ALIGN_CENTER)
  1551. x += (l->right - l->left - total) / 2;
  1552. else if (l->align == BR_ALIGN_RIGHT)
  1553. x = l->right - total;
  1554. if (x < l->left)
  1555. x = l->left;
  1556. for (i = 0; i < l->nruns; i++) {
  1557. struct lay_run *r = &l->runs[i];
  1558. int w = run_width(r);
  1559. int ry = l->line_y + r->dy;
  1560. x += r->gap;
  1561. if (l->draw && vis) {
  1562. if (r->img == -2) {
  1563. int hh = r->ih ? r->ih : 20;
  1564. ngui_fill_rect(l->ui,
  1565. (struct ngui_rect){x, ry, w, hh},
  1566. l->ui->theme.panel_alt);
  1567. ngui_frame(l->ui,
  1568. (struct ngui_rect){x, ry, w, hh},
  1569. l->ui->theme.border);
  1570. ngui_text_px(l->ui, x + 6, ry + 2,
  1571. r->lit, s_len(r->lit), r->color,
  1572. r->font_px, r->bold, r->italic,
  1573. r->mono);
  1574. } else if (r->img >= 0) {
  1575. struct br_image *bi = &images[r->img];
  1576. if (bi->state == 1) {
  1577. struct img_blit b;
  1578. b.dst = l->ui->px;
  1579. b.dst_w = l->ui->w;
  1580. b.dst_h = l->ui->h;
  1581. b.clip_x = page_rect.x;
  1582. b.clip_y = page_rect.y;
  1583. b.clip_w = page_rect.w - 12;
  1584. b.clip_h = page_rect.h;
  1585. b.x = x;
  1586. b.y = ry;
  1587. b.scale = ((unsigned int)r->iw << 16) /
  1588. bi->img.w;
  1589. b.bg = ui.theme.bg;
  1590. b.bg2 = ui.theme.bg;
  1591. b.flags = IMG_BLIT_OVER_DST;
  1592. img_draw(&bi->img, &b);
  1593. }
  1594. } else {
  1595. const char *s = r->has_lit ? r->lit :
  1596. doc.text + r->off;
  1597. unsigned int n = r->has_lit ? s_len(r->lit) :
  1598. r->len;
  1599. ngui_text_px(l->ui, x, ry, s, n,
  1600. r->color, r->font_px, r->bold,
  1601. r->italic, r->mono);
  1602. if (r->underline)
  1603. ngui_fill_rect(l->ui,
  1604. (struct ngui_rect){x,
  1605. ry +
  1606. ngui_text_ascent(l->ui,
  1607. r->font_px,
  1608. r->mono) + 2,
  1609. w, 1}, r->color);
  1610. }
  1611. if (r->href) {
  1612. if (nhits < HIT_MAX) {
  1613. struct hit *h = &hits[nhits];
  1614. int hh = r->img >= 0 ? r->ih :
  1615. ngui_text_line_height(l->ui,
  1616. r->font_px, r->mono);
  1617. int merge = nhits > 0 &&
  1618. hits[nhits - 1].node == r->href &&
  1619. hits[nhits - 1].y == ry &&
  1620. hits[nhits - 1].h == hh &&
  1621. x <= hits[nhits - 1].x +
  1622. hits[nhits - 1].w + 2;
  1623. if (merge) {
  1624. struct hit *p = &hits[nhits - 1];
  1625. int end = x + w;
  1626. if (end > p->x + p->w)
  1627. p->w = end - p->x;
  1628. } else {
  1629. h->x = x;
  1630. h->y = ry;
  1631. h->w = w;
  1632. h->h = hh;
  1633. h->node = r->href;
  1634. nhits++;
  1635. }
  1636. }
  1637. }
  1638. }
  1639. x += w;
  1640. }
  1641. l->y += l->line_h + 2;
  1642. l->nruns = 0;
  1643. l->line_x = l->left;
  1644. l->run_x = l->left;
  1645. l->line_h = 0;
  1646. l->space_pending = 0;
  1647. }
  1648. static void add_run(struct lay *l, unsigned short off, unsigned short len,
  1649. const struct br_style *st, unsigned short href,
  1650. const char *lit)
  1651. {
  1652. unsigned int n = lit ? s_len(lit) : len;
  1653. const char *tp = lit ? lit : doc.text + off;
  1654. int w = (int)ngui_text_width_px(l->ui, tp, n, st->font_px,
  1655. st->bold, st->italic, st->mono);
  1656. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1657. flush_line(l);
  1658. if (l->nruns == 0) {
  1659. l->line_y = l->y;
  1660. l->line_h = 0;
  1661. }
  1662. {
  1663. struct lay_run *r = &l->runs[l->nruns++];
  1664. int gap = l->line_x - l->run_x;
  1665. if (gap < 0)
  1666. gap = 0;
  1667. r->gap = (unsigned short)gap;
  1668. r->off = off;
  1669. r->len = len;
  1670. r->color = st->color;
  1671. r->href = href;
  1672. r->dy = 0;
  1673. r->has_lit = 0;
  1674. r->lit[0] = '\0';
  1675. if (lit && lit[0]) {
  1676. unsigned int i;
  1677. for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++)
  1678. r->lit[i] = lit[i];
  1679. r->lit[i] = '\0';
  1680. r->has_lit = 1;
  1681. }
  1682. r->img = -1;
  1683. r->iw = r->ih = 0;
  1684. r->font_px = st->font_px;
  1685. r->bold = st->bold;
  1686. r->italic = st->italic;
  1687. r->mono = st->mono;
  1688. /* A control with its own background is a button, not body copy;
  1689. * an inherited link underline would strike through the fill. */
  1690. r->underline = st->underline && !st->has_bg;
  1691. r->w = (unsigned short)w;
  1692. if (r->has_lit)
  1693. r->w = (unsigned short)ngui_text_width_px(
  1694. l->ui, r->lit, s_len(r->lit), r->font_px,
  1695. r->bold, r->italic, r->mono);
  1696. w = r->w;
  1697. }
  1698. l->line_x += w;
  1699. l->run_x = l->line_x;
  1700. {
  1701. int lh = ngui_text_line_height(l->ui, st->font_px, st->mono);
  1702. if (st->line_h_px)
  1703. lh = st->line_h_px;
  1704. else if (st->line_h_pct)
  1705. lh = st->font_px * st->line_h_pct / 100;
  1706. if (lh < ngui_text_line_height(l->ui, st->font_px, st->mono))
  1707. lh = ngui_text_line_height(l->ui, st->font_px,
  1708. st->mono);
  1709. if (l->line_h < lh)
  1710. l->line_h = lh;
  1711. }
  1712. }
  1713. static void add_image_run(struct lay *l, unsigned int node,
  1714. const struct br_style *st, unsigned short href)
  1715. {
  1716. struct br_image *bi = 0;
  1717. int idx = doc.nodes[node].img_index;
  1718. int dw, dh, maxw;
  1719. if (idx >= 0 && idx < image_count && images[idx].state == 1)
  1720. bi = &images[idx];
  1721. if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
  1722. int sp = (int)ngui_text_width_px(l->ui, " ", 1, st->font_px,
  1723. st->bold, st->italic,
  1724. st->mono);
  1725. if (l->line_x + sp <= l->right)
  1726. l->line_x += sp;
  1727. }
  1728. l->space_pending = 0;
  1729. if (!bi) {
  1730. const char *alt = br_alt(&doc, node);
  1731. struct br_style gs = *st;
  1732. char tmp[96];
  1733. unsigned int n = 0;
  1734. int w;
  1735. gs.color = BR_GRAY;
  1736. gs.underline = 0;
  1737. s_append(tmp, &n, "[", sizeof(tmp));
  1738. s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp));
  1739. s_append(tmp, &n, "]", sizeof(tmp));
  1740. w = (int)ngui_text_width_px(l->ui, tmp, n, gs.font_px,
  1741. gs.bold, gs.italic, gs.mono);
  1742. if (l->line_x + w > l->right &&
  1743. (l->nruns > 0 || l->line_x > l->left))
  1744. flush_line(l);
  1745. add_run(l, 0, 0, &gs, href, tmp);
  1746. if (!l->draw) {
  1747. int t = l->line_y < 0 ? 0 : l->line_y;
  1748. int hh = ngui_text_line_height(l->ui, gs.font_px,
  1749. gs.mono);
  1750. if (t > 65535)
  1751. t = 65535;
  1752. if (hh > 65535)
  1753. hh = 65535;
  1754. img_top[node] = (unsigned short)t;
  1755. img_h[node] = (unsigned short)hh;
  1756. }
  1757. return;
  1758. }
  1759. maxw = l->right - l->left;
  1760. if (maxw < 8)
  1761. maxw = 8;
  1762. {
  1763. int iw = bi->native_w, ih = bi->native_h;
  1764. int have_w = 0, have_h = 0, css_w = 0, css_h = 0;
  1765. if (iw <= 0 || ih <= 0) {
  1766. iw = (int)bi->img.w;
  1767. ih = (int)bi->img.h;
  1768. }
  1769. dw = iw;
  1770. dh = ih;
  1771. if (doc.nodes[node].width_attr) {
  1772. int want = doc.nodes[node].width_attr;
  1773. dw = doc.nodes[node].width_pct ? maxw * want / 100 : want;
  1774. have_w = 1;
  1775. }
  1776. if (doc.nodes[node].height_attr) {
  1777. int want = doc.nodes[node].height_attr;
  1778. dh = doc.nodes[node].height_pct ? maxw * want / 100 : want;
  1779. have_h = 1;
  1780. }
  1781. if (have_w && !have_h)
  1782. dh = ih ? ih * dw / iw : 0;
  1783. if (have_h && !have_w)
  1784. dw = iw ? iw * dh / ih : 0;
  1785. if (st->width_kind) {
  1786. css_w = 1;
  1787. dw = st->width_kind == 2 ? maxw * (int)st->width / 100 :
  1788. (int)st->width;
  1789. }
  1790. if (st->height_kind) {
  1791. css_h = 1;
  1792. dh = st->height_kind == 2 ? maxw * (int)st->height / 100 :
  1793. (int)st->height;
  1794. }
  1795. if (css_w && !css_h)
  1796. dh = ih ? ih * dw / iw : 0;
  1797. if (css_h && !css_w)
  1798. dw = iw ? iw * dh / ih : 0;
  1799. if (st->max_width_kind) {
  1800. int mw = st->max_width_kind == 2 ?
  1801. maxw * (int)st->max_width / 100 :
  1802. (int)st->max_width;
  1803. if (mw > 0 && dw > mw) {
  1804. dh = iw ? ih * mw / iw : 0;
  1805. dw = mw;
  1806. }
  1807. }
  1808. }
  1809. if (dw < 8)
  1810. dw = 8;
  1811. if (dh < 8)
  1812. dh = 8;
  1813. if (dw > maxw) {
  1814. dh = dw ? dh * maxw / dw : dh;
  1815. dw = maxw;
  1816. }
  1817. if (!l->draw) {
  1818. int t = l->line_y < 0 ? 0 : l->line_y;
  1819. if (t > 65535)
  1820. t = 65535;
  1821. if (dh > 65535)
  1822. dh = 65535;
  1823. img_top[node] = (unsigned short)t;
  1824. img_h[node] = (unsigned short)dh;
  1825. }
  1826. if (l->line_x + dw > l->right && (l->nruns > 0 ||
  1827. l->line_x > l->left))
  1828. flush_line(l);
  1829. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1830. flush_line(l);
  1831. if (l->nruns == 0) {
  1832. l->line_y = l->y;
  1833. l->line_h = 0;
  1834. }
  1835. {
  1836. struct lay_run *r = &l->runs[l->nruns++];
  1837. int gap = l->line_x - l->run_x;
  1838. if (gap < 0)
  1839. gap = 0;
  1840. r->gap = (unsigned short)gap;
  1841. r->off = 0;
  1842. r->len = 0;
  1843. r->color = 0;
  1844. r->href = href;
  1845. /* Inline replaced elements honour their vertical margins; this
  1846. * is how Bootstrap's negative logo margins centre the wordmark. */
  1847. r->dy = st->mt;
  1848. r->has_lit = 0;
  1849. r->lit[0] = '\0';
  1850. r->img = (short)idx;
  1851. r->iw = (unsigned short)dw;
  1852. r->ih = (unsigned short)dh;
  1853. r->w = (unsigned short)dw;
  1854. r->font_px = st->font_px;
  1855. r->bold = st->bold;
  1856. r->italic = st->italic;
  1857. r->mono = st->mono;
  1858. r->underline = 0;
  1859. }
  1860. l->line_x += dw;
  1861. l->run_x = l->line_x;
  1862. if (l->line_h < dh)
  1863. l->line_h = dh;
  1864. }
  1865. static int text_width(struct lay *l, const char *s, unsigned int n,
  1866. const struct br_style *st)
  1867. {
  1868. return (int)ngui_text_width_px(l->ui, s, n, st->font_px,
  1869. st->bold, st->italic, st->mono);
  1870. }
  1871. /* Longest prefix that fits in maxw, at least one byte. */
  1872. static unsigned int fit_prefix(struct lay *l, const char *s, unsigned int n,
  1873. const struct br_style *st, int maxw)
  1874. {
  1875. unsigned int take = 0;
  1876. while (take < n) {
  1877. if (text_width(l, s, take + 1, st) > maxw)
  1878. break;
  1879. take++;
  1880. }
  1881. return take ? take : 1;
  1882. }
  1883. static void emit_word(struct lay *l, const char *s, unsigned int start,
  1884. unsigned int n, unsigned int base,
  1885. const struct br_style *st, unsigned short href)
  1886. {
  1887. int avail = l->right - l->left;
  1888. int w = text_width(l, s + start, n, st);
  1889. unsigned int off = start;
  1890. if (l->line_x + w > l->right &&
  1891. (l->nruns > 0 || l->line_x > l->left))
  1892. flush_line(l);
  1893. if (w <= avail) {
  1894. if (n)
  1895. add_run(l, (unsigned short)(base + start),
  1896. (unsigned short)n, st, href, 0);
  1897. return;
  1898. }
  1899. while (n > 0) {
  1900. unsigned int take = fit_prefix(l, s + off, n, st, avail);
  1901. add_run(l, (unsigned short)(base + off),
  1902. (unsigned short)take, st, href, 0);
  1903. off += take;
  1904. n -= take;
  1905. if (n)
  1906. flush_line(l);
  1907. }
  1908. }
  1909. static void emit_text(struct lay *l, const char *s, unsigned int len,
  1910. const struct br_style *st, unsigned short href, int pre)
  1911. {
  1912. unsigned int i = 0;
  1913. unsigned int base;
  1914. int space_w = (int)ngui_text_width_px(l->ui, " ", 1, st->font_px,
  1915. st->bold, st->italic, st->mono);
  1916. if (!s || !len)
  1917. return;
  1918. if (space_w <= 0)
  1919. space_w = 8;
  1920. base = (unsigned int)(s - doc.text);
  1921. if (st->uppercase) {
  1922. char *p = (char *)s;
  1923. unsigned int j;
  1924. for (j = 0; j < len; j++)
  1925. if (p[j] >= 'a' && p[j] <= 'z')
  1926. p[j] = (char)(p[j] - 'a' + 'A');
  1927. }
  1928. if (pre) {
  1929. while (i < len) {
  1930. unsigned int start;
  1931. if (s[i] == '\n') {
  1932. flush_line(l);
  1933. i++;
  1934. continue;
  1935. }
  1936. if (s[i] == ' ') {
  1937. if (l->line_x + space_w > l->right &&
  1938. (l->nruns > 0 || l->line_x > l->left))
  1939. flush_line(l);
  1940. else
  1941. l->line_x += space_w;
  1942. i++;
  1943. continue;
  1944. }
  1945. start = i;
  1946. while (i < len && s[i] != ' ' && s[i] != '\n')
  1947. i++;
  1948. emit_word(l, s, start, i - start, base, st, href);
  1949. }
  1950. return;
  1951. }
  1952. while (i < len) {
  1953. unsigned int start, n;
  1954. if (s[i] == ' ') {
  1955. l->space_pending = 1;
  1956. i++;
  1957. continue;
  1958. }
  1959. start = i;
  1960. while (i < len && s[i] != ' ')
  1961. i++;
  1962. n = i - start;
  1963. if (l->space_pending &&
  1964. (l->nruns > 0 || l->line_x > l->left)) {
  1965. if (l->line_x + space_w <= l->right)
  1966. l->line_x += space_w;
  1967. }
  1968. l->space_pending = 0;
  1969. emit_word(l, s, start, n, base, st, href);
  1970. }
  1971. }
  1972. static int list_index(unsigned int parent, unsigned int node)
  1973. {
  1974. unsigned int n = 0, i;
  1975. for (i = doc.nodes[parent].first; i && i != node;
  1976. i = doc.nodes[i].next)
  1977. if (doc.nodes[i].tag == BR_TAG_LI)
  1978. n++;
  1979. return (int)n + 1;
  1980. }
  1981. static const char *form_text(unsigned int node)
  1982. {
  1983. const char *v = br_value(&doc, node);
  1984. const char *p = br_placeholder(&doc, node);
  1985. const char *t = br_type(&doc, node);
  1986. if (s_eq(t, "hidden"))
  1987. return 0;
  1988. if (s_eq(t, "submit") || s_eq(t, "button") || s_eq(t, "reset"))
  1989. return v[0] ? v : "Button";
  1990. if (v[0])
  1991. return v;
  1992. if (p[0])
  1993. return p;
  1994. if (doc.nodes[node].tag == BR_TAG_TEXTAREA)
  1995. return "Text";
  1996. if (doc.nodes[node].tag == BR_TAG_SELECT)
  1997. return "Select";
  1998. return "Search";
  1999. }
  2000. static void add_form_run(struct lay *l, unsigned int node,
  2001. const struct br_style *st, unsigned short href)
  2002. {
  2003. const char *text = form_text(node);
  2004. struct lay_run *r;
  2005. int tw, w, h, i;
  2006. if (!text)
  2007. return;
  2008. tw = (int)ngui_text_width_px(l->ui, text, s_len(text), st->font_px,
  2009. st->bold, st->italic, st->mono);
  2010. h = ngui_text_line_height(l->ui, st->font_px, st->mono) + 4;
  2011. if (h < 20)
  2012. h = 20;
  2013. if (st->width_kind == 1)
  2014. w = st->width;
  2015. else if (st->width_kind == 2)
  2016. w = (l->right - l->left) * (int)st->width / 100;
  2017. else
  2018. w = tw + 18;
  2019. if (w < 64)
  2020. w = 64;
  2021. if (w > l->right - l->left)
  2022. w = l->right - l->left;
  2023. if (w < 8)
  2024. w = 8;
  2025. if (l->line_x + w > l->right &&
  2026. (l->nruns > 0 || l->line_x > l->left))
  2027. flush_line(l);
  2028. if (l->nruns >= 96)
  2029. return;
  2030. r = &l->runs[l->nruns++];
  2031. r->gap = (unsigned short)(l->line_x - l->run_x);
  2032. r->off = r->len = 0;
  2033. r->color = st->color;
  2034. r->href = href;
  2035. r->dy = 0;
  2036. r->has_lit = 1;
  2037. for (i = 0; i + 1 < (int)sizeof(r->lit) && text[i]; i++)
  2038. r->lit[i] = text[i];
  2039. r->lit[i] = '\0';
  2040. r->img = -2;
  2041. r->iw = (unsigned short)w;
  2042. r->ih = (unsigned short)h;
  2043. r->w = (unsigned short)w;
  2044. r->font_px = st->font_px;
  2045. r->bold = st->bold;
  2046. r->italic = st->italic;
  2047. r->mono = st->mono;
  2048. r->underline = 0;
  2049. l->line_x += w;
  2050. l->run_x = l->line_x;
  2051. if (l->line_h < h)
  2052. l->line_h = h;
  2053. }
  2054. /* A poster wrapper is absolutely positioned over the media slot; the
  2055. * measured height of its picture is normally added back to the flow. When a
  2056. * loading-skeleton sibling already reserves that slot, adding both doubles
  2057. * the media height and pushes the article text a full screen down. */
  2058. static int has_skeleton_sibling(unsigned int node)
  2059. {
  2060. unsigned int p = doc.nodes[node].parent, c;
  2061. for (c = doc.nodes[p].first; c; c = doc.nodes[c].next) {
  2062. if (c == node || doc.nodes[c].type != BR_NODE_ELEM)
  2063. continue;
  2064. if (doc.nodes[c].class_off &&
  2065. contains_ci(doc.attrs + doc.nodes[c].class_off,
  2066. "skeleton"))
  2067. return 1;
  2068. }
  2069. return 0;
  2070. }
  2071. static void layout_children(struct lay *l, unsigned int parent,
  2072. const struct br_style *pst, int depth,
  2073. unsigned short link);
  2074. static void layout_flex_children(struct lay *l, unsigned int parent,
  2075. const struct br_style *pst, int depth,
  2076. unsigned short link);
  2077. static void layout_grid_children(struct lay *l, unsigned int parent,
  2078. const struct br_style *pst, int depth,
  2079. unsigned short link);
  2080. static void layout_grid_auto(struct lay *l, unsigned int parent,
  2081. const struct br_style *pst, int depth,
  2082. unsigned short link);
  2083. static int flex_item_width(struct lay *l, unsigned int n,
  2084. const struct br_style *cs, int avail);
  2085. static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
  2086. const struct br_style *pst, int depth,
  2087. unsigned short link);
  2088. static void layout_table_row(struct lay *l, unsigned int row,
  2089. const struct br_style *pst, int depth,
  2090. unsigned short link);
  2091. /* A flex container whose items are only inline spans is usually just a
  2092. * heading/icon pair. Our flex row positions such items without emitting
  2093. * their inline text in some deep page contexts; normal inline flow renders
  2094. * the same content correctly. */
  2095. static int flex_wants_row(unsigned int parent, const struct br_style *pst)
  2096. {
  2097. unsigned int c;
  2098. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  2099. struct br_node *nd = &doc.nodes[c];
  2100. struct br_style cs;
  2101. if (nd->type == BR_NODE_TEXT)
  2102. continue;
  2103. br_style_for(&doc, c, pst, &cs);
  2104. if (cs.display == BR_DISPLAY_NONE)
  2105. continue;
  2106. if (cs.display != BR_DISPLAY_INLINE || cs.flex_grow ||
  2107. cs.width_kind || cs.has_bg || cs.border_w ||
  2108. nd->tag == BR_TAG_IMG)
  2109. return 1;
  2110. }
  2111. return 0;
  2112. }
  2113. static int css_radius(const struct br_style *st, int corner,
  2114. int w, int h)
  2115. {
  2116. int r = st->radius[corner];
  2117. if (r <= 0)
  2118. return 0;
  2119. if (r > w / 2)
  2120. r = w / 2;
  2121. if (r > h / 2)
  2122. r = h / 2;
  2123. return r;
  2124. }
  2125. static void svg_viewbox(unsigned node, int *vw, int *vh)
  2126. {
  2127. struct br_node *nd = &doc.nodes[node];
  2128. if (nd->width_attr && nd->height_attr) {
  2129. *vw = nd->width_attr;
  2130. *vh = nd->height_attr;
  2131. } else {
  2132. *vw = 300;
  2133. *vh = 150;
  2134. }
  2135. if (*vw < 1)
  2136. *vw = 1;
  2137. if (*vh < 1)
  2138. *vh = 1;
  2139. }
  2140. static int svg_num(const char **pp)
  2141. {
  2142. const char *p = *pp;
  2143. int neg = 0, v = 0, frac = 0, scale = 1;
  2144. while (*p == ' ' || *p == ',' || *p == '\t' ||
  2145. *p == '\n' || *p == '\r')
  2146. p++;
  2147. if (*p == '-') {
  2148. neg = 1;
  2149. p++;
  2150. } else if (*p == '+') {
  2151. p++;
  2152. }
  2153. while (*p >= '0' && *p <= '9') {
  2154. v = v * 10 + (*p - '0');
  2155. p++;
  2156. }
  2157. if (*p == '.') {
  2158. p++;
  2159. while (*p >= '0' && *p <= '9' && scale < 1000) {
  2160. frac = frac * 10 + (*p - '0');
  2161. scale *= 10;
  2162. p++;
  2163. }
  2164. }
  2165. v += frac / scale;
  2166. *pp = p;
  2167. return neg ? -v : v;
  2168. }
  2169. static void svg_path(struct ngui *ui, const char *d,
  2170. int ox, int oy, int dw, int dh, int vw, int vh,
  2171. unsigned short color)
  2172. {
  2173. int cx = 0, cy = 0, sx = 0, sy = 0;
  2174. char cmd = 0;
  2175. const char *p = d;
  2176. while (p && *p) {
  2177. int rel, nx, ny;
  2178. char c;
  2179. while (*p == ' ' || *p == ',' || *p == '\t' ||
  2180. *p == '\n' || *p == '\r')
  2181. p++;
  2182. if (!*p)
  2183. break;
  2184. if ((*p >= 'A' && *p <= 'Z') ||
  2185. (*p >= 'a' && *p <= 'z'))
  2186. cmd = *p++;
  2187. else if (!cmd) {
  2188. p++;
  2189. continue;
  2190. }
  2191. rel = cmd >= 'a' && cmd <= 'z';
  2192. c = rel ? (char)(cmd - 'a' + 'A') : cmd;
  2193. if (c == 'Z') {
  2194. ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
  2195. ox + sx * dw / vw, oy + sy * dh / vh,
  2196. color);
  2197. cx = sx;
  2198. cy = sy;
  2199. cmd = 0;
  2200. continue;
  2201. }
  2202. if (c == 'H' || c == 'V') {
  2203. const char *save = p;
  2204. int a = svg_num(&p);
  2205. if (p == save) {
  2206. p++;
  2207. continue;
  2208. }
  2209. nx = cx;
  2210. ny = cy;
  2211. if (c == 'H')
  2212. nx = rel ? cx + a : a;
  2213. else
  2214. ny = rel ? cy + a : a;
  2215. } else {
  2216. const char *save = p;
  2217. int a = svg_num(&p);
  2218. int b = svg_num(&p);
  2219. if (p == save) {
  2220. p++;
  2221. continue;
  2222. }
  2223. if (c == 'M') {
  2224. nx = rel ? cx + a : a;
  2225. ny = rel ? cy + b : b;
  2226. cx = sx = nx;
  2227. cy = sy = ny;
  2228. cmd = rel ? 'l' : 'L';
  2229. continue;
  2230. }
  2231. if (c != 'L')
  2232. break;
  2233. nx = rel ? cx + a : a;
  2234. ny = rel ? cy + b : b;
  2235. }
  2236. ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh,
  2237. ox + nx * dw / vw, oy + ny * dh / vh, color);
  2238. cx = nx;
  2239. cy = ny;
  2240. }
  2241. }
  2242. static void draw_svg(struct ngui *ui, unsigned node,
  2243. int ox, int oy, int dw, int dh, int vw, int vh,
  2244. const struct br_style *pst)
  2245. {
  2246. unsigned int c;
  2247. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  2248. struct br_node *nd = &doc.nodes[c];
  2249. struct br_style st;
  2250. const char *nm;
  2251. if (nd->type != BR_NODE_ELEM || !nd->name_off)
  2252. continue;
  2253. nm = doc.attrs + nd->name_off;
  2254. br_style_for(&doc, c, pst, &st);
  2255. if (s_eq(nm, "rect")) {
  2256. struct ngui_rect r;
  2257. int rx = (int)nd->svg_rx * dw / vw;
  2258. r.x = ox + (int)nd->svg_x * dw / vw;
  2259. r.y = oy + (int)nd->svg_y * dh / vh;
  2260. r.w = (int)nd->width_attr * dw / vw;
  2261. r.h = (int)nd->height_attr * dh / vh;
  2262. if (r.w < 1)
  2263. r.w = 1;
  2264. if (r.h < 1)
  2265. r.h = 1;
  2266. if (rx > r.w / 2)
  2267. rx = r.w / 2;
  2268. if (rx > r.h / 2)
  2269. rx = r.h / 2;
  2270. if (st.has_bg) {
  2271. if (rx)
  2272. ngui_fill_round_rect(ui, r, rx, rx,
  2273. rx, rx, st.bg);
  2274. else
  2275. ngui_fill_rect(ui, r, st.bg);
  2276. }
  2277. if (st.border_w) {
  2278. if (rx)
  2279. ngui_frame_round(ui, r, rx, rx, rx, rx,
  2280. st.border_w,
  2281. st.border_color);
  2282. else
  2283. ngui_frame(ui, r, st.border_color);
  2284. }
  2285. } else if (s_eq(nm, "line")) {
  2286. if (st.border_w)
  2287. ngui_line(ui,
  2288. ox + (int)nd->svg_x * dw / vw,
  2289. oy + (int)nd->svg_y * dh / vh,
  2290. ox + (int)nd->width_attr * dw / vw,
  2291. oy + (int)nd->height_attr * dh / vh,
  2292. st.border_color);
  2293. } else if (s_eq(nm, "path")) {
  2294. if (st.border_w && nd->src_off)
  2295. svg_path(ui, doc.attrs + nd->src_off,
  2296. ox, oy, dw, dh, vw, vh,
  2297. st.border_color);
  2298. } else if (s_eq(nm, "text")) {
  2299. unsigned int t = nd->first;
  2300. const char *s;
  2301. unsigned int n, tw;
  2302. int px = st.font_px ? st.font_px : 16;
  2303. int tx, ty, anchor;
  2304. if (!t || doc.nodes[t].type != BR_NODE_TEXT)
  2305. continue;
  2306. s = doc.text + doc.nodes[t].text_off;
  2307. n = doc.nodes[t].text_len;
  2308. tw = ngui_text_width_px(ui, s, n, px, st.bold,
  2309. st.italic, st.mono);
  2310. tx = ox + (int)nd->svg_x * dw / vw;
  2311. ty = oy + (int)nd->svg_y * dh / vh -
  2312. ngui_text_ascent(ui, px, st.mono);
  2313. anchor = st.align;
  2314. if (nd->attr_flags & BR_ATTR_ANCHOR_MIDDLE)
  2315. anchor = BR_ALIGN_CENTER;
  2316. else if (nd->attr_flags & BR_ATTR_ANCHOR_END)
  2317. anchor = BR_ALIGN_RIGHT;
  2318. if (anchor == BR_ALIGN_CENTER)
  2319. tx -= (int)tw / 2;
  2320. else if (anchor == BR_ALIGN_RIGHT)
  2321. tx -= (int)tw;
  2322. ngui_text_px(ui, tx, ty, s, n, st.color, px,
  2323. st.bold, st.italic, st.mono);
  2324. }
  2325. }
  2326. }
  2327. static void layout_block(struct lay *l, unsigned int node,
  2328. const struct br_style *st, int depth,
  2329. unsigned short link)
  2330. {
  2331. int old_left = l->left, old_right = l->right, old_align = l->align;
  2332. int top, l0, r0;
  2333. int flow_y, flow_left, flow_right, positioned;
  2334. if (depth > 24 || st->display == BR_DISPLAY_NONE)
  2335. return;
  2336. flow_y = l->y;
  2337. flow_left = l->left;
  2338. flow_right = l->right;
  2339. positioned = st->pos;
  2340. if (positioned) {
  2341. if (st->pos == 2) {
  2342. l->y = page_rect.y + (st->top_set ? st->top : 0);
  2343. l->left = page_rect.x;
  2344. l->right = page_rect.x + page_rect.w - 12;
  2345. } else {
  2346. l->y = flow_y + (st->top_set ? st->top : 0);
  2347. l->left = flow_left + (st->left_set ? st->left : 0);
  2348. l->right = flow_right + (st->left_set ? st->left : 0);
  2349. }
  2350. old_left = l->left;
  2351. old_right = l->right;
  2352. }
  2353. if (doc.nodes[node].tag == BR_TAG_INPUT ||
  2354. doc.nodes[node].tag == BR_TAG_TEXTAREA ||
  2355. doc.nodes[node].tag == BR_TAG_SELECT) {
  2356. if (!(doc.nodes[node].tag == BR_TAG_INPUT &&
  2357. s_eq(br_type(&doc, node), "hidden")))
  2358. add_form_run(l, node, st, link);
  2359. l->y += 20;
  2360. return;
  2361. }
  2362. flush_line(l);
  2363. l->y += st->mt;
  2364. top = l->y;
  2365. l0 = old_left + st->ml;
  2366. r0 = old_right - st->mr;
  2367. if (r0 < l0 + 16)
  2368. r0 = l0 + 16;
  2369. if (l->draw && block_h[node] &&
  2370. top < page_rect.y + page_rect.h &&
  2371. top + block_h[node] > page_rect.y) {
  2372. struct ngui_rect box = {l0, top, r0 - l0, block_h[node]};
  2373. int rtl = css_radius(st, 0, box.w, box.h);
  2374. int rtr = css_radius(st, 1, box.w, box.h);
  2375. int rbr = css_radius(st, 2, box.w, box.h);
  2376. int rbl = css_radius(st, 3, box.w, box.h);
  2377. if (rtl || rtr || rbr || rbl) {
  2378. if (st->has_bg) {
  2379. int bw = st->border_w;
  2380. if (bw > 0 && st->border_color != st->bg &&
  2381. box.w > bw * 2 && box.h > bw * 2) {
  2382. struct ngui_rect inner = {
  2383. box.x + bw, box.y + bw,
  2384. box.w - bw * 2, box.h - bw * 2
  2385. };
  2386. int itl = rtl > bw ? rtl - bw : 0;
  2387. int itr = rtr > bw ? rtr - bw : 0;
  2388. int ibr = rbr > bw ? rbr - bw : 0;
  2389. int ibl = rbl > bw ? rbl - bw : 0;
  2390. ngui_fill_round_rect(l->ui, box, rtl,
  2391. rtr, rbr, rbl,
  2392. st->border_color);
  2393. ngui_fill_round_rect(l->ui, inner,
  2394. itl, itr, ibr,
  2395. ibl, st->bg);
  2396. } else {
  2397. ngui_fill_round_rect(l->ui, box, rtl,
  2398. rtr, rbr, rbl,
  2399. st->bg);
  2400. }
  2401. } else if (st->border_w) {
  2402. ngui_frame_round(l->ui, box, rtl, rtr, rbr,
  2403. rbl, st->border_w,
  2404. st->border_color);
  2405. }
  2406. } else {
  2407. if (st->has_bg)
  2408. ngui_fill_rect(l->ui, box, st->bg);
  2409. if (st->border_w)
  2410. ngui_frame(l->ui, box, st->border_color);
  2411. }
  2412. }
  2413. l->left = l0 + st->pl;
  2414. l->right = r0 - st->pr;
  2415. if (l->right < l->left + 8)
  2416. l->right = l->left + 8;
  2417. l->align = st->align;
  2418. l->y += st->pt;
  2419. if (doc.nodes[node].tag == BR_TAG_HR) {
  2420. if (l->draw)
  2421. ngui_fill_rect(l->ui, (struct ngui_rect){l->left,
  2422. l->y + 2, l->right - l->left, 1}, BR_GRAY);
  2423. l->y += 8;
  2424. } else if (doc.nodes[node].tag == BR_TAG_VIDEO) {
  2425. int vw = l->right - l->left;
  2426. int vh;
  2427. if (doc.nodes[node].width_pct)
  2428. vw = vw * doc.nodes[node].width_attr / 100;
  2429. else if (doc.nodes[node].width_attr)
  2430. vw = doc.nodes[node].width_attr;
  2431. if (vw > l->right - l->left)
  2432. vw = l->right - l->left;
  2433. if (vw < 8)
  2434. vw = 8;
  2435. vh = vw * 9 / 16;
  2436. if (vh > 240)
  2437. vh = 240;
  2438. if (vh < 80)
  2439. vh = 80;
  2440. if (l->draw) {
  2441. ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y,
  2442. vw, vh}, 0xD69A);
  2443. ngui_frame(l->ui, (struct ngui_rect){l->left, l->y,
  2444. vw, vh}, 0x8410);
  2445. ngui_text(l->ui, l->left + 8, l->y + 8, "[video]",
  2446. 0x4208);
  2447. }
  2448. l->y += vh + 4;
  2449. } else if (doc.nodes[node].tag == BR_TAG_SVG) {
  2450. int vw, vh, dw = l->right - l->left;
  2451. int dh;
  2452. svg_viewbox(node, &vw, &vh);
  2453. if (dw < 8)
  2454. dw = 8;
  2455. dh = dw * vh / vw;
  2456. if (dh < 8)
  2457. dh = 8;
  2458. if (dh > 1400)
  2459. dh = 1400;
  2460. if (l->draw) {
  2461. struct ngui_rect clip = {l->left, l->y, dw, dh};
  2462. ngui_push_clip(l->ui, clip);
  2463. draw_svg(l->ui, node, l->left, l->y, dw, dh,
  2464. vw, vh, st);
  2465. ngui_pop_clip(l->ui);
  2466. }
  2467. l->y += dh;
  2468. } else {
  2469. if (doc.nodes[node].tag == BR_TAG_LI && !st->no_bullet) {
  2470. char *m = li_marker[node];
  2471. if (!m[0]) {
  2472. unsigned int parent = doc.nodes[node].parent;
  2473. if (parent &&
  2474. doc.nodes[parent].tag == BR_TAG_OL) {
  2475. int idx = list_index(parent, node);
  2476. unsigned int n = 0;
  2477. s_append_dec(m, &n, (unsigned int)idx, 6);
  2478. s_append(m, &n, ".", 6);
  2479. } else {
  2480. s_copy(m, "-", 6);
  2481. }
  2482. }
  2483. if (l->draw) {
  2484. int mw = (int)ngui_text_width_ui(l->ui, m);
  2485. ngui_text(l->ui, l->left - mw - 6, l->y, m,
  2486. st->color);
  2487. }
  2488. }
  2489. if (doc.nodes[node].tag == BR_TAG_TR)
  2490. layout_table_row(l, node, st, depth + 1, link);
  2491. else if (st->grid_auto)
  2492. layout_grid_auto(l, node, st, depth + 1, link);
  2493. else if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 &&
  2494. st->grid_gap > 0)
  2495. layout_grid_children(l, node, st, depth + 1, link);
  2496. else if (st->display == BR_DISPLAY_FLEX && !st->flex_col &&
  2497. flex_wants_row(node, st))
  2498. layout_flex_children(l, node, st, depth + 1, link);
  2499. else
  2500. layout_children(l, node, st, depth + 1, link);
  2501. }
  2502. flush_line(l);
  2503. l->y += st->pb;
  2504. /* Honour an explicit CSS height. Fixed-height header/icon boxes often
  2505. * contain only absolutely positioned children, so their flow height
  2506. * would otherwise collapse to zero. */
  2507. if (st->height_kind == 1 && st->height > 0 &&
  2508. l->y < top + st->height)
  2509. l->y = top + st->height;
  2510. l->left = old_left;
  2511. l->right = old_right;
  2512. l->align = old_align;
  2513. l->y += st->mb;
  2514. if (!l->draw) {
  2515. /* block_h is the element box, not the flow advance: the bottom
  2516. * margin must not be painted as part of the background. */
  2517. int h = l->y - st->mb - top;
  2518. if (h < 0)
  2519. h = 0;
  2520. if (h > 65535)
  2521. h = 65535;
  2522. block_h[node] = (unsigned short)h;
  2523. }
  2524. if (positioned) {
  2525. l->y = flow_y;
  2526. l->left = flow_left;
  2527. l->right = flow_right;
  2528. }
  2529. }
  2530. static int text_node_blank(const struct br_node *nd)
  2531. {
  2532. const char *t = doc.text + nd->text_off;
  2533. unsigned int i;
  2534. for (i = 0; i < nd->text_len; i++)
  2535. if (t[i] != ' ' && t[i] != '\t' && t[i] != '\n' &&
  2536. t[i] != '\r')
  2537. return 0;
  2538. return 1;
  2539. }
  2540. /* Rough shrink-to-fit width for a float without an explicit width: sum the
  2541. * text/image widths in the subtree, plus padding and margins. This keeps
  2542. * Bootstrap's floated nav items on one line instead of giving each the whole
  2543. * row. */
  2544. static int text_space(char c)
  2545. {
  2546. return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
  2547. }
  2548. static int intrinsic_width(struct ngui *ui, unsigned int node,
  2549. const struct br_style *pst, int depth)
  2550. {
  2551. unsigned int c;
  2552. int w = 0;
  2553. if (depth > 12)
  2554. return 0;
  2555. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  2556. struct br_node *nd = &doc.nodes[c];
  2557. struct br_style cs;
  2558. if (nd->type == BR_NODE_TEXT) {
  2559. const char *t = doc.text + nd->text_off;
  2560. unsigned int tn = nd->text_len;
  2561. if (doc.nodes[node].tag == BR_TAG_PRE) {
  2562. w += (int)ngui_text_width_px(ui, t, tn,
  2563. pst->font_px, pst->bold, pst->italic,
  2564. pst->mono);
  2565. } else {
  2566. unsigned int ti = 0;
  2567. int first = 1;
  2568. int lead = w > 0 && tn > 0 && text_space(t[0]);
  2569. int sp = (int)ngui_text_width_px(ui, " ", 1,
  2570. pst->font_px, pst->bold, pst->italic,
  2571. pst->mono);
  2572. while (ti < tn) {
  2573. unsigned int ts;
  2574. while (ti < tn && text_space(t[ti]))
  2575. ti++;
  2576. ts = ti;
  2577. while (ti < tn && !text_space(t[ti]))
  2578. ti++;
  2579. if (ti == ts)
  2580. break;
  2581. if (!first || lead)
  2582. w += sp;
  2583. lead = 0;
  2584. w += (int)ngui_text_width_px(ui,
  2585. t + ts, ti - ts,
  2586. pst->font_px, pst->bold,
  2587. pst->italic, pst->mono);
  2588. first = 0;
  2589. }
  2590. }
  2591. continue;
  2592. }
  2593. br_style_for(&doc, c, pst, &cs);
  2594. if (cs.display == BR_DISPLAY_NONE)
  2595. continue;
  2596. if (nd->tag == BR_TAG_BR) {
  2597. w += 8;
  2598. continue;
  2599. }
  2600. if (nd->tag == BR_TAG_IMG) {
  2601. /* Measure the image as layout will draw it: a CSS or
  2602. * attribute height keeps the intrinsic aspect ratio, and
  2603. * a CSS width overrides the native pixel width. Bootstrap
  2604. * sizes the Cloudron logo with height:40px only. */
  2605. int idx = nd->img_index;
  2606. int iw = 0, ih = 0;
  2607. if (idx >= 0 && idx < image_count &&
  2608. images[idx].state == 1) {
  2609. iw = (int)images[idx].native_w;
  2610. ih = (int)images[idx].native_h;
  2611. if (iw <= 0 || ih <= 0) {
  2612. iw = (int)images[idx].img.w;
  2613. ih = (int)images[idx].img.h;
  2614. }
  2615. }
  2616. if (cs.width_kind == 1) {
  2617. w += cs.width;
  2618. } else if (cs.width_kind == 2) {
  2619. w += 64;
  2620. } else if (nd->width_attr) {
  2621. w += nd->width_attr;
  2622. } else if (nd->height_attr && iw && ih) {
  2623. w += iw * (int)nd->height_attr / ih;
  2624. } else if (cs.height_kind == 1 && iw && ih) {
  2625. w += iw * (int)cs.height / ih;
  2626. } else if (iw) {
  2627. w += iw;
  2628. } else {
  2629. w += 64;
  2630. }
  2631. continue;
  2632. }
  2633. w += cs.pl + cs.pr + cs.ml + cs.mr;
  2634. w += intrinsic_width(ui, c, &cs, depth + 1);
  2635. if (w > 4096)
  2636. break;
  2637. }
  2638. return w;
  2639. }
  2640. /* Lay consecutive floated block children side by side. Bootstrap's grid is
  2641. * the main user: .row > .col-md-* with float:left and a percentage width.
  2642. * Each column is laid out with its own left/right edge and the row advances
  2643. * to the tallest column. Returns the first child not consumed. */
  2644. static unsigned int layout_float_group(struct lay *l, unsigned int first,
  2645. const struct br_style *pst, int depth,
  2646. unsigned short link)
  2647. {
  2648. unsigned int n = first;
  2649. int row_left = l->left, row_right = l->right;
  2650. int row_top, row_bottom, xl, xr, any = 0;
  2651. flush_line(l);
  2652. row_top = l->y;
  2653. row_bottom = l->y;
  2654. xl = row_left;
  2655. xr = row_right;
  2656. while (n) {
  2657. struct br_node *nd = &doc.nodes[n];
  2658. struct br_style cs;
  2659. unsigned short lk = link;
  2660. int colw, right;
  2661. if (nd->type == BR_NODE_TEXT) {
  2662. if (text_node_blank(nd)) {
  2663. n = nd->next;
  2664. continue;
  2665. }
  2666. break;
  2667. }
  2668. br_style_for(&doc, n, pst, &cs);
  2669. if (cs.display == BR_DISPLAY_NONE) {
  2670. n = nd->next;
  2671. continue;
  2672. }
  2673. /* A float blockifies its box: Bootstrap floats inline
  2674. * anchors too (the navbar brand and its text links), and
  2675. * their padding must be honoured. */
  2676. if (!cs.float_left)
  2677. break;
  2678. if (nd->tag == BR_TAG_A && nd->href_off)
  2679. lk = (unsigned short)n;
  2680. right = cs.float_left == 2;
  2681. if (cs.width_kind == 2)
  2682. colw = (row_right - row_left) * (int)cs.width / 100;
  2683. else if (cs.width_kind == 1)
  2684. colw = (int)cs.width;
  2685. else {
  2686. colw = intrinsic_width(l->ui, n, &cs, 0) +
  2687. cs.pl + cs.pr + cs.ml + cs.mr;
  2688. if (colw < 8)
  2689. colw = 8;
  2690. }
  2691. if (colw < 8)
  2692. colw = 8;
  2693. if (colw > row_right - row_left)
  2694. colw = row_right - row_left;
  2695. if (right) {
  2696. if (any && xr - colw < xl) {
  2697. row_top = row_bottom;
  2698. row_bottom = row_top;
  2699. xl = row_left;
  2700. xr = row_right;
  2701. }
  2702. if (xr - colw < xl)
  2703. colw = xr - xl;
  2704. if (colw < 8)
  2705. colw = 8;
  2706. l->left = xr - colw;
  2707. if (l->left < row_left)
  2708. l->left = row_left;
  2709. l->right = xr;
  2710. xr = l->left;
  2711. } else {
  2712. if (any && xl + colw > xr) {
  2713. row_top = row_bottom;
  2714. row_bottom = row_top;
  2715. xl = row_left;
  2716. xr = row_right;
  2717. }
  2718. l->left = xl;
  2719. l->right = xl + colw;
  2720. xl += colw;
  2721. }
  2722. if (l->right < l->left + 8)
  2723. l->right = l->left + 8;
  2724. l->y = row_top;
  2725. l->line_x = l->left;
  2726. l->run_x = l->left;
  2727. l->line_y = l->y;
  2728. l->line_h = 0;
  2729. l->nruns = 0;
  2730. l->space_pending = 0;
  2731. l->align = cs.align;
  2732. layout_block(l, n, &cs, depth + 1, lk);
  2733. if (l->y > row_bottom)
  2734. row_bottom = l->y;
  2735. any = 1;
  2736. n = nd->next;
  2737. }
  2738. l->left = row_left;
  2739. l->right = row_right;
  2740. l->y = row_bottom;
  2741. l->line_x = l->left;
  2742. l->run_x = l->left;
  2743. l->line_y = l->y;
  2744. l->line_h = 0;
  2745. l->nruns = 0;
  2746. l->space_pending = 0;
  2747. return n;
  2748. }
  2749. static int flex_item_width(struct lay *l, unsigned int n,
  2750. const struct br_style *cs, int avail)
  2751. {
  2752. struct br_node *nd = &doc.nodes[n];
  2753. (void)l;
  2754. int w;
  2755. if (cs->width_kind == 2)
  2756. w = avail * (int)cs->width / 100;
  2757. else if (cs->width_kind == 1)
  2758. w = (int)cs->width;
  2759. else if (nd->tag == BR_TAG_IMG) {
  2760. int idx = nd->img_index;
  2761. if (idx >= 0 && idx < image_count &&
  2762. images[idx].state == 1)
  2763. w = (int)images[idx].img.w;
  2764. else if (nd->width_attr)
  2765. w = (int)nd->width_attr;
  2766. else
  2767. w = 64;
  2768. } else {
  2769. w = intrinsic_width(l->ui, n, cs, 0);
  2770. w += cs->pl + cs->pr + cs->ml + cs->mr;
  2771. }
  2772. if (w < 24)
  2773. w = 24;
  2774. if (w > avail)
  2775. w = avail;
  2776. return w;
  2777. }
  2778. /* A run of display:inline-block siblings (file tags, small buttons) belongs
  2779. * on one line, not one full-width block each. Place intrinsic widths from
  2780. * left to right and wrap when the row is full. */
  2781. static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
  2782. const struct br_style *pst, int depth,
  2783. unsigned short link)
  2784. {
  2785. unsigned int n = first;
  2786. int row_left = l->left, row_right = l->right;
  2787. int avail = row_right - row_left;
  2788. int x = row_left, y = l->y, row_h = 0, any = 0;
  2789. flush_line(l);
  2790. y = l->y;
  2791. while (n) {
  2792. struct br_node *nd = &doc.nodes[n];
  2793. struct br_style cs;
  2794. unsigned short lk = link;
  2795. int colw, h;
  2796. if (nd->type == BR_NODE_TEXT) {
  2797. if (!text_node_blank(nd))
  2798. break;
  2799. if (any)
  2800. x += 4;
  2801. n = nd->next;
  2802. continue;
  2803. }
  2804. br_style_for(&doc, n, pst, &cs);
  2805. if (cs.display == BR_DISPLAY_NONE) {
  2806. n = nd->next;
  2807. continue;
  2808. }
  2809. if (cs.display != BR_DISPLAY_INLINE_BLOCK)
  2810. break;
  2811. if (nd->tag == BR_TAG_A && nd->href_off)
  2812. lk = (unsigned short)n;
  2813. colw = flex_item_width(l, n, &cs, avail);
  2814. /* A few pixels of slack keep a single long token (boot.S)
  2815. * from wrapping at the exact content width. */
  2816. colw += 8;
  2817. if (colw > avail)
  2818. colw = avail;
  2819. if (any && x + colw > row_right) {
  2820. y += row_h + 2;
  2821. x = row_left;
  2822. row_h = 0;
  2823. any = 0;
  2824. }
  2825. if (any && x > row_left)
  2826. x += 4;
  2827. l->left = x;
  2828. l->right = x + colw;
  2829. if (l->right < l->left + 8)
  2830. l->right = l->left + 8;
  2831. l->y = y;
  2832. l->line_x = l->left;
  2833. l->run_x = l->left;
  2834. l->line_y = l->y;
  2835. l->line_h = 0;
  2836. l->nruns = 0;
  2837. l->space_pending = 0;
  2838. l->align = cs.align;
  2839. layout_block(l, n, &cs, depth + 1, lk);
  2840. h = l->y - y;
  2841. if (h > row_h)
  2842. row_h = h;
  2843. x += colw;
  2844. any = 1;
  2845. n = nd->next;
  2846. }
  2847. if (any)
  2848. y += row_h;
  2849. l->left = row_left;
  2850. l->right = row_right;
  2851. l->y = y;
  2852. l->line_x = l->left;
  2853. l->run_x = l->left;
  2854. l->line_y = l->y;
  2855. l->line_h = 0;
  2856. l->nruns = 0;
  2857. l->space_pending = 0;
  2858. return n;
  2859. }
  2860. static void layout_flex_children(struct lay *l, unsigned int parent,
  2861. const struct br_style *pst, int depth,
  2862. unsigned short link)
  2863. {
  2864. unsigned int n, c;
  2865. int row_left = l->left, row_right = l->right;
  2866. int avail = row_right - row_left;
  2867. int grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap;
  2868. int x = row_left, row_top = l->y, row_bottom = l->y, any = 0;
  2869. if (avail < 160 || depth > 24) {
  2870. layout_children(l, parent, pst, depth, link);
  2871. return;
  2872. }
  2873. /* If direct text is not just whitespace, normal block flow handles it
  2874. * better than pretending it is a flex item. */
  2875. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  2876. struct br_node *nd = &doc.nodes[c];
  2877. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  2878. layout_children(l, parent, pst, depth, link);
  2879. return;
  2880. }
  2881. }
  2882. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  2883. struct br_node *nd = &doc.nodes[c];
  2884. struct br_style cs;
  2885. if (nd->type == BR_NODE_TEXT)
  2886. continue;
  2887. br_style_for(&doc, c, pst, &cs);
  2888. if (cs.display == BR_DISPLAY_NONE)
  2889. continue;
  2890. if (cs.flex_grow && !cs.width_kind)
  2891. grow_count++;
  2892. else
  2893. fixed += flex_item_width(l, c, &cs, avail);
  2894. }
  2895. grow_w = grow_count ? (avail - fixed) / grow_count : 0;
  2896. if (grow_w < 96)
  2897. grow_w = 96;
  2898. n = doc.nodes[parent].first;
  2899. while (n) {
  2900. struct br_node *nd = &doc.nodes[n];
  2901. struct br_style cs;
  2902. unsigned short lk = link;
  2903. int colw;
  2904. if (nd->type == BR_NODE_TEXT) {
  2905. n = nd->next;
  2906. continue;
  2907. }
  2908. br_style_for(&doc, n, pst, &cs);
  2909. if (cs.display == BR_DISPLAY_NONE) {
  2910. n = nd->next;
  2911. continue;
  2912. }
  2913. if (nd->tag == BR_TAG_A && nd->href_off)
  2914. lk = (unsigned short)n;
  2915. if (cs.flex_grow && !cs.width_kind)
  2916. colw = grow_w;
  2917. else
  2918. colw = flex_item_width(l, n, &cs, avail);
  2919. if (colw > row_right - row_left)
  2920. colw = row_right - row_left;
  2921. if (any && x + colw > row_right) {
  2922. if (pst->flex_wrap == 2) {
  2923. if (colw > row_right - x)
  2924. colw = row_right - x;
  2925. } else {
  2926. row_top = row_bottom;
  2927. row_bottom = row_top;
  2928. x = row_left;
  2929. }
  2930. }
  2931. if (any && gap && x > row_left)
  2932. x += gap;
  2933. l->left = x;
  2934. l->right = x + colw;
  2935. if (l->right < l->left + 8)
  2936. l->right = l->left + 8;
  2937. l->y = row_top;
  2938. l->line_x = l->left;
  2939. l->run_x = l->left;
  2940. l->line_y = l->y;
  2941. l->line_h = 0;
  2942. l->nruns = 0;
  2943. l->space_pending = 0;
  2944. l->align = cs.align;
  2945. layout_block(l, n, &cs, depth + 1, lk);
  2946. if (l->y > row_bottom)
  2947. row_bottom = l->y;
  2948. x += colw;
  2949. any++;
  2950. n = nd->next;
  2951. }
  2952. l->left = row_left;
  2953. l->right = row_right;
  2954. l->y = row_bottom;
  2955. l->line_x = l->left;
  2956. l->run_x = l->left;
  2957. l->line_y = l->y;
  2958. l->line_h = 0;
  2959. l->nruns = 0;
  2960. l->space_pending = 0;
  2961. }
  2962. /* Two-column CSS grid: track sizes from grid-template-columns (px, %, fr)
  2963. * plus the grid-area hints used by site shells (site notice/footer full
  2964. * width, column-start left, page-content right). Anything more elaborate
  2965. * falls back to normal block flow. */
  2966. static int grid_track_width(unsigned char kind, unsigned short val,
  2967. int avail, int other)
  2968. {
  2969. if (kind == 2)
  2970. return avail * (int)val / 100;
  2971. if (kind == 1)
  2972. return val;
  2973. return other;
  2974. }
  2975. static void layout_grid_children(struct lay *l, unsigned int parent,
  2976. const struct br_style *pst, int depth,
  2977. unsigned short link)
  2978. {
  2979. unsigned int n;
  2980. int row_left = l->left, row_right = l->right;
  2981. int avail = row_right - row_left;
  2982. int gap = pst->grid_gap;
  2983. int track_total = avail - gap;
  2984. int col0, col1, x0, x1;
  2985. int row_y, row_bottom, have_left = 0, have_right = 0;
  2986. unsigned int c;
  2987. if (avail < 32 || depth > 24)
  2988. return;
  2989. if (track_total < 16)
  2990. track_total = 16;
  2991. col0 = grid_track_width((unsigned char)(pst->width_kind),
  2992. pst->width, track_total, 0);
  2993. col1 = grid_track_width((unsigned char)(pst->max_width_kind),
  2994. pst->max_width, track_total, 0);
  2995. if (pst->width_kind == 3 && pst->max_width_kind == 3) {
  2996. int w0 = pst->width ? pst->width : 1;
  2997. int w1 = pst->max_width ? pst->max_width : 1;
  2998. col0 = track_total * w0 / (w0 + w1);
  2999. col1 = track_total - col0;
  3000. } else if (pst->width_kind == 3) {
  3001. col0 = track_total - col1;
  3002. if (col0 < 24)
  3003. col0 = 24;
  3004. } else if (pst->max_width_kind == 3) {
  3005. col1 = track_total - col0;
  3006. if (col1 < 24)
  3007. col1 = 24;
  3008. }
  3009. if (col0 < 24)
  3010. col0 = 24;
  3011. if (col1 < 24)
  3012. col1 = 24;
  3013. if (col0 + col1 > track_total) {
  3014. col1 = track_total - col0;
  3015. if (col1 < 24)
  3016. col1 = 24;
  3017. }
  3018. x0 = row_left;
  3019. x1 = row_left + col0 + gap;
  3020. /* Direct text makes a grid ambiguous; use block flow instead. */
  3021. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  3022. struct br_node *nd = &doc.nodes[c];
  3023. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  3024. layout_children(l, parent, pst, depth, link);
  3025. return;
  3026. }
  3027. }
  3028. row_y = row_bottom = l->y;
  3029. n = doc.nodes[parent].first;
  3030. while (n) {
  3031. struct br_node *nd = &doc.nodes[n];
  3032. struct br_style cs;
  3033. unsigned short lk = link;
  3034. int slot, x, w;
  3035. if (nd->type == BR_NODE_TEXT) {
  3036. n = nd->next;
  3037. continue;
  3038. }
  3039. br_style_for(&doc, n, pst, &cs);
  3040. if (cs.display == BR_DISPLAY_NONE) {
  3041. n = nd->next;
  3042. continue;
  3043. }
  3044. if (nd->tag == BR_TAG_A && nd->href_off)
  3045. lk = (unsigned short)n;
  3046. slot = cs.grid_slot;
  3047. if (!slot) {
  3048. if (!have_left)
  3049. slot = 2;
  3050. else if (!have_right)
  3051. slot = 3;
  3052. else
  3053. slot = 2;
  3054. }
  3055. if (slot == 1) {
  3056. if (have_left || have_right) {
  3057. row_y = row_bottom;
  3058. have_left = have_right = 0;
  3059. }
  3060. x = row_left;
  3061. w = avail;
  3062. } else if (slot == 2) {
  3063. if (have_left) {
  3064. row_y = row_bottom;
  3065. have_left = have_right = 0;
  3066. }
  3067. x = x0;
  3068. w = col0;
  3069. have_left = 1;
  3070. } else {
  3071. if (have_right) {
  3072. row_y = row_bottom;
  3073. have_left = have_right = 0;
  3074. }
  3075. x = x1;
  3076. w = col1;
  3077. have_right = 1;
  3078. }
  3079. l->left = x;
  3080. l->right = x + w;
  3081. if (l->right < l->left + 8)
  3082. l->right = l->left + 8;
  3083. l->y = row_y;
  3084. l->line_x = l->left;
  3085. l->run_x = l->left;
  3086. l->line_y = l->y;
  3087. l->line_h = 0;
  3088. l->nruns = 0;
  3089. l->space_pending = 0;
  3090. l->align = cs.align;
  3091. layout_block(l, n, &cs, depth + 1, lk);
  3092. if (l->y > row_bottom)
  3093. row_bottom = l->y;
  3094. n = nd->next;
  3095. }
  3096. l->left = row_left;
  3097. l->right = row_right;
  3098. l->y = row_bottom;
  3099. l->line_x = l->left;
  3100. l->run_x = l->left;
  3101. l->line_y = l->y;
  3102. l->line_h = 0;
  3103. l->nruns = 0;
  3104. l->space_pending = 0;
  3105. }
  3106. /* repeat(auto-fit, minmax(<min>, 1fr)): pick as many equal columns as fit,
  3107. * capped at four. Used by the project docs' card grids. */
  3108. static void layout_grid_auto(struct lay *l, unsigned int parent,
  3109. const struct br_style *pst, int depth,
  3110. unsigned short link)
  3111. {
  3112. unsigned int n, c;
  3113. int row_left = l->left, row_right = l->right;
  3114. int avail = row_right - row_left;
  3115. int gap = pst->grid_gap;
  3116. int minw = pst->width_kind == 1 ? pst->width : 200;
  3117. int ncols, colw, x, y = l->y, start_y = l->y, row_h = 0, col = 0;
  3118. if (avail < 32 || depth > 24) {
  3119. layout_children(l, parent, pst, depth, link);
  3120. return;
  3121. }
  3122. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  3123. struct br_node *nd = &doc.nodes[c];
  3124. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  3125. layout_children(l, parent, pst, depth, link);
  3126. return;
  3127. }
  3128. }
  3129. if (minw < 80)
  3130. minw = 80;
  3131. ncols = (avail + gap) / (minw + gap);
  3132. if (ncols < 1)
  3133. ncols = 1;
  3134. if (ncols > 4)
  3135. ncols = 4;
  3136. colw = (avail - gap * (ncols - 1)) / ncols;
  3137. if (colw < 24)
  3138. colw = 24;
  3139. x = row_left;
  3140. n = doc.nodes[parent].first;
  3141. while (n) {
  3142. struct br_node *nd = &doc.nodes[n];
  3143. struct br_style cs;
  3144. unsigned short lk = link;
  3145. int h;
  3146. if (nd->type == BR_NODE_TEXT) {
  3147. n = nd->next;
  3148. continue;
  3149. }
  3150. br_style_for(&doc, n, pst, &cs);
  3151. if (cs.display == BR_DISPLAY_NONE) {
  3152. n = nd->next;
  3153. continue;
  3154. }
  3155. if (nd->tag == BR_TAG_A && nd->href_off)
  3156. lk = (unsigned short)n;
  3157. l->left = x;
  3158. l->right = x + colw;
  3159. if (l->right < l->left + 8)
  3160. l->right = l->left + 8;
  3161. l->y = y;
  3162. l->line_x = l->left;
  3163. l->run_x = l->left;
  3164. l->line_y = l->y;
  3165. l->line_h = 0;
  3166. l->nruns = 0;
  3167. l->space_pending = 0;
  3168. l->align = cs.align;
  3169. layout_block(l, n, &cs, depth + 1, lk);
  3170. h = l->y - y;
  3171. if (h > row_h)
  3172. row_h = h;
  3173. col++;
  3174. n = nd->next;
  3175. if (col == ncols) {
  3176. y += row_h + gap;
  3177. x = row_left;
  3178. row_h = 0;
  3179. col = 0;
  3180. } else {
  3181. x += colw + gap;
  3182. }
  3183. }
  3184. if (col)
  3185. y += row_h;
  3186. else if (y > start_y)
  3187. y -= gap;
  3188. l->left = row_left;
  3189. l->right = row_right;
  3190. l->y = y;
  3191. l->line_x = l->left;
  3192. l->run_x = l->left;
  3193. l->line_y = l->y;
  3194. l->line_h = 0;
  3195. l->nruns = 0;
  3196. l->space_pending = 0;
  3197. }
  3198. /* Column widths are the widest cell in each column across every row, so all
  3199. * rows line up. The table model is still deliberately basic: no colspan,
  3200. * border-collapse or percentage columns. */
  3201. static void table_columns(struct lay *l, unsigned int table,
  3202. const struct br_style *pst, int *raw, int maxcols,
  3203. int *out_cols, int *out_total)
  3204. {
  3205. unsigned int r, c;
  3206. int cols = 0, total = 0;
  3207. for (c = 0; c < (unsigned int)maxcols; c++)
  3208. raw[c] = 0;
  3209. for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
  3210. struct br_node *rn = &doc.nodes[r];
  3211. int col = 0;
  3212. if (rn->type != BR_NODE_ELEM || rn->tag != BR_TAG_TR)
  3213. continue;
  3214. for (c = rn->first; c; c = doc.nodes[c].next) {
  3215. struct br_node *cn = &doc.nodes[c];
  3216. struct br_style cs;
  3217. int w;
  3218. if (cn->type != BR_NODE_ELEM ||
  3219. (cn->tag != BR_TAG_TD && cn->tag != BR_TAG_TH))
  3220. continue;
  3221. if (col >= maxcols)
  3222. break;
  3223. br_style_for(&doc, c, pst, &cs);
  3224. if (cs.display == BR_DISPLAY_NONE)
  3225. continue;
  3226. w = intrinsic_width(l->ui, c, &cs, 0) + cs.pl + cs.pr +
  3227. cs.ml + cs.mr;
  3228. if (w < 24)
  3229. w = 24;
  3230. if (w > raw[col])
  3231. raw[col] = w;
  3232. if (col + 1 > cols)
  3233. cols = col + 1;
  3234. col++;
  3235. }
  3236. }
  3237. for (c = 0; c < (unsigned int)cols; c++)
  3238. total += raw[c];
  3239. *out_cols = cols;
  3240. *out_total = total;
  3241. }
  3242. static void layout_table_row(struct lay *l, unsigned int row,
  3243. const struct br_style *pst, int depth,
  3244. unsigned short link)
  3245. {
  3246. unsigned int n, cell[12];
  3247. int raw[12], ncell = 0, total = 0, cols = 0;
  3248. int row_left = l->left, row_right = l->right;
  3249. int avail = row_right - row_left;
  3250. int x = row_left, y = l->y, bottom = l->y;
  3251. for (n = doc.nodes[row].first; n; n = doc.nodes[n].next) {
  3252. struct br_node *nd = &doc.nodes[n];
  3253. struct br_style cs;
  3254. if (nd->type == BR_NODE_TEXT) {
  3255. if (text_node_blank(nd))
  3256. continue;
  3257. layout_children(l, row, pst, depth, link);
  3258. return;
  3259. }
  3260. if (nd->tag != BR_TAG_TD && nd->tag != BR_TAG_TH) {
  3261. layout_children(l, row, pst, depth, link);
  3262. return;
  3263. }
  3264. br_style_for(&doc, n, pst, &cs);
  3265. if (cs.display == BR_DISPLAY_NONE)
  3266. continue;
  3267. if (ncell >= 12) {
  3268. layout_children(l, row, pst, depth, link);
  3269. return;
  3270. }
  3271. cell[ncell] = n;
  3272. ncell++;
  3273. }
  3274. if (ncell < 2 || avail < 32) {
  3275. layout_children(l, row, pst, depth, link);
  3276. return;
  3277. }
  3278. table_columns(l, doc.nodes[row].parent, pst, raw, 12, &cols, &total);
  3279. if (cols < 2 || total <= 0) {
  3280. int c;
  3281. for (c = 0; c < ncell; c++) {
  3282. struct br_style cs;
  3283. br_style_for(&doc, cell[c], pst, &cs);
  3284. raw[c] = intrinsic_width(l->ui, cell[c], &cs, 0) +
  3285. cs.pl + cs.pr + cs.ml + cs.mr;
  3286. if (raw[c] < 24)
  3287. raw[c] = 24;
  3288. total += raw[c];
  3289. }
  3290. }
  3291. for (n = 0; n < (unsigned int)ncell; n++) {
  3292. struct br_style cs;
  3293. int w = (int)((long)raw[n] * avail / total);
  3294. br_style_for(&doc, cell[n], pst, &cs);
  3295. if (w < 24)
  3296. w = 24;
  3297. if (w < 8)
  3298. w = 8;
  3299. l->left = x;
  3300. l->right = x + w;
  3301. l->y = y;
  3302. l->line_x = l->left;
  3303. l->run_x = l->left;
  3304. l->line_y = l->y;
  3305. l->line_h = 0;
  3306. l->nruns = 0;
  3307. l->space_pending = 0;
  3308. l->align = cs.align;
  3309. layout_block(l, cell[n], &cs, depth + 1, link);
  3310. if (l->y > bottom)
  3311. bottom = l->y;
  3312. x += w;
  3313. }
  3314. l->left = row_left;
  3315. l->right = row_right;
  3316. l->y = bottom;
  3317. l->line_x = l->left;
  3318. l->run_x = l->left;
  3319. l->line_y = l->y;
  3320. l->line_h = 0;
  3321. l->nruns = 0;
  3322. l->space_pending = 0;
  3323. }
  3324. /* Positioned elements (fixed headers, overlays) are drawn after the normal
  3325. * flow so they sit above it, like CSS painting order. Their flow origin is
  3326. * saved for absolute boxes; fixed boxes only need the viewport rect. */
  3327. struct br_deferred {
  3328. unsigned int node;
  3329. int flow_y, flow_left, flow_right;
  3330. unsigned short link;
  3331. };
  3332. static struct br_deferred deferred[16];
  3333. static int ndeferred;
  3334. static void layout_children(struct lay *l, unsigned int parent,
  3335. const struct br_style *pst, int depth,
  3336. unsigned short link)
  3337. {
  3338. unsigned int n;
  3339. if (depth > 24)
  3340. return;
  3341. n = doc.nodes[parent].first;
  3342. while (n) {
  3343. struct br_node *nd = &doc.nodes[n];
  3344. struct br_style cs;
  3345. unsigned short lk = link;
  3346. if (nd->type == BR_NODE_TEXT) {
  3347. emit_text(l, doc.text + nd->text_off, nd->text_len, pst,
  3348. link, (nd->flags & BR_NODE_PREFORMAT) != 0);
  3349. n = nd->next;
  3350. continue;
  3351. }
  3352. br_style_for(&doc, n, pst, &cs);
  3353. if (cs.display == BR_DISPLAY_NONE) {
  3354. n = nd->next;
  3355. continue;
  3356. }
  3357. if (nd->tag == BR_TAG_A && nd->href_off)
  3358. lk = (unsigned short)n;
  3359. if (cs.pos) {
  3360. if (l->draw && ndeferred < 16) {
  3361. deferred[ndeferred].node = n;
  3362. deferred[ndeferred].flow_y = l->y;
  3363. deferred[ndeferred].flow_left = l->left;
  3364. deferred[ndeferred].flow_right = l->right;
  3365. deferred[ndeferred].link = lk;
  3366. ndeferred++;
  3367. } else {
  3368. layout_block(l, n, &cs, depth + 1, lk);
  3369. }
  3370. if (cs.pos == 1 && block_h[n] &&
  3371. doc.nodes[n].class_off) {
  3372. const char *cl = doc.attrs +
  3373. doc.nodes[n].class_off;
  3374. if ((contains_ci(cl, "poster-wrapper") ||
  3375. contains_ci(cl, "ts-picture")) &&
  3376. !has_skeleton_sibling(n))
  3377. l->y += block_h[n];
  3378. }
  3379. n = nd->next;
  3380. continue;
  3381. }
  3382. if (nd->tag == BR_TAG_BR) {
  3383. flush_line(l);
  3384. n = nd->next;
  3385. continue;
  3386. }
  3387. if (nd->tag == BR_TAG_HR) {
  3388. flush_line(l);
  3389. if (l->draw)
  3390. ngui_fill_rect(l->ui,
  3391. (struct ngui_rect){l->left, l->y + 2,
  3392. l->right - l->left, 1}, BR_GRAY);
  3393. l->y += 8;
  3394. n = nd->next;
  3395. continue;
  3396. }
  3397. if (nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_TEXTAREA ||
  3398. nd->tag == BR_TAG_SELECT) {
  3399. if (!(nd->tag == BR_TAG_INPUT &&
  3400. s_eq(br_type(&doc, n), "hidden")))
  3401. add_form_run(l, n, &cs, lk);
  3402. n = nd->next;
  3403. continue;
  3404. }
  3405. if (nd->tag == BR_TAG_IMG) {
  3406. add_image_run(l, n, &cs, lk);
  3407. n = nd->next;
  3408. continue;
  3409. }
  3410. if (nd->tag == BR_TAG_VIDEO) {
  3411. layout_block(l, n, &cs, depth + 1, lk);
  3412. n = nd->next;
  3413. continue;
  3414. }
  3415. if (cs.float_left) {
  3416. n = layout_float_group(l, n, pst, depth, lk);
  3417. continue;
  3418. }
  3419. if (cs.display == BR_DISPLAY_INLINE_BLOCK) {
  3420. n = layout_inline_blocks(l, n, pst, depth, link);
  3421. continue;
  3422. }
  3423. if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag))
  3424. layout_block(l, n, &cs, depth + 1, lk);
  3425. else {
  3426. layout_children(l, n, &cs, depth + 1, lk);
  3427. if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
  3428. l->space_pending = 1;
  3429. }
  3430. n = nd->next;
  3431. }
  3432. }
  3433. /* One layout state, kept out of the 8 KB user stack: it holds 96 line
  3434. * runs and a recursive page can otherwise overflow the stack. */
  3435. static struct lay layout_state;
  3436. static int measure_page(int left, int right)
  3437. {
  3438. struct lay *l = &layout_state;
  3439. struct br_style base = base_style();
  3440. unsigned int i;
  3441. for (i = 0; i < BR_MAX_NODES; i++) {
  3442. block_h[i] = 0;
  3443. img_top[i] = 0;
  3444. img_h[i] = 0;
  3445. }
  3446. l->draw = 0;
  3447. l->ui = 0;
  3448. l->left = left;
  3449. l->right = right;
  3450. l->y = 0;
  3451. l->line_x = left;
  3452. l->run_x = left;
  3453. l->line_y = 0;
  3454. l->line_h = 0;
  3455. l->align = BR_ALIGN_LEFT;
  3456. l->depth = 0;
  3457. l->nruns = 0;
  3458. l->space_pending = 0;
  3459. layout_children(l, doc.root, &base, 0, 0);
  3460. return l->y + 8;
  3461. }
  3462. static void render_page(int left, int right, int y0)
  3463. {
  3464. struct lay *l = &layout_state;
  3465. struct br_style base = base_style();
  3466. l->draw = 1;
  3467. l->ui = &ui;
  3468. l->left = left;
  3469. l->right = right;
  3470. l->y = y0;
  3471. l->line_x = left;
  3472. l->run_x = left;
  3473. l->line_y = y0;
  3474. l->line_h = 0;
  3475. l->align = BR_ALIGN_LEFT;
  3476. l->depth = 0;
  3477. l->nruns = 0;
  3478. l->space_pending = 0;
  3479. nhits = 0;
  3480. ndeferred = 0;
  3481. layout_children(l, doc.root, &base, 0, 0);
  3482. {
  3483. int i;
  3484. for (i = 0; i < ndeferred; i++) {
  3485. struct br_style st;
  3486. br_style_for(&doc, deferred[i].node, &base, &st);
  3487. if (st.display == BR_DISPLAY_NONE)
  3488. continue;
  3489. l->draw = 1;
  3490. l->left = deferred[i].flow_left;
  3491. l->right = deferred[i].flow_right;
  3492. l->y = deferred[i].flow_y;
  3493. l->line_x = l->left;
  3494. l->run_x = l->left;
  3495. l->line_y = l->y;
  3496. l->line_h = 0;
  3497. l->nruns = 0;
  3498. l->space_pending = 0;
  3499. l->align = st.align;
  3500. if (st.pos == 2 && block_h[deferred[i].node]) {
  3501. struct ngui_rect clip;
  3502. int ch = block_h[deferred[i].node] + 24;
  3503. clip.x = page_rect.x;
  3504. clip.y = page_rect.y;
  3505. clip.w = page_rect.w - 12;
  3506. if (ch > page_rect.h)
  3507. ch = page_rect.h;
  3508. clip.h = ch;
  3509. ngui_push_clip(&ui, clip);
  3510. if (st.has_bg)
  3511. ngui_fill_rect(&ui, clip, st.bg);
  3512. layout_block(l, deferred[i].node, &st, 0,
  3513. deferred[i].link);
  3514. ngui_pop_clip(&ui);
  3515. } else {
  3516. layout_block(l, deferred[i].node, &st, 0,
  3517. deferred[i].link);
  3518. }
  3519. }
  3520. }
  3521. }
  3522. /* ---- UI ---- */
  3523. static int find_hit(int x, int y)
  3524. {
  3525. int i;
  3526. for (i = nhits - 1; i >= 0; i--) {
  3527. struct hit *h = &hits[i];
  3528. if (x >= h->x && y >= h->y && x < h->x + h->w &&
  3529. y < h->y + h->h)
  3530. return (int)h->node;
  3531. }
  3532. return 0;
  3533. }
  3534. static void draw_toolbar(struct ngui_rect r, unsigned int *action)
  3535. {
  3536. int x = r.x + 4, y = r.y + 4, h = 22;
  3537. int compact = r.w < 560;
  3538. int go_w = r.w < 420 ? 32 : 42;
  3539. int in_x, in_w, res;
  3540. ngui_fill_rect(&ui, r, ui.theme.panel);
  3541. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  3542. ui.theme.border);
  3543. if (ngui_icon_button(&ui, ID_BACK, (struct ngui_rect){x, y, 28, h},
  3544. NGUI_ICON_CHEVRON_LEFT))
  3545. *action |= ACT_BACK;
  3546. x += 32;
  3547. if (!compact) {
  3548. if (ngui_icon_button(&ui, ID_FORWARD,
  3549. (struct ngui_rect){x, y, 28, h},
  3550. NGUI_ICON_CHEVRON_RIGHT))
  3551. *action |= ACT_FORWARD;
  3552. x += 32;
  3553. }
  3554. if (ngui_icon_button(&ui, ID_RELOAD, (struct ngui_rect){x, y, 28, h},
  3555. NGUI_ICON_REFRESH))
  3556. *action |= ACT_RELOAD;
  3557. x += 32;
  3558. if (!compact) {
  3559. if (ngui_icon_button(&ui, ID_HOME,
  3560. (struct ngui_rect){x, y, 28, h},
  3561. NGUI_ICON_HOME))
  3562. *action |= ACT_HOME;
  3563. x += 32;
  3564. }
  3565. in_x = x + 2;
  3566. in_w = r.x + r.w - 6 - go_w - 4 - in_x;
  3567. if (in_w < 60)
  3568. in_w = 60;
  3569. res = ngui_text_input(&ui, ID_URL,
  3570. (struct ngui_rect){in_x, y, in_w, h}, &url_in);
  3571. if (res & NGUI_INPUT_SUBMIT)
  3572. *action |= ACT_NAV;
  3573. if (res & NGUI_INPUT_CANCEL)
  3574. ngui_set_focus(&ui, 0);
  3575. if (ngui_button(&ui, ID_GO,
  3576. (struct ngui_rect){r.x + r.w - 4 - go_w, y, go_w, h},
  3577. "Go"))
  3578. *action |= ACT_NAV;
  3579. }
  3580. static int page_hover_node(void)
  3581. {
  3582. if (!ui.inside)
  3583. return 0;
  3584. if (ui.mouse_x < page_rect.x ||
  3585. ui.mouse_x >= page_rect.x + page_rect.w - 12 ||
  3586. ui.mouse_y < page_rect.y ||
  3587. ui.mouse_y >= page_rect.y + page_rect.h)
  3588. return 0;
  3589. return find_hit(ui.mouse_x, ui.mouse_y);
  3590. }
  3591. static void blit_page(struct ngui_rect page, unsigned short *src, int sw)
  3592. {
  3593. int x, y, w = page.w, h = page.h;
  3594. if (!src || w <= 0 || h <= 0)
  3595. return;
  3596. for (y = 0; y < h; y++) {
  3597. unsigned short *s = src + (page.y + y) * sw + page.x;
  3598. unsigned short *d = ui.px + (page.y + y) * ui.w + page.x;
  3599. for (x = 0; x + 1 < w; x += 2) {
  3600. *(unsigned int *)(d + x) = *(unsigned int *)(s + x);
  3601. }
  3602. if (x < w)
  3603. d[x] = s[x];
  3604. }
  3605. }
  3606. static void draw_page(struct ngui_rect page)
  3607. {
  3608. int left, right, ch;
  3609. int old_panel, old_dim;
  3610. unsigned short *saved_px;
  3611. int saved_w, saved_h, use_buf;
  3612. page_rect = page;
  3613. if (page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h &&
  3614. page_cache_valid && layout_valid &&
  3615. !ui.pressed && !(ui.buttons & 1) && !ui.released &&
  3616. page_scroll.offset == page_cache_scroll &&
  3617. page_cache_nnode == doc.nnode &&
  3618. page_cache_x == page.x && page_cache_y == page.y &&
  3619. page_cache_w == page.w && page_cache_h == page.h) {
  3620. blit_page(page, page_backbuf, pagebuf_w);
  3621. return;
  3622. }
  3623. ngui_fill_rect(&ui, page, ui.theme.bg);
  3624. if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
  3625. const char *s = page_loading ? "Loading..." : "No page";
  3626. int w = (int)ngui_text_width_ui(&ui, s);
  3627. ngui_text(&ui, page.x + (page.w - w) / 2,
  3628. page.y + page.h / 2 - 8, s, ui.theme.text_dim);
  3629. page_scroll.content_h = 0;
  3630. page_scroll.view_h = page.h;
  3631. page_cache_valid = 0;
  3632. return;
  3633. }
  3634. if (page.w < 40 || page.h < 40) {
  3635. page_cache_valid = 0;
  3636. return;
  3637. }
  3638. saved_px = ui.px;
  3639. saved_w = ui.w;
  3640. saved_h = ui.h;
  3641. use_buf = page_backbuf && pagebuf_w == ui.w && pagebuf_h == ui.h;
  3642. if (use_buf) {
  3643. ui.px = page_backbuf;
  3644. ui.w = pagebuf_w;
  3645. ui.h = pagebuf_h;
  3646. ngui_fill_rect(&ui, page, ui.theme.bg);
  3647. }
  3648. left = page.x + 8;
  3649. right = page.x + page.w - 16;
  3650. if (!layout_valid || left != layout_left || right != layout_right ||
  3651. layout_nnode != doc.nnode) {
  3652. layout_content_h = measure_page(left, right);
  3653. layout_valid = 1;
  3654. layout_left = left;
  3655. layout_right = right;
  3656. layout_nnode = doc.nnode;
  3657. }
  3658. ch = layout_content_h;
  3659. page_scroll.content_h = ch;
  3660. page_scroll.view_h = page.h;
  3661. ngui_scroll_begin(&ui, page, &page_scroll);
  3662. render_page(left, right, page.y + 4 - page_scroll.offset);
  3663. old_panel = ui.theme.panel_alt;
  3664. old_dim = ui.theme.text_dim;
  3665. ui.theme.panel_alt = 0xC618;
  3666. ui.theme.text_dim = 0x8410;
  3667. ngui_scroll_end(&ui, page, &page_scroll);
  3668. ui.theme.panel_alt = old_panel;
  3669. ui.theme.text_dim = old_dim;
  3670. if (use_buf) {
  3671. ui.px = saved_px;
  3672. ui.w = saved_w;
  3673. ui.h = saved_h;
  3674. blit_page(page, page_backbuf, pagebuf_w);
  3675. page_cache_valid = 1;
  3676. page_cache_scroll = page_scroll.offset;
  3677. page_cache_nnode = doc.nnode;
  3678. page_cache_x = page.x;
  3679. page_cache_y = page.y;
  3680. page_cache_w = page.w;
  3681. page_cache_h = page.h;
  3682. } else {
  3683. page_cache_valid = 0;
  3684. }
  3685. }
  3686. static void draw_status(struct ngui_rect r)
  3687. {
  3688. char msg[BR_URL_MAX + 32];
  3689. unsigned int n = 0;
  3690. int hover = page_hover_node();
  3691. unsigned short color;
  3692. const char *kind;
  3693. ngui_fill_rect(&ui, r, ui.theme.panel);
  3694. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
  3695. ui.theme.border);
  3696. if (hover) {
  3697. const char *href = br_href(&doc, (unsigned int)hover);
  3698. char url[BR_URL_MAX];
  3699. if (resolve_link(page_url, href, url, sizeof(url)) == 0) {
  3700. s_append(msg, &n, "Link: ", sizeof(msg));
  3701. s_append(msg, &n, url, sizeof(msg));
  3702. } else {
  3703. s_copy(msg, "Link", sizeof(msg));
  3704. }
  3705. color = ui.theme.accent;
  3706. } else if (status_text[0]) {
  3707. s_copy(msg, status_text, sizeof(msg));
  3708. color = status_error ? ui.theme.error : ui.theme.text;
  3709. } else if (page_title[0]) {
  3710. s_copy(msg, page_title, sizeof(msg));
  3711. color = ui.theme.text;
  3712. } else {
  3713. s_copy(msg, "Ready", sizeof(msg));
  3714. color = ui.theme.text_dim;
  3715. }
  3716. kind = s_eq(page_url, "about:home") ? "HOME" :
  3717. page_is_local ? "FILE" :
  3718. (starts_with(page_url, "https://") ? "HTTPS" : "HTTP");
  3719. ngui_text_ellipsis(&ui, r, r.x + 6, r.y + 1, msg, color);
  3720. {
  3721. int kw = (int)ngui_text_width_ui(&ui, kind);
  3722. ngui_text(&ui, r.x + r.w - 6 - kw, r.y + 1, kind,
  3723. ui.theme.text_dim);
  3724. }
  3725. }
  3726. static void draw(unsigned int *action)
  3727. {
  3728. struct ngui_rect r = {0, 0, ui.w, ui.h};
  3729. struct ngui_rect toolbar, status, page;
  3730. toolbar = ngui_cut_top(&r, BR_TOOLBAR_H);
  3731. status = ngui_cut_bottom(&r, BR_STATUS_H);
  3732. page = r;
  3733. draw_toolbar(toolbar, action);
  3734. draw_page(page);
  3735. draw_status(status);
  3736. }
  3737. static void redraw_now(void)
  3738. {
  3739. unsigned int action = 0;
  3740. ngui_begin(&ui);
  3741. draw(&action);
  3742. ngui_end(&ui);
  3743. }
  3744. static void handle_keys(unsigned int *action)
  3745. {
  3746. int k, scrolled = 0;
  3747. /* Batch queued page keys: a slow full-page render must not make a
  3748. * held Page Down advance only once every few frames. */
  3749. while (scrolled < 3 && ngui_scroll_keys(&ui, &page_scroll))
  3750. scrolled++;
  3751. if (scrolled)
  3752. ngui_invalidate(&ui);
  3753. if (ngui_focus(&ui))
  3754. return;
  3755. while ((k = ngui_key(&ui)) != 0) {
  3756. switch (k) {
  3757. case NGUI_KEY_ESC:
  3758. *action |= ACT_CLOSE;
  3759. break;
  3760. case NGUI_KEY_TAB:
  3761. case 0x0C: /* Ctrl+L */
  3762. ngui_set_focus(&ui, ID_URL);
  3763. break;
  3764. case 0x12: /* Ctrl+R */
  3765. *action |= ACT_RELOAD;
  3766. break;
  3767. case NGUI_KEY_BACKSPACE:
  3768. *action |= ACT_BACK;
  3769. break;
  3770. case NGUI_KEY_UP:
  3771. ngui_scroll_to(&page_scroll,
  3772. page_scroll.offset - 24);
  3773. ngui_invalidate(&ui);
  3774. break;
  3775. case NGUI_KEY_DOWN:
  3776. ngui_scroll_to(&page_scroll,
  3777. page_scroll.offset + 24);
  3778. ngui_invalidate(&ui);
  3779. break;
  3780. case NGUI_KEY_HOME:
  3781. ngui_scroll_to(&page_scroll, 0);
  3782. ngui_invalidate(&ui);
  3783. break;
  3784. case NGUI_KEY_END:
  3785. ngui_scroll_to(&page_scroll,
  3786. page_scroll.content_h -
  3787. page_scroll.view_h);
  3788. ngui_invalidate(&ui);
  3789. break;
  3790. default:
  3791. break;
  3792. }
  3793. }
  3794. }
  3795. static void handle_page_click(unsigned int *action)
  3796. {
  3797. int node;
  3798. char url[BR_URL_MAX];
  3799. if (!ui.pressed)
  3800. return;
  3801. if (ui.press_x < page_rect.x ||
  3802. ui.press_x >= page_rect.x + page_rect.w - 12 ||
  3803. ui.press_y < page_rect.y ||
  3804. ui.press_y >= page_rect.y + page_rect.h)
  3805. return;
  3806. node = find_hit(ui.press_x, ui.press_y);
  3807. if (!node)
  3808. return;
  3809. if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
  3810. sizeof(url)) != 0)
  3811. return;
  3812. s_copy(click_url, url, sizeof(click_url));
  3813. *action |= ACT_NAV;
  3814. }
  3815. void _start(const char *arg)
  3816. {
  3817. struct win_info wi;
  3818. if (u_win_create(BR_W, BR_H, "Browser", &wi) != 0 &&
  3819. u_win_create(960, 640, "Browser", &wi) != 0 &&
  3820. u_win_create(780, 540, "Browser", &wi) != 0) {
  3821. u_puts("browser: no graphics window\n");
  3822. u_exit(1);
  3823. }
  3824. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  3825. url_in.placeholder = "http://...";
  3826. br_doc_init(&doc);
  3827. br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
  3828. ui.theme.accent);
  3829. br_set_viewport(wi.width);
  3830. if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
  3831. s_copy(cwd, "/", sizeof(cwd));
  3832. heap_clear();
  3833. page_scroll.offset = 0;
  3834. page_scroll.content_h = 0;
  3835. if (arg && arg[0])
  3836. navigate(arg, 1);
  3837. else
  3838. load_home(1);
  3839. for (;;) {
  3840. unsigned int action = 0;
  3841. unsigned int now;
  3842. click_url[0] = '\0';
  3843. if (pending_action) {
  3844. action |= pending_action & ACT_WAIT_NAV;
  3845. if (pending_url[0])
  3846. s_copy(click_url, pending_url, sizeof(click_url));
  3847. pending_action = 0;
  3848. pending_url[0] = '\0';
  3849. cancel_fetch = 0;
  3850. } else if (preload_active) {
  3851. if (preload_wait)
  3852. preload_wait = 0;
  3853. else
  3854. preload_step();
  3855. } else if (preload_pending &&
  3856. page_scroll.offset != preload_scroll) {
  3857. preload_scroll = page_scroll.offset;
  3858. preload_pending = 0;
  3859. preload_node = 0;
  3860. preload_active = 1;
  3861. }
  3862. now = u_ticks_ms();
  3863. if (status_until && (int)(now - status_until) >= 0) {
  3864. status_text[0] = '\0';
  3865. status_until = 0;
  3866. status_error = 0;
  3867. ngui_invalidate(&ui);
  3868. }
  3869. if (!ngui_begin(&ui)) {
  3870. /* A queued navigation (e.g. Enter in the URL bar during
  3871. * a slow fetch) must be handled even when this frame
  3872. * needs no redraw. */
  3873. if (!action) {
  3874. u_sleep_ms(10);
  3875. continue;
  3876. }
  3877. } else {
  3878. draw(&action);
  3879. handle_keys(&action);
  3880. handle_page_click(&action);
  3881. ngui_end(&ui);
  3882. }
  3883. if (action & ACT_NAV) {
  3884. if (click_url[0]) {
  3885. navigate(click_url, 1);
  3886. click_url[0] = '\0';
  3887. } else {
  3888. navigate(url_in.text, 1);
  3889. }
  3890. }
  3891. if (action & ACT_BACK) {
  3892. if (hist_pos > 0) {
  3893. hist_pos--;
  3894. navigate(history[hist_pos], 0);
  3895. }
  3896. }
  3897. if (action & ACT_FORWARD) {
  3898. if (hist_pos + 1 < hist_len) {
  3899. hist_pos++;
  3900. navigate(history[hist_pos], 0);
  3901. }
  3902. }
  3903. if (action & ACT_RELOAD) {
  3904. if (s_eq(page_url, "about:home"))
  3905. load_home(0);
  3906. else
  3907. navigate(page_url, 0);
  3908. }
  3909. if (action & ACT_HOME)
  3910. load_home(1);
  3911. if (action & ACT_CLOSE)
  3912. quit = 1;
  3913. if (quit)
  3914. break;
  3915. if (pending_action)
  3916. continue;
  3917. u_sleep_ms(10);
  3918. }
  3919. fetch_helper_stop();
  3920. u_win_close();
  3921. u_exit(0);
  3922. }