mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
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:
parent
c0aab8c738
commit
52cf31a79d
14 changed files with 75 additions and 2 deletions
|
@ -127,6 +127,12 @@ if get_option('log')
|
||||||
config_h.set10('THORVG_LOG_ENABLED', true)
|
config_h.set10('THORVG_LOG_ENABLED', true)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
#File IO
|
||||||
|
if get_option('file') == true
|
||||||
|
config_h.set10('THORVG_FILE_IO_SUPPORT', true)
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
#Extra
|
#Extra
|
||||||
lottie_expressions = lottie_loader and get_option('extra').contains('lottie_expressions')
|
lottie_expressions = lottie_loader and get_option('extra').contains('lottie_expressions')
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,11 @@ option('static',
|
||||||
value: false,
|
value: false,
|
||||||
description: 'Force to use static linking modules in thorvg')
|
description: 'Force to use static linking modules in thorvg')
|
||||||
|
|
||||||
|
option('file',
|
||||||
|
type: 'boolean',
|
||||||
|
value: true,
|
||||||
|
description: 'Enable File IO calls in thorvg')
|
||||||
|
|
||||||
option('extra',
|
option('extra',
|
||||||
type: 'array',
|
type: 'array',
|
||||||
choices: ['', 'lottie_expressions'],
|
choices: ['', 'lottie_expressions'],
|
||||||
|
|
|
@ -59,6 +59,7 @@ JpgLoader::~JpgLoader()
|
||||||
|
|
||||||
bool JpgLoader::open(const string& path)
|
bool JpgLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
auto jpegFile = fopen(path.c_str(), "rb");
|
auto jpegFile = fopen(path.c_str(), "rb");
|
||||||
if (!jpegFile) return false;
|
if (!jpegFile) return false;
|
||||||
|
|
||||||
|
@ -94,6 +95,9 @@ failure:
|
||||||
finalize:
|
finalize:
|
||||||
fclose(jpegFile);
|
fclose(jpegFile);
|
||||||
return ret;
|
return ret;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,7 @@ bool PngLoader::open(const string& path)
|
||||||
|
|
||||||
bool PngLoader::open(const char* data, uint32_t size, bool copy)
|
bool PngLoader::open(const char* data, uint32_t size, bool copy)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
image->opaque = NULL;
|
image->opaque = NULL;
|
||||||
|
|
||||||
if (!png_image_begin_read_from_memory(image, data, size)) return false;
|
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;
|
h = (float)image->height;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -68,6 +68,7 @@ WebpLoader::~WebpLoader()
|
||||||
|
|
||||||
bool WebpLoader::open(const string& path)
|
bool WebpLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
auto webpFile = fopen(path.c_str(), "rb");
|
auto webpFile = fopen(path.c_str(), "rb");
|
||||||
if (!webpFile) return false;
|
if (!webpFile) return false;
|
||||||
|
|
||||||
|
@ -96,6 +97,9 @@ bool WebpLoader::open(const string& path)
|
||||||
finalize:
|
finalize:
|
||||||
fclose(webpFile);
|
fclose(webpFile);
|
||||||
return ret;
|
return ret;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,7 @@ JpgLoader::~JpgLoader()
|
||||||
|
|
||||||
bool JpgLoader::open(const string& path)
|
bool JpgLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
int width, height;
|
int width, height;
|
||||||
decoder = jpgdHeader(path.c_str(), &width, &height);
|
decoder = jpgdHeader(path.c_str(), &width, &height);
|
||||||
if (!decoder) return false;
|
if (!decoder) return false;
|
||||||
|
@ -78,6 +79,9 @@ bool JpgLoader::open(const string& path)
|
||||||
h = static_cast<float>(height);
|
h = static_cast<float>(height);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -219,6 +219,7 @@ bool LottieLoader::open(const char* data, uint32_t size, bool copy)
|
||||||
|
|
||||||
bool LottieLoader::open(const string& path)
|
bool LottieLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
auto f = fopen(path.c_str(), "r");
|
auto f = fopen(path.c_str(), "r");
|
||||||
if (!f) return false;
|
if (!f) return false;
|
||||||
|
|
||||||
|
@ -246,6 +247,9 @@ bool LottieLoader::open(const string& path)
|
||||||
this->copy = true;
|
this->copy = true;
|
||||||
|
|
||||||
return header();
|
return header();
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,7 @@ PngLoader::~PngLoader()
|
||||||
|
|
||||||
bool PngLoader::open(const string& path)
|
bool PngLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
auto pngFile = fopen(path.c_str(), "rb");
|
auto pngFile = fopen(path.c_str(), "rb");
|
||||||
if (!pngFile) return false;
|
if (!pngFile) return false;
|
||||||
|
|
||||||
|
@ -102,6 +103,9 @@ bool PngLoader::open(const string& path)
|
||||||
finalize:
|
finalize:
|
||||||
fclose(pngFile);
|
fclose(pngFile);
|
||||||
return ret;
|
return ret;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3973,6 +3973,7 @@ bool SvgLoader::open(const char* data, uint32_t size, bool copy)
|
||||||
|
|
||||||
bool SvgLoader::open(const string& path)
|
bool SvgLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
ifstream f;
|
ifstream f;
|
||||||
|
@ -3990,6 +3991,9 @@ bool SvgLoader::open(const string& path)
|
||||||
size = filePath.size();
|
size = filePath.size();
|
||||||
|
|
||||||
return header();
|
return header();
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,8 @@
|
||||||
/* Internal Class Implementation */
|
/* Internal Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
|
|
||||||
#if defined(_WIN32) && (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
|
#if defined(_WIN32) && (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
|
||||||
|
|
||||||
static bool _map(TtfLoader* loader, const string& path)
|
static bool _map(TtfLoader* loader, const string& path)
|
||||||
|
@ -155,6 +157,8 @@ static void _unmap(TtfLoader* loader)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif //THORVG_FILE_IO_SUPPORT
|
||||||
|
|
||||||
|
|
||||||
static uint32_t* _codepoints(const char* text, size_t n)
|
static uint32_t* _codepoints(const char* text, size_t n)
|
||||||
{
|
{
|
||||||
|
@ -199,7 +203,11 @@ void TtfLoader::clear()
|
||||||
reader.size = 0;
|
reader.size = 0;
|
||||||
freeData = false;
|
freeData = false;
|
||||||
nomap = false;
|
nomap = false;
|
||||||
} else _unmap(this);
|
} else {
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
|
_unmap(this);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
shape = nullptr;
|
shape = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,9 +244,13 @@ TtfLoader::~TtfLoader()
|
||||||
|
|
||||||
bool TtfLoader::open(const string& path)
|
bool TtfLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
clear();
|
clear();
|
||||||
if (!_map(this, path)) return false;
|
if (!_map(this, path)) return false;
|
||||||
return reader.header();
|
return reader.header();
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -71,6 +71,7 @@ WebpLoader::~WebpLoader()
|
||||||
|
|
||||||
bool WebpLoader::open(const string& path)
|
bool WebpLoader::open(const string& path)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
auto f = fopen(path.c_str(), "rb");
|
auto f = fopen(path.c_str(), "rb");
|
||||||
if (!f) return false;
|
if (!f) return false;
|
||||||
|
|
||||||
|
@ -101,6 +102,9 @@ bool WebpLoader::open(const string& path)
|
||||||
freeData = true;
|
freeData = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -172,6 +172,7 @@ static LoadModule* _find(FileType type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
static LoadModule* _findByPath(const string& path)
|
static LoadModule* _findByPath(const string& path)
|
||||||
{
|
{
|
||||||
auto ext = path.substr(path.find_last_of(".") + 1);
|
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);
|
if (!ext.compare("otf") || !ext.compare("otc")) return _find(FileType::Ttf);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static FileType _convert(const string& mimeType)
|
static FileType _convert(const string& mimeType)
|
||||||
|
@ -292,6 +294,7 @@ bool LoaderMgr::retrieve(LoadModule* loader)
|
||||||
|
|
||||||
LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
|
LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
*invalid = false;
|
*invalid = false;
|
||||||
|
|
||||||
//TODO: svg & lottie is not sharable.
|
//TODO: svg & lottie is not sharable.
|
||||||
|
@ -335,6 +338,7 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*invalid = true;
|
*invalid = true;
|
||||||
|
#endif
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -164,9 +164,14 @@ Type Picture::type() const noexcept
|
||||||
|
|
||||||
Result Picture::load(const std::string& path) noexcept
|
Result Picture::load(const std::string& path) noexcept
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
if (path.empty()) return Result::InvalidArguments;
|
if (path.empty()) return Result::InvalidArguments;
|
||||||
|
|
||||||
return pImpl->load(path);
|
return pImpl->load(path);
|
||||||
|
#else
|
||||||
|
TVGLOG("RENDERER", "FILE IO is disabled!");
|
||||||
|
return Result::NonSupport;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -60,13 +60,17 @@ Result Text::font(const char* name, float size, const char* style) noexcept
|
||||||
|
|
||||||
Result Text::load(const std::string& path) noexcept
|
Result Text::load(const std::string& path) noexcept
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
bool invalid; //invalid path
|
bool invalid; //invalid path
|
||||||
if (!LoaderMgr::loader(path, &invalid)) {
|
if (!LoaderMgr::loader(path, &invalid)) {
|
||||||
if (invalid) return Result::InvalidArguments;
|
if (invalid) return Result::InvalidArguments;
|
||||||
else return Result::NonSupport;
|
else return Result::NonSupport;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result::Success;
|
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
|
Result Text::unload(const std::string& path) noexcept
|
||||||
{
|
{
|
||||||
|
#ifdef THORVG_FILE_IO_SUPPORT
|
||||||
if (LoaderMgr::retrieve(path)) return Result::Success;
|
if (LoaderMgr::retrieve(path)) return Result::Success;
|
||||||
return Result::InsufficientCondition;
|
return Result::InsufficientCondition;
|
||||||
|
#else
|
||||||
|
TVGLOG("RENDERER", "FILE IO is disabled!");
|
||||||
|
return Result::NonSupport;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue