1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qlottielayer_p.h"
5
6#include <QJsonArray>
7#include <QJsonObject>
8#include <QJsonValue>
9#include <QLoggingCategory>
10#include <QtCore/QScopedValueRollback>
11
12#include "qlottieflatlayers_p.h"
13#include "qlottieshapelayer_p.h"
14#include "qlottieprecomplayer_p.h"
15#include "qlottiefilleffect_p.h"
16#include "qlottiebasictransform_p.h"
17#include "qlottierenderer_p.h"
18
19QT_BEGIN_NAMESPACE
20
21QLottieLayer::QLottieLayer(const QLottieLayer &other)
22 : QLottieBase(other)
23{
24 m_layerIndex = other.m_layerIndex;
25 m_startFrame = other.m_startFrame;
26 m_endFrame = other.m_endFrame;
27 m_startTime = other.m_startTime;
28 m_blendMode = other.m_blendMode;
29 m_3dLayer = other.m_3dLayer;
30 m_stretch = other.m_stretch;
31 m_hasLinkedLayer = other.m_hasLinkedLayer;
32 m_linkedLayerId = other.m_linkedLayerId;
33 m_td = other.m_td;
34 m_clipMode = other.m_clipMode;
35 if (other.m_layerTransform) {
36 m_layerTransform = new QLottieBasicTransform(*other.m_layerTransform);
37 m_layerTransform->setParent(this);
38 }
39 m_size = other.m_size;
40 if (other.m_effects) {
41 m_effects = new QLottieBase;
42 for (QLottieBase *effect : other.m_effects->children())
43 m_effects->appendChild(child: effect->clone());
44 }
45 //m_transformAtFirstFrame = other.m_transformAtFirstFrame;
46}
47
48QLottieLayer::~QLottieLayer()
49{
50 if (m_layerTransform)
51 delete m_layerTransform;
52 if (m_effects)
53 delete m_effects;
54}
55
56QLottieBase *QLottieLayer::clone() const
57{
58 return new QLottieLayer(*this);
59}
60
61QLottieLayer *QLottieLayer::construct(QJsonObject definition, const QMap<QString, QJsonObject> &assets)
62{
63 qCDebug(lcLottieQtLottieParser) << "QLottieLayer::construct()";
64
65 QLottieLayer *layer = nullptr;
66 int type = definition.value(key: QLatin1String("ty")).toInt();
67 switch (type) {
68 case 0:
69 qCDebug(lcLottieQtLottieParser) << "Parse precomp layer";
70 layer = new QLottiePrecompLayer(definition, assets);
71 break;
72 case 1:
73 qCDebug(lcLottieQtLottieParser) << "Parse solid layer";
74 layer = new QLottieSolidLayer(definition);
75 break;
76 case 2:
77 qCDebug(lcLottieQtLottieParser) << "Parse image layer";
78 layer = new QLottieImageLayer(definition);
79 break;
80 case 3:
81 qCDebug(lcLottieQtLottieParser) << "Parse null layer";
82 layer = new QLottieNullLayer(definition);
83 break;
84 case 4:
85 qCDebug(lcLottieQtLottieParser) << "Parse shape layer";
86 layer = new QLottieShapeLayer(definition);
87 break;
88 default:
89 qCInfo(lcLottieQtLottieParser) << "Unsupported layer type:" << type;
90 }
91 return layer;
92}
93
94// Take the content of a lottie layers tag and construct the corresponding layer objects
95// Also adds them as children to given parent
96int QLottieLayer::constructLayers(QJsonArray jsonLayers, QLottieBase *parent,
97 const QMap<QString, QJsonObject> &assets)
98{
99 int layersAdded = 0;
100 QJsonArray::const_iterator jsonLayerIt = jsonLayers.constEnd();
101 while (jsonLayerIt != jsonLayers.constBegin()) {
102 jsonLayerIt--;
103 QJsonObject jsonLayer = (*jsonLayerIt).toObject();
104 if (jsonLayer.value(key: QLatin1String("ty")).toInt() == 2) {
105 QString refId = jsonLayer.value(key: QLatin1String("refId")).toString();
106 jsonLayer.insert(key: QLatin1String("asset"), value: assets.value(key: refId));
107 }
108 QLottieLayer *layer = QLottieLayer::construct(definition: jsonLayer, assets);
109 if (layer) {
110 layer->setParent(parent);
111 // Mask layers must be rendered before the layers they affect to
112 // although they appear after in layer hierarchy. For this reason
113 // move a mask in front of the affected layer, so it will be rendered first
114 if (layer->isMaskLayer())
115 parent->insertChildBeforeLast(child: layer);
116 else
117 parent->appendChild(child: layer);
118 layersAdded++;
119 }
120 }
121 return layersAdded;
122}
123
124bool QLottieLayer::active(int frame) const
125{
126 return (!m_hidden && ((frame >= m_startFrame && frame <= m_endFrame) || isStructureDumping()));
127}
128
129void QLottieLayer::parse(const QJsonObject &definition)
130{
131 QLottieBase::parse(definition);
132 if (m_hidden)
133 return;
134
135 qCDebug(lcLottieQtLottieParser) << "QLottieLayer::parse():" << m_name;
136
137 m_layerIndex = definition.value(key: QLatin1String("ind")).toVariant().toInt();
138 m_startFrame = definition.value(key: QLatin1String("ip")).toVariant().toInt();
139 m_endFrame = definition.value(key: QLatin1String("op")).toVariant().toInt();
140 m_blendMode = definition.value(key: QLatin1String("lottie")).toVariant().toInt();
141 m_autoOrient = definition.value(key: QLatin1String("ao")).toBool();
142 m_3dLayer = definition.value(key: QLatin1String("ddd")).toBool();
143 m_stretch = definition.value(key: QLatin1String("sr")).toVariant().toReal();
144 m_linkedLayerId = definition.value(key: QLatin1String("parent")).toVariant().toInt(ok: &m_hasLinkedLayer);
145 m_td = definition.value(key: QLatin1String("td")).toInt();
146 int clipMode = definition.value(key: QLatin1String("tt")).toInt(defaultValue: -1);
147 if (clipMode > -1 && clipMode < 5)
148 m_clipMode = static_cast<MatteClipMode>(clipMode);
149
150 QJsonObject trans = definition.value(key: QLatin1String("ks")).toObject();
151 m_layerTransform = new QLottieBasicTransform(trans, this);
152
153 QJsonArray effects = definition.value(key: QLatin1String("ef")).toArray();
154 parseEffects(definition: effects);
155
156 if (m_clipMode > 2)
157 qCInfo(lcLottieQtLottieParser)
158 << "Lottie Layer: Only alpha mask layer supported:" << m_clipMode;
159 if (m_blendMode > 0)
160 qCInfo(lcLottieQtLottieParser)
161 << "Lottie Layer: Unsupported blend mode" << m_blendMode;
162 if (m_stretch > 1)
163 qCInfo(lcLottieQtLottieParser)
164 << "Lottie Layer: stretch not supported" << m_stretch;
165 if (m_autoOrient)
166 qCInfo(lcLottieQtLottieParser)
167 << "Lottie Layer: auto-orient not supported";
168 if (m_3dLayer)
169 qCInfo(lcLottieQtLottieParser)
170 << "Lottie Layer: is a 3D layer, but not handled";
171}
172
173void QLottieLayer::updateProperties(int frame)
174{
175 if (m_hasLinkedLayer)
176 resolveLinkedLayer();
177
178 int adjFrame = frame - m_startTime;
179 m_isActive = active(frame: adjFrame);
180 if (!m_isActive)
181 return;
182
183 // Update first effects, as they are not children of the layer
184 if (m_effects) {
185 for (QLottieBase* effect : m_effects->children())
186 effect->updateProperties(frame: adjFrame);
187 }
188
189 m_layerTransform->updateProperties(frame: adjFrame);
190
191 QLottieBase::updateProperties(frame: adjFrame);
192}
193
194void QLottieLayer::render(QLottieRenderer &renderer) const
195{
196 if (!m_isActive)
197 return;
198
199 // Render first effects, as they affect the children
200 renderEffects(renderer);
201
202 // In case there is a linked layer, apply its transform first
203 // as it affects tranforms of this layer too
204 applyLayerTransform(renderer);
205
206 renderer.render(layer: *this);
207
208 for (QLottieBase *child : children()) {
209 if (child->hidden())
210 continue;
211 child->render(renderer);
212 }
213}
214
215QLottieBase *QLottieLayer::findChild(const QString &childName)
216{
217 QLottieBase *child = nullptr;
218
219 if (m_effects)
220 child = m_effects->findChild(childName);
221
222 if (child)
223 return child;
224 else
225 return QLottieBase::findChild(childName);
226}
227
228QLottieLayer *QLottieLayer::resolveLinkedLayer()
229{
230 if (m_linkedLayer)
231 return m_linkedLayer;
232
233 Q_ASSERT(parent());
234
235 for (QLottieBase *child : parent()->children()) {
236 QLottieLayer *layer = static_cast<QLottieLayer*>(child);
237 if (layer->layerId() == m_linkedLayerId) {
238 m_linkedLayer = layer;
239 break;
240 }
241 }
242 return m_linkedLayer;
243}
244
245QLottieLayer *QLottieLayer::linkedLayer() const
246{
247 return m_linkedLayer;
248}
249
250bool QLottieLayer::isClippedLayer() const
251{
252 return m_clipMode != NoClip;
253}
254
255bool QLottieLayer::isMaskLayer() const
256{
257 return m_td > 0;
258}
259
260QLottieLayer::MatteClipMode QLottieLayer::clipMode() const
261{
262 return m_clipMode;
263}
264
265int QLottieLayer::layerId() const
266{
267 return m_layerIndex;
268}
269
270QLottieBasicTransform *QLottieLayer::transform() const
271{
272 return m_layerTransform;
273}
274
275void QLottieLayer::renderEffects(QLottieRenderer &renderer) const
276{
277 if (!m_effects)
278 return;
279
280 for (QLottieBase* effect : m_effects->children()) {
281 if (effect->hidden())
282 continue;
283 effect->render(renderer);
284 }
285}
286
287void QLottieLayer::applyLayerTransform(QLottieRenderer &renderer) const
288{
289 if (m_applyingLayerTransform)
290 return;
291 QScopedValueRollback<bool> recursionGuard(m_applyingLayerTransform, true);
292
293 if (!isStructureDumping()) {
294 if (QLottieLayer *ll = linkedLayer())
295 ll->applyLayerTransform(renderer);
296 }
297 if (m_layerTransform)
298 m_layerTransform->render(renderer); // TBD: except opacity
299}
300
301QSize QLottieLayer::size() const
302{
303 return m_size;
304}
305
306const QLottieLayer *QLottieLayer::checkedCast(const QLottieBase *node)
307{
308 const QLottieLayer *res = nullptr;
309 if (node && node->type() >= LOTTIE_LAYER_PRECOMP_IX && node->type() <= LOTTIE_LAYER_TEXT_IX)
310 res = static_cast<const QLottieLayer *>(node);
311 return res;
312}
313
314void QLottieLayer::parseEffects(const QJsonArray &definition, QLottieBase *effectRoot)
315{
316 QJsonArray::const_iterator it = definition.constEnd();
317 while (it != definition.constBegin()) {
318 // Create effects container if at least one effect found
319 if (!m_effects) {
320 m_effects = new QLottieBase;
321 effectRoot = m_effects;
322 }
323 it--;
324 QJsonObject effect = (*it).toObject();
325 int type = effect.value(key: QLatin1String("ty")).toInt();
326 switch (type) {
327 case 0:
328 {
329 QLottieBase *slider = new QLottieBase;
330 slider->parse(definition: effect);
331 effectRoot->appendChild(child: slider);
332 break;
333 }
334 case 5:
335 {
336 if (effect.value(key: QLatin1String("en")).toInt()) {
337 QLottieBase *group = new QLottieBase;
338 group->parse(definition: effect);
339 effectRoot->appendChild(child: group);
340 parseEffects(definition: effect.value(key: QLatin1String("ef")).toArray(), effectRoot: group);
341 }
342 break;
343 }
344 case 21:
345 {
346 QLottieFillEffect *fill = new QLottieFillEffect;
347 fill->construct(definition: effect);
348 effectRoot->appendChild(child: fill);
349 break;
350 }
351 default:
352 qCInfo(lcLottieQtLottieParser)
353 << "QLottieLayer: Unsupported effect" << type;
354 }
355 }
356}
357
358QT_END_NAMESPACE
359

source code of qtlottie/src/lottie/qlottielayer.cpp