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()
31 VisualUrl::ProtocolType ResolveLocation(const std::string& url)
33 const char* urlCStr = url.c_str();
34 const uint32_t length = url.size();
35 if((length > 7) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/')
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]);
49 return VisualUrl::REMOTE;
57 return VisualUrl::BUFFER;
60 else if((length > 6) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/')
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]);
72 return VisualUrl::REMOTE;
79 return VisualUrl::TEXTURE;
82 else if((length > 5) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/')
85 const char fOrs = tolower(urlCStr[0]);
86 const char tOrs = tolower(urlCStr[1]);
87 const char pOrh = tolower(urlCStr[2]);
92 return VisualUrl::REMOTE;
98 return VisualUrl::REMOTE;
101 return VisualUrl::LOCAL;
104 VisualUrl::Type ResolveType(const std::string& url)
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;
111 // parsing from the end for better chance of early outs
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;
131 const char currentChar = tolower(url[index]);
132 const std::size_t offsetFromEnd = count - index - 1u;
133 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
135 // early out if SVG as can't be used in N patch for now
136 if(++svgScore == sizeof(SVG))
138 return VisualUrl::SVG;
141 if((offsetFromEnd < sizeof(TVG)) && (currentChar == TVG[offsetFromEnd]))
143 // early out if TVG as can't be used in N patch for now
144 if(++tvgScore == sizeof(TVG))
146 return VisualUrl::TVG;
149 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
151 //find type, but need to be check used in N patch
152 if(++gifScore == sizeof(GIF))
154 returnType = VisualUrl::GIF;
157 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
159 if(++webpScore == sizeof(WEBP))
161 //find type, but need to be check used in N patch
162 returnType = VisualUrl::WEBP;
165 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
167 // early out if JSON as can't be used in N patch for now
168 if(++jsonScore == sizeof(JSON))
170 return VisualUrl::JSON;
177 if('.' == currentChar)
185 if(('#' == currentChar) || ('9' == currentChar))
191 // early out, not a valid N/9-patch URL
198 if('.' == currentChar)
200 return VisualUrl::N_PATCH;
204 // early out, not a valid N/9-patch URL
212 // if we got here it is a regular image
218 VisualUrl::VisualUrl()
220 mType(VisualUrl::REGULAR_IMAGE),
221 mLocation(VisualUrl::LOCAL)
225 VisualUrl::VisualUrl(const std::string& url)
227 mType(VisualUrl::REGULAR_IMAGE),
228 mLocation(VisualUrl::LOCAL)
232 mLocation = ResolveLocation(url);
233 if(VisualUrl::TEXTURE != mLocation && VisualUrl::BUFFER != mLocation)
235 // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine
236 mType = ResolveType(url);
241 VisualUrl::VisualUrl(const VisualUrl& url)
244 mLocation(url.mLocation)
248 VisualUrl::~VisualUrl()
252 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
258 mLocation = url.mLocation;
263 const std::string& VisualUrl::GetUrl() const
268 VisualUrl::Type VisualUrl::GetType() const
273 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
278 bool VisualUrl::IsValid() const
280 return mUrl.size() > 0u;
283 bool VisualUrl::IsLocalResource() const
285 return mLocation == VisualUrl::LOCAL;
288 bool VisualUrl::IsBufferResource() const
290 return mLocation == VisualUrl::BUFFER;
293 std::string VisualUrl::GetLocation() const
295 return GetLocation(mUrl);
298 std::string VisualUrl::CreateTextureUrl(const std::string& location)
300 return "dali://" + location;
303 std::string VisualUrl::CreateBufferUrl(const std::string& location)
305 return "enbuf://" + location;
308 VisualUrl::ProtocolType VisualUrl::GetProtocolType(const std::string& url)
310 return ResolveLocation(url);
313 std::string VisualUrl::GetLocation(const std::string& url)
315 const auto location = url.find("://");
316 if(std::string::npos != location)
318 return url.substr(location + 3u); // 3 characters forwards from the start of ://
323 } // namespace Internal
325 } // namespace Toolkit