2 * Copyright (c) 2021 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/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/adaptor-framework/window-devel.h>
24 #include <dali/devel-api/common/stage.h>
25 #include <dali/devel-api/object/property-helper-devel.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/object/type-registry-helper.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/devel-api/text/rendering-backend.h>
33 #include <dali-toolkit/internal/styling/style-manager-impl.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-definitions.h>
37 #include <dali-toolkit/internal/text/text-effects-style.h>
38 #include <dali-toolkit/internal/text/text-font-style.h>
39 #include <dali-toolkit/internal/text/text-view.h>
40 #include <dali-toolkit/public-api/text/text-enumerations.h>
42 #include <dali-toolkit/devel-api/controls/control-devel.h>
43 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
44 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
45 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
46 #include <dali-toolkit/public-api/align-enumerations.h>
47 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
48 #include <dali-toolkit/public-api/visuals/visual-properties.h>
50 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
52 using namespace Dali::Toolkit::Text;
62 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
65 * @brief How the text visual should be aligned vertically inside the control.
67 * 0.0f aligns the text to the top, 0.5f aligns the text to the center, 1.0f aligns the text to the bottom.
68 * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
70 const float VERTICAL_ALIGNMENT_TABLE[Text::VerticalAlignment::BOTTOM + 1] =
72 0.0f, // VerticalAlignment::TOP
73 0.5f, // VerticalAlignment::CENTER
74 1.0f // VerticalAlignment::BOTTOM
77 const std::string TEXT_FIT_ENABLE_KEY("enable");
78 const std::string TEXT_FIT_MIN_SIZE_KEY("minSize");
79 const std::string TEXT_FIT_MAX_SIZE_KEY("maxSize");
80 const std::string TEXT_FIT_STEP_SIZE_KEY("stepSize");
81 const std::string TEXT_FIT_FONT_SIZE_TYPE_KEY("fontSizeType");
83 #if defined(DEBUG_ENABLED)
84 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
87 const Scripting::StringEnum AUTO_SCROLL_STOP_MODE_TABLE[] =
89 {"IMMEDIATE", Toolkit::TextLabel::AutoScrollStopMode::IMMEDIATE},
90 {"FINISH_LOOP", Toolkit::TextLabel::AutoScrollStopMode::FINISH_LOOP},
92 const unsigned int AUTO_SCROLL_STOP_MODE_TABLE_COUNT = sizeof(AUTO_SCROLL_STOP_MODE_TABLE) / sizeof(AUTO_SCROLL_STOP_MODE_TABLE[0]);
97 return Toolkit::TextLabel::New();
101 // Setup properties, signals and actions using the type-registry.
102 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::TextLabel, Toolkit::Control, Create);
104 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "text", STRING, TEXT )
105 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
106 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "fontStyle", MAP, FONT_STYLE )
107 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
108 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
109 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
110 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
111 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "enableMarkup", BOOLEAN, ENABLE_MARKUP )
112 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "enableAutoScroll", BOOLEAN, ENABLE_AUTO_SCROLL )
113 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollSpeed", INTEGER, AUTO_SCROLL_SPEED )
114 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollLoopCount", INTEGER, AUTO_SCROLL_LOOP_COUNT )
115 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollGap", FLOAT, AUTO_SCROLL_GAP )
116 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "lineSpacing", FLOAT, LINE_SPACING )
117 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "underline", MAP, UNDERLINE )
118 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "shadow", MAP, SHADOW )
119 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "emboss", MAP, EMBOSS )
120 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "outline", MAP, OUTLINE )
121 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "pixelSize", FLOAT, PIXEL_SIZE )
122 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "ellipsis", BOOLEAN, ELLIPSIS )
123 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollLoopDelay", FLOAT, AUTO_SCROLL_LOOP_DELAY )
124 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollStopMode", STRING, AUTO_SCROLL_STOP_MODE )
125 DALI_PROPERTY_REGISTRATION_READ_ONLY(Toolkit, TextLabel, "lineCount", INTEGER, LINE_COUNT )
126 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "lineWrapMode", INTEGER, LINE_WRAP_MODE )
127 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY(Toolkit, TextLabel, "textDirection", INTEGER, TEXT_DIRECTION )
128 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "verticalLineAlignment", INTEGER, VERTICAL_LINE_ALIGNMENT )
129 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "textBackground", MAP, BACKGROUND )
130 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "ignoreSpacesAfterText", BOOLEAN, IGNORE_SPACES_AFTER_TEXT )
131 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "matchSystemLanguageDirection", BOOLEAN, MATCH_SYSTEM_LANGUAGE_DIRECTION)
132 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "textFit", MAP, TEXT_FIT )
133 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "minLineSize", FLOAT, MIN_LINE_SIZE )
134 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
135 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "fontSizeScale", FLOAT, FONT_SIZE_SCALE )
137 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(Toolkit, TextLabel, "textColor", Color::BLACK, TEXT_COLOR )
138 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, TextLabel, "textColorRed", TEXT_COLOR_RED, TEXT_COLOR, 0)
139 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, TextLabel, "textColorGreen", TEXT_COLOR_GREEN, TEXT_COLOR, 1)
140 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, TextLabel, "textColorBlue", TEXT_COLOR_BLUE, TEXT_COLOR, 2)
141 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit, TextLabel, "textColorAlpha", TEXT_COLOR_ALPHA, TEXT_COLOR, 3)
142 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()
499 Property::Value TextLabel::GetProperty(BaseObject* object, Property::Index index)
501 Property::Value value;
503 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
507 TextLabel& impl(GetImpl(label));
508 DALI_ASSERT_DEBUG(impl.mController && "No text contoller");
512 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
514 value = impl.mRenderingBackend;
517 case Toolkit::TextLabel::Property::TEXT:
520 impl.mController->GetText(text);
524 case Toolkit::TextLabel::Property::FONT_FAMILY:
526 value = impl.mController->GetDefaultFontFamily();
529 case Toolkit::TextLabel::Property::FONT_STYLE:
531 GetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
534 case Toolkit::TextLabel::Property::POINT_SIZE:
536 value = impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE);
539 case Toolkit::TextLabel::Property::MULTI_LINE:
541 value = impl.mController->IsMultiLineEnabled();
544 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
546 const char* name = Text::GetHorizontalAlignmentString(impl.mController->GetHorizontalAlignment());
550 value = std::string(name);
554 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
556 const char* name = Text::GetVerticalAlignmentString(impl.mController->GetVerticalAlignment());
559 value = std::string(name);
563 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
565 value = impl.mController->IsMarkupProcessorEnabled();
568 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
570 value = impl.mController->IsAutoScrollEnabled();
573 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
575 if(impl.mTextScroller)
577 const char* mode = Scripting::GetEnumerationName<Toolkit::TextLabel::AutoScrollStopMode::Type>(impl.mTextScroller->GetStopMode(),
578 AUTO_SCROLL_STOP_MODE_TABLE,
579 AUTO_SCROLL_STOP_MODE_TABLE_COUNT);
582 value = std::string(mode);
587 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
589 if(impl.mTextScroller)
591 value = impl.mTextScroller->GetSpeed();
595 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
597 if(impl.mTextScroller)
599 value = impl.mTextScroller->GetLoopCount();
603 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
605 if(impl.mTextScroller)
607 value = impl.mTextScroller->GetLoopDelay();
611 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
613 if(impl.mTextScroller)
615 value = impl.mTextScroller->GetGap();
619 case Toolkit::TextLabel::Property::LINE_SPACING:
621 value = impl.mController->GetDefaultLineSpacing();
624 case Toolkit::TextLabel::Property::UNDERLINE:
626 GetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
629 case Toolkit::TextLabel::Property::SHADOW:
631 GetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
634 case Toolkit::TextLabel::Property::EMBOSS:
636 GetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
639 case Toolkit::TextLabel::Property::OUTLINE:
641 GetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
644 case Toolkit::TextLabel::Property::PIXEL_SIZE:
646 value = impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE);
649 case Toolkit::TextLabel::Property::ELLIPSIS:
651 value = impl.mController->IsTextElideEnabled();
654 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
656 value = impl.mController->GetLineWrapMode();
659 case Toolkit::TextLabel::Property::LINE_COUNT:
661 float width = label.GetProperty(Actor::Property::SIZE_WIDTH).Get<float>();
662 value = impl.mController->GetLineCount(width);
665 case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
667 value = impl.mController->GetTextDirection();
670 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
672 value = impl.mController->GetVerticalLineAlignment();
675 case Toolkit::DevelTextLabel::Property::BACKGROUND:
677 GetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
680 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
682 value = impl.mController->IsIgnoreSpacesAfterText();
685 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
687 value = impl.mController->IsMatchSystemLanguageDirection();
690 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
692 const bool enabled = impl.mController->IsTextFitEnabled();
693 const float minSize = impl.mController->GetTextFitMinSize();
694 const float maxSize = impl.mController->GetTextFitMaxSize();
695 const float stepSize = impl.mController->GetTextFitStepSize();
698 map.Insert(TEXT_FIT_ENABLE_KEY, enabled);
699 map.Insert(TEXT_FIT_MIN_SIZE_KEY, minSize);
700 map.Insert(TEXT_FIT_MAX_SIZE_KEY, maxSize);
701 map.Insert(TEXT_FIT_STEP_SIZE_KEY, stepSize);
702 map.Insert(TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize");
707 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
709 value = impl.mController->GetDefaultLineSize();
712 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
714 value = impl.mController->GetFontSizeScale();
723 void TextLabel::OnInitialize()
727 Property::Map propertyMap;
728 propertyMap.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
730 mVisual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
731 DevelControl::RegisterVisual(*this, Toolkit::TextLabel::Property::TEXT, mVisual);
733 TextVisual::SetAnimatableTextColorProperty(mVisual, Toolkit::TextLabel::Property::TEXT_COLOR);
735 mController = TextVisual::GetController(mVisual);
736 DALI_ASSERT_DEBUG(mController && "Invalid Text Controller")
738 mController->SetControlInterface(this);
740 // Use height-for-width negotiation by default
741 self.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
742 self.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
744 // Enable highlightability
745 self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
747 // Enable the text ellipsis.
748 mController->SetTextElideEnabled(true); // If false then text larger than control will overflow
750 // Sets layoutDirection value
751 Dali::Stage stage = Dali::Stage::GetCurrent();
752 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(stage.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
753 mController->SetLayoutDirection(layoutDirection);
755 Layout::Engine& engine = mController->GetLayoutEngine();
756 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
758 DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
759 return std::unique_ptr<Dali::Accessibility::Accessible>(
760 new AccessibleImpl(actor, Dali::Accessibility::Role::LABEL));
764 void TextLabel::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
766 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
770 case StyleChange::DEFAULT_FONT_CHANGE:
772 // Property system did not set the font so should update it.
773 const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
774 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str());
775 mController->UpdateAfterFontChange(newFont);
779 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
781 GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
785 case StyleChange::THEME_CHANGE:
787 // Nothing to do, let control base class handle this
792 // Up call to Control
793 Control::OnStyleChange(styleManager, change);
796 Vector3 TextLabel::GetNaturalSize()
799 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
801 Vector3 naturalSize = mController->GetNaturalSize();
802 naturalSize.width += (padding.start + padding.end);
803 naturalSize.height += (padding.top + padding.bottom);
808 float TextLabel::GetHeightForWidth(float width)
811 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
813 return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
816 void TextLabel::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
818 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index);
822 case Toolkit::TextLabel::Property::TEXT_COLOR:
824 const Vector4& textColor = propertyValue.Get<Vector4>();
825 if(mController->GetDefaultColor() != textColor)
827 mController->SetDefaultColor(textColor);
828 mTextUpdateNeeded = true;
834 Control::OnPropertySet(index, propertyValue); // up call to control for non-handled properties
840 void TextLabel::OnRelayout(const Vector2& size, RelayoutContainer& container)
842 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout\n");
845 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
847 Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
849 if(mController->IsTextFitEnabled())
851 mController->FitPointSizeforLayout(contentSize);
852 mController->SetTextFitContentSize(contentSize);
855 // Support Right-To-Left
856 Dali::LayoutDirection::Type layoutDirection;
857 if(mController->IsMatchSystemLanguageDirection())
859 layoutDirection = static_cast<Dali::LayoutDirection::Type>(DevelWindow::Get(Self()).GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
863 layoutDirection = static_cast<Dali::LayoutDirection::Type>(Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
865 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
867 if((Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType)) || mTextUpdateNeeded)
869 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", (mController->IsAutoScrollEnabled()) ? "true" : "false", this);
872 TextVisual::EnableRendererUpdate(mVisual);
874 // Support Right-To-Left of padding
875 if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
877 std::swap(padding.start, padding.end);
880 // Calculate the size of the visual that can fit the text
881 Size layoutSize = mController->GetTextModel()->GetLayoutSize();
882 layoutSize.x = contentSize.x;
884 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
885 if(shadowOffset.y > Math::MACHINE_EPSILON_1)
887 layoutSize.y += shadowOffset.y;
890 float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
891 layoutSize.y += outlineWidth * 2.0f;
892 layoutSize.y = std::min(layoutSize.y, contentSize.y);
894 // Calculate the offset for vertical alignment only, as the layout engine will do the horizontal alignment.
895 Vector2 alignmentOffset;
896 alignmentOffset.x = 0.0f;
897 alignmentOffset.y = (contentSize.y - layoutSize.y) * VERTICAL_ALIGNMENT_TABLE[mController->GetVerticalAlignment()];
899 Property::Map visualTransform;
900 visualTransform.Add(Toolkit::Visual::Transform::Property::SIZE, layoutSize)
901 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
902 .Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2(padding.start, padding.top) + alignmentOffset)
903 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
904 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
905 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN);
906 mVisual.SetTransformAndSize(visualTransform, size);
908 if(mController->IsAutoScrollEnabled())
910 SetUpAutoScrolling();
913 mTextUpdateNeeded = false;
917 void TextLabel::RequestTextRelayout()
920 // Signal that a Relayout may be needed
923 void TextLabel::SetUpAutoScrolling()
925 const Size& controlSize = mController->GetView().GetControlSize();
926 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.
927 const Text::CharacterDirection direction = mController->GetAutoScrollDirection();
929 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n", textNaturalSize.x, textNaturalSize.y, controlSize.x, controlSize.y);
933 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
935 // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
936 mTextScroller = Text::TextScroller::New(*this);
939 // Calculate the actual gap before scrolling wraps.
940 int textPadding = std::max(controlSize.x - textNaturalSize.x, 0.0f);
941 float wrapGap = std::max(mTextScroller->GetGap(), textPadding);
942 Vector2 textureSize = textNaturalSize + Vector2(wrapGap, 0.0f); // Add the gap as a part of the texture
944 // Create a texture of the text for scrolling
945 Size verifiedSize = textureSize;
946 const int maxTextureSize = Dali::GetMaxTextureSize();
948 //if the texture size width exceed maxTextureSize, modify the visual model size and enabled the ellipsis
949 bool actualellipsis = mController->IsTextElideEnabled();
950 if(verifiedSize.width > maxTextureSize)
952 verifiedSize.width = maxTextureSize;
953 if(textNaturalSize.width > maxTextureSize)
955 mController->SetTextElideEnabled(true);
957 GetHeightForWidth(maxTextureSize);
958 wrapGap = std::max(maxTextureSize - textNaturalSize.width, 0.0f);
961 Text::TypesetterPtr typesetter = Text::Typesetter::New(mController->GetTextModel());
963 PixelData data = typesetter->Render(verifiedSize, mController->GetTextDirection(), Text::Typesetter::RENDER_TEXT_AND_STYLES, true, Pixel::RGBA8888); // ignore the horizontal alignment
964 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
965 data.GetPixelFormat(),
968 texture.Upload(data);
970 TextureSet textureSet = TextureSet::New();
971 textureSet.SetTexture(0u, texture);
973 // Filter mode needs to be set to linear to produce better quality while scaling.
974 Sampler sampler = Sampler::New();
975 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
976 sampler.SetWrapMode(Dali::WrapMode::DEFAULT, Dali::WrapMode::REPEAT, Dali::WrapMode::DEFAULT); // Wrap the texture in the x direction
977 textureSet.SetSampler(0u, sampler);
979 // Set parameters for scrolling
980 Renderer renderer = static_cast<Internal::Visual::Base&>(GetImplementation(mVisual)).GetRenderer();
981 mTextScroller->SetParameters(Self(), renderer, textureSet, controlSize, verifiedSize, wrapGap, direction, mController->GetHorizontalAlignment(), mController->GetVerticalAlignment());
982 mController->SetTextElideEnabled(actualellipsis);
985 void TextLabel::ScrollingFinished()
987 // Pure Virtual from TextScroller Interface
988 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
989 mController->SetAutoScrollEnabled(false);
990 RequestTextRelayout();
993 TextLabel::TextLabel()
994 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
995 mRenderingBackend(DEFAULT_RENDERING_BACKEND),
996 mTextUpdateNeeded(false)
1000 TextLabel::~TextLabel()
1004 std::string TextLabel::AccessibleImpl::GetNameRaw()
1006 auto slf = Toolkit::TextLabel::DownCast(self);
1007 return slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1010 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1012 return Toolkit::TextLabel::Property::TEXT;
1015 std::string TextLabel::AccessibleImpl::GetText(size_t startOffset,
1018 if(endOffset <= startOffset)
1021 auto slf = Toolkit::TextLabel::DownCast(self);
1023 slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1025 if(startOffset > txt.size() || endOffset > txt.size())
1028 return txt.substr(startOffset, endOffset - startOffset);
1031 size_t TextLabel::AccessibleImpl::GetCharacterCount()
1033 auto slf = Toolkit::TextLabel::DownCast(self);
1035 slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1040 size_t TextLabel::AccessibleImpl::GetCaretOffset()
1045 bool TextLabel::AccessibleImpl::SetCaretOffset(size_t offset)
1050 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(
1051 size_t offset, Dali::Accessibility::TextBoundary boundary)
1053 auto slf = Toolkit::TextLabel::DownCast(self);
1054 auto txt = slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1055 auto txt_size = txt.size();
1057 auto range = Dali::Accessibility::Range{};
1061 case Dali::Accessibility::TextBoundary::CHARACTER:
1063 if(offset < txt_size)
1065 range.content = txt[offset];
1066 range.startOffset = offset;
1067 range.endOffset = offset + 1;
1071 case Dali::Accessibility::TextBoundary::WORD:
1072 case Dali::Accessibility::TextBoundary::LINE:
1074 auto txt_c_string = txt.c_str();
1075 auto breaks = std::vector<char>(txt_size, 0);
1076 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1077 Accessibility::Accessible::FindWordSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1079 Accessibility::Accessible::FindLineSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1082 while(index < txt_size && counter <= offset)
1087 while(breaks[index])
1093 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1095 if(boundary == Dali::Accessibility::TextBoundary::LINE)
1098 if((counter > 0) && ((counter - 1) == offset))
1100 range.content = txt.substr(start, index - start + 1);
1101 range.startOffset = start;
1102 range.endOffset = index + 1;
1104 if(boundary == Dali::Accessibility::TextBoundary::LINE)
1109 case Dali::Accessibility::TextBoundary::SENTENCE:
1111 /* not supported by efl */
1114 case Dali::Accessibility::TextBoundary::PARAGRAPH:
1116 /* Paragraph is not supported by libunibreak library */
1126 Dali::Accessibility::Range
1127 TextLabel::AccessibleImpl::GetSelection(size_t selectionNum)
1129 // Since DALi supports only one selection indexes higher than 0 are ignored
1130 if(selectionNum > 0)
1133 auto slf = Toolkit::TextLabel::DownCast(self);
1134 auto ctrl = Dali::Toolkit::GetImpl(slf).getController();
1136 ctrl->RetrieveSelection(ret);
1137 auto r = ctrl->GetSelectionIndexes();
1139 return {static_cast<size_t>(r.first), static_cast<size_t>(r.second), ret};
1142 bool TextLabel::AccessibleImpl::RemoveSelection(size_t selectionNum)
1144 // Since DALi supports only one selection indexes higher than 0 are ignored
1145 if(selectionNum > 0)
1148 auto slf = Toolkit::TextLabel::DownCast(self);
1149 Dali::Toolkit::GetImpl(slf).getController()->SetSelection(0, 0);
1153 bool TextLabel::AccessibleImpl::SetSelection(size_t selectionNum,
1157 // Since DALi supports only one selection indexes higher than 0 are ignored
1158 if(selectionNum > 0)
1161 auto slf = Toolkit::TextLabel::DownCast(self);
1162 Dali::Toolkit::GetImpl(slf).getController()->SetSelection(startOffset,
1167 } // namespace Internal
1169 } // namespace Toolkit