2 * Copyright (c) 2018 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 ParseBackgroundProperties( const Property::Map& backgroundProperties,
208 const unsigned int numberOfItems = backgroundProperties.Count();
210 // Parses and applies the style.
211 for( unsigned int index = 0u; index < numberOfItems; ++index )
213 const KeyValuePair& valueGet = backgroundProperties.GetKeyValue( index );
215 if( ENABLE_KEY == valueGet.first.stringKey )
218 enabled = valueGet.second.Get<bool>();
220 else if( COLOR_KEY == valueGet.first.stringKey )
224 color = valueGet.second.Get<Vector4>();
228 return 0u == numberOfItems;
231 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
239 case EffectStyle::DEFAULT:
241 const Property::Map& propertiesMap = value.Get<Property::Map>();
243 bool enabled = false;
244 bool colorDefined = false;
246 bool heightDefined = false;
251 if ( propertiesMap.Empty() )
253 // Map empty so check if a string provided
254 const std::string propertyString = value.Get<std::string>();
256 if ( !propertyString.empty() )
258 Property::Map parsedStringMap;
259 Text::ParsePropertyString( propertyString, parsedStringMap );
261 empty = ParseUnderlineProperties( parsedStringMap,
268 controller->UnderlineSetByString( !empty );
273 empty = ParseUnderlineProperties( propertiesMap,
280 controller->UnderlineSetByString( false );
285 if( enabled != controller->IsUnderlineEnabled() )
287 controller->SetUnderlineEnabled( enabled );
291 // Sets the default underline values.
292 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
294 controller->SetUnderlineColor( color );
298 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
300 controller->SetUnderlineHeight( height );
306 // Disable underline.
307 if( controller->IsUnderlineEnabled() )
309 controller->SetUnderlineEnabled( false );
315 case EffectStyle::INPUT:
317 const std::string& underlineProperties = value.Get<std::string>();
319 controller->SetInputUnderlineProperties( underlineProperties );
323 } // if( controller )
328 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
334 case EffectStyle::DEFAULT:
336 const bool enabled = controller->IsUnderlineEnabled();
337 const Vector4& color = controller->GetUnderlineColor();
338 const float height = controller->GetUnderlineHeight();
340 if ( controller->IsUnderlineSetByString() )
342 std::string underlineProperties = "{\"enable\":";
343 const std::string enabledStr = enabled ? "true" : "false";
344 underlineProperties += "\"" + enabledStr + "\",";
346 std::string colorStr;
347 Vector4ToColorString( color, colorStr );
348 underlineProperties += "\"color\":\"" + colorStr + "\",";
350 std::string heightStr;
351 FloatToString( height, heightStr );
352 underlineProperties += "\"height\":\"" + heightStr + "\"}";
354 value = underlineProperties;
360 const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
361 map.Insert( ENABLE_KEY, enabledStr );
363 std::string colorStr;
364 Vector4ToColorString( color, colorStr );
365 map.Insert( COLOR_KEY, colorStr );
367 std::string heightStr;
368 FloatToString( height, heightStr );
369 map.Insert( HEIGHT_KEY, heightStr );
376 case EffectStyle::INPUT:
378 value = controller->GetInputUnderlineProperties();
385 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
393 case EffectStyle::DEFAULT:
395 const Property::Map& propertiesMap = value.Get<Property::Map>();
397 bool colorDefined = false;
399 bool offsetDefined = false;
401 bool blurRadiusDefined = false;
406 if ( propertiesMap.Empty() )
408 // Map empty so check if a string provided
409 const std::string propertyString = value.Get<std::string>();
411 Property::Map parsedStringMap;
412 Text::ParsePropertyString( propertyString, parsedStringMap );
414 empty = ParseShadowProperties( parsedStringMap,
422 controller->ShadowSetByString( !empty );
427 empty = ParseShadowProperties( propertiesMap,
435 controller->ShadowSetByString( false );
440 // Sets the default shadow values.
441 if( colorDefined && ( controller->GetShadowColor() != color ) )
443 controller->SetShadowColor( color );
447 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
449 controller->SetShadowOffset( offset );
453 if( blurRadiusDefined && ( controller->GetShadowBlurRadius() != blurRadius ) )
455 controller->SetShadowBlurRadius( blurRadius );
462 if( Vector2::ZERO != controller->GetShadowOffset() )
464 controller->SetShadowOffset( Vector2::ZERO );
469 case EffectStyle::INPUT:
471 const std::string& shadowString = value.Get<std::string>();
473 controller->SetInputShadowProperties( shadowString );
477 } // if( controller )
482 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
488 case EffectStyle::DEFAULT:
490 const Vector4& color = controller->GetShadowColor();
491 const Vector2& offset = controller->GetShadowOffset();
492 const float& blurRadius = controller->GetShadowBlurRadius();
494 if ( controller->IsShadowSetByString() )
496 std::string shadowProperties = "{";
498 std::string colorStr;
499 Vector4ToColorString( color, colorStr );
500 shadowProperties += "\"color\":\"" + colorStr + "\",";
502 std::string offsetStr;
503 Vector2ToString( offset, offsetStr );
504 shadowProperties += "\"offset\":\"" + offsetStr + "\",";
506 std::string blurRadiusStr;
507 FloatToString( blurRadius, blurRadiusStr );
508 shadowProperties += "\"blurRadius\":\"" + blurRadiusStr + "\"}";
510 value = shadowProperties;
516 map.Insert( COLOR_KEY, color );
517 map.Insert( OFFSET_KEY, offset );
518 map.Insert( BLUR_RADIUS_KEY, blurRadius );
524 case EffectStyle::INPUT:
526 value = controller->GetInputShadowProperties();
533 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
539 const std::string properties = value.Get< std::string >();
543 case EffectStyle::DEFAULT:
545 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
546 controller->SetDefaultEmbossProperties( properties );
549 case EffectStyle::INPUT:
551 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
552 controller->SetInputEmbossProperties( properties );
561 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
567 case EffectStyle::DEFAULT:
569 value = controller->GetDefaultEmbossProperties();
572 case EffectStyle::INPUT:
574 value = controller->GetInputEmbossProperties();
581 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
589 case EffectStyle::DEFAULT:
591 const Property::Map& propertiesMap = value.Get<Property::Map>();
593 bool colorDefined = false;
595 bool widthDefined = false;
596 unsigned int width = 0u;
600 if ( propertiesMap.Empty() )
602 // Map empty so check if a string provided
603 // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
604 const std::string propertyString = value.Get<std::string>();
606 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
607 controller->SetDefaultOutlineProperties( propertyString );
609 controller->OutlineSetByString( true );
613 empty = ParseOutlineProperties( propertiesMap,
619 controller->OutlineSetByString( false );
624 // Sets the default outline values.
625 if( colorDefined && ( controller->GetOutlineColor() != color ) )
627 controller->SetOutlineColor( color );
631 if( widthDefined && ( controller->GetOutlineWidth() != width ) )
633 controller->SetOutlineWidth( width );
640 if( 0u != controller->GetOutlineWidth() )
642 controller->SetOutlineWidth( 0u );
648 case EffectStyle::INPUT:
650 const std::string& outlineProperties = value.Get<std::string>();
652 controller->SetInputOutlineProperties( outlineProperties );
656 } // if( controller )
661 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
667 case EffectStyle::DEFAULT:
669 if ( controller->IsOutlineSetByString() )
671 value = controller->GetDefaultOutlineProperties();
676 const Vector4& color = controller->GetOutlineColor();
677 const unsigned int width = controller->GetOutlineWidth();
680 map.Insert( COLOR_KEY, color );
681 map.Insert( WIDTH_KEY, static_cast<int>( width ) );
688 case EffectStyle::INPUT:
690 value = controller->GetInputOutlineProperties();
697 bool SetBackgroundProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
705 case EffectStyle::DEFAULT:
707 const Property::Map& propertiesMap = value.Get<Property::Map>();
709 bool enabled = false;
710 bool colorDefined = false;
715 if ( !propertiesMap.Empty() )
717 empty = ParseBackgroundProperties( propertiesMap,
725 if( enabled != controller->IsBackgroundEnabled() )
727 controller->SetBackgroundEnabled( enabled );
731 if( colorDefined && ( controller->GetBackgroundColor() != color ) )
733 controller->SetBackgroundColor( color );
739 // Disable background.
740 if( controller->IsBackgroundEnabled() )
742 controller->SetBackgroundEnabled( false );
748 case EffectStyle::INPUT:
750 // Text background is not supported while inputting yet
754 } // if( controller )
759 void GetBackgroundProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
765 case EffectStyle::DEFAULT:
767 const bool enabled = controller->IsBackgroundEnabled();
768 const Vector4& color = controller->GetBackgroundColor();
771 map.Insert( ENABLE_KEY, enabled );
772 map.Insert( COLOR_KEY, color );
779 case EffectStyle::INPUT:
781 // Text background is not supported while inputting yet
791 } // namespace Toolkit