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
qsvgcsshandler.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/qsvgstyleselector_p.h>
7#include <QtSvg/private/qsvganimatedproperty_p.h>
8#include <QtSvg/private/qsvgutils_p.h>
9#include <QtGui/private/qmath_p.h>
10#include <QtCore/qlist.h>
11
13
14namespace {
15
20
21bool fillColorProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyColor *prop)
22{
23 for (CssKeyFrameValue keyFrame : keyFrames) {
24 if (keyFrame.values.size() != 1)
25 return false;
26
27 QString colorStr = keyFrame.values.first().toString();
28 QColor color = QColor::fromString(colorStr);
29 prop->appendColor(color);
30 prop->appendKeyFrame(keyFrame.keyFrame);
31 }
32
33 return true;
34}
35
36bool fillOpacityProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyFloat *prop)
37{
38 for (CssKeyFrameValue keyFrame : keyFrames) {
39 if (keyFrame.values.size() != 1)
40 return false;
41
42 QString opacity = keyFrame.values.first().toString();
43 prop->appendValue(opacity.toDouble());
44 prop->appendKeyFrame(keyFrame.keyFrame);
45 }
46
47 return true;
48}
49
50bool validateTransform(QList<QList<QSvgAnimatedPropertyTransform::TransformComponent>> &keyFrameComponents) {
51
52 if (keyFrameComponents.size() < 2)
53 return false;
54
55 qsizetype maxIndex = 0;
56 qsizetype maxSize = 0;
57 for (int i = 1; i < keyFrameComponents.size(); i++) {
58 auto &listA = keyFrameComponents[i - 1];
59 auto &listB = keyFrameComponents[i];
60 for (int j = 0; j < qMin(listA.size(), listB.size()); j++) {
61 auto typeA = listA.at(j).type;
62 auto typeB = listB.at(j).type;
63 // TODO: Handle type mismatch as mentioned in CSS Transform Module specs.
64 if (typeA != typeB)
65 return false;
66 }
67
68 if (listA.size() > maxSize) {
69 maxIndex = i - 1;
70 maxSize = listA.size();
71 }
72
73 if (listB.size() > maxSize) {
74 maxIndex = i;
75 maxSize = listB.size();
76 }
77 }
78
79 const auto &longList = keyFrameComponents.at(maxIndex);
80 // pad the shorter list with identical transforms set to default values.
81 for (auto &list : keyFrameComponents) {
82 qsizetype size = list.size();
83
84 for (int j = size; j < maxSize; j++) {
85 QSvgAnimatedPropertyTransform::TransformComponent comp = longList.value(j);
86 switch (comp.type) {
87 case QSvgAnimatedPropertyTransform::TransformComponent::Translate:
88 case QSvgAnimatedPropertyTransform::TransformComponent::Skew:
89 comp.values[0] = 0;
90 comp.values[1] = 0;
91 break;
92 case QSvgAnimatedPropertyTransform::TransformComponent::Rotate:
93 comp.values[0] = 0;
94 comp.values[1] = 0;
95 comp.values[2] = 0;
96 break;
97 case QSvgAnimatedPropertyTransform::TransformComponent::Scale:
98 comp.values[0] = 1;
99 comp.values[1] = 1;
100 break;
101 default:
102 break;
103 }
104 list.append(comp);
105 }
106 }
107
108 return true;
109}
110
111bool fillTransformProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyTransform *prop)
112{
113 // Stores each key frame's list of components
114 QList<QList<QSvgAnimatedPropertyTransform::TransformComponent>> keyFramesComponents;
115
116 for (CssKeyFrameValue keyFrame : keyFrames) {
117 QList<QSvgAnimatedPropertyTransform::TransformComponent> components;
118 for (QCss::Value val : keyFrame.values) {
119 if (val.type == QCss::Value::Function) {
120 QStringList lst = val.variant.toStringList();
121 QStringView transformType = lst.value(0);
122 QStringList args = lst.value(1).split(QStringLiteral(","), Qt::SkipEmptyParts);
123 if (transformType == QStringLiteral("scale")) {
124 QSvgAnimatedPropertyTransform::TransformComponent component;
125 qreal scale0 = QSvgUtils::toDouble(args.value(0).trimmed());
126 qreal scale1 = QSvgUtils::toDouble(args.value(1).trimmed());
127 component.type = QSvgAnimatedPropertyTransform::TransformComponent::Scale;
128 component.values.append(scale0);
129 component.values.append(scale1);
130 components.append(component);
131 } else if (transformType == QStringLiteral("translate")) {
132 QSvgAnimatedPropertyTransform::TransformComponent component;
133 QSvgUtils::LengthType type;
134 qreal translate0 = QSvgUtils::parseLength(args.value(0), &type);
135 translate0 = QSvgUtils::convertToPixels(translate0, false, type);
136 qreal translate1 = QSvgUtils::parseLength(args.value(1), &type);
137 translate1 = QSvgUtils::convertToPixels(translate1, false, type);
138 component.type = QSvgAnimatedPropertyTransform::TransformComponent::Translate;
139 component.values.append(translate0);
140 component.values.append(translate1);
141 components.append(component);
142 } else if (transformType == QStringLiteral("rotate")) {
143 QSvgAnimatedPropertyTransform::TransformComponent component;
144 qreal rotationAngle = QSvgUtils::parseAngle(args.value(0)).value_or(0);
145 component.type = QSvgAnimatedPropertyTransform::TransformComponent::Rotate;
146 component.values.append(rotationAngle);
147 component.values.append(0);
148 component.values.append(0);
149 components.append(component);
150 } else if (transformType == QStringLiteral("skew")) {
151 QSvgAnimatedPropertyTransform::TransformComponent component;
152 qreal skew0 = QSvgUtils::parseAngle(args.value(0)).value_or(0);
153 qreal skew1 = QSvgUtils::parseAngle(args.value(1)).value_or(0);
154 component.type = QSvgAnimatedPropertyTransform::TransformComponent::Skew;
155 component.values.append(skew0);
156 component.values.append(skew1);
157 components.append(component);
158 } else if (transformType == QStringLiteral("matrix")) {
159 QSvgAnimatedPropertyTransform::TransformComponent component1, component2, component3;
160 QSvgUtils::LengthType type;
161 qreal translate0 = QSvgUtils::parseLength(args.value(4), &type);
162 translate0 = QSvgUtils::convertToPixels(translate0, false, type);
163 qreal translate1 = QSvgUtils::parseLength(args.value(5), &type);
164 translate1 = QSvgUtils::convertToPixels(translate1, false, type);
165 qreal scale0 = QSvgUtils::toDouble(args.value(0).trimmed());
166 qreal scale1 = QSvgUtils::toDouble(args.value(3).trimmed());
167 qreal skew0 = QSvgUtils::toDouble((args.value(1).trimmed()));
168 qreal skew1 = QSvgUtils::toDouble((args.value(2).trimmed()));
169 component1.type = QSvgAnimatedPropertyTransform::TransformComponent::Translate;
170 component1.values.append(translate0);
171 component1.values.append(translate1);
172 component2.type = QSvgAnimatedPropertyTransform::TransformComponent::Scale;
173 component2.values.append(scale0);
174 component2.values.append(scale1);
175 component3.type = QSvgAnimatedPropertyTransform::TransformComponent::Skew;
176 component3.values.append(skew0);
177 component3.values.append(skew1);
178 components.append(component1);
179 components.append(component2);
180 components.append(component3);
181 }
182 }
183 }
184 keyFramesComponents.append(components);
185 prop->appendKeyFrame(keyFrame.keyFrame);
186 }
187
188 if (!validateTransform(keyFramesComponents))
189 return false;
190
191 for (auto comp : keyFramesComponents) {
192 prop->appendComponents(comp);
193 }
194 prop->setTransformCount(keyFramesComponents.first().size());
195
196 return true;
197}
198
199bool fillOffsetDistanceProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyFloat *prop)
200{
201 for (CssKeyFrameValue keyFrame : keyFrames) {
202 if (keyFrame.values.size() != 1)
203 return false;
204
205 QString offsetDistance = keyFrame.values.first().toString();
206
207 bool ok = false;
208 qreal distance = offsetDistance.toDouble(&ok);
209 if (!ok)
210 return false;
211
212 QCss::Value::Type type = keyFrame.values.first().type;
213 if (type != QCss::Value::Percentage && !qFuzzyCompare(distance, 0.))
214 return false;
215 distance /= 100;
216 prop->appendValue(distance);
217 prop->appendKeyFrame(keyFrame.keyFrame);
218 }
219
220 return true;
221}
222
223}
224
225QSvgCssHandler::QSvgCssHandler()
226 : m_selector(new QSvgStyleSelector)
227{
228
229}
230
231QSvgCssHandler::~QSvgCssHandler()
232{
233 delete m_selector;
234 m_selector = nullptr;
235}
236
237QSvgCssAnimation *QSvgCssHandler::createAnimation(QStringView name)
238{
239 if (!m_animations.contains(name))
240 return nullptr;
241
242 QCss::AnimationRule animationRule = m_animations[name];
243 QHash<QString, QSvgAbstractAnimatedProperty*> animatedProperies;
244 QSvgCssAnimation *animation = new QSvgCssAnimation;
245
246
247 // Css Parser returns a list of all properties for each key frame. Here,
248 // we store the key frames and values for each property for easier parsing.
249 QHash<QString, QList<CssKeyFrameValue>> keyFrameValues;
250 for (const auto &ruleSet : std::as_const(animationRule.ruleSets)) {
251 for (QCss::Declaration decl : ruleSet.declarations) {
252 CssKeyFrameValue keyFrameValue = {ruleSet.keyFrame, decl.d->values};
253 QList<CssKeyFrameValue> &value = keyFrameValues[decl.d->property];
254 value.append(keyFrameValue);
255 }
256 }
257
258 for (auto it = keyFrameValues.begin(); it != keyFrameValues.end(); it++) {
259 QStringView property = it.key();
260 const QList<CssKeyFrameValue> &keyFrames = it.value();
261 auto *prop = QSvgAbstractAnimatedProperty::createAnimatedProperty(property.toString());
262 if (!prop)
263 continue;
264
265 bool result = false;
266 if (property == QLatin1StringView("fill") || property == QLatin1StringView("stroke"))
267 result = fillColorProperty(keyFrames, static_cast<QSvgAnimatedPropertyColor*>(prop));
268 else if (property == QLatin1StringView("transform"))
269 result = fillTransformProperty(keyFrames, static_cast<QSvgAnimatedPropertyTransform*>(prop));
270 else if (property == QLatin1StringView("fill-opacity") || property == QLatin1StringView("stroke-opacity")
271 || property == QLatin1StringView("opacity"))
272 result = fillOpacityProperty(keyFrames, static_cast<QSvgAnimatedPropertyFloat*>(prop));
273 else if (property == QLatin1StringView("offset-distance"))
274 result = fillOffsetDistanceProperty(keyFrames, static_cast<QSvgAnimatedPropertyFloat*>(prop));
275
276 if (!result) {
277 delete prop;
278 continue;
279 }
280
281 animatedProperies[property] = prop;
282 }
283
284 for (auto it = animatedProperies.begin(); it != animatedProperies.end(); it++)
285 animation->appendProperty(it.value());
286
287 return animation;
288}
289
290void QSvgCssHandler::collectAnimations(const QCss::StyleSheet &sheet)
291{
292 auto sortFunction = [](QCss::AnimationRule::AnimationRuleSet r1, QCss::AnimationRule::AnimationRuleSet r2) {
293 return r1.keyFrame < r2.keyFrame;
294 };
295
296 QList<QCss::AnimationRule> animationRules = sheet.animationRules;
297 for (QCss::AnimationRule rule : animationRules) {
298 std::sort(rule.ruleSets.begin(), rule.ruleSets.end(), sortFunction);
299 m_animations[rule.animName] = rule;
300 }
301}
302
303void QSvgCssHandler::parseStyleSheet(const QStringView str)
304{
305 QString css = str.toString();
306 QCss::StyleSheet sheet;
307 QCss::Parser(css).parse(&sheet);
308 m_selector->styleSheets.append(sheet);
309
310 collectAnimations(sheet);
311}
312
313void QSvgCssHandler::parseCSStoXMLAttrs(const QList<QCss::Declaration> &declarations, QXmlStreamAttributes &attributes) const
314{
315 for (int i = 0; i < declarations.size(); ++i) {
316 const QCss::Declaration &decl = declarations.at(i);
317 if (decl.d->property.isEmpty())
318 continue;
319 QString valueStr;
320 const int valCount = decl.d->values.size();
321 for (int i = 0; i < valCount; ++i) {
322 QCss::Value val = decl.d->values.at(i);
323 switch (val.type) {
324 case QCss::Value::TermOperatorComma:
325 valueStr += QLatin1Char(';');
326 break;
327 case QCss::Value::Uri:
328 {
329 QString temp = val.toString();
330 temp.prepend(QLatin1String("url("));
331 temp.append(QLatin1Char(')'));
332 valueStr += temp;
333 break;
334 }
335 case QCss::Value::Function:
336 {
337 QStringList lst = val.variant.toStringList();
338 valueStr.append(lst.at(0));
339 valueStr.append(QLatin1Char('('));
340 for (int i = 1; i < lst.size(); ++i) {
341 valueStr.append(lst.at(i));
342 if ((i +1) < lst.size())
343 valueStr.append(QLatin1Char(','));
344 }
345 valueStr.append(QLatin1Char(')'));
346 break;
347 }
348 case QCss::Value::KnownIdentifier:
349 switch (val.variant.toInt()) {
350 case QCss::Value_None:
351 valueStr += QLatin1String("none");
352 break;
353 case QCss::Value_Auto:
354 valueStr += QLatin1String("auto");
355 break;
356 default:
357 valueStr += val.toString();
358 break;
359 }
360 break;
361 case QCss::Value::Percentage:
362 valueStr += val.toString() + QLatin1Char('%');
363 break;
364 default:
365 valueStr += val.toString();
366 break;
367 }
368
369 if (i + 1 < valCount)
370 valueStr += QLatin1Char(' ');
371 }
372
373 attributes.append(QString(), decl.d->property, valueStr);
374 }
375}
376
377void QSvgCssHandler::parseCSStoXMLAttrs(const QString &css, QXmlStreamAttributes &attributes) const
378{
379 // preprocess (for unicode escapes), tokenize and remove comments
380 QCss::Parser parser(css);
381 QString key;
382
383 while (parser.hasNext()) {
384 parser.skipSpace();
385
386 if (!parser.hasNext())
387 break;
388 parser.next();
389
390 QString name;
391 QString value;
392
393 if (parser.hasEscapeSequences) {
394 key = parser.lexem();
395 name = key;
396 } else {
397 const QCss::Symbol &sym = parser.symbol();
398 name = sym.text.mid(sym.start, sym.len);
399 }
400
401 parser.skipSpace();
402 if (!parser.test(QCss::COLON))
403 break;
404
405 parser.skipSpace();
406 if (!parser.hasNext())
407 break;
408
409 const int firstSymbol = parser.index;
410 int symbolCount = 0;
411 do {
412 parser.next();
413 ++symbolCount;
414 } while (parser.hasNext() && !parser.test(QCss::SEMICOLON));
415
416 bool canExtractValueByRef = !parser.hasEscapeSequences;
417 if (canExtractValueByRef) {
418 int len = parser.symbols.at(firstSymbol).len;
419 for (int i = firstSymbol + 1; i < firstSymbol + symbolCount; ++i) {
420 len += parser.symbols.at(i).len;
421
422 if (parser.symbols.at(i - 1).start + parser.symbols.at(i - 1).len
423 != parser.symbols.at(i).start) {
424 canExtractValueByRef = false;
425 break;
426 }
427 }
428 if (canExtractValueByRef) {
429 const QCss::Symbol &sym = parser.symbols.at(firstSymbol);
430 value = sym.text.mid(sym.start, len);
431 }
432 }
433 if (!canExtractValueByRef) {
434
435 for (int i = firstSymbol; i < parser.index - 1; ++i)
436 value += parser.symbols.at(i).lexem();
437 }
438
439 attributes.append(QString(), name, value);
440
441 parser.skipSpace();
442 }
443}
444
445void QSvgCssHandler::styleLookup(QSvgNode *node, QXmlStreamAttributes &attributes) const
446{
447 QCss::StyleSelector::NodePtr cssNode;
448 cssNode.ptr = node;
449 QList<QCss::Declaration> decls = m_selector->declarationsForNode(cssNode);
450
451 parseCSStoXMLAttrs(decls, attributes);
452}
453
454QT_END_NAMESPACE
Combined button and popup list for selecting options.
bool fillTransformProperty(const QList< CssKeyFrameValue > &keyFrames, QSvgAnimatedPropertyTransform *prop)
bool fillOffsetDistanceProperty(const QList< CssKeyFrameValue > &keyFrames, QSvgAnimatedPropertyFloat *prop)
bool fillOpacityProperty(const QList< CssKeyFrameValue > &keyFrames, QSvgAnimatedPropertyFloat *prop)
bool fillColorProperty(const QList< CssKeyFrameValue > &keyFrames, QSvgAnimatedPropertyColor *prop)
bool validateTransform(QList< QList< QSvgAnimatedPropertyTransform::TransformComponent > > &keyFrameComponents)