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 );
225 case FontStyle::DEFAULT:
227 controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
228 controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
229 controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
232 case FontStyle::INPUT:
234 controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
235 controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
236 controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
244 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
248 const bool isDefaultStyle = FontStyle::DEFAULT == type;
249 const bool isSetbyString = controller->IsFontStyleSetByString();
251 bool weightDefined = false;
252 bool widthDefined = false;
253 bool slantDefined = false;
254 FontWeight weight = TextAbstraction::FontWeight::NONE;
255 FontWidth width = TextAbstraction::FontWidth::NONE;
256 FontSlant slant = TextAbstraction::FontSlant::NONE;
260 weightDefined = controller->IsDefaultFontWeightDefined();
261 widthDefined = controller->IsDefaultFontWidthDefined();
262 slantDefined = controller->IsDefaultFontSlantDefined();
266 weight = controller->GetDefaultFontWeight();
271 width = controller->GetDefaultFontWidth();
276 slant = controller->GetDefaultFontSlant();
281 weightDefined = controller->IsInputFontWeightDefined();
282 widthDefined = controller->IsInputFontWidthDefined();
283 slantDefined = controller->IsInputFontSlantDefined();
287 weight = controller->GetInputFontWeight();
292 width = controller->GetInputFontWidth();
297 slant = controller->GetInputFontSlant();
307 if( TextAbstraction::FontWeight::NONE != weight )
309 const std::string weightStr( GetEnumerationName( weight,
310 FONT_WEIGHT_STRING_TABLE,
311 FONT_WEIGHT_STRING_TABLE_COUNT ) );
313 map.Insert( WEIGHT_KEY, weightStr );
319 if( TextAbstraction::FontWidth::NONE != width )
321 const std::string widthStr( GetEnumerationName( width,
322 FONT_WIDTH_STRING_TABLE,
323 FONT_WIDTH_STRING_TABLE_COUNT ) );
325 map.Insert( WIDTH_KEY, widthStr );
331 if( TextAbstraction::FontSlant::NONE != slant )
333 const std::string slantStr( GetEnumerationName( slant,
334 FONT_SLANT_STRING_TABLE,
335 FONT_SLANT_STRING_TABLE_COUNT ) );
337 map.Insert( SLANT_KEY, slantStr );
345 std::string fontStyleProperties = "{";
349 if( TextAbstraction::FontWeight::NONE != weight )
351 const std::string weightStr( GetEnumerationName( weight,
352 FONT_WEIGHT_STRING_TABLE,
353 FONT_WEIGHT_STRING_TABLE_COUNT ) );
355 fontStyleProperties += "\"weight\":\"" + weightStr + "\",";
361 if( TextAbstraction::FontWidth::NONE != width )
363 const std::string widthStr( GetEnumerationName( width,
364 FONT_WIDTH_STRING_TABLE,
365 FONT_WIDTH_STRING_TABLE_COUNT ) );
366 fontStyleProperties += "\"width\":\"" + widthStr + "\",";
372 if( TextAbstraction::FontSlant::NONE != slant )
374 const std::string slantStr( GetEnumerationName( slant,
375 FONT_SLANT_STRING_TABLE,
376 FONT_SLANT_STRING_TABLE_COUNT ) );
378 fontStyleProperties += "\"slant\":\"" + slantStr + "\"";
382 // If last character is comma, it will be removed.
383 if((*fontStyleProperties.rbegin()) == ',' )
385 fontStyleProperties = fontStyleProperties.substr( 0, fontStyleProperties.size()-1 );
387 fontStyleProperties += "}";
389 value = fontStyleProperties;
394 FontWeight StringToWeight( const char* const weightStr )
396 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
397 Scripting::GetEnumeration<FontWeight>( weightStr,
398 FONT_WEIGHT_STRING_TABLE,
399 FONT_WEIGHT_STRING_TABLE_COUNT,
405 FontWidth StringToWidth( const char* const widthStr )
407 FontWidth width = TextAbstraction::FontWidth::NORMAL;
408 Scripting::GetEnumeration<FontWidth>( widthStr,
409 FONT_WIDTH_STRING_TABLE,
410 FONT_WIDTH_STRING_TABLE_COUNT,
416 FontSlant StringToSlant( const char* const slantStr )
418 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
419 Scripting::GetEnumeration<FontSlant>( slantStr,
420 FONT_SLANT_STRING_TABLE,
421 FONT_SLANT_STRING_TABLE_COUNT,
429 } // namespace Toolkit