-
Notifications
You must be signed in to change notification settings - Fork 8k
/
lottiemodel.cpp
executable file
·324 lines (306 loc) · 11.3 KB
/
lottiemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "lottiemodel.h"
#include <cassert>
#include <iterator>
#include <stack>
#include "vimageloader.h"
#include "vline.h"
/*
* We process the iterator objects in the children list
* by iterating from back to front. when we find a repeater object
* we remove the objects from satrt till repeater object and then place
* under a new shape group object which we add it as children to the repeater
* object.
* Then we visit the childrens of the newly created shape group object to
* process the remaining repeater object(when children list contains more than
* one repeater).
*
*/
class LottieRepeaterProcesser {
public:
void visitChildren(LOTGroupData *obj)
{
for (auto i = obj->mChildren.rbegin(); i != obj->mChildren.rend();
++i) {
auto child = (*i).get();
if (child->type() == LOTData::Type::Repeater) {
LOTRepeaterData *repeater =
static_cast<LOTRepeaterData *>(child);
// check if this repeater is already processed
// can happen if the layer is an asset and referenced by
// multiple layer.
if (repeater->content()) continue;
repeater->setContent(std::make_shared<LOTShapeGroupData>());
LOTShapeGroupData *content = repeater->content();
// 1. increment the reverse iterator to point to the
// object before the repeater
++i;
// 2. move all the children till repater to the group
std::move(obj->mChildren.begin(), i.base(),
back_inserter(content->mChildren));
// 3. erase the objects from the original children list
obj->mChildren.erase(obj->mChildren.begin(), i.base());
// 5. visit newly created group to process remaining repeater
// object.
visitChildren(content);
// 6. exit the loop as the current iterators are invalid
break;
} else {
visit(child);
}
}
}
void visit(LOTData *obj)
{
switch (obj->type()) {
case LOTData::Type::Repeater:
case LOTData::Type::ShapeGroup:
case LOTData::Type::Layer: {
visitChildren(static_cast<LOTGroupData *>(obj));
break;
}
default:
break;
}
}
};
void LOTCompositionData::processRepeaterObjects()
{
LottieRepeaterProcesser visitor;
visitor.visit(mRootLayer.get());
}
VMatrix LOTRepeaterTransform::matrix(int frameNo, float multiplier) const
{
VPointF scale = mScale.value(frameNo) / 100.f;
scale.setX(std::pow(scale.x(), multiplier));
scale.setY(std::pow(scale.y(), multiplier));
VMatrix m;
m.translate(mPosition.value(frameNo) * multiplier)
.translate(mAnchor.value(frameNo))
.scale(scale)
.rotate(mRotation.value(frameNo) * multiplier)
.translate(-mAnchor.value(frameNo));
return m;
}
VMatrix TransformData::matrix(int frameNo, bool autoOrient) const
{
VMatrix m;
VPointF position;
if (mSeparate) {
position.setX(mX.value(frameNo));
position.setY(mY.value(frameNo));
} else {
position = mPosition.value(frameNo);
}
float angle = autoOrient ? mPosition.angle(frameNo) : 0;
if (m3D) {
m.translate(position)
.rotate(m3D->mRz.value(frameNo) + angle)
.rotate(m3D->mRy.value(frameNo), VMatrix::Axis::Y)
.rotate(m3D->mRx.value(frameNo), VMatrix::Axis::X)
.scale(mScale.value(frameNo) / 100.f)
.translate(-mAnchor.value(frameNo));
} else {
m.translate(position)
.rotate(mRotation.value(frameNo) + angle)
.scale(mScale.value(frameNo) / 100.f)
.translate(-mAnchor.value(frameNo));
}
return m;
}
int LOTStrokeData::getDashInfo(int frameNo, float *array) const
{
if (!mDash.mDashCount) return 0;
// odd case
if (mDash.mDashCount % 2) {
for (int i = 0; i < mDash.mDashCount; i++) {
array[i] = mDash.mDashArray[i].value(frameNo);
}
return mDash.mDashCount;
} else { // even case when last gap info is not provided.
int i;
for (i = 0; i < mDash.mDashCount - 1; i++) {
array[i] = mDash.mDashArray[i].value(frameNo);
}
array[i] = array[i - 1];
array[i + 1] = mDash.mDashArray[i].value(frameNo);
return mDash.mDashCount + 1;
}
}
int LOTGStrokeData::getDashInfo(int frameNo, float *array) const
{
if (!mDash.mDashCount) return 0;
// odd case
if (mDash.mDashCount % 2) {
for (int i = 0; i < mDash.mDashCount; i++) {
array[i] = mDash.mDashArray[i].value(frameNo);
}
return mDash.mDashCount;
} else { // even case when last gap info is not provided.
int i;
for (i = 0; i < mDash.mDashCount - 1; i++) {
array[i] = mDash.mDashArray[i].value(frameNo);
}
array[i] = array[i - 1];
array[i + 1] = mDash.mDashArray[i].value(frameNo);
return mDash.mDashCount + 1;
}
}
/**
* Both the color stops and opacity stops are in the same array.
* There are {@link #colorPoints} colors sequentially as:
* [
* ...,
* position,
* red,
* green,
* blue,
* ...
* ]
*
* The remainder of the array is the opacity stops sequentially as:
* [
* ...,
* position,
* opacity,
* ...
* ]
*/
void LOTGradient::populate(VGradientStops &stops, int frameNo)
{
LottieGradient gradData = mGradient.value(frameNo);
int size = gradData.mGradient.size();
float * ptr = gradData.mGradient.data();
int colorPoints = mColorPoints;
if (colorPoints == -1) { // for legacy bodymovin (ref: lottie-android)
colorPoints = size / 4;
}
int opacityArraySize = size - colorPoints * 4;
float *opacityPtr = ptr + (colorPoints * 4);
stops.clear();
int j = 0;
for (int i = 0; i < colorPoints; i++) {
float colorStop = ptr[0];
LottieColor color = LottieColor(ptr[3], ptr[2], ptr[1], nullptr);
if (opacityArraySize) {
if (j == opacityArraySize) {
// already reached the end
float stop1 = opacityPtr[j - 4];
float op1 = opacityPtr[j - 3];
float stop2 = opacityPtr[j - 2];
float op2 = opacityPtr[j - 1];
if (colorStop > stop2) {
stops.push_back(
std::make_pair(colorStop, color.toColor(op2)));
} else {
float progress = (colorStop - stop1) / (stop2 - stop1);
float opacity = op1 + progress * (op2 - op1);
stops.push_back(
std::make_pair(colorStop, color.toColor(opacity)));
}
continue;
}
for (; j < opacityArraySize; j += 2) {
float opacityStop = opacityPtr[j];
if (opacityStop < colorStop) {
// add a color using opacity stop
stops.push_back(std::make_pair(
opacityStop, color.toColor(opacityPtr[j + 1])));
continue;
}
// add a color using color stop
if (j == 0) {
stops.push_back(std::make_pair(
colorStop, color.toColor(opacityPtr[j + 1])));
} else {
float progress = (colorStop - opacityPtr[j - 2]) /
(opacityPtr[j] - opacityPtr[j - 2]);
float opacity =
opacityPtr[j - 1] +
progress * (opacityPtr[j + 1] - opacityPtr[j - 1]);
stops.push_back(
std::make_pair(colorStop, color.toColor(opacity)));
}
j += 2;
break;
}
} else {
stops.push_back(std::make_pair(colorStop, color.toColor()));
}
ptr += 4;
}
}
void LOTGradient::update(std::unique_ptr<VGradient> &grad, int frameNo)
{
bool init = false;
if (!grad) {
if (mGradientType == 1)
grad = std::make_unique<VLinearGradient>(0, 0, 0, 0);
else
grad = std::make_unique<VRadialGradient>(0, 0, 0, 0, 0, 0);
grad->mSpread = VGradient::Spread::Pad;
init = true;
}
if (!mGradient.isStatic() || init) {
populate(grad->mStops, frameNo);
}
if (mGradientType == 1) { // linear gradient
VPointF start = mStartPoint.value(frameNo);
VPointF end = mEndPoint.value(frameNo);
grad->linear.x1 = start.x();
grad->linear.y1 = start.y();
grad->linear.x2 = end.x();
grad->linear.y2 = end.y();
} else { // radial gradient
VPointF start = mStartPoint.value(frameNo);
VPointF end = mEndPoint.value(frameNo);
grad->radial.cx = start.x();
grad->radial.cy = start.y();
grad->radial.cradius =
VLine::length(start.x(), start.y(), end.x(), end.y());
/*
* Focal point is the point lives in highlight length distance from
* center along the line (start, end) and rotated by highlight angle.
* below calculation first finds the quadrant(angle) on which the point
* lives by applying inverse slope formula then adds the rotation angle
* to find the final angle. then point is retrived using circle equation
* of center, angle and distance.
*/
float progress = mHighlightLength.value(frameNo) / 100.0f;
if (vCompare(progress, 1.0f)) progress = 0.99f;
float startAngle = VLine(start, end).angle();
float highlightAngle = mHighlightAngle.value(frameNo);
float angle = ((startAngle + highlightAngle) * M_PI) / 180.0f;
grad->radial.fx =
grad->radial.cx + std::cos(angle) * progress * grad->radial.cradius;
grad->radial.fy =
grad->radial.cy + std::sin(angle) * progress * grad->radial.cradius;
// Lottie dosen't have any focal radius concept.
grad->radial.fradius = 0;
}
}
void LOTAsset::loadImageData(std::string data)
{
if (!data.empty())
mBitmap = VImageLoader::instance().load(data.c_str(), data.length());
}
void LOTAsset::loadImagePath(std::string path)
{
if (!path.empty()) mBitmap = VImageLoader::instance().load(path.c_str());
}