From 52cf31a79d27e1b2141f2a732130c0b07d158615 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 29 Nov 2024 22:31:45 +0900 Subject: [PATCH] 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 --- meson.build | 6 ++++++ meson_options.txt | 5 +++++ src/loaders/external_jpg/tvgJpgLoader.cpp | 4 ++++ src/loaders/external_png/tvgPngLoader.cpp | 4 ++++ src/loaders/external_webp/tvgWebpLoader.cpp | 4 ++++ src/loaders/jpg/tvgJpgLoader.cpp | 4 ++++ src/loaders/lottie/tvgLottieLoader.cpp | 4 ++++ src/loaders/png/tvgPngLoader.cpp | 4 ++++ src/loaders/svg/tvgSvgLoader.cpp | 4 ++++ src/loaders/ttf/tvgTtfLoader.cpp | 14 +++++++++++++- src/loaders/webp/tvgWebpLoader.cpp | 4 ++++ src/renderer/tvgLoader.cpp | 4 ++++ src/renderer/tvgPicture.cpp | 5 +++++ src/renderer/tvgText.cpp | 11 ++++++++++- 14 files changed, 75 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index f3d1297e..3a696f73 100644 --- a/meson.build +++ b/meson.build @@ -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') diff --git a/meson_options.txt b/meson_options.txt index 84bde242..71ecae3d 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -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'], diff --git a/src/loaders/external_jpg/tvgJpgLoader.cpp b/src/loaders/external_jpg/tvgJpgLoader.cpp index b335847b..f00993ab 100644 --- a/src/loaders/external_jpg/tvgJpgLoader.cpp +++ b/src/loaders/external_jpg/tvgJpgLoader.cpp @@ -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 } diff --git a/src/loaders/external_png/tvgPngLoader.cpp b/src/loaders/external_png/tvgPngLoader.cpp index 49c9f6e8..71bf25a6 100644 --- a/src/loaders/external_png/tvgPngLoader.cpp +++ b/src/loaders/external_png/tvgPngLoader.cpp @@ -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 } diff --git a/src/loaders/external_webp/tvgWebpLoader.cpp b/src/loaders/external_webp/tvgWebpLoader.cpp index 3b54b44f..c1a8c6cc 100644 --- a/src/loaders/external_webp/tvgWebpLoader.cpp +++ b/src/loaders/external_webp/tvgWebpLoader.cpp @@ -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 } diff --git a/src/loaders/jpg/tvgJpgLoader.cpp b/src/loaders/jpg/tvgJpgLoader.cpp index 4e76d770..5c5fdcd3 100644 --- a/src/loaders/jpg/tvgJpgLoader.cpp +++ b/src/loaders/jpg/tvgJpgLoader.cpp @@ -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(height); return true; +#else + return false; +#endif } diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index f8ae80e1..ddf6aefb 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -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 } diff --git a/src/loaders/png/tvgPngLoader.cpp b/src/loaders/png/tvgPngLoader.cpp index 7910c1a7..828f1352 100644 --- a/src/loaders/png/tvgPngLoader.cpp +++ b/src/loaders/png/tvgPngLoader.cpp @@ -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 } diff --git a/src/loaders/svg/tvgSvgLoader.cpp b/src/loaders/svg/tvgSvgLoader.cpp index 1ab3043c..6ba433ba 100644 --- a/src/loaders/svg/tvgSvgLoader.cpp +++ b/src/loaders/svg/tvgSvgLoader.cpp @@ -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 } diff --git a/src/loaders/ttf/tvgTtfLoader.cpp b/src/loaders/ttf/tvgTtfLoader.cpp index 99c776f7..6d66907f 100644 --- a/src/loaders/ttf/tvgTtfLoader.cpp +++ b/src/loaders/ttf/tvgTtfLoader.cpp @@ -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 } diff --git a/src/loaders/webp/tvgWebpLoader.cpp b/src/loaders/webp/tvgWebpLoader.cpp index 31215aa9..d14e4085 100644 --- a/src/loaders/webp/tvgWebpLoader.cpp +++ b/src/loaders/webp/tvgWebpLoader.cpp @@ -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 } diff --git a/src/renderer/tvgLoader.cpp b/src/renderer/tvgLoader.cpp index 4cc0e82e..562102dd 100644 --- a/src/renderer/tvgLoader.cpp +++ b/src/renderer/tvgLoader.cpp @@ -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; } diff --git a/src/renderer/tvgPicture.cpp b/src/renderer/tvgPicture.cpp index 06f80207..752312c1 100644 --- a/src/renderer/tvgPicture.cpp +++ b/src/renderer/tvgPicture.cpp @@ -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 } diff --git a/src/renderer/tvgText.cpp b/src/renderer/tvgText.cpp index b324b950..a09bb302 100644 --- a/src/renderer/tvgText.cpp +++ b/src/renderer/tvgText.cpp @@ -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 }