Primitive visual bevel fix.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-resolve-url.h
1 #ifndef DALI_TOOLKIT_VISUAL_FACTORY_URL_RESOLVE_H
2 #define DALI_TOOLKIT_VISUAL_FACTORY_URL_RESOLVE_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <ctype.h>
21
22 namespace Dali
23 {
24
25 namespace Toolkit
26 {
27
28 namespace Internal
29 {
30
31 namespace UrlType
32 {
33   /**
34    * The type of the URL based on the string contents
35    */
36   enum Type
37   {
38     REGULAR_IMAGE,
39     N_PATCH,
40     SVG
41   };
42 }
43
44 /**
45  * Helper to resolve URL type from the string
46  * @param[in] url to check
47  * @return UrlType
48  */
49 inline UrlType::Type ResolveUrlType( const std::string& url )
50 {
51   // if only one char in string, can only be regular image
52   const std::size_t count = url.size();
53   if( count > 0 )
54   {
55     // parsing from the end for better chance of early outs
56     enum { SUFFIX, HASH, HASH_DOT } state = SUFFIX;
57     char SVG[ 4 ] = { 'g', 'v', 's', '.' };
58     unsigned int svgScore = 0;
59     int index = count;
60     while( --index >= 0 )
61     {
62       const char currentChar = url[ index ];
63       const std::size_t offsetFromEnd = count - index - 1u;
64       if( ( offsetFromEnd < sizeof(SVG) )&&( tolower( currentChar ) == SVG[ offsetFromEnd ] ) )
65       {
66         // early out if SVG as can't be used in N patch for now
67         if( ++svgScore == sizeof(SVG) )
68         {
69           return UrlType::SVG;
70         }
71       }
72       switch( state )
73       {
74         case SUFFIX:
75         {
76           if( '.' == currentChar )
77           {
78             state = HASH;
79           }
80           break;
81         }
82         case HASH:
83         {
84           if( ( '#' == currentChar ) || ( '9' == currentChar ) )
85           {
86             state = HASH_DOT;
87           }
88           else
89           {
90             // early out, not a valid N/9-patch URL
91             return UrlType::REGULAR_IMAGE;
92           }
93           break;
94         }
95         case HASH_DOT:
96         {
97           if( '.' == currentChar )
98           {
99             return UrlType::N_PATCH;
100           }
101           else
102           {
103             // early out, not a valid N/9-patch URL
104             return UrlType::REGULAR_IMAGE;
105           }
106           break;
107         }
108       }
109     }
110   }
111   // if we got here it is a regular image
112   return UrlType::REGULAR_IMAGE;
113 }
114
115 } // namespace Internal
116
117 } // namespace Toolkit
118
119 } // namespace Dali
120
121 #endif /* DALI_TOOLKIT_VISUAL_FACTORY_URL_RESOLVE_H */