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