2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/text-font-style.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/text/property-string-parser.h>
26 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
39 const std::string STYLE_KEY( "style" );
40 const std::string WEIGHT_KEY( "weight" );
41 const std::string WIDTH_KEY( "width" );
42 const std::string SLANT_KEY( "slant" );
43 const std::string FAMILY_KEY( "family" );
44 const std::string TYPE_KEY( "type" );
46 const std::string SYSTEM_TOKEN( "system" );
50 void SetFontFamilyProperty( ControllerPtr controller, const Property::Value& value )
54 const std::string fontFamilyValue = value.Get<std::string>();
56 if( fontFamilyValue.empty() )
58 // Resets the default's font family name.
59 controller->SetDefaultFontFamily( "" );
64 ParsePropertyString( fontFamilyValue, map );
68 // There is no map. The font has been passed as a font's family name with no format.
69 controller->SetDefaultFontFamily( fontFamilyValue );
74 Property::Value* familyValue = map.Find( FAMILY_KEY );
76 std::string fontFamilyName;
77 if( NULL != familyValue )
79 fontFamilyName = familyValue->Get<std::string>();
83 Property::Value* typeValue = map.Find( TYPE_KEY );
86 if( NULL != typeValue )
88 typeStr = typeValue->Get<std::string>();
91 if( TokenComparison( SYSTEM_TOKEN, typeStr.c_str(), typeStr.size() ) )
93 controller->UpdateAfterFontChange( fontFamilyName );
97 controller->SetDefaultFontFamily( fontFamilyName );
103 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
108 if( Property::STRING == value.GetType() )
110 const std::string& fontStyleProperties = value.Get<std::string>();
112 ParsePropertyString( fontStyleProperties, map );
113 controller->FontStyleSetByString( true );
118 map = value.Get<Property::Map>();
119 controller->FontStyleSetByString( false );
125 Property::Value* weightValue = map.Find( WEIGHT_KEY );
127 FontWeight weight = TextAbstraction::FontWeight::NONE;
128 const bool weightDefined = weightValue != NULL;
131 const std::string weightStr = weightValue->Get<std::string>();
133 Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
134 FONT_WEIGHT_STRING_TABLE,
135 FONT_WEIGHT_STRING_TABLE_COUNT,
140 Property::Value* widthValue = map.Find( WIDTH_KEY );
142 FontWidth width = TextAbstraction::FontWidth::NONE;
143 const bool widthDefined = widthValue != NULL;
146 const std::string widthStr = widthValue->Get<std::string>();
148 Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
149 FONT_WIDTH_STRING_TABLE,
150 FONT_WIDTH_STRING_TABLE_COUNT,
155 Property::Value* slantValue = map.Find( SLANT_KEY );
157 FontSlant slant = TextAbstraction::FontSlant::NONE;
158 const bool slantDefined = slantValue != NULL;
161 const std::string slantStr = slantValue->Get<std::string>();
163 Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
164 FONT_SLANT_STRING_TABLE,
165 FONT_SLANT_STRING_TABLE_COUNT,
171 case FontStyle::DEFAULT:
173 // Sets the default font's style values.
174 if( !weightDefined ||
175 ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
177 controller->SetDefaultFontWeight( weight );
181 ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
183 controller->SetDefaultFontWidth( width );
187 ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
189 controller->SetDefaultFontSlant( slant );
193 case FontStyle::INPUT:
195 // Sets the input font's style values.
196 if( !weightDefined ||
197 ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
199 controller->SetInputFontWeight( weight );
203 ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
205 controller->SetInputFontWidth( width );
209 ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
211 controller->SetInputFontSlant( slant );
215 case FontStyle::PLACEHOLDER:
217 // Sets the placeholder text font's style values.
218 if( !weightDefined ||
219 ( weightDefined && ( controller->GetPlaceholderTextFontWeight() != weight ) ) )
221 controller->SetPlaceholderTextFontWeight( weight );
225 ( widthDefined && ( controller->GetPlaceholderTextFontWidth() != width ) ) )
227 controller->SetPlaceholderTextFontWidth( width );
231 ( slantDefined && ( controller->GetPlaceholderTextFontSlant() != slant ) ) )
233 controller->SetPlaceholderTextFontSlant( slant );
243 case FontStyle::DEFAULT:
245 controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
246 controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
247 controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
250 case FontStyle::INPUT:
252 controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
253 controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
254 controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
257 case FontStyle::PLACEHOLDER:
259 controller->SetPlaceholderTextFontWeight( TextAbstraction::FontWeight::NONE );
260 controller->SetPlaceholderTextFontWidth( TextAbstraction::FontWidth::NONE );
261 controller->SetPlaceholderTextFontSlant( TextAbstraction::FontSlant::NONE );
269 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
273 const bool isSetbyString = controller->IsFontStyleSetByString();
275 bool weightDefined = false;
276 bool widthDefined = false;
277 bool slantDefined = false;
278 FontWeight weight = TextAbstraction::FontWeight::NONE;
279 FontWidth width = TextAbstraction::FontWidth::NONE;
280 FontSlant slant = TextAbstraction::FontSlant::NONE;
284 case FontStyle::DEFAULT:
286 weightDefined = controller->IsDefaultFontWeightDefined();
287 widthDefined = controller->IsDefaultFontWidthDefined();
288 slantDefined = controller->IsDefaultFontSlantDefined();
292 weight = controller->GetDefaultFontWeight();
297 width = controller->GetDefaultFontWidth();
302 slant = controller->GetDefaultFontSlant();
306 case FontStyle::INPUT:
308 weightDefined = controller->IsInputFontWeightDefined();
309 widthDefined = controller->IsInputFontWidthDefined();
310 slantDefined = controller->IsInputFontSlantDefined();
314 weight = controller->GetInputFontWeight();
319 width = controller->GetInputFontWidth();
324 slant = controller->GetInputFontSlant();
328 case FontStyle::PLACEHOLDER:
330 // The type is FontStyle::PLACEHOLDER
331 weightDefined = controller->IsPlaceholderTextFontWeightDefined();
332 widthDefined = controller->IsPlaceholderTextFontWidthDefined();
333 slantDefined = controller->IsPlaceholderTextFontSlantDefined();
337 weight = controller->GetPlaceholderTextFontWeight();
342 width = controller->GetPlaceholderTextFontWidth();
347 slant = controller->GetPlaceholderTextFontSlant();
359 if( TextAbstraction::FontWeight::NONE != weight )
361 const std::string weightStr( GetEnumerationName( weight,
362 FONT_WEIGHT_STRING_TABLE,
363 FONT_WEIGHT_STRING_TABLE_COUNT ) );
365 map.Insert( WEIGHT_KEY, weightStr );
371 if( TextAbstraction::FontWidth::NONE != width )
373 const std::string widthStr( GetEnumerationName( width,
374 FONT_WIDTH_STRING_TABLE,
375 FONT_WIDTH_STRING_TABLE_COUNT ) );
377 map.Insert( WIDTH_KEY, widthStr );
383 if( TextAbstraction::FontSlant::NONE != slant )
385 const std::string slantStr( GetEnumerationName( slant,
386 FONT_SLANT_STRING_TABLE,
387 FONT_SLANT_STRING_TABLE_COUNT ) );
389 map.Insert( SLANT_KEY, slantStr );
397 std::string fontStyleProperties = "{";
401 if( TextAbstraction::FontWeight::NONE != weight )
403 const std::string weightStr( GetEnumerationName( weight,
404 FONT_WEIGHT_STRING_TABLE,
405 FONT_WEIGHT_STRING_TABLE_COUNT ) );
407 fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
413 if( TextAbstraction::FontWidth::NONE != width )
415 const std::string widthStr( GetEnumerationName( width,
416 FONT_WIDTH_STRING_TABLE,
417 FONT_WIDTH_STRING_TABLE_COUNT ) );
418 fontStyleProperties += "\"width\":\"" + widthStr + "\",";
424 if( TextAbstraction::FontSlant::NONE != slant )
426 const std::string slantStr( GetEnumerationName( slant,
427 FONT_SLANT_STRING_TABLE,
428 FONT_SLANT_STRING_TABLE_COUNT ) );
430 fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
434 // If last character is comma, it will be removed.
435 if((*fontStyleProperties.rbegin()) == ',' )
437 fontStyleProperties = fontStyleProperties.substr( 0, fontStyleProperties.size()-1 );
439 fontStyleProperties += "}";
441 value = fontStyleProperties;
446 FontWeight StringToWeight( const char* const weightStr )
448 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
449 Scripting::GetEnumeration<FontWeight>( weightStr,
450 FONT_WEIGHT_STRING_TABLE,
451 FONT_WEIGHT_STRING_TABLE_COUNT,
457 FontWidth StringToWidth( const char* const widthStr )
459 FontWidth width = TextAbstraction::FontWidth::NORMAL;
460 Scripting::GetEnumeration<FontWidth>( widthStr,
461 FONT_WIDTH_STRING_TABLE,
462 FONT_WIDTH_STRING_TABLE_COUNT,
468 FontSlant StringToSlant( const char* const slantStr )
470 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
471 Scripting::GetEnumeration<FontSlant>( slantStr,
472 FONT_SLANT_STRING_TABLE,
473 FONT_SLANT_STRING_TABLE_COUNT,
481 } // namespace Toolkit