Merge "Makes the LTR/RTL alignment of text follow the system language by default...
[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/actors/actor-devel.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.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
143 DALI_SIGNAL_REGISTRATION(Toolkit, TextLabel, "anchorClicked", SIGNAL_ANCHOR_CLICKED)
144
145 DALI_TYPE_REGISTRATION_END()
146 // clang-format on
147
148 /// Parses the property map for the TEXT_FIT property
149 void ParseTextFitProperty(Text::ControllerPtr& controller, const Property::Map* propertiesMap)
150 {
151   if(propertiesMap && !propertiesMap->Empty())
152   {
153     bool                     enabled      = false;
154     float                    minSize      = 0.f;
155     float                    maxSize      = 0.f;
156     float                    stepSize     = 0.f;
157     bool                     isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
158     Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
159
160     const unsigned int numberOfItems = propertiesMap->Count();
161
162     // Parses and applies
163     for(unsigned int index = 0u; index < numberOfItems; ++index)
164     {
165       const KeyValuePair& valueGet = propertiesMap->GetKeyValue(index);
166
167       if((Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey) || (TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey))
168       {
169         /// Enable key.
170         enabled = valueGet.second.Get<bool>();
171       }
172       else if((Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey))
173       {
174         /// min size.
175         minSize      = valueGet.second.Get<float>();
176         isMinSizeSet = true;
177       }
178       else if((Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey))
179       {
180         /// max size.
181         maxSize      = valueGet.second.Get<float>();
182         isMaxSizeSet = true;
183       }
184       else if((Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey) || (TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey))
185       {
186         /// step size.
187         stepSize      = valueGet.second.Get<float>();
188         isStepSizeSet = true;
189       }
190       else if((Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey) || (TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey))
191       {
192         if("pixelSize" == valueGet.second.Get<std::string>())
193         {
194           type = Controller::FontSizeType::PIXEL_SIZE;
195         }
196       }
197     }
198
199     controller->SetTextFitEnabled(enabled);
200     if(isMinSizeSet)
201     {
202       controller->SetTextFitMinSize(minSize, type);
203     }
204     if(isMaxSizeSet)
205     {
206       controller->SetTextFitMaxSize(maxSize, type);
207     }
208     if(isStepSizeSet)
209     {
210       controller->SetTextFitStepSize(stepSize, type);
211     }
212   }
213 }
214
215 } // namespace
216
217 Toolkit::TextLabel TextLabel::New()
218 {
219   // Create the implementation, temporarily owned by this handle on stack
220   IntrusivePtr<TextLabel> impl = new TextLabel();
221
222   // Pass ownership to CustomActor handle
223   Toolkit::TextLabel handle(*impl);
224
225   // Second-phase init of the implementation
226   // This can only be done after the CustomActor connection has been made...
227   impl->Initialize();
228
229   return handle;
230 }
231
232 void TextLabel::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
233 {
234   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
235
236   if(label)
237   {
238     TextLabel& impl(GetImpl(label));
239     DALI_ASSERT_ALWAYS(impl.mController && "No text contoller");
240
241     switch(index)
242     {
243       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
244       {
245         int backend = value.Get<int>();
246
247 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
248         if(DevelText::RENDERING_VECTOR_BASED == backend)
249         {
250           backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
251         }
252 #endif
253         if(impl.mRenderingBackend != backend)
254         {
255           impl.mRenderingBackend = backend;
256           impl.mTextUpdateNeeded = true;
257
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);
261         }
262         break;
263       }
264       case Toolkit::TextLabel::Property::TEXT:
265       {
266         impl.mController->SetText(value.Get<std::string>());
267         break;
268       }
269       case Toolkit::TextLabel::Property::FONT_FAMILY:
270       {
271         const std::string& fontFamily = value.Get<std::string>();
272
273         DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str());
274         impl.mController->SetDefaultFontFamily(fontFamily);
275         break;
276       }
277       case Toolkit::TextLabel::Property::FONT_STYLE:
278       {
279         SetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
280         break;
281       }
282       case Toolkit::TextLabel::Property::POINT_SIZE:
283       {
284         const float pointSize = value.Get<float>();
285
286         if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
287         {
288           impl.mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
289         }
290         break;
291       }
292       case Toolkit::TextLabel::Property::MULTI_LINE:
293       {
294         impl.mController->SetMultiLineEnabled(value.Get<bool>());
295         break;
296       }
297       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
298       {
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))
301         {
302           impl.mController->SetHorizontalAlignment(alignment);
303         }
304         break;
305       }
306       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
307       {
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))
310         {
311           impl.mController->SetVerticalAlignment(alignment);
312         }
313         break;
314       }
315       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
316       {
317         const bool enableMarkup = value.Get<bool>();
318         impl.mController->SetMarkupProcessorEnabled(enableMarkup);
319         break;
320       }
321       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
322       {
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())
326         {
327           // If request is disable (false) and auto scrolling is enabled then need to stop it
328           if(enableAutoScroll == false)
329           {
330             if(impl.mTextScroller)
331             {
332               impl.mTextScroller->StopScrolling();
333             }
334           }
335           // If request is enable (true) then start autoscroll as not already running
336           else
337           {
338             impl.mController->SetAutoScrollEnabled(enableAutoScroll);
339           }
340         }
341         break;
342       }
343       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
344       {
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,
350                                                                                            stopMode))
351         {
352           textScroller->SetStopMode(stopMode);
353         }
354         break;
355       }
356       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
357       {
358         impl.GetTextScroller()->SetSpeed(value.Get<int>());
359         break;
360       }
361       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
362       {
363         impl.GetTextScroller()->SetLoopCount(value.Get<int>());
364         break;
365       }
366       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
367       {
368         impl.GetTextScroller()->SetLoopDelay(value.Get<float>());
369         break;
370       }
371       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
372       {
373         impl.GetTextScroller()->SetGap(value.Get<float>());
374         break;
375       }
376       case Toolkit::TextLabel::Property::LINE_SPACING:
377       {
378         const float lineSpacing = value.Get<float>();
379         impl.mTextUpdateNeeded  = impl.mController->SetDefaultLineSpacing(lineSpacing) || impl.mTextUpdateNeeded;
380         break;
381       }
382       case Toolkit::TextLabel::Property::UNDERLINE:
383       {
384         impl.mTextUpdateNeeded = SetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
385         break;
386       }
387       case Toolkit::TextLabel::Property::SHADOW:
388       {
389         impl.mTextUpdateNeeded = SetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
390         break;
391       }
392       case Toolkit::TextLabel::Property::EMBOSS:
393       {
394         impl.mTextUpdateNeeded = SetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
395         break;
396       }
397       case Toolkit::TextLabel::Property::OUTLINE:
398       {
399         impl.mTextUpdateNeeded = SetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
400         break;
401       }
402       case Toolkit::TextLabel::Property::PIXEL_SIZE:
403       {
404         const float pixelSize = value.Get<float>();
405         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize);
406
407         if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE), pixelSize))
408         {
409           impl.mController->SetDefaultFontSize(pixelSize, Text::Controller::PIXEL_SIZE);
410         }
411         break;
412       }
413       case Toolkit::TextLabel::Property::ELLIPSIS:
414       {
415         const bool ellipsis = value.Get<bool>();
416         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis);
417
418         impl.mController->SetTextElideEnabled(ellipsis);
419         break;
420       }
421       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
422       {
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))
425         {
426           DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode);
427           impl.mController->SetLineWrapMode(lineWrapMode);
428         }
429         break;
430       }
431       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
432       {
433         if(impl.mController->GetTextModel())
434         {
435           DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>(value.Get<int>());
436
437           impl.mController->SetVerticalLineAlignment(alignment);
438
439           // Property doesn't affect the layout, only Visual must be updated
440           TextVisual::EnableRendererUpdate(impl.mVisual);
441
442           // No need to trigger full re-layout. Instead call UpdateRenderer() directly
443           TextVisual::UpdateRenderer(impl.mVisual);
444         }
445         break;
446       }
447       case Toolkit::DevelTextLabel::Property::BACKGROUND:
448       {
449         impl.mTextUpdateNeeded = SetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
450         break;
451       }
452       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
453       {
454         impl.mController->SetIgnoreSpacesAfterText(value.Get<bool>());
455         break;
456       }
457       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
458       {
459         impl.mController->SetMatchLayoutDirection(value.Get<bool>() ? DevelText::MatchLayoutDirection::LOCALE : DevelText::MatchLayoutDirection::CONTENTS);
460         break;
461       }
462       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
463       {
464         ParseTextFitProperty(impl.mController, value.GetMap());
465         break;
466       }
467       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
468       {
469         const float lineSize   = value.Get<float>();
470         impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSize(lineSize) || impl.mTextUpdateNeeded;
471         break;
472       }
473       case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
474       {
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);
477
478         if(!Equals(impl.mController->GetFontSizeScale(), scale))
479         {
480           impl.mController->SetFontSizeScale(scale);
481         }
482         break;
483       }
484     }
485
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)
490     {
491       // need to request relayout as size of text may have changed
492       impl.RequestTextRelayout();
493     }
494   }
495 }
496
497 Text::ControllerPtr TextLabel::GetTextController()
498 {
499   return mController;
500 }
501
502 Property::Value TextLabel::GetProperty(BaseObject* object, Property::Index index)
503 {
504   Property::Value value;
505
506   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
507
508   if(label)
509   {
510     TextLabel& impl(GetImpl(label));
511     DALI_ASSERT_DEBUG(impl.mController && "No text contoller");
512
513     switch(index)
514     {
515       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
516       {
517         value = impl.mRenderingBackend;
518         break;
519       }
520       case Toolkit::TextLabel::Property::TEXT:
521       {
522         std::string text;
523         impl.mController->GetText(text);
524         value = text;
525         break;
526       }
527       case Toolkit::TextLabel::Property::FONT_FAMILY:
528       {
529         value = impl.mController->GetDefaultFontFamily();
530         break;
531       }
532       case Toolkit::TextLabel::Property::FONT_STYLE:
533       {
534         GetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
535         break;
536       }
537       case Toolkit::TextLabel::Property::POINT_SIZE:
538       {
539         value = impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE);
540         break;
541       }
542       case Toolkit::TextLabel::Property::MULTI_LINE:
543       {
544         value = impl.mController->IsMultiLineEnabled();
545         break;
546       }
547       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
548       {
549         const char* name = Text::GetHorizontalAlignmentString(impl.mController->GetHorizontalAlignment());
550
551         if(name)
552         {
553           value = std::string(name);
554         }
555         break;
556       }
557       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
558       {
559         const char* name = Text::GetVerticalAlignmentString(impl.mController->GetVerticalAlignment());
560         if(name)
561         {
562           value = std::string(name);
563         }
564         break;
565       }
566       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
567       {
568         value = impl.mController->IsMarkupProcessorEnabled();
569         break;
570       }
571       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
572       {
573         value = impl.mController->IsAutoScrollEnabled();
574         break;
575       }
576       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
577       {
578         if(impl.mTextScroller)
579         {
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);
583           if(mode)
584           {
585             value = std::string(mode);
586           }
587         }
588         break;
589       }
590       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
591       {
592         if(impl.mTextScroller)
593         {
594           value = impl.mTextScroller->GetSpeed();
595         }
596         break;
597       }
598       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
599       {
600         if(impl.mTextScroller)
601         {
602           value = impl.mTextScroller->GetLoopCount();
603         }
604         break;
605       }
606       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
607       {
608         if(impl.mTextScroller)
609         {
610           value = impl.mTextScroller->GetLoopDelay();
611         }
612         break;
613       }
614       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
615       {
616         if(impl.mTextScroller)
617         {
618           value = impl.mTextScroller->GetGap();
619         }
620         break;
621       }
622       case Toolkit::TextLabel::Property::LINE_SPACING:
623       {
624         value = impl.mController->GetDefaultLineSpacing();
625         break;
626       }
627       case Toolkit::TextLabel::Property::UNDERLINE:
628       {
629         GetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
630         break;
631       }
632       case Toolkit::TextLabel::Property::SHADOW:
633       {
634         GetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
635         break;
636       }
637       case Toolkit::TextLabel::Property::EMBOSS:
638       {
639         GetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
640         break;
641       }
642       case Toolkit::TextLabel::Property::OUTLINE:
643       {
644         GetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
645         break;
646       }
647       case Toolkit::TextLabel::Property::PIXEL_SIZE:
648       {
649         value = impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE);
650         break;
651       }
652       case Toolkit::TextLabel::Property::ELLIPSIS:
653       {
654         value = impl.mController->IsTextElideEnabled();
655         break;
656       }
657       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
658       {
659         value = impl.mController->GetLineWrapMode();
660         break;
661       }
662       case Toolkit::TextLabel::Property::LINE_COUNT:
663       {
664         float width = label.GetProperty(Actor::Property::SIZE_WIDTH).Get<float>();
665         value       = impl.mController->GetLineCount(width);
666         break;
667       }
668       case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
669       {
670         value = impl.mController->GetTextDirection();
671         break;
672       }
673       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
674       {
675         value = impl.mController->GetVerticalLineAlignment();
676         break;
677       }
678       case Toolkit::DevelTextLabel::Property::BACKGROUND:
679       {
680         GetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
681         break;
682       }
683       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
684       {
685         value = impl.mController->IsIgnoreSpacesAfterText();
686         break;
687       }
688       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
689       {
690         value = impl.mController->GetMatchLayoutDirection() != DevelText::MatchLayoutDirection::CONTENTS;
691         break;
692       }
693       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
694       {
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();
699
700         Property::Map map;
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");
706
707         value = map;
708         break;
709       }
710       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
711       {
712         value = impl.mController->GetDefaultLineSize();
713         break;
714       }
715       case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
716       {
717         value = impl.mController->GetFontSizeScale();
718         break;
719       }
720     }
721   }
722
723   return value;
724 }
725
726 bool TextLabel::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
727 {
728   Dali::BaseHandle handle(object);
729
730   bool               connected(true);
731   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(handle);
732
733   if(0 == strcmp(signalName.c_str(), SIGNAL_ANCHOR_CLICKED))
734   {
735     if(label)
736     {
737       Internal::TextLabel& labelImpl(GetImpl(label));
738       labelImpl.AnchorClickedSignal().Connect(tracker, functor);
739     }
740   }
741   else
742   {
743     // signalName does not match any signal
744     connected = false;
745   }
746
747   return connected;
748 }
749
750 DevelTextLabel::AnchorClickedSignalType& TextLabel::AnchorClickedSignal()
751 {
752   return mAnchorClickedSignal;
753 }
754
755 void TextLabel::OnInitialize()
756 {
757   Actor self = Self();
758
759   Property::Map propertyMap;
760   propertyMap.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
761
762   mVisual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
763   DevelControl::RegisterVisual(*this, Toolkit::TextLabel::Property::TEXT, mVisual);
764
765   TextVisual::SetAnimatableTextColorProperty(mVisual, Toolkit::TextLabel::Property::TEXT_COLOR);
766
767   mController = TextVisual::GetController(mVisual);
768   DALI_ASSERT_DEBUG(mController && "Invalid Text Controller")
769
770   mController->SetControlInterface(this);
771   mController->SetAnchorControlInterface(this);
772
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);
776
777   // Enable highlightability
778   self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
779
780   // Enable the text ellipsis.
781   mController->SetTextElideEnabled(true); // If false then text larger than control will overflow
782
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);
787
788   self.LayoutDirectionChangedSignal().Connect(this, &TextLabel::OnLayoutDirectionChanged);
789
790   // Forward input events to controller
791   EnableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
792   GetTapGestureDetector().SetMaximumTapsRequired(1);
793
794   Layout::Engine& engine = mController->GetLayoutEngine();
795   engine.SetCursorWidth(0u); // Do not layout space for the cursor.
796
797   DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
798     return std::unique_ptr<Dali::Accessibility::Accessible>(
799       new AccessibleImpl(actor, Dali::Accessibility::Role::LABEL));
800   });
801 }
802
803 void TextLabel::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
804 {
805   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
806
807   switch(change)
808   {
809     case StyleChange::DEFAULT_FONT_CHANGE:
810     {
811       // Property system did not set the font so should update it.
812       const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
813       DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str());
814       mController->UpdateAfterFontChange(newFont);
815       RelayoutRequest();
816       break;
817     }
818     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
819     {
820       GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
821       RelayoutRequest();
822       break;
823     }
824     case StyleChange::THEME_CHANGE:
825     {
826       // Nothing to do, let control base class handle this
827       break;
828     }
829   }
830
831   // Up call to Control
832   Control::OnStyleChange(styleManager, change);
833 }
834
835 void TextLabel::OnTap(const TapGesture& gesture)
836 {
837   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnTap %p\n", mController.Get());
838
839   // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
840   Extents padding;
841   padding                   = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
842   const Vector2& localPoint = gesture.GetLocalPoint();
843   mController->AnchorEvent(localPoint.x - padding.start, localPoint.y - padding.top);
844
845   // parents can also listen for tap gesture events
846   Dali::DevelActor::SetNeedGesturePropagation(Self(), true);
847 }
848
849 void TextLabel::AnchorClicked(const std::string& href)
850 {
851   Dali::Toolkit::TextLabel handle(GetOwner());
852   mAnchorClickedSignal.Emit(handle, href.c_str(), href.length());
853 }
854
855 Vector3 TextLabel::GetNaturalSize()
856 {
857   Extents padding;
858   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
859
860   Vector3 naturalSize = mController->GetNaturalSize();
861   naturalSize.width += (padding.start + padding.end);
862   naturalSize.height += (padding.top + padding.bottom);
863
864   return naturalSize;
865 }
866
867 float TextLabel::GetHeightForWidth(float width)
868 {
869   Extents padding;
870   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
871
872   return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
873 }
874
875 void TextLabel::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
876 {
877   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index);
878
879   switch(index)
880   {
881     case Toolkit::TextLabel::Property::TEXT_COLOR:
882     {
883       const Vector4& textColor = propertyValue.Get<Vector4>();
884       if(mController->GetDefaultColor() != textColor)
885       {
886         mController->SetDefaultColor(textColor);
887         mTextUpdateNeeded = true;
888       }
889       break;
890     }
891     default:
892     {
893       Control::OnPropertySet(index, propertyValue); // up call to control for non-handled properties
894       break;
895     }
896   }
897 }
898
899 void TextLabel::OnRelayout(const Vector2& size, RelayoutContainer& container)
900 {
901   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout\n");
902
903   Actor self = Self();
904
905   Extents padding;
906   padding = self.GetProperty<Extents>(Toolkit::Control::Property::PADDING);
907
908   Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
909
910   if(mController->IsTextFitEnabled())
911   {
912     mController->FitPointSizeforLayout(contentSize);
913     mController->SetTextFitContentSize(contentSize);
914   }
915
916   // Support Right-To-Left
917   Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(self);
918
919   const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
920
921   if((Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType)) || mTextUpdateNeeded)
922   {
923     DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", (mController->IsAutoScrollEnabled()) ? "true" : "false", this);
924
925     // Update the visual
926     TextVisual::EnableRendererUpdate(mVisual);
927
928     // Support Right-To-Left of padding
929     if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
930     {
931       std::swap(padding.start, padding.end);
932     }
933
934     // Calculate the size of the visual that can fit the text
935     Size layoutSize = mController->GetTextModel()->GetLayoutSize();
936     layoutSize.x    = contentSize.x;
937
938     const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
939     if(shadowOffset.y > Math::MACHINE_EPSILON_1)
940     {
941       layoutSize.y += shadowOffset.y;
942     }
943
944     float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
945     layoutSize.y += outlineWidth * 2.0f;
946     layoutSize.y = std::min(layoutSize.y, contentSize.y);
947
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()];
952
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);
961
962     if(mController->IsAutoScrollEnabled())
963     {
964       SetUpAutoScrolling();
965     }
966
967     mTextUpdateNeeded = false;
968   }
969 }
970
971 void TextLabel::RequestTextRelayout()
972 {
973   RelayoutRequest();
974   // Signal that a Relayout may be needed
975 }
976
977 void TextLabel::SetUpAutoScrolling()
978 {
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();
982
983   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n", textNaturalSize.x, textNaturalSize.y, controlSize.x, controlSize.y);
984
985   if(!mTextScroller)
986   {
987     DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
988
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);
991   }
992
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
997
998   // Create a texture of the text for scrolling
999   Size      verifiedSize   = textureSize;
1000   const int maxTextureSize = Dali::GetMaxTextureSize();
1001
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)
1005   {
1006     verifiedSize.width = maxTextureSize;
1007     if(textNaturalSize.width > maxTextureSize)
1008     {
1009       mController->SetTextElideEnabled(true);
1010     }
1011     GetHeightForWidth(maxTextureSize);
1012     wrapGap = std::max(maxTextureSize - textNaturalSize.width, 0.0f);
1013   }
1014
1015   Text::TypesetterPtr typesetter = Text::Typesetter::New(mController->GetTextModel());
1016
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(),
1020                                  data.GetWidth(),
1021                                  data.GetHeight());
1022   texture.Upload(data);
1023
1024   TextureSet textureSet = TextureSet::New();
1025   textureSet.SetTexture(0u, texture);
1026
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);
1032
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);
1037 }
1038
1039 void TextLabel::ScrollingFinished()
1040 {
1041   // Pure Virtual from TextScroller Interface
1042   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
1043   mController->SetAutoScrollEnabled(false);
1044   RequestTextRelayout();
1045 }
1046
1047 void TextLabel::OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type)
1048 {
1049   mController->ChangedLayoutDirection();
1050 }
1051
1052 TextLabel::TextLabel()
1053 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
1054   mRenderingBackend(DEFAULT_RENDERING_BACKEND),
1055   mTextUpdateNeeded(false)
1056 {
1057 }
1058
1059 TextLabel::~TextLabel()
1060 {
1061 }
1062
1063 std::string TextLabel::AccessibleImpl::GetNameRaw()
1064 {
1065   auto self = Toolkit::TextLabel::DownCast(Self());
1066   return self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1067 }
1068
1069 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1070 {
1071   return Toolkit::TextLabel::Property::TEXT;
1072 }
1073
1074 std::string TextLabel::AccessibleImpl::GetText(size_t startOffset, size_t endOffset)
1075 {
1076   if(endOffset <= startOffset)
1077   {
1078     return {};
1079   }
1080
1081   auto self = Toolkit::TextLabel::DownCast(Self());
1082   auto text = self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1083
1084   if(startOffset > text.size() || endOffset > text.size())
1085   {
1086     return {};
1087   }
1088
1089   return text.substr(startOffset, endOffset - startOffset);
1090 }
1091
1092 size_t TextLabel::AccessibleImpl::GetCharacterCount()
1093 {
1094   auto self = Toolkit::TextLabel::DownCast(Self());
1095   auto text = self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1096
1097   return text.size();
1098 }
1099
1100 size_t TextLabel::AccessibleImpl::GetCursorOffset()
1101 {
1102   return {};
1103 }
1104
1105 bool TextLabel::AccessibleImpl::SetCursorOffset(size_t offset)
1106 {
1107   return {};
1108 }
1109
1110 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(size_t offset, Dali::Accessibility::TextBoundary boundary)
1111 {
1112   auto self = Toolkit::TextLabel::DownCast(Self());
1113   auto text = self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1114   auto textSize = text.size();
1115
1116   auto range = Dali::Accessibility::Range{};
1117
1118   switch(boundary)
1119   {
1120     case Dali::Accessibility::TextBoundary::CHARACTER:
1121     {
1122       if(offset < textSize)
1123       {
1124         range.content     = text[offset];
1125         range.startOffset = offset;
1126         range.endOffset   = offset + 1;
1127       }
1128       break;
1129     }
1130     case Dali::Accessibility::TextBoundary::WORD:
1131     case Dali::Accessibility::TextBoundary::LINE:
1132     {
1133       auto textString = text.c_str();
1134       auto breaks = std::vector<char>(textSize, 0);
1135
1136       if(boundary == Dali::Accessibility::TextBoundary::WORD)
1137       {
1138         Accessibility::Accessible::FindWordSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1139       }
1140       else
1141       {
1142         Accessibility::Accessible::FindLineSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1143       }
1144
1145       auto index = 0u;
1146       auto counter = 0u;
1147       while(index < textSize && counter <= offset)
1148       {
1149         auto start = index;
1150         if(breaks[index])
1151         {
1152           while(breaks[index])
1153           {
1154             index++;
1155           }
1156           counter++;
1157         }
1158         else
1159         {
1160           if(boundary == Dali::Accessibility::TextBoundary::WORD)
1161           {
1162             index++;
1163           }
1164           if(boundary == Dali::Accessibility::TextBoundary::LINE)
1165           {
1166             counter++;
1167           }
1168         }
1169
1170         if((counter > 0) && ((counter - 1) == offset))
1171         {
1172           range.content     = text.substr(start, index - start + 1);
1173           range.startOffset = start;
1174           range.endOffset   = index + 1;
1175         }
1176
1177         if(boundary == Dali::Accessibility::TextBoundary::LINE)
1178         {
1179           index++;
1180         }
1181       }
1182       break;
1183     }
1184     case Dali::Accessibility::TextBoundary::SENTENCE:
1185     {
1186       /* not supported by default */
1187       break;
1188     }
1189     case Dali::Accessibility::TextBoundary::PARAGRAPH:
1190     {
1191       /* Paragraph is not supported by libunibreak library */
1192       break;
1193     }
1194     default:
1195       break;
1196   }
1197
1198   return range;
1199 }
1200
1201 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetRangeOfSelection(size_t selectionIndex)
1202 {
1203   // Since DALi supports only one selection indexes higher than 0 are ignored
1204   if(selectionIndex > 0)
1205   {
1206     return {};
1207   }
1208
1209   auto self  = Toolkit::TextLabel::DownCast(Self());
1210   auto controller = Dali::Toolkit::GetImpl(self).GetTextController();
1211   std::string value{};
1212   controller->RetrieveSelection(value);
1213   auto indices = controller->GetSelectionIndexes();
1214
1215   return {static_cast<size_t>(indices.first), static_cast<size_t>(indices.second), value};
1216 }
1217
1218 bool TextLabel::AccessibleImpl::RemoveSelection(size_t selectionIndex)
1219 {
1220   // Since DALi supports only one selection indexes higher than 0 are ignored
1221   if(selectionIndex > 0)
1222   {
1223     return false;
1224   }
1225
1226   auto self = Toolkit::TextLabel::DownCast(Self());
1227   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(0, 0);
1228   return true;
1229 }
1230
1231 bool TextLabel::AccessibleImpl::SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset)
1232 {
1233   // Since DALi supports only one selection indexes higher than 0 are ignored
1234   if(selectionIndex > 0)
1235   {
1236     return false;
1237   }
1238
1239   auto self = Toolkit::TextLabel::DownCast(Self());
1240   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(startOffset, endOffset);
1241   return true;
1242 }
1243
1244 } // namespace Internal
1245
1246 } // namespace Toolkit
1247
1248 } // namespace Dali