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 HEIGHT_KEY( "height" );
39 const std::string ENABLE_KEY( "enable" );
40 const std::string TRUE_TOKEN( "true" );
41 const std::string FALSE_TOKEN( "false" );
44 bool ParseShadowProperties( const Property::Map& shadowPropertiesMap,
50 const unsigned int numberOfItems = shadowPropertiesMap.Count();
52 // Parses and applies the style.
53 for( unsigned int index = 0u; index < numberOfItems; ++index )
55 const KeyValuePair& valueGet = shadowPropertiesMap.GetKeyValue( index );
57 if( COLOR_KEY == valueGet.first.stringKey )
62 const std::string colorStr = valueGet.second.Get<std::string>();
64 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
66 else if( OFFSET_KEY == valueGet.first.stringKey )
71 const std::string offsetStr = valueGet.second.Get<std::string>();
73 StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
77 return 0u == numberOfItems;
80 bool ParseUnderlineProperties( const Property::Map& underlinePropertiesMap,
87 const unsigned int numberOfItems = underlinePropertiesMap.Count();
89 // Parses and applies the style.
90 for( unsigned int index = 0u; index < numberOfItems; ++index )
92 const KeyValuePair& valueGet = underlinePropertiesMap.GetKeyValue( index );
94 if( ENABLE_KEY == valueGet.first.stringKey )
97 const std::string enableStr = valueGet.second.Get<std::string>();
98 enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
100 else if( COLOR_KEY == valueGet.first.stringKey )
105 const std::string colorStr = valueGet.second.Get<std::string>();
107 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
109 else if( HEIGHT_KEY == valueGet.first.stringKey )
112 heightDefined = true;
114 const std::string heightStr = valueGet.second.Get<std::string>();
116 height = StringToFloat( heightStr.c_str() );
120 return 0u == numberOfItems;
123 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
131 case EffectStyle::DEFAULT:
133 const Property::Map& propertiesMap = value.Get<Property::Map>();
135 bool enabled = false;
136 bool colorDefined = false;
138 bool heightDefined = false;
143 if ( propertiesMap.Empty() )
145 // Map empty so check if a string provided
146 const std::string propertyString = value.Get<std::string>();
148 if ( !propertyString.empty() )
150 Property::Map parsedStringMap;
151 Text::ParsePropertyString( propertyString, parsedStringMap );
153 empty = ParseUnderlineProperties( parsedStringMap,
160 controller->UnderlineSetByString( !empty);
165 empty = ParseUnderlineProperties( propertiesMap,
172 controller->UnderlineSetByString( false );
177 if( enabled != controller->IsUnderlineEnabled() )
179 controller->SetUnderlineEnabled( enabled );
183 // Sets the default underline values.
184 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
186 controller->SetUnderlineColor( color );
190 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
192 controller->SetUnderlineHeight( height );
198 // Disable underline.
199 if( controller->IsUnderlineEnabled() )
201 controller->SetUnderlineEnabled( false );
207 case EffectStyle::INPUT:
209 const std::string& underlineProperties = value.Get<std::string>();
211 controller->SetInputUnderlineProperties( underlineProperties );
215 } // if( controller )
220 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
226 case EffectStyle::DEFAULT:
228 const bool enabled = controller->IsUnderlineEnabled();
229 const Vector4& color = controller->GetUnderlineColor();
230 const float height = controller->GetUnderlineHeight();
232 if ( controller->IsUnderlineSetByString() )
234 std::string underlineProperties = "{\"enable\":";
235 const std::string enabledStr = enabled ? "true" : "false";
236 underlineProperties += "\"" + enabledStr + "\",";
238 std::string colorStr;
239 Vector4ToColorString( color, colorStr );
240 underlineProperties += "\"color\":\"" + colorStr + "\",";
242 std::string heightStr;
243 FloatToString( height, heightStr );
244 underlineProperties += "\"height\":\"" + heightStr + "\"}";
246 value = underlineProperties;
252 const std::string enabledStr = enabled ? TRUE_TOKEN : FALSE_TOKEN;
253 map.Insert( ENABLE_KEY, enabledStr );
255 std::string colorStr;
256 Vector4ToColorString( color, colorStr );
257 map.Insert( COLOR_KEY, colorStr );
259 std::string heightStr;
260 FloatToString( height, heightStr );
261 map.Insert( HEIGHT_KEY, heightStr );
268 case EffectStyle::INPUT:
270 value = controller->GetInputUnderlineProperties();
277 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
285 case EffectStyle::DEFAULT:
287 const Property::Map& propertiesMap = value.Get<Property::Map>();
289 bool colorDefined = false;
291 bool offsetDefined = false;
296 if ( propertiesMap.Empty() )
298 // Map empty so check if a string provided
299 const std::string propertyString = value.Get<std::string>();
301 Property::Map parsedStringMap;
302 Text::ParsePropertyString( propertyString, parsedStringMap );
304 empty = ParseShadowProperties( parsedStringMap,
310 controller->ShadowSetByString( !empty );
315 empty = ParseShadowProperties( propertiesMap,
321 controller->ShadowSetByString( false );
326 // Sets the default shadow values.
327 if( colorDefined && ( controller->GetShadowColor() != color ) )
329 controller->SetShadowColor( color );
333 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
335 controller->SetShadowOffset( offset );
342 if( Vector2::ZERO != controller->GetShadowOffset() )
344 controller->SetShadowOffset( Vector2::ZERO );
349 case EffectStyle::INPUT:
351 const std::string& shadowString = value.Get<std::string>();
353 controller->SetInputShadowProperties( shadowString );
357 } // if( controller )
362 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
368 case EffectStyle::DEFAULT:
370 const Vector4& color = controller->GetShadowColor();
371 const Vector2& offset = controller->GetShadowOffset();
373 if ( controller->IsShadowSetByString() )
375 std::string shadowProperties = "{";
377 std::string colorStr;
378 Vector4ToColorString( color, colorStr );
379 shadowProperties += "\"color\":\"" + colorStr + "\",";
381 std::string offsetStr;
382 Vector2ToString( offset, offsetStr );
383 shadowProperties += "\"offset\":\"" + offsetStr + "\"}";
385 value = shadowProperties;
391 std::string colorStr;
392 Vector4ToColorString( color, colorStr );
393 map.Insert( COLOR_KEY, colorStr );
395 std::string offsetStr;
396 Vector2ToString( offset, offsetStr );
397 map.Insert( OFFSET_KEY, offsetStr );
403 case EffectStyle::INPUT:
405 value = controller->GetInputShadowProperties();
412 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
418 const std::string properties = value.Get< std::string >();
422 case EffectStyle::DEFAULT:
424 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
425 controller->SetDefaultEmbossProperties( properties );
428 case EffectStyle::INPUT:
430 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
431 controller->SetInputEmbossProperties( properties );
440 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
446 case EffectStyle::DEFAULT:
448 value = controller->GetDefaultEmbossProperties();
451 case EffectStyle::INPUT:
453 value = controller->GetInputEmbossProperties();
460 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
466 const std::string properties = value.Get< std::string >();
470 case EffectStyle::DEFAULT:
472 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
473 controller->SetDefaultOutlineProperties( properties );
476 case EffectStyle::INPUT:
478 // Stores the input outline's properties string to be recovered by the GetOutlineProperties() function.
479 controller->SetInputOutlineProperties( properties );
488 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
494 case EffectStyle::DEFAULT:
496 value = controller->GetDefaultOutlineProperties();
499 case EffectStyle::INPUT:
501 value = controller->GetInputOutlineProperties();
510 } // namespace Toolkit