2 * Copyright (c) 2020 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/common/dali-common.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/devel-api/common/stage.h>
25 #include <dali/devel-api/object/property-helper-devel.h>
26 #include <dali/devel-api/adaptor-framework/image-loading.h>
27 #include <dali/devel-api/adaptor-framework/window-devel.h>
28 #include <dali/integration-api/debug.h>
31 #include <dali-toolkit/public-api/text/text-enumerations.h>
32 #include <dali-toolkit/devel-api/text/rendering-backend.h>
33 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
34 #include <dali-toolkit/internal/text/property-string-parser.h>
35 #include <dali-toolkit/internal/text/rendering/text-backend.h>
36 #include <dali-toolkit/internal/text/text-effects-style.h>
37 #include <dali-toolkit/internal/text/text-font-style.h>
38 #include <dali-toolkit/internal/text/text-view.h>
39 #include <dali-toolkit/internal/text/text-definitions.h>
40 #include <dali-toolkit/internal/styling/style-manager-impl.h>
42 #include <dali-toolkit/public-api/align-enumerations.h>
43 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
44 #include <dali-toolkit/devel-api/controls/control-devel.h>
45 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
46 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
47 #include <dali-toolkit/public-api/visuals/visual-properties.h>
48 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
50 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
52 using namespace Dali::Toolkit::Text;
65 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
68 * @brief How the text visual should be aligned vertically inside the control.
70 * 0.0f aligns the text to the top, 0.5f aligns the text to the center, 1.0f aligns the text to the bottom.
71 * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
73 const float VERTICAL_ALIGNMENT_TABLE[ Text::VerticalAlignment::BOTTOM + 1 ] =
75 0.0f, // VerticalAlignment::TOP
76 0.5f, // VerticalAlignment::CENTER
77 1.0f // VerticalAlignment::BOTTOM
80 const std::string TEXT_FIT_ENABLE_KEY( "enable" );
81 const std::string TEXT_FIT_MIN_SIZE_KEY( "minSize" );
82 const std::string TEXT_FIT_MAX_SIZE_KEY( "maxSize" );
83 const std::string TEXT_FIT_STEP_SIZE_KEY( "stepSize" );
84 const std::string TEXT_FIT_FONT_SIZE_TYPE_KEY( "fontSizeType" );
86 #if defined ( DEBUG_ENABLED )
87 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
90 const Scripting::StringEnum AUTO_SCROLL_STOP_MODE_TABLE[] =
92 { "IMMEDIATE", Toolkit::TextLabel::AutoScrollStopMode::IMMEDIATE },
93 { "FINISH_LOOP", Toolkit::TextLabel::AutoScrollStopMode::FINISH_LOOP },
95 const unsigned int AUTO_SCROLL_STOP_MODE_TABLE_COUNT = sizeof( AUTO_SCROLL_STOP_MODE_TABLE ) / sizeof( AUTO_SCROLL_STOP_MODE_TABLE[0] );
100 return Toolkit::TextLabel::New();
103 // Setup properties, signals and actions using the type-registry.
104 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
106 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
107 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
108 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontStyle", MAP, FONT_STYLE )
109 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
110 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
111 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
112 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
113 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableMarkup", BOOLEAN, ENABLE_MARKUP )
114 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "enableAutoScroll", BOOLEAN, ENABLE_AUTO_SCROLL )
115 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollSpeed", INTEGER, AUTO_SCROLL_SPEED )
116 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollLoopCount", INTEGER, AUTO_SCROLL_LOOP_COUNT )
117 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollGap", FLOAT, AUTO_SCROLL_GAP )
118 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "lineSpacing", FLOAT, LINE_SPACING )
119 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline", MAP, UNDERLINE )
120 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow", MAP, SHADOW )
121 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "emboss", MAP, EMBOSS )
122 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "outline", MAP, OUTLINE )
123 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "pixelSize", FLOAT, PIXEL_SIZE )
124 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "ellipsis", BOOLEAN, ELLIPSIS )
125 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollLoopDelay", FLOAT, AUTO_SCROLL_LOOP_DELAY )
126 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "autoScrollStopMode", STRING, AUTO_SCROLL_STOP_MODE )
127 DALI_PROPERTY_REGISTRATION_READ_ONLY( Toolkit, TextLabel, "lineCount", INTEGER, LINE_COUNT )
128 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "lineWrapMode", INTEGER, LINE_WRAP_MODE )
129 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY( Toolkit, TextLabel, "textDirection", INTEGER, TEXT_DIRECTION )
130 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "verticalLineAlignment", INTEGER, VERTICAL_LINE_ALIGNMENT )
131 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "textBackground", MAP, BACKGROUND )
132 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "ignoreSpacesAfterText", BOOLEAN, IGNORE_SPACES_AFTER_TEXT )
133 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "matchSystemLanguageDirection", BOOLEAN, MATCH_SYSTEM_LANGUAGE_DIRECTION )
134 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "textFit", MAP, TEXT_FIT )
135 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "minLineSize", FLOAT, MIN_LINE_SIZE )
136 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
137 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, TextLabel, "fontSizeScale", FLOAT, FONT_SIZE_SCALE )
138 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, TextLabel, "textColor", Color::BLACK, TEXT_COLOR )
139 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, TextLabel, "textColorRed", TEXT_COLOR_RED, TEXT_COLOR, 0 )
140 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, TextLabel, "textColorGreen", TEXT_COLOR_GREEN, TEXT_COLOR, 1 )
141 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, TextLabel, "textColorBlue", TEXT_COLOR_BLUE, TEXT_COLOR, 2 )
142 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit, TextLabel, "textColorAlpha", TEXT_COLOR_ALPHA, TEXT_COLOR, 3 )
143 DALI_TYPE_REGISTRATION_END()
145 /// Parses the property map for the TEXT_FIT property
146 void ParseTextFitProperty(Text::ControllerPtr& controller, const Property::Map* propertiesMap)
148 if ( propertiesMap && !propertiesMap->Empty() )
150 bool enabled = false;
153 float stepSize = 0.f;
154 bool isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
155 Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
157 const unsigned int numberOfItems = propertiesMap->Count();
159 // Parses and applies
160 for( unsigned int index = 0u; index < numberOfItems; ++index )
162 const KeyValuePair& valueGet = propertiesMap->GetKeyValue( index );
164 if( ( Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey ) || ( TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey ) )
167 enabled = valueGet.second.Get< bool >();
169 else if( ( Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey ) )
172 minSize = valueGet.second.Get< float >();
175 else if( ( Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey ) )
178 maxSize = valueGet.second.Get< float >();
181 else if( ( Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey ) )
184 stepSize = valueGet.second.Get< float >();
185 isStepSizeSet = true;
187 else if( ( Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey ) || ( TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey ) )
189 if( "pixelSize" == valueGet.second.Get< std::string >() )
191 type = Controller::FontSizeType::PIXEL_SIZE;
196 controller->SetTextFitEnabled( enabled );
199 controller->SetTextFitMinSize( minSize, type );
203 controller->SetTextFitMaxSize( maxSize, type );
207 controller->SetTextFitStepSize( stepSize, type );
214 Toolkit::TextLabel TextLabel::New()
216 // Create the implementation, temporarily owned by this handle on stack
217 IntrusivePtr< TextLabel > impl = new TextLabel();
219 // Pass ownership to CustomActor handle
220 Toolkit::TextLabel handle( *impl );
222 // Second-phase init of the implementation
223 // This can only be done after the CustomActor connection has been made...
229 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
231 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
235 TextLabel& impl( GetImpl( label ) );
236 DALI_ASSERT_ALWAYS( impl.mController && "No text contoller" );
240 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
242 int backend = value.Get< int >();
244 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
245 if( DevelText::RENDERING_VECTOR_BASED == backend )
247 backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
250 if( impl.mRenderingBackend != backend )
252 impl.mRenderingBackend = backend;
253 impl.mTextUpdateNeeded = true;
255 // When using the vector-based rendering, the size of the GLyphs are different
256 TextAbstraction::GlyphType glyphType = (DevelText::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
257 impl.mController->SetGlyphType( glyphType );
261 case Toolkit::TextLabel::Property::TEXT:
263 impl.mController->SetText( value.Get< std::string >() );
266 case Toolkit::TextLabel::Property::FONT_FAMILY:
268 const std::string& fontFamily = value.Get< std::string >();
270 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
271 impl.mController->SetDefaultFontFamily( fontFamily );
274 case Toolkit::TextLabel::Property::FONT_STYLE:
276 SetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
279 case Toolkit::TextLabel::Property::POINT_SIZE:
281 const float pointSize = value.Get< float >();
283 if( !Equals( impl.mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
285 impl.mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
289 case Toolkit::TextLabel::Property::MULTI_LINE:
291 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
294 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
296 Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
297 if( Text::GetHorizontalAlignmentEnumeration( value, alignment ) )
299 impl.mController->SetHorizontalAlignment( alignment );
303 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
305 Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
306 if( Text::GetVerticalAlignmentEnumeration( value, alignment ) )
308 impl.mController->SetVerticalAlignment( alignment );
312 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
314 const bool enableMarkup = value.Get<bool>();
315 impl.mController->SetMarkupProcessorEnabled( enableMarkup );
318 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
320 const bool enableAutoScroll = value.Get<bool>();
321 // If request to auto scroll is the same as current state then do nothing.
322 if ( enableAutoScroll != impl.mController->IsAutoScrollEnabled() )
324 // If request is disable (false) and auto scrolling is enabled then need to stop it
325 if ( enableAutoScroll == false )
327 if( impl.mTextScroller )
329 impl.mTextScroller->StopScrolling();
332 // If request is enable (true) then start autoscroll as not already running
335 impl.mController->SetAutoScrollEnabled( enableAutoScroll );
340 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
342 Text::TextScrollerPtr textScroller = impl.GetTextScroller();
343 Toolkit::TextLabel::AutoScrollStopMode::Type stopMode = textScroller->GetStopMode();
344 if( Scripting::GetEnumerationProperty< Toolkit::TextLabel::AutoScrollStopMode::Type >( value,
345 AUTO_SCROLL_STOP_MODE_TABLE,
346 AUTO_SCROLL_STOP_MODE_TABLE_COUNT,
349 textScroller->SetStopMode( stopMode );
353 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
355 impl.GetTextScroller()->SetSpeed( value.Get<int>() );
358 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
360 impl.GetTextScroller()->SetLoopCount( value.Get<int>() );
363 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
365 impl.GetTextScroller()->SetLoopDelay( value.Get<float>() );
368 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
370 impl.GetTextScroller()->SetGap( value.Get<float>() );
373 case Toolkit::TextLabel::Property::LINE_SPACING:
375 const float lineSpacing = value.Get<float>();
376 impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSpacing( lineSpacing ) || impl.mTextUpdateNeeded;
379 case Toolkit::TextLabel::Property::UNDERLINE:
381 impl.mTextUpdateNeeded = SetUnderlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT ) || impl.mTextUpdateNeeded;
384 case Toolkit::TextLabel::Property::SHADOW:
386 impl.mTextUpdateNeeded = SetShadowProperties( impl.mController, value, Text::EffectStyle::DEFAULT ) || impl.mTextUpdateNeeded;
389 case Toolkit::TextLabel::Property::EMBOSS:
391 impl.mTextUpdateNeeded = SetEmbossProperties( impl.mController, value, Text::EffectStyle::DEFAULT ) || impl.mTextUpdateNeeded;
394 case Toolkit::TextLabel::Property::OUTLINE:
396 impl.mTextUpdateNeeded = SetOutlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT ) || impl.mTextUpdateNeeded;
399 case Toolkit::TextLabel::Property::PIXEL_SIZE:
401 const float pixelSize = value.Get< float >();
402 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize );
404 if( !Equals( impl.mController->GetDefaultFontSize( Text::Controller::PIXEL_SIZE ), pixelSize ) )
406 impl.mController->SetDefaultFontSize( pixelSize, Text::Controller::PIXEL_SIZE );
410 case Toolkit::TextLabel::Property::ELLIPSIS:
412 const bool ellipsis = value.Get<bool>();
413 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis );
415 impl.mController->SetTextElideEnabled( ellipsis );
418 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
420 Text::LineWrap::Mode lineWrapMode( static_cast< Text::LineWrap::Mode >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
421 if( GetLineWrapModeEnumeration( value, lineWrapMode ) )
423 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode );
424 impl.mController->SetLineWrapMode( lineWrapMode );
428 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
430 if( impl.mController->GetTextModel() )
432 DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>( value.Get<int>() );
434 impl.mController->SetVerticalLineAlignment( alignment );
436 // Property doesn't affect the layout, only Visual must be updated
437 TextVisual::EnableRendererUpdate( impl.mVisual );
439 // No need to trigger full re-layout. Instead call UpdateRenderer() directly
440 TextVisual::UpdateRenderer( impl.mVisual );
444 case Toolkit::DevelTextLabel::Property::BACKGROUND:
446 impl.mTextUpdateNeeded = SetBackgroundProperties( impl.mController, value, Text::EffectStyle::DEFAULT ) || impl.mTextUpdateNeeded;
449 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
451 impl.mController->SetIgnoreSpacesAfterText(value.Get< bool >());
454 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
456 impl.mController->SetMatchSystemLanguageDirection(value.Get< bool >());
459 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
461 ParseTextFitProperty(impl.mController, value.GetMap());
464 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
466 const float lineSize = value.Get<float>();
467 impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSize( lineSize ) || impl.mTextUpdateNeeded;
470 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
472 const float scale = value.Get< float >();
473 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p FONT_SIZE_SCALE %f\n", impl.mController.Get(), scale );
475 if( !Equals( impl.mController->GetFontSizeScale(), scale ) )
477 impl.mController->SetFontSizeScale( scale );
483 // Request relayout when text update is needed. It's necessary to call it
484 // as changing the property not via UI interaction brings no effect if only
485 // the mTextUpdateNeeded is changed.
486 if( impl.mTextUpdateNeeded )
488 // need to request relayout as size of text may have changed
489 impl.RequestTextRelayout();
494 Text::ControllerPtr TextLabel::getController() { return mController; }
496 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
498 Property::Value value;
500 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
504 TextLabel& impl( GetImpl( label ) );
505 DALI_ASSERT_DEBUG( impl.mController && "No text contoller" );
509 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
511 value = impl.mRenderingBackend;
514 case Toolkit::TextLabel::Property::TEXT:
517 impl.mController->GetText( text );
521 case Toolkit::TextLabel::Property::FONT_FAMILY:
523 value = impl.mController->GetDefaultFontFamily();
526 case Toolkit::TextLabel::Property::FONT_STYLE:
528 GetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
531 case Toolkit::TextLabel::Property::POINT_SIZE:
533 value = impl.mController->GetDefaultFontSize( Text::Controller::POINT_SIZE );
536 case Toolkit::TextLabel::Property::MULTI_LINE:
538 value = impl.mController->IsMultiLineEnabled();
541 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
543 const char* name = Text::GetHorizontalAlignmentString( impl.mController->GetHorizontalAlignment() );
547 value = std::string( name );
551 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
553 const char* name = Text::GetVerticalAlignmentString( impl.mController->GetVerticalAlignment() );
556 value = std::string( name );
560 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
562 value = impl.mController->IsMarkupProcessorEnabled();
565 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
567 value = impl.mController->IsAutoScrollEnabled();
570 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
572 if( impl.mTextScroller )
574 const char* mode = Scripting::GetEnumerationName< Toolkit::TextLabel::AutoScrollStopMode::Type >( impl.mTextScroller->GetStopMode(),
575 AUTO_SCROLL_STOP_MODE_TABLE,
576 AUTO_SCROLL_STOP_MODE_TABLE_COUNT );
579 value = std::string( mode );
584 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
586 if ( impl.mTextScroller )
588 value = impl.mTextScroller->GetSpeed();
592 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
594 if ( impl.mTextScroller )
596 value = impl.mTextScroller->GetLoopCount();
600 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
602 if ( impl.mTextScroller )
604 value = impl.mTextScroller->GetLoopDelay();
608 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
610 if ( impl.mTextScroller )
612 value = impl.mTextScroller->GetGap();
616 case Toolkit::TextLabel::Property::LINE_SPACING:
618 value = impl.mController->GetDefaultLineSpacing();
621 case Toolkit::TextLabel::Property::UNDERLINE:
623 GetUnderlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
626 case Toolkit::TextLabel::Property::SHADOW:
628 GetShadowProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
631 case Toolkit::TextLabel::Property::EMBOSS:
633 GetEmbossProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
636 case Toolkit::TextLabel::Property::OUTLINE:
638 GetOutlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
641 case Toolkit::TextLabel::Property::PIXEL_SIZE:
643 value = impl.mController->GetDefaultFontSize( Text::Controller::PIXEL_SIZE );
646 case Toolkit::TextLabel::Property::ELLIPSIS:
648 value = impl.mController->IsTextElideEnabled();
651 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
653 value = impl.mController->GetLineWrapMode();
656 case Toolkit::TextLabel::Property::LINE_COUNT:
658 float width = label.GetProperty( Actor::Property::SIZE_WIDTH ).Get<float>();
659 value = impl.mController->GetLineCount( width );
662 case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
664 value = impl.mController->GetTextDirection();
667 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
669 value = impl.mController->GetVerticalLineAlignment();
672 case Toolkit::DevelTextLabel::Property::BACKGROUND:
674 GetBackgroundProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
677 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
679 value = impl.mController->IsIgnoreSpacesAfterText();
682 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
684 value = impl.mController->IsMatchSystemLanguageDirection();
687 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
689 const bool enabled = impl.mController->IsTextFitEnabled();
690 const float minSize = impl.mController->GetTextFitMinSize();
691 const float maxSize = impl.mController->GetTextFitMaxSize();
692 const float stepSize = impl.mController->GetTextFitStepSize();
695 map.Insert( TEXT_FIT_ENABLE_KEY, enabled );
696 map.Insert( TEXT_FIT_MIN_SIZE_KEY, minSize );
697 map.Insert( TEXT_FIT_MAX_SIZE_KEY, maxSize );
698 map.Insert( TEXT_FIT_STEP_SIZE_KEY, stepSize );
699 map.Insert( TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize" );
704 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
706 value = impl.mController->GetDefaultLineSize();
709 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
711 value = impl.mController->GetFontSizeScale();
720 void TextLabel::OnInitialize()
724 Property::Map propertyMap;
725 propertyMap.Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
727 mVisual = Toolkit::VisualFactory::Get().CreateVisual( propertyMap );
728 DevelControl::RegisterVisual( *this, Toolkit::TextLabel::Property::TEXT, mVisual );
730 TextVisual::SetAnimatableTextColorProperty( mVisual, Toolkit::TextLabel::Property::TEXT_COLOR );
732 mController = TextVisual::GetController(mVisual);
733 DALI_ASSERT_DEBUG( mController && "Invalid Text Controller")
735 mController->SetControlInterface(this);
737 // Use height-for-width negotiation by default
738 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
739 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
741 // Enable highlightability
742 self.SetProperty( Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true );
744 // Enable the text ellipsis.
745 mController->SetTextElideEnabled( true ); // If false then text larger than control will overflow
747 // Sets layoutDirection value
748 Dali::Stage stage = Dali::Stage::GetCurrent();
749 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( stage.GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
750 mController->SetLayoutDirection( layoutDirection );
752 Layout::Engine& engine = mController->GetLayoutEngine();
753 engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
755 DevelControl::SetAccessibilityConstructor( self, []( Dali::Actor actor ) {
756 return std::unique_ptr< Dali::Accessibility::Accessible >(
757 new AccessibleImpl( actor, Dali::Accessibility::Role::LABEL ) );
761 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
763 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
767 case StyleChange::DEFAULT_FONT_CHANGE:
769 // Property system did not set the font so should update it.
770 const std::string& newFont = GetImpl( styleManager ).GetDefaultFontFamily();
771 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
772 mController->UpdateAfterFontChange( newFont );
776 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
778 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
782 case StyleChange::THEME_CHANGE:
784 // Nothing to do, let control base class handle this
789 // Up call to Control
790 Control::OnStyleChange( styleManager, change );
793 Vector3 TextLabel::GetNaturalSize()
796 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
798 Vector3 naturalSize = mController->GetNaturalSize();
799 naturalSize.width += ( padding.start + padding.end );
800 naturalSize.height += ( padding.top + padding.bottom );
805 float TextLabel::GetHeightForWidth( float width )
808 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
810 return mController->GetHeightForWidth( width ) + padding.top + padding.bottom;
813 void TextLabel::OnPropertySet( Property::Index index, const Property::Value& propertyValue )
815 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index );
819 case Toolkit::TextLabel::Property::TEXT_COLOR:
821 const Vector4& textColor = propertyValue.Get< Vector4 >();
822 if( mController->GetDefaultColor() != textColor )
824 mController->SetDefaultColor( textColor );
825 mTextUpdateNeeded = true;
831 Control::OnPropertySet( index, propertyValue ); // up call to control for non-handled properties
837 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
839 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnRelayout\n" );
842 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
844 Vector2 contentSize( size.x - ( padding.start + padding.end ), size.y - ( padding.top + padding.bottom ) );
846 if( mController->IsTextFitEnabled() )
848 mController->FitPointSizeforLayout( contentSize );
849 mController->SetTextFitContentSize( contentSize );
852 // Support Right-To-Left
853 Dali::LayoutDirection::Type layoutDirection;
854 if( mController->IsMatchSystemLanguageDirection() )
856 layoutDirection = static_cast<Dali::LayoutDirection::Type>( DevelWindow::Get( Self() ).GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
860 layoutDirection = static_cast<Dali::LayoutDirection::Type>( Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
862 const Text::Controller::UpdateTextType updateTextType = mController->Relayout( contentSize, layoutDirection );
864 if( ( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
865 || mTextUpdateNeeded )
867 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", ( mController->IsAutoScrollEnabled())?"true":"false", this );
870 TextVisual::EnableRendererUpdate( mVisual );
872 // Support Right-To-Left of padding
873 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
875 std::swap( padding.start, padding.end );
878 // Calculate the size of the visual that can fit the text
879 Size layoutSize = mController->GetTextModel()->GetLayoutSize();
880 layoutSize.x = contentSize.x;
882 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
883 if ( shadowOffset.y > Math::MACHINE_EPSILON_1 )
885 layoutSize.y += shadowOffset.y;
888 float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
889 layoutSize.y += outlineWidth * 2.0f;
890 layoutSize.y = std::min( layoutSize.y, contentSize.y );
892 // Calculate the offset for vertical alignment only, as the layout engine will do the horizontal alignment.
893 Vector2 alignmentOffset;
894 alignmentOffset.x = 0.0f;
895 alignmentOffset.y = ( contentSize.y - layoutSize.y ) * VERTICAL_ALIGNMENT_TABLE[mController->GetVerticalAlignment()];
897 Property::Map visualTransform;
898 visualTransform.Add( Toolkit::Visual::Transform::Property::SIZE, layoutSize )
899 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
900 .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( padding.start, padding.top ) + alignmentOffset )
901 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
902 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
903 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
904 mVisual.SetTransformAndSize( visualTransform, size );
906 if ( mController->IsAutoScrollEnabled() )
908 SetUpAutoScrolling();
911 mTextUpdateNeeded = false;
915 void TextLabel::RequestTextRelayout()
918 // Signal that a Relayout may be needed
921 void TextLabel::SetUpAutoScrolling()
923 const Size& controlSize = mController->GetView().GetControlSize();
924 const Size textNaturalSize = GetNaturalSize().GetVectorXY(); // As relayout of text may not be done at this point natural size is used to get size. Single line scrolling only.
925 const Text::CharacterDirection direction = mController->GetAutoScrollDirection();
927 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n",
928 textNaturalSize.x,textNaturalSize.y , controlSize.x,controlSize.y );
930 if ( !mTextScroller )
932 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n" );
934 // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
935 mTextScroller = Text::TextScroller::New( *this );
938 // Calculate the actual gap before scrolling wraps.
939 int textPadding = std::max( controlSize.x - textNaturalSize.x, 0.0f );
940 float wrapGap = std::max( mTextScroller->GetGap(), textPadding );
941 Vector2 textureSize = textNaturalSize + Vector2(wrapGap, 0.0f); // Add the gap as a part of the texture
943 // Create a texture of the text for scrolling
944 Size verifiedSize = textureSize;
945 const int maxTextureSize = Dali::GetMaxTextureSize();
947 //if the texture size width exceed maxTextureSize, modify the visual model size and enabled the ellipsis
948 bool actualellipsis = mController->IsTextElideEnabled();
949 if( verifiedSize.width > maxTextureSize )
951 verifiedSize.width = maxTextureSize;
952 if( textNaturalSize.width > maxTextureSize )
954 mController->SetTextElideEnabled( true );
956 GetHeightForWidth( maxTextureSize );
957 wrapGap = std::max( maxTextureSize - textNaturalSize.width, 0.0f );
960 Text::TypesetterPtr typesetter = Text::Typesetter::New( mController->GetTextModel() );
962 PixelData data = typesetter->Render( verifiedSize, mController->GetTextDirection(), Text::Typesetter::RENDER_TEXT_AND_STYLES, true, Pixel::RGBA8888 ); // ignore the horizontal alignment
963 Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
964 data.GetPixelFormat(),
967 texture.Upload( data );
969 TextureSet textureSet = TextureSet::New();
970 textureSet.SetTexture( 0u, texture );
972 // Filter mode needs to be set to linear to produce better quality while scaling.
973 Sampler sampler = Sampler::New();
974 sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
975 sampler.SetWrapMode( Dali::WrapMode::DEFAULT, Dali::WrapMode::REPEAT, Dali::WrapMode::DEFAULT ); // Wrap the texture in the x direction
976 textureSet.SetSampler( 0u, sampler );
978 // Set parameters for scrolling
979 Renderer renderer = static_cast<Internal::Visual::Base&>( GetImplementation( mVisual ) ).GetRenderer();
980 mTextScroller->SetParameters( Self(), renderer, textureSet, controlSize, verifiedSize, wrapGap, direction, mController->GetHorizontalAlignment(), mController->GetVerticalAlignment() );
981 mController->SetTextElideEnabled( actualellipsis );
984 void TextLabel::ScrollingFinished()
986 // Pure Virtual from TextScroller Interface
987 DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
988 mController->SetAutoScrollEnabled( false );
989 RequestTextRelayout();
992 TextLabel::TextLabel()
993 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
994 mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
995 mTextUpdateNeeded( false )
999 TextLabel::~TextLabel()
1003 std::string TextLabel::AccessibleImpl::GetNameRaw()
1005 auto slf = Toolkit::TextLabel::DownCast( self );
1006 return slf.GetProperty( Toolkit::TextLabel::Property::TEXT ).Get< std::string >();
1009 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1011 return Toolkit::TextLabel::Property::TEXT;
1014 std::string TextLabel::AccessibleImpl::GetText( size_t startOffset,
1017 if( endOffset <= startOffset )
1020 auto slf = Toolkit::TextLabel::DownCast( self );
1022 slf.GetProperty( Toolkit::TextLabel::Property::TEXT ).Get< std::string >();
1024 if( startOffset > txt.size() || endOffset > txt.size() )
1027 return txt.substr( startOffset, endOffset - startOffset );
1030 size_t TextLabel::AccessibleImpl::GetCharacterCount()
1032 auto slf = Toolkit::TextLabel::DownCast( self );
1034 slf.GetProperty( Toolkit::TextLabel::Property::TEXT ).Get< std::string >();
1039 size_t TextLabel::AccessibleImpl::GetCaretOffset()
1044 bool TextLabel::AccessibleImpl::SetCaretOffset(size_t offset)
1049 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(
1050 size_t offset, Dali::Accessibility::TextBoundary boundary )
1052 auto slf = Toolkit::TextLabel::DownCast( self );
1053 auto txt = slf.GetProperty( Toolkit::TextLabel::Property::TEXT ).Get< std::string >();
1054 auto txt_size = txt.size();
1056 auto range = Dali::Accessibility::Range{};
1060 case Dali::Accessibility::TextBoundary::CHARACTER:
1062 if (offset < txt_size)
1064 range.content = txt[offset];
1065 range.startOffset = offset;
1066 range.endOffset = offset + 1;
1070 case Dali::Accessibility::TextBoundary::WORD:
1071 case Dali::Accessibility::TextBoundary::LINE:
1073 auto txt_c_string = txt.c_str();
1074 auto breaks = std::vector< char >( txt_size, 0 );
1075 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1076 Accessibility::Accessible::FindWordSeparationsUtf8((const utf8_t *) txt_c_string, txt_size, "", breaks.data());
1078 Accessibility::Accessible::FindLineSeparationsUtf8((const utf8_t *) txt_c_string, txt_size, "", breaks.data());
1081 while( index < txt_size && counter <= offset )
1086 while(breaks[index])
1092 if (boundary == Dali::Accessibility::TextBoundary::WORD)
1094 if (boundary == Dali::Accessibility::TextBoundary::LINE)
1097 if ((counter > 0) && ((counter - 1) == offset))
1099 range.content = txt.substr(start, index - start + 1);
1100 range.startOffset = start;
1101 range.endOffset = index + 1;
1103 if (boundary == Dali::Accessibility::TextBoundary::LINE)
1108 case Dali::Accessibility::TextBoundary::SENTENCE:
1110 /* not supported by efl */
1113 case Dali::Accessibility::TextBoundary::PARAGRAPH:
1115 /* Paragraph is not supported by libunibreak library */
1125 Dali::Accessibility::Range
1126 TextLabel::AccessibleImpl::GetSelection( size_t selectionNum )
1128 // Since DALi supports only one selection indexes higher than 0 are ignored
1129 if( selectionNum > 0 )
1132 auto slf = Toolkit::TextLabel::DownCast( self );
1133 auto ctrl = Dali::Toolkit::GetImpl( slf ).getController();
1135 ctrl->RetrieveSelection( ret );
1136 auto r = ctrl->GetSelectionIndexes();
1138 return { static_cast<size_t>(r.first), static_cast<size_t>(r.second), ret };
1141 bool TextLabel::AccessibleImpl::RemoveSelection( size_t selectionNum )
1143 // Since DALi supports only one selection indexes higher than 0 are ignored
1144 if( selectionNum > 0 )
1147 auto slf = Toolkit::TextLabel::DownCast( self );
1148 Dali::Toolkit::GetImpl( slf ).getController()->SetSelection( 0, 0 );
1152 bool TextLabel::AccessibleImpl::SetSelection( size_t selectionNum,
1156 // Since DALi supports only one selection indexes higher than 0 are ignored
1157 if( selectionNum > 0 )
1160 auto slf = Toolkit::TextLabel::DownCast( self );
1161 Dali::Toolkit::GetImpl( slf ).getController()->SetSelection( startOffset,
1166 } // namespace Internal
1168 } // namespace Toolkit