From 6a59e1b71554a8db2c8978110501cad890b61ee4 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 11 Aug 2023 12:25:06 +0900 Subject: [PATCH] loader: avoid attempting forceful loading when the given MIME type is not correct. Loading images when the MIME type is incorrect is unnecessary and often results in failure, adding unnecessary processing overhead. Pictures should have a clear MIME type assigned by users. --- inc/thorvg.h | 3 ++- src/lib/tvgLoader.cpp | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index d6afd92d..0c5ad62b 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -1270,7 +1270,7 @@ public: * * @param[in] data A pointer to a memory location where the content of the picture file is stored. * @param[in] size The size in bytes of the memory occupied by the @p data. - * @param[in] mimeType Mimetype or extension of data such as "jpg", "jpeg", "svg", "svg+xml", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one. + * @param[in] mimeType Mimetype or extension of data such as "jpg", "jpeg", "lottie", "svg", "svg+xml", "png", etc. In case an empty string or an unknown type is provided, the loaders will be tried one by one. * @param[in] copy If @c true the data are copied into the engine local buffer, otherwise they are not. * * @retval Result::Success When succeed. @@ -1280,6 +1280,7 @@ public: * * @warning: It's the user responsibility to release the @p data memory if the @p copy is @c true. * + * @note If you are unsure about the MIME type, you can provide an empty value like @c "", and thorvg will attempt to figure it out. * @since 0.5 */ Result load(const char* data, uint32_t size, const std::string& mimeType, bool copy = false) noexcept; diff --git a/src/lib/tvgLoader.cpp b/src/lib/tvgLoader.cpp index 16b604c8..8ed0d575 100644 --- a/src/lib/tvgLoader.cpp +++ b/src/lib/tvgLoader.cpp @@ -164,7 +164,7 @@ static LoadModule* _findByType(const string& mimeType) if (mimeType == "tvg") type = FileType::Tvg; else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg; - else if (mimeType == "lottie" || mimeType == "json") type = FileType::Lottie; + else if (mimeType == "lottie") type = FileType::Lottie; else if (mimeType == "raw") type = FileType::Raw; else if (mimeType == "png") type = FileType::Png; else if (mimeType == "jpg" || mimeType == "jpeg") type = FileType::Jpg; @@ -214,22 +214,24 @@ shared_ptr LoaderMgr::loader(const string& path, bool* invalid) shared_ptr LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy) { - //Try first with the given MimeType - if (auto loader = _findByType(mimeType)) { - if (loader->open(data, size, copy)) { - return shared_ptr(loader); - } else { - TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported. Will try again with other types.", mimeType.c_str()); - delete(loader); + //Try with the given MimeType + if (!mimeType.empty()) { + if (auto loader = _findByType(mimeType)) { + if (loader->open(data, size, copy)) { + return shared_ptr(loader); + } else { + TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported. Will try again with other types.", mimeType.c_str()); + delete(loader); + } } - } - - //Abnormal MimeType. Try with the candidates in the order - for (int i = 0; i < static_cast(FileType::Unknown); i++) { - auto loader = _find(static_cast(i)); - if (loader) { - if (loader->open(data, size, copy)) return shared_ptr(loader); - else delete(loader); + //Unkown MimeType. Try with the candidates in the order + } else { + for (int i = 0; i < static_cast(FileType::Unknown); i++) { + auto loader = _find(static_cast(i)); + if (loader) { + if (loader->open(data, size, copy)) return shared_ptr(loader); + else delete(loader); + } } } return nullptr;