Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsvgcssproperties.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:critical reason:data-parser
4
6#include <QtSvg/private/qsvgutils_p.h>
7
9
10using namespace Qt::Literals::StringLiterals;
11
12namespace {
13
14int parseCssClockValue(QStringView str, bool *ok)
15{
16 int res = 0;
17 int ms = 1000;
18 str = str.trimmed();
19 if (str.endsWith("ms"_L1)) {
20 str.chop(2);
21 ms = 1;
22 } else if (str.endsWith("s"_L1)) {
23 str.chop(1);
24 } else {
25 if (ok)
26 *ok = false;
27 return res;
28 }
29 double val = ms * str.toDouble(ok);
30 if (ok) {
31 if (val > std::numeric_limits<int>::min() && val < std::numeric_limits<int>::max())
32 res = static_cast<int>(val);
33 else
34 *ok = false;
35 }
36 return res;
37}
38
39bool isTimeValue(QStringView str) {
40 if (str.endsWith("ms"_L1))
41 str.chop(2);
42 else if (str.endsWith("s"_L1))
43 str.chop(1);
44 else
45 return false;
46
47 bool ok;
48 Q_UNUSED(str.toDouble(&ok))
49 return ok;
50}
51
52bool isIteration(QStringView str) {
53 if (str == "infinite"_L1)
54 return true;
55 bool ok;
56 Q_UNUSED(str.toDouble(&ok))
57 return ok;
58}
59
60bool isDirection(QStringView str) {
61 return str == "normal"_L1 || str == "reverse"_L1 || str.startsWith("alternate"_L1);
62}
63
64bool isTimingFunction(QStringView str) {
65 return str.startsWith("linear"_L1) || str.startsWith("ease"_L1)
66 || str.startsWith("step"_L1) || str.startsWith("cubic-bezier"_L1);
67}
68
69bool isFillMode(QStringView str) {
70 return str == "none"_L1 || str == "forwards"_L1 || str == "backwards"_L1 || str == "both"_L1;
71}
72
73bool isPlayState(QStringView str) {
74 return str == "paused"_L1 || str =="running"_L1;
75}
76
77}
78
79QSvgCssProperties::QSvgCssProperties(const QXmlStreamAttributes &attributes)
80{
81 QRegularExpression re(";| "_L1);
82 for (int i = 0; i < attributes.size(); ++i) {
83 const QXmlStreamAttribute &attribute = attributes.at(i);
84 QStringView name = attribute.qualifiedName();
85 if (name.isEmpty())
86 continue;
87 QStringView value = attribute.value();
88
89 switch (name.at(0).unicode()) {
90
91 case 'a':
92 if (name == "animation"_L1)
93 shortHandtoLonghandForm(value);
94 if (name == "animation-name"_L1)
95 m_names = value.split(re, Qt::SkipEmptyParts);
96 if (name == "animation-duration"_L1)
97 m_durations = value.split(re, Qt::SkipEmptyParts);
98 if (name == "animation-delay"_L1)
99 m_delays = value.split(re, Qt::SkipEmptyParts);
100 if (name == "animation-iteration-count"_L1)
101 m_iterationCounts = value.split(re, Qt::SkipEmptyParts);
102 if (name == "animation-direction"_L1)
103 m_directions = value.split(re, Qt::SkipEmptyParts);
104 if (name == "animation-timing-function"_L1)
105 m_timingFunctions = value.split(re, Qt::SkipEmptyParts);
106 if (name == "animation-fill-mode"_L1)
107 m_fillModes = value.split(re, Qt::SkipEmptyParts);
108 if (name == "animation-play-state"_L1)
109 m_playStates = value.split(re, Qt::SkipEmptyParts);
110 break;
111
112 case 'o':
113 if (name == "offset-path"_L1)
114 m_offsetPath = value;
115 if (name == "offset-distance"_L1)
116 m_offsetDistance = value;
117 if (name == "offset-rotate"_L1)
118 m_offsetRotate = value;
119 break;
120
121 default:
122 break;
123 }
124 }
125}
126
128{
129 bool ok;
130 QList<QSvgAnimationProperty> parsedProperties;
131 for (int i = 0; i < m_names.size(); i++) {
132 QSvgAnimationProperty property;
133 property.name = m_names.at(i);
134
135 if (!m_durations.isEmpty()) {
136 QStringView durationStr = m_durations.at(i % m_durations.size());
137 int duration = parseCssClockValue(durationStr, &ok);
138 property.duration = ok ? duration : 0;
139 }
140
141 if (!m_delays.isEmpty()) {
142 QStringView delayStr = m_delays.at(i % m_delays.size());
143 int delay = parseCssClockValue(delayStr, &ok);
144 property.delay = ok ? delay : 0;
145 }
146
147 if (!m_iterationCounts.isEmpty()) {
148 QStringView iterationStr = m_iterationCounts.at(i % m_iterationCounts.size());
149 int iteration;
150 if (iterationStr == "infinite"_L1) {
151 iteration = -1;
152 } else {
153 qreal count = iterationStr.toDouble(&ok);
154 iteration = ok ? qMax(1.0, count) : 0;
155 }
156 property.iteration = iteration;
157 }
158
159 parsedProperties.append(property);
160 }
161
162 return parsedProperties;
163}
164
166{
167 QSvgOffsetProperty offset;
168
169 offset.path = QSvgUtils::parsePathDataFast(m_offsetPath);
170
171 qsizetype index;
172 Qt::CaseSensitivity cs = Qt::CaseInsensitive;
173 if ((index = m_offsetRotate.indexOf("auto"_L1, 0, cs)) >= 0) {
174 std::optional<qreal> angle = QSvgUtils::parseAngle(m_offsetRotate.sliced(index + 4));
175 offset.rotateType = angle ? QtSvg::OffsetRotateType::AutoAngle :
176 QtSvg::OffsetRotateType::Auto;
177 offset.angle = angle.value_or(0);
178 } else if ((index = m_offsetRotate.indexOf("reverse"_L1, 0, cs)) >= 0) {
179 std::optional<qreal> angle = QSvgUtils::parseAngle(m_offsetRotate.sliced(index + 7));
180 offset.rotateType = angle ? QtSvg::OffsetRotateType::ReverseAngle :
181 QtSvg::OffsetRotateType::Reverse;
182 offset.angle = angle.value_or(0);
183 } else {
184 std::optional<qreal> angle = QSvgUtils::parseAngle(m_offsetRotate);
185 offset.rotateType = angle ? QtSvg::OffsetRotateType::Angle :
186 QtSvg::OffsetRotateType::Auto;
187 offset.angle = angle.value_or(0);
188 }
189
190 QSvgUtils::LengthType type;
191 bool *ok = nullptr;
192 offset.distance= QSvgUtils::parseLength(m_offsetDistance, &type, ok);
193 if (type == QSvgUtils::LengthType::LT_PERCENT) {
194 offset.distance = offset.distance / 100.0;
195 }
196
197 return offset;
198}
199
200void QSvgCssProperties::shortHandtoLonghandForm(QStringView value)
201{
202 m_names.clear();
203 m_durations.clear();
204 m_delays.clear();
205 m_iterationCounts.clear();
206 m_directions.clear();
207 m_timingFunctions.clear();
208 m_fillModes.clear();
209 m_playStates.clear();
210
211 enum Property : uchar {
212 Duration = 1,
213 Delay = 1 << 1,
214 IterationCount = 1 << 2,
215 Direction = 1 << 3,
216 TimingFunction = 1 << 4,
217 FillMode = 1 << 5,
218 PlayState = 1 << 6,
219 Name = 1 << 7
220 };
221
222 QList<QStringView> animations = value.split(QLatin1Char(';'), Qt::SkipEmptyParts);
223 for (QStringView animation : animations) {
224 uchar propertyFlag = 0;
225 QList<QStringView> animationProperties = animation.split(QLatin1Char(' '), Qt::SkipEmptyParts);
226 for (QStringView property : animationProperties) {
227 if (!(propertyFlag & Property::Duration) && isTimeValue(property)) {
228 m_durations.append(property);
229 propertyFlag |= Property::Duration;
230 } else if (!(propertyFlag & Property::Delay) && isTimeValue(property)) {
231 m_delays.append(property);
232 propertyFlag |= Property::Delay;
233 } else if (!(propertyFlag & Property::IterationCount) && isIteration(property)) {
234 m_iterationCounts.append(property);
235 propertyFlag |= Property::IterationCount;
236 } else if (!(propertyFlag & Property::Direction) && isDirection(property)) {
237 m_directions.append(property);
238 propertyFlag |= Property::Direction;
239 } else if (!(propertyFlag & Property::TimingFunction) && isTimingFunction(property)) {
240 m_timingFunctions.append(property);
241 propertyFlag |= Property::TimingFunction;
242 } else if (!(propertyFlag & Property::FillMode) && isFillMode(property)) {
243 m_fillModes.append(property);
244 propertyFlag |= Property::FillMode;
245 } else if (!(propertyFlag & Property::PlayState)&& isPlayState(property)) {
246 m_playStates.append(property);
247 propertyFlag |= Property::PlayState;
248 } else {
249 m_names.append(property);
250 propertyFlag |= Property::Name;
251 }
252 }
253 }
254}
255
256QT_END_NAMESPACE
QSvgOffsetProperty offset() const
QSvgCssProperties(const QXmlStreamAttributes &attributes)
QList< QSvgAnimationProperty > animations() const
Combined button and popup list for selecting options.