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 char RIVE[4] = {'v', 'i', 'r', '.'};
107 unsigned int svgScore = 0;
108 unsigned int gifScore = 0;
109 unsigned int webpScore = 0;
110 unsigned int jsonScore = 0;
111 unsigned int riveScore = 0;
115 const char currentChar = tolower(url[index]);
116 const std::size_t offsetFromEnd = count - index - 1u;
117 if((offsetFromEnd < sizeof(SVG)) && (currentChar == SVG[offsetFromEnd]))
119 // early out if SVG as can't be used in N patch for now
120 if(++svgScore == sizeof(SVG))
122 return VisualUrl::SVG;
125 if((offsetFromEnd < sizeof(GIF)) && (currentChar == GIF[offsetFromEnd]))
127 // early out if GIF as can't be used in N patch for now
128 if(++gifScore == sizeof(GIF))
130 return VisualUrl::GIF;
133 if((offsetFromEnd < sizeof(WEBP)) && (currentChar == WEBP[offsetFromEnd]))
135 // early out if WEBP as can't be used in N patch for now
136 if(++webpScore == sizeof(WEBP))
138 return VisualUrl::WEBP;
141 if((offsetFromEnd < sizeof(JSON)) && (currentChar == JSON[offsetFromEnd]))
143 // early out if JSON as can't be used in N patch for now
144 if(++jsonScore == sizeof(JSON))
146 return VisualUrl::JSON;
149 if((offsetFromEnd < sizeof(RIVE)) && (currentChar == RIVE[offsetFromEnd]))
151 // early out if RIVE as can't be used in N patch for now
152 if(++riveScore == sizeof(RIVE))
154 return VisualUrl::RIVE;
161 if('.' == currentChar)
169 if(('#' == currentChar) || ('9' == currentChar))
175 // early out, not a valid N/9-patch URL
176 return VisualUrl::REGULAR_IMAGE;
182 if('.' == currentChar)
184 return VisualUrl::N_PATCH;
188 // early out, not a valid N/9-patch URL
189 return VisualUrl::REGULAR_IMAGE;
196 // if we got here it is a regular image
197 return VisualUrl::REGULAR_IMAGE;
202 VisualUrl::VisualUrl()
204 mType(VisualUrl::REGULAR_IMAGE),
205 mLocation(VisualUrl::LOCAL)
209 VisualUrl::VisualUrl(const std::string& url)
211 mType(VisualUrl::REGULAR_IMAGE),
212 mLocation(VisualUrl::LOCAL)
216 mLocation = ResolveLocation(url);
217 if(VisualUrl::TEXTURE != mLocation)
219 // TEXTURE location url doesn't need type resolving, REGULAR_IMAGE is fine
220 mType = ResolveType(url);
225 VisualUrl::VisualUrl(const VisualUrl& url)
228 mLocation(url.mLocation)
232 VisualUrl& VisualUrl::operator=(const VisualUrl& url)
238 mLocation = url.mLocation;
243 const std::string& VisualUrl::GetUrl() const
248 VisualUrl::Type VisualUrl::GetType() const
253 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
258 bool VisualUrl::IsValid() const
260 return mUrl.size() > 0u;
263 bool VisualUrl::IsLocalResource() const
265 return mLocation == VisualUrl::LOCAL;
268 std::string VisualUrl::GetLocation() const
270 const auto location = mUrl.find("://");
271 if(std::string::npos != location)
273 return mUrl.substr(location + 3u); // 3 characters forwards from the start of ://
278 std::string VisualUrl::CreateTextureUrl(const std::string& location)
280 return "dali://" + location;
283 } // namespace Internal
285 } // namespace Toolkit