1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:critical reason:data-parser
#include "qsvgcsshandler_p.h"
#include <QtSvg/private/qsvganimatedproperty_p.h>
#include <QtSvg/private/qsvgutils_p.h>
#include <QtGui/private/qmath_p.h>
QT_BEGIN_NAMESPACE
// Parses the angle from a string and convert it to degrees.
static qreal qsvg_parseAngle(QStringView str, bool *ok = nullptr)
{
QStringView numStr = str.trimmed();
if (numStr.isEmpty()) {
if (ok)
*ok = false;
return false;
}
qreal unitFactor;
if (numStr.endsWith(QLatin1String("deg"))) {
numStr.chop(3);
unitFactor = 1.0;
} else if (numStr.endsWith(QLatin1String("grad"))) {
numStr.chop(4);
// deg = grad * 0.9;
unitFactor = 0.9;
} else if (numStr.endsWith(QLatin1String("rad"))) {
numStr.chop(3);
unitFactor = 180.0 / Q_PI;
} else if (numStr.endsWith(QLatin1String("turn"))) {
numStr.chop(4);
// one circle = one turn
unitFactor = 360.0;
} else {
unitFactor = 0.0;
}
return QSvgUtils::toDouble(numStr, ok) * unitFactor;
}
void QSvgCssHandler::collectAnimations(const QCss::StyleSheet &sheet)
{
auto sortFunction = [](QCss::AnimationRule::AnimationRuleSet r1, QCss::AnimationRule::AnimationRuleSet r2) {
return r1.keyFrame < r2.keyFrame;
};
QList<QCss::AnimationRule> animationRules = sheet.animationRules;
for (QCss::AnimationRule rule : animationRules) {
std::sort(rule.ruleSets.begin(), rule.ruleSets.end(), sortFunction);
m_animations[rule.animName] = rule;
}
}
QSvgCssAnimation *QSvgCssHandler::createAnimation(const QString &name)
{
if (!m_animations.contains(name))
return nullptr;
QCss::AnimationRule animationRule = m_animations[name];
QHash<QString, QSvgAbstractAnimatedProperty*> animatedProperies;
QSvgCssAnimation *animation = new QSvgCssAnimation;
for (const auto &ruleSet : std::as_const(animationRule.ruleSets)) {
for (QCss::Declaration decl : ruleSet.declarations) {
if (decl.d->property == QStringLiteral("fill") || decl.d->property == QStringLiteral("stroke")) {
QSvgAnimatedPropertyColor *prop = nullptr;
if (!animatedProperies.contains(decl.d->property))
animatedProperies[decl.d->property] = QSvgAbstractAnimatedProperty::createAnimatedProperty(decl.d->property);
prop = static_cast<QSvgAnimatedPropertyColor *>(animatedProperies[decl.d->property]);
prop->appendKeyFrame(ruleSet.keyFrame);
updateColorProperty(decl, prop);
} else if (decl.d->property == QStringLiteral("transform")) {
QSvgAnimatedPropertyTransform *prop = nullptr;
if (!animatedProperies.contains(decl.d->property))
animatedProperies[decl.d->property] = QSvgAbstractAnimatedProperty::createAnimatedProperty(decl.d->property);
prop = static_cast<QSvgAnimatedPropertyTransform *>(animatedProperies[decl.d->property]);
prop->appendKeyFrame(ruleSet.keyFrame);
updateTransformProperty(decl, prop);
} else if (decl.d->property == QStringLiteral("fill-opacity") || decl.d->property == QStringLiteral("stroke-opacity")
|| decl.d->property == QStringLiteral("opacity")) {
QSvgAnimatedPropertyFloat *prop = nullptr;
if (!animatedProperies.contains(decl.d->property))
animatedProperies[decl.d->property] = QSvgAbstractAnimatedProperty::createAnimatedProperty(decl.d->property);
prop = static_cast<QSvgAnimatedPropertyFloat *>(animatedProperies[decl.d->property]);
prop->appendKeyFrame(ruleSet.keyFrame);
QString opacity = decl.d->values.first().toString();
prop->appendValue(opacity.toDouble());
}
}
}
for (auto it = animatedProperies.begin(); it != animatedProperies.end(); it++)
animation->appendProperty(it.value());
return animation;
}
void QSvgCssHandler::updateColorProperty(const QCss::Declaration &decl, QSvgAnimatedPropertyColor *property)
{
QString colorStr = decl.d->values.first().toString();
QColor color = QColor::fromString(colorStr);
property->appendColor(color);
}
void QSvgCssHandler::updateTransformProperty(const QCss::Declaration &decl, QSvgAnimatedPropertyTransform *property)
{
for (QCss::Value val : decl.d->values) {
if (val.type == QCss::Value::Function) {
QStringList lst = val.variant.toStringList();
QStringView transformType = lst[0];
QStringList args = lst[1].split(QStringLiteral(","), Qt::SkipEmptyParts);
if (transformType == QStringLiteral("scale")) {
qreal scale0 = QSvgUtils::toDouble(args[0].trimmed());
qreal scale1 = QSvgUtils::toDouble(args[1].trimmed());
property->appendScale(QPointF(scale0, scale1));
} else if (transformType == QStringLiteral("translate")) {
QSvgUtils::LengthType type;
qreal translate0 = QSvgUtils::parseLength(args[0], &type);
translate0 = QSvgUtils::convertToPixels(translate0, false, type);
qreal translate1 = QSvgUtils::parseLength(args[1], &type);
translate1 = QSvgUtils::convertToPixels(translate1, false, type);
property->appendTranslation(QPointF(translate0, translate1));
} else if (transformType == QStringLiteral("rotate")) {
qreal rotationAngle = qsvg_parseAngle(args[0]);
property->appendRotation(rotationAngle);
property->appendCenterOfRotation(QPointF(0, 0));
} else if (transformType == QStringLiteral("skew")) {
qreal skew0 = qsvg_parseAngle(args[0]);
qreal skew1 = qsvg_parseAngle(args[1]);
property->appendSkew(QPointF(skew0, skew1));
} else if (transformType == QStringLiteral("matrix")) {
QSvgUtils::LengthType type;
qreal translate0 = QSvgUtils::parseLength(args[4], &type);
translate0 = QSvgUtils::convertToPixels(translate0, false, type);
qreal translate1 = QSvgUtils::parseLength(args[5], &type);
translate1 = QSvgUtils::convertToPixels(translate1, false, type);
qreal scale0 = QSvgUtils::toDouble(args[0].trimmed());
qreal scale1 = QSvgUtils::toDouble(args[3].trimmed());
qreal skew0 = QSvgUtils::toDouble((args[1].trimmed()));
qreal skew1 = QSvgUtils::toDouble((args[2].trimmed()));
property->appendSkew(QPointF(skew0, skew1));
property->appendTranslation(QPointF(translate0, translate1));
property->appendScale(QPointF(scale0, scale1));
}
}
}
}
QT_END_NAMESPACE
|