From f6069034708eb5f082acf3a679b580902021f793 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Sun, 3 Mar 2024 13:21:46 +0900 Subject: [PATCH] lottie: code refactoring. Maintains compact lines of code and functions without altering logic, consistently prioritizing simplicity in software complexity metrics. --- src/common/tvgMath.h | 1 + src/loaders/lottie/tvgLottieBuilder.cpp | 93 +++++++++++-------------- src/renderer/tvgShape.cpp | 2 +- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/src/common/tvgMath.h b/src/common/tvgMath.h index 50c3458e..7f670826 100644 --- a/src/common/tvgMath.h +++ b/src/common/tvgMath.h @@ -31,6 +31,7 @@ #define MATH_PI 3.14159265358979323846f #define MATH_PI2 1.57079632679489661923f +#define PATH_KAPPA 0.552284f #define mathMin(x, y) (((x) < (y)) ? (x) : (y)) #define mathMax(x, y) (((x) > (y)) ? (x) : (y)) diff --git a/src/loaders/lottie/tvgLottieBuilder.cpp b/src/loaders/lottie/tvgLottieBuilder.cpp index 0261c6a0..65f0a9c7 100644 --- a/src/loaders/lottie/tvgLottieBuilder.cpp +++ b/src/loaders/lottie/tvgLottieBuilder.cpp @@ -23,6 +23,7 @@ #include #include "tvgCommon.h" +#include "tvgMath.h" #include "tvgPaint.h" #include "tvgShape.h" #include "tvgInlist.h" @@ -34,8 +35,6 @@ /************************************************************************/ /* Internal Class Implementation */ /************************************************************************/ -constexpr auto PATH_KAPPA = 0.552284f; - struct RenderRepeater { @@ -373,60 +372,46 @@ static void _repeat(LottieGroup* parent, unique_ptr path, RenderContext* } -static void _appendSharpRect(Shape* shape, float x, float y, float w, float h, float r) -{ - constexpr int commandsCnt = 5; - PathCommand commands[commandsCnt] = { - PathCommand::MoveTo, PathCommand::LineTo, PathCommand::LineTo, - PathCommand::LineTo, PathCommand::Close - }; - - constexpr int pointsCnt = 4; - Point points[pointsCnt] = {{x + w, y}, {x + w, y + h}, {x, y + h}, {x, y}}; - - shape->appendPath(commands, commandsCnt, points, pointsCnt); -} - -static void _appendRoundedRect(Shape* shape, float x, float y, float w, float h, float r) -{ - constexpr int commandsCnt = 10; - PathCommand commands[commandsCnt] = { - PathCommand::MoveTo, PathCommand::LineTo, PathCommand::CubicTo, - PathCommand::LineTo, PathCommand::CubicTo, PathCommand::LineTo, - PathCommand::CubicTo, PathCommand::LineTo, PathCommand::CubicTo, - PathCommand::Close - }; - - auto halfW = w * 0.5f; - auto halfH = h * 0.5f; - - auto rx = r > halfW ? halfW : r; - auto ry = r > halfH ? halfH : r; - - auto hrx = rx * PATH_KAPPA; - auto hry = ry * PATH_KAPPA; - - constexpr int pointsCnt = 17; - Point points[pointsCnt] = { - {x + w, y + ry}, //moveTo - {x + w, y + h - ry}, //lineTo - {x + w, y + h - ry + hry}, {x + w - rx + hrx, y + h}, {x + w - rx, y + h}, //cubicTo - {x + rx, y + h}, //lineTo - {x + rx - hrx, y + h}, {x, y + h - ry + hry}, {x, y + h - ry}, //cubicTo - {x, y + ry}, //lineTo - {x, y + ry - hry}, {x + rx - hrx, y}, {x + rx, y}, //cubicTo - {x + w - rx, y}, //lineTo - {x + w - rx + hrx, y}, {x + w, y + ry - hry}, {x + w, y + ry} //cubicTo - }; - - shape->appendPath(commands, commandsCnt, points, pointsCnt); -} - - static void _appendRect(Shape* shape, float x, float y, float w, float h, float r) { - if (mathZero(r)) _appendSharpRect(shape, x, y, w, h, r); - else _appendRoundedRect(shape, x, y, w, h, r); + //sharp rect + if (mathZero(r)) { + PathCommand commands[] = { + PathCommand::MoveTo, PathCommand::LineTo, PathCommand::LineTo, + PathCommand::LineTo, PathCommand::Close + }; + Point points[] = {{x + w, y}, {x + w, y + h}, {x, y + h}, {x, y}}; + shape->appendPath(commands, 5, points, 4); + //round rect + } else { + PathCommand commands[] = { + PathCommand::MoveTo, PathCommand::LineTo, PathCommand::CubicTo, + PathCommand::LineTo, PathCommand::CubicTo, PathCommand::LineTo, + PathCommand::CubicTo, PathCommand::LineTo, PathCommand::CubicTo, + PathCommand::Close + }; + + auto halfW = w * 0.5f; + auto halfH = h * 0.5f; + auto rx = r > halfW ? halfW : r; + auto ry = r > halfH ? halfH : r; + auto hrx = rx * PATH_KAPPA; + auto hry = ry * PATH_KAPPA; + + Point points[] = { + {x + w, y + ry}, //moveTo + {x + w, y + h - ry}, //lineTo + {x + w, y + h - ry + hry}, {x + w - rx + hrx, y + h}, {x + w - rx, y + h}, //cubicTo + {x + rx, y + h}, //lineTo + {x + rx - hrx, y + h}, {x, y + h - ry + hry}, {x, y + h - ry}, //cubicTo + {x, y + ry}, //lineTo + {x, y + ry - hry}, {x + rx - hrx, y}, {x + rx, y}, //cubicTo + {x + w - rx, y}, //lineTo + {x + w - rx + hrx, y}, {x + w, y + ry - hry}, {x + w, y + ry} //cubicTo + }; + + shape->appendPath(commands, 10, points, 17); + } } static void _updateRect(LottieGroup* parent, LottieObject** child, float frameNo, TVG_UNUSED Inlist& contexts, RenderContext* ctx) diff --git a/src/renderer/tvgShape.cpp b/src/renderer/tvgShape.cpp index 4c98be9f..ab1f378b 100644 --- a/src/renderer/tvgShape.cpp +++ b/src/renderer/tvgShape.cpp @@ -26,7 +26,7 @@ /************************************************************************/ /* Internal Class Implementation */ /************************************************************************/ -constexpr auto PATH_KAPPA = 0.552284f; + /************************************************************************/ /* External Class Implementation */