[dali_1.2.13] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-effects-style.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // FILE HEADER
19 #include <dali-toolkit/internal/text/text-effects-style.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
23 #include <dali-toolkit/internal/text/property-string-parser.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Text
32 {
33
34 namespace
35 {
36 const std::string COLOR_KEY( "color" );
37 const std::string OFFSET_KEY( "offset" );
38 const std::string HEIGHT_KEY( "height" );
39 const std::string ENABLE_KEY( "enable" );
40 const std::string TRUE_TOKEN( "true" );
41 const std::string FALSE_TOKEN( "false" );
42 }
43
44 bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
45                             bool& colorDefined,
46                             Vector4& color,
47                             bool& offsetDefined,
48                             Vector2& offset )
49 {
50   const unsigned int numberOfItems = shadowPropertiesMap.Count();
51
52   // Parses and applies the style.
53   for( unsigned int index = 0u; index < numberOfItems; ++index )
54   {
55     const KeyValuePair& valueGet = shadowPropertiesMap.GetKeyValue( index );
56
57     if( COLOR_KEY == valueGet.first.stringKey )
58     {
59       /// Color key.
60       colorDefined = true;
61
62       const std::string colorStr = valueGet.second.Get<std::string>();
63
64       Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
65     }
66     else if( OFFSET_KEY == valueGet.first.stringKey )
67     {
68       /// Offset key.
69       offsetDefined = true;
70
71       const std::string offsetStr = valueGet.second.Get<std::string>();
72
73       StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
74     }
75   }
76
77   return 0u == numberOfItems;
78 }
79
80 bool ParseUnderlineProperties( const Property::Map& underlinePropertiesMap,
81                                bool& enabled,
82                                bool& colorDefined,
83                                Vector4& color,
84                                bool& heightDefined,
85                                float& height )
86 {
87   const unsigned int numberOfItems = underlinePropertiesMap.Count();
88
89   // Parses and applies the style.
90   for( unsigned int index = 0u; index < numberOfItems; ++index )
91   {
92     const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
93
94     if( ENABLE_KEY == valueGet.first.stringKey )
95     {
96       /// Enable key.
97       const std::string enableStr = valueGet.second.Get<std::string>();
98       enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
99     }
100     else if( COLOR_KEY == valueGet.first.stringKey )
101     {
102       /// Color key.
103       colorDefined = true;
104
105       const std::string colorStr = valueGet.second.Get<std::string>();
106
107       Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
108     }
109     else if( HEIGHT_KEY == valueGet.first.stringKey )
110     {
111       /// Height key.
112       heightDefined = true;
113
114       const std::string heightStr = valueGet.second.Get<std::string>();
115
116       height = StringToFloat( heightStr.c_str() );
117     }
118   }
119
120   return 0u == numberOfItems;
121 }
122
123 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
124 {
125   bool update = false;
126
127   if( controller )
128   {
129     switch( type )
130     {
131       case EffectStyle::DEFAULT:
132       {
133         const Property::Map& propertiesMap = value.Get<Property::Map>();
134
135         bool enabled = false;
136         bool colorDefined = false;
137         Vector4 color;
138         bool heightDefined = false;
139         float height = 0.f;
140
141         const bool empty = ParseUnderlineProperties( propertiesMap,
142                                                      enabled,
143                                                      colorDefined,
144                                                      color,
145                                                      heightDefined,
146                                                      height );
147
148         if( !empty )
149         {
150           if( enabled != controller->IsUnderlineEnabled() )
151           {
152             controller->SetUnderlineEnabled( enabled );
153             update = true;
154           }
155
156           // Sets the default underline values.
157           if( colorDefined && ( controller->GetUnderlineColor() != color ) )
158           {
159             controller->SetUnderlineColor( color );
160             update = true;
161           }
162
163           if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
164           {
165             controller->SetUnderlineHeight( height );
166             update = true;
167           }
168         }
169         else
170         {
171           // Disable underline.
172           if( controller->IsUnderlineEnabled() )
173           {
174             controller->SetUnderlineEnabled( false );
175             update = true;
176           }
177         }
178         break;
179       }
180       case EffectStyle::INPUT:
181       {
182         const std::string& underlineProperties = value.Get<std::string>();
183
184         controller->SetInputUnderlineProperties( underlineProperties );
185         break;
186       }
187     } // switch
188   } // if( controller )
189
190   return update;
191 }
192
193 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
194 {
195   if( controller )
196   {
197     switch( type )
198     {
199       case EffectStyle::DEFAULT:
200       {
201         const bool enabled = controller->IsUnderlineEnabled();
202         const Vector4& color = controller->GetUnderlineColor();
203         const float height = controller->GetUnderlineHeight();
204
205         Property::Map map;
206
207         const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
208         map.Insert( ENABLE_KEY, enabledStr );
209
210         std::string colorStr;
211         Vector4ToColorString( color, colorStr );
212         map.Insert( COLOR_KEY, colorStr );
213
214         std::string heightStr;
215         FloatToString( height, heightStr );
216         map.Insert( HEIGHT_KEY, heightStr );
217
218         value = map;
219         break;
220       }
221       case EffectStyle::INPUT:
222       {
223         value = controller->GetInputUnderlineProperties();
224         break;
225       }
226     }
227   }
228 }
229
230 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
231 {
232   bool update = false;
233
234   if( controller )
235   {
236     switch( type )
237     {
238       case EffectStyle::DEFAULT:
239       {
240         const Property::Map& propertiesMap = value.Get<Property::Map>();
241
242         bool colorDefined = false;
243         Vector4 color;
244         bool offsetDefined = false;
245         Vector2 offset;
246
247         const bool empty = ParseShadowProperties( propertiesMap,
248                                                   colorDefined,
249                                                   color,
250                                                   offsetDefined,
251                                                   offset );
252
253         if( !empty )
254         {
255           // Sets the default shadow values.
256           if( colorDefined && ( controller->GetShadowColor() != color ) )
257           {
258             controller->SetShadowColor( color );
259             update = true;
260           }
261
262           if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
263           {
264             controller->SetShadowOffset( offset );
265             update = true;
266           }
267         }
268         else
269         {
270           // Disable shadow.
271           if( Vector2::ZERO != controller->GetShadowOffset() )
272           {
273             controller->SetShadowOffset( Vector2::ZERO );
274           }
275         }
276         break;
277       }
278       case EffectStyle::INPUT:
279       {
280         const std::string& shadowString = value.Get<std::string>();
281
282         controller->SetInputShadowProperties( shadowString );
283         break;
284       }
285     } // switch
286   } // if( controller )
287
288   return update;
289 }
290
291 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
292 {
293   if( controller )
294   {
295     switch( type )
296     {
297       case EffectStyle::DEFAULT:
298       {
299         const Vector4& color = controller->GetShadowColor();
300         const Vector2& offset = controller->GetShadowOffset();
301
302         Property::Map map;
303
304         std::string colorStr;
305         Vector4ToColorString( color, colorStr );
306         map.Insert( COLOR_KEY, colorStr );
307
308         std::string offsetStr;
309         Vector2ToString( offset, offsetStr );
310         map.Insert( OFFSET_KEY, offsetStr );
311
312         value = map;
313         break;
314       }
315       case EffectStyle::INPUT:
316       {
317         value = controller->GetInputShadowProperties();
318         break;
319       }
320     }
321   }
322 }
323
324 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
325 {
326   bool update = false;
327
328   if( controller )
329   {
330     const std::string properties = value.Get< std::string >();
331
332     switch( type )
333     {
334       case EffectStyle::DEFAULT:
335       {
336         // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
337         controller->SetDefaultEmbossProperties( properties );
338         break;
339       }
340       case EffectStyle::INPUT:
341       {
342         // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
343         controller->SetInputEmbossProperties( properties );
344         break;
345       }
346     }
347   }
348
349   return update;
350 }
351
352 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
353 {
354   if( controller )
355   {
356     switch( type )
357     {
358       case EffectStyle::DEFAULT:
359       {
360         value = controller->GetDefaultEmbossProperties();
361         break;
362       }
363       case EffectStyle::INPUT:
364       {
365         value = controller->GetInputEmbossProperties();
366         break;
367       }
368     }
369   }
370 }
371
372 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
373 {
374   bool update = false;
375
376   if( controller )
377   {
378     const std::string properties = value.Get< std::string >();
379
380     switch( type )
381     {
382       case EffectStyle::DEFAULT:
383       {
384         // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
385         controller->SetDefaultOutlineProperties( properties );
386         break;
387       }
388       case EffectStyle::INPUT:
389       {
390         // Stores the input outline's properties string to be recovered by the GetOutlineProperties() function.
391         controller->SetInputOutlineProperties( properties );
392         break;
393       }
394     }
395   }
396
397   return update;
398 }
399
400 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
401 {
402   if( controller )
403   {
404     switch( type )
405     {
406       case EffectStyle::DEFAULT:
407       {
408         value = controller->GetDefaultOutlineProperties();
409         break;
410       }
411       case EffectStyle::INPUT:
412       {
413         value = controller->GetInputOutlineProperties();
414         break;
415       }
416     }
417   }
418 }
419
420 } // namespace Text
421
422 } // namespace Toolkit
423
424 } // namespace Dali