2 * Copyright (c) 2015 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/devel-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/devel-api/controls/control-depth-index-ranges.h>
28 #include <dali-toolkit/internal/text/rendering/text-backend.h>
29 #include <dali-toolkit/internal/text/text-font-style.h>
30 #include <dali-toolkit/internal/text/text-view.h>
31 #include <dali-toolkit/internal/styling/style-manager-impl.h>
33 using Dali::Toolkit::Text::LayoutEngine;
34 using Dali::Toolkit::Text::Backend;
47 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
53 #if defined(DEBUG_ENABLED)
54 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
57 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
59 { "BEGIN", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN },
60 { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
61 { "END", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END },
63 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
65 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
67 { "TOP", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP },
68 { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
69 { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
71 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
76 return Toolkit::TextLabel::New();
79 // Setup properties, signals and actions using the type-registry.
80 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontStyle", STRING, FONT_STYLE )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "textColor", VECTOR4, TEXT_COLOR )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowOffset", VECTOR2, SHADOW_OFFSET )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowColor", VECTOR4, SHADOW_COLOR )
93 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineEnabled", BOOLEAN, UNDERLINE_ENABLED )
94 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineColor", VECTOR4, UNDERLINE_COLOR )
95 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineHeight", FLOAT, UNDERLINE_HEIGHT )
96 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableMarkup", BOOLEAN, ENABLE_MARKUP )
98 DALI_TYPE_REGISTRATION_END()
102 Toolkit::TextLabel TextLabel::New()
104 // Create the implementation, temporarily owned by this handle on stack
105 IntrusivePtr< TextLabel > impl = new TextLabel();
107 // Pass ownership to CustomActor handle
108 Toolkit::TextLabel handle( *impl );
110 // Second-phase init of the implementation
111 // This can only be done after the CustomActor connection has been made...
117 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
119 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
123 TextLabel& impl( GetImpl( label ) );
126 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
128 const int backend = value.Get< int >();
130 if( impl.mRenderingBackend != backend )
132 impl.mRenderingBackend = backend;
133 impl.mRenderer.Reset();
134 impl.RequestTextRelayout();
138 case Toolkit::TextLabel::Property::TEXT:
140 if( impl.mController )
142 impl.mController->SetText( value.Get< std::string >() );
146 case Toolkit::TextLabel::Property::FONT_FAMILY:
148 if( impl.mController )
150 const std::string fontFamily = value.Get< std::string >();
152 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
153 impl.mController->SetDefaultFontFamily( fontFamily );
157 case Toolkit::TextLabel::Property::FONT_STYLE:
159 SetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
162 case Toolkit::TextLabel::Property::POINT_SIZE:
164 if( impl.mController )
166 const float pointSize = value.Get< float >();
168 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
170 impl.mController->SetDefaultPointSize( pointSize );
175 case Toolkit::TextLabel::Property::MULTI_LINE:
177 if( impl.mController )
179 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
183 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
185 if( impl.mController )
187 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
188 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
189 HORIZONTAL_ALIGNMENT_STRING_TABLE,
190 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
193 impl.mController->SetHorizontalAlignment( alignment );
198 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
200 if( impl.mController )
202 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
203 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
204 VERTICAL_ALIGNMENT_STRING_TABLE,
205 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
208 impl.mController->SetVerticalAlignment( alignment );
214 case Toolkit::TextLabel::Property::TEXT_COLOR:
216 if( impl.mController )
218 const Vector4 textColor = value.Get< Vector4 >();
219 if( impl.mController->GetTextColor() != textColor )
221 impl.mController->SetTextColor( textColor );
222 impl.mRenderer.Reset();
228 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
230 if( impl.mController )
232 const Vector2 shadowOffset = value.Get< Vector2 >();
233 if ( impl.mController->GetShadowOffset() != shadowOffset )
235 impl.mController->SetShadowOffset( shadowOffset );
236 impl.mRenderer.Reset();
241 case Toolkit::TextLabel::Property::SHADOW_COLOR:
243 if( impl.mController )
245 const Vector4 shadowColor = value.Get< Vector4 >();
246 if ( impl.mController->GetShadowColor() != shadowColor )
248 impl.mController->SetShadowColor( shadowColor );
249 impl.mRenderer.Reset();
254 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
256 if( impl.mController )
258 const Vector4 color = value.Get< Vector4 >();
259 if ( impl.mController->GetUnderlineColor() != color )
261 impl.mController->SetUnderlineColor( color );
262 impl.mRenderer.Reset();
267 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
269 if( impl.mController )
271 const bool enabled = value.Get< bool >();
272 if ( impl.mController->IsUnderlineEnabled() != enabled )
274 impl.mController->SetUnderlineEnabled( enabled );
275 impl.mRenderer.Reset();
281 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
283 if( impl.mController )
285 float height = value.Get< float >();
286 if( fabsf( impl.mController->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 )
288 impl.mController->SetUnderlineHeight( height );
289 impl.mRenderer.Reset();
294 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
296 if( impl.mController )
298 const bool enableMarkup = value.Get<bool>();
299 impl.mController->SetMarkupProcessorEnabled( enableMarkup );
307 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
309 Property::Value value;
311 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
315 TextLabel& impl( GetImpl( label ) );
318 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
320 value = impl.mRenderingBackend;
323 case Toolkit::TextLabel::Property::TEXT:
325 if( impl.mController )
328 impl.mController->GetText( text );
333 case Toolkit::TextLabel::Property::FONT_FAMILY:
335 if( impl.mController )
337 value = impl.mController->GetDefaultFontFamily();
341 case Toolkit::TextLabel::Property::FONT_STYLE:
343 GetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
346 case Toolkit::TextLabel::Property::POINT_SIZE:
348 if( impl.mController )
350 value = impl.mController->GetDefaultPointSize();
354 case Toolkit::TextLabel::Property::MULTI_LINE:
356 if( impl.mController )
358 value = impl.mController->IsMultiLineEnabled();
362 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
364 if( impl.mController )
366 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
367 HORIZONTAL_ALIGNMENT_STRING_TABLE,
368 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
371 value = std::string( name );
376 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
378 if( impl.mController )
380 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
381 VERTICAL_ALIGNMENT_STRING_TABLE,
382 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
385 value = std::string( name );
390 case Toolkit::TextLabel::Property::TEXT_COLOR:
392 if ( impl.mController )
394 value = impl.mController->GetTextColor();
398 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
400 if ( impl.mController )
402 value = impl.mController->GetShadowOffset();
406 case Toolkit::TextLabel::Property::SHADOW_COLOR:
408 if ( impl.mController )
410 value = impl.mController->GetShadowColor();
414 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
416 if ( impl.mController )
418 value = impl.mController->GetUnderlineColor();
422 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
424 if ( impl.mController )
426 value = impl.mController->IsUnderlineEnabled();
430 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
432 if ( impl.mController )
434 value = impl.mController->GetUnderlineHeight();
438 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
440 if( impl.mController )
442 value = impl.mController->IsMarkupProcessorEnabled();
452 void TextLabel::OnInitialize()
456 mController = Text::Controller::New( *this );
458 // Use height-for-width negotiation by default
459 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
460 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
462 // Enable the text ellipsis.
463 LayoutEngine& engine = mController->GetLayoutEngine();
465 engine.SetTextEllipsisEnabled( true );
466 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
468 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
471 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
474 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
478 case StyleChange::DEFAULT_FONT_CHANGE:
480 // Property system did not set the font so should update it.
481 std::string newFont = styleManager.GetDefaultFontFamily();
482 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
483 mController->UpdateAfterFontChange( newFont );
487 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
489 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
491 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
493 // Property system did not set the PointSize so should update it.
494 // todo instruct text-controller to update model
498 case StyleChange::THEME_CHANGE:
500 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
506 Vector3 TextLabel::GetNaturalSize()
508 return mController->GetNaturalSize();
511 float TextLabel::GetHeightForWidth( float width )
513 return mController->GetHeightForWidth( width );
516 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
518 if( mController->Relayout( size ) ||
523 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
529 void TextLabel::RequestTextRelayout()
534 void TextLabel::RenderText()
537 Actor renderableActor;
540 renderableActor = mRenderer->Render( mController->GetView(), DepthIndex::TEXT );
543 if( renderableActor != mRenderableActor )
545 UnparentAndReset( mRenderableActor );
547 if( renderableActor )
549 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
550 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
552 self.Add( renderableActor );
554 mRenderableActor = renderableActor;
558 void TextLabel::OnStageConnect( Dali::Actor actor )
560 if ( mHasBeenStaged )
566 mHasBeenStaged = true;
570 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
572 // TextLabel does not show decorations
575 void TextLabel::OnStageConnection( int depth )
577 // Call the Control::OnStageConnection() to set the depth of the background.
578 Control::OnStageConnection( depth );
580 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
583 void TextLabel::TextChanged()
585 // TextLabel does not provide a signal for this
588 void TextLabel::MaxLengthReached()
590 // Pure Virtual from TextController Interface, only needed when inputting text
593 TextLabel::TextLabel()
594 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
595 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
596 mHasBeenStaged( false )
600 TextLabel::~TextLabel()
604 } // namespace Internal
606 } // namespace Toolkit