renderer: make the file io configurable

certain systems, may not support file I/O operations.
ThorVG should provide users with an option to configure
builds according to their requirements.

This ensures that file I/O calls are avoided,
preventing potential crashes.

Please use the meson '-Dfile=true/false' option for this.

Please note that "THORVG_FILE_IO_SUPPORT" might be expected
for your thorvg manual build.

issue: https://github.com/thorvg/thorvg/issues/3008
This commit is contained in:
Hermet Park 2024-11-29 22:31:45 +09:00
parent c0aab8c738
commit 52cf31a79d
14 changed files with 75 additions and 2 deletions

View file

@ -127,6 +127,12 @@ if get_option('log')
config_h.set10('THORVG_LOG_ENABLED', true)
endif
#File IO
if get_option('file') == true
config_h.set10('THORVG_FILE_IO_SUPPORT', true)
endif
#Extra
lottie_expressions = lottie_loader and get_option('extra').contains('lottie_expressions')

View file

@ -58,6 +58,11 @@ option('static',
value: false,
description: 'Force to use static linking modules in thorvg')
option('file',
type: 'boolean',
value: true,
description: 'Enable File IO calls in thorvg')
option('extra',
type: 'array',
choices: ['', 'lottie_expressions'],

View file

@ -59,6 +59,7 @@ JpgLoader::~JpgLoader()
bool JpgLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
auto jpegFile = fopen(path.c_str(), "rb");
if (!jpegFile) return false;
@ -94,6 +95,9 @@ failure:
finalize:
fclose(jpegFile);
return ret;
#else
return false;
#endif
}

View file

@ -67,6 +67,7 @@ bool PngLoader::open(const string& path)
bool PngLoader::open(const char* data, uint32_t size, bool copy)
{
#ifdef THORVG_FILE_IO_SUPPORT
image->opaque = NULL;
if (!png_image_begin_read_from_memory(image, data, size)) return false;
@ -75,6 +76,9 @@ bool PngLoader::open(const char* data, uint32_t size, bool copy)
h = (float)image->height;
return true;
#else
return false;
#endif
}

View file

@ -68,6 +68,7 @@ WebpLoader::~WebpLoader()
bool WebpLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
auto webpFile = fopen(path.c_str(), "rb");
if (!webpFile) return false;
@ -96,6 +97,9 @@ bool WebpLoader::open(const string& path)
finalize:
fclose(webpFile);
return ret;
#else
return false;
#endif
}

View file

@ -70,6 +70,7 @@ JpgLoader::~JpgLoader()
bool JpgLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
int width, height;
decoder = jpgdHeader(path.c_str(), &width, &height);
if (!decoder) return false;
@ -78,6 +79,9 @@ bool JpgLoader::open(const string& path)
h = static_cast<float>(height);
return true;
#else
return false;
#endif
}

View file

@ -219,6 +219,7 @@ bool LottieLoader::open(const char* data, uint32_t size, bool copy)
bool LottieLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
auto f = fopen(path.c_str(), "r");
if (!f) return false;
@ -246,6 +247,9 @@ bool LottieLoader::open(const string& path)
this->copy = true;
return header();
#else
return false;
#endif
}

View file

@ -70,6 +70,7 @@ PngLoader::~PngLoader()
bool PngLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
auto pngFile = fopen(path.c_str(), "rb");
if (!pngFile) return false;
@ -102,6 +103,9 @@ bool PngLoader::open(const string& path)
finalize:
fclose(pngFile);
return ret;
#else
return false;
#endif
}

View file

@ -3973,6 +3973,7 @@ bool SvgLoader::open(const char* data, uint32_t size, bool copy)
bool SvgLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
clear();
ifstream f;
@ -3990,6 +3991,9 @@ bool SvgLoader::open(const string& path)
size = filePath.size();
return header();
#else
return false;
#endif
}

View file

@ -36,6 +36,8 @@
/* Internal Class Implementation */
/************************************************************************/
#ifdef THORVG_FILE_IO_SUPPORT
#if defined(_WIN32) && (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
static bool _map(TtfLoader* loader, const string& path)
@ -155,6 +157,8 @@ static void _unmap(TtfLoader* loader)
}
#endif
#endif //THORVG_FILE_IO_SUPPORT
static uint32_t* _codepoints(const char* text, size_t n)
{
@ -199,7 +203,11 @@ void TtfLoader::clear()
reader.size = 0;
freeData = false;
nomap = false;
} else _unmap(this);
} else {
#ifdef THORVG_FILE_IO_SUPPORT
_unmap(this);
#endif
}
shape = nullptr;
}
@ -236,9 +244,13 @@ TtfLoader::~TtfLoader()
bool TtfLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
clear();
if (!_map(this, path)) return false;
return reader.header();
#else
return false;
#endif
}

View file

@ -71,6 +71,7 @@ WebpLoader::~WebpLoader()
bool WebpLoader::open(const string& path)
{
#ifdef THORVG_FILE_IO_SUPPORT
auto f = fopen(path.c_str(), "rb");
if (!f) return false;
@ -101,6 +102,9 @@ bool WebpLoader::open(const string& path)
freeData = true;
return true;
#else
return false;
#endif
}

View file

@ -172,6 +172,7 @@ static LoadModule* _find(FileType type)
}
#ifdef THORVG_FILE_IO_SUPPORT
static LoadModule* _findByPath(const string& path)
{
auto ext = path.substr(path.find_last_of(".") + 1);
@ -185,6 +186,7 @@ static LoadModule* _findByPath(const string& path)
if (!ext.compare("otf") || !ext.compare("otc")) return _find(FileType::Ttf);
return nullptr;
}
#endif
static FileType _convert(const string& mimeType)
@ -292,6 +294,7 @@ bool LoaderMgr::retrieve(LoadModule* loader)
LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
{
#ifdef THORVG_FILE_IO_SUPPORT
*invalid = false;
//TODO: svg & lottie is not sharable.
@ -335,6 +338,7 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
}
}
*invalid = true;
#endif
return nullptr;
}

View file

@ -164,9 +164,14 @@ Type Picture::type() const noexcept
Result Picture::load(const std::string& path) noexcept
{
#ifdef THORVG_FILE_IO_SUPPORT
if (path.empty()) return Result::InvalidArguments;
return pImpl->load(path);
#else
TVGLOG("RENDERER", "FILE IO is disabled!");
return Result::NonSupport;
#endif
}

View file

@ -60,13 +60,17 @@ Result Text::font(const char* name, float size, const char* style) noexcept
Result Text::load(const std::string& path) noexcept
{
#ifdef THORVG_FILE_IO_SUPPORT
bool invalid; //invalid path
if (!LoaderMgr::loader(path, &invalid)) {
if (invalid) return Result::InvalidArguments;
else return Result::NonSupport;
}
return Result::Success;
#else
TVGLOG("RENDERER", "FILE IO is disabled!");
return Result::NonSupport;
#endif
}
@ -87,8 +91,13 @@ Result Text::load(const char* name, const char* data, uint32_t size, const strin
Result Text::unload(const std::string& path) noexcept
{
#ifdef THORVG_FILE_IO_SUPPORT
if (LoaderMgr::retrieve(path)) return Result::Success;
return Result::InsufficientCondition;
#else
TVGLOG("RENDERER", "FILE IO is disabled!");
return Result::NonSupport;
#endif
}