Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / box.lucb

#==============================================================================================
#
#   box - Layout::Box: a node with a box (natural and auto content sizes, abspos children)
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/Box.cpp (lines 1-91) and the inline members of Box.h: the two
#       constructors, auto_content_box_size, visit_edges, the PaintableBox, the preferred aspect
#       ratio, the contained absolutely positioned children and the intrinsic sizes cache.
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Construction ============================================================================

## Ported from Libraries/LibWeb/Layout/Box.cpp:20 Web::Layout::Box::Box.
pub func layout_box_construct_css_computed_properties(this: LayoutBox*, document: DomDocument*, node: DomNode*?, style: CssComputedProperties*):
    layout_node_with_style_and_box_model_metrics_construct_css_computed_properties(this.layout_node_with_style_and_box_model_metrics(), document, node, style)

## Ported from Libraries/LibWeb/Layout/Box.cpp:25 Web::Layout::Box::Box.
pub func layout_box_construct_css_computed_values(this: LayoutBox*, document: DomDocument*, node: DomNode*?, computed_values: CssComputedValues*):
    layout_node_with_style_and_box_model_metrics_construct_css_computed_values(this.layout_node_with_style_and_box_model_metrics(), document, node, computed_values)

# mark: Sizes ===================================================================================

## Ported from Libraries/LibWeb/Layout/Box.h:38 Web::Layout::Box::natural_size (virtual).
# https://www.w3.org/TR/css-images-3/#natural-dimensions
pub func layout_box_natural_size_impl(this: LayoutBox*) -> CssSizeWithAspectRatio:
    _ = this
    return CssSizeWithAspectRatio()

## Ported from Libraries/LibWeb/Layout/Box.cpp:34 Web::Layout::Box::auto_content_box_size.
# When computed width/height is auto, auto_content_box_size gives the fallback content-box size for
# elements whose used size is determined by natural dimensions, attributes, or defaults other than
# the generic UA fallback (300x150). Any returned aspect ratio comes from natural dimensions (when
# available) or may be computed from fallback sizing. Don't confuse this with the CSS preferred
# aspect ratio.
pub func layout_box_auto_content_box_size(this: LayoutBox*) -> CssSizeWithAspectRatio:
    # https://drafts.csswg.org/css-contain-2/#containment-size
    # Replaced elements must be treated as having a natural width and height of 0 and no natural aspect
    # ratio.
    if layout_node_has_size_containment(this.layout_node()):
        return CssSizeWithAspectRatio(width = gfx.CssPixels.from_int(0), height = gfx.CssPixels.from_int(0), aspect_ratio = none)

    return layout_box_compute_auto_content_box_size(this)

## Ported from Libraries/LibWeb/Layout/Box.h:46 Web::Layout::Box::has_auto_content_box_size (virtual).
pub func layout_box_has_auto_content_box_size_impl(this: LayoutBox*) -> bool:
    _ = this
    return false

## Ported from Libraries/LibWeb/Layout/Box.cpp:66 Web::Layout::Box::preferred_aspect_ratio.
# https://www.w3.org/TR/css-sizing-4/#preferred-aspect-ratio
pub func layout_box_preferred_aspect_ratio(this: LayoutBox*) -> gfx.CssPixelFraction?:
    let computed_aspect_ratio = css_computed_values_aspect_ratio(&layout_node_computed_values(this.layout_node()).computed_values)

    # https://www.w3.org/TR/css-contain-2/#containment-size
    if not layout_node_has_size_containment(this.layout_node()) and computed_aspect_ratio.use_natural_aspect_ratio_if_available:
        let auto_size = layout_box_auto_content_box_size(this)
        if css_size_with_aspect_ratio_has_aspect_ratio(&auto_size):
            return auto_size.aspect_ratio

    let ratio = computed_aspect_ratio.preferred_ratio else return none

    if css_ratio_is_degenerate(&ratio):
        return none

    var fraction: gfx.CssPixelFraction = ---
    gfx.css_pixel_fraction_construct_f64(&fraction, css_ratio_numerator(&ratio), css_ratio_denominator(&ratio))
    # ratio.is_degenerate() operates on doubles while CSSPixelFraction uses CSSPixels, so we need to check again here.
    # (`fraction == 0` compares through operator CSSPixels.)
    if gfx.eq_css_pixels_int(gfx.css_pixel_fraction_to_css_pixels(&fraction), 0):
        return none

    return fraction

## Ported from Libraries/LibWeb/Layout/Box.h:50 Web::Layout::Box::has_preferred_aspect_ratio.
pub func layout_box_has_preferred_aspect_ratio(this: LayoutBox*) -> bool:
    return layout_box_preferred_aspect_ratio(this) != none

## Ported from Libraries/LibWeb/Layout/Box.h:54 Web::Layout::Box::did_set_content_size (virtual).
pub func layout_box_did_set_content_size_impl(this: LayoutBox*):
    _ = this

## Ported from Libraries/LibWeb/Layout/Box.h:75 Web::Layout::Box::compute_auto_content_box_size (virtual).
pub func layout_box_compute_auto_content_box_size_impl(this: LayoutBox*) -> CssSizeWithAspectRatio:
    return layout_box_natural_size(this)

# mark: Paintables and edges ====================================================================

## Ported from Libraries/LibWeb/Layout/Box.cpp:45 Web::Layout::Box::visit_edges (override).
pub func layout_box_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (LayoutBox*)base
    layout_node_with_style_and_box_model_metrics_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_vector(visitor, &this.m_contained_abspos_children)

## Ported from Libraries/LibWeb/Layout/Box.cpp:51 Web::Layout::Box::create_paintable (override).
pub func layout_box_create_paintable_impl(base: LayoutNode*) -> PaintPaintable*?:
    return paint_paintable_box_create_layout_box((LayoutBox*)base).paint_paintable()

## Ported from Libraries/LibWeb/Layout/Box.cpp:56 Web::Layout::Box::paintable_box.
pub func layout_box_paintable_box(this: LayoutBox*) -> PaintPaintableBox*?:
    return (PaintPaintableBox*?)layout_node_first_paintable(this.layout_node())

## Ported from Libraries/LibWeb/Layout/Box.cpp:61 Web::Layout::Box::paintable_box.
pub func layout_box_paintable_box_const(this: LayoutBox*) -> PaintPaintableBox*?:
    return (PaintPaintableBox*?)layout_node_first_paintable(this.layout_node())

## Ported from Libraries/LibWeb/Layout/Box.h:78 Web::Layout::Box::is_box (override).
pub func layout_box_is_box_impl(base: LayoutNode*) -> bool:
    _ = base
    return true

# mark: Contained abspos children and intrinsic sizes ===========================================

## Ported from Libraries/LibWeb/Layout/Box.h:58 Web::Layout::Box::add_contained_abspos_child.
pub func layout_box_add_contained_abspos_child(this: LayoutBox*, child: LayoutNode*):
    ak.vector_append_t(&this.m_contained_abspos_children, child)

## Ported from Libraries/LibWeb/Layout/Box.h:59 Web::Layout::Box::clear_contained_abspos_children.
pub func layout_box_clear_contained_abspos_children(this: LayoutBox*):
    ak.vector_clear(&this.m_contained_abspos_children)

## Ported from Libraries/LibWeb/Layout/Box.h:60 Web::Layout::Box::contained_abspos_children.
pub func layout_box_contained_abspos_children(this: LayoutBox*) -> const ak.Vector[LayoutNode*]*:
    return &this.m_contained_abspos_children

## Ported from Libraries/LibWeb/Layout/Box.h:64 Web::Layout::Box::cached_intrinsic_sizes.
pub func layout_box_cached_intrinsic_sizes(this: LayoutBox*) -> LayoutIntrinsicSizes*:
    if this.m_cached_intrinsic_sizes == none:
        this.m_cached_intrinsic_sizes = (new LayoutIntrinsicSizes) else trap("out of memory")
    return this.m_cached_intrinsic_sizes else trap("unreachable: the cache was just made")

## Ported from Libraries/LibWeb/Layout/Box.h:70 Web::Layout::Box::reset_cached_intrinsic_sizes.
pub func layout_box_reset_cached_intrinsic_sizes(this: LayoutBox*):
    this.m_cached_intrinsic_sizes = none