dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / layout / tests_layout_inline.lucb
#==============================================================================================
#
# tests_layout_inline - inline layout (line boxes and their fragments) of parsed documents,
# pinned to the reference build
#
# DESCRIPTION:
# Runs the cases of tests_layout_inline_cases through the port with the harness of
# tests_layout_geometry. They are Ladybird's own Tests/LibWeb/Layout/input/block-and-inline
# tests with text (no script, no external resources); the local oracle that wrote the
# table (luce-browser-tools, oracles/luce-browser-engine/layout_geometry, cases_inline.txt)
# reproduces each one's expected layout dump exactly. Each case lays out a document with
# inline formatting contexts: text chunks shaped with the test font, line breaking,
# collapsible whitespace, inline boxes with their margins, borders and padding,
# inline-blocks, vertical-align, text-align, white-space, text-indent, floats beside lines
# and the static position of absolutely positioned boxes.
#
#==============================================================================================
# mark: The "lines" mode ========================================================================
## The index of `node` in the pre-order of the tree under `root`.
func test_layout_inline_node_index(root: LayoutNode*, node: LayoutNode*) -> usize:
var index: usize = 0
var found: usize = 0
for candidate in layout_node_inclusive_subtree(root):
if candidate == node:
found = index
index += 1
return found
## The line boxes the layout state has for every block container, in pre-order, with their fragments and glyph runs
## (the oracle's dump_line_boxes, which reads them before commit). Glyph floats are printed with `{}` of their f32, as
## the oracle prints them.
func test_layout_inline_dump_line_boxes(out: ak.StringBuilder*, state: const LayoutState*, root: LayoutNode*):
var builder = ak.string_builder_new()
var node_index: usize = 0
for node in layout_node_inclusive_subtree(root):
let index = node_index
node_index += 1
let block = as_if_layout_block_container(node) else continue
let used_values = layout_state_try_get_layout_node_with_style(state, block.layout_node_with_style()) else continue
if ak.vector_is_empty(&used_values.line_boxes):
continue
dump_appendff(&builder, "{} #{}: {} line boxes\n", [ak.arg_string_view(dump_class_name(node.cell())), ak.arg_u64((u64)index), ak.arg_u64((u64)ak.vector_size(&used_values.line_boxes))])
for line_index in 0..<ak.vector_size(&used_values.line_boxes):
let line_box = ak.vector_at_const(&used_values.line_boxes, line_index)
let available = layout_available_size_to_string(&line_box.m_original_available_width)
dump_appendff(&builder, " line {}: inline {} block {} bottom {} baseline {} width {} height {} available {} break {} forced {} trailing {}\n", [
ak.arg_u64((u64)line_index),
dump_arg_css_pixels(layout_line_box_inline_length(line_box)),
dump_arg_css_pixels(layout_line_box_block_length(line_box)),
dump_arg_css_pixels(layout_line_box_bottom(line_box)),
dump_arg_css_pixels(layout_line_box_baseline(line_box)),
dump_arg_css_pixels(layout_line_box_width(line_box)),
dump_arg_css_pixels(layout_line_box_height(line_box)),
dump_arg_string(&available),
ak.arg_bool(line_box.m_has_break),
ak.arg_bool(line_box.m_has_forced_break),
dump_arg_css_pixels(layout_line_box_get_trailing_whitespace_width(line_box)),
])
let fragments = layout_line_box_fragments_const(line_box)
for fragment_index in 0..<ak.vector_size(fragments):
let fragment = ak.vector_at_const(fragments, fragment_index)
let fragment_node = layout_line_box_fragment_layout_node(fragment)
dump_appendff(&builder, " frag {}: {} #{} start {} length {} offset ", [
ak.arg_u64((u64)fragment_index),
ak.arg_string_view(dump_class_name(fragment_node.cell())),
ak.arg_u64((u64)test_layout_inline_node_index(root, fragment_node)),
ak.arg_u64((u64)layout_line_box_fragment_start(fragment)),
ak.arg_u64((u64)layout_line_box_fragment_length_in_code_units(fragment)),
])
dump_append_css_pixel_point(&builder, layout_line_box_fragment_offset(fragment))
let size = layout_line_box_fragment_size(fragment)
dump_appendff(&builder, " size [{}x{}] border_box_top {} baseline {} writing_mode {} direction {} trailing_ws {} truncated {} ends_ws {} justifiable {} atomic {}\n", [
dump_arg_css_pixels(size.m_width),
dump_arg_css_pixels(size.m_height),
dump_arg_css_pixels(layout_line_box_fragment_border_box_top(fragment)),
dump_arg_css_pixels(layout_line_box_fragment_baseline(fragment)),
ak.arg_u64((u64)layout_line_box_fragment_writing_mode(fragment)),
ak.arg_u64((u64)fragment.m_direction),
ak.arg_bool(layout_line_box_fragment_has_trailing_whitespace(fragment)),
ak.arg_bool(layout_line_box_fragment_is_fully_truncated(fragment)),
ak.arg_bool(layout_line_box_fragment_ends_in_whitespace(fragment)),
ak.arg_bool(layout_line_box_fragment_is_justifiable_whitespace(fragment)),
ak.arg_bool(layout_line_box_fragment_is_atomic_inline(fragment)),
])
if let glyph_run = layout_line_box_fragment_glyph_run(fragment):
let glyphs = web_fonts.glyph_run_glyphs(glyph_run)
dump_appendff(&builder, " glyphs {} type {} size {} width {}:", [
ak.arg_u64((u64)ak.vector_size(glyphs)),
ak.arg_u64((u64)web_fonts.glyph_run_text_type(glyph_run)),
ak.arg_f32(web_fonts.font_pixel_size(web_fonts.glyph_run_font(glyph_run))),
ak.arg_f32(web_fonts.glyph_run_width(glyph_run)),
])
for glyph in ak.vector_span(glyphs):
dump_appendff(&builder, " {},{} {} {} {}", [
ak.arg_f32(glyph.position.m_x),
ak.arg_f32(glyph.position.m_y),
ak.arg_f32(glyph.glyph_width),
ak.arg_u64((u64)glyph.glyph_id),
ak.arg_u64((u64)glyph.length_in_code_units),
])
ak.string_builder_append(&builder, "\n")
test_layout_tree_put_lines(out, ak.string_builder_string_view(&builder))
## Runs one case: "layout" (the layout dump, tests_layout_geometry's harness) or "lines" (the line boxes before commit).
func test_layout_inline_run_case(heap: gc.Heap*, realm: Realm*, test_case: const TestLayoutGeometryCase*) -> ak.String:
if test_case.mode != "lines":
return test_layout_geometry_run_case(heap, realm, test_case)
var out = ak.string_builder_new()
var (document, navigable) = test_layout_tree_build(heap, realm, test_case.html, test_case.css)
defer gc.root_release(&navigable)
var layout_state: LayoutState
test_layout_geometry_lay_out(document, &layout_state)
gc.heap_collect_garbage(heap)
let root = dom_document_unsafe_layout_node(document) else trap("no layout tree")
test_layout_inline_dump_line_boxes(&out, &layout_state, root.layout_node())
return ak.string_builder_to_string_without_validation(&out)
# mark: Tests ===================================================================================
test "inline layout (line boxes, fragments, text-align, vertical-align, white-space, floats beside lines) matches the reference build":
web_initialize_strings()
test_fonts_setup()
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
var (realm, realm_root) = test_layout_tree_window_realm(vm)
defer gc.root_release(&realm_root)
let heap = vm_heap(vm)
var passed: usize = 0
var failed: usize = 0
let allocator: memory.Allocator = heap
with allocator:
test_page_client_color_scheme = CssPreferredColorScheme.light
defer test_page_client_color_scheme = CssPreferredColorScheme.auto
test_media_invalidation_install_user_agent_sheets(realm)
defer test_style_computer_release_user_agent_sheets()
for test_case in &test_layout_inline_cases:
let actual = test_layout_inline_run_case(heap, realm, test_case)
let actual_view = ak.string_bytes_as_string_view(&actual)
if ak.string_view_equals(&actual_view, test_case.expected):
passed += 1
else:
failed += 1
print(f"inline layout case mismatch: {test_case.html} / {test_case.css}")
print(f"expected:\n{test_case.expected}actual:\n{ak.string_view_text(actual_view)}")
print(f"inline layout: {passed} passed, {failed} failed")
assert(failed == 0)
assert(passed > 0)