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