Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / tests_layout_svg.lucb

#==============================================================================================
#
#   tests_layout_svg - SVG layout and the SVG paintables of parsed documents, pinned to the
#                      reference build
#
#   DESCRIPTION:
#       Runs the cases of tests_layout_svg_cases through the port. The local oracle that wrote
#       the table (luce-browser-tools, oracles/luce-browser-engine/layout_geometry,
#       cases_svg.txt, own_svg_cases.py) runs the reference build's LibWeb on the same
#       documents. Modes:
#
#       layout  tests_layout_geometry's harness (Document::update_layout and the three dumps of
#               a Layout test). The first cases are Ladybird's own Layout tests with SVG whose
#               elements are ported; the others are written for regions r49 and r53: every
#               preserveAspectRatio alignment with meet and slice, viewBox edge cases, nested
#               viewports, groups and transforms, shapes with strokes, percentages, and <svg>
#               boxes in blocks, inline content, flex and grid containers.
#       svg     every SVG paintable in the layout tree's pre-order: its class and absolute rect,
#               the computed transforms layout gave it, its mask and clip areas, its computed path
#               (SVG path data) and bounding box, whether it contributes to a clip path, its clip
#               path bounds and whether it anti-aliases.
#
#==============================================================================================

# mark: The "svg" mode ==========================================================================

## `{}` of a Gfx::AffineTransform's six values (AK's float formatter), after its name.
func test_layout_svg_put_transform(builder: ak.StringBuilder*, name: str, transform: gfx.AffineTransform):
    dump_appendff(builder, " {} [{} {} {} {} {} {}]", [ak.arg_str(name), ak.arg_f32(gfx.affine_transform_a(&transform)), ak.arg_f32(gfx.affine_transform_b(&transform)),
                                                       ak.arg_f32(gfx.affine_transform_c(&transform)), ak.arg_f32(gfx.affine_transform_d(&transform)),
                                                       ak.arg_f32(gfx.affine_transform_e(&transform)), ak.arg_f32(gfx.affine_transform_f(&transform))])

## `{}` of an optional CSSPixelRect, or "none".
func test_layout_svg_put_optional_rect(builder: ak.StringBuilder*, rect: gfx.CssPixelRect?):
    if let value = rect:
        dump_append_css_pixel_rect(builder, value)
    else:
        ak.string_builder_append(builder, "none")

## The "svg" mode: the SVG paintables of the layout tree in pre-order (the oracle's run_svg).
func test_layout_svg_dump_paintables(out: ak.StringBuilder*, root: LayoutNode*):
    var builder = ak.string_builder_new()
    var index: usize = 0
    for node in layout_node_inclusive_subtree(root):
        let this_index = index
        index += 1
        let paintable = layout_node_first_paintable(node) else continue
        if not is_paint_svg_paintable(paintable) and not is_paint_svgsvg_paintable(paintable):
            continue
        let box = (PaintPaintableBox*)paintable
        dump_appendff(&builder, "#{} {} rect ", [ak.arg_u64((u64)this_index), ak.arg_str(paintable.cell().vtable.class_info.name)])
        dump_append_css_pixel_rect(&builder, paint_paintable_box_absolute_rect(box))
        ak.string_builder_append(&builder, "\n")
        if let graphics = as_if_paint_svg_graphics_paintable(paintable):
            let transforms = paint_svg_graphics_paintable_computed_transforms(graphics)
            ak.string_builder_append(&builder, "  transforms")
            test_layout_svg_put_transform(&builder, "viewbox", paint_svg_graphics_paintable_computed_transforms_svg_to_viewbox_transform(&transforms))
            test_layout_svg_put_transform(&builder, "svg", paint_svg_graphics_paintable_computed_transforms_svg_transform(&transforms))
            test_layout_svg_put_transform(&builder, "css", paint_svg_graphics_paintable_computed_transforms_svg_to_css_pixels_transform(&transforms))
            ak.string_builder_append(&builder, "\n  mask ")
            test_layout_svg_put_optional_rect(&builder, paint_paintable_box_get_mask_area(box))
            ak.string_builder_append(&builder, " clip ")
            test_layout_svg_put_optional_rect(&builder, paint_paintable_box_get_clip_area(box))
            ak.string_builder_append(&builder, "\n")
        if let path_paintable = as_if_paint_svg_path_paintable(paintable):
            if let path = paint_svg_path_paintable_computed_path(path_paintable):
                let svg_string = gfx.path_to_svg_string(&path)
                ak.string_builder_append(&builder, "  path ")
                ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&svg_string))
                let bounding_box = gfx.path_bounding_box(&path)
                dump_appendff(&builder, "\n  bbox [{},{} {}x{}]\n", [ak.arg_f32(bounding_box.m_location.m_x), ak.arg_f32(bounding_box.m_location.m_y),
                                                                     ak.arg_f32(bounding_box.m_size.m_width), ak.arg_f32(bounding_box.m_size.m_height)])
            else:
                ak.string_builder_append(&builder, "  path none\n")
        if let svg_paintable = as_if_paint_svg_paintable(paintable):
            dump_appendff(&builder, "  contributes {} bounds ", [ak.arg_bool(paint_svg_paintable_contributes_to_clip_path(svg_paintable))])
            test_layout_svg_put_optional_rect(&builder, paint_svg_paintable_clip_path_geometry_bounds(svg_paintable, layout_identity_affine_transform()))
            dump_appendff(&builder, " anti-alias {}\n", [ak.arg_bool(paint_svg_paintable_should_anti_alias(svg_paintable) == display_list.ShouldAntiAlias.yes)])
    test_layout_tree_put_lines(out, ak.string_builder_string_view(&builder))

## Runs one case: "layout" (tests_layout_geometry's harness) or "svg".
func test_layout_svg_run_case(heap: gc.Heap*, realm: Realm*, test_case: const TestLayoutGeometryCase*) -> ak.String:
    if test_case.mode == "layout":
        return test_layout_geometry_run_case(heap, realm, test_case)
    assert(test_case.mode == "svg")
    var out = ak.string_builder_new()
    var (document, navigable) = test_layout_tree_parse_and_style(heap, realm, test_case.html, test_case.css)
    defer gc.root_release(&navigable)
    test_layout_geometry_update_layout(document)
    gc.heap_collect_garbage(heap)
    let root = dom_document_unsafe_layout_node(document) else trap("no layout tree")
    test_layout_svg_dump_paintables(&out, root.layout_node())
    return ak.string_builder_to_string_without_validation(&out)

# mark: Tests ===================================================================================

test "SVG layout (viewports, viewBox and preserveAspectRatio, groups, transforms, shapes, <svg> sizing) and the SVG paintables match 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_svg_cases:
            let actual = test_layout_svg_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"SVG layout case mismatch ({test_case.mode}): {test_case.html} / {test_case.css}")
                print(f"expected:\n{test_case.expected}actual:\n{ak.string_view_text(actual_view)}")
    print(f"SVG layout: {passed} passed, {failed} failed")
    assert(failed == 0)
    assert(passed > 0)