[dali_1.1.37] 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 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         }
117         case EffectStyle::INPUT:
118         {
119           // Sets the input underline values.
120           // TODO: to be implemented.
121           break;
122         }
123       }
124     }
125     else
126     {
127       // Disable underline.
128       if( controller->IsUnderlineEnabled() )
129       {
130         controller->SetUnderlineEnabled( false );
131         update = true;
132       }
133     }
134   }
135
136   return update;
137 }
138
139 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
140 {
141   if( controller )
142   {
143     switch( type )
144     {
145       case EffectStyle::DEFAULT:
146       {
147         value = controller->GetDefaultUnderlineProperties();
148         break;
149       }
150       case EffectStyle::INPUT:
151       {
152         value = controller->GetInputUnderlineProperties();
153         break;
154       }
155     }
156   }
157 }
158
159 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
160 {
161   bool update = false;
162
163   if( controller )
164   {
165     const std::string properties = value.Get< std::string >();
166
167     switch( type )
168     {
169       case EffectStyle::DEFAULT:
170       {
171         // Stores the default shadow's properties string to be recovered by the GetShadowProperties() function.
172         controller->SetDefaultShadowProperties( properties );
173         break;
174       }
175       case EffectStyle::INPUT:
176       {
177         // Stores the input shadow's properties string to be recovered by the GetShadowProperties() function.
178         controller->SetInputShadowProperties( properties );
179         break;
180       }
181     }
182
183     // Parses and applies the style.
184     Property::Map map;
185     ParsePropertyString( properties, map );
186
187     if( !map.Empty() )
188     {
189       /// Color key
190       Property::Value* colorValue = map.Find( COLOR_KEY );
191
192       Vector4 color;
193       const bool colorDefined = colorValue != NULL;
194       if( colorDefined )
195       {
196         const std::string colorStr = colorValue->Get<std::string>();
197
198         ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
199       }
200
201       /// Offset key
202       Property::Value* offsetValue = map.Find( OFFSET_KEY );
203
204       Vector2 offset;
205       const bool offsetDefined = offsetValue != NULL;
206       if( offsetDefined )
207       {
208         const std::string offsetStr = offsetValue->Get<std::string>();
209
210         StringOffsetToVector2( offsetStr, offset );
211       }
212
213       switch( type )
214       {
215         case EffectStyle::DEFAULT:
216         {
217           // Sets the default shadow values.
218           if( colorDefined && ( controller->GetShadowColor() != color ) )
219           {
220             controller->SetShadowColor( color );
221             update = true;
222           }
223
224           if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
225           {
226             controller->SetShadowOffset( offset );
227             update = true;
228           }
229         }
230         case EffectStyle::INPUT:
231         {
232           // Sets the input shadow values.
233           // TODO: to be implemented.
234           break;
235         }
236       }
237     }
238   }
239
240   return update;
241 }
242
243 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
244 {
245   if( controller )
246   {
247     switch( type )
248     {
249       case EffectStyle::DEFAULT:
250       {
251         value = controller->GetDefaultShadowProperties();
252         break;
253       }
254       case EffectStyle::INPUT:
255       {
256         value = controller->GetInputShadowProperties();
257         break;
258       }
259     }
260   }
261 }
262
263 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
264 {
265   bool update = false;
266
267   if( controller )
268   {
269     const std::string properties = value.Get< std::string >();
270
271     switch( type )
272     {
273       case EffectStyle::DEFAULT:
274       {
275         // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
276         controller->SetDefaultEmbossProperties( properties );
277         break;
278       }
279       case EffectStyle::INPUT:
280       {
281         // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
282         controller->SetInputEmbossProperties( properties );
283         break;
284       }
285     }
286   }
287
288   return update;
289 }
290
291 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
292 {
293   if( controller )
294   {
295     switch( type )
296     {
297       case EffectStyle::DEFAULT:
298       {
299         value = controller->GetDefaultEmbossProperties();
300         break;
301       }
302       case EffectStyle::INPUT:
303       {
304         value = controller->GetInputEmbossProperties();
305         break;
306       }
307     }
308   }
309 }
310
311 bool SetOutlineProperties( 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 outline's properties string to be recovered by the GetOutlineProperties() function.
324         controller->SetDefaultOutlineProperties( properties );
325         break;
326       }
327       case EffectStyle::INPUT:
328       {
329         // Stores the input outline's properties string to be recovered by the GetOutlineProperties() function.
330         controller->SetInputOutlineProperties( properties );
331         break;
332       }
333     }
334   }
335
336   return update;
337 }
338
339 void GetOutlineProperties( 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->GetDefaultOutlineProperties();
348         break;
349       }
350       case EffectStyle::INPUT:
351       {
352         value = controller->GetInputOutlineProperties();
353         break;
354       }
355     }
356   }
357 }
358
359 } // namespace Text
360
361 } // namespace Toolkit
362
363 } // namespace Dali