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