From 35dac266dddc34173b2c30e8f77ae6fd18bdfb0a Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Wed, 5 Feb 2025 15:05:57 +0900 Subject: [PATCH] lottie: neat code++ --- src/loaders/lottie/tvgLottieBuilder.cpp | 52 ++++++++++-------------- src/loaders/lottie/tvgLottieModifier.cpp | 6 +-- src/loaders/lottie/tvgLottieModifier.h | 2 +- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/loaders/lottie/tvgLottieBuilder.cpp b/src/loaders/lottie/tvgLottieBuilder.cpp index a07e50fd..25cd171c 100644 --- a/src/loaders/lottie/tvgLottieBuilder.cpp +++ b/src/loaders/lottie/tvgLottieBuilder.cpp @@ -458,7 +458,6 @@ static void _appendRect(Shape* shape, Point& pos, Point& size, float r, bool clo void LottieBuilder::updateRect(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx) { auto rect = static_cast(*child); - auto size = rect->size(frameNo, exps); auto pos = rect->position(frameNo, exps) - size * 0.5f; auto r = rect->radius(frameNo, exps); @@ -481,45 +480,36 @@ void LottieBuilder::updateRect(LottieGroup* parent, LottieObject** child, float } -static void _appendCircle(Shape* shape, float cx, float cy, float rx, float ry, const LottieOffsetModifier* offset, Matrix* transform, bool clockwise) +static void _appendCircle(Shape* shape, Point& center, Point& radius, bool clockwise, RenderContext* ctx) { - if (offset) offset->modifyEllipse(rx, ry); + if (ctx->offset) ctx->offset->modifyEllipse(radius); - if (rx == 0.0f || ry == 0.0f) return; + if (tvg::zero(radius)) return; - auto rxKappa = rx * PATH_KAPPA; - auto ryKappa = ry * PATH_KAPPA; + auto rKappa = radius * PATH_KAPPA; constexpr int cmdsCnt = 6; - PathCommand commands[cmdsCnt] = { - PathCommand::MoveTo, PathCommand::CubicTo, PathCommand::CubicTo, - PathCommand::CubicTo, PathCommand::CubicTo, PathCommand::Close - }; + PathCommand cmds[cmdsCnt] = {PathCommand::MoveTo, PathCommand::CubicTo, PathCommand::CubicTo, PathCommand::CubicTo, PathCommand::CubicTo, PathCommand::Close}; constexpr int ptsCnt = 13; - Point points[ptsCnt]; + Point pts[ptsCnt]; - if (clockwise) { - points[0] = {cx, cy - ry}; //moveTo - points[1] = {cx + rxKappa, cy - ry}; points[2] = {cx + rx, cy - ryKappa}; points[3] = {cx + rx, cy}; //cubicTo - points[4] = {cx + rx, cy + ryKappa}; points[5] = {cx + rxKappa, cy + ry}; points[6] = {cx, cy + ry}; //cubicTo - points[7] = {cx - rxKappa, cy + ry}; points[8] = {cx - rx, cy + ryKappa}; points[9] = {cx - rx, cy}; //cubicTo - points[10] = {cx - rx, cy - ryKappa}; points[11] = {cx - rxKappa, cy - ry}; points[12] = {cx, cy - ry}; //cubicTo - } else { - points[0] = {cx, cy - ry}; //moveTo - points[1] = {cx - rxKappa, cy - ry}; points[2] = {cx - rx, cy - ryKappa}; points[3] = {cx - rx, cy}; //cubicTo - points[4] = {cx - rx, cy + ryKappa}; points[5] = {cx - rxKappa, cy + ry}; points[6] = {cx, cy + ry}; //cubicTo - points[7] = {cx + rxKappa, cy + ry}; points[8] = {cx + rx, cy + ryKappa}; points[9] = {cx + rx, cy}; //cubicTo - points[10] = {cx + rx, cy - ryKappa}; points[11] = {cx + rxKappa, cy - ry}; points[12] = {cx, cy - ry}; //cubicTo - } + int table[2][ptsCnt] = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 12}}; + int* idx = clockwise ? table[0] : table[1]; - if (transform) { + pts[idx[0]] = {center.x, center.y - radius.y}; //moveTo + pts[idx[1]] = {center.x + rKappa.x, center.y - radius.y}; pts[idx[2]] = {center.x + radius.x, center.y - rKappa.y}; pts[idx[3]] = {center.x + radius.x, center.y}; //cubicTo + pts[idx[4]] = {center.x + radius.x, center.y + rKappa.y}; pts[idx[5]] = {center.x + rKappa.x, center.y + radius.y}; pts[idx[6]] = {center.x, center.y + radius.y}; //cubicTo + pts[idx[7]] = {center.x - rKappa.x, center.y + radius.y}; pts[idx[8]] = {center.x - radius.x, center.y + rKappa.y}; pts[idx[9]] = {center.x - radius.x, center.y}; //cubicTo + pts[idx[10]] = {center.x - radius.x, center.y - rKappa.y}; pts[idx[11]] = {center.x - rKappa.x, center.y - radius.y}; pts[idx[12]] = {center.x, center.y - radius.y}; //cubicTo + + if (ctx->transform) { for (int i = 0; i < ptsCnt; ++i) { - points[i] *= *transform; + pts[i] *= *ctx->transform; } } - shape->appendPath(commands, cmdsCnt, points, ptsCnt); + shape->appendPath(cmds, cmdsCnt, pts, ptsCnt); } @@ -527,17 +517,17 @@ void LottieBuilder::updateEllipse(LottieGroup* parent, LottieObject** child, flo { auto ellipse = static_cast(*child); - auto position = ellipse->position(frameNo, exps); - auto size = ellipse->size(frameNo, exps); + auto pos = ellipse->position(frameNo, exps); + auto size = ellipse->size(frameNo, exps) * 0.5f; if (!ctx->repeaters.empty()) { auto shape = ellipse->pooling(); shape->reset(); - _appendCircle(shape, position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->offset, ctx->transform, ellipse->clockwise); + _appendCircle(shape, pos, size, ellipse->clockwise, ctx); _repeat(parent, shape, ctx); } else { _draw(parent, ellipse, ctx); - _appendCircle(ctx->merging, position.x, position.y, size.x * 0.5f, size.y * 0.5f, ctx->offset, ctx->transform, ellipse->clockwise); + _appendCircle(ctx->merging, pos, size, ellipse->clockwise, ctx); } } diff --git a/src/loaders/lottie/tvgLottieModifier.cpp b/src/loaders/lottie/tvgLottieModifier.cpp index f9d4007b..7a5f265e 100644 --- a/src/loaders/lottie/tvgLottieModifier.cpp +++ b/src/loaders/lottie/tvgLottieModifier.cpp @@ -387,9 +387,9 @@ bool LottieOffsetModifier::modifyRect(const PathCommand* inCmds, uint32_t inCmds } -bool LottieOffsetModifier::modifyEllipse(float& rx, float& ry) const +bool LottieOffsetModifier::modifyEllipse(Point& radius) const { - rx += offset; - ry += offset; + radius.x += offset; + radius.y += offset; return true; } \ No newline at end of file diff --git a/src/loaders/lottie/tvgLottieModifier.h b/src/loaders/lottie/tvgLottieModifier.h index cc3f77e6..444ca836 100644 --- a/src/loaders/lottie/tvgLottieModifier.h +++ b/src/loaders/lottie/tvgLottieModifier.h @@ -52,7 +52,7 @@ struct LottieOffsetModifier bool modifyPath(const PathCommand* inCmds, uint32_t inCmdsCnt, const Point* inPts, uint32_t inPtsCnt, Array& outCmds, Array& outPts) const; bool modifyPolystar(const Array& inCmds, const Array& inPts, Array& outCmds, Array& outPts) const; bool modifyRect(const PathCommand* inCmds, uint32_t inCmdsCnt, const Point* inPts, uint32_t inPtsCnt, Array& outCmds, Array& outPts) const; - bool modifyEllipse(float& rx, float& ry) const; + bool modifyEllipse(Point& radius) const; private: struct State