فهرست منبع

Add CSS border-radius support

Johannes Zellner 2 هفته پیش
والد
کامیت
1677e906bf
5فایلهای تغییر یافته به همراه336 افزوده شده و 9 حذف شده
  1. 35 8
      browser.c
  2. 156 1
      browser_html.c
  3. 3 0
      browser_html.h
  4. 136 0
      ngui.c
  5. 6 0
      ngui.h

+ 35 - 8
browser.c

@@ -2273,6 +2273,20 @@ static int flex_wants_row(unsigned int parent, const struct br_style *pst)
 	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 layout_block(struct lay *l, unsigned int node,
 			 const struct br_style *st, int depth,
 			 unsigned short link)
@@ -2319,14 +2333,27 @@ static void layout_block(struct lay *l, unsigned int node,
 	if (l->draw && block_h[node] &&
 	    top < page_rect.y + page_rect.h &&
 	    top + block_h[node] > page_rect.y) {
-		if (st->has_bg)
-			ngui_fill_rect(l->ui, (struct ngui_rect){l0, top,
-						 r0 - l0, block_h[node]},
-				       st->bg);
-		if (st->border_w)
-			ngui_frame(l->ui, (struct ngui_rect){l0, top,
-						 r0 - l0, block_h[node]},
-				   st->border_color);
+		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 (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)
+				ngui_frame_round(l->ui, box, rtl, rtr, rbr,
+						 rbl, st->border_w,
+						 st->border_color);
+			else
+				ngui_frame(l->ui, box, st->border_color);
+		}
 	}
 	l->left = l0 + st->pl;
 	l->right = r0 - st->pr;

+ 156 - 1
browser_html.c

@@ -971,7 +971,9 @@ enum {
 	PROP_COLOR, PROP_BG, PROP_SCALE, PROP_BOLD, PROP_ALIGN, PROP_UNDERLINE,
 	PROP_DISPLAY, PROP_MT, PROP_MR, PROP_MB, PROP_ML,
 	PROP_PT, PROP_PR, PROP_PB, PROP_PL,
-	PROP_BORDER_W, PROP_BORDER_COLOR, PROP_LIST_NONE,
+	PROP_BORDER_W, PROP_BORDER_COLOR,
+	PROP_RADIUS_TL, PROP_RADIUS_TR, PROP_RADIUS_BR, PROP_RADIUS_BL,
+	PROP_LIST_NONE,
 	PROP_WIDTH, PROP_WIDTH_KIND, PROP_MAXW, PROP_MAXW_KIND,
 	PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
 	PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
@@ -1899,6 +1901,70 @@ static unsigned int css_skip_nested(const char *s, unsigned int len,
 	return i;
 }
 
+/* One border-radius length.  Unlike parse_size(), percentages keep their
+ * kind: the drawer scales them to the box, not the viewport. */
+static int parse_radius_value(const char *s, unsigned int len,
+			      unsigned int *pos, unsigned short *val,
+			      unsigned char *kind)
+{
+	unsigned int i = *pos;
+	unsigned long v = 0;
+	int digits = 0, fdigits = 0, frac = 0, pct = 0, rem = 0;
+
+	while (i < len && is_space(s[i]))
+		i++;
+	while (i < len && s[i] >= '0' && s[i] <= '9') {
+		v = v * 10 + (unsigned int)(s[i] - '0');
+		digits = 1;
+		i++;
+	}
+	if (i < len && s[i] == '.') {
+		i++;
+		while (i < len && s[i] >= '0' && s[i] <= '9') {
+			fdigits = 1;
+			if (frac < 2) {
+				v = v * 10 + (unsigned int)(s[i] - '0');
+				frac++;
+			}
+			i++;
+		}
+	}
+	while (frac < 2) {
+		v *= 10;
+		frac++;
+	}
+	while (i < len && is_space(s[i]))
+		i++;
+	if (i < len && s[i] == '%') {
+		pct = 1;
+		i++;
+	} else if (i + 2 < len && s[i] == 'r' && s[i + 1] == 'e' &&
+		   s[i + 2] == 'm') {
+		rem = 1;
+		i += 3;
+	} else if (i + 1 < len && s[i] == 'e' && s[i + 1] == 'm') {
+		rem = 2;
+		i += 2;
+	} else if (i + 1 < len && s[i] == 'p' && s[i + 1] == 'x') {
+		i += 2;
+	} else if (i < len && is_ident(s[i])) {
+		return 0;
+	}
+	if (!digits && !fdigits)
+		return 0;
+	if (rem == 1)
+		v *= root_font_px;
+	else if (rem == 2)
+		v *= 16;
+	v /= 100;
+	if (v > 2048)
+		v = 2048;
+	*val = (unsigned short)v;
+	*kind = pct ? 2 : 1;
+	*pos = i;
+	return 1;
+}
+
 static void parse_decls(struct br_doc *d, const char *s,
 			unsigned int len, struct br_decl *out,
 			unsigned int *nout)
@@ -2212,6 +2278,86 @@ static void parse_decls(struct br_doc *d, const char *s,
 				    contains_ci_len(s + vstart, vend - vstart,
 						    "none"));
 			decl_add(out, nout, PROP_LIST_NONE, none, important);
+		} else if (str_eq(prop, "border-radius") ||
+			   str_eq(prop, "border-top-left-radius") ||
+			   str_eq(prop, "border-top-right-radius") ||
+			   str_eq(prop, "border-bottom-right-radius") ||
+			   str_eq(prop, "border-bottom-left-radius")) {
+			unsigned short v[4];
+			unsigned char k[4];
+			unsigned int pos = 0;
+			int n = 0;
+
+			while (pos < vend - vstart && n < 4) {
+				while (pos < vend - vstart &&
+				       is_space(s[vstart + pos]))
+					pos++;
+				if (pos >= vend - vstart ||
+				    s[vstart + pos] == '/')
+					break;
+				if (!parse_radius_value(s + vstart,
+							vend - vstart, &pos,
+							&v[n], &k[n]))
+					break;
+				n++;
+			}
+			if (n) {
+				unsigned short r[4];
+				unsigned char rk[4];
+				int i;
+
+				if (n == 1) {
+					for (i = 0; i < 4; i++) {
+						r[i] = v[0];
+						rk[i] = k[0];
+					}
+				} else if (n == 2) {
+					r[0] = r[2] = v[0];
+					r[1] = r[3] = v[1];
+					rk[0] = rk[2] = k[0];
+					rk[1] = rk[3] = k[1];
+				} else if (n == 3) {
+					r[0] = v[0];
+					r[1] = r[3] = v[1];
+					r[2] = v[2];
+					rk[0] = k[0];
+					rk[1] = rk[3] = k[1];
+					rk[2] = k[2];
+				} else {
+					for (i = 0; i < 4; i++) {
+						r[i] = v[i];
+						rk[i] = k[i];
+					}
+				}
+				for (i = 0; i < 4; i++)
+					r[i] = rk[i] == 2 ? 255 :
+					       (r[i] > 255 ? 255 : r[i]);
+				if (str_eq(prop, "border-top-left-radius")) {
+					decl_add(out, nout, PROP_RADIUS_TL,
+						 (short)r[0], important);
+				} else if (str_eq(prop,
+						  "border-top-right-radius")) {
+					decl_add(out, nout, PROP_RADIUS_TR,
+						 (short)r[0], important);
+				} else if (str_eq(prop,
+						  "border-bottom-right-radius")) {
+					decl_add(out, nout, PROP_RADIUS_BR,
+						 (short)r[0], important);
+				} else if (str_eq(prop,
+						  "border-bottom-left-radius")) {
+					decl_add(out, nout, PROP_RADIUS_BL,
+						 (short)r[0], important);
+				} else {
+					decl_add(out, nout, PROP_RADIUS_TL,
+						 (short)r[0], important);
+					decl_add(out, nout, PROP_RADIUS_TR,
+						 (short)r[1], important);
+					decl_add(out, nout, PROP_RADIUS_BR,
+						 (short)r[2], important);
+					decl_add(out, nout, PROP_RADIUS_BL,
+						 (short)r[3], important);
+				}
+			}
 		} else if (str_eq(prop, "border") ||
 			   str_eq(prop, "border-top") ||
 			   str_eq(prop, "border-right") ||
@@ -3238,6 +3384,13 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_BORDER_COLOR:
 		st->border_color = (unsigned short)dcl->val;
 		break;
+	case PROP_RADIUS_TL:
+	case PROP_RADIUS_TR:
+	case PROP_RADIUS_BR:
+	case PROP_RADIUS_BL:
+		st->radius[dcl->prop - PROP_RADIUS_TL] =
+			(unsigned char)dcl->val;
+		break;
 	case PROP_LIST_NONE:
 		st->no_bullet = (unsigned char)(dcl->val != 0);
 		break;
@@ -3344,6 +3497,8 @@ void br_style_for(struct br_doc *d, unsigned int node,
 	out->float_left = 0;
 	out->border_w = 0;
 	out->border_color = 0;
+	for (i = 0; i < 4; i++)
+		out->radius[i] = 0;
 	out->width = out->max_width = out->height = 0;
 	out->width_kind = out->max_width_kind = out->height_kind = 0;
 	out->mt = out->mr = out->mb = out->ml = 0;

+ 3 - 0
browser_html.h

@@ -96,6 +96,9 @@ struct br_style {
 	unsigned char no_bullet;    /* list-style: none (inherited) */
 	unsigned char uppercase;    /* text-transform: uppercase (inherited) */
 	unsigned char border_w;
+	/* border-radius in pixels, one entry per corner: TL, TR, BR, BL.
+	 * Percentages are stored as 255 so they clamp to the box half. */
+	unsigned char radius[4];
 	unsigned char width_kind;   /* 0 none, 1 px, 2 percent */
 	unsigned short width;
 	unsigned char max_width_kind;

+ 136 - 0
ngui.c

@@ -434,6 +434,142 @@ 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;
+
+	if (v <= 0)
+		return 0;
+	while (bit > v)
+		bit >>= 1;
+	while (bit) {
+		if (v >= r + bit) {
+			v -= r + bit;
+			r = (r >> 1) + bit;
+		} else {
+			r >>= 1;
+		}
+		bit >>= 1;
+	}
+	return r;
+}
+
+/* Horizontal inset of one scanline of a corner with the given radius. */
+static int round_inset(int y, int h, int r)
+{
+	int dy, d2;
+
+	if (r <= 0)
+		return 0;
+	if (y < r)
+		dy = r - y - 1;
+	else if (y >= h - r)
+		dy = y - (h - r);
+	else
+		return 0;
+	d2 = r * r - dy * dy;
+	if (d2 < 0)
+		d2 = 0;
+	return r - ngui_isqrt(d2);
+}
+
+void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
+			  int tl, int tr, int br, int bl,
+			  unsigned short color)
+{
+	int y, half;
+
+	if (rect_empty(&r))
+		return;
+	half = r.w < r.h ? r.w / 2 : r.h / 2;
+	if (tl > half) tl = half;
+	if (tr > half) tr = half;
+	if (br > half) br = half;
+	if (bl > half) bl = half;
+	if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
+		ngui_fill_rect(ui, r, color);
+		return;
+	}
+	for (y = 0; y < r.h; y++) {
+		int li = 0, ri = 0;
+
+		if (y < tl)
+			li = round_inset(y, r.h, tl);
+		if (y < tr)
+			ri = round_inset(y, r.h, tr);
+		if (y >= r.h - bl) {
+			int i = round_inset(y, r.h, bl);
+
+			if (i > li)
+				li = i;
+		}
+		if (y >= r.h - br) {
+			int i = round_inset(y, r.h, br);
+
+			if (i > ri)
+				ri = i;
+		}
+		if (r.w - li - ri > 0)
+			ngui_fill_rect(ui, rect_make(r.x + li, r.y + y,
+					     r.w - li - ri, 1), color);
+	}
+}
+
+void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
+		      int tl, int tr, int br, int bl, int thickness,
+		      unsigned short color)
+{
+	int y, half;
+
+	if (rect_empty(&r))
+		return;
+	if (thickness < 1)
+		thickness = 1;
+	half = r.w < r.h ? r.w / 2 : r.h / 2;
+	if (tl > half) tl = half;
+	if (tr > half) tr = half;
+	if (br > half) br = half;
+	if (bl > half) bl = half;
+	if (tl <= 0 && tr <= 0 && br <= 0 && bl <= 0) {
+		ngui_frame(ui, r, color);
+		return;
+	}
+	for (y = 0; y < r.h; y++) {
+		int li = 0, ri = 0;
+		int x0, x1;
+
+		if (y < tl)
+			li = round_inset(y, r.h, tl);
+		if (y < tr)
+			ri = round_inset(y, r.h, tr);
+		if (y >= r.h - bl) {
+			int i = round_inset(y, r.h, bl);
+
+			if (i > li)
+				li = i;
+		}
+		if (y >= r.h - br) {
+			int i = round_inset(y, r.h, br);
+
+			if (i > ri)
+				ri = i;
+		}
+		x0 = r.x + li;
+		x1 = r.x + r.w - 1 - ri;
+		if (x0 > x1)
+			continue;
+		if (y < thickness || y >= r.h - thickness) {
+			ngui_fill_rect(ui, rect_make(x0, r.y + y,
+					     x1 - x0 + 1, 1), color);
+		} else {
+			ngui_fill_rect(ui, rect_make(x0, r.y + y,
+					     thickness, 1), color);
+			ngui_fill_rect(ui, rect_make(x1 - thickness + 1,
+					     r.y + y, thickness, 1), color);
+		}
+	}
+}
+
 static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
 {
 	if (pixel_visible(ui, x, y))

+ 6 - 0
ngui.h

@@ -161,6 +161,12 @@ void ngui_push_clip(struct ngui *ui, struct ngui_rect r);
 void ngui_pop_clip(struct ngui *ui);
 void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color);
 void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color);
+void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
+			  int tl, int tr, int br, int bl,
+			  unsigned short color);
+void ngui_frame_round(struct ngui *ui, struct ngui_rect r,
+		      int tl, int tr, int br, int bl, int thickness,
+		      unsigned short color);
 void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
 	       unsigned short color);
 void ngui_text(struct ngui *ui, int x, int y, const char *s,