1 #include "lottieitem.h"
10 * 1. time stretch is pre calculated and applied to all the properties of the
11 * lottilayer model and all its children
12 * 2. The frame property could be reversed using,time-reverse layer property in
13 * AE. which means (start frame > endFrame) 3.
16 LOTCompItem::LOTCompItem(LOTModel *model)
17 : mRootModel(model), mUpdateViewBox(false), mCurFrameNo(-1)
19 mCompData = model->mRoot.get();
20 mRootLayer = createLayerItem(mCompData->mRootLayer.get());
21 mRootLayer->updateStaticProperty();
22 mViewSize = mCompData->size();
25 std::unique_ptr<LOTLayerItem>
26 LOTCompItem::createLayerItem(LOTLayerData *layerData)
28 switch (layerData->mLayerType) {
29 case LayerType::Precomp: {
30 return std::make_unique<LOTCompLayerItem>(layerData);
33 case LayerType::Solid: {
34 return std::make_unique<LOTSolidLayerItem>(layerData);
37 case LayerType::Shape: {
38 return std::make_unique<LOTShapeLayerItem>(layerData);
41 case LayerType::Null: {
42 return std::make_unique<LOTNullLayerItem>(layerData);
51 void LOTCompItem::resize(const VSize &size)
53 if (mViewSize == size) return;
55 mUpdateViewBox = true;
58 VSize LOTCompItem::size() const
63 bool LOTCompItem::update(int frameNo)
65 // check if cached frame is same as requested frame.
66 if (!mUpdateViewBox && (mCurFrameNo == frameNo)) return false;
69 * if viewbox dosen't scale exactly to the viewport
70 * we scale the viewbox keeping AspectRatioPreserved and then align the
71 * viewbox to the viewport using AlignCenter rule.
73 VSize viewPort = mViewSize;
74 VSize viewBox = mCompData->size();
76 float sx = float(viewPort.width()) / viewBox.width();
77 float sy = float(viewPort.height()) / viewBox.height();
78 float scale = fmin(sx, sy);
79 float tx = (viewPort.width() - viewBox.width() * scale) * 0.5;
80 float ty = (viewPort.height() - viewBox.height() * scale) * 0.5;
83 m.scale(scale, scale).translate(tx, ty);
84 mRootLayer->update(frameNo, m, 1.0);
86 mCurFrameNo = frameNo;
87 mUpdateViewBox = false;
91 void LOTCompItem::buildRenderList()
93 mDrawableList.clear();
94 mRootLayer->renderList(mDrawableList);
97 for (auto &i : mDrawableList) {
98 LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i);
100 mRenderList.push_back(lotDrawable->mCNode.get());
104 const std::vector<LOTNode *> &LOTCompItem::renderList() const
109 bool LOTCompItem::render(const lottie::Surface &surface)
111 VBitmap bitmap((uchar *)surface.buffer(), surface.width(), surface.height(),
112 surface.bytesPerLine(), VBitmap::Format::ARGB32_Premultiplied,
115 /* schedule all preprocess task for this frame at once.
117 mDrawableList.clear();
118 mRootLayer->renderList(mDrawableList);
119 for (auto &e : mDrawableList) {
123 VPainter painter(&bitmap);
124 mRootLayer->render(&painter, {}, {}, nullptr);
129 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
130 float parentAlpha, const DirtyFlag &/*flag*/)
132 if (mData->mShape.isStatic()) {
133 if (mLocalPath.empty()) {
134 mData->mShape.value(frameNo).toPath(mLocalPath);
137 mData->mShape.value(frameNo).toPath(mLocalPath);
139 float opacity = mData->opacity(frameNo);
140 opacity = opacity * parentAlpha;
141 mCombinedAlpha = opacity;
143 VPath path = mLocalPath;
144 path.transform(parentMatrix);
146 mRleTask = VRaster::generateFillInfo(std::move(path), std::move(mRle));
150 VRle LOTMaskItem::rle()
152 if (mRleTask.valid()) {
153 mRle = mRleTask.get();
154 if (!vCompare(mCombinedAlpha, 1.0f))
155 mRle *= (mCombinedAlpha * 255);
156 if (mData->mInv) mRle.invert();
161 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
165 mDrawableList.clear();
166 matteSource->renderList(mDrawableList);
167 for (auto &i : mDrawableList) {
168 matteRle = matteRle + i->rle();
171 if (!inheritMatte.empty())
172 matteRle = matteRle & inheritMatte;
174 matteRle = inheritMatte;
176 mDrawableList.clear();
177 renderList(mDrawableList);
181 mask = maskRle(painter->clipBoundingRect());
182 if (!inheritMask.empty())
183 mask = mask & inheritMask;
184 // if resulting mask is empty then return.
191 for (auto &i : mDrawableList) {
192 painter->setBrush(i->mBrush);
194 if (!mask.empty()) rle = rle & mask;
196 if (rle.empty()) continue;
198 if (!matteRle.empty()) {
199 if (mLayerData->mMatteType == MatteType::AlphaInv) {
200 rle = rle - matteRle;
202 rle = rle & matteRle;
205 painter->drawRle(VPoint(), rle);
209 VRle LOTLayerItem::maskRle(const VRect &clipRect)
212 for (auto &i : mMasks) {
213 switch (i->maskMode()) {
214 case LOTMaskData::Mode::Add: {
215 rle = rle + i->rle();
218 case LOTMaskData::Mode::Substarct: {
219 if (rle.empty() && !clipRect.empty())
220 rle = VRle::toRle(clipRect);
221 rle = rle - i->rle();
224 case LOTMaskData::Mode::Intersect: {
225 rle = rle & i->rle();
228 case LOTMaskData::Mode::Difference: {
229 rle = rle ^ i->rle();
239 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData): mLayerData(layerData)
241 if (mLayerData->mHasMask) {
242 for (auto &i : mLayerData->mMasks) {
243 mMasks.push_back(std::make_unique<LOTMaskItem>(i.get()));
248 void LOTLayerItem::updateStaticProperty()
250 if (mParentLayer) mParentLayer->updateStaticProperty();
252 mStatic = mLayerData->isStatic();
253 mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
254 mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
257 void LOTLayerItem::update(int frameNo, const VMatrix &parentMatrix,
261 // 1. check if the layer is part of the current frame
262 if (!visible()) return;
264 // 2. calculate the parent matrix and alpha
265 VMatrix m = matrix(frameNo);
267 float alpha = parentAlpha * opacity(frameNo);
269 // 6. update the mask
271 for (auto &i : mMasks) i->update(frameNo, m, alpha, mDirtyFlag);
274 // 3. update the dirty flag based on the change
275 if (!mCombinedMatrix.fuzzyCompare(m)) {
276 mDirtyFlag |= DirtyFlagBit::Matrix;
278 if (!vCompare(mCombinedAlpha, alpha)) {
279 mDirtyFlag |= DirtyFlagBit::Alpha;
282 mCombinedAlpha = alpha;
284 // 4. if no parent property change and layer is static then nothing to do.
285 if ((flag() & DirtyFlagBit::None) && isStatic()) return;
287 // 5. update the content of the layer
290 // 6. reset the dirty flag
291 mDirtyFlag = DirtyFlagBit::None;
294 float LOTLayerItem::opacity(int frameNo) const
296 return mLayerData->mTransform->opacity(frameNo);
299 VMatrix LOTLayerItem::matrix(int frameNo) const
302 return mLayerData->mTransform->matrix(frameNo) *
303 mParentLayer->matrix(frameNo);
305 return mLayerData->mTransform->matrix(frameNo);
308 bool LOTLayerItem::visible() const
310 if (frameNo() >= mLayerData->inFrame() &&
311 frameNo() < mLayerData->outFrame())
317 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel)
318 : LOTLayerItem(layerModel)
320 // 1. create layer item
321 for (auto &i : mLayerData->mChildren) {
322 LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get());
323 auto layerItem = LOTCompItem::createLayerItem(layerModel);
324 if (layerItem) mLayers.push_back(std::move(layerItem));
327 // 2. update parent layer
328 for (const auto &layer : mLayers) {
329 int id = layer->parentId();
331 auto search = std::find_if(mLayers.begin(), mLayers.end(),
332 [id](const auto& val){ return val->id() == id;});
333 if (search != mLayers.end()) layer->setParentLayer((*search).get());
335 // update the precomp layer if its not the root layer.
336 if (!layerModel->root()) layer->setPrecompLayer(this);
339 // 3. keep the layer in back-to-front order.
340 // as lottie model keeps the data in front-toback-order.
341 std::reverse(mLayers.begin(), mLayers.end());
344 void LOTCompLayerItem::updateStaticProperty()
346 LOTLayerItem::updateStaticProperty();
348 for (const auto &layer : mLayers) {
349 layer->updateStaticProperty();
353 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
357 mDrawableList.clear();
358 matteSource->renderList(mDrawableList);
359 for (auto &i : mDrawableList) {
360 matteRle = matteRle + i->rle();
363 if (!inheritMatte.empty())
364 matteRle = matteRle & inheritMatte;
366 matteRle = inheritMatte;
371 mask = maskRle(painter->clipBoundingRect());
372 if (!inheritMask.empty())
373 mask = mask & inheritMask;
374 // if resulting mask is empty then return.
381 LOTLayerItem *matteLayer = nullptr;
382 for (const auto &layer : mLayers) {
383 if (!matteLayer && layer->hasMatte()) {
384 matteLayer = layer.get();
389 if (matteLayer->visible() && layer->visible())
390 matteLayer->render(painter, mask, matteRle, layer.get());
391 matteLayer = nullptr;
393 if (layer->visible())
394 layer->render(painter, mask, matteRle, nullptr);
399 void LOTCompLayerItem::updateContent()
401 for (const auto &layer : mLayers) {
402 layer->update(frameNo(), combinedMatrix(), combinedAlpha());
406 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
408 if (!visible()) return;
410 for (const auto &layer : mLayers) {
411 layer->renderList(list);
415 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData)
416 : LOTLayerItem(layerData)
420 void LOTSolidLayerItem::updateContent()
423 mRenderNode = std::make_unique<LOTDrawable>();
424 mRenderNode->mType = VDrawable::Type::Fill;
425 mRenderNode->mFlag |= VDrawable::DirtyState::All;
428 if (flag() & DirtyFlagBit::Matrix) {
431 VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
432 path.transform(combinedMatrix());
433 mRenderNode->mFlag |= VDrawable::DirtyState::Path;
434 mRenderNode->mPath = path;
436 if (flag() & DirtyFlagBit::Alpha) {
437 LottieColor color = mLayerData->solidColor();
438 VBrush brush(color.toColor(combinedAlpha()));
439 mRenderNode->setBrush(brush);
440 mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
444 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
446 if (!visible()) return;
448 list.push_back(mRenderNode.get());
451 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData)
452 : LOTLayerItem(layerData)
455 void LOTNullLayerItem::updateContent() {}
457 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData)
458 : LOTLayerItem(layerData)
460 mRoot = std::make_unique<LOTContentGroupItem>(nullptr);
461 mRoot->addChildren(layerData);
463 std::vector<LOTPathDataItem *> list;
464 mRoot->processPaintItems(list);
466 if (layerData->hasPathOperator()) {
468 mRoot->processTrimItems(list);
472 std::unique_ptr<LOTContentItem>
473 LOTShapeLayerItem::createContentItem(LOTData *contentData)
475 switch (contentData->type()) {
476 case LOTData::Type::ShapeGroup: {
477 return std::make_unique<LOTContentGroupItem>(
478 static_cast<LOTShapeGroupData *>(contentData));
481 case LOTData::Type::Rect: {
482 return std::make_unique<LOTRectItem>(static_cast<LOTRectData *>(contentData));
485 case LOTData::Type::Ellipse: {
486 return std::make_unique<LOTEllipseItem>(static_cast<LOTEllipseData *>(contentData));
489 case LOTData::Type::Shape: {
490 return std::make_unique<LOTShapeItem>(static_cast<LOTShapeData *>(contentData));
493 case LOTData::Type::Polystar: {
494 return std::make_unique<LOTPolystarItem>(static_cast<LOTPolystarData *>(contentData));
497 case LOTData::Type::Fill: {
498 return std::make_unique<LOTFillItem>(static_cast<LOTFillData *>(contentData));
501 case LOTData::Type::GFill: {
502 return std::make_unique<LOTGFillItem>(static_cast<LOTGFillData *>(contentData));
505 case LOTData::Type::Stroke: {
506 return std::make_unique<LOTStrokeItem>(static_cast<LOTStrokeData *>(contentData));
509 case LOTData::Type::GStroke: {
510 return std::make_unique<LOTGStrokeItem>(static_cast<LOTGStrokeData *>(contentData));
513 case LOTData::Type::Repeater: {
514 return std::make_unique<LOTRepeaterItem>(static_cast<LOTRepeaterData *>(contentData));
517 case LOTData::Type::Trim: {
518 return std::make_unique<LOTTrimItem>(static_cast<LOTTrimData *>(contentData));
527 void LOTShapeLayerItem::updateContent()
529 mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
531 if (mLayerData->hasPathOperator()) {
536 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
538 if (!visible()) return;
539 mRoot->renderList(list);
542 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data) : mData(data)
547 void LOTContentGroupItem::addChildren(LOTGroupData *data)
551 for (auto &i : data->mChildren) {
552 auto content = LOTShapeLayerItem::createContentItem(i.get());
554 content->setParent(this);
555 mContents.push_back(std::move(content));
559 // keep the content in back-to-front order.
560 std::reverse(mContents.begin(), mContents.end());
563 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix,
564 float parentAlpha, const DirtyFlag &flag)
566 VMatrix m = parentMatrix;
567 float alpha = parentAlpha;
568 DirtyFlag newFlag = flag;
571 // update the matrix and the flag
572 if ((flag & DirtyFlagBit::Matrix) ||
573 !mData->mTransform->staticMatrix()) {
574 newFlag |= DirtyFlagBit::Matrix;
576 m = mData->mTransform->matrix(frameNo);
578 alpha *= mData->mTransform->opacity(frameNo);
580 if (!vCompare(alpha, parentAlpha)) {
581 newFlag |= DirtyFlagBit::Alpha;
587 for (const auto &content : mContents) {
588 content->update(frameNo, m, alpha, newFlag);
592 void LOTContentGroupItem::applyTrim()
594 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
595 auto content = (*i).get();
596 if (auto trim = dynamic_cast<LOTTrimItem *>(content)) {
598 } else if (auto group = dynamic_cast<LOTContentGroupItem *>(content)) {
604 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
606 for (const auto &content : mContents) {
607 content->renderList(list);
611 void LOTContentGroupItem::processPaintItems(
612 std::vector<LOTPathDataItem *> &list)
614 int curOpCount = list.size();
615 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
616 auto content = (*i).get();
617 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
618 // add it to the list
619 list.push_back(pathNode);
620 } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(content)) {
621 // the node is a paint data node update the path list of the paint item.
622 paintNode->addPathItems(list, curOpCount);
623 } else if (auto groupNode =
624 dynamic_cast<LOTContentGroupItem *>(content)) {
625 // update the groups node with current list
626 groupNode->processPaintItems(list);
631 void LOTContentGroupItem::processTrimItems(
632 std::vector<LOTPathDataItem *> &list)
634 int curOpCount = list.size();
635 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
636 auto content = (*i).get();
637 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
638 // add it to the list
639 list.push_back(pathNode);
640 } else if (auto trimNode = dynamic_cast<LOTTrimItem *>(content)) {
641 // the node is a paint data node update the path list of the paint item.
642 trimNode->addPathItems(list, curOpCount);
643 } else if (auto groupNode =
644 dynamic_cast<LOTContentGroupItem *>(content)) {
645 // update the groups node with current list
646 groupNode->processTrimItems(list);
651 void LOTPathDataItem::update(int frameNo, const VMatrix &,
652 float, const DirtyFlag &flag)
654 mPathChanged = false;
656 // 1. update the local path if needed
657 if (hasChanged(frameNo)) {
658 updatePath(mLocalPath, frameNo);
665 // 3. compute the final path with parentMatrix
666 if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
671 const VPath & LOTPathDataItem::finalPath()
673 if (mPathChanged || mNeedUpdate) {
674 mFinalPath.clone(mTemp);
675 mFinalPath.transform(static_cast<LOTContentGroupItem *>(parent())->matrix());
680 LOTRectItem::LOTRectItem(LOTRectData *data)
681 : LOTPathDataItem(data->isStatic()), mData(data)
685 void LOTRectItem::updatePath(VPath& path, int frameNo)
687 VPointF pos = mData->mPos.value(frameNo);
688 VPointF size = mData->mSize.value(frameNo);
689 float roundness = mData->mRound.value(frameNo);
690 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
694 path.addRoundRect(r, roundness, roundness, mData->direction());
695 updateCache(frameNo, pos, size, roundness);
698 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
699 : LOTPathDataItem(data->isStatic()), mData(data)
703 void LOTEllipseItem::updatePath(VPath& path, int frameNo)
705 VPointF pos = mData->mPos.value(frameNo);
706 VPointF size = mData->mSize.value(frameNo);
707 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
711 path.addOval(r, mData->direction());
712 updateCache(frameNo, pos, size);
715 LOTShapeItem::LOTShapeItem(LOTShapeData *data)
716 : LOTPathDataItem(data->isStatic()), mData(data)
720 void LOTShapeItem::updatePath(VPath& path, int frameNo)
722 mData->mShape.value(frameNo).toPath(path);
725 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
726 : LOTPathDataItem(data->isStatic()), mData(data)
730 void LOTPolystarItem::updatePath(VPath& path, int frameNo)
732 VPointF pos = mData->mPos.value(frameNo);
733 float points = mData->mPointCount.value(frameNo);
734 float innerRadius = mData->mInnerRadius.value(frameNo);
735 float outerRadius = mData->mOuterRadius.value(frameNo);
736 float innerRoundness = mData->mInnerRoundness.value(frameNo);
737 float outerRoundness = mData->mOuterRoundness.value(frameNo);
738 float rotation = mData->mRotation.value(frameNo);
743 if (mData->mType == LOTPolystarData::PolyType::Star) {
744 path.addPolystar(points, innerRadius, outerRadius, innerRoundness,
745 outerRoundness, 0.0, 0.0, 0.0, mData->direction());
747 path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0,
751 m.translate(pos.x(), pos.y()).rotate(rotation);
754 updateCache(frameNo, pos, points, innerRadius, outerRadius,
755 innerRoundness, outerRoundness, rotation);
759 * PaintData Node handling
762 LOTPaintDataItem::LOTPaintDataItem(bool staticContent):mDrawable(std::make_unique<LOTDrawable>()),
763 mStaticContent(staticContent){}
765 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix,
766 float parentAlpha, const DirtyFlag &flag)
768 mRenderNodeUpdate = true;
769 mParentAlpha = parentAlpha;
773 updateContent(frameNo);
776 void LOTPaintDataItem::updateRenderNode()
779 for (auto &i : mPathItems) {
789 for (auto &i : mPathItems) {
790 mPath.addPath(i->finalPath());
792 mDrawable->setPath(mPath);
794 if (mDrawable->mFlag & VDrawable::DirtyState::Path)
795 mDrawable->mPath = mPath;
799 void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list)
801 if (mRenderNodeUpdate) {
803 LOTPaintDataItem::updateRenderNode();
804 mRenderNodeUpdate = false;
806 list.push_back(mDrawable.get());
810 void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
812 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
816 LOTFillItem::LOTFillItem(LOTFillData *data)
817 : LOTPaintDataItem(data->isStatic()), mData(data)
821 void LOTFillItem::updateContent(int frameNo)
823 LottieColor c = mData->mColor.value(frameNo);
824 float opacity = mData->opacity(frameNo);
825 mColor = c.toColor(opacity);
826 mFillRule = mData->fillRule();
829 void LOTFillItem::updateRenderNode()
831 VColor color = mColor;
833 color.setAlpha(color.a * parentAlpha());
835 mDrawable->setBrush(brush);
836 mDrawable->setFillRule(mFillRule);
839 LOTGFillItem::LOTGFillItem(LOTGFillData *data)
840 : LOTPaintDataItem(data->isStatic()), mData(data)
844 void LOTGFillItem::updateContent(int frameNo)
846 mData->update(mGradient, frameNo);
847 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
848 mFillRule = mData->fillRule();
851 void LOTGFillItem::updateRenderNode()
853 mDrawable->setBrush(VBrush(mGradient.get()));
854 mDrawable->setFillRule(mFillRule);
857 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data)
858 : LOTPaintDataItem(data->isStatic()), mData(data)
863 void LOTStrokeItem::updateContent(int frameNo)
865 LottieColor c = mData->mColor.value(frameNo);
866 float opacity = mData->opacity(frameNo);
867 mColor = c.toColor(opacity);
868 mCap = mData->capStyle();
869 mJoin = mData->joinStyle();
870 mMiterLimit = mData->meterLimit();
871 mWidth = mData->width(frameNo);
872 if (mData->hasDashInfo()) {
873 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
877 static float getScale(const VMatrix &matrix)
879 constexpr float SQRT_2 = 1.41421;
881 VPointF p2(SQRT_2, SQRT_2);
884 VPointF final = p2 - p1;
886 return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0;
889 void LOTStrokeItem::updateRenderNode()
891 VColor color = mColor;
893 color.setAlpha(color.a * parentAlpha());
895 mDrawable->setBrush(brush);
896 float scale = getScale(static_cast<LOTContentGroupItem *>(parent())->matrix());
897 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
899 if (mDashArraySize) {
900 for (int i = 0 ; i < mDashArraySize ; i++)
901 mDashArray[i] *= scale;
902 mDrawable->setDashInfo(mDashArray, mDashArraySize);
906 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data)
907 : LOTPaintDataItem(data->isStatic()), mData(data)
912 void LOTGStrokeItem::updateContent(int frameNo)
914 mData->update(mGradient, frameNo);
915 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
916 mCap = mData->capStyle();
917 mJoin = mData->joinStyle();
918 mMiterLimit = mData->meterLimit();
919 mWidth = mData->width(frameNo);
920 if (mData->hasDashInfo()) {
921 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
925 void LOTGStrokeItem::updateRenderNode()
927 float scale = getScale(mGradient->mMatrix);
928 mDrawable->setBrush(VBrush(mGradient.get()));
929 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
931 if (mDashArraySize) {
932 for (int i = 0 ; i < mDashArraySize ; i++)
933 mDashArray[i] *= scale;
934 mDrawable->setDashInfo(mDashArray, mDashArraySize);
938 LOTTrimItem::LOTTrimItem(LOTTrimData *data) : mData(data) {}
940 void LOTTrimItem::update(int frameNo, const VMatrix &/*parentMatrix*/,
941 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
945 if (mCache.mFrameNo == frameNo) return;
947 float start = mData->start(frameNo);
948 float end = mData->end(frameNo);
949 float offset = mData->offset(frameNo);
951 if (!(vCompare(mCache.mStart, start) && vCompare(mCache.mEnd, end) &&
952 vCompare(mCache.mOffset, offset))) {
954 mCache.mStart = start;
956 mCache.mOffset = offset;
958 mCache.mFrameNo = frameNo;
961 void LOTTrimItem::update()
963 // when both path and trim are not dirty
964 if (!(mDirty || pathDirty())) return;
966 //@TODO take the offset and trim type into account.
967 for (auto &i : mPathItems) {
969 pm.setStart(mCache.mStart);
970 pm.setEnd(mCache.mEnd);
971 pm.setOffset(mCache.mOffset);
972 i->updatePath(pm.trim(i->localPath()));
977 void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
979 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
983 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mData(data) {}
985 void LOTRepeaterItem::update(int /*frameNo*/, const VMatrix &/*parentMatrix*/,
986 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
990 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &/*list*/) {}
992 void LOTDrawable::sync()
994 if (!mCNode) mCNode = std::make_unique<LOTNode>();
996 mCNode->mFlag = ChangeFlagNone;
997 if (mFlag & DirtyState::None) return;
999 if (mFlag & DirtyState::Path) {
1000 const std::vector<VPath::Element> &elm = mPath.elements();
1001 const std::vector<VPointF> & pts = mPath.points();
1002 const float *ptPtr = reinterpret_cast<const float *>(pts.data());
1003 const char * elmPtr = reinterpret_cast<const char *>(elm.data());
1004 mCNode->mPath.elmPtr = elmPtr;
1005 mCNode->mPath.elmCount = elm.size();
1006 mCNode->mPath.ptPtr = ptPtr;
1007 mCNode->mPath.ptCount = 2 * pts.size();
1008 mCNode->mFlag |= ChangeFlagPath;
1011 if (mStroke.enable) {
1012 mCNode->mStroke.width = mStroke.width;
1013 mCNode->mStroke.meterLimit = mStroke.meterLimit;
1014 mCNode->mStroke.enable = 1;
1016 switch (mFillRule) {
1017 case FillRule::EvenOdd:
1018 mCNode->mFillRule = LOTFillRule::FillEvenOdd;
1021 mCNode->mFillRule = LOTFillRule::FillWinding;
1025 switch (mStroke.cap) {
1026 case CapStyle::Flat:
1027 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1029 case CapStyle::Square:
1030 mCNode->mStroke.cap = LOTCapStyle::CapSquare;
1032 case CapStyle::Round:
1033 mCNode->mStroke.cap = LOTCapStyle::CapRound;
1036 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1040 switch (mStroke.join) {
1041 case JoinStyle::Miter:
1042 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1044 case JoinStyle::Bevel:
1045 mCNode->mStroke.join = LOTJoinStyle::JoinBevel;
1047 case JoinStyle::Round:
1048 mCNode->mStroke.join = LOTJoinStyle::JoinRound;
1051 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1055 mCNode->mStroke.dashArray = mStroke.mDash.data();
1056 mCNode->mStroke.dashArraySize = mStroke.mDash.size();
1059 mCNode->mStroke.enable = 0;
1062 switch (mBrush.type()) {
1063 case VBrush::Type::Solid:
1064 mCNode->mType = LOTBrushType::BrushSolid;
1065 mCNode->mColor.r = mBrush.mColor.r;
1066 mCNode->mColor.g = mBrush.mColor.g;
1067 mCNode->mColor.b = mBrush.mColor.b;
1068 mCNode->mColor.a = mBrush.mColor.a;
1070 case VBrush::Type::LinearGradient:
1071 mCNode->mType = LOTBrushType::BrushGradient;
1072 mCNode->mGradient.type = LOTGradientType::GradientLinear;
1073 mCNode->mGradient.start.x = mBrush.mGradient->linear.x1;
1074 mCNode->mGradient.start.y = mBrush.mGradient->linear.y1;
1075 mCNode->mGradient.end.x = mBrush.mGradient->linear.x2;
1076 mCNode->mGradient.end.y = mBrush.mGradient->linear.y2;
1078 case VBrush::Type::RadialGradient:
1079 mCNode->mType = LOTBrushType::BrushGradient;
1080 mCNode->mGradient.type = LOTGradientType::GradientRadial;
1081 mCNode->mGradient.center.x = mBrush.mGradient->radial.cx;
1082 mCNode->mGradient.center.y = mBrush.mGradient->radial.cy;
1083 mCNode->mGradient.focal.x = mBrush.mGradient->radial.fx;
1084 mCNode->mGradient.focal.y = mBrush.mGradient->radial.fy;
1085 mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius;
1086 mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius;