Merge "Added code for stylable transitions" into 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 }
42
43 bool ParseProperties( const std::string& shadowProperties,
44                       bool& colorDefined,
45                       Vector4& color,
46                       bool& offsetDefined,
47                       Vector2& offset )
48 {
49   // Parses and applies the style.
50   Property::Map map;
51   Text::ParsePropertyString( shadowProperties, map );
52
53   const bool empty = map.Empty();
54
55   if( !empty )
56   {
57     /// Color key.
58     Property::Value* colorValue = map.Find( COLOR_KEY );
59
60     colorDefined = colorValue != NULL;
61     if( colorDefined )
62     {
63       const std::string colorStr = colorValue->Get<std::string>();
64
65       Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
66     }
67
68     /// Offset key.
69     Property::Value* offsetValue = map.Find( OFFSET_KEY );
70
71     offsetDefined = offsetValue != NULL;
72     if( offsetDefined )
73     {
74       const std::string offsetStr = offsetValue->Get<std::string>();
75
76       StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
77     }
78   }
79
80   return empty;
81 }
82
83 bool ParseProperties( const std::string& underlineProperties,
84                       bool& enabled,
85                       bool& colorDefined,
86                       Vector4& color,
87                       bool& heightDefined,
88                       float& height )
89 {
90   // Parses and applies the style.
91   Property::Map map;
92   Text::ParsePropertyString( underlineProperties, map );
93
94   const bool empty = map.Empty();
95
96   if( !empty )
97   {
98     /// Enable key.
99     Property::Value* enableValue = map.Find( ENABLE_KEY );
100
101     enabled = false;
102     const bool enabledDefined = enableValue != NULL;
103     if( enabledDefined )
104     {
105       const std::string enableStr = enableValue->Get<std::string>();
106       enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
107     }
108
109     /// Color key.
110     Property::Value* colorValue = map.Find( COLOR_KEY );
111
112     colorDefined = colorValue != NULL;
113     if( colorDefined )
114     {
115       const std::string colorStr = colorValue->Get<std::string>();
116
117       Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
118     }
119
120     /// Height key.
121     Property::Value* heightValue = map.Find( HEIGHT_KEY );
122
123     height = 0.f;
124     heightDefined = heightValue != NULL;
125     if( heightDefined )
126     {
127       const std::string heightStr = heightValue->Get<std::string>();
128
129       height = StringToFloat( heightStr.c_str() );
130     }
131   }
132
133   return empty;
134 }
135
136 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
137 {
138   bool update = false;
139
140   if( controller )
141   {
142     const std::string properties = value.Get<std::string>();
143
144     bool enabled = false;
145     bool colorDefined = false;
146     Vector4 color;
147     bool heightDefined = false;
148     float height = 0.f;
149
150     const bool empty = ParseProperties( properties,
151                                         enabled,
152                                         colorDefined,
153                                         color,
154                                         heightDefined,
155                                         height );
156
157     if( !empty )
158     {
159       switch( type )
160       {
161         case EffectStyle::DEFAULT:
162         {
163           if( enabled != controller->IsUnderlineEnabled() )
164           {
165             controller->SetUnderlineEnabled( enabled );
166             update = true;
167           }
168
169           // Sets the default underline values.
170           if( colorDefined && ( controller->GetUnderlineColor() != color ) )
171           {
172             controller->SetUnderlineColor( color );
173             update = true;
174           }
175
176           if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
177           {
178             controller->SetUnderlineHeight( height );
179             update = true;
180           }
181           break;
182         }
183         case EffectStyle::INPUT:
184         {
185           // Sets the input underline values.
186           // TODO: to be implemented.
187           break;
188         }
189       }
190     }
191     else
192     {
193       switch( type )
194       {
195         case EffectStyle::DEFAULT:
196         {
197           // Disable underline.
198           if( controller->IsUnderlineEnabled() )
199           {
200             controller->SetUnderlineEnabled( false );
201             update = true;
202           }
203           break;
204         }
205         case EffectStyle::INPUT:
206         {
207           // Sets the input underline values.
208           // TODO: to be implemented.
209           controller->SetInputUnderlineProperties( properties );
210           break;
211         }
212       }
213     }
214   }
215
216   return update;
217 }
218
219 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
220 {
221   if( controller )
222   {
223     switch( type )
224     {
225       case EffectStyle::DEFAULT:
226       {
227         const bool enabled = controller->IsUnderlineEnabled();
228         const Vector4& color = controller->GetUnderlineColor();
229         const float height = controller->GetUnderlineHeight();
230
231         std::string underlineProperties = "{\"enable\":";
232         const std::string enabledStr = enabled ? "true" : "false";
233         underlineProperties += "\"" + enabledStr + "\",";
234
235         std::string colorStr;
236         Vector4ToColorString( color, colorStr );
237         underlineProperties += "\"color\":\"" + colorStr + "\",";
238
239         std::string heightStr;
240         FloatToString( height, heightStr );
241         underlineProperties += "\"height\":\"" + heightStr + "\"}";
242
243         value = underlineProperties;
244         break;
245       }
246       case EffectStyle::INPUT:
247       {
248         value = controller->GetInputUnderlineProperties();
249         break;
250       }
251     }
252   }
253 }
254
255 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
256 {
257   bool update = false;
258
259   if( controller )
260   {
261     const std::string properties = value.Get< std::string >();
262
263     bool colorDefined = false;
264     Vector4 color;
265     bool offsetDefined = false;
266     Vector2 offset;
267
268     const bool empty = ParseProperties( properties,
269                                         colorDefined,
270                                         color,
271                                         offsetDefined,
272                                         offset );
273
274     if( !empty )
275     {
276       switch( type )
277       {
278         case EffectStyle::DEFAULT:
279         {
280           // Sets the default shadow values.
281           if( colorDefined && ( controller->GetShadowColor() != color ) )
282           {
283             controller->SetShadowColor( color );
284             update = true;
285           }
286
287           if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
288           {
289             controller->SetShadowOffset( offset );
290             update = true;
291           }
292           break;
293         }
294         case EffectStyle::INPUT:
295         {
296           // Sets the input shadow values.
297           // TODO: to be implemented.
298           break;
299         }
300       }
301     }
302     else
303     {
304       switch( type )
305       {
306         case EffectStyle::DEFAULT:
307         {
308           // Disable shadow.
309           if( Vector2::ZERO != controller->GetShadowOffset() )
310           {
311             controller->SetShadowOffset( Vector2::ZERO );
312           }
313           break;
314         }
315         case EffectStyle::INPUT:
316         {
317           // Sets the input shadow values.
318           // TODO: to be implemented.
319           controller->SetInputShadowProperties( properties );
320           break;
321         }
322       }
323     }
324   }
325
326   return update;
327 }
328
329 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
330 {
331   if( controller )
332   {
333     switch( type )
334     {
335       case EffectStyle::DEFAULT:
336       {
337         const Vector4& color = controller->GetShadowColor();
338         const Vector2& offset = controller->GetShadowOffset();
339
340         std::string shadowProperties = "{";
341
342         std::string colorStr;
343         Vector4ToColorString( color, colorStr );
344         shadowProperties += "\"color\":\"" + colorStr + "\",";
345
346         std::string offsetStr;
347         Vector2ToString( offset, offsetStr );
348         shadowProperties += "\"offset\":\"" + offsetStr + "\"}";
349
350         value = shadowProperties;
351         break;
352       }
353       case EffectStyle::INPUT:
354       {
355         value = controller->GetInputShadowProperties();
356         break;
357       }
358     }
359   }
360 }
361
362 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
363 {
364   bool update = false;
365
366   if( controller )
367   {
368     const std::string properties = value.Get< std::string >();
369
370     switch( type )
371     {
372       case EffectStyle::DEFAULT:
373       {
374         // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
375         controller->SetDefaultEmbossProperties( properties );
376         break;
377       }
378       case EffectStyle::INPUT:
379       {
380         // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
381         controller->SetInputEmbossProperties( properties );
382         break;
383       }
384     }
385   }
386
387   return update;
388 }
389
390 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
391 {
392   if( controller )
393   {
394     switch( type )
395     {
396       case EffectStyle::DEFAULT:
397       {
398         value = controller->GetDefaultEmbossProperties();
399         break;
400       }
401       case EffectStyle::INPUT:
402       {
403         value = controller->GetInputEmbossProperties();
404         break;
405       }
406     }
407   }
408 }
409
410 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
411 {
412   bool update = false;
413
414   if( controller )
415   {
416     const std::string properties = value.Get< std::string >();
417
418     switch( type )
419     {
420       case EffectStyle::DEFAULT:
421       {
422         // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
423         controller->SetDefaultOutlineProperties( properties );
424         break;
425       }
426       case EffectStyle::INPUT:
427       {
428         // Stores the input outline's properties string to be recovered by the GetOutlineProperties() function.
429         controller->SetInputOutlineProperties( properties );
430         break;
431       }
432     }
433   }
434
435   return update;
436 }
437
438 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
439 {
440   if( controller )
441   {
442     switch( type )
443     {
444       case EffectStyle::DEFAULT:
445       {
446         value = controller->GetDefaultOutlineProperties();
447         break;
448       }
449       case EffectStyle::INPUT:
450       {
451         value = controller->GetInputOutlineProperties();
452         break;
453       }
454     }
455   }
456 }
457
458 } // namespace Text
459
460 } // namespace Toolkit
461
462 } // namespace Dali