2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 #include "tvgCommon.h"
23 #include "tvgSaveModule.h"
25 #ifdef THORVG_TVG_SAVER_SUPPORT
26 #include "tvgTvgSaver.h"
29 /************************************************************************/
30 /* Internal Class Implementation */
31 /************************************************************************/
35 SaveModule* saveModule = nullptr;
38 if (saveModule) delete(saveModule);
43 static SaveModule* _find(FileType type)
47 #ifdef THORVG_TVG_SAVER_SUPPORT
57 #ifdef THORVG_LOG_ENABLED
69 TVGLOG("SAVER", "%s format is not supported", format);
75 static SaveModule* _find(const string& path)
77 auto ext = path.substr(path.find_last_of(".") + 1);
78 if (!ext.compare("tvg")) {
79 return _find(FileType::Tvg);
85 /************************************************************************/
86 /* External Class Implementation */
87 /************************************************************************/
89 Saver::Saver() : pImpl(new Impl())
100 Result Saver::save(std::unique_ptr<Paint> paint, const string& path, bool compress) noexcept
102 auto p = paint.release();
103 if (!p) return Result::MemoryCorruption;
105 //Already on saving an other resource.
106 if (pImpl->saveModule) {
108 return Result::InsufficientCondition;
111 if (auto saveModule = _find(path)) {
112 if (saveModule->save(p, path, compress)) {
113 pImpl->saveModule = saveModule;
114 return Result::Success;
118 return Result::Unknown;
122 return Result::NonSupport;
126 Result Saver::sync() noexcept
128 if (!pImpl->saveModule) return Result::InsufficientCondition;
129 pImpl->saveModule->close();
130 delete(pImpl->saveModule);
131 pImpl->saveModule = nullptr;
133 return Result::Success;
137 unique_ptr<Saver> Saver::gen() noexcept
139 return unique_ptr<Saver>(new Saver);