9using namespace Qt::Literals::StringLiterals;
13int parseCssClockValue(QStringView str,
bool *ok)
18 if (str.endsWith(
"ms"_L1)) {
21 }
else if (str.endsWith(
"s"_L1)) {
28 double val = ms * str.toDouble(ok);
30 if (val > std::numeric_limits<
int>::min() && val < std::numeric_limits<
int>::max())
31 res =
static_cast<
int>(val);
38bool isTimeValue(QStringView str) {
39 if (str.endsWith(
"ms"_L1))
41 else if (str.endsWith(
"s"_L1))
47 Q_UNUSED(str.toDouble(&ok))
51bool isIteration(QStringView str) {
52 if (str ==
"infinite"_L1)
55 Q_UNUSED(str.toDouble(&ok))
59bool isDirection(QStringView str) {
60 return str ==
"normal"_L1 || str ==
"reverse"_L1 || str.startsWith(
"alternate"_L1);
63bool isTimingFunction(QStringView str) {
64 return str.startsWith(
"linear"_L1) || str.startsWith(
"ease"_L1)
65 || str.startsWith(
"step"_L1) || str.startsWith(
"cubic-bezier"_L1);
68bool isFillMode(QStringView str) {
69 return str ==
"none"_L1 || str ==
"forwards"_L1 || str ==
"backwards"_L1 || str ==
"both"_L1;
72bool isPlayState(QStringView str) {
73 return str ==
"paused"_L1 || str ==
"running"_L1;
80 QRegularExpression re(
";| "_L1);
81 for (
int i = 0; i < attributes.size(); ++i) {
82 const QXmlStreamAttribute &attribute = attributes.at(i);
83 QStringView name = attribute.qualifiedName();
86 QStringView value = attribute.value();
88 switch (name.at(0).unicode()) {
91 if (name ==
"animation"_L1)
92 shortHandtoLonghandForm(value);
93 if (name ==
"animation-name"_L1)
94 m_names = value.split(re, Qt::SkipEmptyParts);
95 if (name ==
"animation-duration"_L1)
96 m_durations = value.split(re, Qt::SkipEmptyParts);
97 if (name ==
"animation-delay"_L1)
98 m_delays = value.split(re, Qt::SkipEmptyParts);
99 if (name ==
"animation-iteration-count"_L1)
100 m_iterationCounts = value.split(re, Qt::SkipEmptyParts);
101 if (name ==
"animation-direction"_L1)
102 m_directions = value.split(re, Qt::SkipEmptyParts);
103 if (name ==
"animation-timing-function"_L1)
104 m_timingFunctions = value.split(re, Qt::SkipEmptyParts);
105 if (name ==
"animation-fill-mode"_L1)
106 m_fillModes = value.split(re, Qt::SkipEmptyParts);
107 if (name ==
"animation-play-state"_L1)
108 m_playStates = value.split(re, Qt::SkipEmptyParts);
120 QList<QSvgAnimationProperty> parsedProperties;
121 for (
int i = 0; i < m_names.size(); i++) {
122 QSvgAnimationProperty property;
123 property.name = m_names.at(i);
125 if (!m_durations.isEmpty()) {
126 QStringView durationStr = m_durations.at(i % m_durations.size());
127 int duration = parseCssClockValue(durationStr, &ok);
128 property.duration = ok ? duration : 0;
131 if (!m_delays.isEmpty()) {
132 QStringView delayStr = m_delays.at(i % m_delays.size());
133 int delay = parseCssClockValue(delayStr, &ok);
134 property.delay = ok ? delay : 0;
137 if (!m_iterationCounts.isEmpty()) {
138 QStringView iterationStr = m_iterationCounts.at(i % m_iterationCounts.size());
140 if (iterationStr ==
"infinite"_L1) {
143 qreal count = iterationStr.toDouble(&ok);
144 iteration = ok ? qMax(1.0, count) : 0;
146 property.iteration = iteration;
149 parsedProperties.append(property);
152 return parsedProperties;
160 m_iterationCounts.clear();
161 m_directions.clear();
162 m_timingFunctions.clear();
164 m_playStates.clear();
166 enum Property : uchar {
169 IterationCount = 1 << 2,
171 TimingFunction = 1 << 4,
177 QList<QStringView> animations = value.split(QLatin1Char(
';'), Qt::SkipEmptyParts);
178 for (QStringView animation : animations) {
179 uchar propertyFlag = 0;
180 QList<QStringView> animationProperties = animation.split(QLatin1Char(
' '), Qt::SkipEmptyParts);
181 for (QStringView property : animationProperties) {
182 if (!(propertyFlag & Property::Duration) && isTimeValue(property)) {
183 m_durations.append(property);
184 propertyFlag |= Property::Duration;
185 }
else if (!(propertyFlag & Property::Delay) && isTimeValue(property)) {
186 m_delays.append(property);
187 propertyFlag |= Property::Delay;
188 }
else if (!(propertyFlag & Property::IterationCount) && isIteration(property)) {
189 m_iterationCounts.append(property);
190 propertyFlag |= Property::IterationCount;
191 }
else if (!(propertyFlag & Property::Direction) && isDirection(property)) {
192 m_directions.append(property);
193 propertyFlag |= Property::Direction;
194 }
else if (!(propertyFlag & Property::TimingFunction) && isTimingFunction(property)) {
195 m_timingFunctions.append(property);
196 propertyFlag |= Property::TimingFunction;
197 }
else if (!(propertyFlag & Property::FillMode) && isFillMode(property)) {
198 m_fillModes.append(property);
199 propertyFlag |= Property::FillMode;
200 }
else if (!(propertyFlag & Property::PlayState)&& isPlayState(property)) {
201 m_playStates.append(property);
202 propertyFlag |= Property::PlayState;
204 m_names.append(property);
205 propertyFlag |= Property::Name;
QList< QSvgAnimationProperty > parse() const
QSvgCssAnimationProperties(const QXmlStreamAttributes &attributes)