Merge "Fix the texture bleeding with wrapping in atlas" into devel/master
[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     // Parses and applies the style.
57     Property::Map map;
58     ParsePropertyString( style, map );
59
60     if( !map.Empty() )
61     {
62       /// Weight key
63       Property::Value* weightValue = map.Find( WEIGHT_KEY );
64
65       FontWeight weight = TextAbstraction::FontWeight::NONE;
66       const bool weightDefined = weightValue != NULL;
67       if( weightDefined )
68       {
69         const std::string weightStr = weightValue->Get<std::string>();
70
71         Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
72                                                  FONT_WEIGHT_STRING_TABLE,
73                                                  FONT_WEIGHT_STRING_TABLE_COUNT,
74                                                  weight );
75       }
76
77       /// Width key
78       Property::Value* widthValue = map.Find( WIDTH_KEY );
79
80       FontWidth width = TextAbstraction::FontWidth::NONE;
81       const bool widthDefined = widthValue != NULL;
82       if( widthDefined )
83       {
84         const std::string widthStr = widthValue->Get<std::string>();
85
86         Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
87                                                 FONT_WIDTH_STRING_TABLE,
88                                                 FONT_WIDTH_STRING_TABLE_COUNT,
89                                                 width );
90       }
91
92       /// Slant key
93       Property::Value* slantValue = map.Find( SLANT_KEY );
94
95       FontSlant slant = TextAbstraction::FontSlant::NONE;
96       const bool slantDefined = slantValue != NULL;
97       if( slantDefined )
98       {
99         const std::string slantStr = slantValue->Get<std::string>();
100
101         Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
102                                                 FONT_SLANT_STRING_TABLE,
103                                                 FONT_SLANT_STRING_TABLE_COUNT,
104                                                 slant );
105       }
106
107       switch( type )
108       {
109         case FontStyle::DEFAULT:
110         {
111           // Sets the default font's style values.
112           if( !weightDefined ||
113               ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
114           {
115             controller->SetDefaultFontWeight( weight );
116           }
117
118           if( !widthDefined ||
119               ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
120           {
121             controller->SetDefaultFontWidth( width );
122           }
123
124           if( !slantDefined ||
125               ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
126           {
127             controller->SetDefaultFontSlant( slant );
128           }
129           break;
130         }
131         case FontStyle::INPUT:
132         {
133           // Sets the input font's style values.
134           if( !weightDefined ||
135               ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
136           {
137             controller->SetInputFontWeight( weight );
138           }
139
140           if( !widthDefined ||
141               ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
142           {
143             controller->SetInputFontWidth( width );
144           }
145
146           if( !slantDefined ||
147               ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
148           {
149             controller->SetInputFontSlant( slant );
150           }
151           break;
152         }
153       } // switch
154     } // map not empty
155     else
156     {
157       switch( type )
158       {
159         case FontStyle::DEFAULT:
160         {
161           controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
162           controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
163           controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
164           break;
165         }
166         case FontStyle::INPUT:
167         {
168           controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
169           controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
170           controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
171           break;
172         }
173       } // switch
174     } // map.Empty()
175   } // controller
176 }
177
178 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
179 {
180   if( controller )
181   {
182     const bool isDefaultStyle = FontStyle::DEFAULT == type;
183
184     bool weightDefined = false;
185     bool widthDefined = false;
186     bool slantDefined = false;
187     FontWeight weight = TextAbstraction::FontWeight::NONE;
188     FontWidth width = TextAbstraction::FontWidth::NONE;
189     FontSlant slant = TextAbstraction::FontSlant::NONE;
190
191     if( isDefaultStyle )
192     {
193       weightDefined = controller->IsDefaultFontWeightDefined();
194       widthDefined = controller->IsDefaultFontWidthDefined();
195       slantDefined = controller->IsDefaultFontSlantDefined();
196
197       if( weightDefined )
198       {
199         weight = controller->GetDefaultFontWeight();
200       }
201
202       if( widthDefined )
203       {
204         width = controller->GetDefaultFontWidth();
205       }
206
207       if( slantDefined )
208       {
209         slant = controller->GetDefaultFontSlant();
210       }
211     }
212     else
213     {
214       weightDefined = controller->IsInputFontWeightDefined();
215       widthDefined = controller->IsInputFontWidthDefined();
216       slantDefined = controller->IsInputFontSlantDefined();
217
218       if( weightDefined )
219       {
220         weight = controller->GetInputFontWeight();
221       }
222
223       if( widthDefined )
224       {
225         width = controller->GetInputFontWidth();
226       }
227
228       if( slantDefined )
229       {
230         slant = controller->GetInputFontSlant();
231       }
232     }
233
234     if( weightDefined || widthDefined || slantDefined )
235     {
236       std::string styleString("{");
237       if( weightDefined )
238       {
239         if( TextAbstraction::FontWeight::NONE != weight )
240         {
241           const std::string weightStr( GetEnumerationName( weight,
242                                                            FONT_WEIGHT_STRING_TABLE,
243                                                            FONT_WEIGHT_STRING_TABLE_COUNT ) );
244
245           styleString += "\"weight\":\"" + weightStr + "\"";
246         }
247         else
248         {
249           weightDefined = false;
250         }
251       }
252
253       if( widthDefined )
254       {
255         if( TextAbstraction::FontWidth::NONE != width )
256         {
257           const std::string widthStr( GetEnumerationName( width,
258                                                           FONT_WIDTH_STRING_TABLE,
259                                                           FONT_WIDTH_STRING_TABLE_COUNT ) );
260
261           if( weightDefined )
262           {
263             styleString += ",";
264           }
265           styleString += "\"width\":\"" + widthStr + "\"";
266         }
267         else
268         {
269           widthDefined = false;
270         }
271       }
272
273       if( slantDefined )
274       {
275         if( TextAbstraction::FontSlant::NONE != slant )
276         {
277           const std::string slantStr( GetEnumerationName( slant,
278                                                           FONT_SLANT_STRING_TABLE,
279                                                           FONT_SLANT_STRING_TABLE_COUNT ) );
280
281           if( weightDefined || widthDefined )
282           {
283             styleString += ",";
284           }
285           styleString += "\"slant\":\"" + slantStr + "\"";
286         }
287         else
288         {
289           slantDefined = false;
290         }
291       }
292
293       if( weightDefined || widthDefined || slantDefined )
294       {
295         styleString += "}";
296       }
297       else
298       {
299         styleString.clear();
300       }
301
302       value = styleString;
303     }
304   }
305 }
306
307 FontWeight StringToWeight( const char* const weightStr )
308 {
309   FontWeight weight = TextAbstraction::FontWeight::NORMAL;
310   Scripting::GetEnumeration<FontWeight>( weightStr,
311                                          FONT_WEIGHT_STRING_TABLE,
312                                          FONT_WEIGHT_STRING_TABLE_COUNT,
313                                          weight );
314
315   return weight;
316 }
317
318 FontWidth StringToWidth( const char* const widthStr )
319 {
320   FontWidth width = TextAbstraction::FontWidth::NORMAL;
321   Scripting::GetEnumeration<FontWidth>( widthStr,
322                                         FONT_WIDTH_STRING_TABLE,
323                                         FONT_WIDTH_STRING_TABLE_COUNT,
324                                         width );
325
326   return width;
327 }
328
329 FontSlant StringToSlant( const char* const slantStr )
330 {
331   FontSlant slant = TextAbstraction::FontSlant::NORMAL;
332   Scripting::GetEnumeration<FontSlant>( slantStr,
333                                         FONT_SLANT_STRING_TABLE,
334                                         FONT_SLANT_STRING_TABLE_COUNT,
335                                         slant );
336
337   return slant;
338 }
339
340 } // namespace Text
341
342 } // namespace Toolkit
343
344 } // namespace Dali