c175cb2b49e1574b5efb1bebff79037f88c6f47b
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.cpp
1 /*
2  * Copyright (c) 2020 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/public-api/object/type-registry-helper.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/object/property-helper-devel.h>
25 #include <dali/devel-api/adaptor-framework/image-loading.h>
26 #include <dali/integration-api/debug.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/public-api/text/text-enumerations.h>
30 #include <dali-toolkit/devel-api/text/rendering-backend.h>
31 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
32 #include <dali-toolkit/internal/text/property-string-parser.h>
33 #include <dali-toolkit/internal/text/rendering/text-backend.h>
34 #include <dali-toolkit/internal/text/text-effects-style.h>
35 #include <dali-toolkit/internal/text/text-font-style.h>
36 #include <dali-toolkit/internal/text/text-view.h>
37 #include <dali-toolkit/internal/text/text-definitions.h>
38 #include <dali-toolkit/internal/styling/style-manager-impl.h>
39
40 #include <dali-toolkit/public-api/align-enumerations.h>
41 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
42 #include <dali-toolkit/devel-api/controls/control-devel.h>
43 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
44 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
45 #include <dali-toolkit/public-api/visuals/visual-properties.h>
46 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
47
48 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
49
50 using namespace Dali::Toolkit::Text;
51
52 namespace Dali
53 {
54
55 namespace Toolkit
56 {
57
58 namespace Internal
59 {
60
61 namespace
62 {
63   const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
64
65   /**
66    * @brief How the text visual should be aligned vertically inside the control.
67    *
68    * 0.0f aligns the text to the top, 0.5f aligns the text to the center, 1.0f aligns the text to the bottom.
69    * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
70    */
71   const float VERTICAL_ALIGNMENT_TABLE[ Text::VerticalAlignment::BOTTOM + 1 ] =
72   {
73     0.0f,  // VerticalAlignment::TOP
74     0.5f,  // VerticalAlignment::CENTER
75     1.0f   // VerticalAlignment::BOTTOM
76   };
77
78   const std::string TEXT_FIT_ENABLE_KEY( "enable" );
79   const std::string TEXT_FIT_MIN_SIZE_KEY( "minSize" );
80   const std::string TEXT_FIT_MAX_SIZE_KEY( "maxSize" );
81   const std::string TEXT_FIT_STEP_SIZE_KEY( "stepSize" );
82   const std::string TEXT_FIT_FONT_SIZE_TYPE_KEY( "fontSizeType" );
83 }
84
85 namespace
86 {
87
88 #if defined ( DEBUG_ENABLED )
89   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
90 #endif
91
92 const Scripting::StringEnum AUTO_SCROLL_STOP_MODE_TABLE[] =
93 {
94   { "IMMEDIATE", Toolkit::TextLabel::AutoScrollStopMode::IMMEDIATE },
95   { "FINISH_LOOP",  Toolkit::TextLabel::AutoScrollStopMode::FINISH_LOOP  },
96 };
97 const unsigned int AUTO_SCROLL_STOP_MODE_TABLE_COUNT = sizeof( AUTO_SCROLL_STOP_MODE_TABLE ) / sizeof( AUTO_SCROLL_STOP_MODE_TABLE[0] );
98
99 // Type registration
100 BaseHandle Create()
101 {
102   return Toolkit::TextLabel::New();
103 }
104
105 // Setup properties, signals and actions using the type-registry.
106 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
107
108 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "text",                      STRING,  TEXT                       )
109 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "fontFamily",                STRING,  FONT_FAMILY                )
110 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "fontStyle",                 MAP,     FONT_STYLE                 )
111 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "pointSize",                 FLOAT,   POINT_SIZE                 )
112 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "multiLine",                 BOOLEAN, MULTI_LINE                 )
113 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "horizontalAlignment",       STRING,  HORIZONTAL_ALIGNMENT       )
114 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "verticalAlignment",         STRING,  VERTICAL_ALIGNMENT         )
115 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "enableMarkup",              BOOLEAN, ENABLE_MARKUP              )
116 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "enableAutoScroll",          BOOLEAN, ENABLE_AUTO_SCROLL         )
117 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "autoScrollSpeed",           INTEGER, AUTO_SCROLL_SPEED          )
118 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "autoScrollLoopCount",       INTEGER, AUTO_SCROLL_LOOP_COUNT     )
119 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "autoScrollGap",             FLOAT,   AUTO_SCROLL_GAP            )
120 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "lineSpacing",               FLOAT,   LINE_SPACING               )
121 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "underline",                 MAP,     UNDERLINE                  )
122 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "shadow",                    MAP,     SHADOW                     )
123 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "emboss",                    MAP,     EMBOSS                     )
124 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "outline",                   MAP,     OUTLINE                    )
125 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "pixelSize",                 FLOAT,   PIXEL_SIZE                 )
126 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "ellipsis",                  BOOLEAN, ELLIPSIS                   )
127 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "autoScrollLoopDelay",       FLOAT,   AUTO_SCROLL_LOOP_DELAY     )
128 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "autoScrollStopMode",        STRING,  AUTO_SCROLL_STOP_MODE      )
129 DALI_PROPERTY_REGISTRATION_READ_ONLY( Toolkit, TextLabel, "lineCount",                 INTEGER, LINE_COUNT                 )
130 DALI_PROPERTY_REGISTRATION( Toolkit,           TextLabel, "lineWrapMode",              INTEGER, LINE_WRAP_MODE             )
131 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY( Toolkit, TextLabel, "textDirection",       INTEGER, TEXT_DIRECTION             )
132 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "verticalLineAlignment",     INTEGER, VERTICAL_LINE_ALIGNMENT    )
133 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "textBackground",            MAP,     BACKGROUND                 )
134 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "ignoreSpacesAfterText",     BOOLEAN, IGNORE_SPACES_AFTER_TEXT   )
135 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "matchSystemLanguageDirection", BOOLEAN, MATCH_SYSTEM_LANGUAGE_DIRECTION )
136 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "textFit",                   MAP,     TEXT_FIT                   )
137 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "minLineSize",               FLOAT,   MIN_LINE_SIZE              )
138 DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit,     TextLabel, "renderingBackend",          INTEGER, RENDERING_BACKEND          )
139 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, TextLabel, "textColor",      Color::BLACK,     TEXT_COLOR     )
140 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit,    TextLabel, "textColorRed",   TEXT_COLOR_RED,   TEXT_COLOR, 0  )
141 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit,    TextLabel, "textColorGreen", TEXT_COLOR_GREEN, TEXT_COLOR, 1  )
142 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit,    TextLabel, "textColorBlue",  TEXT_COLOR_BLUE,  TEXT_COLOR, 2  )
143 DALI_ANIMATABLE_PROPERTY_COMPONENT_REGISTRATION( Toolkit,    TextLabel, "textColorAlpha", TEXT_COLOR_ALPHA, TEXT_COLOR, 3  )
144 DALI_TYPE_REGISTRATION_END()
145
146 } // namespace
147
148 Toolkit::TextLabel TextLabel::New()
149 {
150   // Create the implementation, temporarily owned by this handle on stack
151   IntrusivePtr< TextLabel > impl = new TextLabel();
152
153   // Pass ownership to CustomActor handle
154   Toolkit::TextLabel handle( *impl );
155
156   // Second-phase init of the implementation
157   // This can only be done after the CustomActor connection has been made...
158   impl->Initialize();
159
160   return handle;
161 }
162
163 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
164 {
165   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
166
167   if( label )
168   {
169     TextLabel& impl( GetImpl( label ) );
170     switch( index )
171     {
172       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
173       {
174         int backend = value.Get< int >();
175
176 #ifndef ENABLE_VECTOR_BASED_TEXT_RENDERING
177         if( DevelText::RENDERING_VECTOR_BASED == backend )
178         {
179           backend = TextAbstraction::BITMAP_GLYPH; // Fallback to bitmap-based rendering
180         }
181 #endif
182         if( impl.mRenderingBackend != backend )
183         {
184           impl.mRenderingBackend = backend;
185           impl.mTextUpdateNeeded = true;
186
187           if( impl.mController )
188           {
189             // When using the vector-based rendering, the size of the GLyphs are different
190             TextAbstraction::GlyphType glyphType = (DevelText::RENDERING_VECTOR_BASED == impl.mRenderingBackend) ? TextAbstraction::VECTOR_GLYPH : TextAbstraction::BITMAP_GLYPH;
191             impl.mController->SetGlyphType( glyphType );
192           }
193         }
194         break;
195       }
196       case Toolkit::TextLabel::Property::TEXT:
197       {
198         if( impl.mController )
199         {
200           impl.mController->SetText( value.Get< std::string >() );
201         }
202         break;
203       }
204       case Toolkit::TextLabel::Property::FONT_FAMILY:
205       {
206         if( impl.mController )
207         {
208           const std::string& fontFamily = value.Get< std::string >();
209
210           DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::SetProperty Property::FONT_FAMILY newFont(%s)\n", fontFamily.c_str() );
211           impl.mController->SetDefaultFontFamily( fontFamily );
212         }
213         break;
214       }
215       case Toolkit::TextLabel::Property::FONT_STYLE:
216       {
217         SetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
218         break;
219       }
220       case Toolkit::TextLabel::Property::POINT_SIZE:
221       {
222         if( impl.mController )
223         {
224           const float pointSize = value.Get< float >();
225
226           if( !Equals( impl.mController->GetDefaultFontSize( Text::Controller::POINT_SIZE ), pointSize ) )
227           {
228             impl.mController->SetDefaultFontSize( pointSize, Text::Controller::POINT_SIZE );
229           }
230         }
231         break;
232       }
233       case Toolkit::TextLabel::Property::MULTI_LINE:
234       {
235         if( impl.mController )
236         {
237           impl.mController->SetMultiLineEnabled( value.Get< bool >() );
238         }
239         break;
240       }
241       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
242       {
243         if( impl.mController )
244         {
245           Text::HorizontalAlignment::Type alignment( static_cast< Text::HorizontalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
246           if( Text::GetHorizontalAlignmentEnumeration( value, alignment ) )
247           {
248             impl.mController->SetHorizontalAlignment( alignment );
249           }
250         }
251         break;
252       }
253       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
254       {
255         if( impl.mController )
256         {
257           Toolkit::Text::VerticalAlignment::Type alignment( static_cast< Text::VerticalAlignment::Type >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
258           if( Text::GetVerticalAlignmentEnumeration( value, alignment ) )
259           {
260             impl.mController->SetVerticalAlignment( alignment );
261           }
262         }
263         break;
264       }
265       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
266       {
267         if( impl.mController )
268         {
269           const bool enableMarkup = value.Get<bool>();
270           impl.mController->SetMarkupProcessorEnabled( enableMarkup );
271         }
272         break;
273       }
274       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
275       {
276         if( impl.mController )
277         {
278           const bool enableAutoScroll = value.Get<bool>();
279           // If request to auto scroll is the same as current state then do nothing.
280           if ( enableAutoScroll != impl.mController->IsAutoScrollEnabled() )
281           {
282              // If request is disable (false) and auto scrolling is enabled then need to stop it
283              if ( enableAutoScroll == false )
284              {
285                if( impl.mTextScroller )
286                {
287                  impl.mTextScroller->StopScrolling();
288                }
289              }
290              // If request is enable (true) then start autoscroll as not already running
291              else
292              {
293                impl.mController->SetAutoScrollEnabled( enableAutoScroll );
294              }
295           }
296         }
297         break;
298       }
299       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
300       {
301         if( !impl.mTextScroller )
302         {
303           impl.mTextScroller = Text::TextScroller::New( impl );
304         }
305         Toolkit::TextLabel::AutoScrollStopMode::Type stopMode = impl.mTextScroller->GetStopMode();
306         if( Scripting::GetEnumerationProperty< Toolkit::TextLabel::AutoScrollStopMode::Type >( value,
307                                                                                                     AUTO_SCROLL_STOP_MODE_TABLE,
308                                                                                                     AUTO_SCROLL_STOP_MODE_TABLE_COUNT,
309                                                                                                     stopMode ) )
310         {
311             impl.mTextScroller->SetStopMode( stopMode );
312         }
313         break;
314       }
315       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
316       {
317         if( !impl.mTextScroller )
318         {
319           impl.mTextScroller = Text::TextScroller::New( impl );
320         }
321         impl.mTextScroller->SetSpeed( value.Get<int>() );
322         break;
323       }
324       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
325       {
326         if( !impl.mTextScroller )
327         {
328           impl.mTextScroller = Text::TextScroller::New( impl );
329         }
330         impl.mTextScroller->SetLoopCount( value.Get<int>() );
331         break;
332       }
333       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
334       {
335          if( !impl.mTextScroller )
336         {
337           impl.mTextScroller = Text::TextScroller::New( impl );
338         }
339         impl.mTextScroller->SetLoopDelay( value.Get<float>() );
340         break;
341       }
342       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
343       {
344         if( !impl.mTextScroller )
345         {
346           impl.mTextScroller = Text::TextScroller::New( impl );
347         }
348         impl.mTextScroller->SetGap( value.Get<float>() );
349         break;
350       }
351       case Toolkit::TextLabel::Property::LINE_SPACING:
352       {
353         if( impl.mController )
354         {
355           const float lineSpacing = value.Get<float>();
356
357           // Don't trigger anything if the line spacing didn't change
358           if( impl.mController->SetDefaultLineSpacing( lineSpacing ) )
359           {
360             impl.mTextUpdateNeeded = true;
361           }
362         }
363         break;
364       }
365       case Toolkit::TextLabel::Property::UNDERLINE:
366       {
367         const bool update = SetUnderlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
368         if( update )
369         {
370           impl.mTextUpdateNeeded = true;
371         }
372         break;
373       }
374       case Toolkit::TextLabel::Property::SHADOW:
375       {
376         const bool update = SetShadowProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
377         if( update )
378         {
379           impl.mTextUpdateNeeded = true;
380         }
381         break;
382       }
383       case Toolkit::TextLabel::Property::EMBOSS:
384       {
385         const bool update = SetEmbossProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
386         if( update )
387         {
388           impl.mTextUpdateNeeded = true;
389         }
390         break;
391       }
392       case Toolkit::TextLabel::Property::OUTLINE:
393       {
394         const bool update = SetOutlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
395         if( update )
396         {
397           impl.mTextUpdateNeeded = true;
398         }
399         break;
400       }
401       case Toolkit::TextLabel::Property::PIXEL_SIZE:
402       {
403         if( impl.mController )
404         {
405           const float pixelSize = value.Get< float >();
406           DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p PIXEL_SIZE %f\n", impl.mController.Get(), pixelSize );
407
408           if( !Equals( impl.mController->GetDefaultFontSize( Text::Controller::PIXEL_SIZE ), pixelSize ) )
409           {
410             impl.mController->SetDefaultFontSize( pixelSize, Text::Controller::PIXEL_SIZE );
411           }
412         }
413         break;
414       }
415       case Toolkit::TextLabel::Property::ELLIPSIS:
416       {
417         if( impl.mController )
418         {
419           const bool ellipsis = value.Get<bool>();
420           DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p ELLIPSIS %d\n", impl.mController.Get(), ellipsis );
421
422           impl.mController->SetTextElideEnabled( ellipsis );
423         }
424         break;
425       }
426       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
427       {
428         if( impl.mController )
429         {
430           Text::LineWrap::Mode lineWrapMode( static_cast< Text::LineWrap::Mode >( -1 ) ); // Set to invalid value to ensure a valid mode does get set
431           if( GetLineWrapModeEnumeration( value, lineWrapMode ) )
432           {
433             DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel %p LineWrap::MODE %d\n", impl.mController.Get(), lineWrapMode );
434             impl.mController->SetLineWrapMode( lineWrapMode );
435           }
436         }
437         break;
438       }
439       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
440       {
441         if( impl.mController && impl.mController->GetTextModel() )
442         {
443           DevelText::VerticalLineAlignment::Type alignment = static_cast<DevelText::VerticalLineAlignment::Type>( value.Get<int>() );
444
445           impl.mController->SetVerticalLineAlignment( alignment );
446
447           // Property doesn't affect the layout, only Visual must be updated
448           TextVisual::EnableRendererUpdate( impl.mVisual );
449
450           // No need to trigger full re-layout. Instead call UpdateRenderer() directly
451           TextVisual::UpdateRenderer( impl.mVisual );
452         }
453         break;
454       }
455       case Toolkit::DevelTextLabel::Property::BACKGROUND:
456       {
457         const bool update = SetBackgroundProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
458         if( update )
459         {
460           impl.mTextUpdateNeeded = true;
461         }
462         break;
463       }
464       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
465       {
466         impl.mController->SetIgnoreSpacesAfterText(value.Get< bool >());
467         break;
468       }
469       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
470       {
471         impl.mController->SetMatchSystemLanguageDirection(value.Get< bool >());
472         break;
473       }
474       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
475       {
476         const Property::Map& propertiesMap = value.Get<Property::Map>();
477
478         bool enabled = false;
479         float minSize = 0.f;
480         float maxSize = 0.f;
481         float stepSize = 0.f;
482         bool isMinSizeSet = false, isMaxSizeSet = false, isStepSizeSet = false;
483         Controller::FontSizeType type = Controller::FontSizeType::POINT_SIZE;
484
485         if ( !propertiesMap.Empty() )
486         {
487           const unsigned int numberOfItems = propertiesMap.Count();
488
489           // Parses and applies
490           for( unsigned int index = 0u; index < numberOfItems; ++index )
491           {
492             const KeyValuePair& valueGet = propertiesMap.GetKeyValue( index );
493
494             if( ( Controller::TextFitInfo::Property::TEXT_FIT_ENABLE == valueGet.first.indexKey ) || ( TEXT_FIT_ENABLE_KEY == valueGet.first.stringKey ) )
495             {
496               /// Enable key.
497               enabled = valueGet.second.Get< bool >();
498             }
499             else if( ( Controller::TextFitInfo::Property::TEXT_FIT_MIN_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_MIN_SIZE_KEY == valueGet.first.stringKey ) )
500             {
501               /// min size.
502               minSize = valueGet.second.Get< float >();
503               isMinSizeSet = true;
504             }
505             else if( ( Controller::TextFitInfo::Property::TEXT_FIT_MAX_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_MAX_SIZE_KEY == valueGet.first.stringKey ) )
506             {
507               /// max size.
508               maxSize = valueGet.second.Get< float >();
509               isMaxSizeSet = true;
510             }
511             else if( ( Controller::TextFitInfo::Property::TEXT_FIT_STEP_SIZE == valueGet.first.indexKey ) || ( TEXT_FIT_STEP_SIZE_KEY == valueGet.first.stringKey ) )
512             {
513               /// step size.
514               stepSize = valueGet.second.Get< float >();
515               isStepSizeSet = true;
516             }
517             else if( ( Controller::TextFitInfo::Property::TEXT_FIT_FONT_SIZE_TYPE == valueGet.first.indexKey ) || ( TEXT_FIT_FONT_SIZE_TYPE_KEY == valueGet.first.stringKey ) )
518             {
519               if( "pixelSize" == valueGet.second.Get< std::string >() )
520               {
521                 type = Controller::FontSizeType::PIXEL_SIZE;
522               }
523             }
524           }
525
526           impl.mController->SetTextFitEnabled( enabled );
527           if( isMinSizeSet )
528           {
529             impl.mController->SetTextFitMinSize( minSize, type );
530           }
531           if( isMaxSizeSet )
532           {
533             impl.mController->SetTextFitMaxSize( maxSize, type );
534           }
535           if( isStepSizeSet )
536           {
537             impl.mController->SetTextFitStepSize( stepSize, type );
538           }
539         }
540         break;
541       }
542       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
543       {
544         if( impl.mController )
545         {
546           const float lineSize = value.Get<float>();
547
548           if( impl.mController->SetDefaultLineSize( lineSize ) )
549           {
550             impl.mTextUpdateNeeded = true;
551           }
552         }
553         break;
554       }
555     }
556
557     // Request relayout when text update is needed. It's necessary to call it
558     // as changing the property not via UI interaction brings no effect if only
559     // the mTextUpdateNeeded is changed.
560     if( impl.mTextUpdateNeeded )
561     {
562       // need to request relayout as size of text may have changed
563       impl.RequestTextRelayout();
564     }
565   }
566 }
567
568 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
569 {
570   Property::Value value;
571
572   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
573
574   if( label )
575   {
576     TextLabel& impl( GetImpl( label ) );
577     switch( index )
578     {
579       case Toolkit::DevelTextLabel::Property::RENDERING_BACKEND:
580       {
581         value = impl.mRenderingBackend;
582         break;
583       }
584       case Toolkit::TextLabel::Property::TEXT:
585       {
586         if( impl.mController )
587         {
588           std::string text;
589           impl.mController->GetText( text );
590           value = text;
591         }
592         break;
593       }
594       case Toolkit::TextLabel::Property::FONT_FAMILY:
595       {
596         if( impl.mController )
597         {
598           value = impl.mController->GetDefaultFontFamily();
599         }
600         break;
601       }
602       case Toolkit::TextLabel::Property::FONT_STYLE:
603       {
604         GetFontStyleProperty( impl.mController, value, Text::FontStyle::DEFAULT );
605         break;
606       }
607       case Toolkit::TextLabel::Property::POINT_SIZE:
608       {
609         if( impl.mController )
610         {
611           value = impl.mController->GetDefaultFontSize( Text::Controller::POINT_SIZE );
612         }
613         break;
614       }
615       case Toolkit::TextLabel::Property::MULTI_LINE:
616       {
617         if( impl.mController )
618         {
619           value = impl.mController->IsMultiLineEnabled();
620         }
621         break;
622       }
623       case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
624       {
625         if( impl.mController )
626         {
627           const char* name = Text::GetHorizontalAlignmentString( impl.mController->GetHorizontalAlignment() );
628
629            if ( name )
630            {
631              value = std::string( name );
632            }
633         }
634         break;
635       }
636       case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
637       {
638         if( impl.mController )
639         {
640           const char* name = Text::GetVerticalAlignmentString( impl.mController->GetVerticalAlignment() );
641           if( name )
642           {
643             value = std::string( name );
644           }
645         }
646         break;
647       }
648       case Toolkit::TextLabel::Property::ENABLE_MARKUP:
649       {
650         if( impl.mController )
651         {
652           value = impl.mController->IsMarkupProcessorEnabled();
653         }
654         break;
655       }
656       case Toolkit::TextLabel::Property::ENABLE_AUTO_SCROLL:
657       {
658         if( impl.mController )
659         {
660           value = impl.mController->IsAutoScrollEnabled();
661         }
662         break;
663       }
664       case Toolkit::TextLabel::Property::AUTO_SCROLL_STOP_MODE:
665       {
666         if( impl.mTextScroller )
667         {
668           const char* mode = Scripting::GetEnumerationName< Toolkit::TextLabel::AutoScrollStopMode::Type >( impl.mTextScroller->GetStopMode(),
669                                                                                                                  AUTO_SCROLL_STOP_MODE_TABLE,
670                                                                                                                  AUTO_SCROLL_STOP_MODE_TABLE_COUNT );
671           if( mode )
672           {
673             value = std::string( mode );
674           }
675         }
676         break;
677       }
678       case Toolkit::TextLabel::Property::AUTO_SCROLL_SPEED:
679       {
680         TextLabel& impl( GetImpl( label ) );
681         if ( impl.mTextScroller )
682         {
683           value = impl.mTextScroller->GetSpeed();
684         }
685         break;
686       }
687       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_COUNT:
688       {
689         if( impl.mController )
690         {
691           TextLabel& impl( GetImpl( label ) );
692           if ( impl.mTextScroller )
693           {
694             value = impl.mTextScroller->GetLoopCount();
695           }
696         }
697         break;
698       }
699       case Toolkit::TextLabel::Property::AUTO_SCROLL_LOOP_DELAY:
700       {
701         if( impl.mController )
702         {
703           TextLabel& impl( GetImpl( label ) );
704           if ( impl.mTextScroller )
705           {
706             value = impl.mTextScroller->GetLoopDelay();
707           }
708         }
709         break;
710       }
711       case Toolkit::TextLabel::Property::AUTO_SCROLL_GAP:
712       {
713         TextLabel& impl( GetImpl( label ) );
714         if ( impl.mTextScroller )
715         {
716           value = impl.mTextScroller->GetGap();
717         }
718         break;
719       }
720       case Toolkit::TextLabel::Property::LINE_SPACING:
721       {
722         if( impl.mController )
723         {
724           value = impl.mController->GetDefaultLineSpacing();
725         }
726         break;
727       }
728       case Toolkit::TextLabel::Property::UNDERLINE:
729       {
730         GetUnderlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
731         break;
732       }
733       case Toolkit::TextLabel::Property::SHADOW:
734       {
735         GetShadowProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
736         break;
737       }
738       case Toolkit::TextLabel::Property::EMBOSS:
739       {
740         GetEmbossProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
741         break;
742       }
743       case Toolkit::TextLabel::Property::OUTLINE:
744       {
745         GetOutlineProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
746         break;
747       }
748       case Toolkit::TextLabel::Property::PIXEL_SIZE:
749       {
750         if( impl.mController )
751         {
752           value = impl.mController->GetDefaultFontSize( Text::Controller::PIXEL_SIZE );
753         }
754         break;
755       }
756       case Toolkit::TextLabel::Property::ELLIPSIS:
757       {
758         if( impl.mController )
759         {
760           value = impl.mController->IsTextElideEnabled();
761         }
762         break;
763       }
764       case Toolkit::TextLabel::Property::LINE_WRAP_MODE:
765       {
766         if( impl.mController )
767         {
768           value = impl.mController->GetLineWrapMode();
769         }
770         break;
771       }
772       case Toolkit::TextLabel::Property::LINE_COUNT:
773       {
774         if( impl.mController )
775         {
776           float width = label.GetProperty( Actor::Property::SIZE_WIDTH ).Get<float>();
777           value = impl.mController->GetLineCount( width );
778         }
779         break;
780       }
781       case Toolkit::DevelTextLabel::Property::TEXT_DIRECTION:
782       {
783         if( impl.mController )
784         {
785           value = impl.mController->GetTextDirection();
786         }
787         break;
788       }
789       case Toolkit::DevelTextLabel::Property::VERTICAL_LINE_ALIGNMENT:
790       {
791         if( impl.mController )
792         {
793           value = impl.mController->GetVerticalLineAlignment();
794         }
795         break;
796       }
797       case Toolkit::DevelTextLabel::Property::BACKGROUND:
798       {
799         GetBackgroundProperties( impl.mController, value, Text::EffectStyle::DEFAULT );
800         break;
801       }
802       case Toolkit::DevelTextLabel::Property::IGNORE_SPACES_AFTER_TEXT:
803       {
804         value = impl.mController->IsIgnoreSpacesAfterText();
805         break;
806       }
807       case Toolkit::DevelTextLabel::Property::MATCH_SYSTEM_LANGUAGE_DIRECTION:
808       {
809         value = impl.mController->IsMatchSystemLanguageDirection();
810         break;
811       }
812       case Toolkit::DevelTextLabel::Property::TEXT_FIT:
813       {
814         const bool enabled = impl.mController->IsTextFitEnabled();
815         const float minSize = impl.mController->GetTextFitMinSize();
816         const float maxSize = impl.mController->GetTextFitMaxSize();
817         const float stepSize = impl.mController->GetTextFitStepSize();
818
819         Property::Map map;
820         map.Insert( TEXT_FIT_ENABLE_KEY, enabled );
821         map.Insert( TEXT_FIT_MIN_SIZE_KEY, minSize );
822         map.Insert( TEXT_FIT_MAX_SIZE_KEY, maxSize );
823         map.Insert( TEXT_FIT_STEP_SIZE_KEY, stepSize );
824         map.Insert( TEXT_FIT_FONT_SIZE_TYPE_KEY, "pointSize" );
825
826         value = map;
827         break;
828       }
829       case Toolkit::DevelTextLabel::Property::MIN_LINE_SIZE:
830       {
831         if( impl.mController )
832         {
833           value = impl.mController->GetDefaultLineSize();
834         }
835         break;
836       }
837     }
838   }
839
840   return value;
841 }
842
843 void TextLabel::OnInitialize()
844 {
845   Actor self = Self();
846
847   Property::Map propertyMap;
848   propertyMap.Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT );
849
850   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( propertyMap );
851   DevelControl::RegisterVisual( *this, Toolkit::TextLabel::Property::TEXT, mVisual  );
852
853   TextVisual::SetAnimatableTextColorProperty( mVisual, Toolkit::TextLabel::Property::TEXT_COLOR );
854
855   mController = TextVisual::GetController(mVisual);
856   if( mController )
857   {
858     mController->SetControlInterface(this);
859   }
860
861   // Use height-for-width negotiation by default
862   self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
863   self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
864
865   // Enable the text ellipsis.
866   mController->SetTextElideEnabled( true );   // If false then text larger than control will overflow
867
868   // Sets layoutDirection value
869   Dali::Stage stage = Dali::Stage::GetCurrent();
870   Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( stage.GetRootLayer().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
871   mController->SetLayoutDirection( layoutDirection );
872
873   Layout::Engine& engine = mController->GetLayoutEngine();
874   engine.SetCursorWidth( 0u ); // Do not layout space for the cursor.
875 }
876
877 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
878 {
879   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnStyleChange\n");
880
881   switch ( change )
882   {
883     case StyleChange::DEFAULT_FONT_CHANGE:
884     {
885       // Property system did not set the font so should update it.
886       const std::string& newFont = GetImpl( styleManager ).GetDefaultFontFamily();
887       DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnStyleChange StyleChange::DEFAULT_FONT_CHANGE newFont(%s)\n", newFont.c_str() );
888       mController->UpdateAfterFontChange( newFont );
889       RelayoutRequest();
890       break;
891     }
892     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
893     {
894       GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
895       RelayoutRequest();
896       break;
897     }
898     case StyleChange::THEME_CHANGE:
899     {
900       // Nothing to do, let control base class handle this
901       break;
902     }
903   }
904
905   // Up call to Control
906   Control::OnStyleChange( styleManager, change );
907 }
908
909 Vector3 TextLabel::GetNaturalSize()
910 {
911   Extents padding;
912   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
913
914   Vector3 naturalSize = mController->GetNaturalSize();
915   naturalSize.width += ( padding.start + padding.end );
916   naturalSize.height += ( padding.top + padding.bottom );
917
918   return naturalSize;
919 }
920
921 float TextLabel::GetHeightForWidth( float width )
922 {
923   Extents padding;
924   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
925
926   return mController->GetHeightForWidth( width ) + padding.top + padding.bottom;
927 }
928
929 void TextLabel::OnPropertySet( Property::Index index, Property::Value propertyValue )
930 {
931   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextLabel::OnPropertySet index[%d]\n", index );
932
933   switch ( index )
934   {
935     case Toolkit::TextLabel::Property::TEXT_COLOR:
936     {
937       const Vector4& textColor = propertyValue.Get< Vector4 >();
938       if( mController->GetDefaultColor() != textColor )
939       {
940          mController->SetDefaultColor( textColor );
941          mTextUpdateNeeded = true;
942       }
943       break;
944     }
945     default:
946     {
947       Control::OnPropertySet( index, propertyValue ); // up call to control for non-handled properties
948       break;
949     }
950   }
951 }
952
953 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
954 {
955   DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnRelayout\n" );
956
957   Extents padding;
958   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
959
960   Vector2 contentSize( size.x - ( padding.start + padding.end ), size.y - ( padding.top + padding.bottom ) );
961
962   if( mController->IsTextFitEnabled() )
963   {
964     mController->FitPointSizeforLayout( contentSize );
965     mController->SetTextFitContentSize( contentSize );
966   }
967
968   // Support Right-To-Left
969   Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
970
971   const Text::Controller::UpdateTextType updateTextType = mController->Relayout( contentSize, layoutDirection );
972
973   if( ( Text::Controller::NONE_UPDATED != ( Text::Controller::MODEL_UPDATED & updateTextType ) )
974      || mTextUpdateNeeded )
975   {
976     DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::OnRelayout IsAutoScrollEnabled[%s] [%p]\n", ( mController->IsAutoScrollEnabled())?"true":"false", this );
977
978     // Update the visual
979     TextVisual::EnableRendererUpdate( mVisual );
980
981     // Support Right-To-Left of padding
982     if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
983     {
984       std::swap( padding.start, padding.end );
985     }
986
987     // Calculate the size of the visual that can fit the text
988     Size layoutSize = mController->GetTextModel()->GetLayoutSize();
989     layoutSize.x = contentSize.x;
990
991     const Vector2& shadowOffset = mController->GetTextModel()->GetShadowOffset();
992     if ( shadowOffset.y > Math::MACHINE_EPSILON_1 )
993     {
994       layoutSize.y += shadowOffset.y;
995     }
996
997     float outlineWidth = mController->GetTextModel()->GetOutlineWidth();
998     layoutSize.y += outlineWidth * 2.0f;
999     layoutSize.y = std::min( layoutSize.y, contentSize.y );
1000
1001     // Calculate the offset for vertical alignment only, as the layout engine will do the horizontal alignment.
1002     Vector2 alignmentOffset;
1003     alignmentOffset.x = 0.0f;
1004     alignmentOffset.y = ( contentSize.y - layoutSize.y ) * VERTICAL_ALIGNMENT_TABLE[mController->GetVerticalAlignment()];
1005
1006     Property::Map visualTransform;
1007     visualTransform.Add( Toolkit::Visual::Transform::Property::SIZE, layoutSize )
1008                    .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
1009                    .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( padding.start, padding.top ) + alignmentOffset )
1010                    .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
1011                    .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
1012                    .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
1013     mVisual.SetTransformAndSize( visualTransform, size );
1014
1015     if ( mController->IsAutoScrollEnabled() )
1016     {
1017       SetUpAutoScrolling();
1018     }
1019
1020     mTextUpdateNeeded = false;
1021   }
1022 }
1023
1024 void TextLabel::RequestTextRelayout()
1025 {
1026   RelayoutRequest();
1027   // Signal that a Relayout may be needed
1028 }
1029
1030 void TextLabel::SetUpAutoScrolling()
1031 {
1032   const Size& controlSize = mController->GetView().GetControlSize();
1033   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.
1034   const Text::CharacterDirection direction = mController->GetAutoScrollDirection();
1035
1036   DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling textNaturalSize[%f,%f] controlSize[%f,%f]\n",
1037                  textNaturalSize.x,textNaturalSize.y , controlSize.x,controlSize.y );
1038
1039   if ( !mTextScroller )
1040   {
1041     DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::SetUpAutoScrolling Creating default TextScoller\n" );
1042
1043     // If speed, loopCount or gap not set via property system then will need to create a TextScroller with defaults
1044     mTextScroller = Text::TextScroller::New( *this );
1045   }
1046
1047   // Calculate the actual gap before scrolling wraps.
1048   int textPadding = std::max( controlSize.x - textNaturalSize.x, 0.0f );
1049   float wrapGap = std::max( mTextScroller->GetGap(), textPadding );
1050   Vector2 textureSize = textNaturalSize + Vector2(wrapGap, 0.0f); // Add the gap as a part of the texture
1051
1052   // Create a texture of the text for scrolling
1053   Size verifiedSize = textureSize;
1054   const int maxTextureSize = Dali::GetMaxTextureSize();
1055
1056   //if the texture size width exceed maxTextureSize, modify the visual model size and enabled the ellipsis
1057   bool actualellipsis = mController->IsTextElideEnabled();
1058   if( verifiedSize.width > maxTextureSize )
1059   {
1060     verifiedSize.width = maxTextureSize;
1061     if( textNaturalSize.width > maxTextureSize )
1062     {
1063       mController->SetTextElideEnabled( true );
1064     }
1065     GetHeightForWidth( maxTextureSize );
1066     wrapGap = std::max( maxTextureSize - textNaturalSize.width, 0.0f );
1067   }
1068
1069   Text::TypesetterPtr typesetter = Text::Typesetter::New( mController->GetTextModel() );
1070
1071   PixelData data = typesetter->Render( verifiedSize, mController->GetTextDirection(), Text::Typesetter::RENDER_TEXT_AND_STYLES, true, Pixel::RGBA8888 ); // ignore the horizontal alignment
1072   Texture texture = Texture::New( Dali::TextureType::TEXTURE_2D,
1073                                   data.GetPixelFormat(),
1074                                   data.GetWidth(),
1075                                   data.GetHeight() );
1076   texture.Upload( data );
1077
1078   TextureSet textureSet = TextureSet::New();
1079   textureSet.SetTexture( 0u, texture );
1080
1081   // Filter mode needs to be set to linear to produce better quality while scaling.
1082   Sampler sampler = Sampler::New();
1083   sampler.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
1084   sampler.SetWrapMode( Dali::WrapMode::DEFAULT, Dali::WrapMode::REPEAT, Dali::WrapMode::DEFAULT ); // Wrap the texture in the x direction
1085   textureSet.SetSampler( 0u, sampler );
1086
1087   // Set parameters for scrolling
1088   Renderer renderer = static_cast<Internal::Visual::Base&>( GetImplementation( mVisual ) ).GetRenderer();
1089   mTextScroller->SetParameters( Self(), renderer, textureSet, controlSize, verifiedSize, wrapGap, direction, mController->GetHorizontalAlignment(), mController->GetVerticalAlignment() );
1090   mController->SetTextElideEnabled( actualellipsis );
1091 }
1092
1093 void TextLabel::ScrollingFinished()
1094 {
1095   // Pure Virtual from TextScroller Interface
1096   DALI_LOG_INFO( gLogFilter, Debug::General, "TextLabel::ScrollingFinished\n");
1097   mController->SetAutoScrollEnabled( false );
1098   RequestTextRelayout();
1099 }
1100
1101 TextLabel::TextLabel()
1102 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
1103   mRenderingBackend( DEFAULT_RENDERING_BACKEND ),
1104   mTextUpdateNeeded( false )
1105 {
1106 }
1107
1108 TextLabel::~TextLabel()
1109 {
1110 }
1111
1112 } // namespace Internal
1113
1114 } // namespace Toolkit
1115
1116 } // namespace Dali