common: nothing was rendered after an empty masked node came across
[platform/core/graphics/tizenvg.git] / src / lib / tvgSaver.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
3
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:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
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
20  * SOFTWARE.
21  */
22 #include "tvgCommon.h"
23 #include "tvgSaveModule.h"
24
25 #ifdef THORVG_TVG_SAVER_SUPPORT
26     #include "tvgTvgSaver.h"
27 #endif
28
29 /************************************************************************/
30 /* Internal Class Implementation                                        */
31 /************************************************************************/
32
33 struct Saver::Impl
34 {
35     SaveModule* saveModule = nullptr;
36     ~Impl()
37     {
38         if (saveModule) delete(saveModule);
39     }
40 };
41
42
43 static SaveModule* _find(FileType type)
44 {
45     switch(type) {
46         case FileType::Tvg: {
47 #ifdef THORVG_TVG_SAVER_SUPPORT
48             return new TvgSaver;
49 #endif
50             break;
51         }
52         default: {
53             break;
54         }
55     }
56
57 #ifdef THORVG_LOG_ENABLED
58     const char *format;
59     switch(type) {
60         case FileType::Tvg: {
61             format = "TVG";
62             break;
63         }
64         default: {
65             format = "???";
66             break;
67         }
68     }
69     TVGLOG("SAVER", "%s format is not supported", format);
70 #endif
71     return nullptr;
72 }
73
74
75 static SaveModule* _find(const string& path)
76 {
77     auto ext = path.substr(path.find_last_of(".") + 1);
78     if (!ext.compare("tvg")) {
79         return _find(FileType::Tvg);
80     }
81     return nullptr;
82 }
83
84
85 /************************************************************************/
86 /* External Class Implementation                                        */
87 /************************************************************************/
88
89 Saver::Saver() : pImpl(new Impl())
90 {
91 }
92
93
94 Saver::~Saver()
95 {
96     delete(pImpl);
97 }
98
99
100 Result Saver::save(std::unique_ptr<Paint> paint, const string& path, bool compress) noexcept
101 {
102     auto p = paint.release();
103     if (!p) return Result::MemoryCorruption;
104
105     //Already on saving an other resource.
106     if (pImpl->saveModule) {
107         delete(p);
108         return Result::InsufficientCondition;
109     }
110
111     if (auto saveModule = _find(path)) {
112         if (saveModule->save(p, path, compress)) {
113             pImpl->saveModule = saveModule;
114             return Result::Success;
115         } else {
116             delete(p);
117             delete(saveModule);
118             return Result::Unknown;
119         }
120     }
121     delete(p);
122     return Result::NonSupport;
123 }
124
125
126 Result Saver::sync() noexcept
127 {
128     if (!pImpl->saveModule) return Result::InsufficientCondition;
129     pImpl->saveModule->close();
130     delete(pImpl->saveModule);
131     pImpl->saveModule = nullptr;
132
133     return Result::Success;
134 }
135
136
137 unique_ptr<Saver> Saver::gen() noexcept
138 {
139     return unique_ptr<Saver>(new Saver);
140 }