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/actors/actor-devel.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/window-devel.h>
25 #include <dali/devel-api/common/stage.h>
26 #include <dali/devel-api/object/property-helper-devel.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/common/dali-common.h>
29 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
33 #include <dali-toolkit/devel-api/text/rendering-backend.h>
34 #include <dali-toolkit/internal/styling/style-manager-impl.h>
35 #include <dali-toolkit/internal/text/property-string-parser.h>
36 #include <dali-toolkit/internal/text/rendering/text-backend.h>
37 #include <dali-toolkit/internal/text/text-definitions.h>
38 #include <dali-toolkit/internal/text/text-effects-style.h>
39 #include <dali-toolkit/internal/text/text-font-style.h>
40 #include <dali-toolkit/internal/text/text-view.h>
41 #include <dali-toolkit/public-api/text/text-enumerations.h>
43 #include <dali-toolkit/devel-api/controls/control-devel.h>
44 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
45 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
46 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
47 #include <dali-toolkit/public-api/align-enumerations.h>
48 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
49 #include <dali-toolkit/public-api/visuals/visual-properties.h>
51 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
53 using namespace Dali::Toolkit::Text;
63 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
66 * @brief How the text visual should be aligned vertically inside the control.
68 * 0.0f aligns the text to the top, 0.5f aligns the text to the center, 1.0f aligns the text to the bottom.
69 * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
71 const float VERTICAL_ALIGNMENT_TABLE[Text::VerticalAlignment::BOTTOM + 1] =
73 0.0f, // VerticalAlignment::TOP
74 0.5f, // VerticalAlignment::CENTER
75 1.0f // VerticalAlignment::BOTTOM
78 const std::string TEXT_FIT_ENABLE_KEY("enable");
79 const std::string TEXT_FIT_MIN_SIZE_KEY("minSize");
80 const std::string TEXT_FIT_MAX_SIZE_KEY("maxSize");
81 const std::string TEXT_FIT_STEP_SIZE_KEY("stepSize");
82 const std::string TEXT_FIT_FONT_SIZE_TYPE_KEY("fontSizeType");
84 #if defined(DEBUG_ENABLED)
85 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
88 const Scripting::StringEnum AUTO_SCROLL_STOP_MODE_TABLE[] =
90 {"IMMEDIATE", Toolkit::TextLabel::AutoScrollStopMode::IMMEDIATE},
91 {"FINISH_LOOP", Toolkit::TextLabel::AutoScrollStopMode::FINISH_LOOP},
93 const unsigned int AUTO_SCROLL_STOP_MODE_TABLE_COUNT = sizeof(AUTO_SCROLL_STOP_MODE_TABLE) / sizeof(AUTO_SCROLL_STOP_MODE_TABLE[0]);
98 return Toolkit::TextLabel::New();
102 // Setup properties, signals and actions using the type-registry.
103 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::TextLabel, Toolkit::Control, Create);
105 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "text", STRING, TEXT )
106 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "fontFamily", STRING, FONT_FAMILY )
107 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "fontStyle", MAP, FONT_STYLE )
108 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "pointSize", FLOAT, POINT_SIZE )
109 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
110 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "horizontalAlignment", STRING, HORIZONTAL_ALIGNMENT )
111 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "verticalAlignment", STRING, VERTICAL_ALIGNMENT )
112 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "enableMarkup", BOOLEAN, ENABLE_MARKUP )
113 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "enableAutoScroll", BOOLEAN, ENABLE_AUTO_SCROLL )
114 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollSpeed", INTEGER, AUTO_SCROLL_SPEED )
115 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollLoopCount", INTEGER, AUTO_SCROLL_LOOP_COUNT )
116 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollGap", FLOAT, AUTO_SCROLL_GAP )
117 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "lineSpacing", FLOAT, LINE_SPACING )
118 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "underline", MAP, UNDERLINE )
119 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "shadow", MAP, SHADOW )
120 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "emboss", MAP, EMBOSS )
121 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "outline", MAP, OUTLINE )
122 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "pixelSize", FLOAT, PIXEL_SIZE )
123 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "ellipsis", BOOLEAN, ELLIPSIS )
124 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollLoopDelay", FLOAT, AUTO_SCROLL_LOOP_DELAY )
125 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "autoScrollStopMode", STRING, AUTO_SCROLL_STOP_MODE )
126 DALI_PROPERTY_REGISTRATION_READ_ONLY(Toolkit, TextLabel, "lineCount", INTEGER, LINE_COUNT )
127 DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "lineWrapMode", INTEGER, LINE_WRAP_MODE )
128 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY(Toolkit, TextLabel, "textDirection", INTEGER, TEXT_DIRECTION )
129 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "verticalLineAlignment", INTEGER, VERTICAL_LINE_ALIGNMENT )
130 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "textBackground", MAP, BACKGROUND )
131 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "ignoreSpacesAfterText", BOOLEAN, IGNORE_SPACES_AFTER_TEXT )
132 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "matchSystemLanguageDirection", BOOLEAN, MATCH_SYSTEM_LANGUAGE_DIRECTION)
133 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "textFit", MAP, TEXT_FIT )
134 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "minLineSize", FLOAT, MIN_LINE_SIZE )
135 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit, TextLabel, "renderingBackend", INTEGER, RENDERING_BACKEND )
136 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)
144 DALI_SIGNAL_REGISTRATION(Toolkit, TextLabel, "anchorClicked", SIGNAL_ANCHOR_CLICKED)
146 DALI_TYPE_REGISTRATION_END()
149 /// Parses the property map for the TEXT_FIT property
150 void ParseTextFitProperty(Text::ControllerPtr& controller, const Property::Map* propertiesMap)
152 if(propertiesMap && !propertiesMap->Empty())
154 bool enabled = false;
157 float stepSize = 0.f;
158 bool isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
159 Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
161 const unsigned int numberOfItems = propertiesMap->Count();
163 // Parses and applies
164 for(unsigned int index = 0u; index < numberOfItems; ++index)
166 const KeyValuePair& valueGet = propertiesMap->GetKeyValue(index);
168 if((Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey) || (TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey))
171 enabled = valueGet.second.Get<bool>();
173 else if((Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey))
176 minSize = valueGet.second.Get<float>();
179 else if((Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey))
182 maxSize = valueGet.second.Get<float>();
185 else if((Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey) || (TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey))
188 stepSize = valueGet.second.Get<float>();
189 isStepSizeSet = true;
191 else if((Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey) || (TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey))
193 if("pixelSize" == valueGet.second.Get<std::string>())
195 type = Controller::FontSizeType::PIXEL_SIZE;
200 controller->SetTextFitEnabled(enabled);
203 controller->SetTextFitMinSize(minSize, type);
207 controller->SetTextFitMaxSize(maxSize, type);
211 controller->SetTextFitStepSize(stepSize, type);
218 Toolkit::TextLabel TextLabel::New()
220 // Create the implementation, temporarily owned by this handle on stack
221 IntrusivePtr<TextLabel> impl = new TextLabel();
223 // Pass ownership to CustomActor handle
224 Toolkit::TextLabel handle(*impl);
226 // Second-phase init of the implementation
227 // This can only be done after the CustomActor connection has been made...
233 void TextLabel::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
235 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
239 TextLabel& impl(GetImpl(label));
240 DALI_ASSERT_ALWAYS(impl.mController && "No text contoller");
244 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
246 int backend = value.Get<int>();
248 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
249 if(DevelText::RENDERING_VECTOR_BASED == backend)
251 backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
254 if(impl.mRenderingBackend != backend)
256 impl.mRenderingBackend = backend;
257 impl.mTextUpdateNeeded = true;
259 // When using the vector-based rendering, the size of the GLyphs are different
260 TextAbstraction::GlyphType glyphType = (DevelText::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
261 impl.mController->SetGlyphType(glyphType);
265 case Toolkit::TextLabel::Property::TEXT:
267 impl.mController->SetText(value.Get<std::string>());
270 case Toolkit::TextLabel::Property::FONT_FAMILY:
272 const std::string& fontFamily = value.Get<std::string>();
274 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str());
275 impl.mController->SetDefaultFontFamily(fontFamily);
278 case Toolkit::TextLabel::Property::FONT_STYLE:
280 SetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
283 case Toolkit::TextLabel::Property::POINT_SIZE:
285 const float pointSize = value.Get<float>();
287 if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
289 impl.mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
293 case Toolkit::TextLabel::Property::MULTI_LINE:
295 impl.mController->SetMultiLineEnabled(value.Get<bool>());
298 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
300 Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
301 if(Text::GetHorizontalAlignmentEnumeration(value, alignment))
303 impl.mController->SetHorizontalAlignment(alignment);
307 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
309 Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
310 if(Text::GetVerticalAlignmentEnumeration(value, alignment))
312 impl.mController->SetVerticalAlignment(alignment);
316 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
318 const bool enableMarkup = value.Get<bool>();
319 impl.mController->SetMarkupProcessorEnabled(enableMarkup);
322 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
324 const bool enableAutoScroll = value.Get<bool>();
325 // If request to auto scroll is the same as current state then do nothing.
326 if(enableAutoScroll != impl.mController->IsAutoScrollEnabled())
328 // If request is disable (false) and auto scrolling is enabled then need to stop it
329 if(enableAutoScroll == false)
331 if(impl.mTextScroller)
333 impl.mTextScroller->StopScrolling();
336 // If request is enable (true) then start autoscroll as not already running
339 impl.mController->SetAutoScrollEnabled(enableAutoScroll);
344 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
346 Text::TextScrollerPtr textScroller = impl.GetTextScroller();
347 Toolkit::TextLabel::AutoScrollStopMode::Type stopMode = textScroller->GetStopMode();
348 if(Scripting::GetEnumerationProperty<Toolkit::TextLabel::AutoScrollStopMode::Type>(value,
349 AUTO_SCROLL_STOP_MODE_TABLE,
350 AUTO_SCROLL_STOP_MODE_TABLE_COUNT,
353 textScroller->SetStopMode(stopMode);
357 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
359 impl.GetTextScroller()->SetSpeed(value.Get<int>());
362 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
364 impl.GetTextScroller()->SetLoopCount(value.Get<int>());
367 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
369 impl.GetTextScroller()->SetLoopDelay(value.Get<float>());
372 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
374 impl.GetTextScroller()->SetGap(value.Get<float>());
377 case Toolkit::TextLabel::Property::LINE_SPACING:
379 const float lineSpacing = value.Get<float>();
380 impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSpacing(lineSpacing) || impl.mTextUpdateNeeded;
383 case Toolkit::TextLabel::Property::UNDERLINE:
385 impl.mTextUpdateNeeded = SetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
388 case Toolkit::TextLabel::Property::SHADOW:
390 impl.mTextUpdateNeeded = SetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
393 case Toolkit::TextLabel::Property::EMBOSS:
395 impl.mTextUpdateNeeded = SetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
398 case Toolkit::TextLabel::Property::OUTLINE:
400 impl.mTextUpdateNeeded = SetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
403 case Toolkit::TextLabel::Property::PIXEL_SIZE:
405 const float pixelSize = value.Get<float>();
406 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize);
408 if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE), pixelSize))
410 impl.mController->SetDefaultFontSize(pixelSize, Text::Controller::PIXEL_SIZE);
414 case Toolkit::TextLabel::Property::ELLIPSIS:
416 const bool ellipsis = value.Get<bool>();
417 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis);
419 impl.mController->SetTextElideEnabled(ellipsis);
422 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
424 Text::LineWrap::Mode lineWrapMode(static_cast<Text::LineWrap::Mode>(-1)); // Set to invalid value to ensure a valid mode does get set
425 if(GetLineWrapModeEnumeration(value, lineWrapMode))
427 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode);
428 impl.mController->SetLineWrapMode(lineWrapMode);
432 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
434 if(impl.mController->GetTextModel())
436 DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>(value.Get<int>());
438 impl.mController->SetVerticalLineAlignment(alignment);
440 // Property doesn't affect the layout, only Visual must be updated
441 TextVisual::EnableRendererUpdate(impl.mVisual);
443 // No need to trigger full re-layout. Instead call UpdateRenderer() directly
444 TextVisual::UpdateRenderer(impl.mVisual);
448 case Toolkit::DevelTextLabel::Property::BACKGROUND:
450 impl.mTextUpdateNeeded = SetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
453 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
455 impl.mController->SetIgnoreSpacesAfterText(value.Get<bool>());
458 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
460 impl.mController->SetMatchSystemLanguageDirection(value.Get<bool>());
463 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
465 ParseTextFitProperty(impl.mController, value.GetMap());
468 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
470 const float lineSize = value.Get<float>();
471 impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSize(lineSize) || impl.mTextUpdateNeeded;
474 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
476 const float scale = value.Get<float>();
477 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p FONT_SIZE_SCALE %f\n", impl.mController.Get(), scale);
479 if(!Equals(impl.mController->GetFontSizeScale(), scale))
481 impl.mController->SetFontSizeScale(scale);
487 // Request relayout when text update is needed. It's necessary to call it
488 // as changing the property not via UI interaction brings no effect if only
489 // the mTextUpdateNeeded is changed.
490 if(impl.mTextUpdateNeeded)
492 // need to request relayout as size of text may have changed
493 impl.RequestTextRelayout();
498 Text::ControllerPtr TextLabel::getController()
503 Property::Value TextLabel::GetProperty(BaseObject* object, Property::Index index)
505 Property::Value value;
507 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
511 TextLabel& impl(GetImpl(label));
512 DALI_ASSERT_DEBUG(impl.mController && "No text contoller");
516 case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
518 value = impl.mRenderingBackend;
521 case Toolkit::TextLabel::Property::TEXT:
524 impl.mController->GetText(text);
528 case Toolkit::TextLabel::Property::FONT_FAMILY:
530 value = impl.mController->GetDefaultFontFamily();
533 case Toolkit::TextLabel::Property::FONT_STYLE:
535 GetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
538 case Toolkit::TextLabel::Property::POINT_SIZE:
540 value = impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE);
543 case Toolkit::TextLabel::Property::MULTI_LINE:
545 value = impl.mController->IsMultiLineEnabled();
548 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
550 const char* name = Text::GetHorizontalAlignmentString(impl.mController->GetHorizontalAlignment());
554 value = std::string(name);
558 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
560 const char* name = Text::GetVerticalAlignmentString(impl.mController->GetVerticalAlignment());
563 value = std::string(name);
567 case Toolkit::TextLabel::Property::ENABLE_MARKUP:
569 value = impl.mController->IsMarkupProcessorEnabled();
572 case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
574 value = impl.mController->IsAutoScrollEnabled();
577 case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
579 if(impl.mTextScroller)
581 const char* mode = Scripting::GetEnumerationName<Toolkit::TextLabel::AutoScrollStopMode::Type>(impl.mTextScroller->GetStopMode(),
582 AUTO_SCROLL_STOP_MODE_TABLE,
583 AUTO_SCROLL_STOP_MODE_TABLE_COUNT);
586 value = std::string(mode);
591 case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
593 if(impl.mTextScroller)
595 value = impl.mTextScroller->GetSpeed();
599 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
601 if(impl.mTextScroller)
603 value = impl.mTextScroller->GetLoopCount();
607 case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
609 if(impl.mTextScroller)
611 value = impl.mTextScroller->GetLoopDelay();
615 case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
617 if(impl.mTextScroller)
619 value = impl.mTextScroller->GetGap();
623 case Toolkit::TextLabel::Property::LINE_SPACING:
625 value = impl.mController->GetDefaultLineSpacing();
628 case Toolkit::TextLabel::Property::UNDERLINE:
630 GetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
633 case Toolkit::TextLabel::Property::SHADOW:
635 GetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
638 case Toolkit::TextLabel::Property::EMBOSS:
640 GetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
643 case Toolkit::TextLabel::Property::OUTLINE:
645 GetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
648 case Toolkit::TextLabel::Property::PIXEL_SIZE:
650 value = impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE);
653 case Toolkit::TextLabel::Property::ELLIPSIS:
655 value = impl.mController->IsTextElideEnabled();
658 case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
660 value = impl.mController->GetLineWrapMode();
663 case Toolkit::TextLabel::Property::LINE_COUNT:
665 float width = label.GetProperty(Actor::Property::SIZE_WIDTH).Get<float>();
666 value = impl.mController->GetLineCount(width);
669 case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
671 value = impl.mController->GetTextDirection();
674 case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
676 value = impl.mController->GetVerticalLineAlignment();
679 case Toolkit::DevelTextLabel::Property::BACKGROUND:
681 GetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
684 case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
686 value = impl.mController->IsIgnoreSpacesAfterText();
689 case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
691 value = impl.mController->IsMatchSystemLanguageDirection();
694 case Toolkit::DevelTextLabel::Property::TEXT_FIT:
696 const bool enabled = impl.mController->IsTextFitEnabled();
697 const float minSize = impl.mController->GetTextFitMinSize();
698 const float maxSize = impl.mController->GetTextFitMaxSize();
699 const float stepSize = impl.mController->GetTextFitStepSize();
702 map.Insert(TEXT_FIT_ENABLE_KEY, enabled);
703 map.Insert(TEXT_FIT_MIN_SIZE_KEY, minSize);
704 map.Insert(TEXT_FIT_MAX_SIZE_KEY, maxSize);
705 map.Insert(TEXT_FIT_STEP_SIZE_KEY, stepSize);
706 map.Insert(TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize");
711 case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
713 value = impl.mController->GetDefaultLineSize();
716 case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
718 value = impl.mController->GetFontSizeScale();
727 bool TextLabel::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
729 Dali::BaseHandle handle(object);
731 bool connected(true);
732 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(handle);
734 if(0 == strcmp(signalName.c_str(), SIGNAL_ANCHOR_CLICKED))
738 Internal::TextLabel& labelImpl(GetImpl(label));
739 labelImpl.AnchorClickedSignal().Connect(tracker, functor);
744 // signalName does not match any signal
751 DevelTextLabel::AnchorClickedSignalType& TextLabel::AnchorClickedSignal()
753 return mAnchorClickedSignal;
756 void TextLabel::OnInitialize()
760 Property::Map propertyMap;
761 propertyMap.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
763 mVisual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
764 DevelControl::RegisterVisual(*this, Toolkit::TextLabel::Property::TEXT, mVisual);
766 TextVisual::SetAnimatableTextColorProperty(mVisual, Toolkit::TextLabel::Property::TEXT_COLOR);
768 mController = TextVisual::GetController(mVisual);
769 DALI_ASSERT_DEBUG(mController && "Invalid Text Controller")
771 mController->SetControlInterface(this);
772 mController->SetAnchorControlInterface(this);
774 // Use height-for-width negotiation by default
775 self.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
776 self.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
778 // Enable highlightability
779 self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
781 // Enable the text ellipsis.
782 mController->SetTextElideEnabled(true); // If false then text larger than control will overflow
784 // Sets layoutDirection value
785 Dali::Stage stage = Dali::Stage::GetCurrent();
786 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(stage.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
787 mController->SetLayoutDirection(layoutDirection);
789 // Forward input events to controller
790 EnableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
791 GetTapGestureDetector().SetMaximumTapsRequired(1);
793 Layout::Engine& engine = mController->GetLayoutEngine();
794 engine.SetCursorWidth(0u); // Do not layout space for the cursor.
796 DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
797 return std::unique_ptr<Dali::Accessibility::Accessible>(
798 new AccessibleImpl(actor, Dali::Accessibility::Role::LABEL));
802 void TextLabel::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
804 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
808 case StyleChange::DEFAULT_FONT_CHANGE:
810 // Property system did not set the font so should update it.
811 const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
812 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str());
813 mController->UpdateAfterFontChange(newFont);
817 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
819 GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
823 case StyleChange::THEME_CHANGE:
825 // Nothing to do, let control base class handle this
830 // Up call to Control
831 Control::OnStyleChange(styleManager, change);
834 void TextLabel::OnTap(const TapGesture& gesture)
836 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnTap %p\n", mController.Get());
838 // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
840 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
841 const Vector2& localPoint = gesture.GetLocalPoint();
842 mController->AnchorEvent(localPoint.x - padding.start, localPoint.y - padding.top);
844 // parents can also listen for tap gesture events
845 Dali::DevelActor::SetNeedGesturePropagation(Self(), true);
848 void TextLabel::AnchorClicked(const std::string& href)
850 Dali::Toolkit::TextLabel handle(GetOwner());
851 mAnchorClickedSignal.Emit(handle, href.c_str(), href.length());
854 Vector3 TextLabel::GetNaturalSize()
857 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
859 Vector3 naturalSize = mController->GetNaturalSize();
860 naturalSize.width += (padding.start + padding.end);
861 naturalSize.height += (padding.top + padding.bottom);
866 float TextLabel::GetHeightForWidth(float width)
869 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
871 return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
874 void TextLabel::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
876 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index);
880 case Toolkit::TextLabel::Property::TEXT_COLOR:
882 const Vector4& textColor = propertyValue.Get<Vector4>();
883 if(mController->GetDefaultColor() != textColor)
885 mController->SetDefaultColor(textColor);
886 mTextUpdateNeeded = true;
892 Control::OnPropertySet(index, propertyValue); // up call to control for non-handled properties
898 void TextLabel::OnRelayout(const Vector2& size, RelayoutContainer& container)
900 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout\n");
903 padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
905 Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
907 if(mController->IsTextFitEnabled())
909 mController->FitPointSizeforLayout(contentSize);
910 mController->SetTextFitContentSize(contentSize);
913 // Support Right-To-Left
914 Dali::LayoutDirection::Type layoutDirection;
915 if(mController->IsMatchSystemLanguageDirection())
917 layoutDirection = static_cast<Dali::LayoutDirection::Type>(DevelWindow::Get(Self()).GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
921 layoutDirection = static_cast<Dali::LayoutDirection::Type>(Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
923 const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
925 if((Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType)) || mTextUpdateNeeded)
927 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", (mController->IsAutoScrollEnabled()) ? "true" : "false", this);
930 TextVisual::EnableRendererUpdate(mVisual);
932 // Support Right-To-Left of padding
933 if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
935 std::swap(padding.start, padding.end);
938 // Calculate the size of the visual that can fit the text
939 Size layoutSize = mController->GetTextModel()->GetLayoutSize();
940 layoutSize.x = contentSize.x;
942 const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
943 if(shadowOffset.y > Math::MACHINE_EPSILON_1)
945 layoutSize.y += shadowOffset.y;
948 float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
949 layoutSize.y += outlineWidth * 2.0f;
950 layoutSize.y = std::min(layoutSize.y, contentSize.y);
952 // Calculate the offset for vertical alignment only, as the layout engine will do the horizontal alignment.
953 Vector2 alignmentOffset;
954 alignmentOffset.x = 0.0f;
955 alignmentOffset.y = (contentSize.y - layoutSize.y) * VERTICAL_ALIGNMENT_TABLE[mController->GetVerticalAlignment()];
957 Property::Map visualTransform;
958 visualTransform.Add(Toolkit::Visual::Transform::Property::SIZE, layoutSize)
959 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
960 .Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2(padding.start, padding.top) + alignmentOffset)
961 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
962 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
963 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN);
964 mVisual.SetTransformAndSize(visualTransform, size);
966 if(mController->IsAutoScrollEnabled())
968 SetUpAutoScrolling();
971 mTextUpdateNeeded = false;
975 void TextLabel::RequestTextRelayout()
978 // Signal that a Relayout may be needed
981 void TextLabel::SetUpAutoScrolling()
983 const Size& controlSize = mController->GetView().GetControlSize();
984 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.
985 const Text::CharacterDirection direction = mController->GetAutoScrollDirection();
987 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n", textNaturalSize.x, textNaturalSize.y, controlSize.x, controlSize.y);
991 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
993 // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
994 mTextScroller = Text::TextScroller::New(*this);
997 // Calculate the actual gap before scrolling wraps.
998 int textPadding = std::max(controlSize.x - textNaturalSize.x, 0.0f);
999 float wrapGap = std::max(mTextScroller->GetGap(), textPadding);
1000 Vector2 textureSize = textNaturalSize + Vector2(wrapGap, 0.0f); // Add the gap as a part of the texture
1002 // Create a texture of the text for scrolling
1003 Size verifiedSize = textureSize;
1004 const int maxTextureSize = Dali::GetMaxTextureSize();
1006 //if the texture size width exceed maxTextureSize, modify the visual model size and enabled the ellipsis
1007 bool actualellipsis = mController->IsTextElideEnabled();
1008 if(verifiedSize.width > maxTextureSize)
1010 verifiedSize.width = maxTextureSize;
1011 if(textNaturalSize.width > maxTextureSize)
1013 mController->SetTextElideEnabled(true);
1015 GetHeightForWidth(maxTextureSize);
1016 wrapGap = std::max(maxTextureSize - textNaturalSize.width, 0.0f);
1019 Text::TypesetterPtr typesetter = Text::Typesetter::New(mController->GetTextModel());
1021 PixelData data = typesetter->Render(verifiedSize, mController->GetTextDirection(), Text::Typesetter::RENDER_TEXT_AND_STYLES, true, Pixel::RGBA8888); // ignore the horizontal alignment
1022 Texture texture = Texture::New(Dali::TextureType::TEXTURE_2D,
1023 data.GetPixelFormat(),
1026 texture.Upload(data);
1028 TextureSet textureSet = TextureSet::New();
1029 textureSet.SetTexture(0u, texture);
1031 // Filter mode needs to be set to linear to produce better quality while scaling.
1032 Sampler sampler = Sampler::New();
1033 sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
1034 sampler.SetWrapMode(Dali::WrapMode::DEFAULT, Dali::WrapMode::REPEAT, Dali::WrapMode::DEFAULT); // Wrap the texture in the x direction
1035 textureSet.SetSampler(0u, sampler);
1037 // Set parameters for scrolling
1038 Renderer renderer = static_cast<Internal::Visual::Base&>(GetImplementation(mVisual)).GetRenderer();
1039 mTextScroller->SetParameters(Self(), renderer, textureSet, controlSize, verifiedSize, wrapGap, direction, mController->GetHorizontalAlignment(), mController->GetVerticalAlignment());
1040 mController->SetTextElideEnabled(actualellipsis);
1043 void TextLabel::ScrollingFinished()
1045 // Pure Virtual from TextScroller Interface
1046 DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
1047 mController->SetAutoScrollEnabled(false);
1048 RequestTextRelayout();
1051 TextLabel::TextLabel()
1052 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
1053 mRenderingBackend(DEFAULT_RENDERING_BACKEND),
1054 mTextUpdateNeeded(false)
1058 TextLabel::~TextLabel()
1062 std::string TextLabel::AccessibleImpl::GetNameRaw()
1064 auto slf = Toolkit::TextLabel::DownCast(Self());
1065 return slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1068 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1070 return Toolkit::TextLabel::Property::TEXT;
1073 std::string TextLabel::AccessibleImpl::GetText(size_t startOffset,
1076 if(endOffset <= startOffset)
1079 auto slf = Toolkit::TextLabel::DownCast(Self());
1081 slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1083 if(startOffset > txt.size() || endOffset > txt.size())
1086 return txt.substr(startOffset, endOffset - startOffset);
1089 size_t TextLabel::AccessibleImpl::GetCharacterCount()
1091 auto slf = Toolkit::TextLabel::DownCast(Self());
1093 slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1098 size_t TextLabel::AccessibleImpl::GetCaretOffset()
1103 bool TextLabel::AccessibleImpl::SetCaretOffset(size_t offset)
1108 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(
1109 size_t offset, Dali::Accessibility::TextBoundary boundary)
1111 auto slf = Toolkit::TextLabel::DownCast(Self());
1112 auto txt = slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1113 auto txt_size = txt.size();
1115 auto range = Dali::Accessibility::Range{};
1119 case Dali::Accessibility::TextBoundary::CHARACTER:
1121 if(offset < txt_size)
1123 range.content = txt[offset];
1124 range.startOffset = offset;
1125 range.endOffset = offset + 1;
1129 case Dali::Accessibility::TextBoundary::WORD:
1130 case Dali::Accessibility::TextBoundary::LINE:
1132 auto txt_c_string = txt.c_str();
1133 auto breaks = std::vector<char>(txt_size, 0);
1134 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1135 Accessibility::Accessible::FindWordSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1137 Accessibility::Accessible::FindLineSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1140 while(index < txt_size && counter <= offset)
1145 while(breaks[index])
1151 if(boundary == Dali::Accessibility::TextBoundary::WORD)
1153 if(boundary == Dali::Accessibility::TextBoundary::LINE)
1156 if((counter > 0) && ((counter - 1) == offset))
1158 range.content = txt.substr(start, index - start + 1);
1159 range.startOffset = start;
1160 range.endOffset = index + 1;
1162 if(boundary == Dali::Accessibility::TextBoundary::LINE)
1167 case Dali::Accessibility::TextBoundary::SENTENCE:
1169 /* not supported by efl */
1172 case Dali::Accessibility::TextBoundary::PARAGRAPH:
1174 /* Paragraph is not supported by libunibreak library */
1184 Dali::Accessibility::Range
1185 TextLabel::AccessibleImpl::GetSelection(size_t selectionNum)
1187 // Since DALi supports only one selection indexes higher than 0 are ignored
1188 if(selectionNum > 0)
1191 auto slf = Toolkit::TextLabel::DownCast(Self());
1192 auto ctrl = Dali::Toolkit::GetImpl(slf).getController();
1194 ctrl->RetrieveSelection(ret);
1195 auto r = ctrl->GetSelectionIndexes();
1197 return {static_cast<size_t>(r.first), static_cast<size_t>(r.second), ret};
1200 bool TextLabel::AccessibleImpl::RemoveSelection(size_t selectionNum)
1202 // Since DALi supports only one selection indexes higher than 0 are ignored
1203 if(selectionNum > 0)
1206 auto slf = Toolkit::TextLabel::DownCast(Self());
1207 Dali::Toolkit::GetImpl(slf).getController()->SetSelection(0, 0);
1211 bool TextLabel::AccessibleImpl::SetSelection(size_t selectionNum,
1215 // Since DALi supports only one selection indexes higher than 0 are ignored
1216 if(selectionNum > 0)
1219 auto slf = Toolkit::TextLabel::DownCast(Self());
1220 Dali::Toolkit::GetImpl(slf).getController()->SetSelection(startOffset,
1225 } // namespace Internal
1227 } // namespace Toolkit