nx_browser_demo_html.nx source
↩ module page · 106 lines · 4079 B
1// nx_browser_demo_html.nx -- end-to-end HTML-input demo. Tokenizes
2// "<body>42</body>", builds a LayoutBox tree via nx_layout_from_dom,
3// positions the text box manually, runs the integrated paint walk
4// with text rendering, dumps to stdout.
5//
6// Demonstrates the full bits-up pipeline:
7// HTML bytes
8// -> nx_html_tokenizer (Phase 1)
9// -> nx_layout_from_dom (Phase 3)
10// -> nx_paint_walk_layout (Phase 4, with text rendering)
11// -> nx_paint_fb_ascii_dump (Phase 5)
12// -> sys_write stdout
13//
14// No Chromium / WebKit / Blink / Servo path anywhere. All RV64 nx.
15
16import "nx_syscalls.nx"
17import "nx_css_tokenize.nx"
18import "nx_css_apply.nx"
19import "nx_css_color_decode.nx"
20import "nx_html_tokenizer.nx"
21import "nx_layout_box.nx"
22import "nx_layout_default_display.nx"
23import "nx_layout_from_dom.nx"
24import "nx_paint_solid_rect.nx"
25import "nx_paint_text.nx"
26import "nx_paint_walk_layout.nx"
27import "nx_paint_fb_ascii_dump.nx"
28
29func _put_byte(buf: *u8, i: i64, v: i64) -> i64 {
30 buf[i] = v as u8
31 return 0
32}
33
34func main() -> i64 {
35 // Build HTML source: "<body>42</body>" (15 bytes).
36 let html: *u8 = (sys_mmap(32)) as *u8
37 var p: i64 = 0
38 _put_byte(html, p, 60) p = p + 1 // '<'
39 _put_byte(html, p, 98) p = p + 1 // 'b'
40 _put_byte(html, p, 111) p = p + 1 // 'o'
41 _put_byte(html, p, 100) p = p + 1 // 'd'
42 _put_byte(html, p, 121) p = p + 1 // 'y'
43 _put_byte(html, p, 62) p = p + 1 // '>'
44 _put_byte(html, p, 52) p = p + 1 // '4'
45 _put_byte(html, p, 50) p = p + 1 // '2'
46 _put_byte(html, p, 60) p = p + 1 // '<'
47 _put_byte(html, p, 47) p = p + 1 // '/'
48 _put_byte(html, p, 98) p = p + 1 // 'b'
49 _put_byte(html, p, 111) p = p + 1 // 'o'
50 _put_byte(html, p, 100) p = p + 1 // 'd'
51 _put_byte(html, p, 121) p = p + 1 // 'y'
52 _put_byte(html, p, 62) p = p + 1 // '>'
53 let html_len: i64 = p
54
55 // Tokenize + build LayoutBox tree.
56 let max_boxes: i64 = 16
57 let boxes: *LayoutBox = (sys_mmap((max_boxes * NX_LAYOUT_BOX_BYTES) as nx_size)) as *LayoutBox
58 let tree: *LayoutTree = (sys_mmap(NX_LAYOUT_TREE_BYTES as nx_size)) as *LayoutTree
59 nx_layout_tree_init(tree, boxes, max_boxes)
60
61 let stk_idx: *i64 = (sys_mmap(64)) as *i64
62 let stk: *LayoutFromDomStack = (sys_mmap(NX_LAYOUT_FROM_DOM_STACK_BYTES as nx_size)) as *LayoutFromDomStack
63 nx_layout_from_dom_stack_init(stk, stk_idx, 8)
64
65 let root: i64 = nx_layout_from_dom(html, html_len, tree, stk)
66 if root != 0 { return 1 }
67
68 // Tree should be: root(0) -> body(1) -> text(2)
69 // Manually position the text box (no nx_layout_block invocation here).
70 let text_box: *LayoutBox = (boxes as *u8 + (2 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox
71 text_box.x = 2
72 text_box.y = 2
73
74 // Framebuffer.
75 let w: i64 = 24
76 let h: i64 = 10
77 let pixels: *u8 = (sys_mmap((w * h * 4) as nx_size)) as *u8
78 let fb: *Framebuffer = (sys_mmap(NX_FRAMEBUFFER_BYTES as nx_size)) as *Framebuffer
79 nx_framebuffer_init(fb, pixels, w, h)
80 nx_framebuffer_clear(fb)
81
82 // Paint context. Empty cascade since this fixture has no CSS.
83 let computed: *CssComputedDecl = (sys_mmap(NX_CSS_COMPUTED_DECL_BYTES as nx_size)) as *CssComputedDecl
84 let table_buf: *u8 = (sys_mmap(64)) as *u8
85 let table: *PaintPropTable = (sys_mmap(NX_PAINT_PROP_TABLE_BYTES as nx_size)) as *PaintPropTable
86 nx_paint_prop_table_init(table, table_buf)
87
88 let ctx: *PaintCtx = (sys_mmap(NX_PAINT_CTX_BYTES as nx_size)) as *PaintCtx
89 nx_paint_ctx_init(ctx, fb, tree, computed, 0, html, table)
90
91 let white: *CssColor = (sys_mmap(NX_CSS_COLOR_BYTES as nx_size)) as *CssColor
92 white.r = 255
93 white.g = 255
94 white.b = 255
95 white.a = 255
96 nx_paint_ctx_set_text(ctx, html, white)
97
98 nx_paint_walk_layout(ctx, root)
99
100 // ASCII dump to stdout.
101 let out_buf: *u8 = (sys_mmap(512)) as *u8
102 let n: i64 = nx_paint_fb_ascii_dump(fb, out_buf, 512)
103 if n < 0 { return 2 }
104 sys_write(1, out_buf, n)
105 return 0
106}