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>
|
2025-08-18 13:04:15 +01:00
|
|
|
#include <LibWeb/CSS/Serialize.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 {
|
|
|
|
|
2025-09-02 13:48:49 +01:00
|
|
|
Frequency::Frequency(double value, FrequencyUnit unit)
|
|
|
|
: m_unit(unit)
|
2022-02-21 17:49:47 +00:00
|
|
|
, 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
|
|
|
{
|
2025-09-02 13:48:49 +01:00
|
|
|
return { value, FrequencyUnit::Hz };
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Frequency Frequency::percentage_of(Percentage const& percentage) const
|
|
|
|
{
|
2025-09-02 13:48:49 +01:00
|
|
|
return Frequency { percentage.as_fraction() * m_value, m_unit };
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
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-08-18 13:04:15 +01:00
|
|
|
// https://drafts.csswg.org/cssom/#serialize-a-css-value
|
|
|
|
// -> <frequency>
|
|
|
|
// The <number> component serialized as per <number> followed by the unit in its canonical form as defined in its
|
|
|
|
// respective specification.
|
|
|
|
if (serialization_mode == SerializationMode::ResolvedValue) {
|
|
|
|
StringBuilder builder;
|
|
|
|
serialize_a_number(builder, to_hertz());
|
|
|
|
builder.append("hz"sv);
|
|
|
|
return builder.to_string_without_validation();
|
|
|
|
}
|
|
|
|
StringBuilder builder;
|
|
|
|
serialize_a_number(builder, raw_value());
|
|
|
|
builder.append(unit_name());
|
|
|
|
return builder.to_string_without_validation();
|
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
|
|
|
{
|
2025-09-04 12:27:20 +01:00
|
|
|
return ratio_between_units(m_unit, FrequencyUnit::Hz) * m_value;
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
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-09-05 16:22:17 +02:00
|
|
|
CalculationResolutionContext context {
|
|
|
|
.percentage_basis = reference_value,
|
|
|
|
.length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node),
|
|
|
|
};
|
|
|
|
return calculated->resolve_frequency(context).value();
|
2024-08-02 14:28:24 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:49:47 +00:00
|
|
|
}
|