common: nothing was rendered after an empty masked node came across
[platform/core/graphics/tizenvg.git] / src / lib / tvgLoader.cpp
1 /*
2  * Copyright (c) 2020-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 "tvgLoader.h"
23
24 #ifdef THORVG_SVG_LOADER_SUPPORT
25     #include "tvgSvgLoader.h"
26 #endif
27
28 #ifdef THORVG_PNG_LOADER_SUPPORT
29     #include "tvgPngLoader.h"
30 #endif
31
32 #ifdef THORVG_TVG_LOADER_SUPPORT
33     #include "tvgTvgLoader.h"
34 #endif
35
36 #ifdef THORVG_JPG_LOADER_SUPPORT
37     #include "tvgJpgLoader.h"
38 #endif
39
40 #include "tvgRawLoader.h"
41
42 /************************************************************************/
43 /* Internal Class Implementation                                        */
44 /************************************************************************/
45
46 static LoadModule* _find(FileType type)
47 {
48     switch(type) {
49         case FileType::Tvg: {
50 #ifdef THORVG_TVG_LOADER_SUPPORT
51             return new TvgLoader;
52 #endif
53             break;
54         }
55         case FileType::Svg: {
56 #ifdef THORVG_SVG_LOADER_SUPPORT
57             return new SvgLoader;
58 #endif
59             break;
60         }
61         case FileType::Raw: {
62             return new RawLoader;
63             break;
64         }
65         case FileType::Png: {
66 #ifdef THORVG_PNG_LOADER_SUPPORT
67             return new PngLoader;
68 #endif
69             break;
70         }
71         case FileType::Jpg: {
72 #ifdef THORVG_JPG_LOADER_SUPPORT
73             return new JpgLoader;
74 #endif
75             break;
76         }
77         default: {
78             break;
79         }
80     }
81
82 #ifdef THORVG_LOG_ENABLED
83     const char *format;
84     switch(type) {
85         case FileType::Tvg: {
86             format = "TVG";
87             break;
88         }
89         case FileType::Svg: {
90             format = "SVG";
91             break;
92         }
93         case FileType::Raw: {
94             format = "RAW";
95             break;
96         }
97         case FileType::Png: {
98             format = "PNG";
99             break;
100         }
101         case FileType::Jpg: {
102             format = "JPG";
103             break;
104         }
105         default: {
106             format = "???";
107             break;
108         }
109     }
110     TVGLOG("LOADER", "%s format is not supported", format);
111 #endif
112     return nullptr;
113 }
114
115
116 static LoadModule* _findByPath(const string& path)
117 {
118     auto ext = path.substr(path.find_last_of(".") + 1);
119     if (!ext.compare("tvg")) return _find(FileType::Tvg);
120     if (!ext.compare("svg")) return _find(FileType::Svg);
121     if (!ext.compare("png")) return _find(FileType::Png);
122     if (!ext.compare("jpg")) return _find(FileType::Jpg);
123     return nullptr;
124 }
125
126
127 static LoadModule* _findByType(const string& mimeType)
128 {
129     if (mimeType.empty()) return nullptr;
130
131     auto type = FileType::Unknown;
132
133     if (mimeType == "tvg") type = FileType::Tvg;
134     else if (mimeType == "svg" || mimeType == "svg+xml") type = FileType::Svg;
135     else if (mimeType == "raw") type = FileType::Raw;
136     else if (mimeType == "png") type = FileType::Png;
137     else if (mimeType == "jpg" || mimeType == "jpeg") type = FileType::Jpg;
138     else {
139         TVGLOG("LOADER", "Given mimetype is unknown = \"%s\".", mimeType.c_str());
140         return nullptr;
141     }
142
143     return _find(type);
144 }
145
146
147 /************************************************************************/
148 /* External Class Implementation                                        */
149 /************************************************************************/
150
151
152 bool LoaderMgr::init()
153 {
154     //TODO:
155
156     return true;
157 }
158
159
160 bool LoaderMgr::term()
161 {
162     //TODO:
163
164     return true;
165 }
166
167
168 shared_ptr<LoadModule> LoaderMgr::loader(const string& path, bool* invalid)
169 {
170     *invalid = false;
171
172     if (auto loader = _findByPath(path)) {
173         if (loader->open(path)) return shared_ptr<LoadModule>(loader);
174         else delete(loader);
175         *invalid = true;
176     }
177     return nullptr;
178 }
179
180
181 shared_ptr<LoadModule> LoaderMgr::loader(const char* data, uint32_t size, const string& mimeType, bool copy)
182 {
183     //Try first with the given MimeType
184     if (auto loader = _findByType(mimeType)) {
185         if (loader->open(data, size, copy)) {
186             return shared_ptr<LoadModule>(loader);
187         } else {
188             TVGLOG("LOADER", "Given mimetype \"%s\" seems incorrect or not supported. Will try again with other types.", mimeType.c_str());
189             delete(loader);
190         }
191     }
192
193     //Abnormal MimeType. Try with the candidates in the order
194     for (int i = 0; i < static_cast<int>(FileType::Unknown); i++) {
195         auto loader = _find(static_cast<FileType>(i));
196         if (loader) {
197             if (loader->open(data, size, copy)) return shared_ptr<LoadModule>(loader);
198             else delete(loader);
199         }
200     }
201     return nullptr;
202 }
203
204
205 shared_ptr<LoadModule> LoaderMgr::loader(const uint32_t *data, uint32_t w, uint32_t h, bool copy)
206 {
207     //function is dedicated for raw images only
208     auto loader = new RawLoader;
209     if (loader->open(data, w, h, copy)) return shared_ptr<LoadModule>(loader);
210     else delete(loader);
211
212     return nullptr;
213 }