2 * Copyright (c) 2019 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/devel-api/controls/text-controls/text-style-properties-devel.h>
23 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
24 #include <dali-toolkit/internal/text/property-string-parser.h>
37 const std::string COLOR_KEY( "color" );
38 const std::string OFFSET_KEY( "offset" );
39 const std::string BLUR_RADIUS_KEY( "blurRadius" );
40 const std::string WIDTH_KEY( "width" );
41 const std::string HEIGHT_KEY( "height" );
42 const std::string ENABLE_KEY( "enable" );
43 const std::string TRUE_TOKEN( "true" );
44 const std::string FALSE_TOKEN( "false" );
47 bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
52 bool& blurRadiusDefined,
55 const unsigned int numberOfItems = shadowPropertiesMap.Count();
57 // Parses and applies the style.
58 for( unsigned int index = 0u; index < numberOfItems; ++index )
60 const KeyValuePair& valueGet = shadowPropertiesMap.GetKeyValue( index );
62 if( ( DevelText::Shadow::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
67 if( valueGet.second.GetType() == Dali::Property::STRING )
69 const std::string colorStr = valueGet.second.Get<std::string>();
70 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
74 color = valueGet.second.Get<Vector4>();
77 else if( ( DevelText::Shadow::Property::OFFSET == valueGet.first.indexKey ) || ( OFFSET_KEY == valueGet.first.stringKey ) )
82 if( valueGet.second.GetType() == Dali::Property::STRING )
84 const std::string offsetStr = valueGet.second.Get<std::string>();
85 StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
89 offset = valueGet.second.Get<Vector2>();
92 else if( ( DevelText::Shadow::Property::BLUR_RADIUS == valueGet.first.indexKey ) || ( BLUR_RADIUS_KEY == valueGet.first.stringKey ) )
95 blurRadiusDefined = true;
97 if( valueGet.second.GetType() == Dali::Property::STRING )
99 const std::string blurRadiusStr = valueGet.second.Get<std::string>();
100 blurRadius = StringToFloat( blurRadiusStr.c_str() );
104 blurRadius = valueGet.second.Get<float>();
109 return 0u == numberOfItems;
112 bool ParseUnderlineProperties( const Property::Map& underlinePropertiesMap,
119 const unsigned int numberOfItems = underlinePropertiesMap.Count();
121 // Parses and applies the style.
122 for( unsigned int index = 0u; index < numberOfItems; ++index )
124 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
126 if( ( DevelText::Underline::Property::ENABLE == valueGet.first.indexKey ) || ( ENABLE_KEY == valueGet.first.stringKey ) )
129 if( valueGet.second.GetType() == Dali::Property::STRING )
131 const std::string enableStr = valueGet.second.Get<std::string>();
132 enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
136 enabled = valueGet.second.Get<bool>();
139 else if( ( DevelText::Underline::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
144 if( valueGet.second.GetType() == Dali::Property::STRING )
146 const std::string colorStr = valueGet.second.Get<std::string>();
147 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
151 color = valueGet.second.Get<Vector4>();
154 else if( ( DevelText::Underline::Property::HEIGHT == valueGet.first.indexKey ) || ( HEIGHT_KEY == valueGet.first.stringKey ) )
157 heightDefined = true;
159 if( valueGet.second.GetType() == Dali::Property::STRING )
161 const std::string heightStr = valueGet.second.Get<std::string>();
162 height = StringToFloat( heightStr.c_str() );
166 height = valueGet.second.Get<float>();
171 return 0u == numberOfItems;
174 bool ParseOutlineProperties( const Property::Map& underlinePropertiesMap,
180 const unsigned int numberOfItems = underlinePropertiesMap.Count();
182 // Parses and applies the style.
183 for( unsigned int index = 0u; index < numberOfItems; ++index )
185 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
187 if( ( DevelText::Outline::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
191 color = valueGet.second.Get<Vector4>();
193 else if( ( DevelText::Outline::Property::WIDTH == valueGet.first.indexKey ) || ( WIDTH_KEY == valueGet.first.stringKey ) )
197 width = static_cast<uint16_t>( valueGet.second.Get<float>() );
201 return 0u == numberOfItems;
204 bool ParseBackgroundProperties( const Property::Map& backgroundProperties,
209 const unsigned int numberOfItems = backgroundProperties.Count();
211 // Parses and applies the style.
212 for( unsigned int index = 0u; index < numberOfItems; ++index )
214 const KeyValuePair& valueGet = backgroundProperties.GetKeyValue( index );
216 if( ( DevelText::Background::Property::ENABLE == valueGet.first.indexKey ) || ( ENABLE_KEY == valueGet.first.stringKey ) )
219 enabled = valueGet.second.Get<bool>();
221 else if( ( DevelText::Background::Property::COLOR == valueGet.first.indexKey ) || ( COLOR_KEY == valueGet.first.stringKey ) )
225 color = valueGet.second.Get<Vector4>();
229 return 0u == numberOfItems;
232 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
240 case EffectStyle::DEFAULT:
242 const Property::Map& propertiesMap = value.Get<Property::Map>();
244 bool enabled = false;
245 bool colorDefined = false;
247 bool heightDefined = false;
252 if ( propertiesMap.Empty() )
254 // Map empty so check if a string provided
255 const std::string propertyString = value.Get<std::string>();
257 if ( !propertyString.empty() )
259 Property::Map parsedStringMap;
260 Text::ParsePropertyString( propertyString, parsedStringMap );
262 empty = ParseUnderlineProperties( parsedStringMap,
269 controller->UnderlineSetByString( !empty );
274 empty = ParseUnderlineProperties( propertiesMap,
281 controller->UnderlineSetByString( false );
286 if( enabled != controller->IsUnderlineEnabled() )
288 controller->SetUnderlineEnabled( enabled );
292 // Sets the default underline values.
293 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
295 controller->SetUnderlineColor( color );
299 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
301 controller->SetUnderlineHeight( height );
307 // Disable underline.
308 if( controller->IsUnderlineEnabled() )
310 controller->SetUnderlineEnabled( false );
316 case EffectStyle::INPUT:
318 const std::string& underlineProperties = value.Get<std::string>();
320 controller->SetInputUnderlineProperties( underlineProperties );
324 } // if( controller )
329 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
335 case EffectStyle::DEFAULT:
337 const bool enabled = controller->IsUnderlineEnabled();
338 const Vector4& color = controller->GetUnderlineColor();
339 const float height = controller->GetUnderlineHeight();
341 if ( controller->IsUnderlineSetByString() )
343 std::string underlineProperties = "{\"enable\":";
344 const std::string enabledStr = enabled ? "true" : "false";
345 underlineProperties += "\"" + enabledStr + "\",";
347 std::string colorStr;
348 Vector4ToColorString( color, colorStr );
349 underlineProperties += "\"color\":\"" + colorStr + "\",";
351 std::string heightStr;
352 FloatToString( height, heightStr );
353 underlineProperties += "\"height\":\"" + heightStr + "\"}";
355 value = underlineProperties;
361 map.Insert( ENABLE_KEY, enabled );
362 map.Insert( COLOR_KEY, color );
363 map.Insert( HEIGHT_KEY, height );
370 case EffectStyle::INPUT:
372 value = controller->GetInputUnderlineProperties();
379 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
387 case EffectStyle::DEFAULT:
389 const Property::Map& propertiesMap = value.Get<Property::Map>();
391 bool colorDefined = false;
393 bool offsetDefined = false;
395 bool blurRadiusDefined = false;
400 if ( propertiesMap.Empty() )
402 // Map empty so check if a string provided
403 const std::string propertyString = value.Get<std::string>();
405 Property::Map parsedStringMap;
406 Text::ParsePropertyString( propertyString, parsedStringMap );
408 empty = ParseShadowProperties( parsedStringMap,
416 controller->ShadowSetByString( !empty );
421 empty = ParseShadowProperties( propertiesMap,
429 controller->ShadowSetByString( false );
434 // Sets the default shadow values.
435 if( colorDefined && ( controller->GetShadowColor() != color ) )
437 controller->SetShadowColor( color );
441 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
443 controller->SetShadowOffset( offset );
447 if( blurRadiusDefined && ( controller->GetShadowBlurRadius() != blurRadius ) )
449 controller->SetShadowBlurRadius( blurRadius );
456 if( Vector2::ZERO != controller->GetShadowOffset() )
458 controller->SetShadowOffset( Vector2::ZERO );
463 case EffectStyle::INPUT:
465 const std::string& shadowString = value.Get<std::string>();
467 controller->SetInputShadowProperties( shadowString );
471 } // if( controller )
476 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
482 case EffectStyle::DEFAULT:
484 const Vector4& color = controller->GetShadowColor();
485 const Vector2& offset = controller->GetShadowOffset();
486 const float& blurRadius = controller->GetShadowBlurRadius();
488 if ( controller->IsShadowSetByString() )
490 std::string shadowProperties = "{";
492 std::string colorStr;
493 Vector4ToColorString( color, colorStr );
494 shadowProperties += "\"color\":\"" + colorStr + "\",";
496 std::string offsetStr;
497 Vector2ToString( offset, offsetStr );
498 shadowProperties += "\"offset\":\"" + offsetStr + "\",";
500 std::string blurRadiusStr;
501 FloatToString( blurRadius, blurRadiusStr );
502 shadowProperties += "\"blurRadius\":\"" + blurRadiusStr + "\"}";
504 value = shadowProperties;
510 map.Insert( COLOR_KEY, color );
511 map.Insert( OFFSET_KEY, offset );
512 map.Insert( BLUR_RADIUS_KEY, blurRadius );
518 case EffectStyle::INPUT:
520 value = controller->GetInputShadowProperties();
527 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
533 const std::string properties = value.Get< std::string >();
537 case EffectStyle::DEFAULT:
539 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
540 controller->SetDefaultEmbossProperties( properties );
543 case EffectStyle::INPUT:
545 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
546 controller->SetInputEmbossProperties( properties );
555 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
561 case EffectStyle::DEFAULT:
563 value = controller->GetDefaultEmbossProperties();
566 case EffectStyle::INPUT:
568 value = controller->GetInputEmbossProperties();
575 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
583 case EffectStyle::DEFAULT:
585 const Property::Map& propertiesMap = value.Get<Property::Map>();
587 bool colorDefined = false;
589 bool widthDefined = false;
594 if ( propertiesMap.Empty() )
596 // Map empty so check if a string provided
597 // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
598 const std::string propertyString = value.Get<std::string>();
600 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
601 controller->SetDefaultOutlineProperties( propertyString );
603 controller->OutlineSetByString( true );
607 empty = ParseOutlineProperties( propertiesMap,
613 controller->OutlineSetByString( false );
618 // Sets the default outline values.
619 if( colorDefined && ( controller->GetOutlineColor() != color ) )
621 controller->SetOutlineColor( color );
625 if( widthDefined && ( controller->GetOutlineWidth() != width ) )
627 controller->SetOutlineWidth( width );
634 if( 0u != controller->GetOutlineWidth() )
636 controller->SetOutlineWidth( 0u );
642 case EffectStyle::INPUT:
644 const std::string& outlineProperties = value.Get<std::string>();
646 controller->SetInputOutlineProperties( outlineProperties );
650 } // if( controller )
655 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
661 case EffectStyle::DEFAULT:
663 if ( controller->IsOutlineSetByString() )
665 value = controller->GetDefaultOutlineProperties();
670 const Vector4& color = controller->GetOutlineColor();
671 const uint16_t width = controller->GetOutlineWidth();
674 map.Insert( COLOR_KEY, color );
675 map.Insert( WIDTH_KEY, static_cast<int>( width ) );
682 case EffectStyle::INPUT:
684 value = controller->GetInputOutlineProperties();
691 bool SetBackgroundProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
699 case EffectStyle::DEFAULT:
701 const Property::Map& propertiesMap = value.Get<Property::Map>();
703 bool enabled = false;
704 bool colorDefined = false;
709 if ( !propertiesMap.Empty() )
711 empty = ParseBackgroundProperties( propertiesMap,
719 if( enabled != controller->IsBackgroundEnabled() )
721 controller->SetBackgroundEnabled( enabled );
725 if( colorDefined && ( controller->GetBackgroundColor() != color ) )
727 controller->SetBackgroundColor( color );
733 // Disable background.
734 if( controller->IsBackgroundEnabled() )
736 controller->SetBackgroundEnabled( false );
742 case EffectStyle::INPUT:
744 // Text background is not supported while inputting yet
748 } // if( controller )
753 void GetBackgroundProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
759 case EffectStyle::DEFAULT:
761 const bool enabled = controller->IsBackgroundEnabled();
762 const Vector4& color = controller->GetBackgroundColor();
765 map.Insert( ENABLE_KEY, enabled );
766 map.Insert( COLOR_KEY, color );
773 case EffectStyle::INPUT:
775 // Text background is not supported while inputting yet
785 } // namespace Toolkit