|
|
@@ -95,6 +95,7 @@ struct br_image {
|
|
|
int state; /* 0 empty, 1 ready */
|
|
|
struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */
|
|
|
int native_w, native_h;
|
|
|
+ unsigned int url_hash; /* <img> source, 0 = not shareable */
|
|
|
char err[32];
|
|
|
};
|
|
|
|
|
|
@@ -408,6 +409,22 @@ static void form_reset_state(void)
|
|
|
form_focus = 0;
|
|
|
}
|
|
|
|
|
|
+/* A <button> with element content (an icon <svg>/<img>, a <span> label)
|
|
|
+ * is laid out like a normal element so that content shows; only a text or
|
|
|
+ * empty button is drawn as a native control. Clicks still reach it
|
|
|
+ * through the box hit test. */
|
|
|
+static int button_has_elements(unsigned int node)
|
|
|
+{
|
|
|
+ unsigned int c;
|
|
|
+
|
|
|
+ if (node >= doc.nnode || doc.nodes[node].tag != BR_TAG_BUTTON)
|
|
|
+ return 0;
|
|
|
+ for (c = doc.nodes[node].first; c; c = doc.nodes[c].next)
|
|
|
+ if (doc.nodes[c].type == BR_NODE_ELEM)
|
|
|
+ return 1;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
static int form_is_control(unsigned int node)
|
|
|
{
|
|
|
unsigned int tag;
|
|
|
@@ -416,7 +433,8 @@ static int form_is_control(unsigned int node)
|
|
|
return 0;
|
|
|
tag = doc.nodes[node].tag;
|
|
|
return tag == BR_TAG_INPUT || tag == BR_TAG_TEXTAREA ||
|
|
|
- tag == BR_TAG_SELECT || tag == BR_TAG_BUTTON;
|
|
|
+ tag == BR_TAG_SELECT ||
|
|
|
+ (tag == BR_TAG_BUTTON && !button_has_elements(node));
|
|
|
}
|
|
|
|
|
|
#define HIT_MAX 96
|
|
|
@@ -2042,6 +2060,10 @@ static void decode_arena_put(void)
|
|
|
/* box_w/box_h: the laid-out box a background image paints, or 0. The
|
|
|
* bitmap then only needs to cover that box (an app-store icon is 48 px), not
|
|
|
* BR_IMG_LAYOUT_W, which keeps hundreds of small images inside the budget. */
|
|
|
+static int svg_text_sniff(const unsigned char *d, unsigned int len);
|
|
|
+static int svg_decode_image(const unsigned char *data, unsigned int len,
|
|
|
+ struct br_image *bi, int box_w, int box_h);
|
|
|
+
|
|
|
static int decode_image_data(const unsigned char *data, unsigned int len,
|
|
|
struct br_image *bi, int box_w, int box_h)
|
|
|
{
|
|
|
@@ -2055,7 +2077,10 @@ static int decode_image_data(const unsigned char *data, unsigned int len,
|
|
|
unsigned char *alpha = 0;
|
|
|
int dw, dh, r;
|
|
|
|
|
|
+ bi->url_hash = 0;
|
|
|
if (img_sniff(data, len, &info) != 0) {
|
|
|
+ if (svg_text_sniff(data, len))
|
|
|
+ return svg_decode_image(data, len, bi, box_w, box_h);
|
|
|
s_copy(bi->err, "unknown image format", sizeof(bi->err));
|
|
|
return -1;
|
|
|
}
|
|
|
@@ -4028,7 +4053,8 @@ static void preload_job_error(struct fetch_worker *w)
|
|
|
}
|
|
|
|
|
|
static void preload_decode(unsigned int node, int bg,
|
|
|
- const unsigned char *data, unsigned int len)
|
|
|
+ const unsigned char *data, unsigned int len,
|
|
|
+ const char *url)
|
|
|
{
|
|
|
if (image_count >= BR_MAX_IMAGES) {
|
|
|
if (bg)
|
|
|
@@ -4042,6 +4068,12 @@ static void preload_decode(unsigned int node, int bg,
|
|
|
layout_boxes[node].valid ? layout_boxes[node].w : 0,
|
|
|
bg && node < BR_MAX_NODES &&
|
|
|
layout_boxes[node].valid ? layout_boxes[node].h : 0) == 0) {
|
|
|
+ /* An <img> bitmap only depends on its URL, so the next
|
|
|
+ * <img> with the same source reuses it (heise repeats one
|
|
|
+ * comment icon 85 times). Backgrounds are scaled to their
|
|
|
+ * box and are not shared. */
|
|
|
+ images[image_count].url_hash =
|
|
|
+ !bg && url ? script_cache_hash(url) | 1u : 0;
|
|
|
if (bg)
|
|
|
doc.nodes[node].bg_img_index = (short)image_count;
|
|
|
else
|
|
|
@@ -4078,7 +4110,7 @@ static int preload_finish_worker(struct fetch_worker *w)
|
|
|
&tr, err, sizeof(err));
|
|
|
if (rv >= 0)
|
|
|
preload_decode(w->node, w->bg, image_file_buf,
|
|
|
- (unsigned int)rv);
|
|
|
+ (unsigned int)rv, w->url);
|
|
|
else
|
|
|
preload_job_error(w);
|
|
|
fetch_worker_release(w);
|
|
|
@@ -4130,7 +4162,6 @@ static int preload_fill_jobs(void)
|
|
|
continue;
|
|
|
}
|
|
|
if (bsrc[0] &&
|
|
|
- !contains_ci(bsrc, ".svg") &&
|
|
|
!starts_with(bsrc, "data:") &&
|
|
|
resolve_link(page_url, bsrc, url,
|
|
|
sizeof(url)) == 0) {
|
|
|
@@ -4150,7 +4181,7 @@ static int preload_fill_jobs(void)
|
|
|
&data, &len,
|
|
|
err,
|
|
|
sizeof(err)) == 0)
|
|
|
- preload_decode(i, 1, data, len);
|
|
|
+ preload_decode(i, 1, data, len, 0);
|
|
|
else
|
|
|
nd->bg_img_index = -2;
|
|
|
continue;
|
|
|
@@ -4192,8 +4223,7 @@ static int preload_fill_jobs(void)
|
|
|
continue;
|
|
|
}
|
|
|
src = br_src(&doc, i);
|
|
|
- if (!src[0] || contains_ci(src, ".svg") ||
|
|
|
- starts_with(src, "data:")) {
|
|
|
+ if (!src[0] || starts_with(src, "data:")) {
|
|
|
preload_node++;
|
|
|
continue;
|
|
|
}
|
|
|
@@ -4215,6 +4245,22 @@ static int preload_fill_jobs(void)
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ {
|
|
|
+ unsigned int uh = script_cache_hash(url) | 1u;
|
|
|
+ int k;
|
|
|
+
|
|
|
+ for (k = 0; k < image_count; k++)
|
|
|
+ if (images[k].state == 1 &&
|
|
|
+ images[k].url_hash == uh)
|
|
|
+ break;
|
|
|
+ if (k < image_count) {
|
|
|
+ nd->img_index = (short)k;
|
|
|
+ layout_valid = 0;
|
|
|
+ preload_node = i + 1;
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
if (url[0] == '/') {
|
|
|
unsigned char *data = 0;
|
|
|
unsigned int len = 0;
|
|
|
@@ -4222,7 +4268,7 @@ static int preload_fill_jobs(void)
|
|
|
preload_node = i + 1;
|
|
|
if (fetch_image(url, BR_IMG_FILE_MAX, &data, &len,
|
|
|
err, sizeof(err)) == 0)
|
|
|
- preload_decode(i, 0, data, len);
|
|
|
+ preload_decode(i, 0, data, len, url);
|
|
|
else
|
|
|
nd->img_index = -2;
|
|
|
continue;
|
|
|
@@ -6959,6 +7005,165 @@ static struct svg_cache_ent *svg_layer(unsigned int node, int w, int h,
|
|
|
return e;
|
|
|
}
|
|
|
|
|
|
+/* ---- SVG files as images (<img src="logo.svg">, CSS backgrounds) ----
|
|
|
+ *
|
|
|
+ * The markup is parsed into a detached subtree of the page document and
|
|
|
+ * drawn with the inline-SVG renderer above into a fresh RGB565 + alpha
|
|
|
+ * bitmap, which then behaves like any decoded image. currentColor is
|
|
|
+ * black (an image has no inherited text colour); <text> is not drawn. */
|
|
|
+static int svg_text_sniff(const unsigned char *d, unsigned int len)
|
|
|
+{
|
|
|
+ unsigned int i = 0;
|
|
|
+
|
|
|
+ if (len >= 3 && d[0] == 0xEF && d[1] == 0xBB && d[2] == 0xBF)
|
|
|
+ i = 3;
|
|
|
+ while (i < len) {
|
|
|
+ while (i < len && (d[i] == ' ' || d[i] == '\t' ||
|
|
|
+ d[i] == '\r' || d[i] == '\n'))
|
|
|
+ i++;
|
|
|
+ if (i + 4 <= len && !memcmp(d + i, "<svg", 4))
|
|
|
+ return 1;
|
|
|
+ if (i + 4 <= len && !memcmp(d + i, "<!--", 4)) {
|
|
|
+ i += 4;
|
|
|
+ while (i + 3 <= len && memcmp(d + i, "-->", 3))
|
|
|
+ i++;
|
|
|
+ i += 3;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (i + 1 < len && d[i] == '<' && (d[i + 1] == '?' || d[i + 1] == '!')) {
|
|
|
+ while (i < len && d[i] != '>')
|
|
|
+ i++;
|
|
|
+ i++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static int svg_len_attr(unsigned int node, const char *name)
|
|
|
+{
|
|
|
+ const char *v = br_find_attr(&doc, node, name);
|
|
|
+ int n = 0, any = 0;
|
|
|
+
|
|
|
+ if (!v)
|
|
|
+ return 0;
|
|
|
+ while (*v == ' ')
|
|
|
+ v++;
|
|
|
+ while (*v >= '0' && *v <= '9') {
|
|
|
+ n = n * 10 + (*v++ - '0');
|
|
|
+ any = 1;
|
|
|
+ }
|
|
|
+ if (*v == '.')
|
|
|
+ while (*++v >= '0' && *v <= '9')
|
|
|
+ ;
|
|
|
+ if (!any || *v == '%')
|
|
|
+ return 0;
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+static int svg_decode_image(const unsigned char *data, unsigned int len,
|
|
|
+ struct br_image *bi, int box_w, int box_h)
|
|
|
+{
|
|
|
+ unsigned int holder, svg = 0, c, need, i;
|
|
|
+ struct br_style base = base_style(), st;
|
|
|
+ struct svgr_layer L;
|
|
|
+ double vb[4], m[6];
|
|
|
+ int w, h, have_vb, nat_w, nat_h;
|
|
|
+
|
|
|
+ holder = br_create_element(&doc, "div", 3);
|
|
|
+ if (!holder) {
|
|
|
+ s_copy(bi->err, "page is full", sizeof(bi->err));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ holder--;
|
|
|
+ br_set_inner_html(&doc, holder, (const char *)data, len);
|
|
|
+ for (c = doc.nodes[holder].first; c; c = doc.nodes[c].next)
|
|
|
+ if (doc.nodes[c].type == BR_NODE_ELEM &&
|
|
|
+ doc.nodes[c].tag == BR_TAG_SVG) {
|
|
|
+ svg = c;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!svg) {
|
|
|
+ s_copy(bi->err, "bad SVG", sizeof(bi->err));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ have_vb = svg_viewbox4(svg, vb) && vb[2] > 0 && vb[3] > 0;
|
|
|
+ w = svg_len_attr(svg, "width");
|
|
|
+ h = svg_len_attr(svg, "height");
|
|
|
+ if (box_w > 0 && box_h > 0) {
|
|
|
+ w = box_w;
|
|
|
+ h = box_h;
|
|
|
+ } else if (!w && !h) {
|
|
|
+ w = have_vb ? (int)(vb[2] + 0.5) : 150;
|
|
|
+ h = have_vb ? (int)(vb[3] + 0.5) : 150;
|
|
|
+ } else if (!w) {
|
|
|
+ w = have_vb ? (int)(h * vb[2] / vb[3] + 0.5) : h;
|
|
|
+ } else if (!h) {
|
|
|
+ h = have_vb ? (int)(w * vb[3] / vb[2] + 0.5) : w;
|
|
|
+ }
|
|
|
+ /* Layout uses the intrinsic size; small icons are drawn at up to 4x
|
|
|
+ * so CSS scaling stays crisp. */
|
|
|
+ nat_w = w;
|
|
|
+ nat_h = h;
|
|
|
+ while (w < 96 && h < 96 && w * h > 0 && w * 2 <= 384) {
|
|
|
+ w *= 2;
|
|
|
+ h *= 2;
|
|
|
+ }
|
|
|
+ if (w > BR_IMG_LAYOUT_W) {
|
|
|
+ h = (int)((long)h * BR_IMG_LAYOUT_W / w);
|
|
|
+ w = BR_IMG_LAYOUT_W;
|
|
|
+ }
|
|
|
+ if (h > 2048)
|
|
|
+ h = 2048;
|
|
|
+ if (w < 1)
|
|
|
+ w = 1;
|
|
|
+ if (h < 1)
|
|
|
+ h = 1;
|
|
|
+ need = (unsigned int)w * (unsigned int)h;
|
|
|
+ if (image_pixels + need > BR_IMG_PIXEL_BUDGET) {
|
|
|
+ s_copy(bi->err, "image budget exhausted", sizeof(bi->err));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (!svg_geom_buf) {
|
|
|
+ svg_geom_buf = malloc(SVG_MAX_POINTS * (2 * sizeof(float) + 1));
|
|
|
+ if (!svg_geom_buf) {
|
|
|
+ s_copy(bi->err, "out of memory", sizeof(bi->err));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ svgr_init(&svg_geom, svg_geom_buf, SVG_MAX_POINTS);
|
|
|
+ L.px = (unsigned short *)malloc(need * sizeof(unsigned short));
|
|
|
+ L.a = (unsigned char *)calloc(need, 1);
|
|
|
+ if (!L.px || !L.a) {
|
|
|
+ free(L.px);
|
|
|
+ free(L.a);
|
|
|
+ s_copy(bi->err, "out of memory", sizeof(bi->err));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ for (i = 0; i < need; i++)
|
|
|
+ L.px[i] = 0xFFFF;
|
|
|
+ L.w = w;
|
|
|
+ L.h = h;
|
|
|
+ base.color = 0x0000;
|
|
|
+ br_style_for(&doc, svg, &base, &st);
|
|
|
+ svg_root_matrix(svg, w, h, m);
|
|
|
+ svg_vp_w = have_vb ? vb[2] : w;
|
|
|
+ svg_vp_h = have_vb ? vb[3] : h;
|
|
|
+ svg_draw_children(&L, svg, m, &st, 255, 0);
|
|
|
+ bi->native_w = nat_w;
|
|
|
+ bi->native_h = nat_h;
|
|
|
+ bi->img.format = 0;
|
|
|
+ bi->img.w = (unsigned int)w;
|
|
|
+ bi->img.h = (unsigned int)h;
|
|
|
+ bi->img.px = L.px;
|
|
|
+ bi->img.alpha = L.a;
|
|
|
+ bi->img.flags = 0;
|
|
|
+ bi->state = 1;
|
|
|
+ image_pixels += need;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
/* Intrinsic size of an inline <svg> in a w-wide containing box. */
|
|
|
static void svg_size(unsigned int node, const struct br_style *st, int cw,
|
|
|
int *out_w, int *out_h)
|
|
|
@@ -7202,10 +7407,11 @@ static void layout_block(struct lay *l, unsigned int node,
|
|
|
rel_dx += st->tx;
|
|
|
rel_dy += st->ty;
|
|
|
}
|
|
|
- if (doc.nodes[node].tag == BR_TAG_INPUT ||
|
|
|
- doc.nodes[node].tag == BR_TAG_TEXTAREA ||
|
|
|
- doc.nodes[node].tag == BR_TAG_SELECT ||
|
|
|
- doc.nodes[node].tag == BR_TAG_BUTTON) {
|
|
|
+ if ((doc.nodes[node].tag == BR_TAG_INPUT ||
|
|
|
+ doc.nodes[node].tag == BR_TAG_TEXTAREA ||
|
|
|
+ doc.nodes[node].tag == BR_TAG_SELECT ||
|
|
|
+ doc.nodes[node].tag == BR_TAG_BUTTON) &&
|
|
|
+ !button_has_elements(node)) {
|
|
|
if (doc.nodes[node].tag == BR_TAG_INPUT &&
|
|
|
s_eq(br_type(&doc, node), "hidden"))
|
|
|
return;
|
|
|
@@ -9397,8 +9603,9 @@ static void layout_children(struct lay *l, unsigned int parent,
|
|
|
n = nd->next;
|
|
|
continue;
|
|
|
}
|
|
|
- if (nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_TEXTAREA ||
|
|
|
- nd->tag == BR_TAG_SELECT || nd->tag == BR_TAG_BUTTON) {
|
|
|
+ if ((nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_TEXTAREA ||
|
|
|
+ nd->tag == BR_TAG_SELECT || nd->tag == BR_TAG_BUTTON) &&
|
|
|
+ !button_has_elements(n)) {
|
|
|
if (!(nd->tag == BR_TAG_INPUT &&
|
|
|
s_eq(br_type(&doc, n), "hidden"))) {
|
|
|
form_fill_line = pst->display ==
|