Просмотр исходного кода

Fix browser rounded fills and transparent images

The rounded-box scanline helper used a broken integer square root, so
corner insets barely varied and a pill rendered as a cross shape.  Use
the shift-and-subtract algorithm with the proper power-of-four seed and
two-bit steps.

Paint rounded boxes through a real rounded fill: with a distinct border
colour, fill the outer box with the border colour and an inset rounded
box with the background, so both outline and fill follow the radius.
Transparent-background boxes keep the rounded frame.  Suppress the link
underline on filled controls, and stop including the bottom margin in
the painted background height.

Keep the alpha plane of scaled images so transparent PNG logos
composite over the page instead of the browser theme background, and
add an alpha-output mode to img_draw() for the scaled bitmap.

Verified on cloudron.io (transparent logo, clean Install capsule), the
local nd-aktuell/heise/tagesschau fixtures, and round.html; make
test-images passes.
Johannes Zellner 2 недель назад
Родитель
Сommit
0a45281731
4 измененных файлов с 73 добавлено и 22 удалено
  1. 54 14
      browser.c
  2. 2 0
      img.h
  3. 12 5
      img_blit.c
  4. 5 3
      ngui.c

+ 54 - 14
browser.c

@@ -405,6 +405,7 @@ static int decode_image_data(const unsigned char *data, unsigned int len,
 	struct img_image decoded;
 	unsigned int est, bytes, i;
 	unsigned short *px;
+	unsigned char *alpha = 0;
 	int dw, dh, r;
 
 	if (img_sniff(data, len, &info) != 0) {
@@ -455,6 +456,18 @@ static int decode_image_data(const unsigned char *data, unsigned int len,
 		s_copy(bi->err, "out of memory", sizeof(bi->err));
 		return -1;
 	}
+	/* Keep the alpha plane for the scaled image so transparent logos and
+	 * icons composite over the page instead of the browser's dark theme
+	 * background.  Very large alpha images stay flattened to bound the
+	 * heap use. */
+	if (decoded.alpha && (unsigned int)dw * (unsigned int)dh <= (1u << 20)) {
+		alpha = (unsigned char *)heap_alloc((unsigned int)dw *
+						    (unsigned int)dh, 16);
+		if (alpha) {
+			for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
+				alpha[i] = 255;
+		}
+	}
 	for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
 		px[i] = ui.theme.bg;
 
@@ -473,7 +486,8 @@ static int decode_image_data(const unsigned char *data, unsigned int len,
 		b.scale = ((unsigned int)dw << 16) / decoded.w;
 		b.bg = ui.theme.bg;
 		b.bg2 = ui.theme.bg;
-		b.flags = IMG_BLIT_OVER_DST;
+		b.dst_a = alpha;
+		b.flags = alpha ? IMG_BLIT_SAVE_ALPHA : IMG_BLIT_OVER_DST;
 		img_draw(&decoded, &b);
 	}
 
@@ -481,7 +495,7 @@ static int decode_image_data(const unsigned char *data, unsigned int len,
 	bi->img.w = (unsigned int)dw;
 	bi->img.h = (unsigned int)dh;
 	bi->img.px = px;
-	bi->img.alpha = 0;
+	bi->img.alpha = alpha;
 	bi->img.flags = 0;
 	bi->state = 1;
 	return 0;
@@ -1853,7 +1867,9 @@ static void add_run(struct lay *l, unsigned short off, unsigned short len,
 		r->iw = r->ih = 0;
 		r->scale = st->scale;
 		r->bold = st->bold;
-		r->underline = st->underline;
+		/* 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;
 	}
 	l->line_x += w;
 	l->run_x = l->line_x;
@@ -2339,19 +2355,41 @@ static void layout_block(struct lay *l, unsigned int node,
 		int rbr = css_radius(st, 2, box.w, box.h);
 		int rbl = css_radius(st, 3, box.w, box.h);
 
-		if (st->has_bg) {
-			if (rtl || rtr || rbr || rbl)
-				ngui_fill_round_rect(l->ui, box, rtl, rtr,
-						     rbr, rbl, st->bg);
-			else
-				ngui_fill_rect(l->ui, box, st->bg);
-		}
-		if (st->border_w) {
-			if (rtl || rtr || rbr || rbl)
+		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
+			}
+		} 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);
 		}
 	}
@@ -2436,7 +2474,9 @@ static void layout_block(struct lay *l, unsigned int node,
 	l->align = old_align;
 	l->y += st->mb;
 	if (!l->draw) {
-		int h = l->y - top;
+		/* 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)

+ 2 - 0
img.h

@@ -75,11 +75,13 @@ struct img_blit {
 	unsigned int bg;         /* RGB565 background for alpha */
 	unsigned int bg2;        /* second checker colour */
 	unsigned int flags;
+	unsigned char *dst_a;    /* optional destination alpha plane */
 };
 
 #define IMG_BLIT_CHECKER  1  /* alpha over a checkered background */
 #define IMG_BLIT_OVER_DST 2  /* alpha over the existing destination pixels */
 #define IMG_BLIT_FAST     4  /* nearest sampling while dragging */
+#define IMG_BLIT_SAVE_ALPHA 8 /* write source alpha to dst_a, keep RGB raw */
 
 void img_mem_init(struct img_mem *m, void *base, unsigned int size);
 unsigned int img_mem_mark(const struct img_mem *m);

+ 12 - 5
img_blit.c

@@ -202,11 +202,18 @@ void img_draw(const struct img_image *im, const struct img_blit *b)
 						(unsigned short)b->bg;
 				else
 					bg = (unsigned short)b->bg;
-				d = (im->alpha && sa < 255) ?
-					blend(sr, sg, sb, sa, bg) :
-					(unsigned short)(((sr >> 3) << 11) |
-							 ((sg >> 2) << 5) |
-							 (sb >> 3));
+				if ((b->flags & IMG_BLIT_SAVE_ALPHA) && b->dst_a) {
+					b->dst_a[di] = (unsigned char)(sa > 255 ? 255 : sa);
+					d = (unsigned short)(((sr >> 3) << 11) |
+							     ((sg >> 2) << 5) |
+							     (sb >> 3));
+				} else {
+					d = (im->alpha && sa < 255) ?
+						blend(sr, sg, sb, sa, bg) :
+						(unsigned short)(((sr >> 3) << 11) |
+								 ((sg >> 2) << 5) |
+								 (sb >> 3));
+				}
 				b->dst[di] = d;
 			}
 		}

+ 5 - 3
ngui.c

@@ -436,12 +436,14 @@ void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
 
 static int ngui_isqrt(int v)
 {
-	int r = 0, bit = 1 << 15;
+	int r = 0, bit = 1 << 30;
 
 	if (v <= 0)
 		return 0;
+	/* Classic shift-and-subtract sqrt: bit must be the highest power
+	 * of four that fits in v, and each step consumes two bits. */
 	while (bit > v)
-		bit >>= 1;
+		bit >>= 2;
 	while (bit) {
 		if (v >= r + bit) {
 			v -= r + bit;
@@ -449,7 +451,7 @@ static int ngui_isqrt(int v)
 		} else {
 			r >>= 1;
 		}
-		bit >>= 1;
+		bit >>= 2;
 	}
 	return r;
 }