browser.c 96 KB

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