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();
110 // parsing from the end for better chance of early outs
117 char SVG[4] = {'g', 'v', 's', '.'};
118 char GIF[4] = {'f', 'i', 'g', '.'};
119 char WEBP[5] = {'p', 'b', 'e', 'w', '.'};
120 char JSON[5] = {'n', 'o', 's', 'j', '.'};
121 char TVG[4] = {'g', 'v', 't', '.'};
122 unsigned int svgScore = 0;
123 unsigned int tvgScore = 0;
124 unsigned int gifScore = 0;
125 unsigned int webpScore = 0;
126 unsigned int jsonScore = 0;
130 const char currentChar = tolower(url[index]);
131 const std::size_t offsetFromEnd = count - index - 1u;
132 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
134 // early out if SVG as can't be used in N patch for now
135 if(++svgScore == sizeof(SVG))
137 return VisualUrl::SVG;
140 if((offsetFromEnd < sizeof(TVG)) && (currentChar == TVG[offsetFromEnd]))
142 // early out if TVG as can't be used in N patch for now
143 if(++tvgScore == sizeof(TVG))
145 return VisualUrl::TVG;
148 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
150 // early out if GIF as can't be used in N patch for now
151 if(++gifScore == sizeof(GIF))
153 return VisualUrl::GIF;
156 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
158 // early out if WEBP as can't be used in N patch for now
159 if(++webpScore == sizeof(WEBP))
161 return VisualUrl::WEBP;
164 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
166 // early out if JSON as can't be used in N patch for now
167 if(++jsonScore == sizeof(JSON))
169 return VisualUrl::JSON;
176 if('.' == currentChar)
184 if(('#' == currentChar) || ('9' == currentChar))
190 // early out, not a valid N/9-patch URL
191 return VisualUrl::REGULAR_IMAGE;
197 if('.' == currentChar)
199 return VisualUrl::N_PATCH;
203 // early out, not a valid N/9-patch URL
204 return VisualUrl::REGULAR_IMAGE;
211 // if we got here it is a regular image
212 return VisualUrl::REGULAR_IMAGE;
217 VisualUrl::VisualUrl()
219 mType(VisualUrl::REGULAR_IMAGE),
220 mLocation(VisualUrl::LOCAL)
224 VisualUrl::VisualUrl(const std::string& url)
226 mType(VisualUrl::REGULAR_IMAGE),
227 mLocation(VisualUrl::LOCAL)
231 mLocation = ResolveLocation(url);
232 if(VisualUrl::TEXTURE != mLocation && VisualUrl::BUFFER != mLocation)
234 // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine
235 mType = ResolveType(url);
240 VisualUrl::VisualUrl(const VisualUrl& url)
243 mLocation(url.mLocation)
247 VisualUrl::~VisualUrl()
251 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
257 mLocation = url.mLocation;
262 const std::string& VisualUrl::GetUrl() const
267 VisualUrl::Type VisualUrl::GetType() const
272 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
277 bool VisualUrl::IsValid() const
279 return mUrl.size() > 0u;
282 bool VisualUrl::IsLocalResource() const
284 return mLocation == VisualUrl::LOCAL;
287 bool VisualUrl::IsBufferResource() const
289 return mLocation == VisualUrl::BUFFER;
292 std::string VisualUrl::GetLocation() const
294 return GetLocation(mUrl);
297 std::string VisualUrl::CreateTextureUrl(const std::string& location)
299 return "dali://" + location;
302 std::string VisualUrl::CreateBufferUrl(const std::string& location)
304 return "enbuf://" + location;
307 VisualUrl::ProtocolType VisualUrl::GetProtocolType(const std::string& url)
309 return ResolveLocation(url);
312 std::string VisualUrl::GetLocation(const std::string& url)
314 const auto location = url.find("://");
315 if(std::string::npos != location)
317 return url.substr(location + 3u); // 3 characters forwards from the start of ://
322 } // namespace Internal
324 } // namespace Toolkit