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