Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / ratio.lucb

#==============================================================================================
#
#   ratio - Ratio: a <ratio> value
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Ratio.cpp (lines 1-39) and the inline members of Ratio.h. C++'s
#       operator== and operator<=> are css_ratio_eq and css_ratio_cmp.
#
#       Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/CSS/Ratio.cpp:13 Web::CSS::Ratio::Ratio (`second` defaults to 1).
pub func css_ratio_construct(this: CssRatio*, first: f64, second: f64 = 1.0):
    this.m_first_value = first
    this.m_second_value = second

## `Ratio { first, second }` as a value.
pub func css_ratio_new(first: f64, second: f64 = 1.0) -> CssRatio:
    var ratio: CssRatio = ---
    css_ratio_construct(&ratio, first, second)
    return ratio

## Ported from Libraries/LibWeb/CSS/Ratio.h:17 Web::CSS::Ratio::numerator.
pub func css_ratio_numerator(this: const CssRatio*) -> f64:
    return this.m_first_value

## Ported from Libraries/LibWeb/CSS/Ratio.h:18 Web::CSS::Ratio::denominator.
pub func css_ratio_denominator(this: const CssRatio*) -> f64:
    return this.m_second_value

## Ported from Libraries/LibWeb/CSS/Ratio.h:19 Web::CSS::Ratio::value.
pub func css_ratio_value(this: const CssRatio*) -> f64:
    return this.m_first_value / this.m_second_value

## Ported from Libraries/LibWeb/CSS/Ratio.cpp:20 Web::CSS::Ratio::is_degenerate.
# https://www.w3.org/TR/css-values-4/#degenerate-ratio
pub func css_ratio_is_degenerate(this: const CssRatio*) -> bool:
    return not math.is_finite(this.m_first_value) or this.m_first_value == 0.0 or not math.is_finite(this.m_second_value) or this.m_second_value == 0.0

## Ported from Libraries/LibWeb/CSS/Ratio.cpp:26 Web::CSS::Ratio::to_string.
pub func css_ratio_to_string(this: const CssRatio*) -> ak.String:
    # https://drafts.csswg.org/cssom/#serialize-a-css-value
    # -> <ratio>
    # The numerator serialized as per <number> followed by the literal string " / ", followed by the denominator
    # serialized as per <number>.
    var builder = ak.string_builder_new()
    css_syntax.serialize_a_number_string_builder(&builder, this.m_first_value)
    ak.string_builder_append(&builder, " / ")
    css_syntax.serialize_a_number_string_builder(&builder, this.m_second_value)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Ratio.h:24 Web::CSS::Ratio::operator==.
pub func css_ratio_eq(this: const CssRatio*, other: CssRatio) -> bool:
    return css_ratio_value(this) == css_ratio_value(&other)

## Ported from Libraries/LibWeb/CSS/Ratio.h:29 Web::CSS::Ratio::operator<=>.
pub func css_ratio_cmp(this: const CssRatio*, other: CssRatio) -> i32:
    let this_value = css_ratio_value(this)
    let other_value = css_ratio_value(&other)

    if this_value < other_value:
        return -1
    if this_value > other_value:
        return 1
    return 0