dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / layout / formatting_context_6.lucb
#==============================================================================================
#
# formatting_context_6 - Layout::FormattingContext: box rectangles, baselines, replaced
# sizing and max sizes treated as none
#
# DESCRIPTION:
# Port of LibWeb/Layout/FormattingContext.cpp (lines 2332-2597): rectangles in ancestor
# coordinate spaces, box baselines (vertical-align, line boxes, the child a baseline
# derives from), whether a box is sized as a replaced element, when a max-width or
# max-height is treated as none, and gap_to_px.
#
# Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Rectangles ==============================================================================
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2332 Web::Layout::FormattingContext::absolute_content_rect.
pub func layout_formatting_context_absolute_content_rect(this: LayoutFormattingContext*, box: LayoutBox*) -> gfx.CssPixelRect:
let box_state = layout_state_get(this.m_state, box.layout_node_with_style())
var rect = gfx.CssPixelRect(box_state.offset, layout_state_used_values_content_size(box_state))
var block_or_none = layout_state_used_values_containing_block_used_values(box_state)
while let block = block_or_none:
gfx.css_pixel_rect_translate_by_css_pixel_point(&rect, block.offset)
block_or_none = layout_state_used_values_containing_block_used_values(block)
return rect
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2341 Web::Layout::FormattingContext::box_child_to_derive_baseline_from.
pub func layout_formatting_context_box_child_to_derive_baseline_from(this: LayoutFormattingContext*, box: LayoutBox*) -> LayoutBox*?:
if not tree_node_has_children(box.layout_node()) or layout_node_children_are_inline(box.layout_node()):
return none
# Find the last in-flow child that has a baseline (either directly via line boxes, or via its descendants).
var child_or_none = tree_node_last_child(box.layout_node())
while let child = child_or_none:
child_or_none = tree_node_previous_sibling(child)
let child_box = as_if_layout_box(child) else continue
if layout_node_is_out_of_flow_layout_formatting_context(child_box.layout_node(), this):
continue
if not ak.vector_is_empty(&layout_state_get(this.m_state, child_box.layout_node_with_style()).line_boxes):
return child_box
if layout_formatting_context_box_child_to_derive_baseline_from(this, child_box) != none:
return child_box
return none
## `vertical_align.has<CSS::VerticalAlign>()`'s keyword, or none for a length-percentage.
func layout_formatting_context_vertical_align_keyword(vertical_align: VerticalAlignOrCssLengthPercentage) -> css_data.VerticalAlign?:
match vertical_align:
.vertical_align(value):
return value
.css_length_percentage(_):
return none
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2360 Web::Layout::FormattingContext::box_baseline.
pub func layout_formatting_context_box_baseline(this: LayoutFormattingContext*, box: LayoutBox*) -> gfx.CssPixels:
let box_state = layout_state_get(this.m_state, box.layout_node_with_style())
# https://drafts.csswg.org/css2/#propdef-vertical-align
let vertical_align = css_computed_values_vertical_align(layout_box_computed(box))
if let keyword = layout_formatting_context_vertical_align_keyword(vertical_align):
match keyword:
.top:
# Top: Align the top of the aligned subtree with the top of the line box.
return layout_state_used_values_border_box_top(box_state)
.middle:
# Middle: Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.
let containing_block = layout_node_containing_block(box.layout_node()) else trap("a box without a containing block")
let metrics = web_fonts.font_pixel_metrics(layout_node_first_available_font(containing_block.layout_node()))
return layout_div_int(layout_state_used_values_margin_box_height(box_state), 2).add(gfx.css_pixels_nearest_value_for_f32(metrics.x_height / 2.0))
.bottom:
# Bottom: Align the bottom of the aligned subtree with the bottom of the line box.
return layout_state_used_values_content_height(box_state).add(layout_state_used_values_margin_box_top(box_state))
.text_top:
# TextTop: Align the top of the box with the top of the parent's content area (see 10.6.1).
return css_computed_values_font_size(layout_box_computed(box))
.text_bottom:
# TextBottom: Align the bottom of the box with the bottom of the parent's content area (see 10.6.1).
let containing_block = layout_node_containing_block(box.layout_node()) else trap("a box without a containing block")
let metrics = web_fonts.font_pixel_metrics(layout_node_first_available_font(containing_block.layout_node()))
return layout_state_used_values_margin_box_height(box_state).sub(gfx.css_pixels_nearest_value_for_f32(metrics.descent * 2.0))
_:
_ = 0
# https://drafts.csswg.org/css2/#propdef-vertical-align
# The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either
# no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the
# baseline is the bottom margin edge.
# NB: This overflow exception only applies to inline-block, not to inline-flex or inline-grid containers, which
# always derive their baselines from their content per CSS Align and the respective Flexbox/Grid specs.
let display = layout_node_display(box.layout_node())
let overflow_x = css_computed_values_overflow_x(layout_box_computed(box))
let overflow_y = css_computed_values_overflow_y(layout_box_computed(box))
let has_visible_overflow = overflow_x == css_data.Overflow.visible and overflow_y == css_data.Overflow.visible
let is_flex_or_grid_container = css_display_is_flex_inside(&display) or css_display_is_grid_inside(&display)
let is_inline_flex_or_grid_container = css_display_is_inline_outside(&display) and is_flex_or_grid_container
let always_derive_from_content = is_flex_or_grid_container or has_visible_overflow
if always_derive_from_content and not ak.vector_is_empty(&box_state.line_boxes):
let last_line_box = ak.vector_at_const(&box_state.line_boxes, ak.vector_size(&box_state.line_boxes) - 1)
let last_line_box_top = layout_line_box_bottom(last_line_box).sub(layout_line_box_block_length(last_line_box))
return layout_state_used_values_margin_box_top(box_state).add(last_line_box_top).add(layout_line_box_baseline(last_line_box))
# Derive baseline from block children if the box is flex/grid inside or has visible overflow.
# AD-HOC: We also derive baseline from children for <input> elements. Per the HTML spec, inputs have
# `overflow: clip !important`, so CSS2 says to use bottom margin edge. However, the internal shadow tree
# baseline should determine the control's baseline for proper alignment with adjacent text.
# https://html.spec.whatwg.org/multipage/rendering.html#form-controls
if let child_box = layout_formatting_context_box_child_to_derive_baseline_from(this, box):
var is_input = false
if let dom_node = layout_node_dom_node(box.layout_node()):
is_input = is_html_input_element(dom_node)
if always_derive_from_content or is_input:
let child_box_state = layout_state_get(this.m_state, child_box.layout_node_with_style())
let child_offset_from_margin_edge = child_box_state.offset.m_y.sub(layout_state_used_values_margin_box_top(child_box_state))
# https://drafts.csswg.org/css-flexbox-1/#flex-baselines
# Otherwise, if the flex container has at least one flex item, the flex container's first/last main-axis
# baseline set is generated from the alignment baseline of the startmost/endmost flex item.
# https://drafts.csswg.org/css-grid-1/#grid-baselines
# Otherwise, the grid container's first (last) baseline set is generated from the alignment baseline of the
# first (last) grid item in row-major grid order.
# FIXME: This does not yet select the spec-defined startmost/endmost flex item, or the first/last grid item
# in row-major grid order.
if is_inline_flex_or_grid_container and not ak.vector_is_empty(&child_box_state.line_boxes) and not layout_line_box_is_empty(ak.vector_at_const(&child_box_state.line_boxes, 0)):
let first_line_box = ak.vector_at_const(&child_box_state.line_boxes, 0)
let first_line_box_top = layout_line_box_bottom(first_line_box).sub(layout_line_box_block_length(first_line_box))
let child_first_line_baseline = layout_state_used_values_margin_box_top(child_box_state).add(first_line_box_top).add(layout_line_box_baseline(first_line_box))
return layout_state_used_values_margin_box_top(box_state).add(child_offset_from_margin_edge).add(child_first_line_baseline)
return layout_state_used_values_margin_box_top(box_state).add(child_offset_from_margin_edge).add(layout_formatting_context_box_baseline(this, child_box))
# If none of the children have a baseline set, the bottom margin edge of the box is used.
return layout_state_used_values_margin_box_height(box_state)
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2441 Web::Layout::margin_box_rect.
func layout_margin_box_rect(used_values: const LayoutStateUsedValues*) -> gfx.CssPixelRect:
let zero = layout_zero_px()
return gfx.CssPixelRect(
gfx.CssPixelPoint(
ak.max(layout_state_used_values_margin_box_left(used_values), zero).neg(),
ak.max(layout_state_used_values_margin_box_top(used_values), zero).neg(),
),
gfx.CssPixelSize(
ak.max(layout_state_used_values_margin_box_left(used_values), zero).add(layout_state_used_values_content_width(used_values)).add(ak.max(layout_state_used_values_margin_box_right(used_values), zero)),
ak.max(layout_state_used_values_margin_box_top(used_values), zero).add(layout_state_used_values_content_height(used_values)).add(ak.max(layout_state_used_values_margin_box_bottom(used_values), zero)),
),
)
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2455 Web::Layout::FormattingContext::content_box_rect.
pub func layout_formatting_context_content_box_rect_layout_box(this: LayoutFormattingContext*, box: LayoutBox*) -> gfx.CssPixelRect:
return layout_formatting_context_content_box_rect_layout_state_used_values(this, layout_state_get(this.m_state, box.layout_node_with_style()))
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2460 Web::Layout::FormattingContext::content_box_rect.
pub func layout_formatting_context_content_box_rect_layout_state_used_values(this: LayoutFormattingContext*, used_values: const LayoutStateUsedValues*) -> gfx.CssPixelRect:
_ = this
return gfx.CssPixelRect(used_values.offset, layout_state_used_values_content_size(used_values))
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2465 Web::Layout::FormattingContext::content_box_rect_in_ancestor_coordinate_space.
pub func layout_formatting_context_content_box_rect_in_ancestor_coordinate_space(this: LayoutFormattingContext*, used_values: const LayoutStateUsedValues*, ancestor_box: LayoutBox*) -> gfx.CssPixelRect:
_ = this
var rect = gfx.CssPixelRect(gfx.CssPixelPoint(layout_zero_px(), layout_zero_px()), layout_state_used_values_content_size(used_values))
var current_or_none: const LayoutStateUsedValues*? = used_values
while let current = current_or_none:
if layout_state_used_values_node_const(current) == ancestor_box.layout_node_with_style():
return rect
gfx.css_pixel_rect_translate_by_css_pixel_point(&rect, current.offset)
current_or_none = layout_state_used_values_containing_block_used_values(current)
# If we get here, ancestor_box was not a containing block ancestor of `box`!
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2477 Web::Layout::FormattingContext::margin_box_rect_in_ancestor_coordinate_space.
pub func layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(this: LayoutFormattingContext*, used_values: const LayoutStateUsedValues*, ancestor_box: LayoutBox*) -> gfx.CssPixelRect:
_ = this
var rect = layout_margin_box_rect(used_values)
var current_or_none: const LayoutStateUsedValues*? = used_values
while let current = current_or_none:
if layout_state_used_values_node_const(current) == ancestor_box.layout_node_with_style():
return rect
gfx.css_pixel_rect_translate_by_css_pixel_point(&rect, current.offset)
current_or_none = layout_state_used_values_containing_block_used_values(current)
# If we get here, ancestor_box was not a containing block ancestor of `box`!
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2489 Web::Layout::FormattingContext::margin_box_rect_in_ancestor_coordinate_space.
pub func layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_box(this: LayoutFormattingContext*, box: LayoutBox*, ancestor_box: LayoutBox*) -> gfx.CssPixelRect:
return layout_formatting_context_margin_box_rect_in_ancestor_coordinate_space_layout_state_used_values(this, layout_state_get(this.m_state, box.layout_node_with_style()), ancestor_box)
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2494 Web::Layout::FormattingContext::box_is_sized_as_replaced_element.
pub func layout_formatting_context_box_is_sized_as_replaced_element(this: LayoutFormattingContext*, box: LayoutBox*, available_space: LayoutAvailableSpace) -> bool:
# When a box has a preferred aspect ratio, its automatic sizes are calculated the same as for a
# replaced element with a natural aspect ratio and no natural size in that axis, see e.g. CSS2 §10
# and CSS Flexible Box Model Level 1 §9.2.
# https://www.w3.org/TR/css-sizing-4/#aspect-ratio-automatic
if layout_node_is_replaced_box(box.layout_node()) and layout_box_has_auto_content_box_size(box):
return true
if layout_box_has_preferred_aspect_ratio(box) or layout_box_has_auto_content_box_size(box):
# From CSS2:
# If height and width both have computed values of auto and the element has an intrinsic ratio but no intrinsic height or width,
# then the used value of width is undefined in CSS 2.
# However, it is suggested that, if the containing block’s width does not itself depend on the replaced element’s width,
# then the used value of width is calculated from the constraint equation used for block-level, non-replaced elements in normal flow.
# AD-HOC: If box has preferred aspect ratio but width and height are not specified, then we should
# size it as a normal box to match other browsers.
let auto_size = layout_box_auto_content_box_size(box)
if layout_formatting_context_should_treat_width_as_auto(this, box, available_space) and layout_formatting_context_should_treat_height_as_auto(this, box, available_space) and not css_size_with_aspect_ratio_has_width(&auto_size) and not css_size_with_aspect_ratio_has_height(&auto_size):
return false
return true
return false
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2526 Web::Layout::FormattingContext::should_treat_max_width_as_none.
pub func layout_formatting_context_should_treat_max_width_as_none(this: LayoutFormattingContext*, box: LayoutBox*, available_width: LayoutAvailableSize) -> bool:
let max_width = css_computed_values_max_width(layout_box_computed(box))
if css_size_is_none(&max_width):
return true
if layout_available_size_is_max_content(&available_width) and css_size_is_max_content(&max_width):
return true
# https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
if css_size_contains_percentage(&max_width):
if layout_available_size_is_max_content(&available_width):
return true
if layout_available_size_is_min_content(&available_width):
if not layout_node_is_replaced_box(box.layout_node()):
return true
return false
var containing_block = layout_node_containing_block(box.layout_node())
while let block = containing_block:
let display = layout_node_display(block.layout_node())
if not (layout_node_is_anonymous(block.layout_node()) and not css_display_is_table_cell(&display)):
break
containing_block = layout_node_containing_block(block.layout_node())
let block = containing_block else return true
let containing_block_used_values = layout_state_try_get_layout_node_with_style(this.m_state, block.layout_node_with_style()) else return true
if not layout_state_used_values_has_definite_width(containing_block_used_values):
return true
if css_size_is_fit_content(&max_width) and layout_available_size_is_intrinsic_sizing_constraint(&available_width):
return true
if css_size_is_max_content(&max_width) and layout_available_size_is_max_content(&available_width):
return true
if css_size_is_min_content(&max_width) and layout_available_size_is_min_content(&available_width):
return true
return false
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2560 Web::Layout::FormattingContext::should_treat_max_height_as_none.
pub func layout_formatting_context_should_treat_max_height_as_none(this: LayoutFormattingContext*, box: LayoutBox*, available_height: LayoutAvailableSize) -> bool:
# https://www.w3.org/TR/CSS22/visudet.html#min-max-heights
# If the height of the containing block is not specified explicitly (i.e., it depends on content height),
# and this element is not absolutely positioned, the percentage value is treated as '0' (for 'min-height')
# or 'none' (for 'max-height').
let max_height = css_computed_values_max_height(layout_box_computed(box))
if css_size_is_none(&max_height):
return true
if css_size_contains_percentage(&max_height):
if layout_available_size_is_min_content(&available_height):
return false
var containing_block = layout_node_containing_block(box.layout_node())
while let block = containing_block:
let display = layout_node_display(block.layout_node())
if not (layout_node_is_anonymous(block.layout_node()) and not css_display_is_table_cell(&display)):
break
containing_block = layout_node_containing_block(block.layout_node())
let block = containing_block else return true
let containing_block_used_values = layout_state_try_get_layout_node_with_style(this.m_state, block.layout_node_with_style()) else return true
if not layout_state_used_values_has_definite_height(containing_block_used_values):
return true
if css_size_is_fit_content(&max_height) and layout_available_size_is_intrinsic_sizing_constraint(&available_height):
return true
if css_size_is_max_content(&max_height) and layout_available_size_is_max_content(&available_height):
return true
if css_size_is_min_content(&max_height) and layout_available_size_is_min_content(&available_height):
return true
return false
## Ported from Libraries/LibWeb/Layout/FormattingContext.cpp:2590 Web::Layout::FormattingContext::gap_to_px.
pub func layout_formatting_context_gap_to_px(this: LayoutFormattingContext*, gap: CssLengthPercentageOrCssNormalGap, reference_value: gfx.CssPixels) -> gfx.CssPixels:
match gap:
.css_normal_gap(_):
return layout_zero_px()
.css_length_percentage(length_percentage):
return css_percentage_or_to_px(&length_percentage.percentage_or, layout_formatting_context_context_box(this).layout_node(), reference_value)