", sizeof(error_page)); { const char *p = detail; while (*p && n + 1 < sizeof(error_page)) { if (*p == '<') s_append(page, &n, "<", sizeof(error_page)); else if (*p == '>') s_append(page, &n, ">", sizeof(error_page)); else if (*p == '&') s_append(page, &n, "&", sizeof(error_page)); else page[n++] = *p; p++; } } page[n] = '\0'; s_append(page, &n, "
", sizeof(error_page)); s_append(page, &n, page_url, sizeof(error_page)); s_append(page, &n, "
", sizeof(error_page)); br_parse(&doc, page, n); layout_valid = 0; preload_active = 0; preload_pending = 0; preload_wait = 0; s_copy(page_title, title, sizeof(page_title)); page_loading = 0; set_status(title, 1, 0); ngui_invalidate(&ui); } static int decode_direct_image(const unsigned char *data, unsigned int len, unsigned int node) { if (image_count >= BR_MAX_IMAGES) return -1; if (decode_image_data(data, len, &images[0]) != 0) return -1; doc.nodes[node].img_index = 0; image_count = 1; return 0; } static int ancestor_is(unsigned int node, unsigned int tag) { unsigned int p = doc.nodes[node].parent; while (p) { if (doc.nodes[p].tag == tag) return 1; p = doc.nodes[p].parent; } return 0; } static int load_stylesheets(void) { static char seen[BR_MAX_SHEETS][BR_URL_MAX]; unsigned int i, sheets = 0, nseen = 0; for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) { struct br_node *nd = &doc.nodes[i]; const char *rel, *href; char url[BR_URL_MAX]; unsigned char *data; unsigned int len; char err[64]; int rv; if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK) continue; rel = br_rel(&doc, i); if (!contains_ci(rel, "stylesheet")) continue; href = br_href(&doc, i); if (!href[0]) continue; if (resolve_link(page_url, href, url, sizeof(url)) != 0) continue; { unsigned int k; int dup = 0; for (k = 0; k < nseen; k++) if (s_eq(seen[k], url)) { dup = 1; break; } if (dup) continue; if (nseen < BR_MAX_SHEETS) { s_copy(seen[nseen], url, BR_URL_MAX); nseen++; } } rv = fetch_css(url, &data, &len, err, sizeof(err)); if (rv == FETCH_ABORT) return FETCH_ABORT; if (rv != 0) continue; if (len) br_parse_css_text(&doc, (const char *)data, len); sheets++; } br_parse_inline_css(&doc); br_reset_styles(&doc); layout_valid = 0; return 0; } static void preload_begin(void) { preload_active = 1; preload_pending = 0; preload_wait = 1; preload_node = 0; preload_loaded = 0; preload_scroll = 0; } /* Styles are computed top-down during layout, but images inside a hidden * menu/carousel are never visited and keep img_top == 0. Without this check * the preloader walks the DOM in node order and spends the whole image * budget on logos and off-canvas thumbnails the reader cannot see. */ static int node_hidden(unsigned int node) { unsigned int path[32], n = 0, p = node, i; struct br_style st, parent; while (p && n < sizeof(path) / sizeof(path[0])) { path[n++] = p; p = doc.nodes[p].parent; } parent = (struct br_style){0}; parent.font_px = 16; parent.display = BR_DISPLAY_BLOCK; for (i = n; i > 0; i--) { br_style_for(&doc, path[i - 1], &parent, &st); if (st.display == BR_DISPLAY_NONE || st.vis_hidden || st.opacity_hidden) return 1; parent = st; } return 0; } /* Load at most one image per call. Images below the current viewport are * left pending until the page is scrolled; this keeps a long article with * many 4K screenshots from stalling on content the user cannot see yet. */ static void preload_step(void) { unsigned int batch = 0; while (preload_active && preload_node < doc.nnode) { unsigned int i = preload_node; struct br_node *nd = &doc.nodes[i]; const char *src; char url[BR_URL_MAX]; unsigned char *data; unsigned int len; char err[64]; char msg[64]; unsigned int n = 0; int rv; if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG || nd->img_index != -1) { preload_node++; continue; } if (ancestor_is(i, BR_TAG_NOSCRIPT) || node_hidden(i)) { preload_node++; continue; } src = br_src(&doc, i); if (!src[0] || contains_ci(src, ".svg") || starts_with(src, "data:")) { preload_node++; continue; } if (resolve_link(page_url, src, url, sizeof(url)) != 0) { preload_node++; continue; } if (image_count >= BR_MAX_IMAGES) { preload_node = doc.nnode; break; } /* Do not stop at the first below-fold image: DOM order is * not visual order, and a footer/ad image early in the DOM * used to hide every later article image from the loader. */ if (img_top[i] > (unsigned int)(page_scroll.offset + page_rect.h + 160)) { preload_pending = 1; preload_node++; continue; } preload_node = i + 1; s_append(msg, &n, "Loading images ", sizeof(msg)); s_append_dec(msg, &n, (unsigned int)preload_loaded + 1, sizeof(msg)); s_append(msg, &n, "...", sizeof(msg)); set_status(msg, 0, 0); ngui_invalidate(&ui); data = 0; len = 0; rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err, sizeof(err)); if (rv == FETCH_ABORT) { preload_active = 0; preload_pending = 0; return; } if (rv == 0 && decode_image_data(data, len, &images[image_count]) == 0) { nd->img_index = (short)image_count; image_count++; preload_loaded++; layout_valid = 0; } else { /* Remember failures so a scroll does not retry an * unreachable tracking/ad image with the full network * timeout on every pass. */ nd->img_index = -2; } ngui_invalidate(&ui); /* A full page layout after every image dominates loading on a * long page. Decode a small batch, then let the main loop draw * once for the whole batch. */ if (++batch >= 4) return; } preload_active = 0; if (preload_pending) preload_scroll = page_scroll.offset; else preload_pending = 0; set_status(post_status_text, post_status_error, post_status_ttl); ngui_invalidate(&ui); } static void history_push(const char *url) { if (!url || !url[0]) return; if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len && s_eq(history[hist_pos], url)) return; if (hist_len > 0 && hist_pos + 1 < hist_len) hist_len = hist_pos + 1; if (hist_len < BR_HISTORY) { s_copy(history[hist_len], url, BR_URL_MAX); hist_pos = hist_len; hist_len++; } else { int i; for (i = 1; i < BR_HISTORY; i++) s_copy(history[i - 1], history[i], BR_URL_MAX); s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX); hist_pos = BR_HISTORY - 1; } } static void load_page(const char *url, int local, int push) { unsigned char *body = 0; unsigned int len = 0; int truncated = 0; struct img_info image_info; char ctype[64]; char final[BR_URL_MAX]; char err[80]; const char *detail = 0; int rv; final[0] = '\0'; cancel_fetch = 0; preload_active = 0; preload_pending = 0; heap_clear(); image_reset(); layout_valid = 0; page_scroll.offset = 0; page_scroll.dragging = 0; br_parse_plain(&doc, "", 0); /* blank while loading */ page_title[0] = '\0'; page_loading = 1; page_is_local = local; s_copy(page_url, url, sizeof(page_url)); s_copy(url_in.text, page_url, NGUI_INPUT_MAX); url_in.len = s_len(url_in.text); url_in.cursor = url_in.len; url_in.scroll = 0; ngui_set_focus(&ui, 0); if (final[0]) final[0] = '\0'; if (push) history_push(url); set_status("Loading...", 0, 0); ngui_invalidate(&ui); redraw_now(); if (heap_start() != 0) { show_error("Out of memory", "Cannot allocate the page buffer"); return; } ngui_font_attach(&ui); browser_rules_alloc(); rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype, sizeof(ctype), final, sizeof(final), err, sizeof(err), &truncated); if (rv == FETCH_ABORT) return; if (rv != 0) { detail = err; show_error("Cannot load this page", detail); s_copy(url_in.text, page_url, NGUI_INPUT_MAX); url_in.len = s_len(url_in.text); url_in.cursor = url_in.len; url_in.scroll = 0; return; } page_is_local = local; if (final[0]) { s_copy(page_url, final, sizeof(page_url)); s_copy(url_in.text, page_url, NGUI_INPUT_MAX); url_in.len = s_len(url_in.text); url_in.cursor = url_in.len; url_in.scroll = 0; } if (contains_ci(ctype, "text/plain")) { br_parse_plain(&doc, (const char *)body, len); } else if (contains_ci(ctype, "image/") || (!contains_ci(ctype, "text/html") && img_sniff(body, len, &image_info) == 0)) { unsigned int node = br_parse_image_doc(&doc, page_url); if (node) decode_direct_image(body, len, node); s_copy(page_title, "Image", sizeof(page_title)); } else { br_set_viewport(ui.w); br_parse(&doc, (const char *)body, len); s_copy(page_title, doc.title[0] ? doc.title : page_url, sizeof(page_title)); if (load_stylesheets() == FETCH_ABORT) return; if (pending_action) return; } post_status_text[0] = '\0'; post_status_error = 0; post_status_ttl = 0; if (truncated) { s_copy(post_status_text, "Page truncated at the buffer limit", sizeof(post_status_text)); post_status_ttl = 6000; } else if (doc.truncated) { s_copy(post_status_text, "Page truncated by the parser", sizeof(post_status_text)); post_status_ttl = 6000; } else if (starts_with(page_url, "https://")) { s_copy(post_status_text, "HTTPS: certificate NOT verified", sizeof(post_status_text)); post_status_ttl = 6000; } preload_begin(); page_loading = 0; s_copy(url_in.text, page_url, NGUI_INPUT_MAX); url_in.len = s_len(url_in.text); url_in.cursor = url_in.len; url_in.scroll = 0; ngui_set_focus(&ui, 0); ngui_invalidate(&ui); } static void load_home(int push) { cancel_fetch = 0; preload_active = 0; preload_pending = 0; heap_clear(); image_reset(); layout_valid = 0; page_scroll.offset = 0; page_scroll.dragging = 0; br_parse_plain(&doc, "", 0); page_loading = 1; page_is_local = 0; s_copy(page_url, "about:home", sizeof(page_url)); s_copy(url_in.text, page_url, NGUI_INPUT_MAX); url_in.len = s_len(url_in.text); url_in.cursor = url_in.len; url_in.scroll = 0; ngui_set_focus(&ui, 0); if (push) history_push(page_url); ngui_invalidate(&ui); redraw_now(); if (heap_start() != 0) { show_error("Out of memory", "Cannot allocate the browser heap"); return; } ngui_font_attach(&ui); browser_rules_alloc(); { static const char home[] = "A small HTML/CSS browser built on ngui, the " "shared HTTP client and the image library.
" "Type a URL in the bar above. Esc closes the " "window, Ctrl+L focuses the address bar.
" "javascript: not supported\nforms: not supported" "\nimages: PNG JPEG GIF BMP TGA PNM ICO" ""; br_set_viewport(ui.w); br_parse(&doc, home, sizeof(home) - 1); br_parse_inline_css(&doc); br_reset_styles(&doc); s_copy(page_title, doc.title, sizeof(page_title)); } post_status_text[0] = '\0'; post_status_error = 0; post_status_ttl = 0; page_loading = 0; set_status("", 0, 0); s_copy(url_in.text, page_url, NGUI_INPUT_MAX); url_in.len = s_len(url_in.text); url_in.cursor = url_in.len; url_in.scroll = 0; ngui_set_focus(&ui, 0); ngui_invalidate(&ui); } static void navigate(const char *input, int push) { char target[BR_URL_MAX]; char *t; if (!input) return; s_copy(target, input, sizeof(target)); t = trim(target); if (!t[0]) return; if (s_eq(t, "about:home")) { load_home(push); return; } if (starts_with(t, "file://")) { make_abs_local(t + 7, target, sizeof(target)); load_page(target, 1, push); return; } if (is_http(t)) { load_page(t, 0, push); return; } { struct sys_stat st; if (t[0] == '/' || (u_stat(t, &st) == 0 && !st.is_dir)) { make_abs_local(t, target, sizeof(target)); load_page(target, 1, push); return; } } { char with_scheme[BR_URL_MAX]; unsigned int n = 0; s_append(with_scheme, &n, "http://", sizeof(with_scheme)); s_append(with_scheme, &n, t, sizeof(with_scheme)); load_page(with_scheme, 0, push); } } /* ---- layout ---- */ static struct br_style base_style(void) { struct br_style s = { 0 }; s.color = ui.theme.text; s.bg = ui.theme.bg; s.has_bg = 0; s.border_color = 0; s.font_px = 16; s.bold = 0; s.italic = 0; s.mono = 0; s.underline = 0; s.align = BR_ALIGN_LEFT; s.display = BR_DISPLAY_BLOCK; s.no_bullet = 0; s.uppercase = 0; s.border_w = 0; s.width_kind = s.max_width_kind = s.height_kind = 0; s.width = s.max_width = s.height = 0; s.line_h_pct = 0; s.line_h_px = 0; s.mt = s.mb = s.ml = s.mr = 0; s.pt = s.pb = s.pl = s.pr = 0; return s; } static int run_width(const struct lay_run *r) { if (r->img >= 0) return r->iw; if (r->img == -2) return r->iw; return (int)r->w; } static void flush_line(struct lay *l) { int total, x, i, vis; if (!l->nruns && l->line_x == l->left) { l->line_h = 0; return; } total = l->line_x - l->left; x = l->left; vis = 1; if (l->draw) { int lh = l->line_h ? l->line_h : 16; vis = l->line_y + lh >= page_rect.y && l->line_y <= page_rect.y + page_rect.h; } if (l->align == BR_ALIGN_CENTER) x += (l->right - l->left - total) / 2; else if (l->align == BR_ALIGN_RIGHT) x = l->right - total; if (x < l->left) x = l->left; for (i = 0; i < l->nruns; i++) { struct lay_run *r = &l->runs[i]; int w = run_width(r); int ry = l->line_y + r->dy; x += r->gap; if (l->draw && vis) { if (r->img == -2) { int hh = r->ih ? r->ih : 20; ngui_fill_rect(l->ui, (struct ngui_rect){x, ry, w, hh}, l->ui->theme.panel_alt); ngui_frame(l->ui, (struct ngui_rect){x, ry, w, hh}, l->ui->theme.border); ngui_text_px(l->ui, x + 6, ry + 2, r->lit, s_len(r->lit), r->color, r->font_px, r->bold, r->italic, r->mono); } else if (r->img >= 0) { struct br_image *bi = &images[r->img]; if (bi->state == 1) { struct img_blit b; b.dst = l->ui->px; b.dst_w = l->ui->w; b.dst_h = l->ui->h; b.clip_x = page_rect.x; b.clip_y = page_rect.y; b.clip_w = page_rect.w - 12; b.clip_h = page_rect.h; b.x = x; b.y = ry; b.scale = ((unsigned int)r->iw << 16) / bi->img.w; b.bg = ui.theme.bg; b.bg2 = ui.theme.bg; b.flags = IMG_BLIT_OVER_DST; img_draw(&bi->img, &b); } } else { const char *s = r->has_lit ? r->lit : doc.text + r->off; unsigned int n = r->has_lit ? s_len(r->lit) : r->len; ngui_text_px(l->ui, x, ry, s, n, r->color, r->font_px, r->bold, r->italic, r->mono); if (r->underline) ngui_fill_rect(l->ui, (struct ngui_rect){x, ry + ngui_text_ascent(l->ui, r->font_px, r->mono) + 2, w, 1}, r->color); } if (r->href) { if (nhits < HIT_MAX) { struct hit *h = &hits[nhits]; int hh = r->img >= 0 ? r->ih : ngui_text_line_height(l->ui, r->font_px, r->mono); int merge = nhits > 0 && hits[nhits - 1].node == r->href && hits[nhits - 1].y == ry && hits[nhits - 1].h == hh && x <= hits[nhits - 1].x + hits[nhits - 1].w + 2; if (merge) { struct hit *p = &hits[nhits - 1]; int end = x + w; if (end > p->x + p->w) p->w = end - p->x; } else { h->x = x; h->y = ry; h->w = w; h->h = hh; h->node = r->href; nhits++; } } } } x += w; } l->y += l->line_h + 2; l->nruns = 0; l->line_x = l->left; l->run_x = l->left; l->line_h = 0; l->space_pending = 0; } static void add_run(struct lay *l, unsigned short off, unsigned short len, const struct br_style *st, unsigned short href, const char *lit) { unsigned int n = lit ? s_len(lit) : len; const char *tp = lit ? lit : doc.text + off; int w = (int)ngui_text_width_px(l->ui, tp, n, st->font_px, st->bold, st->italic, st->mono); if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0]))) flush_line(l); if (l->nruns == 0) { l->line_y = l->y; l->line_h = 0; } { struct lay_run *r = &l->runs[l->nruns++]; int gap = l->line_x - l->run_x; if (gap < 0) gap = 0; r->gap = (unsigned short)gap; r->off = off; r->len = len; r->color = st->color; r->href = href; r->dy = 0; r->has_lit = 0; r->lit[0] = '\0'; if (lit && lit[0]) { unsigned int i; for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++) r->lit[i] = lit[i]; r->lit[i] = '\0'; r->has_lit = 1; } r->img = -1; r->iw = r->ih = 0; r->font_px = st->font_px; r->bold = st->bold; r->italic = st->italic; r->mono = st->mono; /* A control with its own background is a button, not body copy; * an inherited link underline would strike through the fill. */ r->underline = st->underline && !st->has_bg; r->w = (unsigned short)w; if (r->has_lit) r->w = (unsigned short)ngui_text_width_px( l->ui, r->lit, s_len(r->lit), r->font_px, r->bold, r->italic, r->mono); w = r->w; } l->line_x += w; l->run_x = l->line_x; { int lh = ngui_text_line_height(l->ui, st->font_px, st->mono); if (st->line_h_px) lh = st->line_h_px; else if (st->line_h_pct) lh = st->font_px * st->line_h_pct / 100; if (lh < ngui_text_line_height(l->ui, st->font_px, st->mono)) lh = ngui_text_line_height(l->ui, st->font_px, st->mono); if (l->line_h < lh) l->line_h = lh; } } static void add_image_run(struct lay *l, unsigned int node, const struct br_style *st, unsigned short href) { struct br_image *bi = 0; int idx = doc.nodes[node].img_index; int dw, dh, maxw; if (idx >= 0 && idx < image_count && images[idx].state == 1) bi = &images[idx]; if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) { int sp = (int)ngui_text_width_px(l->ui, " ", 1, st->font_px, st->bold, st->italic, st->mono); if (l->line_x + sp <= l->right) l->line_x += sp; } l->space_pending = 0; if (!bi) { const char *alt = br_alt(&doc, node); struct br_style gs = *st; char tmp[96]; unsigned int n = 0; int w; gs.color = BR_GRAY; gs.underline = 0; s_append(tmp, &n, "[", sizeof(tmp)); s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp)); s_append(tmp, &n, "]", sizeof(tmp)); w = (int)ngui_text_width_px(l->ui, tmp, n, gs.font_px, gs.bold, gs.italic, gs.mono); if (l->line_x + w > l->right && (l->nruns > 0 || l->line_x > l->left)) flush_line(l); add_run(l, 0, 0, &gs, href, tmp); if (!l->draw) { int t = l->line_y < 0 ? 0 : l->line_y; int hh = ngui_text_line_height(l->ui, gs.font_px, gs.mono); if (t > 65535) t = 65535; if (hh > 65535) hh = 65535; img_top[node] = (unsigned short)t; img_h[node] = (unsigned short)hh; } return; } maxw = l->right - l->left; if (maxw < 8) maxw = 8; { int iw = bi->native_w, ih = bi->native_h; int have_w = 0, have_h = 0, css_w = 0, css_h = 0; if (iw <= 0 || ih <= 0) { iw = (int)bi->img.w; ih = (int)bi->img.h; } dw = iw; dh = ih; if (doc.nodes[node].width_attr) { int want = doc.nodes[node].width_attr; dw = doc.nodes[node].width_pct ? maxw * want / 100 : want; have_w = 1; } if (doc.nodes[node].height_attr) { int want = doc.nodes[node].height_attr; dh = doc.nodes[node].height_pct ? maxw * want / 100 : want; have_h = 1; } if (have_w && !have_h) dh = ih ? ih * dw / iw : 0; if (have_h && !have_w) dw = iw ? iw * dh / ih : 0; if (st->width_kind) { css_w = 1; dw = st->width_kind == 2 ? maxw * (int)st->width / 100 : (int)st->width; } if (st->height_kind) { css_h = 1; dh = st->height_kind == 2 ? maxw * (int)st->height / 100 : (int)st->height; } if (css_w && !css_h) dh = ih ? ih * dw / iw : 0; if (css_h && !css_w) dw = iw ? iw * dh / ih : 0; if (st->max_width_kind) { int mw = st->max_width_kind == 2 ? maxw * (int)st->max_width / 100 : (int)st->max_width; if (mw > 0 && dw > mw) { dh = iw ? ih * mw / iw : 0; dw = mw; } } } if (dw < 8) dw = 8; if (dh < 8) dh = 8; if (dw > maxw) { dh = dw ? dh * maxw / dw : dh; dw = maxw; } if (!l->draw) { int t = l->line_y < 0 ? 0 : l->line_y; if (t > 65535) t = 65535; if (dh > 65535) dh = 65535; img_top[node] = (unsigned short)t; img_h[node] = (unsigned short)dh; } if (l->line_x + dw > l->right && (l->nruns > 0 || l->line_x > l->left)) flush_line(l); if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0]))) flush_line(l); if (l->nruns == 0) { l->line_y = l->y; l->line_h = 0; } { struct lay_run *r = &l->runs[l->nruns++]; int gap = l->line_x - l->run_x; if (gap < 0) gap = 0; r->gap = (unsigned short)gap; r->off = 0; r->len = 0; r->color = 0; r->href = href; /* Inline replaced elements honour their vertical margins; this * is how Bootstrap's negative logo margins centre the wordmark. */ r->dy = st->mt; r->has_lit = 0; r->lit[0] = '\0'; r->img = (short)idx; r->iw = (unsigned short)dw; r->ih = (unsigned short)dh; r->w = (unsigned short)dw; r->font_px = st->font_px; r->bold = st->bold; r->italic = st->italic; r->mono = st->mono; r->underline = 0; } l->line_x += dw; l->run_x = l->line_x; if (l->line_h < dh) l->line_h = dh; } static int text_width(struct lay *l, const char *s, unsigned int n, const struct br_style *st) { return (int)ngui_text_width_px(l->ui, s, n, st->font_px, st->bold, st->italic, st->mono); } /* Longest prefix that fits in maxw, at least one byte. */ static unsigned int fit_prefix(struct lay *l, const char *s, unsigned int n, const struct br_style *st, int maxw) { unsigned int take = 0; while (take < n) { if (text_width(l, s, take + 1, st) > maxw) break; take++; } return take ? take : 1; } static void emit_word(struct lay *l, const char *s, unsigned int start, unsigned int n, unsigned int base, const struct br_style *st, unsigned short href) { int avail = l->right - l->left; int w = text_width(l, s + start, n, st); unsigned int off = start; if (l->line_x + w > l->right && (l->nruns > 0 || l->line_x > l->left)) flush_line(l); if (w <= avail) { if (n) add_run(l, (unsigned short)(base + start), (unsigned short)n, st, href, 0); return; } while (n > 0) { unsigned int take = fit_prefix(l, s + off, n, st, avail); add_run(l, (unsigned short)(base + off), (unsigned short)take, st, href, 0); off += take; n -= take; if (n) flush_line(l); } } static void emit_text(struct lay *l, const char *s, unsigned int len, const struct br_style *st, unsigned short href, int pre) { unsigned int i = 0; unsigned int base; int space_w = (int)ngui_text_width_px(l->ui, " ", 1, st->font_px, st->bold, st->italic, st->mono); if (!s || !len) return; if (space_w <= 0) space_w = 8; base = (unsigned int)(s - doc.text); if (st->uppercase) { char *p = (char *)s; unsigned int j; for (j = 0; j < len; j++) if (p[j] >= 'a' && p[j] <= 'z') p[j] = (char)(p[j] - 'a' + 'A'); } if (pre) { while (i < len) { unsigned int start; if (s[i] == '\n') { flush_line(l); i++; continue; } if (s[i] == ' ') { if (l->line_x + space_w > l->right && (l->nruns > 0 || l->line_x > l->left)) flush_line(l); else l->line_x += space_w; i++; continue; } start = i; while (i < len && s[i] != ' ' && s[i] != '\n') i++; emit_word(l, s, start, i - start, base, st, href); } return; } while (i < len) { unsigned int start, n; if (s[i] == ' ') { l->space_pending = 1; i++; continue; } start = i; while (i < len && s[i] != ' ') i++; n = i - start; if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) { if (l->line_x + space_w <= l->right) l->line_x += space_w; } l->space_pending = 0; emit_word(l, s, start, n, base, st, href); } } static int list_index(unsigned int parent, unsigned int node) { unsigned int n = 0, i; for (i = doc.nodes[parent].first; i && i != node; i = doc.nodes[i].next) if (doc.nodes[i].tag == BR_TAG_LI) n++; return (int)n + 1; } static const char *form_text(unsigned int node) { const char *v = br_value(&doc, node); const char *p = br_placeholder(&doc, node); const char *t = br_type(&doc, node); if (s_eq(t, "hidden")) return 0; if (s_eq(t, "submit") || s_eq(t, "button") || s_eq(t, "reset")) return v[0] ? v : "Button"; if (v[0]) return v; if (p[0]) return p; if (doc.nodes[node].tag == BR_TAG_TEXTAREA) return "Text"; if (doc.nodes[node].tag == BR_TAG_SELECT) return "Select"; return "Search"; } static void add_form_run(struct lay *l, unsigned int node, const struct br_style *st, unsigned short href) { const char *text = form_text(node); struct lay_run *r; int tw, w, h, i; if (!text) return; tw = (int)ngui_text_width_px(l->ui, text, s_len(text), st->font_px, st->bold, st->italic, st->mono); h = ngui_text_line_height(l->ui, st->font_px, st->mono) + 4; if (h < 20) h = 20; if (st->width_kind == 1) w = st->width; else if (st->width_kind == 2) w = (l->right - l->left) * (int)st->width / 100; else w = tw + 18; if (w < 64) w = 64; if (w > l->right - l->left) w = l->right - l->left; if (w < 8) w = 8; if (l->line_x + w > l->right && (l->nruns > 0 || l->line_x > l->left)) flush_line(l); if (l->nruns >= 96) return; r = &l->runs[l->nruns++]; r->gap = (unsigned short)(l->line_x - l->run_x); r->off = r->len = 0; r->color = st->color; r->href = href; r->dy = 0; r->has_lit = 1; for (i = 0; i + 1 < (int)sizeof(r->lit) && text[i]; i++) r->lit[i] = text[i]; r->lit[i] = '\0'; r->img = -2; r->iw = (unsigned short)w; r->ih = (unsigned short)h; r->w = (unsigned short)w; r->font_px = st->font_px; r->bold = st->bold; r->italic = st->italic; r->mono = st->mono; r->underline = 0; l->line_x += w; l->run_x = l->line_x; if (l->line_h < h) l->line_h = h; } /* A poster wrapper is absolutely positioned over the media slot; the * measured height of its picture is normally added back to the flow. When a * loading-skeleton sibling already reserves that slot, adding both doubles * the media height and pushes the article text a full screen down. */ static int has_skeleton_sibling(unsigned int node) { unsigned int p = doc.nodes[node].parent, c; for (c = doc.nodes[p].first; c; c = doc.nodes[c].next) { if (c == node || doc.nodes[c].type != BR_NODE_ELEM) continue; if (doc.nodes[c].class_off && contains_ci(doc.attrs + doc.nodes[c].class_off, "skeleton")) return 1; } return 0; } static void layout_children(struct lay *l, unsigned int parent, const struct br_style *pst, int depth, unsigned short link); static void layout_flex_children(struct lay *l, unsigned int parent, const struct br_style *pst, int depth, unsigned short link); static void layout_grid_children(struct lay *l, unsigned int parent, const struct br_style *pst, int depth, unsigned short link); static void layout_grid_auto(struct lay *l, unsigned int parent, const struct br_style *pst, int depth, unsigned short link); static int flex_item_width(struct lay *l, unsigned int n, const struct br_style *cs, int avail); static unsigned int layout_inline_blocks(struct lay *l, unsigned int first, const struct br_style *pst, int depth, unsigned short link); static void layout_table_row(struct lay *l, unsigned int row, const struct br_style *pst, int depth, unsigned short link); /* A flex container whose items are only inline spans is usually just a * heading/icon pair. Our flex row positions such items without emitting * their inline text in some deep page contexts; normal inline flow renders * the same content correctly. */ static int flex_wants_row(unsigned int parent, const struct br_style *pst) { unsigned int c; for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) { struct br_node *nd = &doc.nodes[c]; struct br_style cs; if (nd->type == BR_NODE_TEXT) continue; br_style_for(&doc, c, pst, &cs); if (cs.display == BR_DISPLAY_NONE) continue; if (cs.display != BR_DISPLAY_INLINE || cs.flex_grow || cs.width_kind || cs.has_bg || cs.border_w || nd->tag == BR_TAG_IMG) return 1; } return 0; } static int css_radius(const struct br_style *st, int corner, int w, int h) { int r = st->radius[corner]; if (r <= 0) return 0; if (r > w / 2) r = w / 2; if (r > h / 2) r = h / 2; return r; } static void svg_viewbox(unsigned node, int *vw, int *vh) { struct br_node *nd = &doc.nodes[node]; if (nd->width_attr && nd->height_attr) { *vw = nd->width_attr; *vh = nd->height_attr; } else { *vw = 300; *vh = 150; } if (*vw < 1) *vw = 1; if (*vh < 1) *vh = 1; } static int svg_num(const char **pp) { const char *p = *pp; int neg = 0, v = 0, frac = 0, scale = 1; while (*p == ' ' || *p == ',' || *p == '\t' || *p == '\n' || *p == '\r') p++; if (*p == '-') { neg = 1; p++; } else if (*p == '+') { p++; } while (*p >= '0' && *p <= '9') { v = v * 10 + (*p - '0'); p++; } if (*p == '.') { p++; while (*p >= '0' && *p <= '9' && scale < 1000) { frac = frac * 10 + (*p - '0'); scale *= 10; p++; } } v += frac / scale; *pp = p; return neg ? -v : v; } static void svg_path(struct ngui *ui, const char *d, int ox, int oy, int dw, int dh, int vw, int vh, unsigned short color) { int cx = 0, cy = 0, sx = 0, sy = 0; char cmd = 0; const char *p = d; while (p && *p) { int rel, nx, ny; char c; while (*p == ' ' || *p == ',' || *p == '\t' || *p == '\n' || *p == '\r') p++; if (!*p) break; if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) cmd = *p++; else if (!cmd) { p++; continue; } rel = cmd >= 'a' && cmd <= 'z'; c = rel ? (char)(cmd - 'a' + 'A') : cmd; if (c == 'Z') { ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh, ox + sx * dw / vw, oy + sy * dh / vh, color); cx = sx; cy = sy; cmd = 0; continue; } if (c == 'H' || c == 'V') { const char *save = p; int a = svg_num(&p); if (p == save) { p++; continue; } nx = cx; ny = cy; if (c == 'H') nx = rel ? cx + a : a; else ny = rel ? cy + a : a; } else { const char *save = p; int a = svg_num(&p); int b = svg_num(&p); if (p == save) { p++; continue; } if (c == 'M') { nx = rel ? cx + a : a; ny = rel ? cy + b : b; cx = sx = nx; cy = sy = ny; cmd = rel ? 'l' : 'L'; continue; } if (c != 'L') break; nx = rel ? cx + a : a; ny = rel ? cy + b : b; } ngui_line(ui, ox + cx * dw / vw, oy + cy * dh / vh, ox + nx * dw / vw, oy + ny * dh / vh, color); cx = nx; cy = ny; } } static void draw_svg(struct ngui *ui, unsigned node, int ox, int oy, int dw, int dh, int vw, int vh, const struct br_style *pst) { unsigned int c; for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) { struct br_node *nd = &doc.nodes[c]; struct br_style st; const char *nm; if (nd->type != BR_NODE_ELEM || !nd->name_off) continue; nm = doc.attrs + nd->name_off; br_style_for(&doc, c, pst, &st); if (s_eq(nm, "rect")) { struct ngui_rect r; int rx = (int)nd->svg_rx * dw / vw; r.x = ox + (int)nd->svg_x * dw / vw; r.y = oy + (int)nd->svg_y * dh / vh; r.w = (int)nd->width_attr * dw / vw; r.h = (int)nd->height_attr * dh / vh; if (r.w < 1) r.w = 1; if (r.h < 1) r.h = 1; if (rx > r.w / 2) rx = r.w / 2; if (rx > r.h / 2) rx = r.h / 2; if (st.has_bg) { if (rx) ngui_fill_round_rect(ui, r, rx, rx, rx, rx, st.bg); else ngui_fill_rect(ui, r, st.bg); } if (st.border_w) { if (rx) ngui_frame_round(ui, r, rx, rx, rx, rx, st.border_w, st.border_color); else ngui_frame(ui, r, st.border_color); } } else if (s_eq(nm, "line")) { if (st.border_w) ngui_line(ui, ox + (int)nd->svg_x * dw / vw, oy + (int)nd->svg_y * dh / vh, ox + (int)nd->width_attr * dw / vw, oy + (int)nd->height_attr * dh / vh, st.border_color); } else if (s_eq(nm, "path")) { if (st.border_w && nd->src_off) svg_path(ui, doc.attrs + nd->src_off, ox, oy, dw, dh, vw, vh, st.border_color); } else if (s_eq(nm, "text")) { unsigned int t = nd->first; const char *s; unsigned int n, tw; int px = st.font_px ? st.font_px : 16; int tx, ty, anchor; if (!t || doc.nodes[t].type != BR_NODE_TEXT) continue; s = doc.text + doc.nodes[t].text_off; n = doc.nodes[t].text_len; tw = ngui_text_width_px(ui, s, n, px, st.bold, st.italic, st.mono); tx = ox + (int)nd->svg_x * dw / vw; ty = oy + (int)nd->svg_y * dh / vh - ngui_text_ascent(ui, px, st.mono); anchor = st.align; if (nd->attr_flags & BR_ATTR_ANCHOR_MIDDLE) anchor = BR_ALIGN_CENTER; else if (nd->attr_flags & BR_ATTR_ANCHOR_END) anchor = BR_ALIGN_RIGHT; if (anchor == BR_ALIGN_CENTER) tx -= (int)tw / 2; else if (anchor == BR_ALIGN_RIGHT) tx -= (int)tw; ngui_text_px(ui, tx, ty, s, n, st.color, px, st.bold, st.italic, st.mono); } } } static void layout_block(struct lay *l, unsigned int node, const struct br_style *st, int depth, unsigned short link) { int old_left = l->left, old_right = l->right, old_align = l->align; int top, l0, r0; int flow_y, flow_left, flow_right, positioned; if (depth > 24 || st->display == BR_DISPLAY_NONE) return; flow_y = l->y; flow_left = l->left; flow_right = l->right; positioned = st->pos; if (positioned) { if (st->pos == 2) { l->y = page_rect.y + (st->top_set ? st->top : 0); l->left = page_rect.x; l->right = page_rect.x + page_rect.w - 12; } else { l->y = flow_y + (st->top_set ? st->top : 0); l->left = flow_left + (st->left_set ? st->left : 0); l->right = flow_right + (st->left_set ? st->left : 0); } old_left = l->left; old_right = l->right; } if (doc.nodes[node].tag == BR_TAG_INPUT || doc.nodes[node].tag == BR_TAG_TEXTAREA || doc.nodes[node].tag == BR_TAG_SELECT) { if (!(doc.nodes[node].tag == BR_TAG_INPUT && s_eq(br_type(&doc, node), "hidden"))) add_form_run(l, node, st, link); l->y += 20; return; } flush_line(l); l->y += st->mt; top = l->y; l0 = old_left + st->ml; r0 = old_right - st->mr; if (r0 < l0 + 16) r0 = l0 + 16; if (l->draw && block_h[node] && top < page_rect.y + page_rect.h && top + block_h[node] > page_rect.y) { struct ngui_rect box = {l0, top, r0 - l0, block_h[node]}; int rtl = css_radius(st, 0, box.w, box.h); int rtr = css_radius(st, 1, box.w, box.h); int rbr = css_radius(st, 2, box.w, box.h); int rbl = css_radius(st, 3, box.w, box.h); if (rtl || rtr || rbr || rbl) { if (st->has_bg) { int bw = st->border_w; if (bw > 0 && st->border_color != st->bg && box.w > bw * 2 && box.h > bw * 2) { struct ngui_rect inner = { box.x + bw, box.y + bw, box.w - bw * 2, box.h - bw * 2 }; int itl = rtl > bw ? rtl - bw : 0; int itr = rtr > bw ? rtr - bw : 0; int ibr = rbr > bw ? rbr - bw : 0; int ibl = rbl > bw ? rbl - bw : 0; ngui_fill_round_rect(l->ui, box, rtl, rtr, rbr, rbl, st->border_color); ngui_fill_round_rect(l->ui, inner, itl, itr, ibr, ibl, st->bg); } else { ngui_fill_round_rect(l->ui, box, rtl, rtr, rbr, rbl, st->bg); } } else if (st->border_w) { ngui_frame_round(l->ui, box, rtl, rtr, rbr, rbl, st->border_w, st->border_color); } } else { if (st->has_bg) ngui_fill_rect(l->ui, box, st->bg); if (st->border_w) ngui_frame(l->ui, box, st->border_color); } } l->left = l0 + st->pl; l->right = r0 - st->pr; if (l->right < l->left + 8) l->right = l->left + 8; l->align = st->align; l->y += st->pt; if (doc.nodes[node].tag == BR_TAG_HR) { if (l->draw) ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y + 2, l->right - l->left, 1}, BR_GRAY); l->y += 8; } else if (doc.nodes[node].tag == BR_TAG_VIDEO) { int vw = l->right - l->left; int vh; if (doc.nodes[node].width_pct) vw = vw * doc.nodes[node].width_attr / 100; else if (doc.nodes[node].width_attr) vw = doc.nodes[node].width_attr; if (vw > l->right - l->left) vw = l->right - l->left; if (vw < 8) vw = 8; vh = vw * 9 / 16; if (vh > 240) vh = 240; if (vh < 80) vh = 80; if (l->draw) { ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y, vw, vh}, 0xD69A); ngui_frame(l->ui, (struct ngui_rect){l->left, l->y, vw, vh}, 0x8410); ngui_text(l->ui, l->left + 8, l->y + 8, "[video]", 0x4208); } l->y += vh + 4; } else if (doc.nodes[node].tag == BR_TAG_SVG) { int vw, vh, dw = l->right - l->left; int dh; svg_viewbox(node, &vw, &vh); if (dw < 8) dw = 8; dh = dw * vh / vw; if (dh < 8) dh = 8; if (dh > 1400) dh = 1400; if (l->draw) { struct ngui_rect clip = {l->left, l->y, dw, dh}; ngui_push_clip(l->ui, clip); draw_svg(l->ui, node, l->left, l->y, dw, dh, vw, vh, st); ngui_pop_clip(l->ui); } l->y += dh; } else { if (doc.nodes[node].tag == BR_TAG_LI && !st->no_bullet) { char *m = li_marker[node]; if (!m[0]) { unsigned int parent = doc.nodes[node].parent; if (parent && doc.nodes[parent].tag == BR_TAG_OL) { int idx = list_index(parent, node); unsigned int n = 0; s_append_dec(m, &n, (unsigned int)idx, 6); s_append(m, &n, ".", 6); } else { s_copy(m, "-", 6); } } if (l->draw) { int mw = (int)ngui_text_width_ui(l->ui, m); ngui_text(l->ui, l->left - mw - 6, l->y, m, st->color); } } if (doc.nodes[node].tag == BR_TAG_TR) layout_table_row(l, node, st, depth + 1, link); else if (st->grid_auto) layout_grid_auto(l, node, st, depth + 1, link); else if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 && st->grid_gap > 0) layout_grid_children(l, node, st, depth + 1, link); else if (st->display == BR_DISPLAY_FLEX && !st->flex_col && flex_wants_row(node, st)) layout_flex_children(l, node, st, depth + 1, link); else layout_children(l, node, st, depth + 1, link); } flush_line(l); l->y += st->pb; /* Honour an explicit CSS height. Fixed-height header/icon boxes often * contain only absolutely positioned children, so their flow height * would otherwise collapse to zero. */ if (st->height_kind == 1 && st->height > 0 && l->y < top + st->height) l->y = top + st->height; l->left = old_left; l->right = old_right; l->align = old_align; l->y += st->mb; if (!l->draw) { /* block_h is the element box, not the flow advance: the bottom * margin must not be painted as part of the background. */ int h = l->y - st->mb - top; if (h < 0) h = 0; if (h > 65535) h = 65535; block_h[node] = (unsigned short)h; } if (positioned) { l->y = flow_y; l->left = flow_left; l->right = flow_right; } } static int text_node_blank(const struct br_node *nd) { const char *t = doc.text + nd->text_off; unsigned int i; for (i = 0; i < nd->text_len; i++) if (t[i] != ' ' && t[i] != '\t' && t[i] != '\n' && t[i] != '\r') return 0; return 1; } /* Rough shrink-to-fit width for a float without an explicit width: sum the * text/image widths in the subtree, plus padding and margins. This keeps * Bootstrap's floated nav items on one line instead of giving each the whole * row. */ static int text_space(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'; } static int intrinsic_width(struct ngui *ui, unsigned int node, const struct br_style *pst, int depth) { unsigned int c; int w = 0; if (depth > 12) return 0; for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) { struct br_node *nd = &doc.nodes[c]; struct br_style cs; if (nd->type == BR_NODE_TEXT) { const char *t = doc.text + nd->text_off; unsigned int tn = nd->text_len; if (doc.nodes[node].tag == BR_TAG_PRE) { w += (int)ngui_text_width_px(ui, t, tn, pst->font_px, pst->bold, pst->italic, pst->mono); } else { unsigned int ti = 0; int first = 1; int lead = w > 0 && tn > 0 && text_space(t[0]); int sp = (int)ngui_text_width_px(ui, " ", 1, pst->font_px, pst->bold, pst->italic, pst->mono); while (ti < tn) { unsigned int ts; while (ti < tn && text_space(t[ti])) ti++; ts = ti; while (ti < tn && !text_space(t[ti])) ti++; if (ti == ts) break; if (!first || lead) w += sp; lead = 0; w += (int)ngui_text_width_px(ui, t + ts, ti - ts, pst->font_px, pst->bold, pst->italic, pst->mono); first = 0; } } continue; } br_style_for(&doc, c, pst, &cs); if (cs.display == BR_DISPLAY_NONE) continue; if (nd->tag == BR_TAG_BR) { w += 8; continue; } if (nd->tag == BR_TAG_IMG) { /* Measure the image as layout will draw it: a CSS or * attribute height keeps the intrinsic aspect ratio, and * a CSS width overrides the native pixel width. Bootstrap * sizes the Cloudron logo with height:40px only. */ int idx = nd->img_index; int iw = 0, ih = 0; if (idx >= 0 && idx < image_count && images[idx].state == 1) { iw = (int)images[idx].native_w; ih = (int)images[idx].native_h; if (iw <= 0 || ih <= 0) { iw = (int)images[idx].img.w; ih = (int)images[idx].img.h; } } if (cs.width_kind == 1) { w += cs.width; } else if (cs.width_kind == 2) { w += 64; } else if (nd->width_attr) { w += nd->width_attr; } else if (nd->height_attr && iw && ih) { w += iw * (int)nd->height_attr / ih; } else if (cs.height_kind == 1 && iw && ih) { w += iw * (int)cs.height / ih; } else if (iw) { w += iw; } else { w += 64; } continue; } w += cs.pl + cs.pr + cs.ml + cs.mr; w += intrinsic_width(ui, c, &cs, depth + 1); if (w > 4096) break; } return w; } /* Lay consecutive floated block children side by side. Bootstrap's grid is * the main user: .row > .col-md-* with float:left and a percentage width. * Each column is laid out with its own left/right edge and the row advances * to the tallest column. Returns the first child not consumed. */ static unsigned int layout_float_group(struct lay *l, unsigned int first, const struct br_style *pst, int depth, unsigned short link) { unsigned int n = first; int row_left = l->left, row_right = l->right; int row_top, row_bottom, xl, xr, any = 0; flush_line(l); row_top = l->y; row_bottom = l->y; xl = row_left; xr = row_right; while (n) { struct br_node *nd = &doc.nodes[n]; struct br_style cs; unsigned short lk = link; int colw, right; if (nd->type == BR_NODE_TEXT) { if (text_node_blank(nd)) { n = nd->next; continue; } break; } br_style_for(&doc, n, pst, &cs); if (cs.display == BR_DISPLAY_NONE) { n = nd->next; continue; } /* A float blockifies its box: Bootstrap floats inline * anchors too (the navbar brand and its text links), and * their padding must be honoured. */ if (!cs.float_left) break; if (nd->tag == BR_TAG_A && nd->href_off) lk = (unsigned short)n; right = cs.float_left == 2; if (cs.width_kind == 2) colw = (row_right - row_left) * (int)cs.width / 100; else if (cs.width_kind == 1) colw = (int)cs.width; else { colw = intrinsic_width(l->ui, n, &cs, 0) + cs.pl + cs.pr + cs.ml + cs.mr; if (colw < 8) colw = 8; } if (colw < 8) colw = 8; if (colw > row_right - row_left) colw = row_right - row_left; if (right) { if (any && xr - colw < xl) { row_top = row_bottom; row_bottom = row_top; xl = row_left; xr = row_right; } if (xr - colw < xl) colw = xr - xl; if (colw < 8) colw = 8; l->left = xr - colw; if (l->left < row_left) l->left = row_left; l->right = xr; xr = l->left; } else { if (any && xl + colw > xr) { row_top = row_bottom; row_bottom = row_top; xl = row_left; xr = row_right; } l->left = xl; l->right = xl + colw; xl += colw; } if (l->right < l->left + 8) l->right = l->left + 8; l->y = row_top; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; l->align = cs.align; layout_block(l, n, &cs, depth + 1, lk); if (l->y > row_bottom) row_bottom = l->y; any = 1; n = nd->next; } l->left = row_left; l->right = row_right; l->y = row_bottom; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; return n; } static int flex_item_width(struct lay *l, unsigned int n, const struct br_style *cs, int avail) { struct br_node *nd = &doc.nodes[n]; (void)l; int w; if (cs->width_kind == 2) w = avail * (int)cs->width / 100; else if (cs->width_kind == 1) w = (int)cs->width; else if (nd->tag == BR_TAG_IMG) { int idx = nd->img_index; if (idx >= 0 && idx < image_count && images[idx].state == 1) w = (int)images[idx].img.w; else if (nd->width_attr) w = (int)nd->width_attr; else w = 64; } else { w = intrinsic_width(l->ui, n, cs, 0); w += cs->pl + cs->pr + cs->ml + cs->mr; } if (w < 24) w = 24; if (w > avail) w = avail; return w; } /* A run of display:inline-block siblings (file tags, small buttons) belongs * on one line, not one full-width block each. Place intrinsic widths from * left to right and wrap when the row is full. */ static unsigned int layout_inline_blocks(struct lay *l, unsigned int first, const struct br_style *pst, int depth, unsigned short link) { unsigned int n = first; int row_left = l->left, row_right = l->right; int avail = row_right - row_left; int x = row_left, y = l->y, row_h = 0, any = 0; flush_line(l); y = l->y; while (n) { struct br_node *nd = &doc.nodes[n]; struct br_style cs; unsigned short lk = link; int colw, h; if (nd->type == BR_NODE_TEXT) { if (!text_node_blank(nd)) break; if (any) x += 4; n = nd->next; continue; } br_style_for(&doc, n, pst, &cs); if (cs.display == BR_DISPLAY_NONE) { n = nd->next; continue; } if (cs.display != BR_DISPLAY_INLINE_BLOCK) break; if (nd->tag == BR_TAG_A && nd->href_off) lk = (unsigned short)n; colw = flex_item_width(l, n, &cs, avail); /* A few pixels of slack keep a single long token (boot.S) * from wrapping at the exact content width. */ colw += 8; if (colw > avail) colw = avail; if (any && x + colw > row_right) { y += row_h + 2; x = row_left; row_h = 0; any = 0; } if (any && x > row_left) x += 4; l->left = x; l->right = x + colw; if (l->right < l->left + 8) l->right = l->left + 8; l->y = y; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; l->align = cs.align; layout_block(l, n, &cs, depth + 1, lk); h = l->y - y; if (h > row_h) row_h = h; x += colw; any = 1; n = nd->next; } if (any) y += row_h; l->left = row_left; l->right = row_right; l->y = y; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; return n; } static void layout_flex_children(struct lay *l, unsigned int parent, const struct br_style *pst, int depth, unsigned short link) { unsigned int n, c; int row_left = l->left, row_right = l->right; int avail = row_right - row_left; int grow_count = 0, fixed = 0, grow_w, gap = pst->grid_gap; int x = row_left, row_top = l->y, row_bottom = l->y, any = 0; if (avail < 160 || depth > 24) { layout_children(l, parent, pst, depth, link); return; } /* If direct text is not just whitespace, normal block flow handles it * better than pretending it is a flex item. */ for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) { struct br_node *nd = &doc.nodes[c]; if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) { layout_children(l, parent, pst, depth, link); return; } } for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) { struct br_node *nd = &doc.nodes[c]; struct br_style cs; if (nd->type == BR_NODE_TEXT) continue; br_style_for(&doc, c, pst, &cs); if (cs.display == BR_DISPLAY_NONE) continue; if (cs.flex_grow && !cs.width_kind) grow_count++; else fixed += flex_item_width(l, c, &cs, avail); } grow_w = grow_count ? (avail - fixed) / grow_count : 0; if (grow_w < 96) grow_w = 96; n = doc.nodes[parent].first; while (n) { struct br_node *nd = &doc.nodes[n]; struct br_style cs; unsigned short lk = link; int colw; if (nd->type == BR_NODE_TEXT) { n = nd->next; continue; } br_style_for(&doc, n, pst, &cs); if (cs.display == BR_DISPLAY_NONE) { n = nd->next; continue; } if (nd->tag == BR_TAG_A && nd->href_off) lk = (unsigned short)n; if (cs.flex_grow && !cs.width_kind) colw = grow_w; else colw = flex_item_width(l, n, &cs, avail); if (colw > row_right - row_left) colw = row_right - row_left; if (any && x + colw > row_right) { if (pst->flex_wrap == 2) { if (colw > row_right - x) colw = row_right - x; } else { row_top = row_bottom; row_bottom = row_top; x = row_left; } } if (any && gap && x > row_left) x += gap; l->left = x; l->right = x + colw; if (l->right < l->left + 8) l->right = l->left + 8; l->y = row_top; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; l->align = cs.align; layout_block(l, n, &cs, depth + 1, lk); if (l->y > row_bottom) row_bottom = l->y; x += colw; any++; n = nd->next; } l->left = row_left; l->right = row_right; l->y = row_bottom; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; } /* Two-column CSS grid: track sizes from grid-template-columns (px, %, fr) * plus the grid-area hints used by site shells (site notice/footer full * width, column-start left, page-content right). Anything more elaborate * falls back to normal block flow. */ static int grid_track_width(unsigned char kind, unsigned short val, int avail, int other) { if (kind == 2) return avail * (int)val / 100; if (kind == 1) return val; return other; } static void layout_grid_children(struct lay *l, unsigned int parent, const struct br_style *pst, int depth, unsigned short link) { unsigned int n; int row_left = l->left, row_right = l->right; int avail = row_right - row_left; int gap = pst->grid_gap; int track_total = avail - gap; int col0, col1, x0, x1; int row_y, row_bottom, have_left = 0, have_right = 0; unsigned int c; if (avail < 32 || depth > 24) return; if (track_total < 16) track_total = 16; col0 = grid_track_width((unsigned char)(pst->width_kind), pst->width, track_total, 0); col1 = grid_track_width((unsigned char)(pst->max_width_kind), pst->max_width, track_total, 0); if (pst->width_kind == 3 && pst->max_width_kind == 3) { int w0 = pst->width ? pst->width : 1; int w1 = pst->max_width ? pst->max_width : 1; col0 = track_total * w0 / (w0 + w1); col1 = track_total - col0; } else if (pst->width_kind == 3) { col0 = track_total - col1; if (col0 < 24) col0 = 24; } else if (pst->max_width_kind == 3) { col1 = track_total - col0; if (col1 < 24) col1 = 24; } if (col0 < 24) col0 = 24; if (col1 < 24) col1 = 24; if (col0 + col1 > track_total) { col1 = track_total - col0; if (col1 < 24) col1 = 24; } x0 = row_left; x1 = row_left + col0 + gap; /* Direct text makes a grid ambiguous; use block flow instead. */ for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) { struct br_node *nd = &doc.nodes[c]; if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) { layout_children(l, parent, pst, depth, link); return; } } row_y = row_bottom = l->y; n = doc.nodes[parent].first; while (n) { struct br_node *nd = &doc.nodes[n]; struct br_style cs; unsigned short lk = link; int slot, x, w; if (nd->type == BR_NODE_TEXT) { n = nd->next; continue; } br_style_for(&doc, n, pst, &cs); if (cs.display == BR_DISPLAY_NONE) { n = nd->next; continue; } if (nd->tag == BR_TAG_A && nd->href_off) lk = (unsigned short)n; slot = cs.grid_slot; if (!slot) { if (!have_left) slot = 2; else if (!have_right) slot = 3; else slot = 2; } if (slot == 1) { if (have_left || have_right) { row_y = row_bottom; have_left = have_right = 0; } x = row_left; w = avail; } else if (slot == 2) { if (have_left) { row_y = row_bottom; have_left = have_right = 0; } x = x0; w = col0; have_left = 1; } else { if (have_right) { row_y = row_bottom; have_left = have_right = 0; } x = x1; w = col1; have_right = 1; } l->left = x; l->right = x + w; if (l->right < l->left + 8) l->right = l->left + 8; l->y = row_y; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; l->align = cs.align; layout_block(l, n, &cs, depth + 1, lk); if (l->y > row_bottom) row_bottom = l->y; n = nd->next; } l->left = row_left; l->right = row_right; l->y = row_bottom; l->line_x = l->left; l->run_x = l->left; l->line_y = l->y; l->line_h = 0; l->nruns = 0; l->space_pending = 0; } /* repeat(auto-fit, minmax(