Truly independent web browser
LibWeb: Rotate <animateMotion> targets as its rotate attribute says
Jelle Raaijmakers11fe8222b92e
diff --git a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cppindex 628a0090217..e7ffb4fc98f 100644--- a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp+++ b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp@@ -6,11 +6,13 @@#include <AK/CharacterTypes.h>#include <LibGfx/Path.h>+#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>#include <LibWeb/CSS/StyleValues/StyleValue.h>#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/SVG/AttributeNames.h>+#include <LibWeb/SVG/AttributeParsing.h>#include <LibWeb/SVG/Path.h>#include <LibWeb/SVG/SVGAnimateMotionElement.h>#include <LibWeb/SVG/SVGGeometryElement.h>@@ -28,7 +30,7 @@ SVGAnimateMotionElement::SVGAnimateMotionElement(DOM::Document& document, DOM::Qvoid SVGAnimateMotionElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_){Base::attribute_changed(name, old_value, value, namespace_);- if (first_is_one_of(name, AttributeNames::from, AttributeNames::path, AttributeNames::to) && !namespace_.has_value())+ if (first_is_one_of(name, AttributeNames::from, AttributeNames::path, AttributeNames::rotate, AttributeNames::to) && !namespace_.has_value())update_animation();}@@ -98,8 +100,96 @@ static NonnullRefPtr<CSS::StyleValue const> translation(Gfx::FloatPoint point){ CSS::LengthStyleValue::create(CSS::Length::make_px(point.x())), CSS::LengthStyleValue::create(CSS::Length::make_px(point.y())) });}+static Optional<Gfx::FloatPoint> translation_in_pixels(CSS::StyleValue const& translation)+{+ if (!translation.is_transformation())+ return {};+ auto values = translation.as_transformation().values();+ if (values.size() != 2 || !values[0]->is_length() || !values[1]->is_length())+ return {};+ auto x = values[0]->as_length().length();+ auto y = values[1]->as_length().length();+ if (!x.is_absolute() || !y.is_absolute())+ return {};+ return Gfx::FloatPoint { x.absolute_length_to_px_without_rounding(), y.absolute_length_to_px_without_rounding() };+}++namespace {++struct MotionRotation {+ bool follows_direction { false };+ double degrees { 0 };++ bool is_none() const { return !follows_direction && degrees == 0; }+};++// Rotations along the motion path, each within half a turn of the previous one so that the target turns the short way+// between them.+class MotionRotations {+public:+ explicit MotionRotations(MotionRotation rotation)+ : m_rotation(rotation)+ {+ }++ NonnullRefPtr<CSS::StyleValue const> rotation_for(Gfx::FloatVector2 direction)+ {+ auto degrees = m_rotation.degrees;+ if (m_rotation.follows_direction)+ degrees += AK::to_degrees(AK::atan2(static_cast<double>(direction.y()), static_cast<double>(direction.x())));+ if (m_previous_degrees.has_value())+ degrees -= 360 * AK::round((degrees - *m_previous_degrees) / 360);+ m_previous_degrees = degrees;+ return CSS::TransformationStyleValue::create(CSS::PropertyID::Rotate, CSS::TransformFunction::Rotate,+ { CSS::AngleStyleValue::create(CSS::Angle::make_degrees(degrees)) });+ }++private:+ MotionRotation m_rotation;+ Optional<double> m_previous_degrees;+};++}++// https://svgwg.org/specs/animations/#RotateAttribute+static MotionRotation motion_rotation(DOM::Element const& element)+{+ // The ‘rotate’ attribute post-multiplies a supplemental transformation matrix onto the CTM of the target element to+ // apply a rotation transformation about the origin of the current user coordinate system. The rotation+ // transformation is applied after the supplemental translation transformation that is computed due to the ‘path’+ // attribute.+ // AD-HOC: Animate the rotate property for this, which applies after the translate property. Unlike the supplemental+ // transformation matrix, it rotates about the transform origin of the target, which is the origin of its+ // user coordinate system unless the transform-origin property says otherwise, and it replaces any rotate+ // property of the target.+ auto rotate = element.get_attribute(AttributeNames::rotate);+ if (!rotate.has_value())+ return {};++ // auto+ // Indicates that the object is rotated over time by the angle of the direction (i.e., directional tangent+ // vector) of the motion path.+ if (*rotate == "auto"sv)+ return { .follows_direction = true, .degrees = 0 };++ // auto-reverse+ // Indicates that the object is rotated over time by the angle of the direction (i.e., directional tangent+ // vector) of the motion path plus 180 degrees.+ if (*rotate == "auto-reverse"sv)+ return { .follows_direction = true, .degrees = 180 };++ // <number>+ // Indicates that the target element has a constant rotation transformation applied to it, where the rotation+ // angle is the specified number of degrees.+ if (auto number = parse_number_percentage(*rotate); number.has_value() && !number->is_percentage() && isfinite(number->value()))+ return { .follows_direction = false, .degrees = number->value() };++ // NB: SMIL Animation ignores a value with any error in its syntax, which leaves the initial value of 0.+ return {};+}+// https://svgwg.org/specs/animations/#AnimateMotionElement-Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes_along_motion_path(Gfx::Path const& motion_path, CSS::PropertyID property_id)+Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes_along_motion_path(Gfx::Path const& motion_path, CSS::PropertyID property_id) const{// NB: Follow polylines through the motion path, with enough points along its curves to stay as close to them as the// length of the motion path is measured.@@ -114,10 +204,16 @@ Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes_along_motion// distance along the path algorithm.// NB: Each vertex gets the fraction of the length of the motion path it is along as its offset, between which the// target moves in straight lines. A motion path without length keeps the target at its initial point.+ auto rotation = motion_rotation(*this);+ MotionRotations rotations { rotation };auto length = vertices.last().distance;if (length == 0) {- auto initial_point = translation(vertices.first().point);- return { keyframe(0, property_id, initial_point), keyframe(1, property_id, initial_point) };+ Vector<Animations::BaseKeyframe> keyframes { keyframe(0, property_id, translation(vertices.first().point)), keyframe(1, property_id, translation(vertices.first().point)) };+ if (!rotation.is_none()) {+ for (auto& keyframe : keyframes)+ set_keyframe_property(keyframe, CSS::PropertyID::Rotate, rotations.rotation_for(vertices.first().direction));+ }+ return keyframes;}// When a path is combined with a "paced" ‘calcMode’ setting, all "move to" commands are considered to have 0 length@@ -125,10 +221,15 @@ Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes_along_motion// NB: The vertices where a subpath ends and where the next one starts are at the same distance, so the target jumps// between them.// FIXME: Divide the duration between the points of the path for the other calcMode values.+ // NB: Where the motion path changes direction at once, its vertex appears twice, so the target turns at once.Vector<Animations::BaseKeyframe> keyframes;keyframes.ensure_capacity(vertices.size());- for (auto const& vertex : vertices)- keyframes.unchecked_append(keyframe(vertex.distance / length, property_id, translation(vertex.point)));+ for (auto const& vertex : vertices) {+ auto vertex_keyframe = keyframe(vertex.distance / length, property_id, translation(vertex.point));+ if (!rotation.is_none())+ set_keyframe_property(vertex_keyframe, CSS::PropertyID::Rotate, rotations.rotation_for(vertex.direction));+ keyframes.unchecked_append(move(vertex_keyframe));+ }return keyframes;}@@ -138,7 +239,7 @@ Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes(SVGElement c// Regarding the definition of the motion path, the ‘mpath’ element overrides the ‘path’ attribute, which overrides// ‘values’, which overrides ‘from’, ‘by’ and ‘to’.// FIXME: Take the motion path from the values and by attributes.- // FIXME: Apply the keyPoints and rotate attributes.+ // FIXME: Apply the keyPoints attribute.if (m_referenced_motion_path.has_value())return keyframes_along_motion_path(*m_referenced_motion_path, property_id);if (auto path = get_attribute(AttributeNames::path); path.has_value())@@ -149,15 +250,43 @@ Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes(SVGElement cif (!to_translation)return {};+ auto from = get_attribute(AttributeNames::from);+ RefPtr<CSS::StyleValue const> from_translation;+ if (from.has_value()) {+ from_translation = parse_coordinate_pair_as_translation(target, *from);+ if (!from_translation)+ return {};+ }+// NB: Without a from value, this is a to animation, which animates from the underlying value. The keyframe effect// takes its missing start keyframe from that value.- auto from = get_attribute(AttributeNames::from);- if (!from.has_value())- return { keyframe(1, property_id, *to_translation) };- auto from_translation = parse_coordinate_pair_as_translation(target, *from);- if (!from_translation)- return {};- return { keyframe(0, property_id, *from_translation), keyframe(1, property_id, *to_translation) };+ Vector<Animations::BaseKeyframe> keyframes;+ if (from_translation)+ keyframes.append(keyframe(0, property_id, *from_translation));+ keyframes.append(keyframe(1, property_id, *to_translation));++ auto rotation = motion_rotation(*this);+ if (rotation.is_none())+ return keyframes;++ // NB: The underlying value of a motion is no translation at all, so a to animation moves from the origin. Only+ // coordinates in absolute lengths give the motion a known direction. Rotations do not come from the underlying+ // value, so a to animation gets a start keyframe for its rotation alone.+ auto from_point = from_translation ? translation_in_pixels(*from_translation) : Gfx::FloatPoint {};+ auto to_point = translation_in_pixels(*to_translation);+ Gfx::FloatVector2 direction;+ if (from_point.has_value() && to_point.has_value())+ direction = { to_point->x() - from_point->x(), to_point->y() - from_point->y() };+ if (!from_translation) {+ Animations::BaseKeyframe rotation_keyframe;+ rotation_keyframe.offset = 0;+ rotation_keyframe.properties = Animations::BaseKeyframe::ParsedProperties {};+ keyframes.prepend(move(rotation_keyframe));+ }+ MotionRotations rotations { rotation };+ for (auto& keyframe : keyframes)+ set_keyframe_property(keyframe, CSS::PropertyID::Rotate, rotations.rotation_for(direction));+ return keyframes;}}diff --git a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.hindex 83007ab0481..cf89e728577 100644--- a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h+++ b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h@@ -26,7 +26,7 @@ private:virtual Optional<CSS::PropertyID> target_property() const override;virtual Vector<Animations::BaseKeyframe> keyframes(SVGElement const& target, CSS::PropertyID) const override;- static Vector<Animations::BaseKeyframe> keyframes_along_motion_path(Gfx::Path const&, CSS::PropertyID);+ Vector<Animations::BaseKeyframe> keyframes_along_motion_path(Gfx::Path const&, CSS::PropertyID) const;Optional<Gfx::Path> m_referenced_motion_path;};diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp b/Libraries/LibWeb/SVG/SVGAnimationElement.cppindex 04264764b52..b40accec343 100644--- a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.cpp@@ -155,12 +155,16 @@ Animations::BaseKeyframe SVGAnimationElement::keyframe(double offset, CSS::Prope{Animations::BaseKeyframe keyframe;keyframe.offset = offset;- Animations::BaseKeyframe::ParsedProperties properties;- properties.set(CSS::PropertyNameAndID::from_id(property_id), CSS::RustStyleValueHandle::retained(value.rust_style_value_data()));- keyframe.properties = move(properties);+ keyframe.properties = Animations::BaseKeyframe::ParsedProperties {};+ set_keyframe_property(keyframe, property_id, value);return keyframe;}+void SVGAnimationElement::set_keyframe_property(Animations::BaseKeyframe& keyframe, CSS::PropertyID property_id, CSS::StyleValue const& value)+{+ keyframe.parsed_properties().set(CSS::PropertyNameAndID::from_id(property_id), CSS::RustStyleValueHandle::retained(value.rust_style_value_data()));+}+void SVGAnimationElement::update_animation(){if (m_animation) {diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.h b/Libraries/LibWeb/SVG/SVGAnimationElement.hindex 662afc295c6..79fc693d819 100644--- a/Libraries/LibWeb/SVG/SVGAnimationElement.h+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.h@@ -45,6 +45,7 @@ protected:virtual Optional<CSS::PropertyID> target_property() const;virtual Vector<Animations::BaseKeyframe> keyframes(SVGElement const& target, CSS::PropertyID) const = 0;static Animations::BaseKeyframe keyframe(double offset, CSS::PropertyID, CSS::StyleValue const&);+ static void set_keyframe_property(Animations::BaseKeyframe&, CSS::PropertyID, CSS::StyleValue const&);void update_animation();diff --git a/Tests/LibWeb/Text/expected/SVG/animate-motion-element-rotate.txt b/Tests/LibWeb/Text/expected/SVG/animate-motion-element-rotate.txtnew file mode 100644index 00000000000..b6d348ec1af--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/animate-motion-element-rotate.txt@@ -0,0 +1,18 @@+t=0.5+ corner: 50.0, 0.0, 0.0deg+ curve: 85.4, 85.4, 135.0deg+ auto-reverse: 0.0, 25.0, -90.0deg+ mpath: 0.0, 75.0, -90.0deg+ from-to: 25.0, 25.0, 45.0deg+ to: 0.0, 25.0, 90.0deg+ to-with-angle: 25.0, 0.0, 30.0deg+ relative-lengths: 0.0, 40.0, 0.0deg+t=1.5+ corner: 100.0, 50.0, 90.0deg+ curve: 14.6, 85.4, -135.0deg+ auto-reverse: 0.0, 75.0, -90.0deg+ mpath: 0.0, 25.0, -90.0deg+ from-to: 75.0, 75.0, 45.0deg+ to: 0.0, 75.0, 90.0deg+ to-with-angle: 75.0, 0.0, 30.0deg+ relative-lengths: 0.0, 120.0, 0.0degdiff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-rotate-angle.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-rotate-angle.txtindex 7f334f81554..ab5e64b5d85 100644--- a/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-rotate-angle.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-rotate-angle.txt@@ -2,12 +2,11 @@ Harness status: OKFound 7 tests-3 Pass-4 Fail-Fail rotate="45" applies a constant 45 degree rotation-Fail rotate="-90" applies a constant -90 degree rotation-Fail rotate="30" applies a constant 30 degree rotation to a path animation+7 Pass+Pass rotate="45" applies a constant 45 degree rotation+Pass rotate="-90" applies a constant -90 degree rotation+Pass rotate="30" applies a constant 30 degree rotation to a path animationPass rotate="0" applies no rotationPass an absent rotate attribute applies no rotationPass an unparseable rotate value applies no rotation-Fail rotate="auto" follows the direction of motion\ No newline at end of file+Pass rotate="auto" follows the direction of motion\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/SVG/animate-motion-element-rotate.html b/Tests/LibWeb/Text/input/SVG/animate-motion-element-rotate.htmlnew file mode 100644index 00000000000..65e74be68d8--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/animate-motion-element-rotate.html@@ -0,0 +1,30 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<svg id="svg" width="400" height="400">+ <rect id="corner" width="10" height="10"><animateMotion path="M0,0 L100,0 L100,100" rotate="auto" dur="2s"/></rect>+ <rect id="curve" width="10" height="10"><animateMotion path="M100,50 A50,50 0 0 1 0,50" rotate="auto" dur="2s"/></rect>+ <rect id="auto-reverse" width="10" height="10"><animateMotion path="M0,0 L0,100" rotate="auto-reverse" dur="2s"/></rect>+ <rect id="mpath" width="10" height="10"><animateMotion rotate="auto" dur="2s"><mpath href="#motion-path"/></animateMotion></rect>+ <rect id="from-to" width="10" height="10"><animateMotion from="0,0" to="100,100" rotate="auto" dur="2s"/></rect>+ <rect id="to" width="10" height="10"><animateMotion to="0,100" rotate="auto" dur="2s"/></rect>+ <rect id="to-with-angle" width="10" height="10"><animateMotion to="100,0" rotate="30" dur="2s"/></rect>+ <rect id="relative-lengths" width="10" height="10"><animateMotion from="0,0" to="0,10em" rotate="auto" dur="2s"/></rect>+ <path id="motion-path" d="M0,100 L0,0" fill="none"/>+</svg>+<script>+ asyncTest(async done => {+ await new Promise(resolve => window.addEventListener("load", resolve));+ await new Promise(requestAnimationFrame);+ svg.pauseAnimations();+ for (const time of [0.5, 1.5]) {+ svg.setCurrentTime(time);+ println(`t=${time}`);+ for (const rect of document.querySelectorAll("rect")) {+ const matrix = rect.getCTM();+ const angle = Math.atan2(matrix.b, matrix.a) * 180 / Math.PI;+ println(` ${rect.id}: ${matrix.e.toFixed(1)}, ${matrix.f.toFixed(1)}, ${angle.toFixed(1)}deg`);+ }+ }+ done();+ });+</script>