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 int backend = value.Get< int >();
130 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
131 if( Text::RENDERING_VECTOR_BASED == backend )
133 backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
136 if( impl.mRenderingBackend != backend )
138 impl.mRenderingBackend = backend;
139 impl.mRenderer.Reset();
141 if( impl.mController )
143 // When using the vector-based rendering, the size of the GLyphs are different
144 TextAbstraction::GlyphType glyphType = (Text::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
145 impl.mController->SetGlyphType( glyphType );
150 case Toolkit::TextLabel::Property::TEXT:
152 if( impl.mController )
154 impl.mController->SetText( value.Get< std::string >() );
158 case Toolkit::TextLabel::Property::FONT_FAMILY:
160 if( impl.mController )
162 const std::string fontFamily = value.Get< std::string >();
164 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
165 impl.mController->SetDefaultFontFamily( fontFamily );
169 case Toolkit::TextLabel::Property::FONT_STYLE:
171 SetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
174 case Toolkit::TextLabel::Property::POINT_SIZE:
176 if( impl.mController )
178 const float pointSize = value.Get< float >();
180 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
182 impl.mController->SetDefaultPointSize( pointSize );
187 case Toolkit::TextLabel::Property::MULTI_LINE:
189 if( impl.mController )
191 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
195 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
197 if( impl.mController )
199 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
200 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
201 HORIZONTAL_ALIGNMENT_STRING_TABLE,
202 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
205 impl.mController->SetHorizontalAlignment( alignment );
210 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
212 if( impl.mController )
214 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
215 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
216 VERTICAL_ALIGNMENT_STRING_TABLE,
217 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
220 impl.mController->SetVerticalAlignment( alignment );
226 case Toolkit::TextLabel::Property::TEXT_COLOR:
228 if( impl.mController )
230 const Vector4 textColor = value.Get< Vector4 >();
231 if( impl.mController->GetTextColor() != textColor )
233 impl.mController->SetTextColor( textColor );
234 impl.mRenderer.Reset();
240 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
242 if( impl.mController )
244 const Vector2 shadowOffset = value.Get< Vector2 >();
245 if ( impl.mController->GetShadowOffset() != shadowOffset )
247 impl.mController->SetShadowOffset( shadowOffset );
248 impl.mRenderer.Reset();
253 case Toolkit::TextLabel::Property::SHADOW_COLOR:
255 if( impl.mController )
257 const Vector4 shadowColor = value.Get< Vector4 >();
258 if ( impl.mController->GetShadowColor() != shadowColor )
260 impl.mController->SetShadowColor( shadowColor );
261 impl.mRenderer.Reset();
266 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
268 if( impl.mController )
270 const Vector4 color = value.Get< Vector4 >();
271 if ( impl.mController->GetUnderlineColor() != color )
273 impl.mController->SetUnderlineColor( color );
274 impl.mRenderer.Reset();
279 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
281 if( impl.mController )
283 const bool enabled = value.Get< bool >();
284 if ( impl.mController->IsUnderlineEnabled() != enabled )
286 impl.mController->SetUnderlineEnabled( enabled );
287 impl.mRenderer.Reset();
293 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
295 if( impl.mController )
297 float height = value.Get< float >();
298 if( fabsf( impl.mController->GetUnderlineHeight() - height ) > Math::MACHINE_EPSILON_1000 )
300 impl.mController->SetUnderlineHeight( height );
301 impl.mRenderer.Reset();
306 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
308 if( impl.mController )
310 const bool enableMarkup = value.Get<bool>();
311 impl.mController->SetMarkupProcessorEnabled( enableMarkup );
319 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
321 Property::Value value;
323 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
327 TextLabel& impl( GetImpl( label ) );
330 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
332 value = impl.mRenderingBackend;
335 case Toolkit::TextLabel::Property::TEXT:
337 if( impl.mController )
340 impl.mController->GetText( text );
345 case Toolkit::TextLabel::Property::FONT_FAMILY:
347 if( impl.mController )
349 value = impl.mController->GetDefaultFontFamily();
353 case Toolkit::TextLabel::Property::FONT_STYLE:
355 GetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
358 case Toolkit::TextLabel::Property::POINT_SIZE:
360 if( impl.mController )
362 value = impl.mController->GetDefaultPointSize();
366 case Toolkit::TextLabel::Property::MULTI_LINE:
368 if( impl.mController )
370 value = impl.mController->IsMultiLineEnabled();
374 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
376 if( impl.mController )
378 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
379 HORIZONTAL_ALIGNMENT_STRING_TABLE,
380 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
383 value = std::string( name );
388 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
390 if( impl.mController )
392 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
393 VERTICAL_ALIGNMENT_STRING_TABLE,
394 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
397 value = std::string( name );
402 case Toolkit::TextLabel::Property::TEXT_COLOR:
404 if ( impl.mController )
406 value = impl.mController->GetTextColor();
410 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
412 if ( impl.mController )
414 value = impl.mController->GetShadowOffset();
418 case Toolkit::TextLabel::Property::SHADOW_COLOR:
420 if ( impl.mController )
422 value = impl.mController->GetShadowColor();
426 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
428 if ( impl.mController )
430 value = impl.mController->GetUnderlineColor();
434 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
436 if ( impl.mController )
438 value = impl.mController->IsUnderlineEnabled();
442 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
444 if ( impl.mController )
446 value = impl.mController->GetUnderlineHeight();
450 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
452 if( impl.mController )
454 value = impl.mController->IsMarkupProcessorEnabled();
464 void TextLabel::OnInitialize()
468 mController = Text::Controller::New( *this );
470 // When using the vector-based rendering, the size of the GLyphs are different
471 TextAbstraction::GlyphType glyphType = (Text::RENDERING_VECTOR_BASED == mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
472 mController->SetGlyphType( glyphType );
474 // Use height-for-width negotiation by default
475 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
476 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
478 // Enable the text ellipsis.
479 LayoutEngine& engine = mController->GetLayoutEngine();
481 engine.SetTextEllipsisEnabled( true );
482 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
484 self.OnStageSignal().Connect( this, &TextLabel::OnStageConnect );
487 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
489 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
493 case StyleChange::DEFAULT_FONT_CHANGE:
495 // Property system did not set the font so should update it.
496 const std::string& newFont = GetImpl( styleManager ).GetDefaultFontFamily();
497 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
498 mController->UpdateAfterFontChange( newFont );
502 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
504 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_SIZE_CHANGE (%f)\n", mController->GetDefaultPointSize() );
506 if ( (mController->GetDefaultPointSize() <= 0.0f) ) // If DefaultPointSize not set by Property system it will be 0.0f
508 // Property system did not set the PointSize so should update it.
509 // todo instruct text-controller to update model
513 case StyleChange::THEME_CHANGE:
515 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
521 Vector3 TextLabel::GetNaturalSize()
523 return mController->GetNaturalSize();
526 float TextLabel::GetHeightForWidth( float width )
528 return mController->GetHeightForWidth( width );
531 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
533 if( mController->Relayout( size ) ||
538 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
544 void TextLabel::RequestTextRelayout()
549 void TextLabel::RenderText()
552 Actor renderableActor;
555 renderableActor = mRenderer->Render( mController->GetView(), DepthIndex::TEXT );
558 if( renderableActor != mRenderableActor )
560 UnparentAndReset( mRenderableActor );
562 if( renderableActor )
564 // TODO: Scroll and alignment needs to be refactored.
565 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
566 renderableActor.SetPosition( 0.f, alignmentOffset.y );
568 self.Add( renderableActor );
570 mRenderableActor = renderableActor;
574 void TextLabel::OnStageConnect( Dali::Actor actor )
576 if ( mHasBeenStaged )
582 mHasBeenStaged = true;
586 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
588 // TextLabel does not show decorations
591 void TextLabel::OnStageConnection( int depth )
593 // Call the Control::OnStageConnection() to set the depth of the background.
594 Control::OnStageConnection( depth );
596 // The depth of the text renderer is set in the RenderText() called from OnRelayout().
599 void TextLabel::TextChanged()
601 // TextLabel does not provide a signal for this
604 void TextLabel::MaxLengthReached()
606 // Pure Virtual from TextController Interface, only needed when inputting text
609 TextLabel::TextLabel()
610 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
611 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
612 mHasBeenStaged( false )
616 TextLabel::~TextLabel()
620 } // namespace Internal
622 } // namespace Toolkit