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