2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/internal/visuals/visual-url.h>
21 #include <cstring> // for toupper()
24 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
25 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
35 VisualUrl::ProtocolType ResolveLocation(const std::string& url)
37 const char* urlCStr = url.c_str();
38 const uint32_t length = url.size();
39 if((length > 7) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/')
42 if(('h' == tolower(urlCStr[0])) &&
43 ('t' == tolower(urlCStr[1])) &&
44 ('t' == tolower(urlCStr[2])) &&
45 ('p' == tolower(urlCStr[3])) &&
46 ('s' == tolower(urlCStr[4])))
48 return VisualUrl::REMOTE;
51 else if((length > 6) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/')
54 const char hOrd = tolower(urlCStr[0]);
55 const char tOra = tolower(urlCStr[1]);
56 const char tOrl = tolower(urlCStr[2]);
57 const char pOri = tolower(urlCStr[3]);
63 return VisualUrl::REMOTE;
70 return VisualUrl::TEXTURE;
73 else if((length > 5) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/')
76 const char fOrS = tolower(urlCStr[0]);
77 if(('f' == fOrS) || ('s' == fOrS))
79 const char tOrs = tolower(urlCStr[1]);
80 if(('t' == tOrs) || ('s' == tOrs))
82 const char pOrh = tolower(urlCStr[2]);
83 if(('p' == pOrh) || ('h' == pOrh))
85 return VisualUrl::REMOTE;
90 return VisualUrl::LOCAL;
93 VisualUrl::Type ResolveType(const std::string& url)
95 // if only one char in string, can only be regular image
96 const std::size_t count = url.size();
99 // parsing from the end for better chance of early outs
106 char SVG[4] = {'g', 'v', 's', '.'};
107 char GIF[4] = {'f', 'i', 'g', '.'};
108 char WEBP[5] = {'p', 'b', 'e', 'w', '.'};
109 char JSON[5] = {'n', 'o', 's', 'j', '.'};
110 unsigned int svgScore = 0;
111 unsigned int gifScore = 0;
112 unsigned int webpScore = 0;
113 unsigned int jsonScore = 0;
117 const char currentChar = tolower(url[index]);
118 const std::size_t offsetFromEnd = count - index - 1u;
119 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
121 // early out if SVG as can't be used in N patch for now
122 if(++svgScore == sizeof(SVG))
124 return VisualUrl::SVG;
127 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
129 // early out if GIF as can't be used in N patch for now
130 if(++gifScore == sizeof(GIF))
132 return VisualUrl::GIF;
135 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
137 // early out if WEBP as can't be used in N patch for now
138 if(++webpScore == sizeof(WEBP))
140 return VisualUrl::WEBP;
143 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
145 // early out if JSON as can't be used in N patch for now
146 if(++jsonScore == sizeof(JSON))
148 return VisualUrl::JSON;
155 if('.' == currentChar)
163 if(('#' == currentChar) || ('9' == currentChar))
169 // early out, not a valid N/9-patch URL
170 return VisualUrl::REGULAR_IMAGE;
176 if('.' == currentChar)
178 return VisualUrl::N_PATCH;
182 // early out, not a valid N/9-patch URL
183 return VisualUrl::REGULAR_IMAGE;
190 // if we got here it is a regular image
191 return VisualUrl::REGULAR_IMAGE;
196 VisualUrl::VisualUrl()
198 mType(VisualUrl::REGULAR_IMAGE),
199 mLocation(VisualUrl::LOCAL)
203 VisualUrl::VisualUrl(const std::string& url)
205 mType(VisualUrl::REGULAR_IMAGE),
206 mLocation(VisualUrl::LOCAL)
210 mLocation = ResolveLocation(url);
211 if(VisualUrl::TEXTURE != mLocation)
213 // TEXTURE location url doesn't need type resolving, REGULAR_IMAGE is fine
214 mType = ResolveType(url);
218 Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get();
221 GetImplementation(factory).GetTextureManager().UseExternalTexture(*this);
227 VisualUrl::VisualUrl(const VisualUrl& url)
230 mLocation(url.mLocation)
232 if(VisualUrl::TEXTURE == mLocation)
234 Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get();
237 GetImplementation(factory).GetTextureManager().UseExternalTexture(*this);
242 VisualUrl::~VisualUrl()
244 if(VisualUrl::TEXTURE == mLocation)
246 Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get();
249 GetImplementation(factory).GetTextureManager().RemoveExternalTexture(mUrl);
254 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
258 if(VisualUrl::TEXTURE == mLocation)
260 Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get();
263 GetImplementation(factory).GetTextureManager().RemoveExternalTexture(mUrl);
269 mLocation = url.mLocation;
271 if(VisualUrl::TEXTURE == mLocation)
273 Toolkit::VisualFactory factory = Toolkit::VisualFactory::Get();
276 GetImplementation(factory).GetTextureManager().UseExternalTexture(*this);
283 const std::string& VisualUrl::GetUrl() const
288 VisualUrl::Type VisualUrl::GetType() const
293 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
298 bool VisualUrl::IsValid() const
300 return mUrl.size() > 0u;
303 bool VisualUrl::IsLocalResource() const
305 return mLocation == VisualUrl::LOCAL;
308 std::string VisualUrl::GetLocation() const
310 return GetLocation(mUrl);
313 std::string VisualUrl::CreateTextureUrl(const std::string& location)
315 return "dali://" + location;
318 VisualUrl::ProtocolType VisualUrl::GetProtocolType(const std::string& url)
320 return ResolveLocation(url);
323 std::string VisualUrl::GetLocation(const std::string& url)
325 const auto location = url.find("://");
326 if(std::string::npos != location)
328 return url.substr(location + 3u); // 3 characters forwards from the start of ://
334 } // namespace Internal
336 } // namespace Toolkit