ladybird/Userland/Libraries/LibWeb/CSS/Size.cpp
Aliaksandr Kalenik 0fb571c1c2 LibWeb: Add Length::resolved() overload for CSSPixels
Since we always pass the px value as an argument to resolved(), we can
pass it directly as CSSPixels instead of wrapping it in Length. This
approach allows us to avoid converting to a double, resulting in fewer
precision issues.
2023-08-30 20:09:15 +02:00

117 lines
2.6 KiB
C++

/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/Size.h>
namespace Web::CSS {
Size::Size(Type type, LengthPercentage length_percentage)
: m_type(type)
, m_length_percentage(move(length_percentage))
{
}
CSSPixels Size::to_px(Layout::Node const& node, CSSPixels reference_value) const
{
return m_length_percentage.resolved(node, CSS::Length::make_px(reference_value)).to_px(node);
}
CSS::Length Size::resolved(Layout::Node const& node, Length const& reference_value) const
{
return m_length_percentage.resolved(node, reference_value);
}
CSS::Length Size::resolved(Layout::Node const& node, CSSPixels reference_value) const
{
return m_length_percentage.resolved(node, reference_value);
}
Size Size::make_auto()
{
return Size { Type::Auto, Length::make_auto() };
}
Size Size::make_px(CSSPixels px)
{
return make_length(CSS::Length::make_px(px));
}
Size Size::make_length(Length length)
{
return Size { Type::Length, move(length) };
}
Size Size::make_percentage(Percentage percentage)
{
return Size { Type::Percentage, move(percentage) };
}
Size Size::make_calculated(NonnullRefPtr<Web::CSS::CalculatedStyleValue> calculated)
{
return Size { Type::Calculated, move(calculated) };
}
Size Size::make_min_content()
{
return Size { Type::MinContent, Length::make_auto() };
}
Size Size::make_max_content()
{
return Size { Type::MaxContent, Length::make_auto() };
}
Size Size::make_fit_content(Length available_space)
{
return Size { Type::FitContent, move(available_space) };
}
Size Size::make_fit_content()
{
// NOTE: We use "auto" as a stand-in for "stretch" here.
return Size { Type::FitContent, Length::make_auto() };
}
Size Size::make_none()
{
return Size { Type::None, Length::make_auto() };
}
bool Size::contains_percentage() const
{
switch (m_type) {
case Type::Auto:
case Type::MinContent:
case Type::MaxContent:
case Type::None:
return false;
default:
return m_length_percentage.contains_percentage();
}
}
String Size::to_string() const
{
switch (m_type) {
case Type::Auto:
return "auto"_string;
case Type::Calculated:
case Type::Length:
case Type::Percentage:
return m_length_percentage.to_string();
case Type::MinContent:
return "min-content"_string;
case Type::MaxContent:
return "max-content"_string;
case Type::FitContent:
return MUST(String::formatted("fit-content({})", m_length_percentage.to_string()));
case Type::None:
return "none"_string;
}
VERIFY_NOT_REACHED();
}
}