| 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 "lottieitem.h" |
| 20 | #include <algorithm> |
| 21 | #include <cmath> |
| 22 | #include <iterator> |
| 23 | #include "lottiekeypath.h" |
| 24 | #include "vbitmap.h" |
| 25 | #include "vdasher.h" |
| 26 | #include "vpainter.h" |
| 27 | #include "vraster.h" |
| 28 |
| 29 | /* Lottie Layer Rules |
| 30 | * 1. time stretch is pre calculated and applied to all the properties of the |
| 31 | * lottilayer model and all its children |
| 32 | * 2. The frame property could be reversed using,time-reverse layer property in |
| 33 | * AE. which means (start frame > endFrame) 3. |
| 34 | */ |
| 35 |
| 36 | static bool transformProp(rlottie::Property prop) |
| 37 | { |
| 38 | switch (prop) { |
| 39 | case rlottie::Property::TrAnchor: |
| 40 | case rlottie::Property::TrScale: |
| 41 | case rlottie::Property::TrOpacity: |
| 42 | case rlottie::Property::TrPosition: |
| 43 | case rlottie::Property::TrRotation: |
| 44 | return true; |
| 45 | default: |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | static bool fillProp(rlottie::Property prop) |
| 50 | { |
| 51 | switch (prop) { |
| 52 | case rlottie::Property::Color: |
| 53 | case rlottie::Property::FillOpacity: |
| 54 | return true; |
| 55 | default: |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 |
| 60 | static bool strokeProp(rlottie::Property prop) |
| 61 | { |
| 62 | switch (prop) { |
| 63 | case rlottie::Property::Color: |
| 64 | case rlottie::Property::StrokeOpacity: |
| 65 | case rlottie::Property::StrokeWidth: |
| 66 | return true; |
| 67 | default: |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 |
| 72 | LOTCompItem::LOTCompItem(LOTModel *model) |
| 73 | : mUpdateViewBox(false), mCurFrameNo(-1) |
| 74 | { |
| 75 | mCompData = model->mRoot.get(); |
| 76 | mRootLayer = createLayerItem(mCompData->mRootLayer.get()); |
| 77 | mRootLayer->setComplexContent(false); |
| 78 | mViewSize = mCompData->size(); |
| 79 | } |
| 80 |
| 81 | void LOTCompItem::setValue(const std::string &keypath, LOTVariant &value) |
| 82 | { |
| 83 | LOTKeyPath key(keypath); |
| 84 | mRootLayer->resolveKeyPath(key, 0, value); |
| 85 | mCurFrameNo = -1; |
| 86 | } |
| 87 |
| 88 | std::unique_ptr<LOTLayerItem> LOTCompItem::createLayerItem( |
| 89 | LOTLayerData *layerData) |
| 90 | { |
| 91 | switch (layerData->mLayerType) { |
| 92 | case LayerType::Precomp: { |
| 93 | return std::make_unique<LOTCompLayerItem>(layerData); |
| 94 | } |
| 95 | case LayerType::Solid: { |
| 96 | return std::make_unique<LOTSolidLayerItem>(layerData); |
| 97 | } |
| 98 | case LayerType::Shape: { |
| 99 | return std::make_unique<LOTShapeLayerItem>(layerData); |
| 100 | } |
| 101 | case LayerType::Null: { |
| 102 | return std::make_unique<LOTNullLayerItem>(layerData); |
| 103 | } |
| 104 | case LayerType::Image: { |
| 105 | return std::make_unique<LOTImageLayerItem>(layerData); |
| 106 | } |
| 107 | default: |
| 108 | return nullptr; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 |
| 113 | void LOTCompItem::resize(const VSize &size) |
| 114 | { |
| 115 | if (mViewSize == size) return; |
| 116 | mViewSize = size; |
| 117 | mUpdateViewBox = true; |
| 118 | } |
| 119 |
| 120 | VSize LOTCompItem::size() const |
| 121 | { |
| 122 | return mViewSize; |
| 123 | } |
| 124 |
| 125 | bool LOTCompItem::update(int frameNo) |
| 126 | { |
| 127 | // check if cached frame is same as requested frame. |
| 128 | if (!mUpdateViewBox && (mCurFrameNo == frameNo)) return false; |
| 129 |
| 130 | /* |
| 131 | * if viewbox dosen't scale exactly to the viewport |
| 132 | * we scale the viewbox keeping AspectRatioPreserved and then align the |
| 133 | * viewbox to the viewport using AlignCenter rule. |
| 134 | */ |
| 135 | VSize viewPort = mViewSize; |
| 136 | VSize viewBox = mCompData->size(); |
| 137 |
| 138 | float sx = float(viewPort.width()) / viewBox.width(); |
| 139 | float sy = float(viewPort.height()) / viewBox.height(); |
| 140 | float scale = fmin(sx, sy); |
| 141 | float tx = (viewPort.width() - viewBox.width() * scale) * 0.5; |
| 142 | float ty = (viewPort.height() - viewBox.height() * scale) * 0.5; |
| 143 |
| 144 | VMatrix m; |
| 145 | m.translate(tx, ty).scale(scale, scale); |
| 146 | mRootLayer->update(frameNo, m, 1.0); |
| 147 |
| 148 | mCurFrameNo = frameNo; |
| 149 | mUpdateViewBox = false; |
| 150 | return true; |
| 151 | } |
| 152 |
| 153 | void LOTCompItem::buildRenderTree() |
| 154 | { |
| 155 | mRootLayer->buildLayerNode(); |
| 156 | } |
| 157 |
| 158 | const LOTLayerNode *LOTCompItem::renderTree() const |
| 159 | { |
| 160 | return mRootLayer->layerNode(); |
| 161 | } |
| 162 |
| 163 | bool LOTCompItem::render(const rlottie::Surface &surface) |
| 164 | { |
| 165 | VBitmap bitmap(reinterpret_cast<uchar *>(surface.buffer()), surface.width(), |
| 166 | surface.height(), surface.bytesPerLine(), |
| 167 | VBitmap::Format::ARGB32); |
| 168 |
| 169 | /* schedule all preprocess task for this frame at once. |
| 170 | */ |
| 171 | mDrawableList.clear(); |
| 172 | mRootLayer->renderList(mDrawableList); |
| 173 | VRect clip(0, 0, surface.drawRegionWidth(), surface.drawRegionHeight()); |
| 174 | for (auto &e : mDrawableList) { |
| 175 | e->preprocess(clip); |
| 176 | } |
| 177 |
| 178 | VPainter painter(&bitmap); |
| 179 | // set sub surface area for drawing. |
| 180 | painter.setDrawRegion( |
| 181 | VRect(surface.drawRegionPosX(), surface.drawRegionPosY(), |
| 182 | surface.drawRegionWidth(), surface.drawRegionHeight())); |
| 183 | mRootLayer->render(&painter, {}, {}); |
| 184 |
| 185 | return true; |
| 186 | } |
| 187 |
| 188 | void LOTMaskItem::update(int frameNo, const VMatrix & parentMatrix, |
| 189 | float /*parentAlpha*/, const DirtyFlag &flag) |
| 190 | { |
| 191 | if (flag.testFlag(DirtyFlagBit::None) && mData->isStatic()) return; |
| 192 |
| 193 | if (mData->mShape.isStatic()) { |
| 194 | if (mLocalPath.empty()) { |
| 195 | mData->mShape.value(frameNo).toPath(mLocalPath); |
| 196 | } |
| 197 | } else { |
| 198 | mData->mShape.value(frameNo).toPath(mLocalPath); |
| 199 | } |
| 200 | /* mask item dosen't inherit opacity */ |
| 201 | mCombinedAlpha = mData->opacity(frameNo); |
| 202 |
| 203 | mFinalPath.clone(mLocalPath); |
| 204 | mFinalPath.transform(parentMatrix); |
| 205 |
| 206 | mRasterizer.rasterize(mFinalPath); |
| 207 | mRasterRequest = true; |
| 208 | } |
| 209 |
| 210 | VRle LOTMaskItem::rle() |
| 211 | { |
| 212 | if (mRasterRequest) { |
| 213 | mRasterRequest = false; |
| 214 | if (!vCompare(mCombinedAlpha, 1.0f)) |
| 215 | mRasterizer.rle() *= (mCombinedAlpha * 255); |
| 216 | if (mData->mInv) mRasterizer.rle().invert(); |
| 217 | } |
| 218 | return mRasterizer.rle(); |
| 219 | } |
| 220 |
| 221 | void LOTLayerItem::buildLayerNode() |
| 222 | { |
| 223 | if (!mLayerCNode) { |
| 224 | mLayerCNode = std::make_unique<LOTLayerNode>(); |
| 225 | mLayerCNode->mMaskList.ptr = nullptr; |
| 226 | mLayerCNode->mMaskList.size = 0; |
| 227 | mLayerCNode->mLayerList.ptr = nullptr; |
| 228 | mLayerCNode->mLayerList.size = 0; |
| 229 | mLayerCNode->mNodeList.ptr = nullptr; |
| 230 | mLayerCNode->mNodeList.size = 0; |
| 231 | mLayerCNode->mMatte = MatteNone; |
| 232 | mLayerCNode->mVisible = 0; |
| 233 | mLayerCNode->mAlpha = 255; |
| 234 | mLayerCNode->mClipPath.ptPtr = nullptr; |
| 235 | mLayerCNode->mClipPath.elmPtr = nullptr; |
| 236 | mLayerCNode->mClipPath.ptCount = 0; |
| 237 | mLayerCNode->mClipPath.elmCount = 0; |
| 238 | mLayerCNode->name = name().c_str(); |
| 239 | } |
| 240 | if (complexContent()) mLayerCNode->mAlpha = combinedAlpha() * 255; |
| 241 | mLayerCNode->mVisible = visible(); |
| 242 | // update matte |
| 243 | if (hasMatte()) { |
| 244 | switch (mLayerData->mMatteType) { |
| 245 | case MatteType::Alpha: |
| 246 | mLayerCNode->mMatte = MatteAlpha; |
| 247 | break; |
| 248 | case MatteType::AlphaInv: |
| 249 | mLayerCNode->mMatte = MatteAlphaInv; |
| 250 | break; |
| 251 | case MatteType::Luma: |
| 252 | mLayerCNode->mMatte = MatteLuma; |
| 253 | break; |
| 254 | case MatteType::LumaInv: |
| 255 | mLayerCNode->mMatte = MatteLumaInv; |
| 256 | break; |
| 257 | default: |
| 258 | mLayerCNode->mMatte = MatteNone; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | if (mLayerMask) { |
| 263 | mMasksCNode.clear(); |
| 264 | mMasksCNode.resize(mLayerMask->mMasks.size()); |
| 265 | size_t i = 0; |
| 266 | for (const auto &mask : mLayerMask->mMasks) { |
| 267 | LOTMask * cNode = &mMasksCNode[i++]; |
| 268 | const std::vector<VPath::Element> &elm = mask.mFinalPath.elements(); |
| 269 | const std::vector<VPointF> & pts = mask.mFinalPath.points(); |
| 270 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 271 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 272 | cNode->mPath.ptPtr = ptPtr; |
| 273 | cNode->mPath.ptCount = pts.size(); |
| 274 | cNode->mPath.elmPtr = elmPtr; |
| 275 | cNode->mPath.elmCount = elm.size(); |
| 276 | cNode->mAlpha = mask.mCombinedAlpha * 255; |
| 277 | switch (mask.maskMode()) { |
| 278 | case LOTMaskData::Mode::Add: |
| 279 | cNode->mMode = MaskAdd; |
| 280 | break; |
| 281 | case LOTMaskData::Mode::Substarct: |
| 282 | cNode->mMode = MaskSubstract; |
| 283 | break; |
| 284 | case LOTMaskData::Mode::Intersect: |
| 285 | cNode->mMode = MaskIntersect; |
| 286 | break; |
| 287 | case LOTMaskData::Mode::Difference: |
| 288 | cNode->mMode = MaskDifference; |
| 289 | break; |
| 290 | default: |
| 291 | cNode->mMode = MaskAdd; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | mLayerCNode->mMaskList.ptr = mMasksCNode.data(); |
| 296 | mLayerCNode->mMaskList.size = mMasksCNode.size(); |
| 297 | } |
| 298 | } |
| 299 |
| 300 | void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask, |
| 301 | const VRle &matteRle) |
| 302 | { |
| 303 | mDrawableList.clear(); |
| 304 | renderList(mDrawableList); |
| 305 |
| 306 | VRle mask; |
| 307 | if (mLayerMask) { |
| 308 | mask = mLayerMask->maskRle(painter->clipBoundingRect()); |
| 309 | if (!inheritMask.empty()) mask = mask & inheritMask; |
| 310 | // if resulting mask is empty then return. |
| 311 | if (mask.empty()) return; |
| 312 | } else { |
| 313 | mask = inheritMask; |
| 314 | } |
| 315 |
| 316 | for (auto &i : mDrawableList) { |
| 317 | painter->setBrush(i->mBrush); |
| 318 | VRle rle = i->rle(); |
| 319 | if (matteRle.empty()) { |
| 320 | if (mask.empty()) { |
| 321 | // no mask no matte |
| 322 | painter->drawRle(VPoint(), rle); |
| 323 | } else { |
| 324 | // only mask |
| 325 | painter->drawRle(rle, mask); |
| 326 | } |
| 327 |
| 328 | } else { |
| 329 | if (!mask.empty()) rle = rle & mask; |
| 330 |
| 331 | if (rle.empty()) continue; |
| 332 | if (matteType() == MatteType::AlphaInv) { |
| 333 | rle = rle - matteRle; |
| 334 | painter->drawRle(VPoint(), rle); |
| 335 | } else { |
| 336 | // render with matteRle as clip. |
| 337 | painter->drawRle(rle, matteRle); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 |
| 343 | LOTLayerMaskItem::LOTLayerMaskItem(LOTLayerData *layerData) |
| 344 | { |
| 345 | mMasks.reserve(layerData->mMasks.size()); |
| 346 |
| 347 | for (auto &i : layerData->mMasks) { |
| 348 | mMasks.emplace_back(i.get()); |
| 349 | mStatic &= i->isStatic(); |
| 350 | } |
| 351 | } |
| 352 |
| 353 | void LOTLayerMaskItem::update(int frameNo, const VMatrix &parentMatrix, |
| 354 | float parentAlpha, const DirtyFlag &flag) |
| 355 | { |
| 356 | if (flag.testFlag(DirtyFlagBit::None) && isStatic()) return; |
| 357 |
| 358 | for (auto &i : mMasks) { |
| 359 | i.update(frameNo, parentMatrix, parentAlpha, flag); |
| 360 | } |
| 361 | mDirty = true; |
| 362 | } |
| 363 |
| 364 | VRle LOTLayerMaskItem::maskRle(const VRect &clipRect) |
| 365 | { |
| 366 | if (!mDirty) return mRle; |
| 367 |
| 368 | VRle rle; |
| 369 | for (auto &i : mMasks) { |
| 370 | switch (i.maskMode()) { |
| 371 | case LOTMaskData::Mode::Add: { |
| 372 | rle = rle + i.rle(); |
| 373 | break; |
| 374 | } |
| 375 | case LOTMaskData::Mode::Substarct: { |
| 376 | if (rle.empty() && !clipRect.empty()) rle = VRle::toRle(clipRect); |
| 377 | rle = rle - i.rle(); |
| 378 | break; |
| 379 | } |
| 380 | case LOTMaskData::Mode::Intersect: { |
| 381 | if (rle.empty() && !clipRect.empty()) rle = VRle::toRle(clipRect); |
| 382 | rle = rle & i.rle(); |
| 383 | break; |
| 384 | } |
| 385 | case LOTMaskData::Mode::Difference: { |
| 386 | rle = rle ^ i.rle(); |
| 387 | break; |
| 388 | } |
| 389 | default: |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 |
| 394 | if (!rle.empty() && !rle.unique()) { |
| 395 | mRle.clone(rle); |
| 396 | } else { |
| 397 | mRle = rle; |
| 398 | } |
| 399 | mDirty = false; |
| 400 | return mRle; |
| 401 | } |
| 402 |
| 403 | LOTLayerItem::LOTLayerItem(LOTLayerData *layerData) : mLayerData(layerData) |
| 404 | { |
| 405 | if (mLayerData->mHasMask) |
| 406 | mLayerMask = std::make_unique<LOTLayerMaskItem>(mLayerData); |
| 407 | } |
| 408 |
| 409 | bool LOTLayerItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 410 | LOTVariant &value) |
| 411 | { |
| 412 | if (!keyPath.matches(name(), depth)) { |
| 413 | return false; |
| 414 | } |
| 415 |
| 416 | if (!keyPath.skip(name())) { |
| 417 | if (keyPath.fullyResolvesTo(name(), depth) && |
| 418 | transformProp(value.property())) { |
| 419 | //@TODO handle propery update. |
| 420 | } |
| 421 | } |
| 422 | return true; |
| 423 | } |
| 424 |
| 425 | bool LOTShapeLayerItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 426 | LOTVariant &value) |
| 427 | { |
| 428 | if (LOTLayerItem::resolveKeyPath(keyPath, depth, value)) { |
| 429 | if (keyPath.propagate(name(), depth)) { |
| 430 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 431 | mRoot->resolveKeyPath(keyPath, newDepth, value); |
| 432 | } |
| 433 | return true; |
| 434 | } |
| 435 | return false; |
| 436 | } |
| 437 |
| 438 | bool LOTCompLayerItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 439 | LOTVariant &value) |
| 440 | { |
| 441 | if (LOTLayerItem::resolveKeyPath(keyPath, depth, value)) { |
| 442 | if (keyPath.propagate(name(), depth)) { |
| 443 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 444 | for (const auto &layer : mLayers) { |
| 445 | layer->resolveKeyPath(keyPath, newDepth, value); |
| 446 | } |
| 447 | } |
| 448 | return true; |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 |
| 453 | void LOTLayerItem::update(int frameNumber, const VMatrix &parentMatrix, |
| 454 | float parentAlpha) |
| 455 | { |
| 456 | mFrameNo = frameNumber; |
| 457 | // 1. check if the layer is part of the current frame |
| 458 | if (!visible()) return; |
| 459 |
| 460 | float alpha = parentAlpha * opacity(frameNo()); |
| 461 | if (vIsZero(alpha)) { |
| 462 | mCombinedAlpha = 0; |
| 463 | return; |
| 464 | } |
| 465 |
| 466 | // 2. calculate the parent matrix and alpha |
| 467 | VMatrix m = matrix(frameNo()); |
| 468 | m *= parentMatrix; |
| 469 |
| 470 | // 3. update the dirty flag based on the change |
| 471 | if (!mCombinedMatrix.fuzzyCompare(m)) { |
| 472 | mDirtyFlag |= DirtyFlagBit::Matrix; |
| 473 | } |
| 474 | if (!vCompare(mCombinedAlpha, alpha)) { |
| 475 | mDirtyFlag |= DirtyFlagBit::Alpha; |
| 476 | } |
| 477 | mCombinedMatrix = m; |
| 478 | mCombinedAlpha = alpha; |
| 479 |
| 480 | // 4. update the mask |
| 481 | if (mLayerMask) { |
| 482 | mLayerMask->update(frameNo(), m, alpha, mDirtyFlag); |
| 483 | } |
| 484 |
| 485 | // 5. if no parent property change and layer is static then nothing to do. |
| 486 | if (!mLayerData->precompLayer() && flag().testFlag(DirtyFlagBit::None) && |
| 487 | isStatic()) |
| 488 | return; |
| 489 |
| 490 | // 6. update the content of the layer |
| 491 | updateContent(); |
| 492 |
| 493 | // 7. reset the dirty flag |
| 494 | mDirtyFlag = DirtyFlagBit::None; |
| 495 | } |
| 496 |
| 497 | VMatrix LOTLayerItem::matrix(int frameNo) const |
| 498 | { |
| 499 | return mParentLayer |
| 500 | ? (mLayerData->matrix(frameNo) * mParentLayer->matrix(frameNo)) |
| 501 | : mLayerData->matrix(frameNo); |
| 502 | } |
| 503 |
| 504 | bool LOTLayerItem::visible() const |
| 505 | { |
| 506 | if (frameNo() >= mLayerData->inFrame() && |
| 507 | frameNo() < mLayerData->outFrame()) |
| 508 | return true; |
| 509 | else |
| 510 | return false; |
| 511 | } |
| 512 |
| 513 | LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel) |
| 514 | : LOTLayerItem(layerModel) |
| 515 | { |
| 516 | // 1. create layer item |
| 517 | for (auto &i : mLayerData->mChildren) { |
| 518 | LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get()); |
| 519 | auto layerItem = LOTCompItem::createLayerItem(layerModel); |
| 520 | if (layerItem) mLayers.push_back(std::move(layerItem)); |
| 521 | } |
| 522 |
| 523 | // 2. update parent layer |
| 524 | for (const auto &layer : mLayers) { |
| 525 | int id = layer->parentId(); |
| 526 | if (id >= 0) { |
| 527 | auto search = |
| 528 | std::find_if(mLayers.begin(), mLayers.end(), |
| 529 | [id](const auto &val) { return val->id() == id; }); |
| 530 | if (search != mLayers.end()) layer->setParentLayer((*search).get()); |
| 531 | } |
| 532 | } |
| 533 |
| 534 | // 3. keep the layer in back-to-front order. |
| 535 | // as lottie model keeps the data in front-toback-order. |
| 536 | std::reverse(mLayers.begin(), mLayers.end()); |
| 537 |
| 538 | // 4. check if its a nested composition |
| 539 | if (!layerModel->layerSize().empty()) { |
| 540 | mClipper = std::make_unique<LOTClipperItem>(layerModel->layerSize()); |
| 541 | } |
| 542 |
| 543 | if (mLayers.size() > 1) setComplexContent(true); |
| 544 | } |
| 545 |
| 546 | void LOTCompLayerItem::buildLayerNode() |
| 547 | { |
| 548 | LOTLayerItem::buildLayerNode(); |
| 549 | if (mClipper) { |
| 550 | const std::vector<VPath::Element> &elm = mClipper->mPath.elements(); |
| 551 | const std::vector<VPointF> & pts = mClipper->mPath.points(); |
| 552 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 553 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 554 | layerNode()->mClipPath.ptPtr = ptPtr; |
| 555 | layerNode()->mClipPath.elmPtr = elmPtr; |
| 556 | layerNode()->mClipPath.ptCount = 2 * pts.size(); |
| 557 | layerNode()->mClipPath.elmCount = elm.size(); |
| 558 | } |
| 559 | if (mLayers.size() != mLayersCNode.size()) { |
| 560 | for (const auto &layer : mLayers) { |
| 561 | layer->buildLayerNode(); |
| 562 | mLayersCNode.push_back(layer->layerNode()); |
| 563 | } |
| 564 | layerNode()->mLayerList.ptr = mLayersCNode.data(); |
| 565 | layerNode()->mLayerList.size = mLayersCNode.size(); |
| 566 | } else { |
| 567 | for (const auto &layer : mLayers) { |
| 568 | layer->buildLayerNode(); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 |
| 573 | void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, |
| 574 | const VRle &matteRle) |
| 575 | { |
| 576 | if (vIsZero(combinedAlpha())) return; |
| 577 |
| 578 | if (vCompare(combinedAlpha(), 1.0)) { |
| 579 | renderHelper(painter, inheritMask, matteRle); |
| 580 | } else { |
| 581 | if (complexContent()) { |
| 582 | VSize size = painter->clipBoundingRect().size(); |
| 583 | VPainter srcPainter; |
| 584 | VBitmap srcBitmap(size.width(), size.height(), |
| 585 | VBitmap::Format::ARGB32); |
| 586 | srcPainter.begin(&srcBitmap); |
| 587 | renderHelper(&srcPainter, inheritMask, matteRle); |
| 588 | srcPainter.end(); |
| 589 | painter->drawBitmap(VPoint(), srcBitmap, combinedAlpha() * 255); |
| 590 | } else { |
| 591 | renderHelper(painter, inheritMask, matteRle); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 |
| 596 | void LOTCompLayerItem::renderHelper(VPainter *painter, const VRle &inheritMask, |
| 597 | const VRle &matteRle) |
| 598 | { |
| 599 | VRle mask; |
| 600 | if (mLayerMask) { |
| 601 | mask = mLayerMask->maskRle(painter->clipBoundingRect()); |
| 602 | if (!inheritMask.empty()) mask = mask & inheritMask; |
| 603 | // if resulting mask is empty then return. |
| 604 | if (mask.empty()) return; |
| 605 | } else { |
| 606 | mask = inheritMask; |
| 607 | } |
| 608 |
| 609 | if (mClipper) { |
| 610 | if (mask.empty()) { |
| 611 | mask = mClipper->rle(); |
| 612 | } else { |
| 613 | mask = mClipper->rle() & mask; |
| 614 | } |
| 615 | } |
| 616 |
| 617 | LOTLayerItem *matte = nullptr; |
| 618 | for (const auto &layer : mLayers) { |
| 619 | if (layer->hasMatte()) { |
| 620 | matte = layer.get(); |
| 621 | } else { |
| 622 | if (layer->visible()) { |
| 623 | if (matte) { |
| 624 | if (matte->visible()) |
| 625 | renderMatteLayer(painter, mask, matteRle, matte, |
| 626 | layer.get()); |
| 627 | } else { |
| 628 | layer->render(painter, mask, matteRle); |
| 629 | } |
| 630 | } |
| 631 | matte = nullptr; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 |
| 636 | void LOTCompLayerItem::renderMatteLayer(VPainter *painter, const VRle &mask, |
| 637 | const VRle & matteRle, |
| 638 | LOTLayerItem *layer, LOTLayerItem *src) |
| 639 | { |
| 640 | VSize size = painter->clipBoundingRect().size(); |
| 641 | // Decide if we can use fast matte. |
| 642 | // 1. draw src layer to matte buffer |
| 643 | VPainter srcPainter; |
| 644 | src->bitmap().reset(size.width(), size.height(), |
| 645 | VBitmap::Format::ARGB32); |
| 646 | srcPainter.begin(&src->bitmap()); |
| 647 | src->render(&srcPainter, mask, matteRle); |
| 648 | srcPainter.end(); |
| 649 |
| 650 | // 2. draw layer to layer buffer |
| 651 | VPainter layerPainter; |
| 652 | layer->bitmap().reset(size.width(), size.height(), |
| 653 | VBitmap::Format::ARGB32); |
| 654 | layerPainter.begin(&layer->bitmap()); |
| 655 | layer->render(&layerPainter, mask, matteRle); |
| 656 |
| 657 | // 2.1update composition mode |
| 658 | switch (layer->matteType()) { |
| 659 | case MatteType::Alpha: |
| 660 | case MatteType::Luma: { |
| 661 | layerPainter.setCompositionMode( |
| 662 | VPainter::CompositionMode::CompModeDestIn); |
| 663 | break; |
| 664 | } |
| 665 | case MatteType::AlphaInv: |
| 666 | case MatteType::LumaInv: { |
| 667 | layerPainter.setCompositionMode( |
| 668 | VPainter::CompositionMode::CompModeDestOut); |
| 669 | break; |
| 670 | } |
| 671 | default: |
| 672 | break; |
| 673 | } |
| 674 |
| 675 | // 2.2 update srcBuffer if the matte is luma type |
| 676 | if (layer->matteType() == MatteType::Luma || |
| 677 | layer->matteType() == MatteType::LumaInv) { |
| 678 | src->bitmap().updateLuma(); |
| 679 | } |
| 680 |
| 681 | // 2.3 draw src buffer as mask |
| 682 | layerPainter.drawBitmap(VPoint(), src->bitmap()); |
| 683 | layerPainter.end(); |
| 684 | // 3. draw the result buffer into painter |
| 685 | painter->drawBitmap(VPoint(), layer->bitmap()); |
| 686 | } |
| 687 |
| 688 | void LOTClipperItem::update(const VMatrix &matrix) |
| 689 | { |
| 690 | mPath.reset(); |
| 691 | mPath.addRect(VRectF(0, 0, mSize.width(), mSize.height())); |
| 692 | mPath.transform(matrix); |
| 693 | mRasterizer.rasterize(mPath); |
| 694 | } |
| 695 |
| 696 | VRle LOTClipperItem::rle() |
| 697 | { |
| 698 | return mRasterizer.rle(); |
| 699 | } |
| 700 |
| 701 | void LOTCompLayerItem::updateContent() |
| 702 | { |
| 703 | if (mClipper && flag().testFlag(DirtyFlagBit::Matrix)) { |
| 704 | mClipper->update(combinedMatrix()); |
| 705 | } |
| 706 | int mappedFrame = mLayerData->timeRemap(frameNo()); |
| 707 | float alpha = combinedAlpha(); |
| 708 | if (complexContent()) alpha = 1; |
| 709 | for (const auto &layer : mLayers) { |
| 710 | layer->update(mappedFrame, combinedMatrix(), alpha); |
| 711 | } |
| 712 | } |
| 713 |
| 714 | void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list) |
| 715 | { |
| 716 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 717 |
| 718 | LOTLayerItem *matte = nullptr; |
| 719 | for (const auto &layer : mLayers) { |
| 720 | if (layer->hasMatte()) { |
| 721 | matte = layer.get(); |
| 722 | } else { |
| 723 | if (layer->visible()) { |
| 724 | if (matte) { |
| 725 | if (matte->visible()) { |
| 726 | layer->renderList(list); |
| 727 | matte->renderList(list); |
| 728 | } |
| 729 | } else { |
| 730 | layer->renderList(list); |
| 731 | } |
| 732 | } |
| 733 | matte = nullptr; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 |
| 738 | LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData) |
| 739 | : LOTLayerItem(layerData) |
| 740 | { |
| 741 | } |
| 742 |
| 743 | void LOTSolidLayerItem::updateContent() |
| 744 | { |
| 745 | if (flag() & DirtyFlagBit::Matrix) { |
| 746 | VPath path; |
| 747 | path.addRect( |
| 748 | VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight())); |
| 749 | path.transform(combinedMatrix()); |
| 750 | mRenderNode.mFlag |= VDrawable::DirtyState::Path; |
| 751 | mRenderNode.mPath = path; |
| 752 | } |
| 753 | if (flag() & DirtyFlagBit::Alpha) { |
| 754 | LottieColor color = mLayerData->solidColor(); |
| 755 | VBrush brush(color.toColor(combinedAlpha())); |
| 756 | mRenderNode.setBrush(brush); |
| 757 | mRenderNode.mFlag |= VDrawable::DirtyState::Brush; |
| 758 | } |
| 759 | } |
| 760 |
| 761 | void LOTSolidLayerItem::buildLayerNode() |
| 762 | { |
| 763 | LOTLayerItem::buildLayerNode(); |
| 764 |
| 765 | mDrawableList.clear(); |
| 766 | renderList(mDrawableList); |
| 767 |
| 768 | mCNodeList.clear(); |
| 769 | for (auto &i : mDrawableList) { |
| 770 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 771 | lotDrawable->sync(); |
| 772 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 773 | } |
| 774 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 775 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 776 | } |
| 777 |
| 778 | void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list) |
| 779 | { |
| 780 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 781 |
| 782 | list.push_back(&mRenderNode); |
| 783 | } |
| 784 |
| 785 | LOTImageLayerItem::LOTImageLayerItem(LOTLayerData *layerData) |
| 786 | : LOTLayerItem(layerData) |
| 787 | { |
| 788 | VBrush brush(mLayerData->mAsset->bitmap()); |
| 789 | mRenderNode.setBrush(brush); |
| 790 | } |
| 791 |
| 792 | void LOTImageLayerItem::updateContent() |
| 793 | { |
| 794 | if (flag() & DirtyFlagBit::Matrix) { |
| 795 | VPath path; |
| 796 | path.addRect(VRectF(0, 0, mLayerData->mAsset->mWidth, |
| 797 | mLayerData->mAsset->mHeight)); |
| 798 | path.transform(combinedMatrix()); |
| 799 | mRenderNode.mFlag |= VDrawable::DirtyState::Path; |
| 800 | mRenderNode.mPath = path; |
| 801 | mRenderNode.mBrush.setMatrix(combinedMatrix()); |
| 802 | } |
| 803 |
| 804 | if (flag() & DirtyFlagBit::Alpha) { |
| 805 | //@TODO handle alpha with the image. |
| 806 | } |
| 807 | } |
| 808 |
| 809 | void LOTImageLayerItem::renderList(std::vector<VDrawable *> &list) |
| 810 | { |
| 811 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 812 |
| 813 | list.push_back(&mRenderNode); |
| 814 | } |
| 815 |
| 816 | void LOTImageLayerItem::buildLayerNode() |
| 817 | { |
| 818 | LOTLayerItem::buildLayerNode(); |
| 819 |
| 820 | mDrawableList.clear(); |
| 821 | renderList(mDrawableList); |
| 822 |
| 823 | mCNodeList.clear(); |
| 824 | for (auto &i : mDrawableList) { |
| 825 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 826 | lotDrawable->sync(); |
| 827 |
| 828 | lotDrawable->mCNode->mImageInfo.data = |
| 829 | lotDrawable->mBrush.mTexture.data(); |
| 830 | lotDrawable->mCNode->mImageInfo.width = |
| 831 | lotDrawable->mBrush.mTexture.width(); |
| 832 | lotDrawable->mCNode->mImageInfo.height = |
| 833 | lotDrawable->mBrush.mTexture.height(); |
| 834 |
| 835 | lotDrawable->mCNode->mImageInfo.mMatrix.m11 = combinedMatrix().m_11(); |
| 836 | lotDrawable->mCNode->mImageInfo.mMatrix.m12 = combinedMatrix().m_12(); |
| 837 | lotDrawable->mCNode->mImageInfo.mMatrix.m13 = combinedMatrix().m_13(); |
| 838 |
| 839 | lotDrawable->mCNode->mImageInfo.mMatrix.m21 = combinedMatrix().m_21(); |
| 840 | lotDrawable->mCNode->mImageInfo.mMatrix.m22 = combinedMatrix().m_22(); |
| 841 | lotDrawable->mCNode->mImageInfo.mMatrix.m23 = combinedMatrix().m_23(); |
| 842 |
| 843 | lotDrawable->mCNode->mImageInfo.mMatrix.m31 = combinedMatrix().m_tx(); |
| 844 | lotDrawable->mCNode->mImageInfo.mMatrix.m32 = combinedMatrix().m_ty(); |
| 845 | lotDrawable->mCNode->mImageInfo.mMatrix.m33 = combinedMatrix().m_33(); |
| 846 |
| 847 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 848 | } |
| 849 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 850 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 851 | } |
| 852 |
| 853 | LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData) |
| 854 | : LOTLayerItem(layerData) |
| 855 | { |
| 856 | } |
| 857 | void LOTNullLayerItem::updateContent() {} |
| 858 |
| 859 | LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData) |
| 860 | : LOTLayerItem(layerData), |
| 861 | mRoot(std::make_unique<LOTContentGroupItem>(nullptr)) |
| 862 | { |
| 863 | mRoot->addChildren(layerData); |
| 864 |
| 865 | std::vector<LOTPathDataItem *> list; |
| 866 | mRoot->processPaintItems(list); |
| 867 |
| 868 | if (layerData->hasPathOperator()) { |
| 869 | list.clear(); |
| 870 | mRoot->processTrimItems(list); |
| 871 | } |
| 872 | } |
| 873 |
| 874 | std::unique_ptr<LOTContentItem> LOTShapeLayerItem::createContentItem( |
| 875 | LOTData *contentData) |
| 876 | { |
| 877 | switch (contentData->type()) { |
| 878 | case LOTData::Type::ShapeGroup: { |
| 879 | return std::make_unique<LOTContentGroupItem>( |
| 880 | static_cast<LOTGroupData *>(contentData)); |
| 881 | } |
| 882 | case LOTData::Type::Rect: { |
| 883 | return std::make_unique<LOTRectItem>( |
| 884 | static_cast<LOTRectData *>(contentData)); |
| 885 | } |
| 886 | case LOTData::Type::Ellipse: { |
| 887 | return std::make_unique<LOTEllipseItem>( |
| 888 | static_cast<LOTEllipseData *>(contentData)); |
| 889 | } |
| 890 | case LOTData::Type::Shape: { |
| 891 | return std::make_unique<LOTShapeItem>( |
| 892 | static_cast<LOTShapeData *>(contentData)); |
| 893 | } |
| 894 | case LOTData::Type::Polystar: { |
| 895 | return std::make_unique<LOTPolystarItem>( |
| 896 | static_cast<LOTPolystarData *>(contentData)); |
| 897 | } |
| 898 | case LOTData::Type::Fill: { |
| 899 | return std::make_unique<LOTFillItem>( |
| 900 | static_cast<LOTFillData *>(contentData)); |
| 901 | } |
| 902 | case LOTData::Type::GFill: { |
| 903 | return std::make_unique<LOTGFillItem>( |
| 904 | static_cast<LOTGFillData *>(contentData)); |
| 905 | } |
| 906 | case LOTData::Type::Stroke: { |
| 907 | return std::make_unique<LOTStrokeItem>( |
| 908 | static_cast<LOTStrokeData *>(contentData)); |
| 909 | } |
| 910 | case LOTData::Type::GStroke: { |
| 911 | return std::make_unique<LOTGStrokeItem>( |
| 912 | static_cast<LOTGStrokeData *>(contentData)); |
| 913 | } |
| 914 | case LOTData::Type::Repeater: { |
| 915 | return std::make_unique<LOTRepeaterItem>( |
| 916 | static_cast<LOTRepeaterData *>(contentData)); |
| 917 | } |
| 918 | case LOTData::Type::Trim: { |
| 919 | return std::make_unique<LOTTrimItem>( |
| 920 | static_cast<LOTTrimData *>(contentData)); |
| 921 | } |
| 922 | default: |
| 923 | return nullptr; |
| 924 | break; |
| 925 | } |
| 926 | } |
| 927 |
| 928 | void LOTShapeLayerItem::updateContent() |
| 929 | { |
| 930 | mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag()); |
| 931 |
| 932 | if (mLayerData->hasPathOperator()) { |
| 933 | mRoot->applyTrim(); |
| 934 | } |
| 935 | } |
| 936 |
| 937 | void LOTShapeLayerItem::buildLayerNode() |
| 938 | { |
| 939 | LOTLayerItem::buildLayerNode(); |
| 940 |
| 941 | mDrawableList.clear(); |
| 942 | renderList(mDrawableList); |
| 943 |
| 944 | mCNodeList.clear(); |
| 945 | for (auto &i : mDrawableList) { |
| 946 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 947 | lotDrawable->sync(); |
| 948 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 949 | } |
| 950 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 951 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 952 | } |
| 953 |
| 954 | void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list) |
| 955 | { |
| 956 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 957 | mRoot->renderList(list); |
| 958 | } |
| 959 |
| 960 | bool LOTContentGroupItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 961 | LOTVariant &value) |
| 962 | { |
| 963 | if (!keyPath.matches(name(), depth)) { |
| 964 | return false; |
| 965 | } |
| 966 |
| 967 | if (!keyPath.skip(name())) { |
| 968 | if (keyPath.fullyResolvesTo(name(), depth) && |
| 969 | transformProp(value.property())) { |
| 970 | //@TODO handle property update |
| 971 | } |
| 972 | } |
| 973 |
| 974 | if (keyPath.propagate(name(), depth)) { |
| 975 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 976 | for (auto &child : mContents) { |
| 977 | child->resolveKeyPath(keyPath, newDepth, value); |
| 978 | } |
| 979 | } |
| 980 | return true; |
| 981 | } |
| 982 |
| 983 | bool LOTFillItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 984 | LOTVariant &value) |
| 985 | { |
| 986 | if (!keyPath.matches(mModel.name(), depth)) { |
| 987 | return false; |
| 988 | } |
| 989 |
| 990 | if (keyPath.fullyResolvesTo(mModel.name(), depth) && |
| 991 | fillProp(value.property())) { |
| 992 | mModel.filter().addValue(value); |
| 993 | return true; |
| 994 | } |
| 995 | return false; |
| 996 | } |
| 997 |
| 998 | bool LOTStrokeItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 999 | LOTVariant &value) |
| 1000 | { |
| 1001 | if (!keyPath.matches(mModel.name(), depth)) { |
| 1002 | return false; |
| 1003 | } |
| 1004 |
| 1005 | if (keyPath.fullyResolvesTo(mModel.name(), depth) && |
| 1006 | strokeProp(value.property())) { |
| 1007 | mModel.filter().addValue(value); |
| 1008 | return true; |
| 1009 | } |
| 1010 | return false; |
| 1011 | } |
| 1012 |
| 1013 | LOTContentGroupItem::LOTContentGroupItem(LOTGroupData *data) |
| 1014 | : LOTContentItem(ContentType::Group), mData(data) |
| 1015 | { |
| 1016 | addChildren(mData); |
| 1017 | } |
| 1018 |
| 1019 | void LOTContentGroupItem::addChildren(LOTGroupData *data) |
| 1020 | { |
| 1021 | if (!data) return; |
| 1022 |
| 1023 | for (auto &i : data->mChildren) { |
| 1024 | auto content = LOTShapeLayerItem::createContentItem(i.get()); |
| 1025 | if (content) { |
| 1026 | content->setParent(this); |
| 1027 | mContents.push_back(std::move(content)); |
| 1028 | } |
| 1029 | } |
| 1030 |
| 1031 | // keep the content in back-to-front order. |
| 1032 | std::reverse(mContents.begin(), mContents.end()); |
| 1033 | } |
| 1034 |
| 1035 | void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix, |
| 1036 | float parentAlpha, const DirtyFlag &flag) |
| 1037 | { |
| 1038 | VMatrix m = parentMatrix; |
| 1039 | float alpha = parentAlpha; |
| 1040 | DirtyFlag newFlag = flag; |
| 1041 |
| 1042 | if (mData && mData->mTransform) { |
| 1043 | // update the matrix and the flag |
| 1044 | if ((flag & DirtyFlagBit::Matrix) || !mData->mTransform->isStatic()) { |
| 1045 | newFlag |= DirtyFlagBit::Matrix; |
| 1046 | } |
| 1047 | m = mData->mTransform->matrix(frameNo); |
| 1048 | m *= parentMatrix; |
| 1049 | alpha *= mData->mTransform->opacity(frameNo); |
| 1050 |
| 1051 | if (!vCompare(alpha, parentAlpha)) { |
| 1052 | newFlag |= DirtyFlagBit::Alpha; |
| 1053 | } |
| 1054 | } |
| 1055 |
| 1056 | mMatrix = m; |
| 1057 |
| 1058 | for (const auto &content : mContents) { |
| 1059 | content->update(frameNo, m, alpha, newFlag); |
| 1060 | } |
| 1061 | } |
| 1062 |
| 1063 | void LOTContentGroupItem::applyTrim() |
| 1064 | { |
| 1065 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1066 | auto content = (*i).get(); |
| 1067 | switch (content->type()) { |
| 1068 | case ContentType::Trim: { |
| 1069 | static_cast<LOTTrimItem *>(content)->update(); |
| 1070 | break; |
| 1071 | } |
| 1072 | case ContentType::Group: { |
| 1073 | static_cast<LOTContentGroupItem *>(content)->applyTrim(); |
| 1074 | break; |
| 1075 | } |
| 1076 | default: |
| 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 |
| 1082 | void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list) |
| 1083 | { |
| 1084 | for (const auto &content : mContents) { |
| 1085 | content->renderList(list); |
| 1086 | } |
| 1087 | } |
| 1088 |
| 1089 | void LOTContentGroupItem::processPaintItems( |
| 1090 | std::vector<LOTPathDataItem *> &list) |
| 1091 | { |
| 1092 | int curOpCount = list.size(); |
| 1093 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1094 | auto content = (*i).get(); |
| 1095 | switch (content->type()) { |
| 1096 | case ContentType::Path: { |
| 1097 | list.push_back(static_cast<LOTPathDataItem *>(content)); |
| 1098 | break; |
| 1099 | } |
| 1100 | case ContentType::Paint: { |
| 1101 | static_cast<LOTPaintDataItem *>(content)->addPathItems(list, |
| 1102 | curOpCount); |
| 1103 | break; |
| 1104 | } |
| 1105 | case ContentType::Group: { |
| 1106 | static_cast<LOTContentGroupItem *>(content)->processPaintItems( |
| 1107 | list); |
| 1108 | break; |
| 1109 | } |
| 1110 | default: |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 |
| 1116 | void LOTContentGroupItem::processTrimItems(std::vector<LOTPathDataItem *> &list) |
| 1117 | { |
| 1118 | int curOpCount = list.size(); |
| 1119 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1120 | auto content = (*i).get(); |
| 1121 |
| 1122 | switch (content->type()) { |
| 1123 | case ContentType::Path: { |
| 1124 | list.push_back(static_cast<LOTPathDataItem *>(content)); |
| 1125 | break; |
| 1126 | } |
| 1127 | case ContentType::Trim: { |
| 1128 | static_cast<LOTTrimItem *>(content)->addPathItems(list, curOpCount); |
| 1129 | break; |
| 1130 | } |
| 1131 | case ContentType::Group: { |
| 1132 | static_cast<LOTContentGroupItem *>(content)->processTrimItems(list); |
| 1133 | break; |
| 1134 | } |
| 1135 | default: |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 |
| 1141 | /* |
| 1142 | * LOTPathDataItem uses 3 path objects for path object reuse. |
| 1143 | * mLocalPath - keeps track of the local path of the item before |
| 1144 | * applying path operation and transformation. |
| 1145 | * mTemp - keeps a referece to the mLocalPath and can be updated by the |
| 1146 | * path operation objects(trim, merge path), |
| 1147 | * mFinalPath - it takes a deep copy of the intermediate path(mTemp) each time |
| 1148 | * when the path is dirty(so if path changes every frame we don't realloc just |
| 1149 | * copy to the final path). NOTE: As path objects are COW objects we have to be |
| 1150 | * carefull about the refcount so that we don't generate deep copy while |
| 1151 | * modifying the path objects. |
| 1152 | */ |
| 1153 | void LOTPathDataItem::update(int frameNo, const VMatrix &, float, |
| 1154 | const DirtyFlag &flag) |
| 1155 | { |
| 1156 | mPathChanged = false; |
| 1157 |
| 1158 | // 1. update the local path if needed |
| 1159 | if (hasChanged(frameNo)) { |
| 1160 | // loose the reference to mLocalPath if any |
| 1161 | // from the last frame update. |
| 1162 | mTemp = VPath(); |
| 1163 |
| 1164 | updatePath(mLocalPath, frameNo); |
| 1165 | mPathChanged = true; |
| 1166 | mNeedUpdate = true; |
| 1167 | } |
| 1168 | // 2. keep a reference path in temp in case there is some |
| 1169 | // path operation like trim which will update the path. |
| 1170 | // we don't want to update the local path. |
| 1171 | mTemp = mLocalPath; |
| 1172 |
| 1173 | // 3. compute the final path with parentMatrix |
| 1174 | if ((flag & DirtyFlagBit::Matrix) || mPathChanged) { |
| 1175 | mPathChanged = true; |
| 1176 | } |
| 1177 | } |
| 1178 |
| 1179 | const VPath &LOTPathDataItem::finalPath() |
| 1180 | { |
| 1181 | if (mPathChanged || mNeedUpdate) { |
| 1182 | mFinalPath.clone(mTemp); |
| 1183 | mFinalPath.transform( |
| 1184 | static_cast<LOTContentGroupItem *>(parent())->matrix()); |
| 1185 | mNeedUpdate = false; |
| 1186 | } |
| 1187 | return mFinalPath; |
| 1188 | } |
| 1189 | LOTRectItem::LOTRectItem(LOTRectData *data) |
| 1190 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1191 | { |
| 1192 | } |
| 1193 |
| 1194 | void LOTRectItem::updatePath(VPath &path, int frameNo) |
| 1195 | { |
| 1196 | VPointF pos = mData->mPos.value(frameNo); |
| 1197 | VPointF size = mData->mSize.value(frameNo); |
| 1198 | float roundness = mData->mRound.value(frameNo); |
| 1199 | VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), |
| 1200 | size.y()); |
| 1201 |
| 1202 | path.reset(); |
| 1203 | path.addRoundRect(r, roundness, mData->direction()); |
| 1204 | } |
| 1205 |
| 1206 | LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data) |
| 1207 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1208 | { |
| 1209 | } |
| 1210 |
| 1211 | void LOTEllipseItem::updatePath(VPath &path, int frameNo) |
| 1212 | { |
| 1213 | VPointF pos = mData->mPos.value(frameNo); |
| 1214 | VPointF size = mData->mSize.value(frameNo); |
| 1215 | VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), |
| 1216 | size.y()); |
| 1217 |
| 1218 | path.reset(); |
| 1219 | path.addOval(r, mData->direction()); |
| 1220 | } |
| 1221 |
| 1222 | LOTShapeItem::LOTShapeItem(LOTShapeData *data) |
| 1223 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1224 | { |
| 1225 | } |
| 1226 |
| 1227 | void LOTShapeItem::updatePath(VPath &path, int frameNo) |
| 1228 | { |
| 1229 | mData->mShape.value(frameNo).toPath(path); |
| 1230 | } |
| 1231 |
| 1232 | LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data) |
| 1233 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1234 | { |
| 1235 | } |
| 1236 |
| 1237 | void LOTPolystarItem::updatePath(VPath &path, int frameNo) |
| 1238 | { |
| 1239 | VPointF pos = mData->mPos.value(frameNo); |
| 1240 | float points = mData->mPointCount.value(frameNo); |
| 1241 | float innerRadius = mData->mInnerRadius.value(frameNo); |
| 1242 | float outerRadius = mData->mOuterRadius.value(frameNo); |
| 1243 | float innerRoundness = mData->mInnerRoundness.value(frameNo); |
| 1244 | float outerRoundness = mData->mOuterRoundness.value(frameNo); |
| 1245 | float rotation = mData->mRotation.value(frameNo); |
| 1246 |
| 1247 | path.reset(); |
| 1248 | VMatrix m; |
| 1249 |
| 1250 | if (mData->mType == LOTPolystarData::PolyType::Star) { |
| 1251 | path.addPolystar(points, innerRadius, outerRadius, innerRoundness, |
| 1252 | outerRoundness, 0.0, 0.0, 0.0, mData->direction()); |
| 1253 | } else { |
| 1254 | path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0, |
| 1255 | mData->direction()); |
| 1256 | } |
| 1257 |
| 1258 | m.translate(pos.x(), pos.y()).rotate(rotation); |
| 1259 | m.rotate(rotation); |
| 1260 | path.transform(m); |
| 1261 | } |
| 1262 |
| 1263 | /* |
| 1264 | * PaintData Node handling |
| 1265 | * |
| 1266 | */ |
| 1267 | LOTPaintDataItem::LOTPaintDataItem(bool staticContent) |
| 1268 | : LOTContentItem(ContentType::Paint), mStaticContent(staticContent) |
| 1269 | { |
| 1270 | } |
| 1271 |
| 1272 | void LOTPaintDataItem::update(int frameNo, const VMatrix & /*parentMatrix*/, |
| 1273 | float parentAlpha, const DirtyFlag &flag) |
| 1274 | { |
| 1275 | mRenderNodeUpdate = true; |
| 1276 | mParentAlpha = parentAlpha; |
| 1277 | mFlag = flag; |
| 1278 | mFrameNo = frameNo; |
| 1279 |
| 1280 | updateContent(frameNo); |
| 1281 | } |
| 1282 |
| 1283 | void LOTPaintDataItem::updateRenderNode() |
| 1284 | { |
| 1285 | bool dirty = false; |
| 1286 | for (auto &i : mPathItems) { |
| 1287 | if (i->dirty()) { |
| 1288 | dirty = true; |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 |
| 1293 | if (dirty) { |
| 1294 | mPath.reset(); |
| 1295 |
| 1296 | for (auto &i : mPathItems) { |
| 1297 | mPath.addPath(i->finalPath()); |
| 1298 | } |
| 1299 | mDrawable.setPath(mPath); |
| 1300 | } else { |
| 1301 | if (mDrawable.mFlag & VDrawable::DirtyState::Path) |
| 1302 | mDrawable.mPath = mPath; |
| 1303 | } |
| 1304 | } |
| 1305 |
| 1306 | void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list) |
| 1307 | { |
| 1308 | if (mRenderNodeUpdate) { |
| 1309 | updateRenderNode(); |
| 1310 | LOTPaintDataItem::updateRenderNode(); |
| 1311 | mRenderNodeUpdate = false; |
| 1312 | } |
| 1313 | list.push_back(&mDrawable); |
| 1314 | } |
| 1315 |
| 1316 | void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, |
| 1317 | int startOffset) |
| 1318 | { |
| 1319 | std::copy(list.begin() + startOffset, list.end(), |
| 1320 | back_inserter(mPathItems)); |
| 1321 | } |
| 1322 |
| 1323 | LOTFillItem::LOTFillItem(LOTFillData *data) |
| 1324 | : LOTPaintDataItem(data->isStatic()), mModel(data) |
| 1325 | { |
| 1326 | } |
| 1327 |
| 1328 | void LOTFillItem::updateContent(int frameNo) |
| 1329 | { |
| 1330 | mColor = mModel.color(frameNo).toColor(mModel.opacity(frameNo)); |
| 1331 | } |
| 1332 |
| 1333 | void LOTFillItem::updateRenderNode() |
| 1334 | { |
| 1335 | VColor color = mColor; |
| 1336 |
| 1337 | color.setAlpha(color.a * parentAlpha()); |
| 1338 | VBrush brush(color); |
| 1339 | mDrawable.setBrush(brush); |
| 1340 | mDrawable.setFillRule(mModel.fillRule()); |
| 1341 | } |
| 1342 |
| 1343 | LOTGFillItem::LOTGFillItem(LOTGFillData *data) |
| 1344 | : LOTPaintDataItem(data->isStatic()), mData(data) |
| 1345 | { |
| 1346 | } |
| 1347 |
| 1348 | void LOTGFillItem::updateContent(int frameNo) |
| 1349 | { |
| 1350 | mAlpha = mData->opacity(frameNo); |
| 1351 | mData->update(mGradient, frameNo); |
| 1352 | mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix(); |
| 1353 | mFillRule = mData->fillRule(); |
| 1354 | } |
| 1355 |
| 1356 | void LOTGFillItem::updateRenderNode() |
| 1357 | { |
| 1358 | mGradient->setAlpha(mAlpha * parentAlpha()); |
| 1359 | mDrawable.setBrush(VBrush(mGradient.get())); |
| 1360 | mDrawable.setFillRule(mFillRule); |
| 1361 | } |
| 1362 |
| 1363 | LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data) |
| 1364 | : LOTPaintDataItem(data->isStatic()), mModel(data) |
| 1365 | { |
| 1366 | mDashArraySize = 0; |
| 1367 | } |
| 1368 |
| 1369 | void LOTStrokeItem::updateContent(int frameNo) |
| 1370 | { |
| 1371 | mColor = mModel.color(frameNo).toColor(mModel.opacity(frameNo)); |
| 1372 | mWidth = mModel.strokeWidth(frameNo); |
| 1373 | if (mModel.hasDashInfo()) { |
| 1374 | mDashArraySize = mModel.getDashInfo(frameNo, mDashArray); |
| 1375 | } |
| 1376 | } |
| 1377 |
| 1378 | static float getScale(const VMatrix &matrix) |
| 1379 | { |
| 1380 | constexpr float SQRT_2 = 1.41421; |
| 1381 | VPointF p1(0, 0); |
| 1382 | VPointF p2(SQRT_2, SQRT_2); |
| 1383 | p1 = matrix.map(p1); |
| 1384 | p2 = matrix.map(p2); |
| 1385 | VPointF final = p2 - p1; |
| 1386 |
| 1387 | return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0; |
| 1388 | } |
| 1389 |
| 1390 | void LOTStrokeItem::updateRenderNode() |
| 1391 | { |
| 1392 | VColor color = mColor; |
| 1393 |
| 1394 | color.setAlpha(color.a * parentAlpha()); |
| 1395 | VBrush brush(color); |
| 1396 | mDrawable.setBrush(brush); |
| 1397 | float scale = |
| 1398 | getScale(static_cast<LOTContentGroupItem *>(parent())->matrix()); |
| 1399 | mDrawable.setStrokeInfo(mModel.capStyle(), mModel.joinStyle(), |
| 1400 | mModel.meterLimit(), mWidth * scale); |
| 1401 | if (mDashArraySize) { |
| 1402 | for (int i = 0; i < mDashArraySize; i++) mDashArray[i] *= scale; |
| 1403 |
| 1404 | /* AE draw the dash even if dash value is 0 */ |
| 1405 | if (vCompare(mDashArray[0], 0.0f)) mDashArray[0] = 0.1; |
| 1406 |
| 1407 | mDrawable.setDashInfo(mDashArray, mDashArraySize); |
| 1408 | } |
| 1409 | } |
| 1410 |
| 1411 | LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data) |
| 1412 | : LOTPaintDataItem(data->isStatic()), mData(data) |
| 1413 | { |
| 1414 | mDashArraySize = 0; |
| 1415 | } |
| 1416 |
| 1417 | void LOTGStrokeItem::updateContent(int frameNo) |
| 1418 | { |
| 1419 | mAlpha = mData->opacity(frameNo); |
| 1420 | mData->update(mGradient, frameNo); |
| 1421 | mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix(); |
| 1422 | mCap = mData->capStyle(); |
| 1423 | mJoin = mData->joinStyle(); |
| 1424 | mMiterLimit = mData->meterLimit(); |
| 1425 | mWidth = mData->width(frameNo); |
| 1426 | if (mData->hasDashInfo()) { |
| 1427 | mDashArraySize = mData->getDashInfo(frameNo, mDashArray); |
| 1428 | } |
| 1429 | } |
| 1430 |
| 1431 | void LOTGStrokeItem::updateRenderNode() |
| 1432 | { |
| 1433 | float scale = getScale(mGradient->mMatrix); |
| 1434 | mGradient->setAlpha(mAlpha * parentAlpha()); |
| 1435 | mDrawable.setBrush(VBrush(mGradient.get())); |
| 1436 | mDrawable.setStrokeInfo(mCap, mJoin, mMiterLimit, mWidth * scale); |
| 1437 | if (mDashArraySize) { |
| 1438 | for (int i = 0; i < mDashArraySize; i++) mDashArray[i] *= scale; |
| 1439 | mDrawable.setDashInfo(mDashArray, mDashArraySize); |
| 1440 | } |
| 1441 | } |
| 1442 |
| 1443 | LOTTrimItem::LOTTrimItem(LOTTrimData *data) |
| 1444 | : LOTContentItem(ContentType::Trim), mData(data) |
| 1445 | { |
| 1446 | } |
| 1447 |
| 1448 | void LOTTrimItem::update(int frameNo, const VMatrix & /*parentMatrix*/, |
| 1449 | float /*parentAlpha*/, const DirtyFlag & /*flag*/) |
| 1450 | { |
| 1451 | mDirty = false; |
| 1452 |
| 1453 | if (mCache.mFrameNo == frameNo) return; |
| 1454 |
| 1455 | LOTTrimData::Segment segment = mData->segment(frameNo); |
| 1456 |
| 1457 | if (!(vCompare(mCache.mSegment.start, segment.start) && |
| 1458 | vCompare(mCache.mSegment.end, segment.end))) { |
| 1459 | mDirty = true; |
| 1460 | mCache.mSegment = segment; |
| 1461 | } |
| 1462 | mCache.mFrameNo = frameNo; |
| 1463 | } |
| 1464 |
| 1465 | void LOTTrimItem::update() |
| 1466 | { |
| 1467 | // when both path and trim are not dirty |
| 1468 | if (!(mDirty || pathDirty())) return; |
| 1469 |
| 1470 | if (vCompare(mCache.mSegment.start, mCache.mSegment.end)) { |
| 1471 | for (auto &i : mPathItems) { |
| 1472 | i->updatePath(VPath()); |
| 1473 | } |
| 1474 | return; |
| 1475 | } |
| 1476 |
| 1477 | if (vCompare(std::fabs(mCache.mSegment.start - mCache.mSegment.end), 1)) { |
| 1478 | for (auto &i : mPathItems) { |
| 1479 | i->updatePath(i->localPath()); |
| 1480 | } |
| 1481 | return; |
| 1482 | } |
| 1483 |
| 1484 | if (mData->type() == LOTTrimData::TrimType::Simultaneously) { |
| 1485 | for (auto &i : mPathItems) { |
| 1486 | VPathMesure pm; |
| 1487 | pm.setStart(mCache.mSegment.start); |
| 1488 | pm.setEnd(mCache.mSegment.end); |
| 1489 | i->updatePath(pm.trim(i->localPath())); |
| 1490 | } |
| 1491 | } else { // LOTTrimData::TrimType::Individually |
| 1492 | float totalLength = 0.0; |
| 1493 | for (auto &i : mPathItems) { |
| 1494 | totalLength += i->localPath().length(); |
| 1495 | } |
| 1496 | float start = totalLength * mCache.mSegment.start; |
| 1497 | float end = totalLength * mCache.mSegment.end; |
| 1498 |
| 1499 | if (start < end) { |
| 1500 | float curLen = 0.0; |
| 1501 | for (auto &i : mPathItems) { |
| 1502 | if (curLen > end) { |
| 1503 | // update with empty path. |
| 1504 | i->updatePath(VPath()); |
| 1505 | continue; |
| 1506 | } |
| 1507 | float len = i->localPath().length(); |
| 1508 |
| 1509 | if (curLen < start && curLen + len < start) { |
| 1510 | curLen += len; |
| 1511 | // update with empty path. |
| 1512 | i->updatePath(VPath()); |
| 1513 | continue; |
| 1514 | } else if (start <= curLen && end >= curLen + len) { |
| 1515 | // inside segment |
| 1516 | curLen += len; |
| 1517 | continue; |
| 1518 | } else { |
| 1519 | float local_start = start > curLen ? start - curLen : 0; |
| 1520 | local_start /= len; |
| 1521 | float local_end = curLen + len < end ? len : end - curLen; |
| 1522 | local_end /= len; |
| 1523 | VPathMesure pm; |
| 1524 | pm.setStart(local_start); |
| 1525 | pm.setEnd(local_end); |
| 1526 | VPath p = pm.trim(i->localPath()); |
| 1527 | i->updatePath(p); |
| 1528 | curLen += len; |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 |
| 1535 | void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, |
| 1536 | int startOffset) |
| 1537 | { |
| 1538 | std::copy(list.begin() + startOffset, list.end(), |
| 1539 | back_inserter(mPathItems)); |
| 1540 | } |
| 1541 |
| 1542 | LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mRepeaterData(data) |
| 1543 | { |
| 1544 | assert(mRepeaterData->content()); |
| 1545 |
| 1546 | mCopies = mRepeaterData->maxCopies(); |
| 1547 |
| 1548 | for (int i = 0; i < mCopies; i++) { |
| 1549 | auto content = |
| 1550 | std::make_unique<LOTContentGroupItem>(mRepeaterData->content()); |
| 1551 | content->setParent(this); |
| 1552 | mContents.push_back(std::move(content)); |
| 1553 | } |
| 1554 | } |
| 1555 |
| 1556 | void LOTRepeaterItem::update(int frameNo, const VMatrix &parentMatrix, |
| 1557 | float parentAlpha, const DirtyFlag &flag) |
| 1558 | { |
| 1559 | DirtyFlag newFlag = flag; |
| 1560 |
| 1561 | float copies = mRepeaterData->copies(frameNo); |
| 1562 | int visibleCopies = int(copies); |
| 1563 |
| 1564 | if (visibleCopies == 0) { |
| 1565 | mHidden = true; |
| 1566 | return; |
| 1567 | } else { |
| 1568 | mHidden = false; |
| 1569 | } |
| 1570 |
| 1571 | if (!mRepeaterData->isStatic()) newFlag |= DirtyFlagBit::Matrix; |
| 1572 |
| 1573 | float offset = mRepeaterData->offset(frameNo); |
| 1574 | float startOpacity = mRepeaterData->mTransform.startOpacity(frameNo); |
| 1575 | float endOpacity = mRepeaterData->mTransform.endOpacity(frameNo); |
| 1576 |
| 1577 | newFlag |= DirtyFlagBit::Alpha; |
| 1578 |
| 1579 | for (int i = 0; i < mCopies; ++i) { |
| 1580 | float newAlpha = |
| 1581 | parentAlpha * lerp(startOpacity, endOpacity, i / copies); |
| 1582 |
| 1583 | // hide rest of the copies , @TODO find a better solution. |
| 1584 | if (i >= visibleCopies) newAlpha = 0; |
| 1585 |
| 1586 | VMatrix result = mRepeaterData->mTransform.matrix(frameNo, i + offset) * |
| 1587 | parentMatrix; |
| 1588 | mContents[i]->update(frameNo, result, newAlpha, newFlag); |
| 1589 | } |
| 1590 | } |
| 1591 |
| 1592 | void LOTRepeaterItem::renderList(std::vector<VDrawable *> &list) |
| 1593 | { |
| 1594 | if (mHidden) return; |
| 1595 | return LOTContentGroupItem::renderList(list); |
| 1596 | } |
| 1597 |
| 1598 | static void updateGStops(LOTNode *n, const VGradient *grad) |
| 1599 | { |
| 1600 | if (grad->mStops.size() != n->mGradient.stopCount) { |
| 1601 | if (n->mGradient.stopCount) free(n->mGradient.stopPtr); |
| 1602 | n->mGradient.stopCount = grad->mStops.size(); |
| 1603 | n->mGradient.stopPtr = (LOTGradientStop *)malloc( |
| 1604 | n->mGradient.stopCount * sizeof(LOTGradientStop)); |
| 1605 | } |
| 1606 |
| 1607 | LOTGradientStop *ptr = n->mGradient.stopPtr; |
| 1608 | for (const auto &i : grad->mStops) { |
| 1609 | ptr->pos = i.first; |
| 1610 | ptr->a = i.second.alpha() * grad->alpha(); |
| 1611 | ptr->r = i.second.red(); |
| 1612 | ptr->g = i.second.green(); |
| 1613 | ptr->b = i.second.blue(); |
| 1614 | ptr++; |
| 1615 | } |
| 1616 | } |
| 1617 |
| 1618 | void LOTDrawable::sync() |
| 1619 | { |
| 1620 | if (!mCNode) { |
| 1621 | mCNode = std::make_unique<LOTNode>(); |
| 1622 | mCNode->mGradient.stopPtr = nullptr; |
| 1623 | mCNode->mGradient.stopCount = 0; |
| 1624 | } |
| 1625 |
| 1626 | mCNode->mFlag = ChangeFlagNone; |
| 1627 | if (mFlag & DirtyState::None) return; |
| 1628 |
| 1629 | if (mFlag & DirtyState::Path) { |
| 1630 | if (mStroke.mDash.size()) { |
| 1631 | VDasher dasher(mStroke.mDash.data(), mStroke.mDash.size()); |
| 1632 | mPath = dasher.dashed(mPath); |
| 1633 | } |
| 1634 | const std::vector<VPath::Element> &elm = mPath.elements(); |
| 1635 | const std::vector<VPointF> & pts = mPath.points(); |
| 1636 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 1637 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 1638 | mCNode->mPath.elmPtr = elmPtr; |
| 1639 | mCNode->mPath.elmCount = elm.size(); |
| 1640 | mCNode->mPath.ptPtr = ptPtr; |
| 1641 | mCNode->mPath.ptCount = 2 * pts.size(); |
| 1642 | mCNode->mFlag |= ChangeFlagPath; |
| 1643 | } |
| 1644 |
| 1645 | if (mStroke.enable) { |
| 1646 | mCNode->mStroke.width = mStroke.width; |
| 1647 | mCNode->mStroke.meterLimit = mStroke.meterLimit; |
| 1648 | mCNode->mStroke.enable = 1; |
| 1649 |
| 1650 | switch (mStroke.cap) { |
| 1651 | case CapStyle::Flat: |
| 1652 | mCNode->mStroke.cap = LOTCapStyle::CapFlat; |
| 1653 | break; |
| 1654 | case CapStyle::Square: |
| 1655 | mCNode->mStroke.cap = LOTCapStyle::CapSquare; |
| 1656 | break; |
| 1657 | case CapStyle::Round: |
| 1658 | mCNode->mStroke.cap = LOTCapStyle::CapRound; |
| 1659 | break; |
| 1660 | } |
| 1661 |
| 1662 | switch (mStroke.join) { |
| 1663 | case JoinStyle::Miter: |
| 1664 | mCNode->mStroke.join = LOTJoinStyle::JoinMiter; |
| 1665 | break; |
| 1666 | case JoinStyle::Bevel: |
| 1667 | mCNode->mStroke.join = LOTJoinStyle::JoinBevel; |
| 1668 | break; |
| 1669 | case JoinStyle::Round: |
| 1670 | mCNode->mStroke.join = LOTJoinStyle::JoinRound; |
| 1671 | break; |
| 1672 | default: |
| 1673 | mCNode->mStroke.join = LOTJoinStyle::JoinMiter; |
| 1674 | break; |
| 1675 | } |
| 1676 | } else { |
| 1677 | mCNode->mStroke.enable = 0; |
| 1678 | } |
| 1679 |
| 1680 | switch (mFillRule) { |
| 1681 | case FillRule::EvenOdd: |
| 1682 | mCNode->mFillRule = LOTFillRule::FillEvenOdd; |
| 1683 | break; |
| 1684 | default: |
| 1685 | mCNode->mFillRule = LOTFillRule::FillWinding; |
| 1686 | break; |
| 1687 | } |
| 1688 |
| 1689 | switch (mBrush.type()) { |
| 1690 | case VBrush::Type::Solid: |
| 1691 | mCNode->mBrushType = LOTBrushType::BrushSolid; |
| 1692 | mCNode->mColor.r = mBrush.mColor.r; |
| 1693 | mCNode->mColor.g = mBrush.mColor.g; |
| 1694 | mCNode->mColor.b = mBrush.mColor.b; |
| 1695 | mCNode->mColor.a = mBrush.mColor.a; |
| 1696 | break; |
| 1697 | case VBrush::Type::LinearGradient: { |
| 1698 | mCNode->mBrushType = LOTBrushType::BrushGradient; |
| 1699 | mCNode->mGradient.type = LOTGradientType::GradientLinear; |
| 1700 | VPointF s = mBrush.mGradient->mMatrix.map( |
| 1701 | {mBrush.mGradient->linear.x1, mBrush.mGradient->linear.y1}); |
| 1702 | VPointF e = mBrush.mGradient->mMatrix.map( |
| 1703 | {mBrush.mGradient->linear.x2, mBrush.mGradient->linear.y2}); |
| 1704 | mCNode->mGradient.start.x = s.x(); |
| 1705 | mCNode->mGradient.start.y = s.y(); |
| 1706 | mCNode->mGradient.end.x = e.x(); |
| 1707 | mCNode->mGradient.end.y = e.y(); |
| 1708 | updateGStops(mCNode.get(), mBrush.mGradient); |
| 1709 | break; |
| 1710 | } |
| 1711 | case VBrush::Type::RadialGradient: { |
| 1712 | mCNode->mBrushType = LOTBrushType::BrushGradient; |
| 1713 | mCNode->mGradient.type = LOTGradientType::GradientRadial; |
| 1714 | VPointF c = mBrush.mGradient->mMatrix.map( |
| 1715 | {mBrush.mGradient->radial.cx, mBrush.mGradient->radial.cy}); |
| 1716 | VPointF f = mBrush.mGradient->mMatrix.map( |
| 1717 | {mBrush.mGradient->radial.fx, mBrush.mGradient->radial.fy}); |
| 1718 | mCNode->mGradient.center.x = c.x(); |
| 1719 | mCNode->mGradient.center.y = c.y(); |
| 1720 | mCNode->mGradient.focal.x = f.x(); |
| 1721 | mCNode->mGradient.focal.y = f.y(); |
| 1722 |
| 1723 | float scale = getScale(mBrush.mGradient->mMatrix); |
| 1724 | mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius * scale; |
| 1725 | mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius * scale; |
| 1726 | updateGStops(mCNode.get(), mBrush.mGradient); |
| 1727 | break; |
| 1728 | } |
| 1729 | default: |
| 1730 | break; |
| 1731 | } |
| 1732 | } |
| 1733 |