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,
178 unsigned int& width )
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<unsigned int>( 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 const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
362 map.Insert( ENABLE_KEY, enabledStr );
364 std::string colorStr;
365 Vector4ToColorString( color, colorStr );
366 map.Insert( COLOR_KEY, colorStr );
368 std::string heightStr;
369 FloatToString( height, heightStr );
370 map.Insert( HEIGHT_KEY, heightStr );
377 case EffectStyle::INPUT:
379 value = controller->GetInputUnderlineProperties();
386 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
394 case EffectStyle::DEFAULT:
396 const Property::Map& propertiesMap = value.Get<Property::Map>();
398 bool colorDefined = false;
400 bool offsetDefined = false;
402 bool blurRadiusDefined = false;
407 if ( propertiesMap.Empty() )
409 // Map empty so check if a string provided
410 const std::string propertyString = value.Get<std::string>();
412 Property::Map parsedStringMap;
413 Text::ParsePropertyString( propertyString, parsedStringMap );
415 empty = ParseShadowProperties( parsedStringMap,
423 controller->ShadowSetByString( !empty );
428 empty = ParseShadowProperties( propertiesMap,
436 controller->ShadowSetByString( false );
441 // Sets the default shadow values.
442 if( colorDefined && ( controller->GetShadowColor() != color ) )
444 controller->SetShadowColor( color );
448 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
450 controller->SetShadowOffset( offset );
454 if( blurRadiusDefined && ( controller->GetShadowBlurRadius() != blurRadius ) )
456 controller->SetShadowBlurRadius( blurRadius );
463 if( Vector2::ZERO != controller->GetShadowOffset() )
465 controller->SetShadowOffset( Vector2::ZERO );
470 case EffectStyle::INPUT:
472 const std::string& shadowString = value.Get<std::string>();
474 controller->SetInputShadowProperties( shadowString );
478 } // if( controller )
483 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
489 case EffectStyle::DEFAULT:
491 const Vector4& color = controller->GetShadowColor();
492 const Vector2& offset = controller->GetShadowOffset();
493 const float& blurRadius = controller->GetShadowBlurRadius();
495 if ( controller->IsShadowSetByString() )
497 std::string shadowProperties = "{";
499 std::string colorStr;
500 Vector4ToColorString( color, colorStr );
501 shadowProperties += "\"color\":\"" + colorStr + "\",";
503 std::string offsetStr;
504 Vector2ToString( offset, offsetStr );
505 shadowProperties += "\"offset\":\"" + offsetStr + "\",";
507 std::string blurRadiusStr;
508 FloatToString( blurRadius, blurRadiusStr );
509 shadowProperties += "\"blurRadius\":\"" + blurRadiusStr + "\"}";
511 value = shadowProperties;
517 map.Insert( COLOR_KEY, color );
518 map.Insert( OFFSET_KEY, offset );
519 map.Insert( BLUR_RADIUS_KEY, blurRadius );
525 case EffectStyle::INPUT:
527 value = controller->GetInputShadowProperties();
534 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
540 const std::string properties = value.Get< std::string >();
544 case EffectStyle::DEFAULT:
546 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
547 controller->SetDefaultEmbossProperties( properties );
550 case EffectStyle::INPUT:
552 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
553 controller->SetInputEmbossProperties( properties );
562 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
568 case EffectStyle::DEFAULT:
570 value = controller->GetDefaultEmbossProperties();
573 case EffectStyle::INPUT:
575 value = controller->GetInputEmbossProperties();
582 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
590 case EffectStyle::DEFAULT:
592 const Property::Map& propertiesMap = value.Get<Property::Map>();
594 bool colorDefined = false;
596 bool widthDefined = false;
597 unsigned int width = 0u;
601 if ( propertiesMap.Empty() )
603 // Map empty so check if a string provided
604 // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
605 const std::string propertyString = value.Get<std::string>();
607 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
608 controller->SetDefaultOutlineProperties( propertyString );
610 controller->OutlineSetByString( true );
614 empty = ParseOutlineProperties( propertiesMap,
620 controller->OutlineSetByString( false );
625 // Sets the default outline values.
626 if( colorDefined && ( controller->GetOutlineColor() != color ) )
628 controller->SetOutlineColor( color );
632 if( widthDefined && ( controller->GetOutlineWidth() != width ) )
634 controller->SetOutlineWidth( width );
641 if( 0u != controller->GetOutlineWidth() )
643 controller->SetOutlineWidth( 0u );
649 case EffectStyle::INPUT:
651 const std::string& outlineProperties = value.Get<std::string>();
653 controller->SetInputOutlineProperties( outlineProperties );
657 } // if( controller )
662 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
668 case EffectStyle::DEFAULT:
670 if ( controller->IsOutlineSetByString() )
672 value = controller->GetDefaultOutlineProperties();
677 const Vector4& color = controller->GetOutlineColor();
678 const unsigned int width = controller->GetOutlineWidth();
681 map.Insert( COLOR_KEY, color );
682 map.Insert( WIDTH_KEY, static_cast<int>( width ) );
689 case EffectStyle::INPUT:
691 value = controller->GetInputOutlineProperties();
698 bool SetBackgroundProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
706 case EffectStyle::DEFAULT:
708 const Property::Map& propertiesMap = value.Get<Property::Map>();
710 bool enabled = false;
711 bool colorDefined = false;
716 if ( !propertiesMap.Empty() )
718 empty = ParseBackgroundProperties( propertiesMap,
726 if( enabled != controller->IsBackgroundEnabled() )
728 controller->SetBackgroundEnabled( enabled );
732 if( colorDefined && ( controller->GetBackgroundColor() != color ) )
734 controller->SetBackgroundColor( color );
740 // Disable background.
741 if( controller->IsBackgroundEnabled() )
743 controller->SetBackgroundEnabled( false );
749 case EffectStyle::INPUT:
751 // Text background is not supported while inputting yet
755 } // if( controller )
760 void GetBackgroundProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
766 case EffectStyle::DEFAULT:
768 const bool enabled = controller->IsBackgroundEnabled();
769 const Vector4& color = controller->GetBackgroundColor();
772 map.Insert( ENABLE_KEY, enabled );
773 map.Insert( COLOR_KEY, color );
780 case EffectStyle::INPUT:
782 // Text background is not supported while inputting yet
792 } // namespace Toolkit