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" );
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
54 void SetFontFamilyProperty( ControllerPtr controller, const Property::Value& value )
58 const std::string fontFamilyValue = value.Get<std::string>();
60 if( fontFamilyValue.empty() )
62 // Resets the default's font family name.
63 controller->SetDefaultFontFamily( "" );
68 ParsePropertyString( fontFamilyValue, map );
72 // There is no map. The font has been passed as a font's family name with no format.
73 controller->SetDefaultFontFamily( fontFamilyValue );
78 Property::Value* familyValue = map.Find( FAMILY_KEY );
80 std::string fontFamilyName;
81 if( NULL != familyValue )
83 fontFamilyName = familyValue->Get<std::string>();
87 Property::Value* typeValue = map.Find( TYPE_KEY );
90 if( NULL != typeValue )
92 typeStr = typeValue->Get<std::string>();
95 if( TokenComparison( SYSTEM_TOKEN, typeStr.c_str(), typeStr.size() ) )
97 controller->UpdateAfterFontChange( fontFamilyName );
101 controller->SetDefaultFontFamily( fontFamilyName );
107 void SetFontStyleProperty( ControllerPtr controller, const Property::Value& value, FontStyle::Type type )
112 if( Property::STRING == value.GetType() )
114 const std::string& fontStyleProperties = value.Get<std::string>();
116 ParsePropertyString( fontStyleProperties, map );
117 controller->FontStyleSetByString( true );
122 map = value.Get<Property::Map>();
123 controller->FontStyleSetByString( false );
129 Property::Value* weightValue = map.Find( WEIGHT_KEY );
131 FontWeight weight = TextAbstraction::FontWeight::NONE;
132 const bool weightDefined = weightValue != NULL;
135 const std::string weightStr = weightValue->Get<std::string>();
137 Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
138 FONT_WEIGHT_STRING_TABLE,
139 FONT_WEIGHT_STRING_TABLE_COUNT,
144 Property::Value* widthValue = map.Find( WIDTH_KEY );
146 FontWidth width = TextAbstraction::FontWidth::NONE;
147 const bool widthDefined = widthValue != NULL;
150 const std::string widthStr = widthValue->Get<std::string>();
152 Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
153 FONT_WIDTH_STRING_TABLE,
154 FONT_WIDTH_STRING_TABLE_COUNT,
159 Property::Value* slantValue = map.Find( SLANT_KEY );
161 FontSlant slant = TextAbstraction::FontSlant::NONE;
162 const bool slantDefined = slantValue != NULL;
165 const std::string slantStr = slantValue->Get<std::string>();
167 Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
168 FONT_SLANT_STRING_TABLE,
169 FONT_SLANT_STRING_TABLE_COUNT,
175 case FontStyle::DEFAULT:
177 // Sets the default font's style values.
178 if( !weightDefined ||
179 ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
181 controller->SetDefaultFontWeight( weight );
185 ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
187 controller->SetDefaultFontWidth( width );
191 ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
193 controller->SetDefaultFontSlant( slant );
197 case FontStyle::INPUT:
199 // Sets the input font's style values.
200 if( !weightDefined ||
201 ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
203 controller->SetInputFontWeight( weight );
207 ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
209 controller->SetInputFontWidth( width );
213 ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
215 controller->SetInputFontSlant( slant );
219 case FontStyle::PLACEHOLDER:
221 // Sets the placeholder text font's style values.
222 if( !weightDefined ||
223 ( weightDefined && ( controller->GetPlaceholderTextFontWeight() != weight ) ) )
225 controller->SetPlaceholderTextFontWeight( weight );
229 ( widthDefined && ( controller->GetPlaceholderTextFontWidth() != width ) ) )
231 controller->SetPlaceholderTextFontWidth( width );
235 ( slantDefined && ( controller->GetPlaceholderTextFontSlant() != slant ) ) )
237 controller->SetPlaceholderTextFontSlant( slant );
247 case FontStyle::DEFAULT:
249 controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
250 controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
251 controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
254 case FontStyle::INPUT:
256 controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
257 controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
258 controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
261 case FontStyle::PLACEHOLDER:
263 controller->SetPlaceholderTextFontWeight( TextAbstraction::FontWeight::NONE );
264 controller->SetPlaceholderTextFontWidth( TextAbstraction::FontWidth::NONE );
265 controller->SetPlaceholderTextFontSlant( TextAbstraction::FontSlant::NONE );
273 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
277 const bool isSetbyString = controller->IsFontStyleSetByString();
279 bool weightDefined = false;
280 bool widthDefined = false;
281 bool slantDefined = false;
282 FontWeight weight = TextAbstraction::FontWeight::NONE;
283 FontWidth width = TextAbstraction::FontWidth::NONE;
284 FontSlant slant = TextAbstraction::FontSlant::NONE;
288 case FontStyle::DEFAULT:
290 weightDefined = controller->IsDefaultFontWeightDefined();
291 widthDefined = controller->IsDefaultFontWidthDefined();
292 slantDefined = controller->IsDefaultFontSlantDefined();
296 weight = controller->GetDefaultFontWeight();
301 width = controller->GetDefaultFontWidth();
306 slant = controller->GetDefaultFontSlant();
310 case FontStyle::INPUT:
312 weightDefined = controller->IsInputFontWeightDefined();
313 widthDefined = controller->IsInputFontWidthDefined();
314 slantDefined = controller->IsInputFontSlantDefined();
318 weight = controller->GetInputFontWeight();
323 width = controller->GetInputFontWidth();
328 slant = controller->GetInputFontSlant();
332 case FontStyle::PLACEHOLDER:
334 // The type is FontStyle::PLACEHOLDER
335 weightDefined = controller->IsPlaceholderTextFontWeightDefined();
336 widthDefined = controller->IsPlaceholderTextFontWidthDefined();
337 slantDefined = controller->IsPlaceholderTextFontSlantDefined();
341 weight = controller->GetPlaceholderTextFontWeight();
346 width = controller->GetPlaceholderTextFontWidth();
351 slant = controller->GetPlaceholderTextFontSlant();
363 if( TextAbstraction::FontWeight::NONE != weight )
365 const std::string weightStr( GetEnumerationName( weight,
366 FONT_WEIGHT_STRING_TABLE,
367 FONT_WEIGHT_STRING_TABLE_COUNT ) );
369 map.Insert( WEIGHT_KEY, weightStr );
375 if( TextAbstraction::FontWidth::NONE != width )
377 const std::string widthStr( GetEnumerationName( width,
378 FONT_WIDTH_STRING_TABLE,
379 FONT_WIDTH_STRING_TABLE_COUNT ) );
381 map.Insert( WIDTH_KEY, widthStr );
387 if( TextAbstraction::FontSlant::NONE != slant )
389 const std::string slantStr( GetEnumerationName( slant,
390 FONT_SLANT_STRING_TABLE,
391 FONT_SLANT_STRING_TABLE_COUNT ) );
393 map.Insert( SLANT_KEY, slantStr );
401 std::string fontStyleProperties = "{";
405 if( TextAbstraction::FontWeight::NONE != weight )
407 const std::string weightStr( GetEnumerationName( weight,
408 FONT_WEIGHT_STRING_TABLE,
409 FONT_WEIGHT_STRING_TABLE_COUNT ) );
411 fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
417 if( TextAbstraction::FontWidth::NONE != width )
419 const std::string widthStr( GetEnumerationName( width,
420 FONT_WIDTH_STRING_TABLE,
421 FONT_WIDTH_STRING_TABLE_COUNT ) );
422 fontStyleProperties += "\"width\":\"" + widthStr + "\",";
428 if( TextAbstraction::FontSlant::NONE != slant )
430 const std::string slantStr( GetEnumerationName( slant,
431 FONT_SLANT_STRING_TABLE,
432 FONT_SLANT_STRING_TABLE_COUNT ) );
434 fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
438 // If last character is comma, it will be removed.
439 if((*fontStyleProperties.rbegin()) == ',' )
441 fontStyleProperties = fontStyleProperties.substr( 0, fontStyleProperties.size()-1 );
443 fontStyleProperties += "}";
445 value = fontStyleProperties;
450 FontWeight StringToWeight( const char* const weightStr )
452 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
453 Scripting::GetEnumeration<FontWeight>( weightStr,
454 FONT_WEIGHT_STRING_TABLE,
455 FONT_WEIGHT_STRING_TABLE_COUNT,
461 FontWidth StringToWidth( const char* const widthStr )
463 FontWidth width = TextAbstraction::FontWidth::NORMAL;
464 Scripting::GetEnumeration<FontWidth>( widthStr,
465 FONT_WIDTH_STRING_TABLE,
466 FONT_WIDTH_STRING_TABLE_COUNT,
472 FontSlant StringToSlant( const char* const slantStr )
474 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
475 Scripting::GetEnumeration<FontSlant>( slantStr,
476 FONT_SLANT_STRING_TABLE,
477 FONT_SLANT_STRING_TABLE_COUNT,
485 } // namespace Toolkit