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/controls/text-controls/text-label-impl.h>
22 #include <dali/public-api/object/type-registry-helper.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/public-api/text/rendering-backend.h>
27 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
28 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
29 #include <dali-toolkit/internal/text/text-effects-style.h>
30 #include <dali-toolkit/internal/styling/style-manager-impl.h>
32 using Dali::Toolkit::Text::LayoutEngine;
45 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
51 #if defined ( DEBUG_ENABLED )
52 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
58 return Toolkit::TextLabel::New();
61 // Setup properties, signals and actions using the type-registry.
62 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
64 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
65 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
66 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
67 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontStyle", STRING, FONT_STYLE )
68 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
69 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
70 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
71 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
72 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "textColor", VECTOR4, TEXT_COLOR )
73 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowOffset", VECTOR2, SHADOW_OFFSET )
74 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowColor", VECTOR4, SHADOW_COLOR )
75 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineEnabled", BOOLEAN, UNDERLINE_ENABLED )
76 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineColor", VECTOR4, UNDERLINE_COLOR )
77 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineHeight", FLOAT, UNDERLINE_HEIGHT )
78 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableMarkup", BOOLEAN, ENABLE_MARKUP )
79 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableAutoScroll", BOOLEAN, ENABLE_AUTO_SCROLL )
80 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollSpeed", INTEGER, AUTO_SCROLL_SPEED )
81 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollLoopCount", INTEGER, AUTO_SCROLL_LOOP_COUNT )
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollGap", FLOAT, AUTO_SCROLL_GAP )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "lineSpacing", FLOAT, LINE_SPACING )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline", STRING, UNDERLINE )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow", STRING, SHADOW )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "emboss", STRING, EMBOSS )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "outline", STRING, OUTLINE )
89 DALI_TYPE_REGISTRATION_END()
91 const int TEXT_VISUAL_PROPERTY_TABLE[] = {
92 Dali::Toolkit::TextVisual::Property::RENDERING_BACKEND,
93 Dali::Toolkit::TextVisual::Property::TEXT,
94 Dali::Toolkit::TextVisual::Property::FONT_FAMILY,
95 Dali::Toolkit::TextVisual::Property::FONT_STYLE,
96 Dali::Toolkit::TextVisual::Property::POINT_SIZE,
97 Dali::Toolkit::TextVisual::Property::MULTI_LINE,
98 Dali::Toolkit::TextVisual::Property::HORIZONTAL_ALIGNMENT,
99 Dali::Toolkit::TextVisual::Property::VERTICAL_ALIGNMENT,
100 Dali::Toolkit::TextVisual::Property::TEXT_COLOR,
101 Dali::Toolkit::TextVisual::Property::SHADOW,
102 Dali::Toolkit::TextVisual::Property::SHADOW,
103 Dali::Toolkit::TextVisual::Property::UNDERLINE,
104 Dali::Toolkit::TextVisual::Property::UNDERLINE,
105 Dali::Toolkit::TextVisual::Property::UNDERLINE,
106 Dali::Toolkit::TextVisual::Property::ENABLE_MARKUP,
107 Dali::Toolkit::TextVisual::Property::ENABLE_AUTO_SCROLL,
108 Dali::Toolkit::TextVisual::Property::AUTO_SCROLL_SPEED,
109 Dali::Toolkit::TextVisual::Property::AUTO_SCROLL_LOOP_COUNT,
110 Dali::Toolkit::TextVisual::Property::AUTO_SCROLL_GAP,
111 Dali::Toolkit::TextVisual::Property::LINE_SPACING,
112 Dali::Toolkit::TextVisual::Property::UNDERLINE,
113 Dali::Toolkit::TextVisual::Property::SHADOW,
114 Dali::Toolkit::TextVisual::Property::EMBOSS,
115 Dali::Toolkit::TextVisual::Property::OUTLINE,
116 Dali::Toolkit::TextVisual::Property::BATCHING_ENABLED
121 Toolkit::TextLabel TextLabel::New()
123 // Create the implementation, temporarily owned by this handle on stack
124 IntrusivePtr< TextLabel > impl = new TextLabel();
126 // Pass ownership to CustomActor handle
127 Toolkit::TextLabel handle( *impl );
129 // Second-phase init of the implementation
130 // This can only be done after the CustomActor connection has been made...
136 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
138 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
142 TextLabel& impl = GetImpl( label );
144 // Sets the property to the to the text visual.
145 impl.mVisual.SetProperty( TEXT_VISUAL_PROPERTY_TABLE[index - Toolkit::TextLabel::PROPERTY_START_INDEX], value );
149 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
151 Property::Value value;
153 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
157 TextLabel& impl = GetImpl( label );
159 if( ( Toolkit::TextLabel::Property::SHADOW_OFFSET == index ) ||
160 ( Toolkit::TextLabel::Property::SHADOW_COLOR == index ) ||
161 ( Toolkit::TextLabel::Property::UNDERLINE_ENABLED == index ) ||
162 ( Toolkit::TextLabel::Property::UNDERLINE_COLOR == index ) ||
163 ( Toolkit::TextLabel::Property::UNDERLINE_HEIGHT == index ) )
165 // TODO : Branch to be removed when the deprecated properties are finally removed.
166 value = impl.mVisual.GetProperty( TEXT_VISUAL_PROPERTY_TABLE[index - Toolkit::TextLabel::PROPERTY_START_INDEX] );
170 case Toolkit::TextLabel::Property::SHADOW_OFFSET: // Converts the deprecated property to the new one.
172 bool colorDefined = false;
174 bool offsetDefined = false;
176 const bool empty = Text::ParseProperties( value.Get<std::string>(),
188 case Toolkit::TextLabel::Property::SHADOW_COLOR: // Converts the deprecated property to the new one.
190 bool colorDefined = false;
192 bool offsetDefined = false;
194 const bool empty = Text::ParseProperties( value.Get<std::string>(),
206 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED: // Converts the deprecated property to the new one.
208 bool enabled = false;
209 bool colorDefined = false;
211 bool heightDefined = false;
213 const bool empty = Text::ParseProperties( value.Get<std::string>(),
226 case Toolkit::TextLabel::Property::UNDERLINE_COLOR: // Converts the deprecated property to the new one.
228 bool enabled = false;
229 bool colorDefined = false;
231 bool heightDefined = false;
233 const bool empty = Text::ParseProperties( value.Get<std::string>(),
240 if( !empty && colorDefined )
246 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT: // Converts the deprecated property to the new one.
248 bool enabled = false;
249 bool colorDefined = false;
251 bool heightDefined = false;
253 const bool empty = Text::ParseProperties( value.Get<std::string>(),
260 if( !empty && heightDefined )
275 // Retrieves the property from the text visual.
276 value = impl.mVisual.GetProperty( TEXT_VISUAL_PROPERTY_TABLE[index - Toolkit::TextLabel::PROPERTY_START_INDEX] );
283 void TextLabel::OnInitialize()
287 // Creates the text's visual.
288 Property::Map visualMap;
289 visualMap[Toolkit::VisualProperty::TYPE] = Toolkit::Visual::TEXT;
290 visualMap[Toolkit::TextVisual::Property::RENDERING_BACKEND] = static_cast<int>( DEFAULT_RENDERING_BACKEND );
292 mVisual = Toolkit::VisualFactory::Get().CreateVisual( visualMap );
293 RegisterVisual( Toolkit::TextLabel::Property::TEXT, self, mVisual );
295 Internal::Visual::Base& visualBase = Toolkit::GetImplementation( mVisual );
296 TextVisual* textVisual = static_cast<TextVisual*>( &visualBase );
298 // Sets the text's control interface.
299 textVisual->SetTextControlInterface( this );
301 // Use height-for-width negotiation by default
302 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
303 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
306 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
308 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
312 case StyleChange::DEFAULT_FONT_CHANGE:
314 // Property system did not set the font so should update it.
315 const std::string& newFont = GetImpl( styleManager ).GetDefaultFontFamily();
316 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
318 const std::string fontString = "{\"family\":\"" + newFont + "\",\"type\":\"system\"}";
319 mVisual.SetProperty( Toolkit::TextVisual::Property::FONT_FAMILY, fontString );
322 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
324 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
327 case StyleChange::THEME_CHANGE:
329 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
335 Vector3 TextLabel::GetNaturalSize()
338 mVisual.GetNaturalSize( naturalSize );
340 return Vector3( naturalSize );
343 float TextLabel::GetHeightForWidth( float width )
345 return mVisual.GetHeightForWidth( width );
348 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
350 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnRelayout\n" );
352 mVisual.SetSize( size );
355 void TextLabel::RequestTextRelayout()
360 void TextLabel::OnStageConnection( int depth )
362 // Call the Control::OnStageConnection() to set the depth of the background.
363 Control::OnStageConnection( depth );
365 // The depth of the text renderer is set by the text-visual called from OnRelayout().
368 TextLabel::TextLabel()
369 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
373 TextLabel::~TextLabel()
377 } // namespace Internal
379 } // namespace Toolkit