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