Merge "Dummy graphics controller for test suite" into devel/graphics
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
20
21 // EXTERNAL INCLUDES
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>
29
30 // INTERNAL INCLUDES
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>
41
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>
49
50 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
51
52 using namespace Dali::Toolkit::Text;
53
54 namespace Dali
55 {
56 namespace Toolkit
57 {
58 namespace Internal
59 {
60 namespace
61 {
62 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
63
64 /**
65  * @brief How the text visual should be aligned vertically inside the control.
66  *
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).
69  */
70 const float VERTICAL_ALIGNMENT_TABLE[Text::VerticalAlignment::BOTTOM + 1] =
71   {
72     0.0f, // VerticalAlignment::TOP
73     0.5f, // VerticalAlignment::CENTER
74     1.0f  // VerticalAlignment::BOTTOM
75 };
76
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");
82
83 #if defined(DEBUG_ENABLED)
84 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
85 #endif
86
87 const Scripting::StringEnum AUTO_SCROLL_STOP_MODE_TABLE[] =
88   {
89     {"IMMEDIATE", Toolkit::TextLabel::AutoScrollStopMode::IMMEDIATE},
90     {"FINISH_LOOP", Toolkit::TextLabel::AutoScrollStopMode::FINISH_LOOP},
91 };
92 const unsigned int AUTO_SCROLL_STOP_MODE_TABLE_COUNT = sizeof(AUTO_SCROLL_STOP_MODE_TABLE) / sizeof(AUTO_SCROLL_STOP_MODE_TABLE[0]);
93
94 // Type registration
95 BaseHandle Create()
96 {
97   return Toolkit::TextLabel::New();
98 }
99
100 // clang-format off
101 // Setup properties, signals and actions using the type-registry.
102 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::TextLabel, Toolkit::Control, Create);
103
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                )
136
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()
143 // clang-format on
144
145 /// Parses the property map for the TEXT_FIT property
146 void ParseTextFitProperty(Text::ControllerPtr& controller, const Property::Map* propertiesMap)
147 {
148   if(propertiesMap && !propertiesMap->Empty())
149   {
150     bool                     enabled      = false;
151     float                    minSize      = 0.f;
152     float                    maxSize      = 0.f;
153     float                    stepSize     = 0.f;
154     bool                     isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
155     Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
156
157     const unsigned int numberOfItems = propertiesMap->Count();
158
159     // Parses and applies
160     for(unsigned int index = 0u; index < numberOfItems; ++index)
161     {
162       const KeyValuePair& valueGet = propertiesMap->GetKeyValue(index);
163
164       if((Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey) || (TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey))
165       {
166         /// Enable key.
167         enabled = valueGet.second.Get<bool>();
168       }
169       else if((Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey))
170       {
171         /// min size.
172         minSize      = valueGet.second.Get<float>();
173         isMinSizeSet = true;
174       }
175       else if((Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey))
176       {
177         /// max size.
178         maxSize      = valueGet.second.Get<float>();
179         isMaxSizeSet = true;
180       }
181       else if((Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey) || (TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey))
182       {
183         /// step size.
184         stepSize      = valueGet.second.Get<float>();
185         isStepSizeSet = true;
186       }
187       else if((Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey) || (TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey))
188       {
189         if("pixelSize" == valueGet.second.Get<std::string>())
190         {
191           type = Controller::FontSizeType::PIXEL_SIZE;
192         }
193       }
194     }
195
196     controller->SetTextFitEnabled(enabled);
197     if(isMinSizeSet)
198     {
199       controller->SetTextFitMinSize(minSize, type);
200     }
201     if(isMaxSizeSet)
202     {
203       controller->SetTextFitMaxSize(maxSize, type);
204     }
205     if(isStepSizeSet)
206     {
207       controller->SetTextFitStepSize(stepSize, type);
208     }
209   }
210 }
211
212 } // namespace
213
214 Toolkit::TextLabel TextLabel::New()
215 {
216   // Create the implementation, temporarily owned by this handle on stack
217   IntrusivePtr<TextLabel> impl = new TextLabel();
218
219   // Pass ownership to CustomActor handle
220   Toolkit::TextLabel handle(*impl);
221
222   // Second-phase init of the implementation
223   // This can only be done after the CustomActor connection has been made...
224   impl->Initialize();
225
226   return handle;
227 }
228
229 void TextLabel::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
230 {
231   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
232
233   if(label)
234   {
235     TextLabel& impl(GetImpl(label));
236     DALI_ASSERT_ALWAYS(impl.mController && "No text contoller");
237
238     switch(index)
239     {
240       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
241       {
242         int backend = value.Get<int>();
243
244 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
245         if(DevelText::RENDERING_VECTOR_BASED == backend)
246         {
247           backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
248         }
249 #endif
250         if(impl.mRenderingBackend != backend)
251         {
252           impl.mRenderingBackend = backend;
253           impl.mTextUpdateNeeded = true;
254
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);
258         }
259         break;
260       }
261       case Toolkit::TextLabel::Property::TEXT:
262       {
263         impl.mController->SetText(value.Get<std::string>());
264         break;
265       }
266       case Toolkit::TextLabel::Property::FONT_FAMILY:
267       {
268         const std::string& fontFamily = value.Get<std::string>();
269
270         DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str());
271         impl.mController->SetDefaultFontFamily(fontFamily);
272         break;
273       }
274       case Toolkit::TextLabel::Property::FONT_STYLE:
275       {
276         SetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
277         break;
278       }
279       case Toolkit::TextLabel::Property::POINT_SIZE:
280       {
281         const float pointSize = value.Get<float>();
282
283         if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
284         {
285           impl.mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
286         }
287         break;
288       }
289       case Toolkit::TextLabel::Property::MULTI_LINE:
290       {
291         impl.mController->SetMultiLineEnabled(value.Get<bool>());
292         break;
293       }
294       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
295       {
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))
298         {
299           impl.mController->SetHorizontalAlignment(alignment);
300         }
301         break;
302       }
303       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
304       {
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))
307         {
308           impl.mController->SetVerticalAlignment(alignment);
309         }
310         break;
311       }
312       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
313       {
314         const bool enableMarkup = value.Get<bool>();
315         impl.mController->SetMarkupProcessorEnabled(enableMarkup);
316         break;
317       }
318       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
319       {
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())
323         {
324           // If request is disable (false) and auto scrolling is enabled then need to stop it
325           if(enableAutoScroll == false)
326           {
327             if(impl.mTextScroller)
328             {
329               impl.mTextScroller->StopScrolling();
330             }
331           }
332           // If request is enable (true) then start autoscroll as not already running
333           else
334           {
335             impl.mController->SetAutoScrollEnabled(enableAutoScroll);
336           }
337         }
338         break;
339       }
340       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
341       {
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,
347                                                                                            stopMode))
348         {
349           textScroller->SetStopMode(stopMode);
350         }
351         break;
352       }
353       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
354       {
355         impl.GetTextScroller()->SetSpeed(value.Get<int>());
356         break;
357       }
358       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
359       {
360         impl.GetTextScroller()->SetLoopCount(value.Get<int>());
361         break;
362       }
363       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
364       {
365         impl.GetTextScroller()->SetLoopDelay(value.Get<float>());
366         break;
367       }
368       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
369       {
370         impl.GetTextScroller()->SetGap(value.Get<float>());
371         break;
372       }
373       case Toolkit::TextLabel::Property::LINE_SPACING:
374       {
375         const float lineSpacing = value.Get<float>();
376         impl.mTextUpdateNeeded  = impl.mController->SetDefaultLineSpacing(lineSpacing) || impl.mTextUpdateNeeded;
377         break;
378       }
379       case Toolkit::TextLabel::Property::UNDERLINE:
380       {
381         impl.mTextUpdateNeeded = SetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
382         break;
383       }
384       case Toolkit::TextLabel::Property::SHADOW:
385       {
386         impl.mTextUpdateNeeded = SetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
387         break;
388       }
389       case Toolkit::TextLabel::Property::EMBOSS:
390       {
391         impl.mTextUpdateNeeded = SetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
392         break;
393       }
394       case Toolkit::TextLabel::Property::OUTLINE:
395       {
396         impl.mTextUpdateNeeded = SetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
397         break;
398       }
399       case Toolkit::TextLabel::Property::PIXEL_SIZE:
400       {
401         const float pixelSize = value.Get<float>();
402         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize);
403
404         if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE), pixelSize))
405         {
406           impl.mController->SetDefaultFontSize(pixelSize, Text::Controller::PIXEL_SIZE);
407         }
408         break;
409       }
410       case Toolkit::TextLabel::Property::ELLIPSIS:
411       {
412         const bool ellipsis = value.Get<bool>();
413         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis);
414
415         impl.mController->SetTextElideEnabled(ellipsis);
416         break;
417       }
418       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
419       {
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))
422         {
423           DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode);
424           impl.mController->SetLineWrapMode(lineWrapMode);
425         }
426         break;
427       }
428       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
429       {
430         if(impl.mController->GetTextModel())
431         {
432           DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>(value.Get<int>());
433
434           impl.mController->SetVerticalLineAlignment(alignment);
435
436           // Property doesn't affect the layout, only Visual must be updated
437           TextVisual::EnableRendererUpdate(impl.mVisual);
438
439           // No need to trigger full re-layout. Instead call UpdateRenderer() directly
440           TextVisual::UpdateRenderer(impl.mVisual);
441         }
442         break;
443       }
444       case Toolkit::DevelTextLabel::Property::BACKGROUND:
445       {
446         impl.mTextUpdateNeeded = SetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
447         break;
448       }
449       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
450       {
451         impl.mController->SetIgnoreSpacesAfterText(value.Get<bool>());
452         break;
453       }
454       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
455       {
456         impl.mController->SetMatchSystemLanguageDirection(value.Get<bool>());
457         break;
458       }
459       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
460       {
461         ParseTextFitProperty(impl.mController, value.GetMap());
462         break;
463       }
464       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
465       {
466         const float lineSize   = value.Get<float>();
467         impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSize(lineSize) || impl.mTextUpdateNeeded;
468         break;
469       }
470       case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
471       {
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);
474
475         if(!Equals(impl.mController->GetFontSizeScale(), scale))
476         {
477           impl.mController->SetFontSizeScale(scale);
478         }
479         break;
480       }
481     }
482
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)
487     {
488       // need to request relayout as size of text may have changed
489       impl.RequestTextRelayout();
490     }
491   }
492 }
493
494 Text::ControllerPtr TextLabel::getController()
495 {
496   return mController;
497 }
498
499 Property::Value TextLabel::GetProperty(BaseObject* object, Property::Index index)
500 {
501   Property::Value value;
502
503   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
504
505   if(label)
506   {
507     TextLabel& impl(GetImpl(label));
508     DALI_ASSERT_DEBUG(impl.mController && "No text contoller");
509
510     switch(index)
511     {
512       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
513       {
514         value = impl.mRenderingBackend;
515         break;
516       }
517       case Toolkit::TextLabel::Property::TEXT:
518       {
519         std::string text;
520         impl.mController->GetText(text);
521         value = text;
522         break;
523       }
524       case Toolkit::TextLabel::Property::FONT_FAMILY:
525       {
526         value = impl.mController->GetDefaultFontFamily();
527         break;
528       }
529       case Toolkit::TextLabel::Property::FONT_STYLE:
530       {
531         GetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
532         break;
533       }
534       case Toolkit::TextLabel::Property::POINT_SIZE:
535       {
536         value = impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE);
537         break;
538       }
539       case Toolkit::TextLabel::Property::MULTI_LINE:
540       {
541         value = impl.mController->IsMultiLineEnabled();
542         break;
543       }
544       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
545       {
546         const char* name = Text::GetHorizontalAlignmentString(impl.mController->GetHorizontalAlignment());
547
548         if(name)
549         {
550           value = std::string(name);
551         }
552         break;
553       }
554       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
555       {
556         const char* name = Text::GetVerticalAlignmentString(impl.mController->GetVerticalAlignment());
557         if(name)
558         {
559           value = std::string(name);
560         }
561         break;
562       }
563       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
564       {
565         value = impl.mController->IsMarkupProcessorEnabled();
566         break;
567       }
568       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
569       {
570         value = impl.mController->IsAutoScrollEnabled();
571         break;
572       }
573       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
574       {
575         if(impl.mTextScroller)
576         {
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);
580           if(mode)
581           {
582             value = std::string(mode);
583           }
584         }
585         break;
586       }
587       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
588       {
589         if(impl.mTextScroller)
590         {
591           value = impl.mTextScroller->GetSpeed();
592         }
593         break;
594       }
595       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
596       {
597         if(impl.mTextScroller)
598         {
599           value = impl.mTextScroller->GetLoopCount();
600         }
601         break;
602       }
603       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
604       {
605         if(impl.mTextScroller)
606         {
607           value = impl.mTextScroller->GetLoopDelay();
608         }
609         break;
610       }
611       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
612       {
613         if(impl.mTextScroller)
614         {
615           value = impl.mTextScroller->GetGap();
616         }
617         break;
618       }
619       case Toolkit::TextLabel::Property::LINE_SPACING:
620       {
621         value = impl.mController->GetDefaultLineSpacing();
622         break;
623       }
624       case Toolkit::TextLabel::Property::UNDERLINE:
625       {
626         GetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
627         break;
628       }
629       case Toolkit::TextLabel::Property::SHADOW:
630       {
631         GetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
632         break;
633       }
634       case Toolkit::TextLabel::Property::EMBOSS:
635       {
636         GetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
637         break;
638       }
639       case Toolkit::TextLabel::Property::OUTLINE:
640       {
641         GetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
642         break;
643       }
644       case Toolkit::TextLabel::Property::PIXEL_SIZE:
645       {
646         value = impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE);
647         break;
648       }
649       case Toolkit::TextLabel::Property::ELLIPSIS:
650       {
651         value = impl.mController->IsTextElideEnabled();
652         break;
653       }
654       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
655       {
656         value = impl.mController->GetLineWrapMode();
657         break;
658       }
659       case Toolkit::TextLabel::Property::LINE_COUNT:
660       {
661         float width = label.GetProperty(Actor::Property::SIZE_WIDTH).Get<float>();
662         value       = impl.mController->GetLineCount(width);
663         break;
664       }
665       case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
666       {
667         value = impl.mController->GetTextDirection();
668         break;
669       }
670       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
671       {
672         value = impl.mController->GetVerticalLineAlignment();
673         break;
674       }
675       case Toolkit::DevelTextLabel::Property::BACKGROUND:
676       {
677         GetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
678         break;
679       }
680       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
681       {
682         value = impl.mController->IsIgnoreSpacesAfterText();
683         break;
684       }
685       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
686       {
687         value = impl.mController->IsMatchSystemLanguageDirection();
688         break;
689       }
690       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
691       {
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();
696
697         Property::Map map;
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");
703
704         value = map;
705         break;
706       }
707       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
708       {
709         value = impl.mController->GetDefaultLineSize();
710         break;
711       }
712       case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
713       {
714         value = impl.mController->GetFontSizeScale();
715         break;
716       }
717     }
718   }
719
720   return value;
721 }
722
723 void TextLabel::OnInitialize()
724 {
725   Actor self = Self();
726
727   Property::Map propertyMap;
728   propertyMap.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
729
730   mVisual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
731   DevelControl::RegisterVisual(*this, Toolkit::TextLabel::Property::TEXT, mVisual);
732
733   TextVisual::SetAnimatableTextColorProperty(mVisual, Toolkit::TextLabel::Property::TEXT_COLOR);
734
735   mController = TextVisual::GetController(mVisual);
736   DALI_ASSERT_DEBUG(mController && "Invalid Text Controller")
737
738   mController->SetControlInterface(this);
739
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);
743
744   // Enable highlightability
745   self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
746
747   // Enable the text ellipsis.
748   mController->SetTextElideEnabled(true); // If false then text larger than control will overflow
749
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);
754
755   Layout::Engine& engine = mController->GetLayoutEngine();
756   engine.SetCursorWidth(0u); // Do not layout space for the cursor.
757
758   DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
759     return std::unique_ptr<Dali::Accessibility::Accessible>(
760       new AccessibleImpl(actor, Dali::Accessibility::Role::LABEL));
761   });
762 }
763
764 void TextLabel::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
765 {
766   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
767
768   switch(change)
769   {
770     case StyleChange::DEFAULT_FONT_CHANGE:
771     {
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);
776       RelayoutRequest();
777       break;
778     }
779     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
780     {
781       GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
782       RelayoutRequest();
783       break;
784     }
785     case StyleChange::THEME_CHANGE:
786     {
787       // Nothing to do, let control base class handle this
788       break;
789     }
790   }
791
792   // Up call to Control
793   Control::OnStyleChange(styleManager, change);
794 }
795
796 Vector3 TextLabel::GetNaturalSize()
797 {
798   Extents padding;
799   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
800
801   Vector3 naturalSize = mController->GetNaturalSize();
802   naturalSize.width += (padding.start + padding.end);
803   naturalSize.height += (padding.top + padding.bottom);
804
805   return naturalSize;
806 }
807
808 float TextLabel::GetHeightForWidth(float width)
809 {
810   Extents padding;
811   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
812
813   return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
814 }
815
816 void TextLabel::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
817 {
818   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index);
819
820   switch(index)
821   {
822     case Toolkit::TextLabel::Property::TEXT_COLOR:
823     {
824       const Vector4& textColor = propertyValue.Get<Vector4>();
825       if(mController->GetDefaultColor() != textColor)
826       {
827         mController->SetDefaultColor(textColor);
828         mTextUpdateNeeded = true;
829       }
830       break;
831     }
832     default:
833     {
834       Control::OnPropertySet(index, propertyValue); // up call to control for non-handled properties
835       break;
836     }
837   }
838 }
839
840 void TextLabel::OnRelayout(const Vector2& size, RelayoutContainer& container)
841 {
842   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout\n");
843
844   Extents padding;
845   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
846
847   Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
848
849   if(mController->IsTextFitEnabled())
850   {
851     mController->FitPointSizeforLayout(contentSize);
852     mController->SetTextFitContentSize(contentSize);
853   }
854
855   // Support Right-To-Left
856   Dali::LayoutDirection::Type layoutDirection;
857   if(mController->IsMatchSystemLanguageDirection())
858   {
859     layoutDirection = static_cast<Dali::LayoutDirection::Type>(DevelWindow::Get(Self()).GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
860   }
861   else
862   {
863     layoutDirection = static_cast<Dali::LayoutDirection::Type>(Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
864   }
865   const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
866
867   if((Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType)) || mTextUpdateNeeded)
868   {
869     DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", (mController->IsAutoScrollEnabled()) ? "true" : "false", this);
870
871     // Update the visual
872     TextVisual::EnableRendererUpdate(mVisual);
873
874     // Support Right-To-Left of padding
875     if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
876     {
877       std::swap(padding.start, padding.end);
878     }
879
880     // Calculate the size of the visual that can fit the text
881     Size layoutSize = mController->GetTextModel()->GetLayoutSize();
882     layoutSize.x    = contentSize.x;
883
884     const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
885     if(shadowOffset.y > Math::MACHINE_EPSILON_1)
886     {
887       layoutSize.y += shadowOffset.y;
888     }
889
890     float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
891     layoutSize.y += outlineWidth * 2.0f;
892     layoutSize.y = std::min(layoutSize.y, contentSize.y);
893
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()];
898
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);
907
908     if(mController->IsAutoScrollEnabled())
909     {
910       SetUpAutoScrolling();
911     }
912
913     mTextUpdateNeeded = false;
914   }
915 }
916
917 void TextLabel::RequestTextRelayout()
918 {
919   RelayoutRequest();
920   // Signal that a Relayout may be needed
921 }
922
923 void TextLabel::SetUpAutoScrolling()
924 {
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();
928
929   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n", textNaturalSize.x, textNaturalSize.y, controlSize.x, controlSize.y);
930
931   if(!mTextScroller)
932   {
933     DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
934
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);
937   }
938
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
943
944   // Create a texture of the text for scrolling
945   Size      verifiedSize   = textureSize;
946   const int maxTextureSize = Dali::GetMaxTextureSize();
947
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)
951   {
952     verifiedSize.width = maxTextureSize;
953     if(textNaturalSize.width > maxTextureSize)
954     {
955       mController->SetTextElideEnabled(true);
956     }
957     GetHeightForWidth(maxTextureSize);
958     wrapGap = std::max(maxTextureSize - textNaturalSize.width, 0.0f);
959   }
960
961   Text::TypesetterPtr typesetter = Text::Typesetter::New(mController->GetTextModel());
962
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(),
966                                  data.GetWidth(),
967                                  data.GetHeight());
968   texture.Upload(data);
969
970   TextureSet textureSet = TextureSet::New();
971   textureSet.SetTexture(0u, texture);
972
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);
978
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);
983 }
984
985 void TextLabel::ScrollingFinished()
986 {
987   // Pure Virtual from TextScroller Interface
988   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
989   mController->SetAutoScrollEnabled(false);
990   RequestTextRelayout();
991 }
992
993 TextLabel::TextLabel()
994 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
995   mRenderingBackend(DEFAULT_RENDERING_BACKEND),
996   mTextUpdateNeeded(false)
997 {
998 }
999
1000 TextLabel::~TextLabel()
1001 {
1002 }
1003
1004 std::string TextLabel::AccessibleImpl::GetNameRaw()
1005 {
1006   auto slf = Toolkit::TextLabel::DownCast(self);
1007   return slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1008 }
1009
1010 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1011 {
1012   return Toolkit::TextLabel::Property::TEXT;
1013 }
1014
1015 std::string TextLabel::AccessibleImpl::GetText(size_t startOffset,
1016                                                size_t endOffset)
1017 {
1018   if(endOffset <= startOffset)
1019     return {};
1020
1021   auto slf = Toolkit::TextLabel::DownCast(self);
1022   auto txt =
1023     slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1024
1025   if(startOffset > txt.size() || endOffset > txt.size())
1026     return {};
1027
1028   return txt.substr(startOffset, endOffset - startOffset);
1029 }
1030
1031 size_t TextLabel::AccessibleImpl::GetCharacterCount()
1032 {
1033   auto slf = Toolkit::TextLabel::DownCast(self);
1034   auto txt =
1035     slf.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1036
1037   return txt.size();
1038 }
1039
1040 size_t TextLabel::AccessibleImpl::GetCaretOffset()
1041 {
1042   return {};
1043 }
1044
1045 bool TextLabel::AccessibleImpl::SetCaretOffset(size_t offset)
1046 {
1047   return {};
1048 }
1049
1050 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(
1051   size_t offset, Dali::Accessibility::TextBoundary boundary)
1052 {
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();
1056
1057   auto range = Dali::Accessibility::Range{};
1058
1059   switch(boundary)
1060   {
1061     case Dali::Accessibility::TextBoundary::CHARACTER:
1062     {
1063       if(offset < txt_size)
1064       {
1065         range.content     = txt[offset];
1066         range.startOffset = offset;
1067         range.endOffset   = offset + 1;
1068       }
1069     }
1070     break;
1071     case Dali::Accessibility::TextBoundary::WORD:
1072     case Dali::Accessibility::TextBoundary::LINE:
1073     {
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());
1078       else
1079         Accessibility::Accessible::FindLineSeparationsUtf8((const utf8_t*)txt_c_string, txt_size, "", breaks.data());
1080       auto index   = 0u;
1081       auto counter = 0u;
1082       while(index < txt_size && counter <= offset)
1083       {
1084         auto start = index;
1085         if(breaks[index])
1086         {
1087           while(breaks[index])
1088             index++;
1089           counter++;
1090         }
1091         else
1092         {
1093           if(boundary == Dali::Accessibility::TextBoundary::WORD)
1094             index++;
1095           if(boundary == Dali::Accessibility::TextBoundary::LINE)
1096             counter++;
1097         }
1098         if((counter > 0) && ((counter - 1) == offset))
1099         {
1100           range.content     = txt.substr(start, index - start + 1);
1101           range.startOffset = start;
1102           range.endOffset   = index + 1;
1103         }
1104         if(boundary == Dali::Accessibility::TextBoundary::LINE)
1105           index++;
1106       }
1107     }
1108     break;
1109     case Dali::Accessibility::TextBoundary::SENTENCE:
1110     {
1111       /* not supported by efl */
1112     }
1113     break;
1114     case Dali::Accessibility::TextBoundary::PARAGRAPH:
1115     {
1116       /* Paragraph is not supported by libunibreak library */
1117     }
1118     break;
1119     default:
1120       break;
1121   }
1122
1123   return range;
1124 }
1125
1126 Dali::Accessibility::Range
1127 TextLabel::AccessibleImpl::GetSelection(size_t selectionNum)
1128 {
1129   // Since DALi supports only one selection indexes higher than 0 are ignored
1130   if(selectionNum > 0)
1131     return {};
1132
1133   auto        slf  = Toolkit::TextLabel::DownCast(self);
1134   auto        ctrl = Dali::Toolkit::GetImpl(slf).getController();
1135   std::string ret;
1136   ctrl->RetrieveSelection(ret);
1137   auto r = ctrl->GetSelectionIndexes();
1138
1139   return {static_cast<size_t>(r.first), static_cast<size_t>(r.second), ret};
1140 }
1141
1142 bool TextLabel::AccessibleImpl::RemoveSelection(size_t selectionNum)
1143 {
1144   // Since DALi supports only one selection indexes higher than 0 are ignored
1145   if(selectionNum > 0)
1146     return false;
1147
1148   auto slf = Toolkit::TextLabel::DownCast(self);
1149   Dali::Toolkit::GetImpl(slf).getController()->SetSelection(0, 0);
1150   return true;
1151 }
1152
1153 bool TextLabel::AccessibleImpl::SetSelection(size_t selectionNum,
1154                                              size_t startOffset,
1155                                              size_t endOffset)
1156 {
1157   // Since DALi supports only one selection indexes higher than 0 are ignored
1158   if(selectionNum > 0)
1159     return false;
1160
1161   auto slf = Toolkit::TextLabel::DownCast(self);
1162   Dali::Toolkit::GetImpl(slf).getController()->SetSelection(startOffset,
1163                                                             endOffset);
1164   return true;
1165 }
1166
1167 } // namespace Internal
1168
1169 } // namespace Toolkit
1170
1171 } // namespace Dali