/src/qtsvg/src/svg/css/qsvgcsshandler.cpp
Line | Count | Source |
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 | | |
5 | | #include "qsvgcsshandler_p.h" |
6 | | #include <QtSvg/private/qsvgstyleselector_p.h> |
7 | | #include <QtSvg/private/qsvganimatedproperty_p.h> |
8 | | #include <QtGui/private/qmath_p.h> |
9 | | #include <QtGui/private/qguisvg_p.h> |
10 | | #include <QtCore/qlist.h> |
11 | | |
12 | | QT_BEGIN_NAMESPACE |
13 | | |
14 | | namespace { |
15 | | |
16 | | struct CssKeyFrameValue{ |
17 | | qreal keyFrame{0.0}; |
18 | | QList<QCss::Value> values; |
19 | | QCss::Declaration timingFunction; |
20 | | }; |
21 | | |
22 | | bool fillPropertyEasing(const QList<CssKeyFrameValue> &keyFrames, QSvgAbstractAnimatedProperty *prop) |
23 | 0 | { |
24 | 0 | for (const CssKeyFrameValue &keyFrameValue : keyFrames) { |
25 | 0 | QStringView timingFunctionStr = QSvgCssHandler::parseDecltoString(keyFrameValue.timingFunction); |
26 | 0 | QSvgEasingInterfacePtr easing; |
27 | 0 | if (timingFunctionStr == QStringLiteral("linear")) { |
28 | 0 | easing = QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction::Linear); |
29 | 0 | } else if (timingFunctionStr == QStringLiteral("ease-in")) { |
30 | 0 | easing = QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction::EaseIn); |
31 | 0 | } else if (timingFunctionStr == QStringLiteral("ease-out")) { |
32 | 0 | easing = QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction::EaseOut); |
33 | 0 | } else if (timingFunctionStr == QStringLiteral("ease-in-out")) { |
34 | 0 | easing = QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction::EaseInOut); |
35 | 0 | } else if (timingFunctionStr == QStringLiteral("step-end")) { |
36 | 0 | easing = QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction::Steps, |
37 | 0 | QSvgCssValues::StepValues{quint32(1), |
38 | 0 | QSvgCssValues::StepPosition::End}); |
39 | 0 | } else if (timingFunctionStr == QStringLiteral("step-start")) { |
40 | 0 | easing = QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction::Steps, |
41 | 0 | QSvgCssValues::StepValues{quint32(1), |
42 | 0 | QSvgCssValues::StepPosition::Start}); |
43 | 0 | } |
44 | |
|
45 | 0 | prop->appendEasing(std::move(easing)); |
46 | 0 | } |
47 | | |
48 | | |
49 | |
|
50 | 0 | return true; |
51 | 0 | } |
52 | | |
53 | | bool fillColorProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyColor *prop) |
54 | 0 | { |
55 | 0 | for (const CssKeyFrameValue &keyFrame : keyFrames) { |
56 | 0 | if (keyFrame.values.size() != 1) |
57 | 0 | return false; |
58 | | |
59 | 0 | QString colorStr = keyFrame.values.first().toString(); |
60 | 0 | QColor color = QColor::fromString(colorStr); |
61 | 0 | prop->appendColor(color); |
62 | 0 | prop->appendKeyFrame(keyFrame.keyFrame); |
63 | 0 | } |
64 | | |
65 | 0 | return true; |
66 | 0 | } |
67 | | |
68 | | bool fillOpacityProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyFloat *prop) |
69 | 0 | { |
70 | 0 | for (const CssKeyFrameValue &keyFrame : keyFrames) { |
71 | 0 | if (keyFrame.values.size() != 1) |
72 | 0 | return false; |
73 | | |
74 | 0 | QString opacity = keyFrame.values.first().toString(); |
75 | 0 | prop->appendValue(opacity.toDouble()); |
76 | 0 | prop->appendKeyFrame(keyFrame.keyFrame); |
77 | 0 | } |
78 | | |
79 | 0 | return true; |
80 | 0 | } |
81 | | |
82 | 0 | bool validateTransform(QList<QList<QSvgAnimatedPropertyTransform::TransformComponent>> &keyFrameComponents) { |
83 | |
|
84 | 0 | if (keyFrameComponents.size() < 2) |
85 | 0 | return false; |
86 | | |
87 | 0 | qsizetype maxIndex = 0; |
88 | 0 | qsizetype maxSize = 0; |
89 | 0 | for (int i = 1; i < keyFrameComponents.size(); i++) { |
90 | 0 | auto &listA = keyFrameComponents[i - 1]; |
91 | 0 | auto &listB = keyFrameComponents[i]; |
92 | 0 | for (int j = 0; j < qMin(listA.size(), listB.size()); j++) { |
93 | 0 | auto typeA = listA.at(j).type; |
94 | 0 | auto typeB = listB.at(j).type; |
95 | | // TODO: Handle type mismatch as mentioned in CSS Transform Module specs. |
96 | 0 | if (typeA != typeB) |
97 | 0 | return false; |
98 | 0 | } |
99 | | |
100 | 0 | if (listA.size() > maxSize) { |
101 | 0 | maxIndex = i - 1; |
102 | 0 | maxSize = listA.size(); |
103 | 0 | } |
104 | |
|
105 | 0 | if (listB.size() > maxSize) { |
106 | 0 | maxIndex = i; |
107 | 0 | maxSize = listB.size(); |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | 0 | const auto &longList = keyFrameComponents.at(maxIndex); |
112 | | // pad the shorter list with identical transforms set to default values. |
113 | 0 | for (auto &list : keyFrameComponents) { |
114 | 0 | qsizetype size = list.size(); |
115 | |
|
116 | 0 | for (int j = size; j < maxSize; j++) { |
117 | 0 | QSvgAnimatedPropertyTransform::TransformComponent comp = longList.value(j); |
118 | 0 | switch (comp.type) { |
119 | 0 | case QSvgAnimatedPropertyTransform::TransformComponent::Translate: |
120 | 0 | case QSvgAnimatedPropertyTransform::TransformComponent::Skew: |
121 | 0 | comp.values[0] = 0; |
122 | 0 | comp.values[1] = 0; |
123 | 0 | break; |
124 | 0 | case QSvgAnimatedPropertyTransform::TransformComponent::Rotate: |
125 | 0 | comp.values[0] = 0; |
126 | 0 | comp.values[1] = 0; |
127 | 0 | comp.values[2] = 0; |
128 | 0 | break; |
129 | 0 | case QSvgAnimatedPropertyTransform::TransformComponent::Scale: |
130 | 0 | comp.values[0] = 1; |
131 | 0 | comp.values[1] = 1; |
132 | 0 | break; |
133 | 0 | default: |
134 | 0 | break; |
135 | 0 | } |
136 | 0 | list.append(comp); |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | 0 | return true; |
141 | 0 | } |
142 | | |
143 | | bool fillTransformProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyTransform *prop) |
144 | 0 | { |
145 | | // Stores each key frame's list of components |
146 | 0 | QList<QList<QSvgAnimatedPropertyTransform::TransformComponent>> keyFramesComponents; |
147 | |
|
148 | 0 | for (const CssKeyFrameValue &keyFrame : keyFrames) { |
149 | 0 | QList<QSvgAnimatedPropertyTransform::TransformComponent> components; |
150 | 0 | for (const QCss::Value &val : keyFrame.values) { |
151 | 0 | if (val.type == QCss::Value::Function) { |
152 | 0 | const QStringList lst = val.variant.toStringList(); |
153 | 0 | const QStringView transformType = lst.value(0); |
154 | 0 | const QList<QStringView> args = |
155 | 0 | QStringView{ lst.value(1) }.split(QStringLiteral(","), Qt::SkipEmptyParts); |
156 | 0 | if (transformType == QStringLiteral("scale")) { |
157 | 0 | QSvgAnimatedPropertyTransform::TransformComponent component; |
158 | 0 | qreal scale0 = QGuiSvg::toDouble(args.value(0).trimmed()); |
159 | 0 | qreal scale1 = QGuiSvg::toDouble(args.value(1).trimmed()); |
160 | 0 | component.type = QSvgAnimatedPropertyTransform::TransformComponent::Scale; |
161 | 0 | component.values.append(scale0); |
162 | 0 | component.values.append(scale1); |
163 | 0 | components.append(component); |
164 | 0 | } else if (transformType == QStringLiteral("translate")) { |
165 | 0 | QSvgAnimatedPropertyTransform::TransformComponent component; |
166 | 0 | QGuiSvg::LengthType type; |
167 | 0 | qreal translate0 = QGuiSvg::parseLength(args.value(0), &type); |
168 | 0 | translate0 = QGuiSvg::convertToPixels(translate0, false, type); |
169 | 0 | qreal translate1 = QGuiSvg::parseLength(args.value(1), &type); |
170 | 0 | translate1 = QGuiSvg::convertToPixels(translate1, false, type); |
171 | 0 | component.type = QSvgAnimatedPropertyTransform::TransformComponent::Translate; |
172 | 0 | component.values.append(translate0); |
173 | 0 | component.values.append(translate1); |
174 | 0 | components.append(component); |
175 | 0 | } else if (transformType == QStringLiteral("rotate")) { |
176 | 0 | QSvgAnimatedPropertyTransform::TransformComponent component; |
177 | 0 | qreal rotationAngle = QGuiSvg::parseAngle(args.value(0)).value_or(0); |
178 | 0 | component.type = QSvgAnimatedPropertyTransform::TransformComponent::Rotate; |
179 | 0 | component.values.append(rotationAngle); |
180 | 0 | component.values.append(0); |
181 | 0 | component.values.append(0); |
182 | 0 | components.append(component); |
183 | 0 | } else if (transformType == QStringLiteral("skew")) { |
184 | 0 | QSvgAnimatedPropertyTransform::TransformComponent component; |
185 | 0 | qreal skew0 = QGuiSvg::parseAngle(args.value(0)).value_or(0); |
186 | 0 | qreal skew1 = QGuiSvg::parseAngle(args.value(1)).value_or(0); |
187 | 0 | component.type = QSvgAnimatedPropertyTransform::TransformComponent::Skew; |
188 | 0 | component.values.append(skew0); |
189 | 0 | component.values.append(skew1); |
190 | 0 | components.append(component); |
191 | 0 | } else if (transformType == QStringLiteral("matrix")) { |
192 | 0 | QSvgAnimatedPropertyTransform::TransformComponent component1, component2, component3; |
193 | 0 | QGuiSvg::LengthType type; |
194 | 0 | qreal translate0 = QGuiSvg::parseLength(args.value(4), &type); |
195 | 0 | translate0 = QGuiSvg::convertToPixels(translate0, false, type); |
196 | 0 | qreal translate1 = QGuiSvg::parseLength(args.value(5), &type); |
197 | 0 | translate1 = QGuiSvg::convertToPixels(translate1, false, type); |
198 | 0 | qreal scale0 = QGuiSvg::toDouble(args.value(0).trimmed()); |
199 | 0 | qreal scale1 = QGuiSvg::toDouble(args.value(3).trimmed()); |
200 | 0 | qreal skew0 = QGuiSvg::toDouble((args.value(1).trimmed())); |
201 | 0 | qreal skew1 = QGuiSvg::toDouble((args.value(2).trimmed())); |
202 | 0 | component1.type = QSvgAnimatedPropertyTransform::TransformComponent::Translate; |
203 | 0 | component1.values.append(translate0); |
204 | 0 | component1.values.append(translate1); |
205 | 0 | component2.type = QSvgAnimatedPropertyTransform::TransformComponent::Scale; |
206 | 0 | component2.values.append(scale0); |
207 | 0 | component2.values.append(scale1); |
208 | 0 | component3.type = QSvgAnimatedPropertyTransform::TransformComponent::Skew; |
209 | 0 | component3.values.append(skew0); |
210 | 0 | component3.values.append(skew1); |
211 | 0 | components.append(component1); |
212 | 0 | components.append(component2); |
213 | 0 | components.append(component3); |
214 | 0 | } |
215 | 0 | } |
216 | 0 | } |
217 | 0 | keyFramesComponents.append(components); |
218 | 0 | prop->appendKeyFrame(keyFrame.keyFrame); |
219 | 0 | } |
220 | |
|
221 | 0 | if (!validateTransform(keyFramesComponents)) |
222 | 0 | return false; |
223 | | |
224 | 0 | for (const auto &comp : std::as_const(keyFramesComponents)) { |
225 | 0 | prop->appendComponents(comp); |
226 | 0 | } |
227 | 0 | prop->setTransformCount(keyFramesComponents.first().size()); |
228 | |
|
229 | 0 | return true; |
230 | 0 | } |
231 | | |
232 | | bool fillOffsetDistanceProperty(const QList<CssKeyFrameValue> &keyFrames, QSvgAnimatedPropertyFloat *prop) |
233 | 0 | { |
234 | 0 | for (const CssKeyFrameValue &keyFrame : keyFrames) { |
235 | 0 | if (keyFrame.values.size() != 1) |
236 | 0 | return false; |
237 | | |
238 | 0 | QString offsetDistance = keyFrame.values.first().toString(); |
239 | |
|
240 | 0 | bool ok = false; |
241 | 0 | qreal distance = offsetDistance.toDouble(&ok); |
242 | 0 | if (!ok) |
243 | 0 | return false; |
244 | | |
245 | 0 | QCss::Value::Type type = keyFrame.values.first().type; |
246 | 0 | if (type != QCss::Value::Percentage && !qFuzzyCompare(distance, 0.)) |
247 | 0 | return false; |
248 | 0 | distance /= 100; |
249 | 0 | prop->appendValue(distance); |
250 | 0 | prop->appendKeyFrame(keyFrame.keyFrame); |
251 | 0 | } |
252 | | |
253 | 0 | return true; |
254 | 0 | } |
255 | | |
256 | | } |
257 | | |
258 | | QSvgCssHandler::QSvgCssHandler() |
259 | 96.8k | : m_selector(new QSvgStyleSelector) |
260 | 96.8k | { |
261 | | |
262 | 96.8k | } |
263 | | |
264 | | QSvgCssHandler::~QSvgCssHandler() |
265 | 96.8k | { |
266 | 96.8k | delete m_selector; |
267 | 96.8k | m_selector = nullptr; |
268 | 96.8k | } |
269 | | |
270 | | QSvgCssAnimation *QSvgCssHandler::createAnimation(QStringView name) |
271 | 0 | { |
272 | 0 | if (!m_animations.contains(name)) |
273 | 0 | return nullptr; |
274 | | |
275 | 0 | QCss::AnimationRule animationRule = m_animations[name]; |
276 | 0 | QHash<QString, QSvgAbstractAnimatedProperty*> animatedProperies; |
277 | 0 | QSvgCssAnimation *animation = new QSvgCssAnimation; |
278 | | |
279 | | |
280 | | // Css Parser returns a list of all properties for each key frame. Here, |
281 | | // we store the key frames and values for each property for easier parsing. |
282 | 0 | QHash<QString, QList<CssKeyFrameValue>> keyFrameValues; |
283 | 0 | for (const auto &ruleSet : std::as_const(animationRule.ruleSets)) { |
284 | 0 | for (const QCss::Declaration &decl : ruleSet.declarations) { |
285 | 0 | QCss::Value timingFunction; |
286 | 0 | CssKeyFrameValue keyFrameValue = {ruleSet.keyFrame, decl.d->values, ruleSet.timingFunction}; |
287 | 0 | QList<CssKeyFrameValue> &value = keyFrameValues[decl.d->property]; |
288 | 0 | value.append(keyFrameValue); |
289 | 0 | } |
290 | 0 | } |
291 | |
|
292 | 0 | for (auto it = keyFrameValues.begin(); it != keyFrameValues.end(); it++) { |
293 | 0 | QStringView property = it.key(); |
294 | 0 | const QList<CssKeyFrameValue> &keyFrames = it.value(); |
295 | 0 | auto *prop = QSvgAbstractAnimatedProperty::createAnimatedProperty(property.toString()); |
296 | 0 | if (!prop) |
297 | 0 | continue; |
298 | | |
299 | 0 | bool result = false; |
300 | 0 | if (property == QLatin1StringView("fill") || property == QLatin1StringView("stroke")) |
301 | 0 | result = fillColorProperty(keyFrames, static_cast<QSvgAnimatedPropertyColor*>(prop)); |
302 | 0 | else if (property == QLatin1StringView("transform")) |
303 | 0 | result = fillTransformProperty(keyFrames, static_cast<QSvgAnimatedPropertyTransform*>(prop)); |
304 | 0 | else if (property == QLatin1StringView("fill-opacity") || property == QLatin1StringView("stroke-opacity") |
305 | 0 | || property == QLatin1StringView("opacity")) |
306 | 0 | result = fillOpacityProperty(keyFrames, static_cast<QSvgAnimatedPropertyFloat*>(prop)); |
307 | 0 | else if (property == QLatin1StringView("offset-distance")) |
308 | 0 | result = fillOffsetDistanceProperty(keyFrames, static_cast<QSvgAnimatedPropertyFloat*>(prop)); |
309 | |
|
310 | 0 | result &= fillPropertyEasing(keyFrames, prop); |
311 | 0 | if (!result) { |
312 | 0 | delete prop; |
313 | 0 | continue; |
314 | 0 | } |
315 | | |
316 | 0 | animatedProperies[property] = prop; |
317 | 0 | } |
318 | |
|
319 | 0 | for (auto it = animatedProperies.begin(); it != animatedProperies.end(); it++) |
320 | 0 | animation->appendProperty(it.value()); |
321 | |
|
322 | 0 | return animation; |
323 | 0 | } |
324 | | |
325 | | QSvgCssEasingPtr QSvgCssHandler::createEasing(QSvgCssValues::EasingFunction easingFunction, |
326 | | const QSvgCssValues::EasingValues &values) |
327 | 0 | { |
328 | 0 | QSvgCssEasingPtr easing; |
329 | |
|
330 | 0 | switch (easingFunction) { |
331 | 0 | case QSvgCssValues::EasingFunction::Ease: |
332 | 0 | case QSvgCssValues::EasingFunction::EaseIn: |
333 | 0 | case QSvgCssValues::EasingFunction::EaseOut: |
334 | 0 | case QSvgCssValues::EasingFunction::EaseInOut: |
335 | 0 | case QSvgCssValues::EasingFunction::Linear: |
336 | 0 | easing = createEasingFromKeyword(easingFunction); |
337 | 0 | break; |
338 | 0 | case QSvgCssValues::EasingFunction::Steps: |
339 | 0 | easing = createStepsEasing(std::get<QSvgCssValues::StepValues>(values)); |
340 | 0 | break; |
341 | 0 | default: |
342 | 0 | easing = createEasingFromKeyword(QSvgCssValues::EasingFunction::Ease); |
343 | 0 | break; |
344 | 0 | } |
345 | | |
346 | 0 | return easing; |
347 | 0 | } |
348 | | |
349 | | void QSvgCssHandler::collectAnimations(const QCss::StyleSheet &sheet) |
350 | 65.9k | { |
351 | 65.9k | auto sortFunction = [](QCss::AnimationRule::AnimationRuleSet r1, QCss::AnimationRule::AnimationRuleSet r2) { |
352 | 0 | return r1.keyFrame < r2.keyFrame; |
353 | 0 | }; |
354 | | |
355 | 65.9k | QList<QCss::AnimationRule> animationRules = sheet.animationRules; |
356 | 65.9k | for (QCss::AnimationRule &rule : animationRules) { |
357 | 0 | std::sort(rule.ruleSets.begin(), rule.ruleSets.end(), sortFunction); |
358 | 0 | m_animations[rule.animName] = rule; |
359 | 0 | } |
360 | 65.9k | } |
361 | | |
362 | | void QSvgCssHandler::parseStyleSheet(const QStringView str) |
363 | 65.9k | { |
364 | 65.9k | QString css = str.toString(); |
365 | 65.9k | QCss::StyleSheet sheet; |
366 | 65.9k | QCss::Parser(css).parse(&sheet); |
367 | 65.9k | m_selector->styleSheets.append(sheet); |
368 | | |
369 | 65.9k | collectAnimations(sheet); |
370 | 65.9k | } |
371 | | |
372 | | QString QSvgCssHandler::parseDecltoString(const QCss::Declaration &decl) |
373 | 35.7k | { |
374 | 35.7k | if (decl.d->property.isEmpty()) |
375 | 0 | return QString(); |
376 | | |
377 | 35.7k | QString valueStr; |
378 | 35.7k | const int valCount = decl.d->values.size(); |
379 | 405k | for (int i = 0; i < valCount; ++i) { |
380 | 369k | QCss::Value val = decl.d->values.at(i); |
381 | 369k | switch (val.type) { |
382 | 26.0k | case QCss::Value::TermOperatorComma: |
383 | 26.0k | valueStr += QLatin1Char(';'); |
384 | 26.0k | break; |
385 | 0 | case QCss::Value::Uri: |
386 | 0 | { |
387 | 0 | QString temp = val.toString(); |
388 | 0 | temp.prepend(QLatin1String("url(")); |
389 | 0 | temp.append(QLatin1Char(')')); |
390 | 0 | valueStr += temp; |
391 | 0 | break; |
392 | 0 | } |
393 | 51 | case QCss::Value::Function: |
394 | 51 | { |
395 | 51 | QStringList lst = val.variant.toStringList(); |
396 | 51 | valueStr.append(lst.at(0)); |
397 | 51 | valueStr.append(QLatin1Char('(')); |
398 | 102 | for (int i = 1; i < lst.size(); ++i) { |
399 | 51 | valueStr.append(lst.at(i)); |
400 | 51 | if ((i +1) < lst.size()) |
401 | 0 | valueStr.append(QLatin1Char(',')); |
402 | 51 | } |
403 | 51 | valueStr.append(QLatin1Char(')')); |
404 | 51 | break; |
405 | 0 | } |
406 | 0 | case QCss::Value::KnownIdentifier: |
407 | 0 | switch (val.variant.toInt()) { |
408 | 0 | case QCss::Value_None: |
409 | 0 | valueStr += QLatin1String("none"); |
410 | 0 | break; |
411 | 0 | case QCss::Value_Auto: |
412 | 0 | valueStr += QLatin1String("auto"); |
413 | 0 | break; |
414 | 0 | default: |
415 | 0 | valueStr += val.toString(); |
416 | 0 | break; |
417 | 0 | } |
418 | 0 | break; |
419 | 5.46k | case QCss::Value::Percentage: |
420 | 5.46k | valueStr += val.toString() + QLatin1Char('%'); |
421 | 5.46k | break; |
422 | 338k | default: |
423 | 338k | valueStr += val.toString(); |
424 | 338k | break; |
425 | 369k | } |
426 | | |
427 | 369k | if (i + 1 < valCount) |
428 | 333k | valueStr += QLatin1Char(' '); |
429 | 369k | } |
430 | | |
431 | 35.7k | return valueStr; |
432 | 35.7k | } |
433 | | |
434 | | void QSvgCssHandler::parseCSStoXMLAttrs(const QList<QCss::Declaration> &declarations, QXmlStreamAttributes &attributes) const |
435 | 803k | { |
436 | 839k | for (int i = 0; i < declarations.size(); ++i) { |
437 | 35.7k | const QCss::Declaration &decl = declarations.at(i); |
438 | 35.7k | QString valueStr = parseDecltoString(decl); |
439 | 35.7k | if (!valueStr.isEmpty()) |
440 | 35.7k | attributes.append(QString(), decl.d->property, valueStr); |
441 | 35.7k | } |
442 | 803k | } |
443 | | |
444 | | void QSvgCssHandler::parseCSStoXMLAttrs(const QString &css, QXmlStreamAttributes &attributes) const |
445 | 117k | { |
446 | | // preprocess (for unicode escapes), tokenize and remove comments |
447 | 117k | QCss::Parser parser(css); |
448 | 117k | QString key; |
449 | | |
450 | 442k | while (parser.hasNext()) { |
451 | 380k | parser.skipSpace(); |
452 | | |
453 | 380k | if (!parser.hasNext()) |
454 | 45 | break; |
455 | 380k | parser.next(); |
456 | | |
457 | 380k | QString name; |
458 | 380k | QString value; |
459 | | |
460 | 380k | if (parser.hasEscapeSequences) { |
461 | 5.98k | key = parser.lexem(); |
462 | 5.98k | name = key; |
463 | 374k | } else { |
464 | 374k | const QCss::Symbol &sym = parser.symbol(); |
465 | 374k | name = sym.text.mid(sym.start, sym.len); |
466 | 374k | } |
467 | | |
468 | 380k | parser.skipSpace(); |
469 | 380k | if (!parser.test(QCss::COLON)) |
470 | 55.4k | break; |
471 | | |
472 | 325k | parser.skipSpace(); |
473 | 325k | if (!parser.hasNext()) |
474 | 715 | break; |
475 | | |
476 | 324k | const int firstSymbol = parser.index; |
477 | 324k | int symbolCount = 0; |
478 | 12.9M | do { |
479 | 12.9M | parser.next(); |
480 | 12.9M | ++symbolCount; |
481 | 12.9M | } while (parser.hasNext() && !parser.test(QCss::SEMICOLON)); |
482 | | |
483 | 324k | bool canExtractValueByRef = !parser.hasEscapeSequences; |
484 | 324k | if (canExtractValueByRef) { |
485 | 319k | int len = parser.symbols.at(firstSymbol).len; |
486 | 8.62M | for (int i = firstSymbol + 1; i < firstSymbol + symbolCount; ++i) { |
487 | 8.30M | len += parser.symbols.at(i).len; |
488 | | |
489 | 8.30M | if (parser.symbols.at(i - 1).start + parser.symbols.at(i - 1).len |
490 | 8.30M | != parser.symbols.at(i).start) { |
491 | 0 | canExtractValueByRef = false; |
492 | 0 | break; |
493 | 0 | } |
494 | 8.30M | } |
495 | 319k | if (canExtractValueByRef) { |
496 | 319k | const QCss::Symbol &sym = parser.symbols.at(firstSymbol); |
497 | 319k | value = sym.text.mid(sym.start, len); |
498 | 319k | } |
499 | 319k | } |
500 | 324k | if (!canExtractValueByRef) { |
501 | | |
502 | 4.32M | for (int i = firstSymbol; i < parser.index - 1; ++i) |
503 | 4.31M | value += parser.symbols.at(i).lexem(); |
504 | 4.92k | } |
505 | | |
506 | 324k | attributes.append(QString(), name, value); |
507 | | |
508 | 324k | parser.skipSpace(); |
509 | 324k | } |
510 | 117k | } |
511 | | |
512 | | void QSvgCssHandler::styleLookup(QSvgNode *node, QXmlStreamAttributes &attributes) const |
513 | 803k | { |
514 | 803k | QCss::StyleSelector::NodePtr cssNode; |
515 | 803k | cssNode.ptr = node; |
516 | 803k | QList<QCss::Declaration> decls = m_selector->declarationsForNode(cssNode); |
517 | | |
518 | 803k | parseCSStoXMLAttrs(decls, attributes); |
519 | 803k | } |
520 | | |
521 | | QSvgCssEasingPtr QSvgCssHandler::createEasingFromKeyword(QSvgCssValues::EasingFunction easingFunction) |
522 | 0 | { |
523 | 0 | constexpr QPointF easeC1(0.25, 0.1); |
524 | 0 | constexpr QPointF easeC2(0.25, 1); |
525 | 0 | constexpr QPointF easeInC1(0.42, 0); |
526 | 0 | constexpr QPointF easeInC2(1, 1); |
527 | 0 | constexpr QPointF easeOutC1(0, 0); |
528 | 0 | constexpr QPointF easeOutC2(0.58, 1); |
529 | 0 | constexpr QPointF linearC1(0, 0); |
530 | 0 | constexpr QPointF linearC2(1, 1); |
531 | |
|
532 | 0 | QSvgCssEasingPtr easing; |
533 | |
|
534 | 0 | switch (easingFunction) { |
535 | 0 | case QSvgCssValues::EasingFunction::Ease: |
536 | 0 | easing = std::make_unique<QSvgCssCubicBezierEasing>(easingFunction, easeC1, easeC2); |
537 | 0 | break; |
538 | 0 | case QSvgCssValues::EasingFunction::EaseIn: |
539 | 0 | easing = std::make_unique<QSvgCssCubicBezierEasing>(easingFunction, easeInC1, easeInC2); |
540 | 0 | break; |
541 | 0 | case QSvgCssValues::EasingFunction::EaseOut: |
542 | 0 | easing = std::make_unique<QSvgCssCubicBezierEasing>(easingFunction, easeOutC1, easeOutC2); |
543 | 0 | break; |
544 | 0 | case QSvgCssValues::EasingFunction::EaseInOut: |
545 | 0 | easing = std::make_unique<QSvgCssCubicBezierEasing>(easingFunction, easeInC1, easeOutC2); |
546 | 0 | break; |
547 | 0 | case QSvgCssValues::EasingFunction::Linear: |
548 | 0 | easing = std::make_unique<QSvgCssCubicBezierEasing>(easingFunction, linearC1, linearC2); |
549 | 0 | break; |
550 | 0 | default: |
551 | 0 | Q_UNREACHABLE(); |
552 | 0 | break; |
553 | 0 | } |
554 | | |
555 | 0 | return easing; |
556 | 0 | } |
557 | | |
558 | | QSvgCssEasingPtr QSvgCssHandler::createStepsEasing(const QSvgCssValues::StepValues &values) |
559 | 0 | { |
560 | 0 | quint32 steps = values.steps; |
561 | 0 | QSvgCssValues::StepPosition position = values.stepPosition; |
562 | |
|
563 | 0 | return std::make_unique<QSvgCssStepsEasing>(steps, position); |
564 | 0 | } |
565 | | |
566 | | QT_END_NAMESPACE |