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" );
43 bool ParseProperties( const std::string& shadowProperties,
49 // Parses and applies the style.
51 Text::ParsePropertyString( shadowProperties, map );
53 const bool empty = map.Empty();
58 Property::Value* colorValue = map.Find( COLOR_KEY );
60 colorDefined = colorValue != NULL;
63 const std::string colorStr = colorValue->Get<std::string>();
65 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
69 Property::Value* offsetValue = map.Find( OFFSET_KEY );
71 offsetDefined = offsetValue != NULL;
74 const std::string offsetStr = offsetValue->Get<std::string>();
76 StringToVector2( offsetStr.c_str(), offsetStr.size(), offset );
83 bool ParseProperties( const std::string& underlineProperties,
90 // Parses and applies the style.
92 Text::ParsePropertyString( underlineProperties, map );
94 const bool empty = map.Empty();
99 Property::Value* enableValue = map.Find( ENABLE_KEY );
102 const bool enabledDefined = enableValue != NULL;
105 const std::string enableStr = enableValue->Get<std::string>();
106 enabled = Text::TokenComparison( TRUE_TOKEN, enableStr.c_str(), enableStr.size() );
110 Property::Value* colorValue = map.Find( COLOR_KEY );
112 colorDefined = colorValue != NULL;
115 const std::string colorStr = colorValue->Get<std::string>();
117 Text::ColorStringToVector4( colorStr.c_str(), colorStr.size(), color );
121 Property::Value* heightValue = map.Find( HEIGHT_KEY );
124 heightDefined = heightValue != NULL;
127 const std::string heightStr = heightValue->Get<std::string>();
129 height = StringToFloat( heightStr.c_str() );
136 bool SetUnderlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
142 const std::string properties = value.Get<std::string>();
144 bool enabled = false;
145 bool colorDefined = false;
147 bool heightDefined = false;
150 const bool empty = ParseProperties( properties,
161 case EffectStyle::DEFAULT:
163 if( enabled != controller->IsUnderlineEnabled() )
165 controller->SetUnderlineEnabled( enabled );
169 // Sets the default underline values.
170 if( colorDefined && ( controller->GetUnderlineColor() != color ) )
172 controller->SetUnderlineColor( color );
176 if( heightDefined && ( fabsf( controller->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 ) )
178 controller->SetUnderlineHeight( height );
183 case EffectStyle::INPUT:
185 // Sets the input underline values.
186 // TODO: to be implemented.
195 case EffectStyle::DEFAULT:
197 // Disable underline.
198 if( controller->IsUnderlineEnabled() )
200 controller->SetUnderlineEnabled( false );
205 case EffectStyle::INPUT:
207 // Sets the input underline values.
208 // TODO: to be implemented.
209 controller->SetInputUnderlineProperties( properties );
219 void GetUnderlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
225 case EffectStyle::DEFAULT:
227 const bool enabled = controller->IsUnderlineEnabled();
228 const Vector4& color = controller->GetUnderlineColor();
229 const float height = controller->GetUnderlineHeight();
231 std::string underlineProperties = "{\"enable\":";
232 const std::string enabledStr = enabled ? "true" : "false";
233 underlineProperties += "\"" + enabledStr + "\",";
235 std::string colorStr;
236 Vector4ToColorString( color, colorStr );
237 underlineProperties += "\"color\":\"" + colorStr + "\",";
239 std::string heightStr;
240 FloatToString( height, heightStr );
241 underlineProperties += "\"height\":\"" + heightStr + "\"}";
243 value = underlineProperties;
246 case EffectStyle::INPUT:
248 value = controller->GetInputUnderlineProperties();
255 bool SetShadowProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
261 const std::string properties = value.Get< std::string >();
263 bool colorDefined = false;
265 bool offsetDefined = false;
268 const bool empty = ParseProperties( properties,
278 case EffectStyle::DEFAULT:
280 // Sets the default shadow values.
281 if( colorDefined && ( controller->GetShadowColor() != color ) )
283 controller->SetShadowColor( color );
287 if( offsetDefined && ( controller->GetShadowOffset() != offset ) )
289 controller->SetShadowOffset( offset );
294 case EffectStyle::INPUT:
296 // Sets the input shadow values.
297 // TODO: to be implemented.
306 case EffectStyle::DEFAULT:
309 if( Vector2::ZERO != controller->GetShadowOffset() )
311 controller->SetShadowOffset( Vector2::ZERO );
315 case EffectStyle::INPUT:
317 // Sets the input shadow values.
318 // TODO: to be implemented.
319 controller->SetInputShadowProperties( properties );
329 void GetShadowProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
335 case EffectStyle::DEFAULT:
337 const Vector4& color = controller->GetShadowColor();
338 const Vector2& offset = controller->GetShadowOffset();
340 std::string shadowProperties = "{";
342 std::string colorStr;
343 Vector4ToColorString( color, colorStr );
344 shadowProperties += "\"color\":\"" + colorStr + "\",";
346 std::string offsetStr;
347 Vector2ToString( offset, offsetStr );
348 shadowProperties += "\"offset\":\"" + offsetStr + "\"}";
350 value = shadowProperties;
353 case EffectStyle::INPUT:
355 value = controller->GetInputShadowProperties();
362 bool SetEmbossProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
368 const std::string properties = value.Get< std::string >();
372 case EffectStyle::DEFAULT:
374 // Stores the default emboss's properties string to be recovered by the GetEmbossProperties() function.
375 controller->SetDefaultEmbossProperties( properties );
378 case EffectStyle::INPUT:
380 // Stores the input emboss's properties string to be recovered by the GetEmbossProperties() function.
381 controller->SetInputEmbossProperties( properties );
390 void GetEmbossProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
396 case EffectStyle::DEFAULT:
398 value = controller->GetDefaultEmbossProperties();
401 case EffectStyle::INPUT:
403 value = controller->GetInputEmbossProperties();
410 bool SetOutlineProperties( ControllerPtr controller, const Property::Value& value, EffectStyle::Type type )
416 const std::string properties = value.Get< std::string >();
420 case EffectStyle::DEFAULT:
422 // Stores the default outline's properties string to be recovered by the GetOutlineProperties() function.
423 controller->SetDefaultOutlineProperties( properties );
426 case EffectStyle::INPUT:
428 // Stores the input outline's properties string to be recovered by the GetOutlineProperties() function.
429 controller->SetInputOutlineProperties( properties );
438 void GetOutlineProperties( ControllerPtr controller, Property::Value& value, EffectStyle::Type type )
444 case EffectStyle::DEFAULT:
446 value = controller->GetDefaultOutlineProperties();
449 case EffectStyle::INPUT:
451 value = controller->GetInputOutlineProperties();
460 } // namespace Toolkit