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-effects-style.h>
22 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
23 #include <dali-toolkit/internal/text/property-string-parser.h>
36 const std::string COLOR_KEY( "color" );
37 const std::string OFFSET_KEY( "offset" );
38 const std::string WIDTH_KEY( "width" );
39 const std::string HEIGHT_KEY( "height" );
40 const std::string ENABLE_KEY( "enable" );
41 const std::string TRUE_TOKEN( "true" );
42 const std::string FALSE_TOKEN( "false" );
45 bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
51 const unsigned int numberOfItems = shadowPropertiesMap.Count();
53 // Parses and applies the style.
54 for( unsigned int index = 0u; index < numberOfItems; ++index )
56 const KeyValuePair& valueGet = shadowPropertiesMap.GetKeyValue( index );
58 if( COLOR_KEY == valueGet.first.stringKey )
63 const std::string colorStr = valueGet.second.Get<std::string>();
65 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
67 else if( OFFSET_KEY == valueGet.first.stringKey )
72 const std::string offsetStr = valueGet.second.Get<std::string>();
74 StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
78 return 0u == numberOfItems;
81 bool ParseUnderlineProperties( const Property::Map& underlinePropertiesMap,
88 const unsigned int numberOfItems = underlinePropertiesMap.Count();
90 // Parses and applies the style.
91 for( unsigned int index = 0u; index < numberOfItems; ++index )
93 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
95 if( ENABLE_KEY == valueGet.first.stringKey )
98 const std::string enableStr = valueGet.second.Get<std::string>();
99 enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
101 else if( COLOR_KEY == valueGet.first.stringKey )
106 const std::string colorStr = valueGet.second.Get<std::string>();
108 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
110 else if( HEIGHT_KEY == valueGet.first.stringKey )
113 heightDefined = true;
115 const std::string heightStr = valueGet.second.Get<std::string>();
117 height = StringToFloat( heightStr.c_str() );
121 return 0u == numberOfItems;
124 bool ParseOutlineProperties( const Property::Map& underlinePropertiesMap,
130 const unsigned int numberOfItems = underlinePropertiesMap.Count();
132 // Parses and applies the style.
133 for( unsigned int index = 0u; index < numberOfItems; ++index )
135 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
137 if( COLOR_KEY == valueGet.first.stringKey )
141 color = valueGet.second.Get<Vector4>();
143 else if( WIDTH_KEY == valueGet.first.stringKey )
147 width = valueGet.second.Get<float>();
151 return 0u == numberOfItems;
154 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
162 case EffectStyle::DEFAULT:
164 const Property::Map& propertiesMap = value.Get<Property::Map>();
166 bool enabled = false;
167 bool colorDefined = false;
169 bool heightDefined = false;
174 if ( propertiesMap.Empty() )
176 // Map empty so check if a string provided
177 const std::string propertyString = value.Get<std::string>();
179 if ( !propertyString.empty() )
181 Property::Map parsedStringMap;
182 Text::ParsePropertyString( propertyString, parsedStringMap );
184 empty = ParseUnderlineProperties( parsedStringMap,
191 controller->UnderlineSetByString( !empty);
196 empty = ParseUnderlineProperties( propertiesMap,
203 controller->UnderlineSetByString( false );
208 if( enabled != controller->IsUnderlineEnabled() )
210 controller->SetUnderlineEnabled( enabled );
214 // Sets the default underline values.
215 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
217 controller->SetUnderlineColor( color );
221 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
223 controller->SetUnderlineHeight( height );
229 // Disable underline.
230 if( controller->IsUnderlineEnabled() )
232 controller->SetUnderlineEnabled( false );
238 case EffectStyle::INPUT:
240 const std::string& underlineProperties = value.Get<std::string>();
242 controller->SetInputUnderlineProperties( underlineProperties );
246 } // if( controller )
251 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
257 case EffectStyle::DEFAULT:
259 const bool enabled = controller->IsUnderlineEnabled();
260 const Vector4& color = controller->GetUnderlineColor();
261 const float height = controller->GetUnderlineHeight();
263 if ( controller->IsUnderlineSetByString() )
265 std::string underlineProperties = "{\"enable\":";
266 const std::string enabledStr = enabled ? "true" : "false";
267 underlineProperties += "\"" + enabledStr + "\",";
269 std::string colorStr;
270 Vector4ToColorString( color, colorStr );
271 underlineProperties += "\"color\":\"" + colorStr + "\",";
273 std::string heightStr;
274 FloatToString( height, heightStr );
275 underlineProperties += "\"height\":\"" + heightStr + "\"}";
277 value = underlineProperties;
283 const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
284 map.Insert( ENABLE_KEY, enabledStr );
286 std::string colorStr;
287 Vector4ToColorString( color, colorStr );
288 map.Insert( COLOR_KEY, colorStr );
290 std::string heightStr;
291 FloatToString( height, heightStr );
292 map.Insert( HEIGHT_KEY, heightStr );
299 case EffectStyle::INPUT:
301 value = controller->GetInputUnderlineProperties();
308 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
316 case EffectStyle::DEFAULT:
318 const Property::Map& propertiesMap = value.Get<Property::Map>();
320 bool colorDefined = false;
322 bool offsetDefined = false;
327 if ( propertiesMap.Empty() )
329 // Map empty so check if a string provided
330 const std::string propertyString = value.Get<std::string>();
332 Property::Map parsedStringMap;
333 Text::ParsePropertyString( propertyString, parsedStringMap );
335 empty = ParseShadowProperties( parsedStringMap,
341 controller->ShadowSetByString( !empty );
346 empty = ParseShadowProperties( propertiesMap,
352 controller->ShadowSetByString( false );
357 // Sets the default shadow values.
358 if( colorDefined && ( controller->GetShadowColor() != color ) )
360 controller->SetShadowColor( color );
364 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
366 controller->SetShadowOffset( offset );
373 if( Vector2::ZERO != controller->GetShadowOffset() )
375 controller->SetShadowOffset( Vector2::ZERO );
380 case EffectStyle::INPUT:
382 const std::string& shadowString = value.Get<std::string>();
384 controller->SetInputShadowProperties( shadowString );
388 } // if( controller )
393 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
399 case EffectStyle::DEFAULT:
401 const Vector4& color = controller->GetShadowColor();
402 const Vector2& offset = controller->GetShadowOffset();
404 if ( controller->IsShadowSetByString() )
406 std::string shadowProperties = "{";
408 std::string colorStr;
409 Vector4ToColorString( color, colorStr );
410 shadowProperties += "\"color\":\"" + colorStr + "\",";
412 std::string offsetStr;
413 Vector2ToString( offset, offsetStr );
414 shadowProperties += "\"offset\":\"" + offsetStr + "\"}";
416 value = shadowProperties;
422 std::string colorStr;
423 Vector4ToColorString( color, colorStr );
424 map.Insert( COLOR_KEY, colorStr );
426 std::string offsetStr;
427 Vector2ToString( offset, offsetStr );
428 map.Insert( OFFSET_KEY, offsetStr );
434 case EffectStyle::INPUT:
436 value = controller->GetInputShadowProperties();
443 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
449 const std::string properties = value.Get< std::string >();
453 case EffectStyle::DEFAULT:
455 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
456 controller->SetDefaultEmbossProperties( properties );
459 case EffectStyle::INPUT:
461 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
462 controller->SetInputEmbossProperties( properties );
471 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
477 case EffectStyle::DEFAULT:
479 value = controller->GetDefaultEmbossProperties();
482 case EffectStyle::INPUT:
484 value = controller->GetInputEmbossProperties();
491 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
499 case EffectStyle::DEFAULT:
501 const Property::Map& propertiesMap = value.Get<Property::Map>();
503 bool colorDefined = false;
505 bool widthDefined = false;
510 if ( propertiesMap.Empty() )
512 // Map empty so check if a string provided
513 // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
514 const std::string propertyString = value.Get<std::string>();
516 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
517 controller->SetDefaultOutlineProperties( propertyString );
519 controller->OutlineSetByString( true );
523 empty = ParseOutlineProperties( propertiesMap,
529 controller->OutlineSetByString( false );
534 // Sets the default outline values.
535 if( colorDefined && ( controller->GetOutlineColor() != color ) )
537 controller->SetOutlineColor( color );
541 if( widthDefined && ( fabsf( controller->GetOutlineWidth() - width ) > Math::MACHINE_EPSILON_1000 ) )
543 controller->SetOutlineWidth( width );
550 if( fabsf( controller->GetOutlineWidth() ) > Math::MACHINE_EPSILON_1000 )
552 controller->SetOutlineWidth( 0.0f );
558 case EffectStyle::INPUT:
560 const std::string& outlineProperties = value.Get<std::string>();
562 controller->SetInputOutlineProperties( outlineProperties );
566 } // if( controller )
571 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
577 case EffectStyle::DEFAULT:
579 if ( controller->IsOutlineSetByString() )
581 value = controller->GetDefaultOutlineProperties();
586 const Vector4& color = controller->GetOutlineColor();
587 const float width = controller->GetOutlineWidth();
591 std::string colorStr;
592 Vector4ToColorString( color, colorStr );
593 map.Insert( COLOR_KEY, colorStr );
595 std::string widthStr;
596 FloatToString( width, widthStr );
597 map.Insert( WIDTH_KEY, widthStr );
604 case EffectStyle::INPUT:
606 value = controller->GetInputOutlineProperties();
615 } // namespace Toolkit