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