Remove Geometry::QUAD() usage in Toolkit
[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::NORMAL;
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::NORMAL;
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::NORMAL;
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 && ( controller->GetDefaultFontWeight() != weight ) )
129           {
130             controller->SetDefaultFontWeight( weight );
131           }
132
133           if( widthDefined && ( controller->GetDefaultFontWidth() != width ) )
134           {
135             controller->SetDefaultFontWidth( width );
136           }
137
138           if( slantDefined && ( controller->GetDefaultFontSlant() != slant ) )
139           {
140             controller->SetDefaultFontSlant( slant );
141           }
142           break;
143         }
144         case FontStyle::INPUT:
145         {
146           // Sets the input font's style values.
147           if( weightDefined && ( controller->GetInputFontWeight() != weight ) )
148           {
149             controller->SetInputFontWeight( weight );
150           }
151
152           if( widthDefined && ( controller->GetInputFontWidth() != width ) )
153           {
154             controller->SetInputFontWidth( width );
155           }
156
157           if( slantDefined && ( controller->GetInputFontSlant() != slant ) )
158           {
159             controller->SetInputFontSlant( slant );
160           }
161           break;
162         }
163       }
164     }
165   }
166 }
167
168 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
169 {
170   if( controller )
171   {
172     switch( type )
173     {
174       case FontStyle::DEFAULT:
175       {
176         value = controller->GetDefaultFontStyle();
177         break;
178       }
179       case FontStyle::INPUT:
180       {
181         value = controller->GetInputFontStyle();
182         break;
183       }
184     }
185   }
186 }
187
188 FontWeight StringToWeight( const char* const weightStr )
189 {
190   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
191   Scripting::GetEnumeration<FontWeight>( weightStr,
192                                          FONT_WEIGHT_STRING_TABLE,
193                                          FONT_WEIGHT_STRING_TABLE_COUNT,
194                                          weight );
195
196   return weight;
197 }
198
199 FontWidth StringToWidth( const char* const widthStr )
200 {
201   FontWidth width = TextAbstraction::FontWidth::NORMAL;
202   Scripting::GetEnumeration<FontWidth>( widthStr,
203                                         FONT_WIDTH_STRING_TABLE,
204                                         FONT_WIDTH_STRING_TABLE_COUNT,
205                                         width );
206
207   return width;
208 }
209
210 FontSlant StringToSlant( const char* const slantStr )
211 {
212   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
213   Scripting::GetEnumeration<FontSlant>( slantStr,
214                                         FONT_SLANT_STRING_TABLE,
215                                         FONT_SLANT_STRING_TABLE_COUNT,
216                                         slant );
217
218   return slant;
219 }
220
221 } // namespace Text
222
223 } // namespace Toolkit
224
225 } // namespace Dali