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