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

Fix browser font shorthand and back large arenas with the heap

A failed size parse on a font-family token no longer erases the already parsed size, and the node/text/attribute arenas move from static BSS to the ring-3 heap so large articles can use bigger caps without overflowing the 4 MB user window. Adds table-layout and font-shorthand ladder pages.
Johannes Zellner 1 неделя назад
Родитель
Сommit
3aedf05ba1

+ 7 - 3
browser.c

@@ -516,6 +516,11 @@ static void heap_clear(void)
 	memset(font_faces, 0, sizeof(font_faces));
 	ngui_font_detach(&ui);
 	heap_reset();
+	/* The document arenas live in that heap; drop the stale pointers so
+	 * the next br_parse_plain() allocates fresh ones. */
+	doc.nodes = 0;
+	doc.text = 0;
+	doc.attrs = 0;
 	image_io_buf = 0;
 	page_backbuf = 0;
 	pagebuf_w = pagebuf_h = 0;
@@ -3196,7 +3201,6 @@ static void load_page(const char *url, int local, int push)
 	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;
@@ -3218,6 +3222,7 @@ static void load_page(const char *url, int local, int push)
 		show_error("Out of memory", "Cannot allocate the page buffer");
 		return;
 	}
+	br_parse_plain(&doc, "", 0);    /* blank while loading */
 	ngui_font_attach(&ui);
 	redraw_now();
 	browser_rules_alloc();
@@ -3337,7 +3342,6 @@ static void load_home(int push)
 	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));
@@ -3354,6 +3358,7 @@ static void load_home(int push)
 		show_error("Out of memory", "Cannot allocate the browser heap");
 		return;
 	}
+	br_parse_plain(&doc, "", 0);
 	ngui_font_attach(&ui);
 	redraw_now();
 	browser_rules_alloc();
@@ -8150,7 +8155,6 @@ void _start(const char *arg)
 	}
 	ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
 	url_in.placeholder = "http://...";
-	br_doc_init(&doc);
 	br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
 		     ui.theme.accent);
 	br_set_viewport(wi.width);

+ 35 - 11
browser_html.c

@@ -3,16 +3,18 @@
  * implements the properties in the browser's documented subset. */
 
 #include "browser_html.h"
+#include <stdlib.h>
 #include <string.h>
 
 static int is_space(char c);
 static int is_ident(char c);
 
-/* ---- storage (one document per process) ---- */
-
-static struct br_node nodes_store[BR_MAX_NODES];
-static char text_store[BR_TEXT_SIZE];
-static char attr_store[BR_ATTR_SIZE];
+/* ---- storage (one document per process) ----
+ *
+ * The node/text/attribute arenas are allocated from the ring-3 heap, not
+ * static BSS: a large article needs several MB and the process image lives
+ * in the 4 MB user window.  heap_reset() on navigation releases them with
+ * the rest of the page heap. */
 static struct br_rule rule_fallback[BR_RULE_FALLBACK];
 static char css_store[BR_CSS_SIZE];
 static int root_font_px = 16;
@@ -791,9 +793,18 @@ void br_doc_init(struct br_doc *d)
 	for (i = 0; i < sizeof(*d); i++)
 		((unsigned char *)d)[i] = 0;
 	rule_index_reset(d);
-	d->nodes = nodes_store;
-	d->text = text_store;
-	d->attrs = attr_store;
+	d->nodes = malloc(BR_MAX_NODES * sizeof(*d->nodes));
+	d->text = malloc(BR_TEXT_SIZE);
+	d->attrs = malloc(BR_ATTR_SIZE);
+	if (!d->nodes || !d->text || !d->attrs) {
+		free(d->nodes);
+		free(d->text);
+		free(d->attrs);
+		d->nodes = 0;
+		d->text = 0;
+		d->attrs = 0;
+		return;
+	}
 	d->rules = rule_fallback;
 	d->rule_cap = BR_RULE_FALLBACK;
 	d->attrs_used = 1;      /* offset 0 = "no attribute" */
@@ -4205,9 +4216,16 @@ static void add_font_shorthand(struct br_decl *out, unsigned int *nout,
 				continue;
 			}
 		}
-		if (!parse_font_size(v + ts, slash < tl ? slash : tl,
-				    &size_raw, &size_unit))
-			continue;
+		{
+			unsigned short raw;
+			unsigned char unit;
+
+			if (!parse_font_size(v + ts, slash < tl ? slash : tl,
+					    &raw, &unit))
+				continue;
+			size_raw = raw;
+			size_unit = unit;
+		}
 		have_size = 1;
 		if (slash < tl) {
 			unsigned int lstart = ts + slash + 1;
@@ -7096,6 +7114,8 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 		return -1;
 	if (!d->nodes)
 		br_doc_init(d);
+	if (!d->nodes || !d->text || !d->attrs)
+		return -1;
 	doc_reset(d);
 	d->script_src = html;
 	stack[0] = 0;
@@ -7369,6 +7389,8 @@ int br_parse_plain(struct br_doc *d, const char *text, unsigned int len)
 {
 	if (!d->nodes)
 		br_doc_init(d);
+	if (!d->nodes || !d->text || !d->attrs)
+		return -1;
 	doc_reset(d);
 	d->plain = 1;
 	d->nodes[0].type = BR_NODE_ELEM;
@@ -7386,6 +7408,8 @@ unsigned int br_parse_image_doc(struct br_doc *d, const char *src)
 
 	if (!d->nodes)
 		br_doc_init(d);
+	if (!d->nodes || !d->text || !d->attrs)
+		return 0;
 	doc_reset(d);
 	d->nodes[0].type = BR_NODE_ELEM;
 	d->nodes[0].tag = BR_TAG_UNKNOWN;

+ 5 - 5
browser_html.h

@@ -6,11 +6,11 @@
 #ifndef BROWSER_HTML_H
 #define BROWSER_HTML_H
 
-#define BR_MAX_NODES     6144
-#define BR_TEXT_SIZE     65536
-#define BR_ATTR_SIZE     524288
-#define BR_CSS_SIZE      12288
-#define BR_MAX_RULES     4608
+#define BR_MAX_NODES     16384
+#define BR_TEXT_SIZE     524288
+#define BR_ATTR_SIZE     1048576
+#define BR_CSS_SIZE      65536
+#define BR_MAX_RULES     8192
 #define BR_RULE_FALLBACK 256
 #define BR_MAX_FACES     8
 #define BR_FACE_FAMILY   40

+ 63 - 0
testdata/browser/compat/049-table-layout.html

@@ -0,0 +1,63 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>049 table width and alignment</title>
+<style>
+  body { background: #ffffff; color: #202020; font-family: sans-serif;
+         margin: 8px; }
+  table { border: 1px solid #404040; border-collapse: collapse; }
+  td { padding: 4px; font-size: 16px; }
+</style>
+</head>
+<body>
+<h1>049 &mdash; table width and alignment</h1>
+<p>The outer table is 85% of the body; its single-column cell fills that
+   width. The nested table is 100% of the cell. Cell text stays left-aligned
+   even inside <code>&lt;center&gt;</code>, and <code>align="right"</code>
+   right-aligns the rank column.</p>
+
+<center>
+<table id="outer" width="85%">
+<tr><td id="outercell" style="background: #ffdddd">OUTER CELL FILLS 85%</td></tr>
+<tr><td>
+  <table id="story" width="100%">
+  <tr>
+    <td id="rank" align="right" style="background: #ffeecc">1.</td>
+    <td style="background: #eeeeff">vote</td>
+    <td style="background: #eeffee">NESTED TITLE FILLS THE CELL</td>
+  </tr>
+  </table>
+</td></tr>
+</table>
+</center>
+
+<div id="result" style="background: #ffc8c8; color: #000000; padding: 8px;
+     font-size: 18px;">checking...</div>
+<script>
+setTimeout(function () {
+  var body = document.body.getBoundingClientRect();
+  var outer = document.getElementById('outer').getBoundingClientRect();
+  var cell = document.getElementById('outercell').getBoundingClientRect();
+  var story = document.getElementById('story').getBoundingClientRect();
+  var rank = document.getElementById('rank').getBoundingClientRect();
+  var want = body.width * 85 / 100;
+  var ok = Math.abs(outer.width - want) <= 12;
+  ok = ok && Math.abs(cell.width - outer.width) <= 12;
+  ok = ok && Math.abs(story.width - outer.width) <= 16;
+  ok = ok && rank.width < outer.width / 2;
+
+  var r = document.getElementById('result');
+  if (r) {
+    r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
+    r.textContent = (ok ? 'TABLE-OK ' : 'TABLE-FAIL ') +
+                    'body=' + Math.round(body.width) +
+                    ' outer=' + Math.round(outer.width) +
+                    ' cell=' + Math.round(cell.width) +
+                    ' story=' + Math.round(story.width) +
+                    ' rank=' + Math.round(rank.width);
+  }
+}, 300);
+</script>
+</body>
+</html>

+ 40 - 0
testdata/browser/compat/050-font-shorthand.html

@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>050 font shorthand</title>
+<style>
+  html { font-size: 10px; }
+  body { background: #ffffff; color: #202020; font-family: sans-serif; }
+  #a { font: 20px/1 Verdana, sans-serif; }
+  #b { font: bold 22px/1.4 monospace; }
+  #c { font: italic 18px/1.2 Verdana; }
+  #d { font-size: 24px; }
+</style>
+</head>
+<body>
+<h1>050 &mdash; font shorthand</h1>
+<p>A family token after the size must not erase the parsed size, and the
+   line-height/weight/style tokens must survive.</p>
+<div id="a">A: 20px family</div>
+<div id="b">B: bold 22px mono</div>
+<div id="c">C: italic 18px</div>
+<div id="d">D: 24px longhand control</div>
+<div id="result" style="background: #ffc8c8; color: #000000; padding: 8px;
+     margin-top: 12px; font-size: 18px;">checking...</div>
+<script>
+setTimeout(function () {
+  function size(id) { return getComputedStyle(document.getElementById(id)).fontSize; }
+  var a = size('a'), b = size('b'), c = size('c'), d = size('d');
+  var ok = a === '20px' && b === '22px' && c === '18px' && d === '24px';
+
+  var r = document.getElementById('result');
+  if (r) {
+    r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
+    r.textContent = (ok ? 'FONT-OK ' : 'FONT-FAIL ') +
+                    'a=' + a + ' b=' + b + ' c=' + c + ' d=' + d;
+  }
+}, 300);
+</script>
+</body>
+</html>

+ 1 - 1
testdata/browser/compat/README

@@ -17,7 +17,7 @@ Serve this directory from the host and open it on the board:
   browser http://<host>:8000/            # board
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
 
-Status: 010-048 and 030a pass.  036a needs the POST echo server:
+Status: 010-050 and 030a pass.  036a needs the POST echo server:
 
   python3 testdata/browser/compat/post_server.py
   browser /floppy1/036a.htm      # action is http://10.0.2.2:8765/echo

+ 2 - 0
testdata/browser/compat/index.html

@@ -71,6 +71,8 @@ a { color: #0645ad; }
 <tr><td>046</td><td><a href="046-flex-footer-query.html">flex footer and queries</a></td><td>space-evenly columns, padded one-line button, query selectors</td><td class="pass">PASS</td></tr>
 <tr><td>047</td><td><a href="047-script-dom-height.html">script DOM and 100% height</a></td><td>script/style elements exist in the DOM; html/body 100% height still scrolls to the footer</td><td class="pass">PASS</td></tr>
 <tr><td>048</td><td><a href="048-link-pseudo.html">link pseudo-class</a></td><td><code>a:link</code> colour applies; visited/hover do not stick</td><td class="pass">PASS</td></tr>
+<tr><td>049</td><td><a href="049-table-layout.html">table width and alignment</a></td><td>legacy width attributes, one-column stretch, nested 100%, cell alignment</td><td class="pass">PASS</td></tr>
+<tr><td>050</td><td><a href="050-font-shorthand.html">font shorthand</a></td><td>size with a family token, bold/italic and line-height</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>