2023-07-05 19:59:36 -03:00
|
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2023-07-05 19:59:36 -03:00
|
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
|
|
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
|
|
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
|
|
|
|
|
* Copyright (c) 2023, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "EasingStyleValue.h"
|
2024-06-15 01:50:25 -03:00
|
|
|
|
#include <AK/BinarySearch.h>
|
2023-07-05 19:59:36 -03:00
|
|
|
|
#include <AK/StringBuilder.h>
|
2025-06-17 05:46:51 -03:00
|
|
|
|
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
|
2025-10-09 07:40:46 -03:00
|
|
|
|
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
|
2023-07-05 19:59:36 -03:00
|
|
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
2024-11-03 00:56:16 -03:00
|
|
|
|
// https://drafts.csswg.org/css-easing/#linear-easing-function-serializing
|
2025-10-09 07:40:46 -03:00
|
|
|
|
String EasingStyleValue::Linear::to_string(SerializationMode mode) const
|
2024-11-02 20:36:44 -03:00
|
|
|
|
{
|
2024-11-03 00:56:16 -03:00
|
|
|
|
// To serialize a linear() function:
|
|
|
|
|
|
// 1. Let s be the string "linear(".
|
2024-11-02 20:36:44 -03:00
|
|
|
|
StringBuilder builder;
|
2024-11-03 00:56:16 -03:00
|
|
|
|
builder.append("linear("sv);
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Serialize each control point of the function,
|
|
|
|
|
|
// concatenate the results using the separator ", ",
|
|
|
|
|
|
// and append the result to s.
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
for (auto stop : stops) {
|
|
|
|
|
|
if (first) {
|
2024-11-02 20:36:44 -03:00
|
|
|
|
first = false;
|
2024-11-03 00:56:16 -03:00
|
|
|
|
} else {
|
|
|
|
|
|
builder.append(", "sv);
|
2024-11-02 20:36:44 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-03 00:56:16 -03:00
|
|
|
|
// To serialize a linear() control point:
|
|
|
|
|
|
// 1. Let s be the serialization, as a <number>, of the control point’s output progress value.
|
2025-10-09 07:40:46 -03:00
|
|
|
|
builder.appendff(stop.output->to_string(mode));
|
2024-11-03 00:56:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 2. If the control point originally lacked an input progress value, return s.
|
|
|
|
|
|
// 3. Otherwise, append " " (U+0020 SPACE) to s,
|
|
|
|
|
|
// then serialize the control point’s input progress value as a <percentage> and append it to s.
|
2025-10-09 07:40:46 -03:00
|
|
|
|
if (stop.input) {
|
|
|
|
|
|
builder.appendff(" {}", stop.input->to_string(mode));
|
2024-11-03 00:56:16 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Return s.
|
2024-11-02 20:36:44 -03:00
|
|
|
|
}
|
2024-11-03 00:56:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 4. Append ")" to s, and return it.
|
|
|
|
|
|
builder.append(')');
|
2024-11-02 20:36:44 -03:00
|
|
|
|
return MUST(builder.to_string());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-18 12:41:33 -03:00
|
|
|
|
// https://drafts.csswg.org/css-easing/#bezier-serialization
|
2025-06-17 18:51:54 -03:00
|
|
|
|
String EasingStyleValue::CubicBezier::to_string(SerializationMode mode) const
|
2024-11-02 20:36:44 -03:00
|
|
|
|
{
|
2025-10-11 07:34:35 -03:00
|
|
|
|
return MUST(String::formatted("cubic-bezier({}, {}, {}, {})", x1->to_string(mode), y1->to_string(mode), x2->to_string(mode), y2->to_string(mode)));
|
2024-11-02 20:36:44 -03:00
|
|
|
|
}
|
2024-06-15 01:50:25 -03:00
|
|
|
|
|
2024-11-18 12:41:33 -03:00
|
|
|
|
// https://drafts.csswg.org/css-easing/#steps-serialization
|
2025-06-17 05:46:51 -03:00
|
|
|
|
String EasingStyleValue::Steps::to_string(SerializationMode mode) const
|
2024-11-02 20:36:44 -03:00
|
|
|
|
{
|
2025-10-11 07:34:35 -03:00
|
|
|
|
auto position = [&] -> Optional<StringView> {
|
|
|
|
|
|
if (first_is_one_of(this->position, StepPosition::JumpEnd, StepPosition::End))
|
|
|
|
|
|
return {};
|
|
|
|
|
|
return CSS::to_string(this->position);
|
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
|
|
if (position.has_value()) {
|
|
|
|
|
|
return MUST(String::formatted("steps({}, {})", number_of_intervals->to_string(mode), position.value()));
|
2024-11-02 20:36:44 -03:00
|
|
|
|
}
|
2025-10-11 07:34:35 -03:00
|
|
|
|
|
|
|
|
|
|
return MUST(String::formatted("steps({})", number_of_intervals->to_string(mode)));
|
2024-11-02 20:36:44 -03:00
|
|
|
|
}
|
2024-06-15 01:50:25 -03:00
|
|
|
|
|
2025-06-17 05:46:51 -03:00
|
|
|
|
String EasingStyleValue::Function::to_string(SerializationMode mode) const
|
2023-07-05 19:59:36 -03:00
|
|
|
|
{
|
2024-11-02 20:36:44 -03:00
|
|
|
|
return visit(
|
|
|
|
|
|
[&](auto const& curve) {
|
2025-06-17 05:46:51 -03:00
|
|
|
|
return curve.to_string(mode);
|
2024-06-15 01:27:23 -03:00
|
|
|
|
});
|
2023-07-05 19:59:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 08:48:32 -03:00
|
|
|
|
ValueComparingNonnullRefPtr<StyleValue const> EasingStyleValue::absolutized(ComputationContext const& computation_context) const
|
2025-10-09 07:40:46 -03:00
|
|
|
|
{
|
|
|
|
|
|
auto const& absolutized_function = m_function.visit(
|
|
|
|
|
|
[&](Linear const& linear) -> Function {
|
|
|
|
|
|
Vector<Linear::Stop> absolutized_stops;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto stop : linear.stops) {
|
|
|
|
|
|
RefPtr<StyleValue const> absolutized_input;
|
|
|
|
|
|
|
|
|
|
|
|
if (stop.input)
|
2025-10-21 08:48:32 -03:00
|
|
|
|
absolutized_input = stop.input->absolutized(computation_context);
|
2025-10-09 07:40:46 -03:00
|
|
|
|
|
2025-10-21 08:48:32 -03:00
|
|
|
|
absolutized_stops.append({ stop.output->absolutized(computation_context), absolutized_input });
|
2025-10-09 07:40:46 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Linear { absolutized_stops };
|
|
|
|
|
|
},
|
|
|
|
|
|
[&](CubicBezier const& cubic_bezier) -> Function {
|
2025-10-10 23:09:11 -03:00
|
|
|
|
return CubicBezier {
|
2025-10-21 08:48:32 -03:00
|
|
|
|
cubic_bezier.x1->absolutized(computation_context),
|
|
|
|
|
|
cubic_bezier.y1->absolutized(computation_context),
|
|
|
|
|
|
cubic_bezier.x2->absolutized(computation_context),
|
|
|
|
|
|
cubic_bezier.y2->absolutized(computation_context)
|
2025-10-10 23:09:11 -03:00
|
|
|
|
};
|
2025-10-09 07:40:46 -03:00
|
|
|
|
},
|
|
|
|
|
|
[&](Steps const& steps) -> Function {
|
2025-10-10 23:09:11 -03:00
|
|
|
|
return Steps {
|
2025-10-21 08:48:32 -03:00
|
|
|
|
steps.number_of_intervals->absolutized(computation_context),
|
2025-10-10 23:09:11 -03:00
|
|
|
|
steps.position
|
|
|
|
|
|
};
|
2025-10-09 07:40:46 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return EasingStyleValue::create(absolutized_function);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-05 19:59:36 -03:00
|
|
|
|
}
|