DALi Version 2.1.4
[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/controls/text-controls/common-text-utils.h>
34 #include <dali-toolkit/internal/styling/style-manager-impl.h>
35 #include <dali-toolkit/internal/text/property-string-parser.h>
36 #include <dali-toolkit/internal/text/rendering/text-backend.h>
37 #include <dali-toolkit/internal/text/text-definitions.h>
38 #include <dali-toolkit/internal/text/text-effects-style.h>
39 #include <dali-toolkit/internal/text/text-font-style.h>
40 #include <dali-toolkit/internal/text/text-view.h>
41 #include <dali-toolkit/public-api/text/text-enumerations.h>
42
43 #include <dali-toolkit/devel-api/controls/control-devel.h>
44 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
45 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
46 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
47 #include <dali-toolkit/public-api/align-enumerations.h>
48 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
49 #include <dali-toolkit/public-api/visuals/visual-properties.h>
50
51 // DEVEL INCLUDES
52 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
53
54 using namespace Dali::Toolkit::Text;
55
56 namespace Dali
57 {
58 namespace Toolkit
59 {
60 namespace Internal
61 {
62 namespace
63 {
64 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
65
66 /**
67  * @brief How the text visual should be aligned vertically inside the control.
68  *
69  * 0.0f aligns the text to the top, 0.5f aligns the text to the center, 1.0f aligns the text to the bottom.
70  * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
71  */
72 const float VERTICAL_ALIGNMENT_TABLE[Text::VerticalAlignment::BOTTOM + 1] =
73   {
74     0.0f, // VerticalAlignment::TOP
75     0.5f, // VerticalAlignment::CENTER
76     1.0f  // VerticalAlignment::BOTTOM
77 };
78
79 const std::string TEXT_FIT_ENABLE_KEY("enable");
80 const std::string TEXT_FIT_MIN_SIZE_KEY("minSize");
81 const std::string TEXT_FIT_MAX_SIZE_KEY("maxSize");
82 const std::string TEXT_FIT_STEP_SIZE_KEY("stepSize");
83 const std::string TEXT_FIT_FONT_SIZE_KEY("fontSize");
84 const std::string TEXT_FIT_FONT_SIZE_TYPE_KEY("fontSizeType");
85
86 #if defined(DEBUG_ENABLED)
87 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
88 #endif
89
90 const Scripting::StringEnum AUTO_SCROLL_STOP_MODE_TABLE[] =
91   {
92     {"IMMEDIATE", Toolkit::TextLabel::AutoScrollStopMode::IMMEDIATE},
93     {"FINISH_LOOP", Toolkit::TextLabel::AutoScrollStopMode::FINISH_LOOP},
94 };
95 const unsigned int AUTO_SCROLL_STOP_MODE_TABLE_COUNT = sizeof(AUTO_SCROLL_STOP_MODE_TABLE) / sizeof(AUTO_SCROLL_STOP_MODE_TABLE[0]);
96
97 // Type registration
98 BaseHandle Create()
99 {
100   return Toolkit::TextLabel::New();
101 }
102
103 // clang-format off
104 // Setup properties, signals and actions using the type-registry.
105 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::TextLabel, Toolkit::Control, Create);
106
107 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "text",                         STRING,  TEXT                           )
108 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "fontFamily",                   STRING,  FONT_FAMILY                    )
109 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "fontStyle",                    MAP,     FONT_STYLE                     )
110 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "pointSize",                    FLOAT,   POINT_SIZE                     )
111 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "multiLine",                    BOOLEAN, MULTI_LINE                     )
112 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "horizontalAlignment",          STRING,  HORIZONTAL_ALIGNMENT           )
113 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "verticalAlignment",            STRING,  VERTICAL_ALIGNMENT             )
114 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "enableMarkup",                 BOOLEAN, ENABLE_MARKUP                  )
115 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "enableAutoScroll",             BOOLEAN, ENABLE_AUTO_SCROLL             )
116 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "autoScrollSpeed",              INTEGER, AUTO_SCROLL_SPEED              )
117 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "autoScrollLoopCount",          INTEGER, AUTO_SCROLL_LOOP_COUNT         )
118 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "autoScrollGap",                FLOAT,   AUTO_SCROLL_GAP                )
119 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "lineSpacing",                  FLOAT,   LINE_SPACING                   )
120 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "underline",                    MAP,     UNDERLINE                      )
121 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "shadow",                       MAP,     SHADOW                         )
122 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "emboss",                       MAP,     EMBOSS                         )
123 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "outline",                      MAP,     OUTLINE                        )
124 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "pixelSize",                    FLOAT,   PIXEL_SIZE                     )
125 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "ellipsis",                     BOOLEAN, ELLIPSIS                       )
126 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "autoScrollLoopDelay",          FLOAT,   AUTO_SCROLL_LOOP_DELAY         )
127 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "autoScrollStopMode",           STRING,  AUTO_SCROLL_STOP_MODE          )
128 DALI_PROPERTY_REGISTRATION_READ_ONLY(Toolkit,       TextLabel, "lineCount",                    INTEGER, LINE_COUNT                     )
129 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextLabel, "lineWrapMode",                 INTEGER, LINE_WRAP_MODE                 )
130 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY(Toolkit, TextLabel, "textDirection",                INTEGER, TEXT_DIRECTION                 )
131 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "verticalLineAlignment",        INTEGER, VERTICAL_LINE_ALIGNMENT        )
132 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "textBackground",               MAP,     BACKGROUND                     )
133 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "ignoreSpacesAfterText",        BOOLEAN, IGNORE_SPACES_AFTER_TEXT       )
134 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "matchSystemLanguageDirection", BOOLEAN, MATCH_SYSTEM_LANGUAGE_DIRECTION)
135 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "textFit",                      MAP,     TEXT_FIT                       )
136 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "minLineSize",                  FLOAT,   MIN_LINE_SIZE                  )
137 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "renderingBackend",             INTEGER, RENDERING_BACKEND              )
138 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "fontSizeScale",                FLOAT,   FONT_SIZE_SCALE                )
139 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextLabel, "ellipsisPosition",             INTEGER, ELLIPSIS_POSITION              )
140
141 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(Toolkit, TextLabel, "textColor",      Color::BLACK,     TEXT_COLOR   )
142 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit,    TextLabel, "textColorRed",   TEXT_COLOR_RED,   TEXT_COLOR, 0)
143 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit,    TextLabel, "textColorGreen", TEXT_COLOR_GREEN, TEXT_COLOR, 1)
144 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit,    TextLabel, "textColorBlue",  TEXT_COLOR_BLUE,  TEXT_COLOR, 2)
145 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION(Toolkit,    TextLabel, "textColorAlpha", TEXT_COLOR_ALPHA, TEXT_COLOR, 3)
146
147 DALI_SIGNAL_REGISTRATION(Toolkit, TextLabel, "anchorClicked", SIGNAL_ANCHOR_CLICKED)
148 DALI_SIGNAL_REGISTRATION(Toolkit, TextLabel, "textFitChanged", SIGNAL_TEXT_FIT_CHANGED)
149
150 DALI_TYPE_REGISTRATION_END()
151 // clang-format on
152
153 /// Parses the property map for the TEXT_FIT property
154 void ParseTextFitProperty(Text::ControllerPtr& controller, const Property::Map* propertiesMap)
155 {
156   if(propertiesMap && !propertiesMap->Empty())
157   {
158     bool                     enabled      = false;
159     float                    minSize      = 0.f;
160     float                    maxSize      = 0.f;
161     float                    stepSize     = 0.f;
162     bool                     isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
163     Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
164
165     const unsigned int numberOfItems = propertiesMap->Count();
166
167     // Parses and applies
168     for(unsigned int index = 0u; index < numberOfItems; ++index)
169     {
170       const KeyValuePair& valueGet = propertiesMap->GetKeyValue(index);
171
172       if((Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey) || (TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey))
173       {
174         /// Enable key.
175         enabled = valueGet.second.Get<bool>();
176       }
177       else if((Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey))
178       {
179         /// min size.
180         minSize      = valueGet.second.Get<float>();
181         isMinSizeSet = true;
182       }
183       else if((Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey) || (TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey))
184       {
185         /// max size.
186         maxSize      = valueGet.second.Get<float>();
187         isMaxSizeSet = true;
188       }
189       else if((Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey) || (TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey))
190       {
191         /// step size.
192         stepSize      = valueGet.second.Get<float>();
193         isStepSizeSet = true;
194       }
195       else if((Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey) || (TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey))
196       {
197         if("pixelSize" == valueGet.second.Get<std::string>())
198         {
199           type = Controller::FontSizeType::PIXEL_SIZE;
200         }
201       }
202     }
203
204     controller->SetTextFitEnabled(enabled);
205     if(isMinSizeSet)
206     {
207       controller->SetTextFitMinSize(minSize, type);
208     }
209     if(isMaxSizeSet)
210     {
211       controller->SetTextFitMaxSize(maxSize, type);
212     }
213     if(isStepSizeSet)
214     {
215       controller->SetTextFitStepSize(stepSize, type);
216     }
217   }
218 }
219
220 } // namespace
221
222 Toolkit::TextLabel TextLabel::New()
223 {
224   // Create the implementation, temporarily owned by this handle on stack
225   IntrusivePtr<TextLabel> impl = new TextLabel();
226
227   // Pass ownership to CustomActor handle
228   Toolkit::TextLabel handle(*impl);
229
230   // Second-phase init of the implementation
231   // This can only be done after the CustomActor connection has been made...
232   impl->Initialize();
233
234   return handle;
235 }
236
237 void TextLabel::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
238 {
239   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
240
241   if(label)
242   {
243     TextLabel& impl(GetImpl(label));
244     DALI_ASSERT_ALWAYS(impl.mController && "No text contoller");
245
246     switch(index)
247     {
248       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
249       {
250         int backend = value.Get<int>();
251
252 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
253         if(DevelText::RENDERING_VECTOR_BASED == backend)
254         {
255           backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
256         }
257 #endif
258         if(impl.mRenderingBackend != backend)
259         {
260           impl.mRenderingBackend = backend;
261           impl.mTextUpdateNeeded = true;
262
263           // When using the vector-based rendering, the size of the GLyphs are different
264           TextAbstraction::GlyphType glyphType = (DevelText::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
265           impl.mController->SetGlyphType(glyphType);
266         }
267         break;
268       }
269       case Toolkit::TextLabel::Property::TEXT:
270       {
271         impl.mController->SetText(value.Get<std::string>());
272
273         if(impl.mController->HasAnchors())
274         {
275           // Forward input events to controller
276           impl.EnableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
277         }
278         else
279         {
280           impl.DisableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
281         }
282
283         break;
284       }
285       case Toolkit::TextLabel::Property::FONT_FAMILY:
286       {
287         const std::string& fontFamily = value.Get<std::string>();
288
289         DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str());
290         impl.mController->SetDefaultFontFamily(fontFamily);
291         break;
292       }
293       case Toolkit::TextLabel::Property::FONT_STYLE:
294       {
295         SetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
296         break;
297       }
298       case Toolkit::TextLabel::Property::POINT_SIZE:
299       {
300         const float pointSize = value.Get<float>();
301
302         if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE), pointSize))
303         {
304           impl.mController->SetDefaultFontSize(pointSize, Text::Controller::POINT_SIZE);
305         }
306         break;
307       }
308       case Toolkit::TextLabel::Property::MULTI_LINE:
309       {
310         impl.mController->SetMultiLineEnabled(value.Get<bool>());
311         break;
312       }
313       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
314       {
315         Text::HorizontalAlignment::Type alignment(static_cast<Text::HorizontalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
316         if(Text::GetHorizontalAlignmentEnumeration(value, alignment))
317         {
318           impl.mController->SetHorizontalAlignment(alignment);
319         }
320         break;
321       }
322       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
323       {
324         Toolkit::Text::VerticalAlignment::Type alignment(static_cast<Text::VerticalAlignment::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
325         if(Text::GetVerticalAlignmentEnumeration(value, alignment))
326         {
327           impl.mController->SetVerticalAlignment(alignment);
328         }
329         break;
330       }
331       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
332       {
333         const bool enableMarkup = value.Get<bool>();
334         impl.mController->SetMarkupProcessorEnabled(enableMarkup);
335
336         if(impl.mController->HasAnchors())
337         {
338           // Forward input events to controller
339           impl.EnableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
340         }
341         else
342         {
343           impl.DisableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP));
344         }
345         break;
346       }
347       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
348       {
349         const bool enableAutoScroll = value.Get<bool>();
350         // If request to auto scroll is the same as current state then do nothing.
351         if(enableAutoScroll != impl.mController->IsAutoScrollEnabled())
352         {
353           // If request is disable (false) and auto scrolling is enabled then need to stop it
354           if(enableAutoScroll == false)
355           {
356             if(impl.mTextScroller)
357             {
358               impl.mTextScroller->StopScrolling();
359             }
360           }
361           // If request is enable (true) then start autoscroll as not already running
362           else
363           {
364             impl.mController->SetAutoScrollEnabled(enableAutoScroll);
365           }
366         }
367         break;
368       }
369       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
370       {
371         Text::TextScrollerPtr                        textScroller = impl.GetTextScroller();
372         Toolkit::TextLabel::AutoScrollStopMode::Type stopMode     = textScroller->GetStopMode();
373         if(Scripting::GetEnumerationProperty<Toolkit::TextLabel::AutoScrollStopMode::Type>(value,
374                                                                                            AUTO_SCROLL_STOP_MODE_TABLE,
375                                                                                            AUTO_SCROLL_STOP_MODE_TABLE_COUNT,
376                                                                                            stopMode))
377         {
378           textScroller->SetStopMode(stopMode);
379         }
380         break;
381       }
382       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
383       {
384         impl.GetTextScroller()->SetSpeed(value.Get<int>());
385         break;
386       }
387       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
388       {
389         impl.GetTextScroller()->SetLoopCount(value.Get<int>());
390         break;
391       }
392       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
393       {
394         impl.GetTextScroller()->SetLoopDelay(value.Get<float>());
395         break;
396       }
397       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
398       {
399         impl.GetTextScroller()->SetGap(value.Get<float>());
400         break;
401       }
402       case Toolkit::TextLabel::Property::LINE_SPACING:
403       {
404         const float lineSpacing = value.Get<float>();
405         impl.mTextUpdateNeeded  = impl.mController->SetDefaultLineSpacing(lineSpacing) || impl.mTextUpdateNeeded;
406         break;
407       }
408       case Toolkit::TextLabel::Property::UNDERLINE:
409       {
410         impl.mTextUpdateNeeded = SetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
411         break;
412       }
413       case Toolkit::TextLabel::Property::SHADOW:
414       {
415         impl.mTextUpdateNeeded = SetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
416         break;
417       }
418       case Toolkit::TextLabel::Property::EMBOSS:
419       {
420         impl.mTextUpdateNeeded = SetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
421         break;
422       }
423       case Toolkit::TextLabel::Property::OUTLINE:
424       {
425         impl.mTextUpdateNeeded = SetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
426         break;
427       }
428       case Toolkit::TextLabel::Property::PIXEL_SIZE:
429       {
430         const float pixelSize = value.Get<float>();
431         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize);
432
433         if(!Equals(impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE), pixelSize))
434         {
435           impl.mController->SetDefaultFontSize(pixelSize, Text::Controller::PIXEL_SIZE);
436         }
437         break;
438       }
439       case Toolkit::TextLabel::Property::ELLIPSIS:
440       {
441         const bool ellipsis = value.Get<bool>();
442         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis);
443
444         impl.mController->SetTextElideEnabled(ellipsis);
445         break;
446       }
447       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
448       {
449         Text::LineWrap::Mode lineWrapMode(static_cast<Text::LineWrap::Mode>(-1)); // Set to invalid value to ensure a valid mode does get set
450         if(GetLineWrapModeEnumeration(value, lineWrapMode))
451         {
452           DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode);
453           impl.mController->SetLineWrapMode(lineWrapMode);
454         }
455         break;
456       }
457       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
458       {
459         if(impl.mController->GetTextModel())
460         {
461           DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>(value.Get<int>());
462
463           impl.mController->SetVerticalLineAlignment(alignment);
464
465           // Property doesn't affect the layout, only Visual must be updated
466           TextVisual::EnableRendererUpdate(impl.mVisual);
467
468           // No need to trigger full re-layout. Instead call UpdateRenderer() directly
469           TextVisual::UpdateRenderer(impl.mVisual);
470         }
471         break;
472       }
473       case Toolkit::DevelTextLabel::Property::BACKGROUND:
474       {
475         impl.mTextUpdateNeeded = SetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT) || impl.mTextUpdateNeeded;
476         break;
477       }
478       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
479       {
480         impl.mController->SetIgnoreSpacesAfterText(value.Get<bool>());
481         break;
482       }
483       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
484       {
485         impl.mController->SetMatchLayoutDirection(value.Get<bool>() ? DevelText::MatchLayoutDirection::LOCALE : DevelText::MatchLayoutDirection::CONTENTS);
486         break;
487       }
488       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
489       {
490         ParseTextFitProperty(impl.mController, value.GetMap());
491         impl.mController->SetTextFitChanged(true);
492         break;
493       }
494       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
495       {
496         const float lineSize   = value.Get<float>();
497         impl.mTextUpdateNeeded = impl.mController->SetDefaultLineSize(lineSize) || impl.mTextUpdateNeeded;
498         break;
499       }
500       case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
501       {
502         const float scale = value.Get<float>();
503         DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p FONT_SIZE_SCALE %f\n", impl.mController.Get(), scale);
504
505         if(!Equals(impl.mController->GetFontSizeScale(), scale))
506         {
507           impl.mController->SetFontSizeScale(scale);
508         }
509         break;
510       }
511       case Toolkit::DevelTextLabel::Property::ELLIPSIS_POSITION:
512       {
513         DevelText::EllipsisPosition::Type ellipsisPositionType(static_cast<DevelText::EllipsisPosition::Type>(-1)); // Set to invalid value to ensure a valid mode does get set
514         if(GetEllipsisPositionTypeEnumeration(value, ellipsisPositionType))
515         {
516           DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel %p EllipsisPosition::Type %d\n", impl.mController.Get(), ellipsisPositionType);
517           impl.mController->SetEllipsisPosition(ellipsisPositionType);
518         }
519         break;
520       }
521     }
522
523     // Request relayout when text update is needed. It's necessary to call it
524     // as changing the property not via UI interaction brings no effect if only
525     // the mTextUpdateNeeded is changed.
526     if(impl.mTextUpdateNeeded)
527     {
528       // need to request relayout as size of text may have changed
529       impl.RequestTextRelayout();
530     }
531   }
532 }
533
534 Text::ControllerPtr TextLabel::GetTextController()
535 {
536   return mController;
537 }
538
539 Property::Value TextLabel::GetProperty(BaseObject* object, Property::Index index)
540 {
541   Property::Value value;
542
543   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(Dali::BaseHandle(object));
544
545   if(label)
546   {
547     TextLabel& impl(GetImpl(label));
548     DALI_ASSERT_DEBUG(impl.mController && "No text contoller");
549
550     switch(index)
551     {
552       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
553       {
554         value = impl.mRenderingBackend;
555         break;
556       }
557       case Toolkit::TextLabel::Property::TEXT:
558       {
559         std::string text;
560         impl.mController->GetText(text);
561         value = text;
562         break;
563       }
564       case Toolkit::TextLabel::Property::FONT_FAMILY:
565       {
566         value = impl.mController->GetDefaultFontFamily();
567         break;
568       }
569       case Toolkit::TextLabel::Property::FONT_STYLE:
570       {
571         GetFontStyleProperty(impl.mController, value, Text::FontStyle::DEFAULT);
572         break;
573       }
574       case Toolkit::TextLabel::Property::POINT_SIZE:
575       {
576         value = impl.mController->GetDefaultFontSize(Text::Controller::POINT_SIZE);
577         break;
578       }
579       case Toolkit::TextLabel::Property::MULTI_LINE:
580       {
581         value = impl.mController->IsMultiLineEnabled();
582         break;
583       }
584       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
585       {
586         const char* name = Text::GetHorizontalAlignmentString(impl.mController->GetHorizontalAlignment());
587
588         if(name)
589         {
590           value = std::string(name);
591         }
592         break;
593       }
594       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
595       {
596         const char* name = Text::GetVerticalAlignmentString(impl.mController->GetVerticalAlignment());
597         if(name)
598         {
599           value = std::string(name);
600         }
601         break;
602       }
603       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
604       {
605         value = impl.mController->IsMarkupProcessorEnabled();
606         break;
607       }
608       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
609       {
610         value = impl.mController->IsAutoScrollEnabled();
611         break;
612       }
613       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
614       {
615         if(impl.mTextScroller)
616         {
617           const char* mode = Scripting::GetEnumerationName<Toolkit::TextLabel::AutoScrollStopMode::Type>(impl.mTextScroller->GetStopMode(),
618                                                                                                          AUTO_SCROLL_STOP_MODE_TABLE,
619                                                                                                          AUTO_SCROLL_STOP_MODE_TABLE_COUNT);
620           if(mode)
621           {
622             value = std::string(mode);
623           }
624         }
625         break;
626       }
627       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
628       {
629         if(impl.mTextScroller)
630         {
631           value = impl.mTextScroller->GetSpeed();
632         }
633         break;
634       }
635       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
636       {
637         if(impl.mTextScroller)
638         {
639           value = impl.mTextScroller->GetLoopCount();
640         }
641         break;
642       }
643       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
644       {
645         if(impl.mTextScroller)
646         {
647           value = impl.mTextScroller->GetLoopDelay();
648         }
649         break;
650       }
651       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
652       {
653         if(impl.mTextScroller)
654         {
655           value = impl.mTextScroller->GetGap();
656         }
657         break;
658       }
659       case Toolkit::TextLabel::Property::LINE_SPACING:
660       {
661         value = impl.mController->GetDefaultLineSpacing();
662         break;
663       }
664       case Toolkit::TextLabel::Property::UNDERLINE:
665       {
666         GetUnderlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
667         break;
668       }
669       case Toolkit::TextLabel::Property::SHADOW:
670       {
671         GetShadowProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
672         break;
673       }
674       case Toolkit::TextLabel::Property::EMBOSS:
675       {
676         GetEmbossProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
677         break;
678       }
679       case Toolkit::TextLabel::Property::OUTLINE:
680       {
681         GetOutlineProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
682         break;
683       }
684       case Toolkit::TextLabel::Property::PIXEL_SIZE:
685       {
686         value = impl.mController->GetDefaultFontSize(Text::Controller::PIXEL_SIZE);
687         break;
688       }
689       case Toolkit::TextLabel::Property::ELLIPSIS:
690       {
691         value = impl.mController->IsTextElideEnabled();
692         break;
693       }
694       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
695       {
696         value = impl.mController->GetLineWrapMode();
697         break;
698       }
699       case Toolkit::TextLabel::Property::LINE_COUNT:
700       {
701         float width = label.GetProperty(Actor::Property::SIZE_WIDTH).Get<float>();
702         value       = impl.mController->GetLineCount(width);
703         break;
704       }
705       case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
706       {
707         value = impl.mController->GetTextDirection();
708         break;
709       }
710       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
711       {
712         value = impl.mController->GetVerticalLineAlignment();
713         break;
714       }
715       case Toolkit::DevelTextLabel::Property::BACKGROUND:
716       {
717         GetBackgroundProperties(impl.mController, value, Text::EffectStyle::DEFAULT);
718         break;
719       }
720       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
721       {
722         value = impl.mController->IsIgnoreSpacesAfterText();
723         break;
724       }
725       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
726       {
727         value = impl.mController->GetMatchLayoutDirection() != DevelText::MatchLayoutDirection::CONTENTS;
728         break;
729       }
730       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
731       {
732         const bool  enabled   = impl.mController->IsTextFitEnabled();
733         const float minSize   = impl.mController->GetTextFitMinSize();
734         const float maxSize   = impl.mController->GetTextFitMaxSize();
735         const float stepSize  = impl.mController->GetTextFitStepSize();
736         const float pointSize = impl.mController->GetTextFitPointSize();
737
738         Property::Map map;
739         map.Insert(TEXT_FIT_ENABLE_KEY, enabled);
740         map.Insert(TEXT_FIT_MIN_SIZE_KEY, minSize);
741         map.Insert(TEXT_FIT_MAX_SIZE_KEY, maxSize);
742         map.Insert(TEXT_FIT_STEP_SIZE_KEY, stepSize);
743         map.Insert(TEXT_FIT_FONT_SIZE_KEY, pointSize);
744         map.Insert(TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize");
745
746         value = map;
747         break;
748       }
749       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
750       {
751         value = impl.mController->GetDefaultLineSize();
752         break;
753       }
754       case Toolkit::DevelTextLabel::Property::FONT_SIZE_SCALE:
755       {
756         value = impl.mController->GetFontSizeScale();
757         break;
758       }
759       case Toolkit::DevelTextLabel::Property::ELLIPSIS_POSITION:
760       {
761         value = impl.mController->GetEllipsisPosition();
762         break;
763       }
764     }
765   }
766
767   return value;
768 }
769
770 bool TextLabel::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
771 {
772   Dali::BaseHandle handle(object);
773
774   bool               connected(true);
775   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast(handle);
776
777   if(0 == strcmp(signalName.c_str(), SIGNAL_ANCHOR_CLICKED))
778   {
779     if(label)
780     {
781       Internal::TextLabel& labelImpl(GetImpl(label));
782       labelImpl.AnchorClickedSignal().Connect(tracker, functor);
783     }
784   }
785   else if(0 == strcmp(signalName.c_str(), SIGNAL_TEXT_FIT_CHANGED))
786   {
787     if(label)
788     {
789       Internal::TextLabel& labelImpl(GetImpl(label));
790       labelImpl.TextFitChangedSignal().Connect(tracker, functor);
791     }
792   }
793   else
794   {
795     // signalName does not match any signal
796     connected = false;
797   }
798
799   return connected;
800 }
801
802 DevelTextLabel::AnchorClickedSignalType& TextLabel::AnchorClickedSignal()
803 {
804   return mAnchorClickedSignal;
805 }
806
807 DevelTextLabel::TextFitChangedSignalType& TextLabel::TextFitChangedSignal()
808 {
809   return mTextFitChangedSignal;
810 }
811
812 void TextLabel::OnInitialize()
813 {
814   Actor self = Self();
815
816   Property::Map propertyMap;
817   propertyMap.Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT);
818
819   mVisual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
820   DevelControl::RegisterVisual(*this, Toolkit::TextLabel::Property::TEXT, mVisual);
821
822   TextVisual::SetAnimatableTextColorProperty(mVisual, Toolkit::TextLabel::Property::TEXT_COLOR);
823
824   mController = TextVisual::GetController(mVisual);
825   DALI_ASSERT_DEBUG(mController && "Invalid Text Controller")
826
827   mController->SetControlInterface(this);
828   mController->SetAnchorControlInterface(this);
829
830   // Use height-for-width negotiation by default
831   self.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
832   self.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
833
834   // Enable highlightability
835   self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
836
837   // Enable the text ellipsis.
838   mController->SetTextElideEnabled(true); // If false then text larger than control will overflow
839
840   // Sets layoutDirection value
841   Dali::Stage                 stage           = Dali::Stage::GetCurrent();
842   Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(stage.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
843   mController->SetLayoutDirection(layoutDirection);
844
845   self.LayoutDirectionChangedSignal().Connect(this, &TextLabel::OnLayoutDirectionChanged);
846
847   Layout::Engine& engine = mController->GetLayoutEngine();
848   engine.SetCursorWidth(0u); // Do not layout space for the cursor.
849
850   DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
851     return std::unique_ptr<Dali::Accessibility::Accessible>(
852       new AccessibleImpl(actor, Dali::Accessibility::Role::LABEL));
853   });
854
855   Accessibility::Bridge::EnabledSignal().Connect(this, &TextLabel::OnAccessibilityStatusChanged);
856   Accessibility::Bridge::DisabledSignal().Connect(this, &TextLabel::OnAccessibilityStatusChanged);
857 }
858
859 void TextLabel::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
860 {
861   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
862
863   switch(change)
864   {
865     case StyleChange::DEFAULT_FONT_CHANGE:
866     {
867       // Property system did not set the font so should update it.
868       const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
869       DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str());
870       mController->UpdateAfterFontChange(newFont);
871       RelayoutRequest();
872       break;
873     }
874     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
875     {
876       GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
877       RelayoutRequest();
878       break;
879     }
880     case StyleChange::THEME_CHANGE:
881     {
882       // Nothing to do, let control base class handle this
883       break;
884     }
885   }
886
887   // Up call to Control
888   Control::OnStyleChange(styleManager, change);
889 }
890
891 void TextLabel::OnTap(const TapGesture& gesture)
892 {
893   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnTap %p\n", mController.Get());
894
895   // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
896   Extents padding;
897   padding                   = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
898   const Vector2& localPoint = gesture.GetLocalPoint();
899   mController->AnchorEvent(localPoint.x - padding.start, localPoint.y - padding.top);
900 }
901
902 void TextLabel::AnchorClicked(const std::string& href)
903 {
904   Dali::Toolkit::TextLabel handle(GetOwner());
905   mAnchorClickedSignal.Emit(handle, href.c_str(), href.length());
906 }
907
908 Vector3 TextLabel::GetNaturalSize()
909 {
910   Extents padding;
911   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
912
913   Vector3 naturalSize = mController->GetNaturalSize();
914   naturalSize.width += (padding.start + padding.end);
915   naturalSize.height += (padding.top + padding.bottom);
916
917   return naturalSize;
918 }
919
920 float TextLabel::GetHeightForWidth(float width)
921 {
922   Extents padding;
923   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
924
925   return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
926 }
927
928 void TextLabel::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
929 {
930   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index);
931
932   switch(index)
933   {
934     case Toolkit::TextLabel::Property::TEXT_COLOR:
935     {
936       const Vector4& textColor = propertyValue.Get<Vector4>();
937       if(mController->GetDefaultColor() != textColor)
938       {
939         mController->SetDefaultColor(textColor);
940         mTextUpdateNeeded = true;
941       }
942       break;
943     }
944     case Toolkit::TextLabel::Property::TEXT:
945     case Toolkit::TextLabel::Property::ENABLE_MARKUP:
946     {
947       CommonTextUtils::SynchronizeTextAnchorsInParent(Self(), mController, mAnchorActors);
948       break;
949     }
950     default:
951     {
952       Control::OnPropertySet(index, propertyValue); // up call to control for non-handled properties
953       break;
954     }
955   }
956 }
957
958 void TextLabel::OnRelayout(const Vector2& size, RelayoutContainer& container)
959 {
960   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout\n");
961
962   Actor self = Self();
963
964   Extents padding;
965   padding = self.GetProperty<Extents>(Toolkit::Control::Property::PADDING);
966
967   Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
968
969   if(mController->IsTextFitEnabled())
970   {
971     mController->FitPointSizeforLayout(contentSize);
972     mController->SetTextFitContentSize(contentSize);
973   }
974
975   // Support Right-To-Left
976   Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(self);
977
978   const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
979
980   if((Text::Controller::NONE_UPDATED != (Text::Controller::MODEL_UPDATED & updateTextType)) || mTextUpdateNeeded)
981   {
982     DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", (mController->IsAutoScrollEnabled()) ? "true" : "false", this);
983
984     // Update the visual
985     TextVisual::EnableRendererUpdate(mVisual);
986
987     // Support Right-To-Left of padding
988     if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
989     {
990       std::swap(padding.start, padding.end);
991     }
992
993     // Calculate the size of the visual that can fit the text
994     Size layoutSize = mController->GetTextModel()->GetLayoutSize();
995     layoutSize.x    = contentSize.x;
996
997     const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
998     if(shadowOffset.y > Math::MACHINE_EPSILON_1)
999     {
1000       layoutSize.y += shadowOffset.y;
1001     }
1002
1003     float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
1004     layoutSize.y += outlineWidth * 2.0f;
1005     layoutSize.y = std::min(layoutSize.y, contentSize.y);
1006
1007     // Calculate the offset for vertical alignment only, as the layout engine will do the horizontal alignment.
1008     Vector2 alignmentOffset;
1009     alignmentOffset.x = 0.0f;
1010     alignmentOffset.y = (contentSize.y - layoutSize.y) * VERTICAL_ALIGNMENT_TABLE[mController->GetVerticalAlignment()];
1011
1012     Property::Map visualTransform;
1013     visualTransform.Add(Toolkit::Visual::Transform::Property::SIZE, layoutSize)
1014       .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
1015       .Add(Toolkit::Visual::Transform::Property::OFFSET, Vector2(padding.start, padding.top) + alignmentOffset)
1016       .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
1017       .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
1018       .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN);
1019     mVisual.SetTransformAndSize(visualTransform, size);
1020
1021     if(mController->IsAutoScrollEnabled())
1022     {
1023       SetUpAutoScrolling();
1024     }
1025
1026     mTextUpdateNeeded = false;
1027   }
1028
1029   if(mController->IsTextFitChanged())
1030   {
1031     EmitTextFitChangedSignal();
1032     mController->SetTextFitChanged(false);
1033   }
1034 }
1035
1036 void TextLabel::RequestTextRelayout()
1037 {
1038   RelayoutRequest();
1039   // Signal that a Relayout may be needed
1040 }
1041
1042 void TextLabel::SetUpAutoScrolling()
1043 {
1044   const Size&                    controlSize     = mController->GetView().GetControlSize();
1045   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.
1046   const Text::CharacterDirection direction       = mController->GetAutoScrollDirection();
1047
1048   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n", textNaturalSize.x, textNaturalSize.y, controlSize.x, controlSize.y);
1049
1050   if(!mTextScroller)
1051   {
1052     DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n");
1053
1054     // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
1055     mTextScroller = Text::TextScroller::New(*this);
1056   }
1057
1058   // Calculate the actual gap before scrolling wraps.
1059   int     textPadding = std::max(controlSize.x - textNaturalSize.x, 0.0f);
1060   float   wrapGap     = std::max(mTextScroller->GetGap(), textPadding);
1061   Vector2 textureSize = textNaturalSize + Vector2(wrapGap, 0.0f); // Add the gap as a part of the texture
1062
1063   // Create a texture of the text for scrolling
1064   Size      verifiedSize   = textureSize;
1065   const int maxTextureSize = Dali::GetMaxTextureSize();
1066
1067   //if the texture size width exceed maxTextureSize, modify the visual model size and enabled the ellipsis
1068   bool actualellipsis = mController->IsTextElideEnabled();
1069   if(verifiedSize.width > maxTextureSize)
1070   {
1071     verifiedSize.width = maxTextureSize;
1072     if(textNaturalSize.width > maxTextureSize)
1073     {
1074       mController->SetTextElideEnabled(true);
1075     }
1076     GetHeightForWidth(maxTextureSize);
1077     wrapGap = std::max(maxTextureSize - textNaturalSize.width, 0.0f);
1078   }
1079
1080   Text::TypesetterPtr typesetter = Text::Typesetter::New(mController->GetTextModel());
1081
1082   PixelData data    = typesetter->Render(verifiedSize, mController->GetTextDirection(), Text::Typesetter::RENDER_TEXT_AND_STYLES, true, Pixel::RGBA8888); // ignore the horizontal alignment
1083   Texture   texture = Texture::New(Dali::TextureType::TEXTURE_2D,
1084                                  data.GetPixelFormat(),
1085                                  data.GetWidth(),
1086                                  data.GetHeight());
1087   texture.Upload(data);
1088
1089   TextureSet textureSet = TextureSet::New();
1090   textureSet.SetTexture(0u, texture);
1091
1092   // Filter mode needs to be set to linear to produce better quality while scaling.
1093   Sampler sampler = Sampler::New();
1094   sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR);
1095   sampler.SetWrapMode(Dali::WrapMode::DEFAULT, Dali::WrapMode::REPEAT, Dali::WrapMode::DEFAULT); // Wrap the texture in the x direction
1096   textureSet.SetSampler(0u, sampler);
1097
1098   // Set parameters for scrolling
1099   Renderer renderer = static_cast<Internal::Visual::Base&>(GetImplementation(mVisual)).GetRenderer();
1100   mTextScroller->SetParameters(Self(), renderer, textureSet, controlSize, verifiedSize, wrapGap, direction, mController->GetHorizontalAlignment(), mController->GetVerticalAlignment());
1101   mController->SetTextElideEnabled(actualellipsis);
1102 }
1103
1104 void TextLabel::ScrollingFinished()
1105 {
1106   // Pure Virtual from TextScroller Interface
1107   DALI_LOG_INFO(gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
1108   mController->SetAutoScrollEnabled(false);
1109   RequestTextRelayout();
1110 }
1111
1112 void TextLabel::OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type)
1113 {
1114   mController->ChangedLayoutDirection();
1115 }
1116
1117 void TextLabel::EmitTextFitChangedSignal()
1118 {
1119   Dali::Toolkit::TextLabel handle(GetOwner());
1120   mTextFitChangedSignal.Emit(handle);
1121 }
1122
1123 void TextLabel::OnAccessibilityStatusChanged()
1124 {
1125   CommonTextUtils::SynchronizeTextAnchorsInParent(Self(), mController, mAnchorActors);
1126 }
1127
1128 TextLabel::TextLabel()
1129 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
1130   mRenderingBackend(DEFAULT_RENDERING_BACKEND),
1131   mTextUpdateNeeded(false)
1132 {
1133 }
1134
1135 TextLabel::~TextLabel()
1136 {
1137 }
1138
1139 Vector<Vector2> TextLabel::GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const
1140 {
1141   return mController->GetTextSize(startIndex, endIndex);
1142 }
1143
1144 Vector<Vector2> TextLabel::GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const
1145 {
1146   return mController->GetTextPosition(startIndex, endIndex);
1147 }
1148
1149 std::string TextLabel::AccessibleImpl::GetNameRaw() const
1150 {
1151   auto self = Toolkit::TextLabel::DownCast(Self());
1152   return self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1153 }
1154
1155 Property::Index TextLabel::AccessibleImpl::GetNamePropertyIndex()
1156 {
1157   return Toolkit::TextLabel::Property::TEXT;
1158 }
1159
1160 std::string TextLabel::AccessibleImpl::GetText(size_t startOffset, size_t endOffset) const
1161 {
1162   if(endOffset <= startOffset)
1163   {
1164     return {};
1165   }
1166
1167   auto self = Toolkit::TextLabel::DownCast(Self());
1168   auto text = self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1169
1170   if(startOffset > text.size() || endOffset > text.size())
1171   {
1172     return {};
1173   }
1174
1175   return text.substr(startOffset, endOffset - startOffset);
1176 }
1177
1178 size_t TextLabel::AccessibleImpl::GetCharacterCount() const
1179 {
1180   auto self = Toolkit::TextLabel::DownCast(Self());
1181   auto text = self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1182
1183   return text.size();
1184 }
1185
1186 size_t TextLabel::AccessibleImpl::GetCursorOffset() const
1187 {
1188   return {};
1189 }
1190
1191 bool TextLabel::AccessibleImpl::SetCursorOffset(size_t offset)
1192 {
1193   return {};
1194 }
1195
1196 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetTextAtOffset(size_t offset, Dali::Accessibility::TextBoundary boundary) const
1197 {
1198   auto self     = Toolkit::TextLabel::DownCast(Self());
1199   auto text     = self.GetProperty(Toolkit::TextLabel::Property::TEXT).Get<std::string>();
1200   auto textSize = text.size();
1201
1202   auto range = Dali::Accessibility::Range{};
1203
1204   switch(boundary)
1205   {
1206     case Dali::Accessibility::TextBoundary::CHARACTER:
1207     {
1208       if(offset < textSize)
1209       {
1210         range.content     = text[offset];
1211         range.startOffset = offset;
1212         range.endOffset   = offset + 1;
1213       }
1214       break;
1215     }
1216     case Dali::Accessibility::TextBoundary::WORD:
1217     case Dali::Accessibility::TextBoundary::LINE:
1218     {
1219       auto textString = text.c_str();
1220       auto breaks     = std::vector<char>(textSize, 0);
1221
1222       if(boundary == Dali::Accessibility::TextBoundary::WORD)
1223       {
1224         Accessibility::Accessible::FindWordSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1225       }
1226       else
1227       {
1228         Accessibility::Accessible::FindLineSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1229       }
1230
1231       auto index   = 0u;
1232       auto counter = 0u;
1233       while(index < textSize && counter <= offset)
1234       {
1235         auto start = index;
1236         if(breaks[index])
1237         {
1238           while(breaks[index])
1239           {
1240             index++;
1241           }
1242           counter++;
1243         }
1244         else
1245         {
1246           if(boundary == Dali::Accessibility::TextBoundary::WORD)
1247           {
1248             index++;
1249           }
1250           if(boundary == Dali::Accessibility::TextBoundary::LINE)
1251           {
1252             counter++;
1253           }
1254         }
1255
1256         if((counter > 0) && ((counter - 1) == offset))
1257         {
1258           range.content     = text.substr(start, index - start + 1);
1259           range.startOffset = start;
1260           range.endOffset   = index + 1;
1261         }
1262
1263         if(boundary == Dali::Accessibility::TextBoundary::LINE)
1264         {
1265           index++;
1266         }
1267       }
1268       break;
1269     }
1270     case Dali::Accessibility::TextBoundary::SENTENCE:
1271     {
1272       /* not supported by default */
1273       break;
1274     }
1275     case Dali::Accessibility::TextBoundary::PARAGRAPH:
1276     {
1277       /* Paragraph is not supported by libunibreak library */
1278       break;
1279     }
1280     default:
1281       break;
1282   }
1283
1284   return range;
1285 }
1286
1287 Dali::Accessibility::Range TextLabel::AccessibleImpl::GetRangeOfSelection(size_t selectionIndex) const
1288 {
1289   // Since DALi supports only one selection indexes higher than 0 are ignored
1290   if(selectionIndex > 0)
1291   {
1292     return {};
1293   }
1294
1295   auto        self       = Toolkit::TextLabel::DownCast(Self());
1296   auto        controller = Dali::Toolkit::GetImpl(self).GetTextController();
1297   std::string value{};
1298   controller->RetrieveSelection(value);
1299   auto indices = controller->GetSelectionIndexes();
1300
1301   return {static_cast<size_t>(indices.first), static_cast<size_t>(indices.second), value};
1302 }
1303
1304 bool TextLabel::AccessibleImpl::RemoveSelection(size_t selectionIndex)
1305 {
1306   // Since DALi supports only one selection indexes higher than 0 are ignored
1307   if(selectionIndex > 0)
1308   {
1309     return false;
1310   }
1311
1312   auto self = Toolkit::TextLabel::DownCast(Self());
1313   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(0, 0);
1314   return true;
1315 }
1316
1317 bool TextLabel::AccessibleImpl::SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset)
1318 {
1319   // Since DALi supports only one selection indexes higher than 0 are ignored
1320   if(selectionIndex > 0)
1321   {
1322     return false;
1323   }
1324
1325   auto self = Toolkit::TextLabel::DownCast(Self());
1326   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(startOffset, endOffset);
1327   return true;
1328 }
1329
1330 int32_t TextLabel::AccessibleImpl::GetLinkCount() const
1331 {
1332   auto self = Toolkit::TextLabel::DownCast(Self());
1333   return Dali::Toolkit::GetImpl(self).mAnchorActors.size();
1334 }
1335
1336 Accessibility::Hyperlink* TextLabel::AccessibleImpl::GetLink(int32_t linkIndex) const
1337 {
1338   if(linkIndex < 0 || linkIndex >= GetLinkCount())
1339   {
1340     return nullptr;
1341   }
1342   auto self        = Toolkit::TextLabel::DownCast(Self());
1343   auto anchorActor = Dali::Toolkit::GetImpl(self).mAnchorActors[linkIndex];
1344   return dynamic_cast<Accessibility::Hyperlink*>(Dali::Accessibility::Accessible::Get(anchorActor));
1345 }
1346
1347 int32_t TextLabel::AccessibleImpl::GetLinkIndex(int32_t characterOffset) const
1348 {
1349   auto self       = Toolkit::TextLabel::DownCast(Self());
1350   auto controller = Dali::Toolkit::GetImpl(self).GetTextController();
1351   return controller->GetAnchorIndex(static_cast<size_t>(characterOffset));
1352 }
1353
1354 } // namespace Internal
1355
1356 } // namespace Toolkit
1357
1358 } // namespace Dali