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.
This commit is contained in:
Hermet Park 2023-08-11 12:25:06 +09:00 committed by Hermet Park
parent 374a125280
commit 6a59e1b715
2 changed files with 20 additions and 17 deletions

View file

@ -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;

View file

@ -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<LoadModule> LoaderMgr::loader(const string& path, bool* invalid)
shared_ptr<LoadModule> 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<LoadModule>(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<LoadModule>(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<int>(FileType::Unknown); i++) {
auto loader = _find(static_cast<FileType>(i));
if (loader) {
if (loader->open(data, size, copy)) return shared_ptr<LoadModule>(loader);
else delete(loader);
//Unkown MimeType. Try with the candidates in the order
} else {
for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
auto loader = _find(static_cast<FileType>(i));
if (loader) {
if (loader->open(data, size, copy)) return shared_ptr<LoadModule>(loader);
else delete(loader);
}
}
}
return nullptr;