2022-02-21 17:49:47 +00:00
|
|
|
/*
|
2025-05-16 20:02:16 +01:00
|
|
|
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
2022-02-21 17:49:47 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2025-05-16 20:02:16 +01:00
|
|
|
#include <LibWeb/CSS/Frequency.h>
|
2023-03-30 15:54:38 +01:00
|
|
|
#include <LibWeb/CSS/Percentage.h>
|
2024-12-11 15:05:56 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
2022-02-21 17:49:47 +00:00
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
2023-05-27 21:10:21 +02:00
|
|
|
Frequency::Frequency(double value, Type type)
|
2022-02-21 17:49:47 +00:00
|
|
|
: m_type(type)
|
|
|
|
, m_value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-27 21:10:21 +02:00
|
|
|
Frequency Frequency::make_hertz(double value)
|
2022-02-21 17:49:47 +00:00
|
|
|
{
|
|
|
|
return { value, Type::Hz };
|
|
|
|
}
|
|
|
|
|
|
|
|
Frequency Frequency::percentage_of(Percentage const& percentage) const
|
|
|
|
{
|
|
|
|
return Frequency { percentage.as_fraction() * m_value, m_type };
|
|
|
|
}
|
|
|
|
|
2025-05-16 20:02:16 +01:00
|
|
|
String Frequency::to_string(SerializationMode serialization_mode) const
|
2022-02-21 17:49:47 +00:00
|
|
|
{
|
2025-05-16 20:02:16 +01:00
|
|
|
if (serialization_mode == SerializationMode::ResolvedValue)
|
|
|
|
return MUST(String::formatted("{}hz", to_hertz()));
|
|
|
|
return MUST(String::formatted("{}{}", raw_value(), unit_name()));
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 21:10:21 +02:00
|
|
|
double Frequency::to_hertz() const
|
2022-02-21 17:49:47 +00:00
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Hz:
|
|
|
|
return m_value;
|
|
|
|
case Type::kHz:
|
|
|
|
return m_value * 1000;
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView Frequency::unit_name() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Hz:
|
|
|
|
return "hz"sv;
|
|
|
|
case Type::kHz:
|
|
|
|
return "khz"sv;
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<Frequency::Type> Frequency::unit_from_name(StringView name)
|
|
|
|
{
|
2023-03-10 08:48:54 +01:00
|
|
|
if (name.equals_ignoring_ascii_case("hz"sv)) {
|
2022-02-21 17:49:47 +00:00
|
|
|
return Type::Hz;
|
2023-03-10 08:48:54 +01:00
|
|
|
} else if (name.equals_ignoring_ascii_case("khz"sv)) {
|
2022-02-21 17:49:47 +00:00
|
|
|
return Type::kHz;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2025-04-15 15:18:27 -06:00
|
|
|
Frequency Frequency::resolve_calculated(NonnullRefPtr<CalculatedStyleValue const> const& calculated, Layout::Node const& layout_node, Frequency const& reference_value)
|
2024-08-02 14:28:24 +02:00
|
|
|
{
|
2025-07-02 19:12:33 +12:00
|
|
|
return calculated->resolve_frequency_deprecated(
|
LibWeb/CSS: Wrap calc()-resolution data in a struct
Initially I added this to the existing CalculationContext, but in
reality, we have some data at parse-time and different data at
resolve-time, so it made more sense to keep those separate.
Instead of needing a variety of methods for resolving a Foo, depending
on whether we have a Layout::Node available, or a percentage basis, or
a length resolution context... put those in a
CalculationResolutionContext, and just pass that one thing to these
methods. This also removes the need for separate resolve_*_percentage()
methods, because we can just pass the percentage basis in to the regular
resolve_foo() method.
This also corrects the issue that *any* calculation may need to resolve
lengths, but we previously only passed a length resolution context to
specific types in some situations. Now, they can all have one available,
though it's up to the caller to provide it.
2025-01-22 16:05:32 +00:00
|
|
|
{
|
|
|
|
.percentage_basis = reference_value,
|
|
|
|
.length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node),
|
|
|
|
})
|
|
|
|
.value();
|
2024-08-02 14:28:24 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|