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/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/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/text/text-definitions.h>
32 #include <dali-toolkit/internal/styling/style-manager-impl.h>
34 using Dali::Toolkit::Text::LayoutEngine;
35 using Dali::Toolkit::Text::Backend;
48 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
54 #if defined ( DEBUG_ENABLED )
55 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
58 const Scripting::StringEnum HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
60 { "BEGIN", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN },
61 { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
62 { "END", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END },
64 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
66 const Scripting::StringEnum VERTICAL_ALIGNMENT_STRING_TABLE[] =
68 { "TOP", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP },
69 { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
70 { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
72 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
77 return Toolkit::TextLabel::New();
80 // Setup properties, signals and actions using the type-registry.
81 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontStyle", STRING, FONT_STYLE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "textColor", VECTOR4, TEXT_COLOR )
92 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowOffset", VECTOR2, SHADOW_OFFSET )
93 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadowColor", VECTOR4, SHADOW_COLOR )
94 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineEnabled", BOOLEAN, UNDERLINE_ENABLED )
95 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineColor", VECTOR4, UNDERLINE_COLOR )
96 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underlineHeight", FLOAT, UNDERLINE_HEIGHT )
97 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableMarkup", BOOLEAN, ENABLE_MARKUP )
98 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableAutoScroll", BOOLEAN, ENABLE_AUTO_SCROLL )
99 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollSpeed", INTEGER, AUTO_SCROLL_SPEED )
100 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollLoopCount", INTEGER, AUTO_SCROLL_LOOP_COUNT )
101 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollGap", FLOAT, AUTO_SCROLL_GAP )
103 DALI_TYPE_REGISTRATION_END()
109 Toolkit::TextLabel TextLabel::New()
111 // Create the implementation, temporarily owned by this handle on stack
112 IntrusivePtr< TextLabel > impl = new TextLabel();
114 // Pass ownership to CustomActor handle
115 Toolkit::TextLabel handle( *impl );
117 // Second-phase init of the implementation
118 // This can only be done after the CustomActor connection has been made...
124 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
126 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
130 TextLabel& impl( GetImpl( label ) );
133 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
135 int backend = value.Get< int >();
137 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
138 if( Text::RENDERING_VECTOR_BASED == backend )
140 backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
143 if( impl.mRenderingBackend != backend )
145 impl.mRenderingBackend = backend;
146 impl.mRenderer.Reset();
148 if( impl.mController )
150 // When using the vector-based rendering, the size of the GLyphs are different
151 TextAbstraction::GlyphType glyphType = (Text::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
152 impl.mController->SetGlyphType( glyphType );
157 case Toolkit::TextLabel::Property::TEXT:
159 if( impl.mController )
161 impl.mController->SetText( value.Get< std::string >() );
165 case Toolkit::TextLabel::Property::FONT_FAMILY:
167 if( impl.mController )
169 const std::string fontFamily = value.Get< std::string >();
171 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
172 impl.mController->SetDefaultFontFamily( fontFamily );
176 case Toolkit::TextLabel::Property::FONT_STYLE:
178 SetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
181 case Toolkit::TextLabel::Property::POINT_SIZE:
183 if( impl.mController )
185 const float pointSize = value.Get< float >();
187 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
189 impl.mController->SetDefaultPointSize( pointSize );
194 case Toolkit::TextLabel::Property::MULTI_LINE:
196 if( impl.mController )
198 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
202 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
204 if( impl.mController )
206 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
207 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
208 HORIZONTAL_ALIGNMENT_STRING_TABLE,
209 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
212 impl.mController->SetHorizontalAlignment( alignment );
217 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
219 if( impl.mController )
221 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
222 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
223 VERTICAL_ALIGNMENT_STRING_TABLE,
224 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
227 impl.mController->SetVerticalAlignment( alignment );
233 case Toolkit::TextLabel::Property::TEXT_COLOR:
235 if( impl.mController )
237 const Vector4 textColor = value.Get< Vector4 >();
238 if( impl.mController->GetTextColor() != textColor )
240 impl.mController->SetTextColor( textColor );
241 impl.mRenderer.Reset();
247 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
249 if( impl.mController )
251 const Vector2 shadowOffset = value.Get< Vector2 >();
252 if ( impl.mController->GetShadowOffset() != shadowOffset )
254 impl.mController->SetShadowOffset( shadowOffset );
255 impl.mRenderer.Reset();
260 case Toolkit::TextLabel::Property::SHADOW_COLOR:
262 if( impl.mController )
264 const Vector4 shadowColor = value.Get< Vector4 >();
265 if ( impl.mController->GetShadowColor() != shadowColor )
267 impl.mController->SetShadowColor( shadowColor );
268 impl.mRenderer.Reset();
273 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
275 if( impl.mController )
277 const Vector4 color = value.Get< Vector4 >();
278 if ( impl.mController->GetUnderlineColor() != color )
280 impl.mController->SetUnderlineColor( color );
281 impl.mRenderer.Reset();
286 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
288 if( impl.mController )
290 const bool enabled = value.Get< bool >();
291 if ( impl.mController->IsUnderlineEnabled() != enabled )
293 impl.mController->SetUnderlineEnabled( enabled );
294 impl.mRenderer.Reset();
300 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
302 if( impl.mController )
304 float height = value.Get< float >();
305 if( fabsf( impl.mController->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 )
307 impl.mController->SetUnderlineHeight( height );
308 impl.mRenderer.Reset();
313 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
315 if( impl.mController )
317 const bool enableMarkup = value.Get<bool>();
318 impl.mController->SetMarkupProcessorEnabled( enableMarkup );
322 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
324 if( impl.mController )
326 const bool enableAutoScroll = value.Get<bool>();
327 // If request to auto scroll is the same as current state then do nothing.
328 if ( enableAutoScroll != impl.mController->IsAutoScrollEnabled() )
330 // If request is disable (false) and auto scrolling is enabled then need to stop it
331 if ( enableAutoScroll == false )
333 if( impl.mTextScroller )
335 impl.mTextScroller->SetLoopCount( 0 ); // Causes the current animation to finish playing (0)
338 // If request is enable (true) then start autoscroll as not already running
341 impl.mController->GetLayoutEngine().SetTextEllipsisEnabled( false );
342 impl.mController->SetAutoScrollEnabled( enableAutoScroll );
348 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
350 if( !impl.mTextScroller )
352 impl.mTextScroller = Text::TextScroller::New( impl );
354 impl.mTextScroller->SetSpeed( value.Get<int>() );
357 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
359 if( !impl.mTextScroller )
361 impl.mTextScroller = Text::TextScroller::New( impl );
363 impl.mTextScroller->SetLoopCount( value.Get<int>() );
366 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
368 if( !impl.mTextScroller )
370 impl.mTextScroller = Text::TextScroller::New( impl );
372 impl.mTextScroller->SetGap( value.Get<float>() );
379 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
381 Property::Value value;
383 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
387 TextLabel& impl( GetImpl( label ) );
390 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
392 value = impl.mRenderingBackend;
395 case Toolkit::TextLabel::Property::TEXT:
397 if( impl.mController )
400 impl.mController->GetText( text );
405 case Toolkit::TextLabel::Property::FONT_FAMILY:
407 if( impl.mController )
409 value = impl.mController->GetDefaultFontFamily();
413 case Toolkit::TextLabel::Property::FONT_STYLE:
415 GetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
418 case Toolkit::TextLabel::Property::POINT_SIZE:
420 if( impl.mController )
422 value = impl.mController->GetDefaultPointSize();
426 case Toolkit::TextLabel::Property::MULTI_LINE:
428 if( impl.mController )
430 value = impl.mController->IsMultiLineEnabled();
434 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
436 if( impl.mController )
438 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
439 HORIZONTAL_ALIGNMENT_STRING_TABLE,
440 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
443 value = std::string( name );
448 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
450 if( impl.mController )
452 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
453 VERTICAL_ALIGNMENT_STRING_TABLE,
454 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
457 value = std::string( name );
462 case Toolkit::TextLabel::Property::TEXT_COLOR:
464 if ( impl.mController )
466 value = impl.mController->GetTextColor();
470 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
472 if ( impl.mController )
474 value = impl.mController->GetShadowOffset();
478 case Toolkit::TextLabel::Property::SHADOW_COLOR:
480 if ( impl.mController )
482 value = impl.mController->GetShadowColor();
486 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
488 if ( impl.mController )
490 value = impl.mController->GetUnderlineColor();
494 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
496 if ( impl.mController )
498 value = impl.mController->IsUnderlineEnabled();
502 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
504 if ( impl.mController )
506 value = impl.mController->GetUnderlineHeight();
510 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
512 if( impl.mController )
514 value = impl.mController->IsMarkupProcessorEnabled();
518 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
520 if( impl.mController )
522 value = impl.mController->IsAutoScrollEnabled();
526 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
528 TextLabel& impl( GetImpl( label ) );
529 if ( impl.mTextScroller )
531 value = impl.mTextScroller->GetSpeed();
535 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
537 if( impl.mController )
539 TextLabel& impl( GetImpl( label ) );
540 if ( impl.mTextScroller )
542 value = impl.mTextScroller->GetLoopCount();
547 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
549 TextLabel& impl( GetImpl( label ) );
550 if ( impl.mTextScroller )
552 value = impl.mTextScroller->GetGap();
562 void TextLabel::OnInitialize()
566 mController = Text::Controller::New( *this );
568 // When using the vector-based rendering, the size of the GLyphs are different
569 TextAbstraction::GlyphType glyphType = (Text::RENDERING_VECTOR_BASED == mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
570 mController->SetGlyphType( glyphType );
572 // Use height-for-width negotiation by default
573 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
574 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
576 // Enable the text ellipsis.
577 LayoutEngine& engine = mController->GetLayoutEngine();
579 engine.SetTextEllipsisEnabled( true ); // If false then text larger than control will overflow
580 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
582 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
585 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
587 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
591 case StyleChange::DEFAULT_FONT_CHANGE:
593 // Property system did not set the font so should update it.
594 const std::string& newFont = GetImpl( styleManager ).GetDefaultFontFamily();
595 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
596 mController->UpdateAfterFontChange( newFont );
600 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
602 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
604 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
606 // Property system did not set the PointSize so should update it.
607 // todo instruct text-controller to update model
611 case StyleChange::THEME_CHANGE:
613 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
619 Vector3 TextLabel::GetNaturalSize()
621 return mController->GetNaturalSize();
624 float TextLabel::GetHeightForWidth( float width )
626 return mController->GetHeightForWidth( width );
629 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
631 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnRelayout\n" );
633 if( mController->Relayout( size ) ||
638 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
644 void TextLabel::RequestTextRelayout()
649 void TextLabel::RenderText()
651 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::RenderText IsAutoScrollEnabled[%s] [%p]\n", ( mController->IsAutoScrollEnabled())?"true":"false", this );
654 Actor renderableActor;
658 renderableActor = mRenderer->Render( mController->GetView(), DepthIndex::TEXT );
661 if( renderableActor != mRenderableActor )
663 UnparentAndReset( mRenderableActor );
665 if( renderableActor )
667 // TODO: Scroll and alignment needs to be refactored.
668 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
669 renderableActor.SetPosition( 0.f, alignmentOffset.y );
671 self.Add( renderableActor );
673 mRenderableActor = renderableActor;
675 if ( mController->IsAutoScrollEnabled() )
677 SetUpAutoScrolling();
682 void TextLabel::SetUpAutoScrolling()
684 const Size& controlSize = mController->GetView().GetControlSize();
685 const Size offScreenSize = GetNaturalSize().GetVectorXY(); // As relayout of text may not be done at this point natural size is used to get size. Single line scrolling only.
686 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
687 const Text::CharacterDirection direction = mController->GetAutoScrollDirection();
689 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling alignmentOffset[%f,%f] offScreenSize[%f,%f] controlSize[%f,%f]\n",
690 alignmentOffset.x, alignmentOffset.y, offScreenSize.x,offScreenSize.y , controlSize.x,controlSize.y);
692 if ( !mTextScroller )
694 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
696 // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
697 mTextScroller = Text::TextScroller::New( *this );
699 mTextScroller->SetParameters( mRenderableActor, controlSize, offScreenSize, direction, alignmentOffset );
702 self.Add( mTextScroller->GetScrollingText() );
703 self.Add( mTextScroller->GetSourceCamera() );
706 void TextLabel::OnStageConnect( Dali::Actor actor )
708 if ( mHasBeenStaged )
714 mHasBeenStaged = true;
718 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
720 // TextLabel does not show decorations
723 void TextLabel::OnStageConnection( int depth )
725 // Call the Control::OnStageConnection() to set the depth of the background.
726 Control::OnStageConnection( depth );
728 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
731 void TextLabel::TextChanged()
733 // TextLabel does not provide a signal for this
736 void TextLabel::MaxLengthReached()
738 // Pure Virtual from TextController Interface, only needed when inputting text
741 void TextLabel::ScrollingFinished()
743 // Pure Virtual from TextScroller Interface
744 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
745 mController->SetAutoScrollEnabled( false );
746 mController->GetLayoutEngine().SetTextEllipsisEnabled( true );
747 RequestTextRelayout();
750 TextLabel::TextLabel()
751 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
752 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
753 mHasBeenStaged( false )
757 TextLabel::~TextLabel()
761 } // namespace Internal
763 } // namespace Toolkit