Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / sizing.lucb

#==============================================================================================
#
#   sizing - SizeWithAspectRatio and the CSS default sizing algorithm
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Sizing.cpp (lines 1-65) and the inline members of Sizing.h.
#
#       Copyright (c) 2022, MacDue <macdue@dueutil.tech>
#       Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/Sizing.h:17 Web::CSS::SizeWithAspectRatio::has_width.
pub func css_size_with_aspect_ratio_has_width(this: const CssSizeWithAspectRatio*) -> bool:
    return this.width != none

## Ported from Libraries/LibWeb/CSS/Sizing.h:18 Web::CSS::SizeWithAspectRatio::has_height.
pub func css_size_with_aspect_ratio_has_height(this: const CssSizeWithAspectRatio*) -> bool:
    return this.height != none

## Ported from Libraries/LibWeb/CSS/Sizing.h:19 Web::CSS::SizeWithAspectRatio::has_aspect_ratio.
pub func css_size_with_aspect_ratio_has_aspect_ratio(this: const CssSizeWithAspectRatio*) -> bool:
    return this.aspect_ratio != none

## `CSSPixelSize { width, height }`.
func css_sizing_pixel_size(width: gfx.CssPixels, height: gfx.CssPixels) -> gfx.CssPixelSize:
    var size: gfx.CssPixelSize = ---
    gfx.css_pixel_size_construct(&size, width, height)
    return size

## Ported from Libraries/LibWeb/CSS/Sizing.cpp:13 Web::CSS::run_default_sizing_algorithm.
# https://drafts.csswg.org/css-images/#default-sizing
pub func css_run_default_sizing_algorithm(specified_width: gfx.CssPixels?, specified_height: gfx.CssPixels?, natural: CssSizeWithAspectRatio, default_size: gfx.CssPixelSize) -> gfx.CssPixelSize:
    # If the specified size is a definite width and height, the concrete object size is given that width and height.
    if let width = specified_width:
        if let height = specified_height:
            return css_sizing_pixel_size(width, height)

    # If the specified size is only a width or height (but not both) then the concrete object size is given that specified width or height.
    # The other dimension is calculated as follows:
    if specified_width != none or specified_height != none:
        # 1. If the object has a natural aspect ratio,
        # the missing dimension of the concrete object size is calculated using that aspect ratio and the present dimension.
        if let aspect_ratio = natural.aspect_ratio:
            if not gfx.css_pixel_fraction_might_be_saturated(&aspect_ratio):
                if let width = specified_width:
                    let one = gfx.css_pixels_from_int(1)
                    let inverse = gfx.css_pixels_div_css_pixel_fraction(&one, aspect_ratio)
                    return css_sizing_pixel_size(width, gfx.css_pixels_mul_css_pixels(&inverse, width))
                if let height = specified_height:
                    return css_sizing_pixel_size(gfx.css_pixels_mul_css_pixel_fraction(&height, aspect_ratio), height)

        # 2. Otherwise, if the missing dimension is present in the object’s natural dimensions,
        # the missing dimension is taken from the object’s natural dimensions.
        if let height = specified_height:
            if let natural_width = natural.width:
                return css_sizing_pixel_size(natural_width, height)
        if let width = specified_width:
            if let natural_height = natural.height:
                return css_sizing_pixel_size(width, natural_height)

        # 3. Otherwise, the missing dimension of the concrete object size is taken from the default object size.
        if let height = specified_height:
            return css_sizing_pixel_size(gfx.css_pixel_size_width(&default_size), height)
        if let width = specified_width:
            return css_sizing_pixel_size(width, gfx.css_pixel_size_height(&default_size))
        trap("VERIFY_NOT_REACHED")

    # If the specified size has no constraints:
    # 1. If the object has a natural height or width, its size is resolved as if its natural dimensions were given as the specified size.
    if css_size_with_aspect_ratio_has_width(&natural) or css_size_with_aspect_ratio_has_height(&natural):
        return css_run_default_sizing_algorithm(natural.width, natural.height, natural, default_size)

    # 2. Otherwise, its size is resolved as a contain constraint against the default object size.
    if let aspect_ratio = natural.aspect_ratio:
        if not gfx.css_pixel_fraction_might_be_saturated(&aspect_ratio):
            if gfx.css_pixel_size_is_empty(&default_size):
                return default_size
            let default_width = gfx.css_pixel_size_width(&default_size).to_double()
            let default_height = gfx.css_pixel_size_height(&default_size).to_double()
            if gfx.css_pixel_fraction_to_double(&aspect_ratio) >= default_width / default_height:
                return css_sizing_pixel_size(gfx.css_pixel_size_width(&default_size), gfx.css_pixels_nearest_value_for_f64(default_width / gfx.css_pixel_fraction_to_double(&aspect_ratio)))
            return css_sizing_pixel_size(gfx.css_pixels_nearest_value_for_f64(default_height * gfx.css_pixel_fraction_to_double(&aspect_ratio)), gfx.css_pixel_size_height(&default_size))

    return default_size