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 unsigned int svgScore = 0;
122 unsigned int gifScore = 0;
123 unsigned int webpScore = 0;
124 unsigned int jsonScore = 0;
128 const char currentChar = tolower(url[index]);
129 const std::size_t offsetFromEnd = count - index - 1u;
130 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
132 // early out if SVG as can't be used in N patch for now
133 if(++svgScore == sizeof(SVG))
135 return VisualUrl::SVG;
138 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
140 // early out if GIF as can't be used in N patch for now
141 if(++gifScore == sizeof(GIF))
143 return VisualUrl::GIF;
146 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
148 // early out if WEBP as can't be used in N patch for now
149 if(++webpScore == sizeof(WEBP))
151 return VisualUrl::WEBP;
154 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
156 // early out if JSON as can't be used in N patch for now
157 if(++jsonScore == sizeof(JSON))
159 return VisualUrl::JSON;
166 if('.' == currentChar)
174 if(('#' == currentChar) || ('9' == currentChar))
180 // early out, not a valid N/9-patch URL
181 return VisualUrl::REGULAR_IMAGE;
187 if('.' == currentChar)
189 return VisualUrl::N_PATCH;
193 // early out, not a valid N/9-patch URL
194 return VisualUrl::REGULAR_IMAGE;
201 // if we got here it is a regular image
202 return VisualUrl::REGULAR_IMAGE;
207 VisualUrl::VisualUrl()
209 mType(VisualUrl::REGULAR_IMAGE),
210 mLocation(VisualUrl::LOCAL)
214 VisualUrl::VisualUrl(const std::string& url)
216 mType(VisualUrl::REGULAR_IMAGE),
217 mLocation(VisualUrl::LOCAL)
221 mLocation = ResolveLocation(url);
222 if(VisualUrl::TEXTURE != mLocation && VisualUrl::BUFFER != mLocation)
224 // TEXTURE and BUFFER location url doesn't need type resolving, REGULAR_IMAGE is fine
225 mType = ResolveType(url);
230 VisualUrl::VisualUrl(const VisualUrl& url)
233 mLocation(url.mLocation)
237 VisualUrl::~VisualUrl()
241 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
247 mLocation = url.mLocation;
252 const std::string& VisualUrl::GetUrl() const
257 VisualUrl::Type VisualUrl::GetType() const
262 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
267 bool VisualUrl::IsValid() const
269 return mUrl.size() > 0u;
272 bool VisualUrl::IsLocalResource() const
274 return mLocation == VisualUrl::LOCAL;
277 bool VisualUrl::IsBufferResource() const
279 return mLocation == VisualUrl::BUFFER;
282 std::string VisualUrl::GetLocation() const
284 return GetLocation(mUrl);
287 std::string VisualUrl::CreateTextureUrl(const std::string& location)
289 return "dali://" + location;
292 std::string VisualUrl::CreateBufferUrl(const std::string& location)
294 return "enbuf://" + location;
297 VisualUrl::ProtocolType VisualUrl::GetProtocolType(const std::string& url)
299 return ResolveLocation(url);
302 std::string VisualUrl::GetLocation(const std::string& url)
304 const auto location = url.find("://");
305 if(std::string::npos != location)
307 return url.substr(location + 3u); // 3 characters forwards from the start of ://
312 } // namespace Internal
314 } // namespace Toolkit