Explorar o código

browser: auto table layout for overflowing columns, <br> empty lines

Hacker News comment pages laid out 5000 px wide:
- A one-column fallback table row only ever scaled its cell up to the
  table width; a cell whose one-line text is wider is now scaled down.
- When the columns' one-line widths do not fit, both table paths
  (layout_table and the per-row table_columns) give each column its
  min-content width plus a share of the rest in proportion to
  max - min, like CSS auto table layout, so long text cells wrap.
  intrinsic_width() gained a min-content mode (iw_min).
- A <br> on an empty line is one empty line; HN's comment header
  (margin-bottom: -10px followed by <br>) no longer overlaps the text,
  and <br><br> after a block leaves its blank lines.

The compat ladder, news.ycombinator.com, docs.cloudron.io, google.com,
getbootstrap 3.4, lobste.rs and heise.de render pixel-identical;
cloudron.io gains the two blank lines its hero <br><br> asks for.
Johannes Zellner hai 13 horas
pai
achega
b70c54bedc
Modificáronse 3 ficheiros con 116 adicións e 21 borrados
  1. 7 1
      AGENTS.md
  2. 0 0
      docs/userland.html
  3. 109 20
      user/apps/browser/browser.c

+ 7 - 1
AGENTS.md

@@ -1819,7 +1819,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   grids and `repeat(N, 1fr)`/`repeat(N, minmax(0,1fr))` equal-column grids
   (Tailwind `grid-cols-N`; `grid_auto` = `BR_GRID_FIXED` + N), `aspect-ratio`
   as the preferred height of an auto-height box (`aspect-video`/
-  `aspect-square` frames), table colspan and percentage columns, and basic
+  `aspect-square` frames), table colspan and percentage columns (columns
+  whose one-line widths do not fit get their min-content width plus a share
+  of the rest in proportion to max - min, as in CSS auto table layout, and a
+  one-column fallback row is scaled down to the table as well as up: a
+  Hacker News comment page used to lay out 5000 px wide; fallback rows
+  cache the table's column widths per pass, which removed an O(rows^2)
+  text measurement), a `<br>` on an empty line is one empty line, and basic
   `float: left/right` columns.  Inline and inline-block `<svg>` icons are
   line runs like `<img>` (`RUN_SVG`), and an inline-block holding only
   inline text with no box of its own stays in the text line instead of

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
docs/userland.html


+ 109 - 20
user/apps/browser/browser.c

@@ -10862,6 +10862,15 @@ static int text_space(char c)
 	return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
 }
 
+/* intrinsic_width() measures the max-content width (everything on one
+ * line).  With iw_min set it measures the min-content width instead: every
+ * word, image or inline box is its own line, so the result is the widest
+ * unbreakable piece (CSS auto table layout needs both). */
+static int iw_min;
+#define IW_ADD(w) do { int iw_w_ = (w); \
+	if (iw_min) { if (iw_w_ > max_w) max_w = iw_w_; } \
+	else line_w += iw_w_; } while (0)
+
 static int intrinsic_width(struct ngui *ui, unsigned int node,
 			   const struct br_style *pst, int depth, int base)
 {
@@ -10876,8 +10885,8 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 
 		/* Icon boxes (::before/::after with a size) are inline
 		 * content of the element. */
-		line_w += pseudo_box_w(node, pst, 1, &ps, base) +
-			  pseudo_box_w(node, pst, 2, &ps, base);
+		IW_ADD(pseudo_box_w(node, pst, 1, &ps, base) +
+			  pseudo_box_w(node, pst, 2, &ps, base));
 	}
 	for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
 		struct br_node *nd = &doc.nodes[c];
@@ -10888,7 +10897,7 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			unsigned int tn = nd->text_len;
 
 			if (doc.nodes[node].tag == BR_TAG_PRE) {
-				line_w += (int)face_text_width(ui, pst, t, tn);
+				IW_ADD((int)face_text_width(ui, pst, t, tn));
 			} else {
 				unsigned int ti = 0;
 				int first = 1;
@@ -10906,10 +10915,10 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 					if (ti == ts)
 						break;
 					if (!first || lead)
-						line_w += sp;
+						IW_ADD(sp);
 					lead = 0;
-					line_w += (int)face_text_width(ui, pst,
-						t + ts, ti - ts);
+					IW_ADD((int)face_text_width(ui, pst,
+						t + ts, ti - ts));
 					first = 0;
 				}
 			}
@@ -10948,11 +10957,11 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			if (cs.width_kind) {
 				int ww = style_width_px(&cs, base);
 
-				line_w += ww > 0 ? ww : 64;
+				IW_ADD(ww > 0 ? ww : 64);
 			} else if (nd->width_attr) {
-				line_w += nd->width_attr;
+				IW_ADD(nd->width_attr);
 			} else if (nd->height_attr && iw && ih) {
-				line_w += iw * (int)nd->height_attr / ih;
+				IW_ADD(iw * (int)nd->height_attr / ih);
 			} else if (cs.height_kind && iw && ih) {
 				/* A percentage height needs the parent's
 				 * definite height (the logo's 2rem box), as in
@@ -10964,11 +10973,11 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 					 (int)pst->height * (int)cs.height / 100 :
 					 0;
 
-				line_w += hh > 0 ? iw * hh / ih : iw;
+				IW_ADD(hh > 0 ? iw * hh / ih : iw);
 			} else if (iw) {
-				line_w += iw;
+				IW_ADD(iw);
 			} else {
-				line_w += 64;
+				IW_ADD(64);
 			}
 			continue;
 		}
@@ -10983,9 +10992,9 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			if (s_eq(br_type(&doc, c), "checkbox") ||
 			    s_eq(br_type(&doc, c), "radio"))
 				fw = 16;
-			line_w += (fw > 0 ? fw : 120) +
+			IW_ADD((fw > 0 ? fw : 120) +
 				  mp_px(&cs, 3, base) + mp_px(&cs, 1, base) +
-				  mp_px(&cs, 7, base) + mp_px(&cs, 5, base);
+				  mp_px(&cs, 7, base) + mp_px(&cs, 5, base));
 			continue;
 		}
 		if (nd->tag == BR_TAG_SVG) {
@@ -10998,7 +11007,7 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			sw += mp_px(&cs, 3, base) + mp_px(&cs, 1, base);
 			if (flex_row || cs.display == BR_DISPLAY_INLINE ||
 			    cs.display == BR_DISPLAY_INLINE_BLOCK) {
-				line_w += sw;
+				IW_ADD(sw);
 			} else {
 				if (line_w > max_w)
 					max_w = line_w;
@@ -11030,7 +11039,7 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			if (flex_row || cs.float_left ||
 			    cs.display == BR_DISPLAY_INLINE ||
 			    cs.display == BR_DISPLAY_INLINE_BLOCK) {
-				line_w += cw;
+				IW_ADD(cw);
 			} else {
 				if (line_w > max_w)
 					max_w = line_w;
@@ -12243,11 +12252,12 @@ static void table_columns_compute(struct lay *l, unsigned int table,
 {
 	unsigned int r, c;
 	int cols = 0, total = 0, fixed_sum = 0, free_raw = 0, free_cols = 0;
-	int fixed[12];
+	int fixed[12], rmin[12];
 
 	for (c = 0; c < (unsigned int)maxcols; c++) {
 		raw[c] = 0;
 		fixed[c] = 0;
+		rmin[c] = 0;
 	}
 	/* Intrinsic widths of single-column cells. */
 	for (r = doc.nodes[table].first; r; r = doc.nodes[r].next) {
@@ -12282,8 +12292,20 @@ static void table_columns_compute(struct lay *l, unsigned int table,
 			if (w < 24)
 				w = 24;
 			if (span == 1) {
+				int wm;
+
 				if (w > raw[col])
 					raw[col] = w;
+				/* min-content: the widest word/image */
+				iw_min = 1;
+				wm = intrinsic_width(l->ui, c, &cs, 0, avail);
+				iw_min = 0;
+				wm += w - intrinsic_width(l->ui, c, &cs, 0,
+							  avail);
+				if (wm > w)
+					wm = w;
+				if (wm > rmin[col])
+					rmin[col] = wm;
 			}
 			if (col + span > cols)
 				cols = col + span;
@@ -12396,6 +12418,29 @@ static void table_columns_compute(struct lay *l, unsigned int table,
 		total = 0;
 		for (i = 0; i < cols; i++)
 			total += raw[i];
+	} else if (total > avail && !fixed_sum) {
+		/* CSS auto table layout: the one-line widths do not fit, so
+		 * every column gets its min-content width plus a share of the
+		 * rest in proportion to (max - min).  A Hacker News comment
+		 * cell (a paragraph measured as one line) used to keep its
+		 * whole one-line width and push the table off the page. */
+		int i, smin = 0, sdiff = 0;
+
+		for (i = 0; i < cols; i++) {
+			if (rmin[i] < 8)
+				rmin[i] = 8;
+			if (rmin[i] > raw[i])
+				rmin[i] = raw[i];
+			smin += rmin[i];
+			sdiff += raw[i] - rmin[i];
+		}
+		for (i = 0; i < cols; i++)
+			raw[i] = rmin[i] + (smin < avail && sdiff > 0 ?
+				 (int)((long long)(avail - smin) *
+				       (raw[i] - rmin[i]) / sdiff) : 0);
+		total = 0;
+		for (i = 0; i < cols; i++)
+			total += raw[i];
 	} else if (total < avail && total > 0) {
 		int i;
 
@@ -12530,8 +12575,12 @@ static void layout_table_row(struct lay *l, unsigned int row,
 		}
 		/* A one-column table still has to fill its own width; the
 		 * intrinsic fallback above otherwise leaves the cell at its
-		 * text width (HN's outer table and nested story table). */
-		if (total > 0 && total < avail) {
+		 * text width (HN's outer table and nested story table).  The
+		 * same holds the other way: cells whose one-line text is
+		 * wider than the table are scaled down to it, or a HN
+		 * comment page's one-column box became 5000 px wide and
+		 * every comment ran off the page. */
+		if (total > 0 && total != avail) {
 			int c;
 
 			for (c = 0; c < ncell; c++)
@@ -12640,6 +12689,7 @@ static void layout_table(struct lay *l, unsigned int table,
 	unsigned int rows[TBL_MAX_ROWS];
 	int rowh[TBL_MAX_ROWS], rowy[TBL_MAX_ROWS];
 	int raw[TBL_MAX_COLS], prefix[TBL_MAX_COLS + 1], fixed[TBL_MAX_COLS];
+	int rmin[TBL_MAX_COLS];
 	int nrows = 0, ncells = 0, cols = 0, total = 0, fixed_sum = 0;
 	int free_raw = 0, free_cols = 0;
 	int table_left = l->left, table_right = l->right;
@@ -12725,6 +12775,7 @@ static void layout_table(struct lay *l, unsigned int table,
 	for (i = 0; i < cols; i++) {
 		raw[i] = 0;
 		fixed[i] = 0;
+		rmin[i] = 0;
 	}
 	/* Intrinsic widths of single-column cells. */
 	for (i = 0; i < ncells; i++) {
@@ -12743,10 +12794,22 @@ static void layout_table(struct lay *l, unsigned int table,
 		if (w < 24)
 			w = 24;
 		if (tbl_cells[i].colspan == 1) {
-			int col = tbl_cells[i].col;
+			int col = tbl_cells[i].col, wm;
 
 			if (w > raw[col])
 				raw[col] = w;
+			/* min-content: the widest word/image, plus the same
+			 * padding/borders as w */
+			iw_min = 1;
+			wm = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0,
+					     avail);
+			iw_min = 0;
+			wm += w - intrinsic_width(l->ui, tbl_cells[i].node,
+						  &cs, 0, avail);
+			if (wm > w)
+				wm = w;
+			if (wm > rmin[col])
+				rmin[col] = wm;
 		}
 	}
 	/* A spanning cell only needs the sum of its columns to be large
@@ -12816,6 +12879,25 @@ static void layout_table(struct lay *l, unsigned int table,
 				if (!fixed[i])
 					raw[i] = free_avail / free_cols;
 		}
+	} else if (total > avail && !fixed_sum) {
+		/* CSS auto table layout, as in table_columns_compute(): the
+		 * min-content widths plus the rest in proportion to
+		 * (max - min), so a long paragraph wraps instead of pushing
+		 * the table off the page. */
+		int smin = 0, sdiff = 0;
+
+		for (i = 0; i < cols; i++) {
+			if (rmin[i] < 8)
+				rmin[i] = 8;
+			if (rmin[i] > raw[i])
+				rmin[i] = raw[i];
+			smin += rmin[i];
+			sdiff += raw[i] - rmin[i];
+		}
+		for (i = 0; i < cols; i++)
+			raw[i] = rmin[i] + (smin < avail && sdiff > 0 ?
+				 (int)((long long)(avail - smin) *
+				       (raw[i] - rmin[i]) / sdiff) : 0);
 	} else if (total < avail && total > 0) {
 		for (i = 0; i < cols; i++)
 			raw[i] = raw[i] * avail / total;
@@ -13293,6 +13375,13 @@ static void layout_children(struct lay *l, unsigned int parent,
 			continue;
 		}
 		if (nd->tag == BR_TAG_BR) {
+			/* A <br> on an empty line is itself one empty line
+			 * (<br><br> leaves a blank line; HN's comment header
+			 * has margin-bottom: -10px and relies on the <br>
+			 * after it to keep clear of the text). */
+			if (l->nruns == 0)
+				l->y += (pst->line_h_px ? pst->line_h_px :
+					 pst->font_px + pst->font_px / 5) + 2;
 			flush_line(l);
 			n = nd->next;
 			continue;

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio