| 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 "lottieloader.h" |
| 20 | #include "lottieparser.h" |
| 21 |
| 22 | #include <cstring> |
| 23 | #include <fstream> |
| 24 | #include <unordered_map> |
| 25 | using namespace std; |
| 26 |
| 27 | #ifdef LOTTIE_CACHE_SUPPORT |
| 28 |
| 29 | class LottieFileCache { |
| 30 | public: |
| 31 | static LottieFileCache &instance() |
| 32 | { |
| 33 | static LottieFileCache CACHE; |
| 34 | return CACHE; |
| 35 | } |
| 36 | std::shared_ptr<LOTModel> find(const std::string &key) |
| 37 | { |
| 38 | auto search = mHash.find(key); |
| 39 | if (search != mHash.end()) { |
| 40 | return search->second; |
| 41 | } else { |
| 42 | return nullptr; |
| 43 | } |
| 44 | } |
| 45 | void add(const std::string &key, std::shared_ptr<LOTModel> value) |
| 46 | { |
| 47 | mHash[key] = std::move(value); |
| 48 | } |
| 49 |
| 50 | private: |
| 51 | LottieFileCache() = default; |
| 52 |
| 53 | std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash; |
| 54 | }; |
| 55 |
| 56 | #else |
| 57 |
| 58 | class LottieFileCache { |
| 59 | public: |
| 60 | static LottieFileCache &instance() |
| 61 | { |
| 62 | static LottieFileCache CACHE; |
| 63 | return CACHE; |
| 64 | } |
| 65 | std::shared_ptr<LOTModel> find(const std::string &) { return nullptr; } |
| 66 | void add(const std::string &, std::shared_ptr<LOTModel>) {} |
| 67 | }; |
| 68 |
| 69 | #endif |
| 70 |
| 71 | static std::string dirname(const std::string &path) |
| 72 | { |
| 73 | const char *ptr = strrchr(path.c_str(), '/'); |
| 74 | int len = int(ptr + 1 - path.c_str()); // +1 to include '/' |
| 75 | return std::string(path, 0, len); |
| 76 | } |
| 77 |
| 78 | bool LottieLoader::load(const std::string &path, std::map<int32_t, int32_t> *colorReplacement, rlottie::FitzModifier fitzModifier) |
| 79 | { |
| 80 | mModel = LottieFileCache::instance().find(path); |
| 81 | if (mModel) return true; |
| 82 |
| 83 | std::ifstream f; |
| 84 | f.open(path); |
| 85 |
| 86 | if (!f.is_open()) { |
| 87 | vCritical << "failed to open file = " << path.c_str(); |
| 88 | return false; |
| 89 | } else { |
| 90 | std::string content; |
| 91 |
| 92 | std::getline(f, content, '\0') ; |
| 93 | f.close(); |
| 94 |
| 95 | if (content.empty()) return false; |
| 96 |
| 97 | const char *str = content.c_str(); |
| 98 | LottieParser parser(const_cast<char *>(str), dirname(path).c_str(), colorReplacement, fitzModifier); |
| 99 | if (parser.hasParsingError()) { |
| 100 | return false; |
| 101 | } |
| 102 | mModel = parser.model(); |
| 103 |
| 104 | if (!mModel) return false; |
| 105 |
| 106 | LottieFileCache::instance().add(path, mModel); |
| 107 | } |
| 108 |
| 109 | return true; |
| 110 | } |
| 111 |
| 112 | bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key, |
| 113 | std::map<int32_t, int32_t> *colorReplacement, |
| 114 | const std::string &resourcePath, rlottie::FitzModifier fitzModifier) |
| 115 | { |
| 116 | mModel = LottieFileCache::instance().find(key); |
| 117 | if (mModel) return true; |
| 118 |
| 119 | LottieParser parser(const_cast<char *>(jsonData.c_str()), resourcePath.c_str(), colorReplacement, fitzModifier); |
| 120 | mModel = parser.model(); |
| 121 |
| 122 | if (!mModel) return false; |
| 123 |
| 124 | LottieFileCache::instance().add(key, mModel); |
| 125 |
| 126 | return true; |
| 127 | } |
| 128 |
| 129 | std::shared_ptr<LOTModel> LottieLoader::model() |
| 130 | { |
| 131 | return mModel; |
| 132 | } |
| 133 |