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 const std::string enableStr = valueGet.second.Get<std::string>();
129 enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
131 else if( COLOR_KEY == valueGet.first.stringKey )
136 const std::string colorStr = valueGet.second.Get<std::string>();
138 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
140 else if( HEIGHT_KEY == valueGet.first.stringKey )
143 heightDefined = true;
145 const std::string heightStr = valueGet.second.Get<std::string>();
147 height = StringToFloat( heightStr.c_str() );
151 return 0u == numberOfItems;
154 bool ParseOutlineProperties( const Property::Map& underlinePropertiesMap,
160 const unsigned int numberOfItems = underlinePropertiesMap.Count();
162 // Parses and applies the style.
163 for( unsigned int index = 0u; index < numberOfItems; ++index )
165 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
167 if( COLOR_KEY == valueGet.first.stringKey )
171 color = valueGet.second.Get<Vector4>();
173 else if( WIDTH_KEY == valueGet.first.stringKey )
177 width = valueGet.second.Get<float>();
181 return 0u == numberOfItems;
184 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
192 case EffectStyle::DEFAULT:
194 const Property::Map& propertiesMap = value.Get<Property::Map>();
196 bool enabled = false;
197 bool colorDefined = false;
199 bool heightDefined = false;
204 if ( propertiesMap.Empty() )
206 // Map empty so check if a string provided
207 const std::string propertyString = value.Get<std::string>();
209 if ( !propertyString.empty() )
211 Property::Map parsedStringMap;
212 Text::ParsePropertyString( propertyString, parsedStringMap );
214 empty = ParseUnderlineProperties( parsedStringMap,
221 controller->UnderlineSetByString( !empty);
226 empty = ParseUnderlineProperties( propertiesMap,
233 controller->UnderlineSetByString( false );
238 if( enabled != controller->IsUnderlineEnabled() )
240 controller->SetUnderlineEnabled( enabled );
244 // Sets the default underline values.
245 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
247 controller->SetUnderlineColor( color );
251 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
253 controller->SetUnderlineHeight( height );
259 // Disable underline.
260 if( controller->IsUnderlineEnabled() )
262 controller->SetUnderlineEnabled( false );
268 case EffectStyle::INPUT:
270 const std::string& underlineProperties = value.Get<std::string>();
272 controller->SetInputUnderlineProperties( underlineProperties );
276 } // if( controller )
281 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
287 case EffectStyle::DEFAULT:
289 const bool enabled = controller->IsUnderlineEnabled();
290 const Vector4& color = controller->GetUnderlineColor();
291 const float height = controller->GetUnderlineHeight();
293 if ( controller->IsUnderlineSetByString() )
295 std::string underlineProperties = "{\"enable\":";
296 const std::string enabledStr = enabled ? "true" : "false";
297 underlineProperties += "\"" + enabledStr + "\",";
299 std::string colorStr;
300 Vector4ToColorString( color, colorStr );
301 underlineProperties += "\"color\":\"" + colorStr + "\",";
303 std::string heightStr;
304 FloatToString( height, heightStr );
305 underlineProperties += "\"height\":\"" + heightStr + "\"}";
307 value = underlineProperties;
313 const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
314 map.Insert( ENABLE_KEY, enabledStr );
316 std::string colorStr;
317 Vector4ToColorString( color, colorStr );
318 map.Insert( COLOR_KEY, colorStr );
320 std::string heightStr;
321 FloatToString( height, heightStr );
322 map.Insert( HEIGHT_KEY, heightStr );
329 case EffectStyle::INPUT:
331 value = controller->GetInputUnderlineProperties();
338 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
346 case EffectStyle::DEFAULT:
348 const Property::Map& propertiesMap = value.Get<Property::Map>();
350 bool colorDefined = false;
352 bool offsetDefined = false;
354 bool blurRadiusDefined = false;
359 if ( propertiesMap.Empty() )
361 // Map empty so check if a string provided
362 const std::string propertyString = value.Get<std::string>();
364 Property::Map parsedStringMap;
365 Text::ParsePropertyString( propertyString, parsedStringMap );
367 empty = ParseShadowProperties( parsedStringMap,
375 controller->ShadowSetByString( !empty );
380 empty = ParseShadowProperties( propertiesMap,
388 controller->ShadowSetByString( false );
393 // Sets the default shadow values.
394 if( colorDefined && ( controller->GetShadowColor() != color ) )
396 controller->SetShadowColor( color );
400 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
402 controller->SetShadowOffset( offset );
406 if( blurRadiusDefined && ( controller->GetShadowBlurRadius() != blurRadius ) )
408 controller->SetShadowBlurRadius( blurRadius );
415 if( Vector2::ZERO != controller->GetShadowOffset() )
417 controller->SetShadowOffset( Vector2::ZERO );
422 case EffectStyle::INPUT:
424 const std::string& shadowString = value.Get<std::string>();
426 controller->SetInputShadowProperties( shadowString );
430 } // if( controller )
435 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
441 case EffectStyle::DEFAULT:
443 const Vector4& color = controller->GetShadowColor();
444 const Vector2& offset = controller->GetShadowOffset();
445 const float& blurRadius = controller->GetShadowBlurRadius();
447 if ( controller->IsShadowSetByString() )
449 std::string shadowProperties = "{";
451 std::string colorStr;
452 Vector4ToColorString( color, colorStr );
453 shadowProperties += "\"color\":\"" + colorStr + "\",";
455 std::string offsetStr;
456 Vector2ToString( offset, offsetStr );
457 shadowProperties += "\"offset\":\"" + offsetStr + "\",";
459 std::string blurRadiusStr;
460 FloatToString( blurRadius, blurRadiusStr );
461 shadowProperties += "\"blurRadius\":\"" + blurRadiusStr + "\"}";
463 value = shadowProperties;
469 map.Insert( COLOR_KEY, color );
470 map.Insert( OFFSET_KEY, offset );
471 map.Insert( BLUR_RADIUS_KEY, blurRadius );
477 case EffectStyle::INPUT:
479 value = controller->GetInputShadowProperties();
486 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
492 const std::string properties = value.Get< std::string >();
496 case EffectStyle::DEFAULT:
498 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
499 controller->SetDefaultEmbossProperties( properties );
502 case EffectStyle::INPUT:
504 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
505 controller->SetInputEmbossProperties( properties );
514 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
520 case EffectStyle::DEFAULT:
522 value = controller->GetDefaultEmbossProperties();
525 case EffectStyle::INPUT:
527 value = controller->GetInputEmbossProperties();
534 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
542 case EffectStyle::DEFAULT:
544 const Property::Map& propertiesMap = value.Get<Property::Map>();
546 bool colorDefined = false;
548 bool widthDefined = false;
553 if ( propertiesMap.Empty() )
555 // Map empty so check if a string provided
556 // This is purely to maintain backward compatibility, but we don't parse the string to be a property map.
557 const std::string propertyString = value.Get<std::string>();
559 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
560 controller->SetDefaultOutlineProperties( propertyString );
562 controller->OutlineSetByString( true );
566 empty = ParseOutlineProperties( propertiesMap,
572 controller->OutlineSetByString( false );
577 // Sets the default outline values.
578 if( colorDefined && ( controller->GetOutlineColor() != color ) )
580 controller->SetOutlineColor( color );
584 if( widthDefined && ( fabsf( controller->GetOutlineWidth() - width ) > Math::MACHINE_EPSILON_1000 ) )
586 controller->SetOutlineWidth( width );
593 if( fabsf( controller->GetOutlineWidth() ) > Math::MACHINE_EPSILON_1000 )
595 controller->SetOutlineWidth( 0.0f );
601 case EffectStyle::INPUT:
603 const std::string& outlineProperties = value.Get<std::string>();
605 controller->SetInputOutlineProperties( outlineProperties );
609 } // if( controller )
614 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
620 case EffectStyle::DEFAULT:
622 if ( controller->IsOutlineSetByString() )
624 value = controller->GetDefaultOutlineProperties();
629 const Vector4& color = controller->GetOutlineColor();
630 const float width = controller->GetOutlineWidth();
634 std::string colorStr;
635 Vector4ToColorString( color, colorStr );
636 map.Insert( COLOR_KEY, colorStr );
638 std::string widthStr;
639 FloatToString( width, widthStr );
640 map.Insert( WIDTH_KEY, widthStr );
647 case EffectStyle::INPUT:
649 value = controller->GetInputOutlineProperties();
658 } // namespace Toolkit