Fix webp&gif issue
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-url.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/visual-url.h>
19
20 // EXTERNAL HEADERS
21 #include <cstring> // for toupper()
22
23 namespace Dali
24 {
25 namespace Toolkit
26 {
27 namespace Internal
28 {
29 namespace
30 {
31 VisualUrl::ProtocolType ResolveLocation(const std::string& url)
32 {
33   const char*    urlCStr = url.c_str();
34   const uint32_t length  = url.size();
35   if((length > 7) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/')
36   {
37     // https:// or enbuf://
38     const char hOre = tolower(urlCStr[0]);
39     const char tOrn = tolower(urlCStr[1]);
40     const char tOrb = tolower(urlCStr[2]);
41     const char pOru = tolower(urlCStr[3]);
42     const char sOrf = tolower(urlCStr[4]);
43     if(('h' == hOre) &&
44        ('t' == tOrn) &&
45        ('t' == tOrb) &&
46        ('p' == pOru) &&
47        ('s' == sOrf))
48     {
49       return VisualUrl::REMOTE;
50     }
51     if(('e' == hOre) &&
52        ('n' == tOrn) &&
53        ('b' == tOrb) &&
54        ('u' == pOru) &&
55        ('f' == sOrf))
56     {
57       return VisualUrl::BUFFER;
58     }
59   }
60   else if((length > 6) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/')
61   {
62     // http:// or dali://
63     const char hOrd = tolower(urlCStr[0]);
64     const char tOra = tolower(urlCStr[1]);
65     const char tOrl = tolower(urlCStr[2]);
66     const char pOri = tolower(urlCStr[3]);
67     if(('h' == hOrd) &&
68        ('t' == tOra) &&
69        ('t' == tOrl) &&
70        ('p' == pOri))
71     {
72       return VisualUrl::REMOTE;
73     }
74     if(('d' == hOrd) &&
75        ('a' == tOra) &&
76        ('l' == tOrl) &&
77        ('i' == pOri))
78     {
79       return VisualUrl::TEXTURE;
80     }
81   }
82   else if((length > 5) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/')
83   {
84     // ftp:// or ssh://
85     const char fOrs = tolower(urlCStr[0]);
86     const char tOrs = tolower(urlCStr[1]);
87     const char pOrh = tolower(urlCStr[2]);
88     if(('f' == fOrs) &&
89        ('t' == tOrs) &&
90        ('p' == pOrh))
91     {
92       return VisualUrl::REMOTE;
93     }
94     if(('s' == fOrs) &&
95        ('s' == tOrs) &&
96        ('h' == pOrh))
97     {
98       return VisualUrl::REMOTE;
99     }
100   }
101   return VisualUrl::LOCAL;
102 }
103
104 VisualUrl::Type ResolveType(const std::string& url)
105 {
106   // if only one char in string, can only be regular image
107   const std::size_t count = url.size();
108   VisualUrl::Type  returnType = VisualUrl::REGULAR_IMAGE;
109   if(count > 0)
110   {
111     // parsing from the end for better chance of early outs
112     enum
113     {
114       SUFFIX,
115       HASH,
116       HASH_DOT
117     } state                = SUFFIX;
118     char         SVG[4]    = {'g', 'v', 's', '.'};
119     char         GIF[4]    = {'f', 'i', 'g', '.'};
120     char         WEBP[5]   = {'p', 'b', 'e', 'w', '.'};
121     char         JSON[5]   = {'n', 'o', 's', 'j', '.'};
122     char         TVG[4]    = {'g', 'v', 't', '.'};
123     unsigned int svgScore  = 0;
124     unsigned int tvgScore  = 0;
125     unsigned int gifScore  = 0;
126     unsigned int webpScore = 0;
127     unsigned int jsonScore = 0;
128     int          index     = count;
129     while(--index >= 0)
130     {
131       const char        currentChar   = tolower(url[index]);
132       const std::size_t offsetFromEnd = count - index - 1u;
133       if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
134       {
135         // early out if SVG as can't be used in N patch for now
136         if(++svgScore == sizeof(SVG))
137         {
138           return VisualUrl::SVG;
139         }
140       }
141       if((offsetFromEnd < sizeof(TVG)) && (currentChar == TVG[offsetFromEnd]))
142       {
143         // early out if TVG as can't be used in N patch for now
144         if(++tvgScore == sizeof(TVG))
145         {
146           return VisualUrl::TVG;
147         }
148       }
149       if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
150       {
151         //find type, but need to be check used in N patch
152         if(++gifScore == sizeof(GIF))
153         {
154           returnType = VisualUrl::GIF;
155         }
156       }
157       if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
158       {
159         if(++webpScore == sizeof(WEBP))
160         {
161           //find type, but need to be check used in N patch
162           returnType = VisualUrl::WEBP;
163         }
164       }
165       if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
166       {
167         // early out if JSON as can't be used in N patch for now
168         if(++jsonScore == sizeof(JSON))
169         {
170           return VisualUrl::JSON;
171         }
172       }
173       switch(state)
174       {
175         case SUFFIX:
176         {
177           if('.' == currentChar)
178           {
179             state = HASH;
180           }
181           break;
182         }
183         case HASH:
184         {
185           if(('#' == currentChar) || ('9' == currentChar))
186           {
187             state = HASH_DOT;
188           }
189           else
190           {
191             // early out, not a valid N/9-patch URL
192             return returnType;
193           }
194           break;
195         }
196         case HASH_DOT:
197         {
198           if('.' == currentChar)
199           {
200             return VisualUrl::N_PATCH;
201           }
202           else
203           {
204             // early out, not a valid N/9-patch URL
205             return returnType;
206           }
207           break;
208         }
209       }
210     }
211   }
212   // if we got here it is a regular image
213   return returnType;
214 }
215
216 } // namespace
217
218 VisualUrl::VisualUrl()
219 : mUrl(),
220   mType(VisualUrl::REGULAR_IMAGE),
221   mLocation(VisualUrl::LOCAL)
222 {
223 }
224
225 VisualUrl::VisualUrl(const std::string& url)
226 : mUrl(url),
227   mType(VisualUrl::REGULAR_IMAGE),
228   mLocation(VisualUrl::LOCAL)
229 {
230   if(!url.empty())
231   {
232     mLocation = ResolveLocation(url);
233     if(VisualUrl::TEXTURE != mLocation && VisualUrl::BUFFER != mLocation)
234     {
235       // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine
236       mType = ResolveType(url);
237     }
238   }
239 }
240
241 VisualUrl::VisualUrl(const VisualUrl& url)
242 : mUrl(url.mUrl),
243   mType(url.mType),
244   mLocation(url.mLocation)
245 {
246 }
247
248 VisualUrl::~VisualUrl()
249 {
250 }
251
252 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
253 {
254   if(&url != this)
255   {
256     mUrl      = url.mUrl;
257     mType     = url.mType;
258     mLocation = url.mLocation;
259   }
260   return *this;
261 }
262
263 const std::string& VisualUrl::GetUrl() const
264 {
265   return mUrl;
266 }
267
268 VisualUrl::Type VisualUrl::GetType() const
269 {
270   return mType;
271 }
272
273 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
274 {
275   return mLocation;
276 }
277
278 bool VisualUrl::IsValid() const
279 {
280   return mUrl.size() > 0u;
281 }
282
283 bool VisualUrl::IsLocalResource() const
284 {
285   return mLocation == VisualUrl::LOCAL;
286 }
287
288 bool VisualUrl::IsBufferResource() const
289 {
290   return mLocation == VisualUrl::BUFFER;
291 }
292
293 std::string VisualUrl::GetLocation() const
294 {
295   return GetLocation(mUrl);
296 }
297
298 std::string VisualUrl::CreateTextureUrl(const std::string& location)
299 {
300   return "dali://" + location;
301 }
302
303 std::string VisualUrl::CreateBufferUrl(const std::string& location)
304 {
305   return "enbuf://" + location;
306 }
307
308 VisualUrl::ProtocolType VisualUrl::GetProtocolType(const std::string& url)
309 {
310   return ResolveLocation(url);
311 }
312
313 std::string VisualUrl::GetLocation(const std::string& url)
314 {
315   const auto location = url.find("://");
316   if(std::string::npos != location)
317   {
318     return url.substr(location + 3u); // 3 characters forwards from the start of ://
319   }
320   return url;
321 }
322
323 } // namespace Internal
324
325 } // namespace Toolkit
326
327 } // namespace Dali