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 BLUR_RADIUS_KEY( "blurRadius" );
39 const std::string WIDTH_KEY( "width" );
40 const std::string HEIGHT_KEY( "height" );
41 const std::string ENABLE_KEY( "enable" );
42 const std::string TRUE_TOKEN( "true" );
43 const std::string FALSE_TOKEN( "false" );
46 bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
51 bool& blurRadiusDefined,
54 const unsigned int numberOfItems = shadowPropertiesMap.Count();
56 // Parses and applies the style.
57 for( unsigned int index = 0u; index < numberOfItems; ++index )
59 const KeyValuePair& valueGet = shadowPropertiesMap.GetKeyValue( index );
61 if( COLOR_KEY == valueGet.first.stringKey )
66 if( valueGet.second.GetType() == Dali::Property::STRING )
68 const std::string colorStr = valueGet.second.Get<std::string>();
69 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
73 color = valueGet.second.Get<Vector4>();
76 else if( OFFSET_KEY == valueGet.first.stringKey )
81 if( valueGet.second.GetType() == Dali::Property::STRING )
83 const std::string offsetStr = valueGet.second.Get<std::string>();
84 StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
88 offset = valueGet.second.Get<Vector2>();
91 else if( BLUR_RADIUS_KEY == valueGet.first.stringKey )
94 blurRadiusDefined = true;
96 if( valueGet.second.GetType() == Dali::Property::STRING )
98 const std::string blurRadiusStr = valueGet.second.Get<std::string>();
99 blurRadius = StringToFloat( blurRadiusStr.c_str() );
103 blurRadius = valueGet.second.Get<float>();
108 return 0u == numberOfItems;
111 bool ParseUnderlineProperties( const Property::Map& underlinePropertiesMap,
118 const unsigned int numberOfItems = underlinePropertiesMap.Count();
120 // Parses and applies the style.
121 for( unsigned int index = 0u; index < numberOfItems; ++index )
123 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
125 if( ENABLE_KEY == valueGet.first.stringKey )
128 if( valueGet.second.GetType() == Dali::Property::STRING )
130 const std::string enableStr = valueGet.second.Get<std::string>();
131 enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
135 enabled = valueGet.second.Get<bool>();
138 else if( COLOR_KEY == valueGet.first.stringKey )
143 if( valueGet.second.GetType() == Dali::Property::STRING )
145 const std::string colorStr = valueGet.second.Get<std::string>();
146 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
150 color = valueGet.second.Get<Vector4>();
153 else if( HEIGHT_KEY == valueGet.first.stringKey )
156 heightDefined = true;
158 if( valueGet.second.GetType() == Dali::Property::STRING )
160 const std::string heightStr = valueGet.second.Get<std::string>();
161 height = StringToFloat( heightStr.c_str() );
165 height = valueGet.second.Get<float>();
170 return 0u == numberOfItems;
173 bool ParseOutlineProperties( const Property::Map& underlinePropertiesMap,
177 unsigned int& width )
179 const unsigned int numberOfItems = underlinePropertiesMap.Count();
181 // Parses and applies the style.
182 for( unsigned int index = 0u; index < numberOfItems; ++index )
184 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
186 if( COLOR_KEY == valueGet.first.stringKey )
190 color = valueGet.second.Get<Vector4>();
192 else if( WIDTH_KEY == valueGet.first.stringKey )
196 width = static_cast<unsigned int>( valueGet.second.Get<float>() );
200 return 0u == numberOfItems;
203 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
211 case EffectStyle::DEFAULT:
213 const Property::Map& propertiesMap = value.Get<Property::Map>();
215 bool enabled = false;
216 bool colorDefined = false;
218 bool heightDefined = false;
223 if ( propertiesMap.Empty() )
225 // Map empty so check if a string provided
226 const std::string propertyString = value.Get<std::string>();
228 if ( !propertyString.empty() )
230 Property::Map parsedStringMap;
231 Text::ParsePropertyString( propertyString, parsedStringMap );
233 empty = ParseUnderlineProperties( parsedStringMap,
240 controller->UnderlineSetByString( !empty );
245 empty = ParseUnderlineProperties( propertiesMap,
252 controller->UnderlineSetByString( false );
257 if( enabled != controller->IsUnderlineEnabled() )
259 controller->SetUnderlineEnabled( enabled );
263 // Sets the default underline values.
264 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
266 controller->SetUnderlineColor( color );
270 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
272 controller->SetUnderlineHeight( height );
278 // Disable underline.
279 if( controller->IsUnderlineEnabled() )
281 controller->SetUnderlineEnabled( false );
287 case EffectStyle::INPUT:
289 const std::string& underlineProperties = value.Get<std::string>();
291 controller->SetInputUnderlineProperties( underlineProperties );
295 } // if( controller )
300 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
306 case EffectStyle::DEFAULT:
308 const bool enabled = controller->IsUnderlineEnabled();
309 const Vector4& color = controller->GetUnderlineColor();
310 const float height = controller->GetUnderlineHeight();
312 if ( controller->IsUnderlineSetByString() )
314 std::string underlineProperties = "{\"enable\":";
315 const std::string enabledStr = enabled ? "true" : "false";
316 underlineProperties += "\"" + enabledStr + "\",";
318 std::string colorStr;
319 Vector4ToColorString( color, colorStr );
320 underlineProperties += "\"color\":\"" + colorStr + "\",";
322 std::string heightStr;
323 FloatToString( height, heightStr );
324 underlineProperties += "\"height\":\"" + heightStr + "\"}";
326 value = underlineProperties;
332 const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
333 map.Insert( ENABLE_KEY, enabledStr );
335 std::string colorStr;
336 Vector4ToColorString( color, colorStr );
337 map.Insert( COLOR_KEY, colorStr );
339 std::string heightStr;
340 FloatToString( height, heightStr );
341 map.Insert( HEIGHT_KEY, heightStr );
348 case EffectStyle::INPUT:
350 value = controller->GetInputUnderlineProperties();
357 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
365 case EffectStyle::DEFAULT:
367 const Property::Map& propertiesMap = value.Get<Property::Map>();
369 bool colorDefined = false;
371 bool offsetDefined = false;
373 bool blurRadiusDefined = false;
378 if ( propertiesMap.Empty() )
380 // Map empty so check if a string provided
381 const std::string propertyString = value.Get<std::string>();
383 Property::Map parsedStringMap;
384 Text::ParsePropertyString( propertyString, parsedStringMap );
386 empty = ParseShadowProperties( parsedStringMap,
394 controller->ShadowSetByString( !empty );
399 empty = ParseShadowProperties( propertiesMap,
407 controller->ShadowSetByString( false );
412 // Sets the default shadow values.
413 if( colorDefined && ( controller->GetShadowColor() != color ) )
415 controller->SetShadowColor( color );
419 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
421 controller->SetShadowOffset( offset );
425 if( blurRadiusDefined && ( controller->GetShadowBlurRadius() != blurRadius ) )
427 controller->SetShadowBlurRadius( blurRadius );
434 if( Vector2::ZERO != controller->GetShadowOffset() )
436 controller->SetShadowOffset( Vector2::ZERO );
441 case EffectStyle::INPUT:
443 const std::string& shadowString = value.Get<std::string>();
445 controller->SetInputShadowProperties( shadowString );
449 } // if( controller )
454 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
460 case EffectStyle::DEFAULT:
462 const Vector4& color = controller->GetShadowColor();
463 const Vector2& offset = controller->GetShadowOffset();
464 const float& blurRadius = controller->GetShadowBlurRadius();
466 if ( controller->IsShadowSetByString() )
468 std::string shadowProperties = "{";
470 std::string colorStr;
471 Vector4ToColorString( color, colorStr );
472 shadowProperties += "\"color\":\"" + colorStr + "\",";
474 std::string offsetStr;
475 Vector2ToString( offset, offsetStr );
476 shadowProperties += "\"offset\":\"" + offsetStr + "\",";
478 std::string blurRadiusStr;
479 FloatToString( blurRadius, blurRadiusStr );
480 shadowProperties += "\"blurRadius\":\"" + blurRadiusStr + "\"}";
482 value = shadowProperties;
488 map.Insert( COLOR_KEY, color );
489 map.Insert( OFFSET_KEY, offset );
490 map.Insert( BLUR_RADIUS_KEY, blurRadius );
496 case EffectStyle::INPUT:
498 value = controller->GetInputShadowProperties();
505 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
511 const std::string properties = value.Get< std::string >();
515 case EffectStyle::DEFAULT:
517 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
518 controller->SetDefaultEmbossProperties( properties );
521 case EffectStyle::INPUT:
523 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
524 controller->SetInputEmbossProperties( properties );
533 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
539 case EffectStyle::DEFAULT:
541 value = controller->GetDefaultEmbossProperties();
544 case EffectStyle::INPUT:
546 value = controller->GetInputEmbossProperties();
553 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
561 case EffectStyle::DEFAULT:
563 const Property::Map& propertiesMap = value.Get<Property::Map>();
565 bool colorDefined = false;
567 bool widthDefined = false;
568 unsigned int width = 0u;
572 if ( propertiesMap.Empty() )
574 // Map empty so check if a string provided
575 // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
576 const std::string propertyString = value.Get<std::string>();
578 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
579 controller->SetDefaultOutlineProperties( propertyString );
581 controller->OutlineSetByString( true );
585 empty = ParseOutlineProperties( propertiesMap,
591 controller->OutlineSetByString( false );
596 // Sets the default outline values.
597 if( colorDefined && ( controller->GetOutlineColor() != color ) )
599 controller->SetOutlineColor( color );
603 if( widthDefined && ( controller->GetOutlineWidth() != width ) )
605 controller->SetOutlineWidth( width );
612 if( 0u != controller->GetOutlineWidth() )
614 controller->SetOutlineWidth( 0u );
620 case EffectStyle::INPUT:
622 const std::string& outlineProperties = value.Get<std::string>();
624 controller->SetInputOutlineProperties( outlineProperties );
628 } // if( controller )
633 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
639 case EffectStyle::DEFAULT:
641 if ( controller->IsOutlineSetByString() )
643 value = controller->GetDefaultOutlineProperties();
648 const Vector4& color = controller->GetOutlineColor();
649 const unsigned int width = controller->GetOutlineWidth();
652 map.Insert( COLOR_KEY, color );
653 map.Insert( WIDTH_KEY, static_cast<int>( width ) );
660 case EffectStyle::INPUT:
662 value = controller->GetInputOutlineProperties();
671 } // namespace Toolkit