(TextController) Refactored out Text updating methods & moved another method to the...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-relayouter.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-controller-relayouter.h>
20
21 // EXTERNAL INCLUDES
22 #include <limits>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
27 #include <dali-toolkit/internal/text/text-controller-impl.h>
28
29 namespace
30 {
31
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
34 #endif
35
36 constexpr float MAX_FLOAT = std::numeric_limits<float>::max();
37
38 float ConvertToEven( float value )
39 {
40   int intValue(static_cast<int>( value ));
41   return static_cast<float>( intValue + ( intValue & 1 ) );
42 }
43
44 } // namespace
45
46 namespace Dali
47 {
48
49 namespace Toolkit
50 {
51
52 namespace Text
53 {
54
55 Vector3 Controller::Relayouter::GetNaturalSize(Controller& controller)
56 {
57   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
58   Vector3 naturalSize;
59
60   // Make sure the model is up-to-date before layouting
61   controller.ProcessModifyEvents();
62
63   Controller::Impl& impl = *controller.mImpl;
64   ModelPtr& model = impl.mModel;
65   VisualModelPtr& visualModel = model->mVisualModel;
66   if( impl.mRecalculateNaturalSize )
67   {
68     // Operations that can be done only once until the text changes.
69     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
70                                                                            GET_SCRIPTS       |
71                                                                            VALIDATE_FONTS    |
72                                                                            GET_LINE_BREAKS   |
73                                                                            BIDI_INFO         |
74                                                                            SHAPE_TEXT        |
75                                                                            GET_GLYPH_METRICS );
76
77     // Set the update info to relayout the whole text.
78     TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
79     textUpdateInfo.mParagraphCharacterIndex = 0u;
80     textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
81
82     // Make sure the model is up-to-date before layouting
83     impl.UpdateModel( onlyOnceOperations );
84
85     // Get a reference to the pending operations member
86     OperationsMask& operationsPending = impl.mOperationsPending;
87
88     // Layout the text for the new width.
89     operationsPending = static_cast<OperationsMask>( operationsPending | LAYOUT | REORDER );
90
91     // Store the actual control's size to restore later.
92     const Size actualControlSize = visualModel->mControlSize;
93
94     DoRelayout( controller,
95                 Size( MAX_FLOAT, MAX_FLOAT ),
96                 static_cast<OperationsMask>( onlyOnceOperations |
97                                              LAYOUT | REORDER ),
98                 naturalSize.GetVectorXY() );
99
100     // Do not do again the only once operations.
101     operationsPending = static_cast<OperationsMask>( operationsPending & ~onlyOnceOperations );
102
103     // Do the size related operations again.
104     const OperationsMask sizeOperations =  static_cast<OperationsMask>( LAYOUT |
105                                                                         ALIGN  |
106                                                                         REORDER );
107     operationsPending = static_cast<OperationsMask>( operationsPending | sizeOperations );
108
109     // Stores the natural size to avoid recalculate it again
110     // unless the text/style changes.
111     visualModel->SetNaturalSize( naturalSize.GetVectorXY() );
112
113     impl.mRecalculateNaturalSize = false;
114
115     // Clear the update info. This info will be set the next time the text is updated.
116     textUpdateInfo.Clear();
117     textUpdateInfo.mClearAll = true;
118
119     // Restore the actual control's size.
120     visualModel->mControlSize = actualControlSize;
121
122     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
123   }
124   else
125   {
126     naturalSize = visualModel->GetNaturalSize();
127
128     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
129   }
130
131   naturalSize.x = ConvertToEven( naturalSize.x );
132   naturalSize.y = ConvertToEven( naturalSize.y );
133
134   return naturalSize;
135 }
136
137 bool Controller::Relayouter::CheckForTextFit(Controller& controller, float pointSize, const Size& layoutSize)
138 {
139   Size textSize;
140   Controller::Impl& impl = *controller.mImpl;
141   TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
142   impl.mFontDefaults->mFitPointSize = pointSize;
143   impl.mFontDefaults->sizeDefined = true;
144   controller.ClearFontData();
145
146   // Operations that can be done only once until the text changes.
147   const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
148                                                                               GET_SCRIPTS |
149                                                                            VALIDATE_FONTS |
150                                                                           GET_LINE_BREAKS |
151                                                                                 BIDI_INFO |
152                                                                                 SHAPE_TEXT|
153                                                                          GET_GLYPH_METRICS );
154
155   textUpdateInfo.mParagraphCharacterIndex = 0u;
156   textUpdateInfo.mRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
157
158   // Make sure the model is up-to-date before layouting
159   impl.UpdateModel( onlyOnceOperations );
160
161   DoRelayout( controller,
162               Size( layoutSize.width, MAX_FLOAT ),
163               static_cast<OperationsMask>( onlyOnceOperations | LAYOUT),
164               textSize);
165
166   // Clear the update info. This info will be set the next time the text is updated.
167   textUpdateInfo.Clear();
168   textUpdateInfo.mClearAll = true;
169
170   if( textSize.width > layoutSize.width || textSize.height > layoutSize.height )
171   {
172     return false;
173   }
174   return true;
175 }
176
177 void Controller::Relayouter::FitPointSizeforLayout(Controller& controller, const Size& layoutSize)
178 {
179   Controller::Impl& impl = *controller.mImpl;
180
181   const OperationsMask operations  = impl.mOperationsPending;
182   if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) || impl.mTextFitContentSize != layoutSize )
183   {
184     ModelPtr& model = impl.mModel;
185
186     bool actualellipsis = model->mElideEnabled;
187     float minPointSize = impl.mTextFitMinSize;
188     float maxPointSize = impl.mTextFitMaxSize;
189     float pointInterval = impl.mTextFitStepSize;
190
191     model->mElideEnabled = false;
192     Vector<float> pointSizeArray;
193
194     // check zero value
195     if( pointInterval < 1.f )
196     {
197       impl.mTextFitStepSize = pointInterval = 1.0f;
198     }
199
200     pointSizeArray.Reserve( static_cast< unsigned int >( ceil( ( maxPointSize - minPointSize ) / pointInterval ) ) );
201
202     for( float i = minPointSize; i < maxPointSize; i += pointInterval )
203     {
204       pointSizeArray.PushBack( i );
205     }
206
207     pointSizeArray.PushBack( maxPointSize );
208
209     int bestSizeIndex = 0;
210     int min = bestSizeIndex + 1;
211     int max = pointSizeArray.Size() - 1;
212     while( min <= max )
213     {
214       int destI = ( min + max ) / 2;
215
216       if( CheckForTextFit( controller, pointSizeArray[destI], layoutSize ) )
217       {
218         bestSizeIndex = min;
219         min = destI + 1;
220       }
221       else
222       {
223         max = destI - 1;
224         bestSizeIndex = max;
225       }
226     }
227
228     model->mElideEnabled = actualellipsis;
229     impl.mFontDefaults->mFitPointSize = pointSizeArray[bestSizeIndex];
230     impl.mFontDefaults->sizeDefined = true;
231     controller.ClearFontData();
232   }
233 }
234
235 float Controller::Relayouter::GetHeightForWidth(Controller& controller, float width)
236 {
237   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", &controller, width );
238   // Make sure the model is up-to-date before layouting
239   controller.ProcessModifyEvents();
240
241   Controller::Impl& impl = *controller.mImpl;
242   ModelPtr& model = impl.mModel;
243   VisualModelPtr& visualModel = model->mVisualModel;
244   TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
245
246   Size layoutSize;
247   if( fabsf( width - visualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 ||
248       textUpdateInfo.mFullRelayoutNeeded ||
249       textUpdateInfo.mClearAll )
250   {
251     // Operations that can be done only once until the text changes.
252     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
253                                                                            GET_SCRIPTS       |
254                                                                            VALIDATE_FONTS    |
255                                                                            GET_LINE_BREAKS   |
256                                                                            BIDI_INFO         |
257                                                                            SHAPE_TEXT        |
258                                                                            GET_GLYPH_METRICS );
259
260     // Set the update info to relayout the whole text.
261     textUpdateInfo.mParagraphCharacterIndex = 0u;
262     textUpdateInfo.mRequestedNumberOfCharacters = model->mLogicalModel->mText.Count();
263
264     // Make sure the model is up-to-date before layouting
265     impl.UpdateModel( onlyOnceOperations );
266
267     // Get a reference to the pending operations member
268     OperationsMask& operationsPending = impl.mOperationsPending;
269
270     // Layout the text for the new width.
271     operationsPending = static_cast<OperationsMask>( operationsPending | LAYOUT );
272
273     // Store the actual control's width.
274     const float actualControlWidth = visualModel->mControlSize.width;
275
276     DoRelayout( controller,
277                 Size( width, MAX_FLOAT ),
278                 static_cast<OperationsMask>( onlyOnceOperations |
279                                              LAYOUT ),
280                 layoutSize );
281
282     // Do not do again the only once operations.
283     operationsPending = static_cast<OperationsMask>( operationsPending & ~onlyOnceOperations );
284
285     // Do the size related operations again.
286     const OperationsMask sizeOperations =  static_cast<OperationsMask>( LAYOUT |
287                                                                         ALIGN  |
288                                                                         REORDER );
289
290     operationsPending = static_cast<OperationsMask>( operationsPending | sizeOperations );
291
292     // Clear the update info. This info will be set the next time the text is updated.
293     textUpdateInfo.Clear();
294     textUpdateInfo.mClearAll = true;
295
296     // Restore the actual control's width.
297     visualModel->mControlSize.width = actualControlWidth;
298
299     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
300   }
301   else
302   {
303     layoutSize = visualModel->GetLayoutSize();
304     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
305   }
306
307   return layoutSize.height;
308 }
309
310 Controller::UpdateTextType Controller::Relayouter::Relayout(Controller& controller, const Size& size, Dali::LayoutDirection::Type layoutDirection)
311 {
312   Controller::Impl& impl = *controller.mImpl;
313   ModelPtr& model = impl.mModel;
314   VisualModelPtr& visualModel = model->mVisualModel;
315   TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
316
317   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f, autoScroll[%s]\n", &controller, size.width, size.height, impl.mIsAutoScrollEnabled ?"true":"false"  );
318
319   UpdateTextType updateTextType = NONE_UPDATED;
320
321   if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
322   {
323     if( 0u != visualModel->mGlyphPositions.Count() )
324     {
325       visualModel->mGlyphPositions.Clear();
326       updateTextType = MODEL_UPDATED;
327     }
328
329     // Clear the update info. This info will be set the next time the text is updated.
330     textUpdateInfo.Clear();
331
332     // Not worth to relayout if width or height is equal to zero.
333     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
334
335     return updateTextType;
336   }
337
338   // Whether a new size has been set.
339   const bool newSize = ( size != visualModel->mControlSize );
340
341   // Get a reference to the pending operations member
342   OperationsMask& operationsPending = impl.mOperationsPending;
343
344   if( newSize )
345   {
346     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", visualModel->mControlSize.width, visualModel->mControlSize.height );
347
348     if( ( 0 == textUpdateInfo.mNumberOfCharactersToAdd ) &&
349         ( 0 == textUpdateInfo.mPreviousNumberOfCharacters ) &&
350         ( ( visualModel->mControlSize.width < Math::MACHINE_EPSILON_1000 ) || ( visualModel->mControlSize.height < Math::MACHINE_EPSILON_1000 ) ) )
351     {
352       textUpdateInfo.mNumberOfCharactersToAdd = model->mLogicalModel->mText.Count();
353     }
354
355     // Layout operations that need to be done if the size changes.
356     operationsPending = static_cast<OperationsMask>( operationsPending  |
357                                                      LAYOUT             |
358                                                      ALIGN              |
359                                                      UPDATE_LAYOUT_SIZE |
360                                                      REORDER );
361     // Set the update info to relayout the whole text.
362     textUpdateInfo.mFullRelayoutNeeded = true;
363     textUpdateInfo.mCharacterIndex = 0u;
364
365     // Store the size used to layout the text.
366     visualModel->mControlSize = size;
367   }
368
369   // Whether there are modify events.
370   if( 0u != impl.mModifyEvents.Count() )
371   {
372     // Style operations that need to be done if the text is modified.
373     operationsPending = static_cast<OperationsMask>( operationsPending | COLOR );
374   }
375
376   // Set the update info to elide the text.
377   if( model->mElideEnabled ||
378       ( ( NULL != impl.mEventData ) && impl.mEventData->mIsPlaceholderElideEnabled ) )
379   {
380     // Update Text layout for applying elided
381     operationsPending = static_cast<OperationsMask>( operationsPending  |
382                                                      ALIGN              |
383                                                      LAYOUT             |
384                                                      UPDATE_LAYOUT_SIZE |
385                                                      REORDER );
386     textUpdateInfo.mFullRelayoutNeeded = true;
387     textUpdateInfo.mCharacterIndex = 0u;
388   }
389
390   if( model->mMatchSystemLanguageDirection  && impl.mLayoutDirection != layoutDirection )
391   {
392     // Clear the update info. This info will be set the next time the text is updated.
393     textUpdateInfo.mClearAll = true;
394     // Apply modifications to the model
395     // Shape the text again is needed because characters like '()[]{}' have to be mirrored and the glyphs generated again.
396     operationsPending = static_cast<OperationsMask>( operationsPending |
397                                                      GET_GLYPH_METRICS |
398                                                      SHAPE_TEXT        |
399                                                      UPDATE_DIRECTION  |
400                                                      LAYOUT            |
401                                                      BIDI_INFO         |
402                                                      REORDER );
403     impl.mLayoutDirection = layoutDirection;
404   }
405
406   // Make sure the model is up-to-date before layouting.
407   controller.ProcessModifyEvents();
408   bool updated = impl.UpdateModel( operationsPending );
409
410   // Layout the text.
411   Size layoutSize;
412   updated = DoRelayout( controller, size, operationsPending, layoutSize ) || updated;
413
414
415   if( updated )
416   {
417     updateTextType = MODEL_UPDATED;
418   }
419
420   // Do not re-do any operation until something changes.
421   operationsPending = NO_OPERATION;
422   model->mScrollPositionLast = model->mScrollPosition;
423
424   // Whether the text control is editable
425   const bool isEditable = NULL != impl.mEventData;
426
427   // Keep the current offset as it will be used to update the decorator's positions (if the size changes).
428   Vector2 offset;
429   if( newSize && isEditable )
430   {
431     offset = model->mScrollPosition;
432   }
433
434   if( !isEditable || !controller.IsMultiLineEnabled() )
435   {
436     // After doing the text layout, the vertical offset to place the actor in the desired position can be calculated.
437     controller.CalculateVerticalOffset( size );
438   }
439
440   if( isEditable )
441   {
442     if( newSize )
443     {
444       // If there is a new size, the scroll position needs to be clamped.
445       impl.ClampHorizontalScroll( layoutSize );
446
447       // Update the decorator's positions is needed if there is a new size.
448       impl.mEventData->mDecorator->UpdatePositions( model->mScrollPosition - offset );
449     }
450
451     // Move the cursor, grab handle etc.
452     if( impl.ProcessInputEvents() )
453     {
454       updateTextType = static_cast<UpdateTextType>( updateTextType | DECORATOR_UPDATED );
455     }
456   }
457
458   // Clear the update info. This info will be set the next time the text is updated.
459   textUpdateInfo.Clear();
460   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
461
462   return updateTextType;
463 }
464
465 bool Controller::Relayouter::DoRelayout(Controller& controller, const Size& size, OperationsMask operationsRequired, Size& layoutSize )
466 {
467   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", &controller, size.width, size.height );
468   bool viewUpdated( false );
469
470   Controller::Impl& impl = *controller.mImpl;
471
472   // Calculate the operations to be done.
473   const OperationsMask operations = static_cast<OperationsMask>( impl.mOperationsPending & operationsRequired );
474
475   TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
476   const CharacterIndex startIndex = textUpdateInfo.mParagraphCharacterIndex;
477   const Length requestedNumberOfCharacters = textUpdateInfo.mRequestedNumberOfCharacters;
478
479   // Get the current layout size.
480   VisualModelPtr& visualModel = impl.mModel->mVisualModel;
481   layoutSize = visualModel->GetLayoutSize();
482
483   if( NO_OPERATION != ( LAYOUT & operations ) )
484   {
485     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout LAYOUT & operations\n");
486
487     // Some vectors with data needed to layout and reorder may be void
488     // after the first time the text has been laid out.
489     // Fill the vectors again.
490
491     // Calculate the number of glyphs to layout.
492     const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
493     const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
494     const GlyphIndex* const charactersToGlyphBuffer = charactersToGlyph.Begin();
495     const Length* const glyphsPerCharacterBuffer = glyphsPerCharacter.Begin();
496
497     const CharacterIndex lastIndex = startIndex + ( ( requestedNumberOfCharacters > 0u ) ? requestedNumberOfCharacters - 1u : 0u );
498     const GlyphIndex startGlyphIndex = textUpdateInfo.mStartGlyphIndex;
499
500     // Make sure the index is not out of bound
501     if ( charactersToGlyph.Count() != glyphsPerCharacter.Count() ||
502          requestedNumberOfCharacters > charactersToGlyph.Count() ||
503          ( lastIndex > charactersToGlyph.Count() && charactersToGlyph.Count() > 0u ) )
504     {
505       std::string currentText;
506       controller.GetText( currentText );
507
508       DALI_LOG_ERROR( "Controller::DoRelayout: Attempting to access invalid buffer\n" );
509       DALI_LOG_ERROR( "Current text is: %s\n", currentText.c_str() );
510       DALI_LOG_ERROR( "startIndex: %u, lastIndex: %u, requestedNumberOfCharacters: %u, charactersToGlyph.Count = %lu, glyphsPerCharacter.Count = %lu\n", startIndex, lastIndex, requestedNumberOfCharacters, charactersToGlyph.Count(), glyphsPerCharacter.Count());
511
512       return false;
513     }
514
515     const Length numberOfGlyphs = ( requestedNumberOfCharacters > 0u ) ? *( charactersToGlyphBuffer + lastIndex ) + *( glyphsPerCharacterBuffer + lastIndex ) - startGlyphIndex : 0u;
516     const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
517
518     if( 0u == totalNumberOfGlyphs )
519     {
520       if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
521       {
522         visualModel->SetLayoutSize( Size::ZERO );
523       }
524
525       // Nothing else to do if there is no glyphs.
526       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
527       return true;
528     }
529
530     // Set the layout parameters.
531     Layout::Parameters layoutParameters( size, impl.mModel);
532
533     // Resize the vector of positions to have the same size than the vector of glyphs.
534     Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
535     glyphPositions.Resize( totalNumberOfGlyphs );
536
537     // Whether the last character is a new paragraph character.
538     const Character* const textBuffer = impl.mModel->mLogicalModel->mText.Begin();
539     textUpdateInfo.mIsLastCharacterNewParagraph =  TextAbstraction::IsNewParagraph( *( textBuffer + ( impl.mModel->mLogicalModel->mText.Count() - 1u ) ) );
540     layoutParameters.isLastNewParagraph = textUpdateInfo.mIsLastCharacterNewParagraph;
541
542     // The initial glyph and the number of glyphs to layout.
543     layoutParameters.startGlyphIndex = startGlyphIndex;
544     layoutParameters.numberOfGlyphs = numberOfGlyphs;
545     layoutParameters.startLineIndex = textUpdateInfo.mStartLineIndex;
546     layoutParameters.estimatedNumberOfLines = textUpdateInfo.mEstimatedNumberOfLines;
547
548     // Update the ellipsis
549     bool elideTextEnabled = impl.mModel->mElideEnabled;
550
551     if( NULL != impl.mEventData )
552     {
553       if( impl.mEventData->mPlaceholderEllipsisFlag && impl.IsShowingPlaceholderText() )
554       {
555         elideTextEnabled = impl.mEventData->mIsPlaceholderElideEnabled;
556       }
557       else if( EventData::INACTIVE != impl.mEventData->mState )
558       {
559         // Disable ellipsis when editing
560         elideTextEnabled = false;
561       }
562
563       // Reset the scroll position in inactive state
564       if( elideTextEnabled && ( impl.mEventData->mState == EventData::INACTIVE ) )
565       {
566         controller.ResetScrollPosition();
567       }
568     }
569
570     // Update the visual model.
571     bool isAutoScrollEnabled = impl.mIsAutoScrollEnabled;
572     Size newLayoutSize;
573     viewUpdated = impl.mLayoutEngine.LayoutText( layoutParameters,
574                                                  newLayoutSize,
575                                                  elideTextEnabled,
576                                                  isAutoScrollEnabled );
577     impl.mIsAutoScrollEnabled = isAutoScrollEnabled;
578
579     viewUpdated = viewUpdated || ( newLayoutSize != layoutSize );
580
581     if( viewUpdated )
582     {
583       layoutSize = newLayoutSize;
584
585       if( NO_OPERATION != ( UPDATE_DIRECTION & operations ) )
586       {
587         impl.mIsTextDirectionRTL = false;
588       }
589
590       if ( ( NO_OPERATION != ( UPDATE_DIRECTION & operations ) ) && !visualModel->mLines.Empty() )
591       {
592         impl.mIsTextDirectionRTL = visualModel->mLines[0u].direction;
593       }
594
595       // Sets the layout size.
596       if( NO_OPERATION != ( UPDATE_LAYOUT_SIZE & operations ) )
597       {
598         visualModel->SetLayoutSize( layoutSize );
599       }
600     } // view updated
601   }
602
603   if( NO_OPERATION != ( ALIGN & operations ) )
604   {
605     // The laid-out lines.
606     Vector<LineRun>& lines = visualModel->mLines;
607
608     CharacterIndex alignStartIndex = startIndex;
609     Length alignRequestedNumberOfCharacters = requestedNumberOfCharacters;
610
611     // the whole text needs to be full aligned.
612     // If you do not do a full aligned, only the last line of the multiline input is aligned.
613     if(  impl.mEventData && impl.mEventData->mUpdateAlignment )
614     {
615       alignStartIndex = 0u;
616       alignRequestedNumberOfCharacters = impl.mModel->mLogicalModel->mText.Count();
617       impl.mEventData->mUpdateAlignment = false;
618     }
619
620     // Need to align with the control's size as the text may contain lines
621     // starting either with left to right text or right to left.
622     impl.mLayoutEngine.Align( size,
623                               alignStartIndex,
624                               alignRequestedNumberOfCharacters,
625                               impl.mModel->mHorizontalAlignment,
626                               lines,
627                               impl.mModel->mAlignmentOffset,
628                               impl.mLayoutDirection,
629                               impl.mModel->mMatchSystemLanguageDirection );
630
631     viewUpdated = true;
632   }
633 #if defined(DEBUG_ENABLED)
634   std::string currentText;
635   controller.GetText( currentText );
636   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::DoRelayout [%p] mImpl->mIsTextDirectionRTL[%s] [%s]\n", &controller, (impl.mIsTextDirectionRTL)?"true":"false",  currentText.c_str() );
637 #endif
638   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
639   return viewUpdated;
640 }
641
642 void Controller::Relayouter::CalculateVerticalOffset(Controller& controller, const Size& controlSize)
643 {
644   Controller::Impl& impl = *controller.mImpl;
645   ModelPtr& model = impl.mModel;
646   Size layoutSize = model->mVisualModel->GetLayoutSize();
647
648   if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
649   {
650     // Get the line height of the default font.
651     layoutSize.height = impl.GetDefaultFontLineHeight();
652   }
653
654   switch( model->mVerticalAlignment )
655   {
656     case VerticalAlignment::TOP:
657     {
658       model->mScrollPosition.y = 0.f;
659       break;
660     }
661     case VerticalAlignment::CENTER:
662     {
663       model->mScrollPosition.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
664       break;
665     }
666     case VerticalAlignment::BOTTOM:
667     {
668       model->mScrollPosition.y = controlSize.height - layoutSize.height;
669       break;
670     }
671   }
672 }
673
674 } // namespace Text
675
676 } // namespace Toolkit
677
678 } // namespace Dali