Fix for multi-language support.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-font-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-font-style.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/property-string-parser.h>
26
27 namespace Dali
28 {
29
30 namespace Toolkit
31 {
32
33 namespace Text
34 {
35
36 namespace
37 {
38 const std::string STYLE_KEY( "style" );
39 const std::string WEIGHT_KEY( "weight" );
40 const std::string WIDTH_KEY( "width" );
41 const std::string SLANT_KEY( "slant" );
42
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
45 #endif
46
47 } // namespace
48
49 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
50 {
51   if( controller )
52   {
53     const std::string style = value.Get< std::string >();
54     DALI_LOG_INFO( gLogFilter, Debug::General, "Text Control %p FONT_STYLE %s\n", controller.Get(), style.c_str() );
55
56     switch( type )
57     {
58       case FontStyle::DEFAULT:
59       {
60         // Stores the default font's style string to be recovered by the GetFontStyleProperty() function.
61         controller->SetDefaultFontStyle( style );
62         break;
63       }
64       case FontStyle::INPUT:
65       {
66         // Stores the input font's style string to be recovered by the GetFontStyleProperty() function.
67         controller->SetInputFontStyle( style );
68         break;
69       }
70     }
71
72     // Parses and applies the style.
73     Property::Map map;
74     ParsePropertyString( style, map );
75
76     if( !map.Empty() )
77     {
78       /// Weight key
79       Property::Value* weightValue = map.Find( WEIGHT_KEY );
80
81       FontWeight weight = TextAbstraction::FontWeight::NONE;
82       const bool weightDefined = weightValue != NULL;
83       if( weightDefined )
84       {
85         const std::string weightStr = weightValue->Get<std::string>();
86
87         Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
88                                                  FONT_WEIGHT_STRING_TABLE,
89                                                  FONT_WEIGHT_STRING_TABLE_COUNT,
90                                                  weight );
91       }
92
93       /// Width key
94       Property::Value* widthValue = map.Find( WIDTH_KEY );
95
96       FontWidth width = TextAbstraction::FontWidth::NONE;
97       const bool widthDefined = widthValue != NULL;
98       if( widthDefined )
99       {
100         const std::string widthStr = widthValue->Get<std::string>();
101
102         Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
103                                                 FONT_WIDTH_STRING_TABLE,
104                                                 FONT_WIDTH_STRING_TABLE_COUNT,
105                                                 width );
106       }
107
108       /// Slant key
109       Property::Value* slantValue = map.Find( SLANT_KEY );
110
111       FontSlant slant = TextAbstraction::FontSlant::NONE;
112       const bool slantDefined = slantValue != NULL;
113       if( slantDefined )
114       {
115         const std::string slantStr = slantValue->Get<std::string>();
116
117         Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
118                                                 FONT_SLANT_STRING_TABLE,
119                                                 FONT_SLANT_STRING_TABLE_COUNT,
120                                                 slant );
121       }
122
123       switch( type )
124       {
125         case FontStyle::DEFAULT:
126         {
127           // Sets the default font's style values.
128           if( !weightDefined ||
129               ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
130           {
131             controller->SetDefaultFontWeight( weight );
132           }
133
134           if( !widthDefined ||
135               ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
136           {
137             controller->SetDefaultFontWidth( width );
138           }
139
140           if( !slantDefined ||
141               ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
142           {
143             controller->SetDefaultFontSlant( slant );
144           }
145           break;
146         }
147         case FontStyle::INPUT:
148         {
149           // Sets the input font's style values.
150           if( !weightDefined ||
151               ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
152           {
153             controller->SetInputFontWeight( weight );
154           }
155
156           if( !widthDefined ||
157               ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
158           {
159             controller->SetInputFontWidth( width );
160           }
161
162           if( !slantDefined ||
163               ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
164           {
165             controller->SetInputFontSlant( slant );
166           }
167           break;
168         }
169       }
170     } // map not empty
171     else
172     {
173       switch( type )
174       {
175         case FontStyle::DEFAULT:
176         {
177           controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
178           controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
179           controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
180           break;
181         }
182         case FontStyle::INPUT:
183         {
184           controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
185           controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
186           controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
187           break;
188         }
189       }
190     }
191   }
192 }
193
194 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
195 {
196   if( controller )
197   {
198     switch( type )
199     {
200       case FontStyle::DEFAULT:
201       {
202         value = controller->GetDefaultFontStyle();
203         break;
204       }
205       case FontStyle::INPUT:
206       {
207         value = controller->GetInputFontStyle();
208         break;
209       }
210     }
211   }
212 }
213
214 FontWeight StringToWeight( const char* const weightStr )
215 {
216   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
217   Scripting::GetEnumeration<FontWeight>( weightStr,
218                                          FONT_WEIGHT_STRING_TABLE,
219                                          FONT_WEIGHT_STRING_TABLE_COUNT,
220                                          weight );
221
222   return weight;
223 }
224
225 FontWidth StringToWidth( const char* const widthStr )
226 {
227   FontWidth width = TextAbstraction::FontWidth::NORMAL;
228   Scripting::GetEnumeration<FontWidth>( widthStr,
229                                         FONT_WIDTH_STRING_TABLE,
230                                         FONT_WIDTH_STRING_TABLE_COUNT,
231                                         width );
232
233   return width;
234 }
235
236 FontSlant StringToSlant( const char* const slantStr )
237 {
238   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
239   Scripting::GetEnumeration<FontSlant>( slantStr,
240                                         FONT_SLANT_STRING_TABLE,
241                                         FONT_SLANT_STRING_TABLE_COUNT,
242                                         slant );
243
244   return slant;
245 }
246
247 } // namespace Text
248
249 } // namespace Toolkit
250
251 } // namespace Dali