browser.c 99 KB

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