From: Subhransu Mohanty Date: Fri, 3 Jul 2020 03:57:03 +0000 (+0900) Subject: rlottie/api: Added loadFromData() api with ColorFilter support X-Git-Tag: submit/tizen/20200713.050659~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c6bae455d0d1856e60ae2541577f904e5d5ee114;p=platform%2Fcore%2Fuifw%2Flottie-player.git rlottie/api: Added loadFromData() api with ColorFilter support Some usecase needs to apply the color filter during parsing for theme support. Ex: Same resource can be used for different theme with different colorfilter which will update the color pallet according to the theme. --- diff --git a/inc/rlottie.h b/inc/rlottie.h index b37f67a..9b55351 100644 --- a/inc/rlottie.h +++ b/inc/rlottie.h @@ -1,16 +1,16 @@ -/* +/* * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -261,6 +261,9 @@ using MarkerList = std::vector>; using LayerInfoList = std::vector>; + +using ColorFilter = std::function; + class LOT_EXPORT Animation { public: @@ -301,6 +304,23 @@ public: loadFromData(std::string jsonData, const std::string &key, const std::string &resourcePath="", bool cachePolicy=true); + /** + * @brief Constructs an animation object from JSON string data and update. + * the color properties using ColorFilter. + + * @param[in] jsonData The JSON string data. + * @param[in] resourcePath the path will be used to search for external resource. + * @param[in] filter The color filter that will be applied for each color property + * found during parsing. + + * @return Animation object that can render the contents of the + * Lottie resource represented by JSON string data. + * + * @internal + */ + static std::unique_ptr + loadFromData(std::string jsonData, std::string resourcePath, ColorFilter filter); + /** * @brief Returns default framerate of the Lottie resource. * diff --git a/src/lottie/lottieanimation.cpp b/src/lottie/lottieanimation.cpp index ef93930..165f8ea 100644 --- a/src/lottie/lottieanimation.cpp +++ b/src/lottie/lottieanimation.cpp @@ -256,8 +256,23 @@ std::unique_ptr Animation::loadFromData( } LottieLoader loader; - if (loader.loadFromData(std::move(jsonData), key, - (resourcePath.empty() ? " " : resourcePath), cachePolicy)) { + if (loader.loadFromData(std::move(jsonData), key, resourcePath, cachePolicy)) { + auto animation = std::unique_ptr(new Animation); + animation->d->init(loader.model()); + return animation; + } + return nullptr; +} + +std::unique_ptr Animation::loadFromData( std::string jsonData, std::string resourcePath, ColorFilter filter) +{ + if (jsonData.empty()) { + vWarning << "jason data is empty"; + return nullptr; + } + + LottieLoader loader; + if (loader.loadFromData(std::move(jsonData), std::move(resourcePath), std::move(filter))) { auto animation = std::unique_ptr(new Animation); animation->d->init(loader.model()); return animation; diff --git a/src/lottie/lottieloader.cpp b/src/lottie/lottieloader.cpp index 82e21e3..1783b0e 100644 --- a/src/lottie/lottieloader.cpp +++ b/src/lottie/lottieloader.cpp @@ -21,6 +21,7 @@ #include #include +#include #ifdef LOTTIE_CACHE_SUPPORT @@ -127,8 +128,7 @@ bool LottieLoader::load(const std::string &path, bool cachePolicy) if (content.empty()) return false; const char *str = content.c_str(); - LottieParser parser(const_cast(str), - dirname(path).c_str()); + LottieParser parser(const_cast(str), dirname(path)); mModel = parser.model(); if (!mModel) return false; @@ -140,16 +140,15 @@ bool LottieLoader::load(const std::string &path, bool cachePolicy) return true; } -bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key, - const std::string &resourcePath, bool cachePolicy) +bool LottieLoader::loadFromData(std::string jsonData, const std::string &key, + std::string resourcePath, bool cachePolicy) { if (cachePolicy) { mModel = LottieModelCache::instance().find(key); if (mModel) return true; } - LottieParser parser(const_cast(jsonData.c_str()), - resourcePath.c_str()); + LottieParser parser(const_cast(jsonData.c_str()), std::move(resourcePath)); mModel = parser.model(); if (!mModel) return false; @@ -160,6 +159,14 @@ bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key, return true; } +bool LottieLoader::loadFromData(std::string jsonData, std::string resourcePath, ColorFilter filter) +{ + LottieParser parser(const_cast(jsonData.c_str()), std::move(resourcePath), std::move(filter)); + mModel = parser.model(); + + return mModel ? true : false; +} + std::shared_ptr LottieLoader::model() { return mModel; diff --git a/src/lottie/lottieloader.h b/src/lottie/lottieloader.h index 4d4646d..084bbea 100644 --- a/src/lottie/lottieloader.h +++ b/src/lottie/lottieloader.h @@ -1,16 +1,16 @@ -/* +/* * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -19,19 +19,21 @@ #ifndef LOTTIELOADER_H #define LOTTIELOADER_H -#include #include +#include class LOTModel; class LottieLoader { public: + using ColorFilter = std::function; static void configureModelCacheSize(size_t cacheSize); bool load(const std::string &filePath, bool cachePolicy); - bool loadFromData(std::string &&jsonData, const std::string &key, - const std::string &resourcePath, bool cachePolicy); + bool loadFromData(std::string jsonData, const std::string &key, + std::string resourcePath, bool cachePolicy); + bool loadFromData(std::string jsonData, std::string resourcePath, ColorFilter filter); std::shared_ptr model(); -private: +private: std::shared_ptr mModel; }; diff --git a/src/lottie/lottieparser.cpp b/src/lottie/lottieparser.cpp index 812695d..2e2720f 100644 --- a/src/lottie/lottieparser.cpp +++ b/src/lottie/lottieparser.cpp @@ -169,8 +169,10 @@ protected: class LottieParserImpl : public LookaheadParserHandler { public: - LottieParserImpl(char *str, const char *dir_path) - : LookaheadParserHandler(str), mDirPath(dir_path) {} + LottieParserImpl(char *str, std::string dir_path, ColorFilter filter) + : LookaheadParserHandler(str), + mColorFilter(std::move(filter)), + mDirPath(std::move(dir_path)) {} bool VerifyType(); bool ParseNext(); public: @@ -263,6 +265,7 @@ public: void resolveLayerRefs(); void parsePathInfo(); private: + ColorFilter mColorFilter; struct { std::vector mInPoint; /* "i" */ std::vector mOutPoint; /* "o" */ @@ -1840,6 +1843,9 @@ void LottieParserImpl::getValue(LottieColor &color) val[i++] = value; } } + + if (mColorFilter) mColorFilter( val[0] , val[1], val[2]) ; + color.r = val[0]; color.g = val[1]; color.b = val[2]; @@ -2331,8 +2337,8 @@ public: #endif LottieParser::~LottieParser() = default; -LottieParser::LottieParser(char *str, const char *dir_path) - : d(std::make_unique(str, dir_path)) +LottieParser::LottieParser(char *str, std::string dir_path, ColorFilter filter) + : d(std::make_unique(str, std::move(dir_path), std::move(filter))) { if (d->VerifyType()) d->parseComposition(); diff --git a/src/lottie/lottieparser.h b/src/lottie/lottieparser.h index 35a8417..378719f 100644 --- a/src/lottie/lottieparser.h +++ b/src/lottie/lottieparser.h @@ -1,16 +1,16 @@ -/* +/* * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -21,12 +21,14 @@ #include "lottiemodel.h" #include +#include +using ColorFilter = std::function; class LottieParserImpl; class LottieParser { public: ~LottieParser(); - LottieParser(char* str, const char *dir_path); + LottieParser(char* str, std::string dir_path, ColorFilter filter = {}); std::shared_ptr model(); private: std::unique_ptr d;