nx_layout_block_horizontal_test.nx source
↩ module page · 180 lines · 6579 B
1// nx_layout_block_horizontal_test.nx -- smoke for the Phase-3
2// horizontal margin/padding axis added to nx_layout_block in-place.
3// Pairs with nx_layout_block_test (vertical-only) which still passes
4// because new cascade lookups default to 0 when unspecified.
5//
6// Fixture (root + 1 child + 1 grandchild):
7//
8// root:
9// padding-left: 10px
10// padding-right: 5px
11// (no margins, no width, no height)
12//
13// child:
14// margin-left: 3px
15// margin-right: 7px
16// (no padding, no explicit width)
17//
18// grandchild:
19// width: 50px (explicit)
20// padding-left: 2px
21//
22// Expected geometry on viewport_w = 800:
23// root.x = 0 + ml(0) = 0
24// root.w = avail(800) (no horizontal margins)
25// root child_avail_w = 800 - pl(10) - pr(5) = 785
26// root content_origin_x = 0 + 10 = 10
27//
28// child.x = content_origin_x(10) + ml(3) = 13
29// child.w = avail(785) - ml(3) - mr(7) = 775
30// child child_avail_w = 775 - pl(0) - pr(0) = 775
31// child content_origin_x = 13 + 0 = 13
32//
33// grand.x = content_origin_x(13) + ml(0) = 13
34// grand.w = 50 (explicit)
35// grand content_origin_x = 13 + pl(2) = 15
36
37import "nx_syscalls.nx"
38import "nx_css_tokenize.nx"
39import "nx_css_parse.nx"
40import "nx_css_selector_match.nx"
41import "nx_css_apply.nx"
42import "nx_css_number_parse.nx"
43import "nx_css_dimension_to_px.nx"
44import "nx_layout_box.nx"
45import "nx_layout_block.nx"
46
47func _put_byte(buf: *u8, i: i64, v: i64) -> i64 {
48 buf[i] = v as u8
49 return 0
50}
51
52func main() -> i64 {
53 let buf: *u8 = (sys_mmap(512)) as *u8
54
55 let table: *LayoutPropTable = (sys_mmap(NX_LAYOUT_PROP_TABLE_BYTES as nx_size)) as *LayoutPropTable
56 nx_layout_prop_table_init(table, buf)
57
58 // Value strings starting at offset 192 (well past table writes
59 // which end at offset 106):
60 // [192..193] "10"
61 // [194] "5"
62 // [195] "3"
63 // [196] "7"
64 // [197..198] "50"
65 // [199] "2"
66 // [200..201] "px"
67 _put_byte(buf, 192, 49) _put_byte(buf, 193, 48) // "10"
68 _put_byte(buf, 194, 53) // "5"
69 _put_byte(buf, 195, 51) // "3"
70 _put_byte(buf, 196, 55) // "7"
71 _put_byte(buf, 197, 53) _put_byte(buf, 198, 48) // "50"
72 _put_byte(buf, 199, 50) // "2"
73 _put_byte(buf, 200, 112) _put_byte(buf, 201, 120) // "px"
74
75 // 6 declarations (2 on root, 2 on child, 2 on grandchild).
76 let n_computed: i64 = 6
77 let computed: *CssComputedDecl = (sys_mmap((n_computed * NX_CSS_COMPUTED_DECL_BYTES) as nx_size)) as *CssComputedDecl
78
79 // root padding-left:10px
80 let c0: *CssComputedDecl = computed
81 c0.element_idx = 0
82 c0.prop_off = table.padding_left_off
83 c0.prop_len = table.padding_left_len
84 c0.val_kind = NX_CSS_VAL_DIMENSION
85 c0.val_off = 192
86 c0.val_len = 2
87 c0.unit_off = 200
88 c0.unit_len = 2
89
90 // root padding-right:5px
91 let c1: *CssComputedDecl = (computed as *u8 + (1 as nx_size) * (NX_CSS_COMPUTED_DECL_BYTES as nx_size)) as *CssComputedDecl
92 c1.element_idx = 0
93 c1.prop_off = table.padding_right_off
94 c1.prop_len = table.padding_right_len
95 c1.val_kind = NX_CSS_VAL_DIMENSION
96 c1.val_off = 194
97 c1.val_len = 1
98 c1.unit_off = 200
99 c1.unit_len = 2
100
101 // child margin-left:3px
102 let c2: *CssComputedDecl = (computed as *u8 + (2 as nx_size) * (NX_CSS_COMPUTED_DECL_BYTES as nx_size)) as *CssComputedDecl
103 c2.element_idx = 1
104 c2.prop_off = table.margin_left_off
105 c2.prop_len = table.margin_left_len
106 c2.val_kind = NX_CSS_VAL_DIMENSION
107 c2.val_off = 195
108 c2.val_len = 1
109 c2.unit_off = 200
110 c2.unit_len = 2
111
112 // child margin-right:7px
113 let c3: *CssComputedDecl = (computed as *u8 + (3 as nx_size) * (NX_CSS_COMPUTED_DECL_BYTES as nx_size)) as *CssComputedDecl
114 c3.element_idx = 1
115 c3.prop_off = table.margin_right_off
116 c3.prop_len = table.margin_right_len
117 c3.val_kind = NX_CSS_VAL_DIMENSION
118 c3.val_off = 196
119 c3.val_len = 1
120 c3.unit_off = 200
121 c3.unit_len = 2
122
123 // grandchild width:50px
124 let c4: *CssComputedDecl = (computed as *u8 + (4 as nx_size) * (NX_CSS_COMPUTED_DECL_BYTES as nx_size)) as *CssComputedDecl
125 c4.element_idx = 2
126 c4.prop_off = table.width_off
127 c4.prop_len = table.width_len
128 c4.val_kind = NX_CSS_VAL_DIMENSION
129 c4.val_off = 197
130 c4.val_len = 2
131 c4.unit_off = 200
132 c4.unit_len = 2
133
134 // grandchild padding-left:2px
135 let c5: *CssComputedDecl = (computed as *u8 + (5 as nx_size) * (NX_CSS_COMPUTED_DECL_BYTES as nx_size)) as *CssComputedDecl
136 c5.element_idx = 2
137 c5.prop_off = table.padding_left_off
138 c5.prop_len = table.padding_left_len
139 c5.val_kind = NX_CSS_VAL_DIMENSION
140 c5.val_off = 199
141 c5.val_len = 1
142 c5.unit_off = 200
143 c5.unit_len = 2
144
145 // Resolve context.
146 let resolve: *CssResolveCtx = (sys_mmap(NX_CSS_RESOLVE_CTX_BYTES as nx_size)) as *CssResolveCtx
147 resolve.root_font_size_px = 16
148 resolve.parent_font_size_px = 16
149 resolve.parent_dimension_px = 800
150
151 // 3-box tree.
152 let max_boxes: i64 = 4
153 let boxes: *LayoutBox = (sys_mmap((max_boxes * NX_LAYOUT_BOX_BYTES) as nx_size)) as *LayoutBox
154 let tree: *LayoutTree = (sys_mmap(NX_LAYOUT_TREE_BYTES as nx_size)) as *LayoutTree
155 nx_layout_tree_init(tree, boxes, max_boxes)
156 let root_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 0)
157 let child_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 1)
158 let grand_idx: i64 = nx_layout_box_append(tree, NX_LAYOUT_BOX_BLOCK, 2)
159 nx_layout_box_attach_child(tree, root_idx, child_idx)
160 nx_layout_box_attach_child(tree, child_idx, grand_idx)
161
162 // Layout.
163 let ctx: *LayoutCtx = (sys_mmap(NX_LAYOUT_CTX_BYTES as nx_size)) as *LayoutCtx
164 nx_layout_ctx_init(ctx, tree, computed, n_computed, buf, 800, resolve)
165 nx_layout_block_layout(ctx, table, root_idx)
166
167 let root: *LayoutBox = boxes
168 if root.x != 0 { return 1 }
169 if root.w != 800 { return 2 }
170
171 let child: *LayoutBox = (boxes as *u8 + (1 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox
172 if child.x != 13 { return 10 } // content_origin_x(10) + ml(3)
173 if child.w != 775 { return 11 } // avail(785) - ml(3) - mr(7)
174
175 let grand: *LayoutBox = (boxes as *u8 + (2 as nx_size) * (NX_LAYOUT_BOX_BYTES as nx_size)) as *LayoutBox
176 if grand.x != 13 { return 20 } // child's content_origin_x = child.x + child.pl(0)
177 if grand.w != 50 { return 21 } // explicit width
178
179 return 0
180}