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)
143 DALI_SIGNAL_REGISTRATION(Toolkit, TextLabel, "anchorClicked", SIGNAL_ANCHOR_CLICKED)
145 DALI_TYPE_REGISTRATION_END()
148 /// Parses the property map for the TEXT_FIT property
149 void ParseTextFitProperty(Text::ControllerPtr& controller, const Property::Map* propertiesMap)
151 if(propertiesMap && !propertiesMap->Empty())
153 bool enabled = false;
156 float stepSize = 0.f;
157 bool isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
158 Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
160 const unsigned int numberOfItems = propertiesMap->Count();
162 // Parses and applies
163 for(unsigned int index = 0u; index < numberOfItems; ++index)
165 const KeyValuePair& valueGet = propertiesMap->GetKeyValue(index);
167 if((Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey) || (TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey))
170 enabled = valueGet.second.Get<bool>();
172 else if((Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey))
175 minSize = valueGet.second.Get<float>();
178 else if((Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey))
181 maxSize = valueGet.second.Get<float>();
184 else if((Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey) || (TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey))
187 stepSize = valueGet.second.Get<float>();
188 isStepSizeSet = true;
190 else if((Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey) || (TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey))
192 if("pixelSize" == valueGet.second.Get<std::string>())
194 type = Controller::FontSizeType::PIXEL_SIZE;
199 controller->SetTextFitEnabled(enabled);
202 controller->SetTextFitMinSize(minSize, type);
206 controller->SetTextFitMaxSize(maxSize, type);
210 controller->SetTextFitStepSize(stepSize, type);
217 Toolkit::TextLabel TextLabel::New()
219 // Create the implementation, temporarily owned by this handle on stack
220 IntrusivePtr<TextLabel> impl = new TextLabel();
222 // Pass ownership to CustomActor handle
223 Toolkit::TextLabel handle(*impl);
225 // Second-phase init of the implementation
226 // This can only be done after the CustomActor connection has been made...
232 void TextLabel::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
234 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
238 TextLabel& impl(GetImpl(label));
239 DALI_ASSERT_ALWAYS(impl.mController && "No text contoller");
243 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
245 int backend = value.Get<int>();
247 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
248 if(DevelText::RENDERING_VECTOR_BASED == backend)
250 backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
253 if(impl.mRenderingBackend != backend)
255 impl.mRenderingBackend = backend;
256 impl.mTextUpdateNeeded = true;
258 // When using the vector-based rendering, the size of the GLyphs are different
259 TextAbstraction::GlyphType glyphType = (DevelText::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
260 impl.mController->SetGlyphType(glyphType);
264 case Toolkit::TextLabel::Property::TEXT:
266 impl.mController->SetText(value.Get<std::string>());
269 case Toolkit::TextLabel::Property::FONT_FAMILY:
271 const std::string& fontFamily = value.Get<std::string>();
273 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str());
274 impl.mController->SetDefaultFontFamily(fontFamily);
277 case Toolkit::TextLabel::Property::FONT_STYLE:
279 SetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
282 case Toolkit::TextLabel::Property::POINT_SIZE:
284 const float pointSize = value.Get<float>();
286 if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
288 impl.mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
292 case Toolkit::TextLabel::Property::MULTI_LINE:
294 impl.mController->SetMultiLineEnabled(value.Get<bool>());
297 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
299 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
300 if(Text::GetHorizontalAlignmentEnumeration(value, alignment))
302 impl.mController->SetHorizontalAlignment(alignment);
306 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
308 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
309 if(Text::GetVerticalAlignmentEnumeration(value, alignment))
311 impl.mController->SetVerticalAlignment(alignment);
315 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
317 const bool enableMarkup = value.Get<bool>();
318 impl.mController->SetMarkupProcessorEnabled(enableMarkup);
321 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
323 const bool enableAutoScroll = value.Get<bool>();
324 // If request to auto scroll is the same as current state then do nothing.
325 if(enableAutoScroll != impl.mController->IsAutoScrollEnabled())
327 // If request is disable (false) and auto scrolling is enabled then need to stop it
328 if(enableAutoScroll == false)
330 if(impl.mTextScroller)
332 impl.mTextScroller->StopScrolling();
335 // If request is enable (true) then start autoscroll as not already running
338 impl.mController->SetAutoScrollEnabled(enableAutoScroll);
343 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
345 Text::TextScrollerPtr textScroller = impl.GetTextScroller();
346 Toolkit::TextLabel::AutoScrollStopMode::Type stopMode = textScroller->GetStopMode();
347 if(Scripting::GetEnumerationProperty<Toolkit::TextLabel::AutoScrollStopMode::Type>(value,
348 AUTO_SCROLL_STOP_MODE_TABLE,
349 AUTO_SCROLL_STOP_MODE_TABLE_COUNT,
352 textScroller->SetStopMode(stopMode);
356 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
358 impl.GetTextScroller()->SetSpeed(value.Get<int>());
361 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
363 impl.GetTextScroller()->SetLoopCount(value.Get<int>());
366 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
368 impl.GetTextScroller()->SetLoopDelay(value.Get<float>());
371 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
373 impl.GetTextScroller()->SetGap(value.Get<float>());
376 case Toolkit::TextLabel::Property::LINE_SPACING:
378 const float lineSpacing = value.Get<float>();
379 impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSpacing(lineSpacing) || impl.mTextUpdateNeeded;
382 case Toolkit::TextLabel::Property::UNDERLINE:
384 impl.mTextUpdateNeeded = SetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
387 case Toolkit::TextLabel::Property::SHADOW:
389 impl.mTextUpdateNeeded = SetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
392 case Toolkit::TextLabel::Property::EMBOSS:
394 impl.mTextUpdateNeeded = SetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
397 case Toolkit::TextLabel::Property::OUTLINE:
399 impl.mTextUpdateNeeded = SetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
402 case Toolkit::TextLabel::Property::PIXEL_SIZE:
404 const float pixelSize = value.Get<float>();
405 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize);
407 if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE), pixelSize))
409 impl.mController->SetDefaultFontSize(pixelSize, Text::Controller::PIXEL_SIZE);
413 case Toolkit::TextLabel::Property::ELLIPSIS:
415 const bool ellipsis = value.Get<bool>();
416 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis);
418 impl.mController->SetTextElideEnabled(ellipsis);
421 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
423 Text::LineWrap::Mode lineWrapMode(static_cast<Text::LineWrap::Mode>(-1)); // Set to invalid value to ensure a valid mode does get set
424 if(GetLineWrapModeEnumeration(value, lineWrapMode))
426 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode);
427 impl.mController->SetLineWrapMode(lineWrapMode);
431 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
433 if(impl.mController->GetTextModel())
435 DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>(value.Get<int>());
437 impl.mController->SetVerticalLineAlignment(alignment);
439 // Property doesn't affect the layout, only Visual must be updated
440 TextVisual::EnableRendererUpdate(impl.mVisual);
442 // No need to trigger full re-layout. Instead call UpdateRenderer() directly
443 TextVisual::UpdateRenderer(impl.mVisual);
447 case Toolkit::DevelTextLabel::Property::BACKGROUND:
449 impl.mTextUpdateNeeded = SetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
452 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
454 impl.mController->SetIgnoreSpacesAfterText(value.Get<bool>());
457 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
459 impl.mController->SetMatchSystemLanguageDirection(value.Get<bool>());
462 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
464 ParseTextFitProperty(impl.mController, value.GetMap());
467 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
469 const float lineSize = value.Get<float>();
470 impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSize(lineSize) || impl.mTextUpdateNeeded;
473 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
475 const float scale = value.Get<float>();
476 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p FONT_SIZE_SCALE %f\n", impl.mController.Get(), scale);
478 if(!Equals(impl.mController->GetFontSizeScale(), scale))
480 impl.mController->SetFontSizeScale(scale);
486 // Request relayout when text update is needed. It's necessary to call it
487 // as changing the property not via UI interaction brings no effect if only
488 // the mTextUpdateNeeded is changed.
489 if(impl.mTextUpdateNeeded)
491 // need to request relayout as size of text may have changed
492 impl.RequestTextRelayout();
497 Text::ControllerPtr TextLabel::getController()
502 Property::Value TextLabel::GetProperty(BaseObject* object, Property::Index index)
504 Property::Value value;
506 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
510 TextLabel& impl(GetImpl(label));
511 DALI_ASSERT_DEBUG(impl.mController && "No text contoller");
515 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
517 value = impl.mRenderingBackend;
520 case Toolkit::TextLabel::Property::TEXT:
523 impl.mController->GetText(text);
527 case Toolkit::TextLabel::Property::FONT_FAMILY:
529 value = impl.mController->GetDefaultFontFamily();
532 case Toolkit::TextLabel::Property::FONT_STYLE:
534 GetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
537 case Toolkit::TextLabel::Property::POINT_SIZE:
539 value = impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE);
542 case Toolkit::TextLabel::Property::MULTI_LINE:
544 value = impl.mController->IsMultiLineEnabled();
547 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
549 const char* name = Text::GetHorizontalAlignmentString(impl.mController->GetHorizontalAlignment());
553 value = std::string(name);
557 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
559 const char* name = Text::GetVerticalAlignmentString(impl.mController->GetVerticalAlignment());
562 value = std::string(name);
566 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
568 value = impl.mController->IsMarkupProcessorEnabled();
571 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
573 value = impl.mController->IsAutoScrollEnabled();
576 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
578 if(impl.mTextScroller)
580 const char* mode = Scripting::GetEnumerationName<Toolkit::TextLabel::AutoScrollStopMode::Type>(impl.mTextScroller->GetStopMode(),
581 AUTO_SCROLL_STOP_MODE_TABLE,
582 AUTO_SCROLL_STOP_MODE_TABLE_COUNT);
585 value = std::string(mode);
590 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
592 if(impl.mTextScroller)
594 value = impl.mTextScroller->GetSpeed();
598 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
600 if(impl.mTextScroller)
602 value = impl.mTextScroller->GetLoopCount();
606 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
608 if(impl.mTextScroller)
610 value = impl.mTextScroller->GetLoopDelay();
614 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
616 if(impl.mTextScroller)
618 value = impl.mTextScroller->GetGap();
622 case Toolkit::TextLabel::Property::LINE_SPACING:
624 value = impl.mController->GetDefaultLineSpacing();
627 case Toolkit::TextLabel::Property::UNDERLINE:
629 GetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
632 case Toolkit::TextLabel::Property::SHADOW:
634 GetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
637 case Toolkit::TextLabel::Property::EMBOSS:
639 GetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
642 case Toolkit::TextLabel::Property::OUTLINE:
644 GetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
647 case Toolkit::TextLabel::Property::PIXEL_SIZE:
649 value = impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE);
652 case Toolkit::TextLabel::Property::ELLIPSIS:
654 value = impl.mController->IsTextElideEnabled();
657 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
659 value = impl.mController->GetLineWrapMode();
662 case Toolkit::TextLabel::Property::LINE_COUNT:
664 float width = label.GetProperty(Actor::Property::SIZE_WIDTH).Get<float>();
665 value = impl.mController->GetLineCount(width);
668 case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
670 value = impl.mController->GetTextDirection();
673 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
675 value = impl.mController->GetVerticalLineAlignment();
678 case Toolkit::DevelTextLabel::Property::BACKGROUND:
680 GetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
683 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
685 value = impl.mController->IsIgnoreSpacesAfterText();
688 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
690 value = impl.mController->IsMatchSystemLanguageDirection();
693 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
695 const bool enabled = impl.mController->IsTextFitEnabled();
696 const float minSize = impl.mController->GetTextFitMinSize();
697 const float maxSize = impl.mController->GetTextFitMaxSize();
698 const float stepSize = impl.mController->GetTextFitStepSize();
701 map.Insert(TEXT_FIT_ENABLE_KEY, enabled);
702 map.Insert(TEXT_FIT_MIN_SIZE_KEY, minSize);
703 map.Insert(TEXT_FIT_MAX_SIZE_KEY, maxSize);
704 map.Insert(TEXT_FIT_STEP_SIZE_KEY, stepSize);
705 map.Insert(TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize");
710 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
712 value = impl.mController->GetDefaultLineSize();
715 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
717 value = impl.mController->GetFontSizeScale();
726 bool TextLabel::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
728 Dali::BaseHandle handle(object);
730 bool connected(true);
731 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(handle);
733 if(0 == strcmp(signalName.c_str(), SIGNAL_ANCHOR_CLICKED))
737 Internal::TextLabel& labelImpl(GetImpl(label));
738 labelImpl.AnchorClickedSignal().Connect(tracker, functor);
743 // signalName does not match any signal
750 DevelTextLabel::AnchorClickedSignalType& TextLabel::AnchorClickedSignal()
752 return mAnchorClickedSignal;
755 void TextLabel::OnInitialize()
759 Property::Map propertyMap;
760 propertyMap.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
762 mVisual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
763 DevelControl::RegisterVisual(*this, Toolkit::TextLabel::Property::TEXT, mVisual);
765 TextVisual::SetAnimatableTextColorProperty(mVisual, Toolkit::TextLabel::Property::TEXT_COLOR);
767 mController = TextVisual::GetController(mVisual);
768 DALI_ASSERT_DEBUG(mController && "Invalid Text Controller")
770 mController->SetControlInterface(this);
771 mController->SetAnchorControlInterface(this);
773 // Use height-for-width negotiation by default
774 self.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
775 self.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
777 // Enable highlightability
778 self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
780 // Enable the text ellipsis.
781 mController->SetTextElideEnabled(true); // If false then text larger than control will overflow
783 // Sets layoutDirection value
784 Dali::Stage stage = Dali::Stage::GetCurrent();
785 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(stage.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
786 mController->SetLayoutDirection(layoutDirection);
788 // Forward input events to controller
789 EnableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
790 GetTapGestureDetector().SetMaximumTapsRequired(1);
792 Layout::Engine& engine = mController->GetLayoutEngine();
793 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
795 DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
796 return std::unique_ptr<Dali::Accessibility::Accessible>(
797 new AccessibleImpl(actor, Dali::Accessibility::Role::LABEL));
801 void TextLabel::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
803 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
807 case StyleChange::DEFAULT_FONT_CHANGE:
809 // Property system did not set the font so should update it.
810 const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
811 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str());
812 mController->UpdateAfterFontChange(newFont);
816 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
818 GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
822 case StyleChange::THEME_CHANGE:
824 // Nothing to do, let control base class handle this
829 // Up call to Control
830 Control::OnStyleChange(styleManager, change);
833 void TextLabel::OnTap(const TapGesture& gesture)
835 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnTap %p\n", mController.Get());
837 // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
839 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
840 const Vector2& localPoint = gesture.GetLocalPoint();
841 mController->AnchorEvent(localPoint.x - padding.start, localPoint.y - padding.top);
844 void TextLabel::AnchorClicked(const std::string& href)
846 Dali::Toolkit::TextLabel handle(GetOwner());
847 mAnchorClickedSignal.Emit(handle, href.c_str(), href.length());
850 Vector3 TextLabel::GetNaturalSize()
853 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
855 Vector3 naturalSize = mController->GetNaturalSize();
856 naturalSize.width += (padding.start + padding.end);
857 naturalSize.height += (padding.top + padding.bottom);
862 float TextLabel::GetHeightForWidth(float width)
865 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
867 return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
870 void TextLabel::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
872 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index);
876 case Toolkit::TextLabel::Property::TEXT_COLOR:
878 const Vector4& textColor = propertyValue.Get<Vector4>();
879 if(mController->GetDefaultColor() != textColor)
881 mController->SetDefaultColor(textColor);
882 mTextUpdateNeeded = true;
888 Control::OnPropertySet(index, propertyValue); // up call to control for non-handled properties
894 void TextLabel::OnRelayout(const Vector2& size, RelayoutContainer& container)
896 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout\n");
899 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
901 Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
903 if(mController->IsTextFitEnabled())
905 mController->FitPointSizeforLayout(contentSize);
906 mController->SetTextFitContentSize(contentSize);
909 // Support Right-To-Left
910 Dali::LayoutDirection::Type layoutDirection;
911 if(mController->IsMatchSystemLanguageDirection())
913 layoutDirection = static_cast<Dali::LayoutDirection::Type>(DevelWindow::Get(Self()).GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
917 layoutDirection = static_cast<Dali::LayoutDirection::Type>(Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
919 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
921 if((Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType)) || mTextUpdateNeeded)
923 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", (mController->IsAutoScrollEnabled()) ? "true" : "false", this);
926 TextVisual::EnableRendererUpdate(mVisual);
928 // Support Right-To-Left of padding
929 if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
931 std::swap(padding.start, padding.end);
934 // Calculate the size of the visual that can fit the text
935 Size layoutSize = mController->GetTextModel()->GetLayoutSize();
936 layoutSize.x = contentSize.x;
938 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
939 if(shadowOffset.y > Math::MACHINE_EPSILON_1)
941 layoutSize.y += shadowOffset.y;
944 float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
945 layoutSize.y += outlineWidth * 2.0f;
946 layoutSize.y = std::min(layoutSize.y, contentSize.y);
948 // Calculate the offset for vertical alignment only, as the layout engine will do the horizontal alignment.
949 Vector2 alignmentOffset;
950 alignmentOffset.x = 0.0f;
951 alignmentOffset.y = (contentSize.y - layoutSize.y) * VERTICAL_ALIGNMENT_TABLE[mController->GetVerticalAlignment()];
953 Property::Map visualTransform;
954 visualTransform.Add(Toolkit::Visual::Transform::Property::SIZE, layoutSize)
955 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
956 .Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2(padding.start, padding.top) + alignmentOffset)
957 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
958 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
959 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN);
960 mVisual.SetTransformAndSize(visualTransform, size);
962 if(mController->IsAutoScrollEnabled())
964 SetUpAutoScrolling();
967 mTextUpdateNeeded = false;
971 void TextLabel::RequestTextRelayout()
974 // Signal that a Relayout may be needed
977 void TextLabel::SetUpAutoScrolling()
979 const Size& controlSize = mController->GetView().GetControlSize();
980 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.
981 const Text::CharacterDirection direction = mController->GetAutoScrollDirection();
983 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n", textNaturalSize.x, textNaturalSize.y, controlSize.x, controlSize.y);
987 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
989 // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
990 mTextScroller = Text::TextScroller::New(*this);
993 // Calculate the actual gap before scrolling wraps.
994 int textPadding = std::max(controlSize.x - textNaturalSize.x, 0.0f);
995 float wrapGap = std::max(mTextScroller->GetGap(), textPadding);
996 Vector2 textureSize = textNaturalSize + Vector2(wrapGap, 0.0f); // Add the gap as a part of the texture
998 // Create a texture of the text for scrolling
999 Size verifiedSize = textureSize;
1000 const int maxTextureSize = Dali::GetMaxTextureSize();
1002 //if the texture size width exceed maxTextureSize, modify the visual model size and enabled the ellipsis
1003 bool actualellipsis = mController->IsTextElideEnabled();
1004 if(verifiedSize.width > maxTextureSize)
1006 verifiedSize.width = maxTextureSize;
1007 if(textNaturalSize.width > maxTextureSize)
1009 mController->SetTextElideEnabled(true);
1011 GetHeightForWidth(maxTextureSize);
1012 wrapGap = std::max(maxTextureSize - textNaturalSize.width, 0.0f);
1015 Text::TypesetterPtr typesetter = Text::Typesetter::New(mController->GetTextModel());
1017 PixelData data = typesetter->Render(verifiedSize, mController->GetTextDirection(), Text::Typesetter::RENDER_TEXT_AND_STYLES, true, Pixel::RGBA8888); // ignore the horizontal alignment
1018 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
1019 data.GetPixelFormat(),
1022 texture.Upload(data);
1024 TextureSet textureSet = TextureSet::New();
1025 textureSet.SetTexture(0u, texture);
1027 // Filter mode needs to be set to linear to produce better quality while scaling.
1028 Sampler sampler = Sampler::New();
1029 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
1030 sampler.SetWrapMode(Dali::WrapMode::DEFAULT, Dali::WrapMode::REPEAT, Dali::WrapMode::DEFAULT); // Wrap the texture in the x direction
1031 textureSet.SetSampler(0u, sampler);
1033 // Set parameters for scrolling
1034 Renderer renderer = static_cast<Internal::Visual::Base&>(GetImplementation(mVisual)).GetRenderer();
1035 mTextScroller->SetParameters(Self(), renderer, textureSet, controlSize, verifiedSize, wrapGap, direction, mController->GetHorizontalAlignment(), mController->GetVerticalAlignment());
1036 mController->SetTextElideEnabled(actualellipsis);
1039 void TextLabel::ScrollingFinished()
1041 // Pure Virtual from TextScroller Interface
1042 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
1043 mController->SetAutoScrollEnabled(false);
1044 RequestTextRelayout();
1047 TextLabel::TextLabel()
1048 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
1049 mRenderingBackend(DEFAULT_RENDERING_BACKEND),
1050 mTextUpdateNeeded(false)
1054 TextLabel::~TextLabel()
1058 std::string TextLabel::AccessibleImpl::GetNameRaw()
1060 auto slf = Toolkit::TextLabel::DownCast(Self());
1061 return slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1064 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1066 return Toolkit::TextLabel::Property::TEXT;
1069 std::string TextLabel::AccessibleImpl::GetText(size_t startOffset,
1072 if(endOffset <= startOffset)
1075 auto slf = Toolkit::TextLabel::DownCast(Self());
1077 slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1079 if(startOffset > txt.size() || endOffset > txt.size())
1082 return txt.substr(startOffset, endOffset - startOffset);
1085 size_t TextLabel::AccessibleImpl::GetCharacterCount()
1087 auto slf = Toolkit::TextLabel::DownCast(Self());
1089 slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1094 size_t TextLabel::AccessibleImpl::GetCaretOffset()
1099 bool TextLabel::AccessibleImpl::SetCaretOffset(size_t offset)
1104 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(
1105 size_t offset, Dali::Accessibility::TextBoundary boundary)
1107 auto slf = Toolkit::TextLabel::DownCast(Self());
1108 auto txt = slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1109 auto txt_size = txt.size();
1111 auto range = Dali::Accessibility::Range{};
1115 case Dali::Accessibility::TextBoundary::CHARACTER:
1117 if(offset < txt_size)
1119 range.content = txt[offset];
1120 range.startOffset = offset;
1121 range.endOffset = offset + 1;
1125 case Dali::Accessibility::TextBoundary::WORD:
1126 case Dali::Accessibility::TextBoundary::LINE:
1128 auto txt_c_string = txt.c_str();
1129 auto breaks = std::vector<char>(txt_size, 0);
1130 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1131 Accessibility::Accessible::FindWordSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1133 Accessibility::Accessible::FindLineSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1136 while(index < txt_size && counter <= offset)
1141 while(breaks[index])
1147 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1149 if(boundary == Dali::Accessibility::TextBoundary::LINE)
1152 if((counter > 0) && ((counter - 1) == offset))
1154 range.content = txt.substr(start, index - start + 1);
1155 range.startOffset = start;
1156 range.endOffset = index + 1;
1158 if(boundary == Dali::Accessibility::TextBoundary::LINE)
1163 case Dali::Accessibility::TextBoundary::SENTENCE:
1165 /* not supported by efl */
1168 case Dali::Accessibility::TextBoundary::PARAGRAPH:
1170 /* Paragraph is not supported by libunibreak library */
1180 Dali::Accessibility::Range
1181 TextLabel::AccessibleImpl::GetSelection(size_t selectionNum)
1183 // Since DALi supports only one selection indexes higher than 0 are ignored
1184 if(selectionNum > 0)
1187 auto slf = Toolkit::TextLabel::DownCast(Self());
1188 auto ctrl = Dali::Toolkit::GetImpl(slf).getController();
1190 ctrl->RetrieveSelection(ret);
1191 auto r = ctrl->GetSelectionIndexes();
1193 return {static_cast<size_t>(r.first), static_cast<size_t>(r.second), ret};
1196 bool TextLabel::AccessibleImpl::RemoveSelection(size_t selectionNum)
1198 // Since DALi supports only one selection indexes higher than 0 are ignored
1199 if(selectionNum > 0)
1202 auto slf = Toolkit::TextLabel::DownCast(Self());
1203 Dali::Toolkit::GetImpl(slf).getController()->SetSelection(0, 0);
1207 bool TextLabel::AccessibleImpl::SetSelection(size_t selectionNum,
1211 // Since DALi supports only one selection indexes higher than 0 are ignored
1212 if(selectionNum > 0)
1215 auto slf = Toolkit::TextLabel::DownCast(Self());
1216 Dali::Toolkit::GetImpl(slf).getController()->SetSelection(startOffset,
1221 } // namespace Internal
1223 } // namespace Toolkit