1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qlottieshape_p.h"
5
6#include <QJsonArray>
7#include <QJsonObject>
8#include <QLoggingCategory>
9
10#include "qlottiegroup_p.h"
11#include "qlottiefill_p.h"
12#include "qlottiegfill_p.h"
13#include "qlottiestroke_p.h"
14#include "qlottierect_p.h"
15#include "qlottieellipse_p.h"
16#include "qlottiepolystar_p.h"
17#include "qlottieround_p.h"
18#include "qlottietrimpath_p.h"
19#include "qlottieshapetransform_p.h"
20#include "qlottiefreeformshape_p.h"
21#include "qlottierepeater_p.h"
22#include "qlottieconstants_p.h"
23
24QT_BEGIN_NAMESPACE
25
26QLottieShape::QLottieShape(const QLottieShape &other)
27 : QLottieBase(other)
28{
29 m_direction = other.m_direction;
30 m_path = other.m_path;
31 m_appliedTrim = other.m_appliedTrim;
32}
33
34QLottieBase *QLottieShape::clone() const
35{
36 return new QLottieShape(*this);
37}
38
39QLottieShape *QLottieShape::construct(QJsonObject definition, QLottieBase *parent)
40{
41 qCDebug(lcLottieQtLottieParser) << "QLottieShape::construct()";
42
43 QLottieShape *shape = nullptr;
44 const QByteArray type = definition.value(key: QLatin1String("ty")).toString().toLatin1();
45
46 if (Q_UNLIKELY(type.size() != 2)) {
47 qCInfo(lcLottieQtLottieParser) << "Unsupported shape type:"
48 << type;
49 return shape;
50 }
51
52#define LOTTIE_SHAPE_TAG(c1, c2) int((quint32(c1)<<8) | quint32(c2))
53
54 int typeToBuild = LOTTIE_SHAPE_TAG(type[0], type[1]);
55
56 switch (typeToBuild) {
57 case LOTTIE_SHAPE_TAG('g', 'r'):
58 {
59 qCDebug(lcLottieQtLottieParser) << "Parse group";
60 shape = new QLottieGroup(definition, parent);
61 shape->setType(LOTTIE_SHAPE_GROUP_IX);
62 break;
63 }
64 case LOTTIE_SHAPE_TAG('r', 'c'):
65 {
66 qCDebug(lcLottieQtLottieParser) << "Parse m_rect";
67 shape = new QLottieRect(definition, parent);
68 shape->setType(LOTTIE_SHAPE_RECT_IX);
69 break;
70 }
71 case LOTTIE_SHAPE_TAG('f', 'l'):
72 {
73 qCDebug(lcLottieQtLottieParser) << "Parse fill";
74 shape = new QLottieFill(definition, parent);
75 shape->setType(LOTTIE_SHAPE_FILL_IX);
76 break;
77 }
78 case LOTTIE_SHAPE_TAG('g', 'f'):
79 {
80 qCDebug(lcLottieQtLottieParser) << "Parse group fill";
81 shape = new QLottieGFill(definition, parent);
82 shape->setType(LOTTIE_SHAPE_GFILL_IX);
83 break;
84 }
85 case LOTTIE_SHAPE_TAG('s', 't'):
86 {
87 qCDebug(lcLottieQtLottieParser) << "Parse stroke";
88 shape = new QLottieStroke(definition, parent);
89 shape->setType(LOTTIE_SHAPE_STROKE_IX);
90 break;
91 }
92 case LOTTIE_SHAPE_TAG('t', 'r'):
93 {
94 qCDebug(lcLottieQtLottieParser) << "Parse shape transform";
95 shape = new QLottieShapeTransform(definition, parent);
96 shape->setType(LOTTIE_SHAPE_TRANS_IX);
97 break;
98 }
99 case LOTTIE_SHAPE_TAG('e', 'l'):
100 {
101 qCDebug(lcLottieQtLottieParser) << "Parse ellipse";
102 shape = new QLottieEllipse(definition, parent);
103 shape->setType(LOTTIE_SHAPE_ELLIPSE_IX);
104 break;
105 }
106 case LOTTIE_SHAPE_TAG('s', 'r'):
107 {
108 qCDebug(lcLottieQtLottieParser) << "Parse polystar";
109 shape = new QLottiePolyStar(definition, parent);
110 shape->setType(LOTTIE_SHAPE_STAR_IX);
111 break;
112 }
113 case LOTTIE_SHAPE_TAG('r', 'd'):
114 {
115 qCDebug(lcLottieQtLottieParser) << "Parse round";
116 shape = new QLottieRound(definition, parent);
117 shape->setType(LOTTIE_SHAPE_ROUND_IX);
118 break;
119 }
120 case LOTTIE_SHAPE_TAG('s', 'h'):
121 {
122 qCDebug(lcLottieQtLottieParser) << "Parse shape";
123 shape = new QLottieFreeFormShape(definition, parent);
124 shape->setType(LOTTIE_SHAPE_SHAPE_IX);
125 break;
126 }
127 case LOTTIE_SHAPE_TAG('t', 'm'):
128 {
129 qCDebug(lcLottieQtLottieParser) << "Parse trim path";
130 shape = new QLottieTrimPath(definition, parent);
131 shape->setType(LOTTIE_SHAPE_TRIM_IX);
132 break;
133 }
134 case LOTTIE_SHAPE_TAG('r', 'p'):
135 {
136 qCDebug(lcLottieQtLottieParser) << "Parse trim path";
137 shape = new QLottieRepeater(definition, parent);
138 shape->setType(LOTTIE_SHAPE_REPEATER_IX);
139 break;
140 }
141 case LOTTIE_SHAPE_TAG('g', 's'): // ### LOTTIE_SHAPE_GSTROKE_IX
142 // fall through
143 default:
144 qCInfo(lcLottieQtLottieParser) << "Unsupported shape type:"
145 << type;
146 }
147
148#undef LOTTIE_SHAPE_TAG
149
150 return shape;
151}
152
153bool QLottieShape::acceptsTrim() const
154{
155 return false;
156}
157
158void QLottieShape::applyTrim(const QLottieTrimPath &trimmer)
159{
160 if (trimmer.isParallel())
161 m_path = trimmer.trim(path: m_path);
162}
163
164int QLottieShape::direction() const
165{
166 return m_direction;
167}
168
169const QPainterPath &QLottieShape::path() const
170{
171 return m_path;
172}
173
174QT_END_NAMESPACE
175

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