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 )
111 const std::string style = value.Get< std::string >();
112 DALI_LOG_INFO( gLogFilter, Debug::General, "Text Control %p FONT_STYLE %s\n", controller.Get(), style.c_str() );
114 // Parses and applies the style.
116 ParsePropertyString( style, map );
121 Property::Value* weightValue = map.Find( WEIGHT_KEY );
123 FontWeight weight = TextAbstraction::FontWeight::NONE;
124 const bool weightDefined = weightValue != NULL;
127 const std::string weightStr = weightValue->Get<std::string>();
129 Scripting::GetEnumeration< FontWeight >( weightStr.c_str(),
130 FONT_WEIGHT_STRING_TABLE,
131 FONT_WEIGHT_STRING_TABLE_COUNT,
136 Property::Value* widthValue = map.Find( WIDTH_KEY );
138 FontWidth width = TextAbstraction::FontWidth::NONE;
139 const bool widthDefined = widthValue != NULL;
142 const std::string widthStr = widthValue->Get<std::string>();
144 Scripting::GetEnumeration< FontWidth >( widthStr.c_str(),
145 FONT_WIDTH_STRING_TABLE,
146 FONT_WIDTH_STRING_TABLE_COUNT,
151 Property::Value* slantValue = map.Find( SLANT_KEY );
153 FontSlant slant = TextAbstraction::FontSlant::NONE;
154 const bool slantDefined = slantValue != NULL;
157 const std::string slantStr = slantValue->Get<std::string>();
159 Scripting::GetEnumeration< FontSlant >( slantStr.c_str(),
160 FONT_SLANT_STRING_TABLE,
161 FONT_SLANT_STRING_TABLE_COUNT,
167 case FontStyle::DEFAULT:
169 // Sets the default font's style values.
170 if( !weightDefined ||
171 ( weightDefined && ( controller->GetDefaultFontWeight() != weight ) ) )
173 controller->SetDefaultFontWeight( weight );
177 ( widthDefined && ( controller->GetDefaultFontWidth() != width ) ) )
179 controller->SetDefaultFontWidth( width );
183 ( slantDefined && ( controller->GetDefaultFontSlant() != slant ) ) )
185 controller->SetDefaultFontSlant( slant );
189 case FontStyle::INPUT:
191 // Sets the input font's style values.
192 if( !weightDefined ||
193 ( weightDefined && ( controller->GetInputFontWeight() != weight ) ) )
195 controller->SetInputFontWeight( weight );
199 ( widthDefined && ( controller->GetInputFontWidth() != width ) ) )
201 controller->SetInputFontWidth( width );
205 ( slantDefined && ( controller->GetInputFontSlant() != slant ) ) )
207 controller->SetInputFontSlant( slant );
217 case FontStyle::DEFAULT:
219 controller->SetDefaultFontWeight( TextAbstraction::FontWeight::NONE );
220 controller->SetDefaultFontWidth( TextAbstraction::FontWidth::NONE );
221 controller->SetDefaultFontSlant( TextAbstraction::FontSlant::NONE );
224 case FontStyle::INPUT:
226 controller->SetInputFontWeight( TextAbstraction::FontWeight::NONE );
227 controller->SetInputFontWidth( TextAbstraction::FontWidth::NONE );
228 controller->SetInputFontSlant( TextAbstraction::FontSlant::NONE );
236 void GetFontStyleProperty( ControllerPtr controller, Property::Value& value, FontStyle::Type type )
240 const bool isDefaultStyle = FontStyle::DEFAULT == type;
242 bool weightDefined = false;
243 bool widthDefined = false;
244 bool slantDefined = false;
245 FontWeight weight = TextAbstraction::FontWeight::NONE;
246 FontWidth width = TextAbstraction::FontWidth::NONE;
247 FontSlant slant = TextAbstraction::FontSlant::NONE;
251 weightDefined = controller->IsDefaultFontWeightDefined();
252 widthDefined = controller->IsDefaultFontWidthDefined();
253 slantDefined = controller->IsDefaultFontSlantDefined();
257 weight = controller->GetDefaultFontWeight();
262 width = controller->GetDefaultFontWidth();
267 slant = controller->GetDefaultFontSlant();
272 weightDefined = controller->IsInputFontWeightDefined();
273 widthDefined = controller->IsInputFontWidthDefined();
274 slantDefined = controller->IsInputFontSlantDefined();
278 weight = controller->GetInputFontWeight();
283 width = controller->GetInputFontWidth();
288 slant = controller->GetInputFontSlant();
292 if( weightDefined || widthDefined || slantDefined )
294 std::string styleString("{");
297 if( TextAbstraction::FontWeight::NONE != weight )
299 const std::string weightStr( GetEnumerationName( weight,
300 FONT_WEIGHT_STRING_TABLE,
301 FONT_WEIGHT_STRING_TABLE_COUNT ) );
303 styleString += "\"weight\":\"" + weightStr + "\"";
307 weightDefined = false;
313 if( TextAbstraction::FontWidth::NONE != width )
315 const std::string widthStr( GetEnumerationName( width,
316 FONT_WIDTH_STRING_TABLE,
317 FONT_WIDTH_STRING_TABLE_COUNT ) );
323 styleString += "\"width\":\"" + widthStr + "\"";
327 widthDefined = false;
333 if( TextAbstraction::FontSlant::NONE != slant )
335 const std::string slantStr( GetEnumerationName( slant,
336 FONT_SLANT_STRING_TABLE,
337 FONT_SLANT_STRING_TABLE_COUNT ) );
339 if( weightDefined || widthDefined )
343 styleString += "\"slant\":\"" + slantStr + "\"";
347 slantDefined = false;
351 if( weightDefined || widthDefined || slantDefined )
365 FontWeight StringToWeight( const char* const weightStr )
367 FontWeight weight = TextAbstraction::FontWeight::NORMAL;
368 Scripting::GetEnumeration<FontWeight>( weightStr,
369 FONT_WEIGHT_STRING_TABLE,
370 FONT_WEIGHT_STRING_TABLE_COUNT,
376 FontWidth StringToWidth( const char* const widthStr )
378 FontWidth width = TextAbstraction::FontWidth::NORMAL;
379 Scripting::GetEnumeration<FontWidth>( widthStr,
380 FONT_WIDTH_STRING_TABLE,
381 FONT_WIDTH_STRING_TABLE_COUNT,
387 FontSlant StringToSlant( const char* const slantStr )
389 FontSlant slant = TextAbstraction::FontSlant::NORMAL;
390 Scripting::GetEnumeration<FontSlant>( slantStr,
391 FONT_SLANT_STRING_TABLE,
392 FONT_SLANT_STRING_TABLE_COUNT,
400 } // namespace Toolkit