2 * Copyright (c) 2022 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 <dali/devel-api/common/hash.h>
22 #include <cstring> // for toupper()
32 VisualUrl::ProtocolType ResolveLocation(const std::string& url)
34 const char* urlCStr = url.c_str();
35 const uint32_t length = url.size();
36 if((length > 7) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/')
38 // https:// or enbuf://
39 const char hOre = tolower(urlCStr[0]);
40 const char tOrn = tolower(urlCStr[1]);
41 const char tOrb = tolower(urlCStr[2]);
42 const char pOru = tolower(urlCStr[3]);
43 const char sOrf = tolower(urlCStr[4]);
50 return VisualUrl::REMOTE;
58 return VisualUrl::BUFFER;
61 else if((length > 6) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/')
64 const char hOrd = tolower(urlCStr[0]);
65 const char tOra = tolower(urlCStr[1]);
66 const char tOrl = tolower(urlCStr[2]);
67 const char pOri = tolower(urlCStr[3]);
73 return VisualUrl::REMOTE;
80 return VisualUrl::TEXTURE;
83 else if((length > 5) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/')
86 const char fOrs = tolower(urlCStr[0]);
87 const char tOrs = tolower(urlCStr[1]);
88 const char pOrh = tolower(urlCStr[2]);
93 return VisualUrl::REMOTE;
99 return VisualUrl::REMOTE;
102 return VisualUrl::LOCAL;
105 VisualUrl::Type ResolveType(const std::string& url)
107 // if only one char in string, can only be regular image
108 const std::size_t count = url.size();
109 VisualUrl::Type returnType = VisualUrl::REGULAR_IMAGE;
112 // parsing from the end for better chance of early outs
119 char SVG[4] = {'g', 'v', 's', '.'};
120 char GIF[4] = {'f', 'i', 'g', '.'};
121 char WEBP[5] = {'p', 'b', 'e', 'w', '.'};
122 char JSON[5] = {'n', 'o', 's', 'j', '.'};
123 char TVG[4] = {'g', 'v', 't', '.'};
124 unsigned int svgScore = 0;
125 unsigned int tvgScore = 0;
126 unsigned int gifScore = 0;
127 unsigned int webpScore = 0;
128 unsigned int jsonScore = 0;
132 const char currentChar = tolower(url[index]);
133 const std::size_t offsetFromEnd = count - index - 1u;
134 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
136 // early out if SVG as can't be used in N patch for now
137 if(++svgScore == sizeof(SVG))
139 return VisualUrl::SVG;
142 if((offsetFromEnd < sizeof(TVG)) && (currentChar == TVG[offsetFromEnd]))
144 // early out if TVG as can't be used in N patch for now
145 if(++tvgScore == sizeof(TVG))
147 return VisualUrl::TVG;
150 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
152 //find type, but need to be check used in N patch
153 if(++gifScore == sizeof(GIF))
155 returnType = VisualUrl::GIF;
158 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
160 if(++webpScore == sizeof(WEBP))
162 //find type, but need to be check used in N patch
163 returnType = VisualUrl::WEBP;
166 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
168 // early out if JSON as can't be used in N patch for now
169 if(++jsonScore == sizeof(JSON))
171 return VisualUrl::JSON;
178 if('.' == currentChar)
186 if(('#' == currentChar) || ('9' == currentChar))
192 // early out, not a valid N/9-patch URL
199 if('.' == currentChar)
201 return VisualUrl::N_PATCH;
205 // early out, not a valid N/9-patch URL
213 // if we got here it is a regular image
219 VisualUrl::VisualUrl()
221 mType(VisualUrl::REGULAR_IMAGE),
222 mLocation(VisualUrl::LOCAL),
227 VisualUrl::VisualUrl(const std::string& url)
229 mType(VisualUrl::REGULAR_IMAGE),
230 mLocation(VisualUrl::LOCAL),
235 mLocation = ResolveLocation(url);
236 if(VisualUrl::TEXTURE != mLocation && VisualUrl::BUFFER != mLocation)
238 // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine
239 mType = ResolveType(url);
244 VisualUrl::VisualUrl(const VisualUrl& url)
247 mLocation(url.mLocation),
248 mUrlHash(url.mUrlHash)
252 VisualUrl::~VisualUrl()
256 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
262 mLocation = url.mLocation;
263 mUrlHash = url.mUrlHash;
268 const std::string& VisualUrl::GetUrl() const
273 const std::uint64_t& VisualUrl::GetUrlHash() const
275 return DALI_UNLIKELY(mUrlHash == 0) ? (mUrlHash = Dali::CalculateHash(mUrl)) : mUrlHash;
278 VisualUrl::Type VisualUrl::GetType() const
283 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
288 bool VisualUrl::IsValid() const
290 return mUrl.size() > 0u;
293 bool VisualUrl::IsLocalResource() const
295 return mLocation == VisualUrl::LOCAL;
298 bool VisualUrl::IsBufferResource() const
300 return mLocation == VisualUrl::BUFFER;
303 std::string VisualUrl::GetLocation() const
305 return GetLocation(mUrl);
308 std::string VisualUrl::CreateTextureUrl(const std::string& location)
310 return "dali://" + location;
313 std::string VisualUrl::CreateBufferUrl(const std::string& location)
315 return "enbuf://" + 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 ://
333 } // namespace Internal
335 } // namespace Toolkit