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