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