| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qsvganimatedproperty_p.h" |
| 5 | #include <QtCore/qpoint.h> |
| 6 | #include <QtGui/qcolor.h> |
| 7 | #include <QtGui/qtransform.h> |
| 8 | #include <QtCore/qloggingcategory.h> |
| 9 | #include <QtCore/qglobalstatic.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | Q_STATIC_LOGGING_CATEGORY(lcSvgAnimatedProperty, "qt.svg.animation.properties" ) |
| 14 | |
| 15 | typedef QHash<QString, QSvgAbstractAnimatedProperty::Type> AnimatableHashType; |
| 16 | Q_GLOBAL_STATIC(AnimatableHashType, animatableProperties) |
| 17 | |
| 18 | static void initHash() |
| 19 | { |
| 20 | animatableProperties->insert(QStringLiteral("fill" ), value: QSvgAbstractAnimatedProperty::Color); |
| 21 | animatableProperties->insert(QStringLiteral("fill-opacity" ), value: QSvgAbstractAnimatedProperty::Float); |
| 22 | animatableProperties->insert(QStringLiteral("stroke-opacity" ), value: QSvgAbstractAnimatedProperty::Float); |
| 23 | animatableProperties->insert(QStringLiteral("stroke" ), value: QSvgAbstractAnimatedProperty::Color); |
| 24 | animatableProperties->insert(QStringLiteral("opacity" ), value: QSvgAbstractAnimatedProperty::Float); |
| 25 | animatableProperties->insert(QStringLiteral("transform" ), value: QSvgAbstractAnimatedProperty::Transform); |
| 26 | } |
| 27 | |
| 28 | static qreal q_lerp(qreal a, qreal b, qreal t) |
| 29 | { |
| 30 | return a + (b - a) * t; |
| 31 | } |
| 32 | |
| 33 | static QPointF pointInterpolator(QPointF v1, QPointF v2, qreal t) |
| 34 | { |
| 35 | qreal x = q_lerp(a: v1.x(), b: v2.x(), t); |
| 36 | qreal y = q_lerp(a: v1.y(), b: v2.y(), t); |
| 37 | |
| 38 | return QPointF(x, y); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | QSvgAbstractAnimatedProperty::QSvgAbstractAnimatedProperty(const QString &name, Type type) |
| 43 | : m_propertyName(name) |
| 44 | , m_type(type) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | QSvgAbstractAnimatedProperty::~QSvgAbstractAnimatedProperty() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void QSvgAbstractAnimatedProperty::setKeyFrames(const QList<qreal> &keyFrames) |
| 53 | { |
| 54 | m_keyFrames = keyFrames; |
| 55 | } |
| 56 | |
| 57 | void QSvgAbstractAnimatedProperty::appendKeyFrame(qreal keyFrame) |
| 58 | { |
| 59 | m_keyFrames.append(t: keyFrame); |
| 60 | } |
| 61 | |
| 62 | QList<qreal> QSvgAbstractAnimatedProperty::keyFrames() const |
| 63 | { |
| 64 | return m_keyFrames; |
| 65 | } |
| 66 | |
| 67 | void QSvgAbstractAnimatedProperty::setPropertyName(const QString &name) |
| 68 | { |
| 69 | m_propertyName = name; |
| 70 | } |
| 71 | |
| 72 | QStringView QSvgAbstractAnimatedProperty::propertyName() const |
| 73 | { |
| 74 | return m_propertyName; |
| 75 | } |
| 76 | |
| 77 | QSvgAbstractAnimatedProperty::Type QSvgAbstractAnimatedProperty::type() const |
| 78 | { |
| 79 | return m_type; |
| 80 | } |
| 81 | |
| 82 | QVariant QSvgAbstractAnimatedProperty::interpolatedValue() const |
| 83 | { |
| 84 | return m_interpolatedValue; |
| 85 | } |
| 86 | |
| 87 | QSvgAbstractAnimatedProperty *QSvgAbstractAnimatedProperty::createAnimatedProperty(const QString &name) |
| 88 | { |
| 89 | if (animatableProperties->isEmpty()) |
| 90 | initHash(); |
| 91 | |
| 92 | if (!animatableProperties->contains(key: name)) { |
| 93 | qCDebug(lcSvgAnimatedProperty) << "Property : " << name << " is not animatable" ; |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | QSvgAbstractAnimatedProperty::Type type = animatableProperties->value(key: name); |
| 98 | QSvgAbstractAnimatedProperty *prop = nullptr; |
| 99 | |
| 100 | switch (type) { |
| 101 | case QSvgAbstractAnimatedProperty::Color: |
| 102 | prop = new QSvgAnimatedPropertyColor(name); |
| 103 | break; |
| 104 | case QSvgAbstractAnimatedProperty::Transform: |
| 105 | prop = new QSvgAnimatedPropertyTransform(name); |
| 106 | break; |
| 107 | case QSvgAbstractAnimatedProperty::Float: |
| 108 | prop = new QSvgAnimatedPropertyFloat(name); |
| 109 | default: |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | return prop; |
| 114 | } |
| 115 | |
| 116 | QSvgAnimatedPropertyColor::QSvgAnimatedPropertyColor(const QString &name) |
| 117 | : QSvgAbstractAnimatedProperty(name, QSvgAbstractAnimatedProperty::Color) |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | void QSvgAnimatedPropertyColor::setColors(const QList<QColor> &colors) |
| 122 | { |
| 123 | m_colors = colors; |
| 124 | } |
| 125 | |
| 126 | void QSvgAnimatedPropertyColor::appendColor(const QColor &color) |
| 127 | { |
| 128 | m_colors.append(t: color); |
| 129 | } |
| 130 | |
| 131 | QList<QColor> QSvgAnimatedPropertyColor::colors() const |
| 132 | { |
| 133 | return m_colors; |
| 134 | } |
| 135 | |
| 136 | void QSvgAnimatedPropertyColor::interpolate(uint index, qreal t) const |
| 137 | { |
| 138 | QColor c1 = m_colors.at(i: index - 1); |
| 139 | QColor c2 = m_colors.at(i: index); |
| 140 | |
| 141 | int alpha = q_lerp(a: c1.alpha(), b: c2.alpha(), t); |
| 142 | int red = q_lerp(a: c1.red(), b: c2.red(), t); |
| 143 | int green = q_lerp(a: c1.green(), b: c2.green(), t); |
| 144 | int blue = q_lerp(a: c1.blue(), b: c2.blue(), t); |
| 145 | |
| 146 | m_interpolatedValue = QColor(red, green, blue, alpha); |
| 147 | } |
| 148 | |
| 149 | QSvgAnimatedPropertyFloat::QSvgAnimatedPropertyFloat(const QString &name) |
| 150 | : QSvgAbstractAnimatedProperty(name, QSvgAbstractAnimatedProperty::Float) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | void QSvgAnimatedPropertyFloat::setValues(const QList<qreal> &values) |
| 155 | { |
| 156 | m_values = values; |
| 157 | } |
| 158 | |
| 159 | void QSvgAnimatedPropertyFloat::appendValue(const qreal value) |
| 160 | { |
| 161 | m_values.append(t: value); |
| 162 | } |
| 163 | |
| 164 | QList<qreal> QSvgAnimatedPropertyFloat::values() const |
| 165 | { |
| 166 | return m_values; |
| 167 | } |
| 168 | |
| 169 | void QSvgAnimatedPropertyFloat::interpolate(uint index, qreal t) const |
| 170 | { |
| 171 | if (index >= (uint)m_keyFrames.size()) { |
| 172 | qCWarning(lcSvgAnimatedProperty) << "Invalid index for key frames" ; |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | qreal float1 = m_values.at(i: index - 1); |
| 177 | qreal float2 = m_values.at(i: index); |
| 178 | |
| 179 | m_interpolatedValue = q_lerp(a: float1, b: float2, t); |
| 180 | } |
| 181 | |
| 182 | QSvgAnimatedPropertyTransform::QSvgAnimatedPropertyTransform(const QString &name) |
| 183 | : QSvgAbstractAnimatedProperty(name, QSvgAbstractAnimatedProperty::Transform) |
| 184 | { |
| 185 | |
| 186 | } |
| 187 | |
| 188 | void QSvgAnimatedPropertyTransform::setTransformCount(quint32 count) |
| 189 | { |
| 190 | m_transformCount = count; |
| 191 | } |
| 192 | |
| 193 | quint32 QSvgAnimatedPropertyTransform::transformCount() const |
| 194 | { |
| 195 | return m_transformCount; |
| 196 | } |
| 197 | |
| 198 | void QSvgAnimatedPropertyTransform::appendComponents(const QList<TransformComponent> &components) |
| 199 | { |
| 200 | m_components.append(l: components); |
| 201 | } |
| 202 | |
| 203 | QList<QSvgAnimatedPropertyTransform::TransformComponent> QSvgAnimatedPropertyTransform::components() const |
| 204 | { |
| 205 | return m_components; |
| 206 | } |
| 207 | |
| 208 | // this function iterates over all TransformComponents in two consecutive |
| 209 | // key frames and interpolate between all TransformComponents. Moreover, |
| 210 | // it requires all key frames to have the same number of TransformComponents. |
| 211 | // This must be ensured by the parser itself, and it is handled in validateTransform |
| 212 | // function in qsvgcsshandler.cpp and in createAnimateTransformNode function |
| 213 | // in qsvghandler.cpp. |
| 214 | void QSvgAnimatedPropertyTransform::interpolate(uint index, qreal t) const |
| 215 | { |
| 216 | if (index >= (uint)m_keyFrames.size()) { |
| 217 | qCWarning(lcSvgAnimatedProperty) << "Invalid index for key frames" ; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (!m_transformCount || |
| 222 | ((m_components.size() / qsizetype(m_transformCount)) != m_keyFrames.size())) { |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | QTransform transform = QTransform(); |
| 227 | |
| 228 | qsizetype startIndex = (index - 1) * qsizetype(m_transformCount); |
| 229 | qsizetype endIndex = index * qsizetype(m_transformCount); |
| 230 | |
| 231 | for (quint32 i = 0; i < m_transformCount; i++) { |
| 232 | TransformComponent tc1 = m_components.at(i: startIndex + i); |
| 233 | TransformComponent tc2 = m_components.at(i: endIndex + i); |
| 234 | if (tc1.type == tc2.type) { |
| 235 | if (tc1.type == TransformComponent::Translate) { |
| 236 | QPointF t1 = QPointF(tc1.values.at(idx: 0), tc1.values.at(idx: 1)); |
| 237 | QPointF t2 = QPointF(tc2.values.at(idx: 0), tc2.values.at(idx: 1)); |
| 238 | QPointF tr = pointInterpolator(v1: t1, v2: t2, t); |
| 239 | transform.translate(dx: tr.x(), dy: tr.y()); |
| 240 | } else if (tc1.type == TransformComponent::Scale) { |
| 241 | QPointF s1 = QPointF(tc1.values.at(idx: 0), tc1.values.at(idx: 1)); |
| 242 | QPointF s2 = QPointF(tc2.values.at(idx: 0), tc2.values.at(idx: 1)); |
| 243 | QPointF sr = pointInterpolator(v1: s1, v2: s2, t); |
| 244 | transform.scale(sx: sr.x(), sy: sr.y()); |
| 245 | } else if (tc1.type == TransformComponent::Rotate) { |
| 246 | QPointF cor1 = QPointF(tc1.values.at(idx: 1), tc1.values.at(idx: 2)); |
| 247 | QPointF cor2 = QPointF(tc2.values.at(idx: 1), tc2.values.at(idx: 2)); |
| 248 | QPointF corResult = pointInterpolator(v1: cor1, v2: cor2, t); |
| 249 | qreal angle1 = tc1.values.at(idx: 0); |
| 250 | qreal angle2 = tc2.values.at(idx: 0); |
| 251 | qreal angleResult = q_lerp(a: angle1, b: angle2, t); |
| 252 | transform.translate(dx: corResult.x(), dy: corResult.y()); |
| 253 | transform.rotate(a: angleResult); |
| 254 | transform.translate(dx: -corResult.x(), dy: -corResult.y()); |
| 255 | } else if (tc1.type == TransformComponent::Skew) { |
| 256 | QPointF skew1 = QPointF(tc1.values.at(idx: 0), tc1.values.at(idx: 1)); |
| 257 | QPointF skew2 = QPointF(tc2.values.at(idx: 0), tc2.values.at(idx: 1)); |
| 258 | QPointF skewResult = pointInterpolator(v1: skew1, v2: skew2, t); |
| 259 | transform.shear(sh: qTan(v: qDegreesToRadians(degrees: skewResult.x())), |
| 260 | sv: qTan(v: qDegreesToRadians(degrees: skewResult.y()))); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | m_interpolatedValue = transform; |
| 266 | } |
| 267 | |
| 268 | QT_END_NAMESPACE |
| 269 | |