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] == '/')
38 if(('h' == tolower(urlCStr[0])) &&
39 ('t' == tolower(urlCStr[1])) &&
40 ('t' == tolower(urlCStr[2])) &&
41 ('p' == tolower(urlCStr[3])) &&
42 ('s' == tolower(urlCStr[4])))
44 return VisualUrl::REMOTE;
47 else if((length > 6) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/')
50 const char hOrd = tolower(urlCStr[0]);
51 const char tOra = tolower(urlCStr[1]);
52 const char tOrl = tolower(urlCStr[2]);
53 const char pOri = tolower(urlCStr[3]);
59 return VisualUrl::REMOTE;
66 return VisualUrl::TEXTURE;
69 else if((length > 5) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/')
72 const char fOrS = tolower(urlCStr[0]);
73 if(('f' == fOrS) || ('s' == fOrS))
75 const char tOrs = tolower(urlCStr[1]);
76 if(('t' == tOrs) || ('s' == tOrs))
78 const char pOrh = tolower(urlCStr[2]);
79 if(('p' == pOrh) || ('h' == pOrh))
81 return VisualUrl::REMOTE;
86 return VisualUrl::LOCAL;
89 VisualUrl::Type ResolveType(const std::string& url)
91 // if only one char in string, can only be regular image
92 const std::size_t count = url.size();
95 // parsing from the end for better chance of early outs
102 char SVG[4] = {'g', 'v', 's', '.'};
103 char GIF[4] = {'f', 'i', 'g', '.'};
104 char WEBP[5] = {'p', 'b', 'e', 'w', '.'};
105 char JSON[5] = {'n', 'o', 's', 'j', '.'};
106 unsigned int svgScore = 0;
107 unsigned int gifScore = 0;
108 unsigned int webpScore = 0;
109 unsigned int jsonScore = 0;
113 const char currentChar = tolower(url[index]);
114 const std::size_t offsetFromEnd = count - index - 1u;
115 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
117 // early out if SVG as can't be used in N patch for now
118 if(++svgScore == sizeof(SVG))
120 return VisualUrl::SVG;
123 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
125 // early out if GIF as can't be used in N patch for now
126 if(++gifScore == sizeof(GIF))
128 return VisualUrl::GIF;
131 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
133 // early out if WEBP as can't be used in N patch for now
134 if(++webpScore == sizeof(WEBP))
136 return VisualUrl::WEBP;
139 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
141 // early out if JSON as can't be used in N patch for now
142 if(++jsonScore == sizeof(JSON))
144 return VisualUrl::JSON;
151 if('.' == currentChar)
159 if(('#' == currentChar) || ('9' == currentChar))
165 // early out, not a valid N/9-patch URL
166 return VisualUrl::REGULAR_IMAGE;
172 if('.' == currentChar)
174 return VisualUrl::N_PATCH;
178 // early out, not a valid N/9-patch URL
179 return VisualUrl::REGULAR_IMAGE;
186 // if we got here it is a regular image
187 return VisualUrl::REGULAR_IMAGE;
192 VisualUrl::VisualUrl()
194 mType(VisualUrl::REGULAR_IMAGE),
195 mLocation(VisualUrl::LOCAL)
199 VisualUrl::VisualUrl(const std::string& url)
201 mType(VisualUrl::REGULAR_IMAGE),
202 mLocation(VisualUrl::LOCAL)
206 mLocation = ResolveLocation(url);
207 if(VisualUrl::TEXTURE != mLocation)
209 // TEXTURE location url doesn't need type resolving, REGULAR_IMAGE is fine
210 mType = ResolveType(url);
215 VisualUrl::VisualUrl(const VisualUrl& url)
218 mLocation(url.mLocation)
222 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
228 mLocation = url.mLocation;
233 const std::string& VisualUrl::GetUrl() const
238 VisualUrl::Type VisualUrl::GetType() const
243 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
248 bool VisualUrl::IsValid() const
250 return mUrl.size() > 0u;
253 bool VisualUrl::IsLocalResource() const
255 return mLocation == VisualUrl::LOCAL;
258 std::string VisualUrl::GetLocation() const
260 const auto location = mUrl.find("://");
261 if(std::string::npos != location)
263 return mUrl.substr(location + 3u); // 3 characters forwards from the start of ://
268 std::string VisualUrl::CreateTextureUrl(const std::string& location)
270 return "dali://" + location;
273 } // namespace Internal
275 } // namespace Toolkit