TextView - Merges the TextActor initialization and update in one function.
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-view / split-by-word-policies.cpp
1 /*
2  * Copyright (c) 2014 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 // FILE HEADER
19 #include <dali-toolkit/internal/controls/text-view/split-by-word-policies.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali-toolkit/internal/controls/text-view/relayout-utilities.h>
24 #include <dali-toolkit/internal/controls/text-view/text-view-processor.h>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 namespace SplitByWord
36 {
37
38 namespace
39 {
40
41 Vector3 OriginalPosition( const TextViewRelayout::RelayoutParameters& relayoutParameters,
42                           const TextView::LayoutParameters& layoutParameters,
43                           TextView::RelayoutData& relayoutData )
44 {
45   const float wordOffset = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.x );
46   const float previousPositionY = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.y );
47
48   if( relayoutParameters.mIsNewLine ||
49       relayoutParameters.mIsFirstCharacter ||
50       ( relayoutParameters.mIsFirstCharacterOfWord && ( wordOffset + relayoutParameters.mWordSize.width > relayoutData.mTextViewSize.width ) ) )
51   {
52     if( !relayoutParameters.mIsNewLine &&
53         ( relayoutParameters.mIsWhiteSpace || relayoutParameters.mIsNewLineCharacter ) )
54     {
55       // Current character is a white space. Don't want to move a white space to the next line.
56       // These white spaces are placed just in the edge.
57       return Vector3( relayoutData.mTextViewSize.width - relayoutParameters.mWordSize.width, relayoutParameters.mPositionOffset.y, 0.f );
58     }
59     else
60     {
61       // Calculates the length of the portion of the line which doesn't exceed the text-view's width and the max character height for the current line.
62       TextViewRelayout::SubLineLayoutInfo subLineInfo;
63       subLineInfo.mLineLength = 0.f;
64       subLineInfo.mMaxCharHeight = 0.f;
65       subLineInfo.mMaxAscender = 0.f;
66       const TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *( relayoutData.mTextLayoutInfo.mLinesLayoutInfo.begin() + relayoutParameters.mIndices.mLineIndex ) );
67
68       TextViewRelayout::CalculateSubLineLayout( relayoutData.mTextViewSize.width,
69                                                 relayoutParameters.mIndices,
70                                                 lineLayoutInfo,
71                                                 TextViewRelayout::WrapByWord,
72                                                 1.f, // Shrink factor
73                                                 subLineInfo );
74
75       if( subLineInfo.mLineLength < Math::MACHINE_EPSILON_1000 )
76       {
77         // It may mean there is a word which is actually longer than the width of the text-view.
78         // In that case the length of this word is needed.
79         if( !lineLayoutInfo.mWordsLayoutInfo.empty() )
80         {
81           const TextViewProcessor::WordLayoutInfo& wordLayoutInfo( *( lineLayoutInfo.mWordsLayoutInfo.begin() + relayoutParameters.mIndices.mWordIndex ) );
82           subLineInfo.mLineLength = wordLayoutInfo.mSize.width;
83         }
84       }
85
86       // Stores some info to calculate the line justification in a post-process.
87       TextView::LineJustificationInfo justificationInfo;
88
89       justificationInfo.mIndices = relayoutParameters.mIndices;
90       justificationInfo.mLineLength = subLineInfo.mLineLength;
91
92       relayoutData.mLineJustificationInfo.push_back( justificationInfo );
93
94       Toolkit::TextView::LineLayoutInfo lineInfo;
95       lineInfo.mCharacterGlobalIndex = relayoutParameters.mCharacterGlobalIndex;    // Index to the first character of the next line.
96       lineInfo.mSize = Size( subLineInfo.mLineLength, subLineInfo.mMaxCharHeight ); // Size of this piece of line.
97       lineInfo.mAscender = subLineInfo.mMaxAscender;                                // Ascender of this piece of line.
98       relayoutData.mLines.push_back( lineInfo );
99
100       return Vector3( 0.f, previousPositionY + subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset, 0.f );
101     }
102   }
103   else
104   {
105     return Vector3( wordOffset, previousPositionY, 0.f );
106   }
107 }
108
109 /**
110  * Calculates character position.
111  * @param[in] relayoutParameters Temporary layout parameters (previous size, previous position, ... )
112  * @param[in] layoutParameters The layout parameters.
113  * @param[in] relayoutData The text-view's data structures.
114  * @return The character's position.
115  */
116 Vector3 SplitWhenExceedPosition( const TextViewRelayout::RelayoutParameters& relayoutParameters,
117                                  const TextView::LayoutParameters& layoutParameters,
118                                  TextView::RelayoutData& relayoutData )
119 {
120   const float wordOffset = ( relayoutParameters.mIsFirstCharacter ? 0.f :  relayoutParameters.mPositionOffset.x );
121   const float previousPositionY = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.y );
122
123   if( ( relayoutParameters.mIsNewLine || relayoutParameters.mIsFirstCharacter ) ||
124       ( relayoutParameters.mIsFirstCharacterOfWord && ( wordOffset + relayoutParameters.mWordSize.width > relayoutData.mTextViewSize.width ) ) ||
125       ( wordOffset + relayoutParameters.mCharacterSize.width > relayoutData.mTextViewSize.width ) )
126   {
127     if( !relayoutParameters.mIsNewLine &&
128         ( relayoutParameters.mIsWhiteSpace || relayoutParameters.mIsNewLineCharacter ) )
129     {
130       // Current character is a white space. Don't want to move a white space to the next line.
131       // These white spaces are placed just in the edge.
132       return Vector3( relayoutData.mTextViewSize.width - relayoutParameters.mWordSize.width, relayoutParameters.mPositionOffset.y, 0.f );
133     }
134     else
135     {
136       // Calculates the line length and the max character height for the current line.
137       TextViewRelayout::SubLineLayoutInfo subLineInfo;
138       subLineInfo.mLineLength = 0.f;
139       subLineInfo.mMaxCharHeight = 0.f;
140       subLineInfo.mMaxAscender = 0.f;
141       const TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *( relayoutData.mTextLayoutInfo.mLinesLayoutInfo.begin() + relayoutParameters.mIndices.mLineIndex ) );
142
143       TextViewRelayout::CalculateSubLineLayout( relayoutData.mTextViewSize.width,
144                                                 relayoutParameters.mIndices,
145                                                 lineLayoutInfo,
146                                                 TextViewRelayout::WrapByWordAndSplit,
147                                                 1.f, // Shrink factor.
148                                                 subLineInfo );
149
150       // Stores some info to calculate the line justification in a post-process.
151       TextView::LineJustificationInfo justificationInfo;
152
153       justificationInfo.mIndices = relayoutParameters.mIndices;
154       justificationInfo.mLineLength = subLineInfo.mLineLength;
155
156       relayoutData.mLineJustificationInfo.push_back( justificationInfo );
157
158       Toolkit::TextView::LineLayoutInfo lineInfo;
159       lineInfo.mCharacterGlobalIndex = relayoutParameters.mCharacterGlobalIndex;    // Index to the first character of the next line.
160       lineInfo.mSize = Size( subLineInfo.mLineLength, subLineInfo.mMaxCharHeight ); // Size of this piece of line.
161       lineInfo.mAscender = subLineInfo.mMaxAscender;                                // Ascender of this piece of line.
162       relayoutData.mLines.push_back( lineInfo );
163
164       return Vector3( 0.f, previousPositionY + subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset, 0.f );
165     }
166   }
167   else
168   {
169     return Vector3( wordOffset, previousPositionY, 0.f );
170   }
171 }
172
173 /**
174  * Calculates character position.
175  * @param[in] relayoutParameters Temporary layout parameters (previous size, previous position, ... )
176  * @param[in] layoutParameters The layout parameters.
177  * @param[in] relayoutData The text-view's data structures.
178  * @return The character's position.
179  */
180 Vector3 ShrinkWidthWhenExceedPosition( const TextViewRelayout::RelayoutParameters& relayoutParameters,
181                                        const TextView::LayoutParameters& layoutParameters,
182                                        TextView::RelayoutData& relayoutData )
183 {
184   const float wordOffset = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.x );
185   const float previousPositionY = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.y );
186   const Size wordSize = relayoutParameters.mWordSize * relayoutData.mShrinkFactor;
187
188   if( ( relayoutParameters.mIsNewLine || relayoutParameters.mIsFirstCharacter ) ||                                           // isNewLine is true when '\n' is found.
189       ( relayoutParameters.mIsFirstCharacterOfWord && ( wordOffset + wordSize.width > relayoutData.mTextViewSize.width ) ) ) // The word doesn't fit in the parent width.
190   {
191     if( !relayoutParameters.mIsNewLine &&
192         ( relayoutParameters.mIsWhiteSpace || relayoutParameters.mIsNewLineCharacter ) )
193     {
194       // Current character is a white space. Don't want to move a white space to the next line.
195       // These white spaces are placed just in the edge.
196       return Vector3( relayoutData.mTextViewSize.width - relayoutParameters.mWordSize.width, relayoutParameters.mPositionOffset.y, 0.f );
197     }
198     else
199     {
200       // Calculates the line length and the max character height for the current line.
201       TextViewRelayout::SubLineLayoutInfo subLineInfo;
202       subLineInfo.mLineLength = 0.f;
203       subLineInfo.mMaxCharHeight = 0.f;
204       subLineInfo.mMaxAscender = 0.f;
205       const TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *( relayoutData.mTextLayoutInfo.mLinesLayoutInfo.begin() + relayoutParameters.mIndices.mLineIndex ) );
206
207       TextViewRelayout::CalculateSubLineLayout( relayoutData.mTextViewSize.width,
208                                                 relayoutParameters.mIndices,
209                                                 lineLayoutInfo,
210                                                 TextViewRelayout::WrapByWord,
211                                                 relayoutData.mShrinkFactor,
212                                                 subLineInfo );
213
214       // Stores some info to calculate the line justification in a post-process.
215       TextView::LineJustificationInfo justificationInfo;
216
217       justificationInfo.mIndices = relayoutParameters.mIndices;
218       justificationInfo.mLineLength = subLineInfo.mLineLength;
219
220       relayoutData.mLineJustificationInfo.push_back( justificationInfo );
221
222       Toolkit::TextView::LineLayoutInfo lineInfo;
223       lineInfo.mCharacterGlobalIndex = relayoutParameters.mCharacterGlobalIndex;    // Index to the first character of the next line.
224       lineInfo.mSize = Size( subLineInfo.mLineLength, subLineInfo.mMaxCharHeight ); // Size of this piece of line.
225       lineInfo.mAscender = subLineInfo.mMaxAscender;                                // Ascender of this piece of line.
226       relayoutData.mLines.push_back( lineInfo );
227
228       return Vector3( 0.f, previousPositionY + subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset * relayoutData.mShrinkFactor, 0.f );
229     }
230   }
231   else
232   {
233     return Vector3( wordOffset, previousPositionY, 0.f );
234   }
235 }
236
237 void CalculatePositionsForShrinkWhenExceed( TextView::RelayoutData& relayoutData,
238                                             const TextView::LayoutParameters& layoutParameters,
239                                             const float shrinkFactor,
240                                             float& newTextHeight )
241 {
242   const float parentWidth = relayoutData.mTextViewSize.width;
243   TextViewProcessor::TextLayoutInfo& textLayoutInfo = relayoutData.mTextLayoutInfo;
244
245   relayoutData.mLineJustificationInfo.clear();
246
247   // Reset the text height. This value is returned in order to shrink further or not the text.
248   newTextHeight = 0.f;
249
250   // Whether the first character is being processed.
251   bool isFirstChar = true;
252
253   // Stores the size of the previous character.
254   Size previousSize;
255   // Stores the position of the previous character.
256   Vector3 previousPosition;
257
258   // Reset the index of lines.
259   TextViewProcessor::TextInfoIndices indices;
260
261   // Whether the last character of the whole text is a new line char.
262   // This information is used to increase or not the height of the whole text by one line.
263   // Increase the whole text's height by one line is useful i.e. in TextInput to place the cursor
264   // after pressing 'Enter' in the last line.
265   bool isLastCharacterNewLineChar = false;
266   // Stores the height of the last character. This height used to be added to the whole text height if
267   // isLastCharacterNewLineChar is true.
268   float lastCharHeight = 0.f;
269
270   relayoutData.mLines.clear();
271   std::size_t characterGlobalIndex = 0u;
272
273   for( TextViewProcessor::LineLayoutInfoContainer::iterator lineIt = textLayoutInfo.mLinesLayoutInfo.begin(), lineEndIt = textLayoutInfo.mLinesLayoutInfo.end();
274        lineIt != lineEndIt;
275        ++lineIt, ++indices.mLineIndex )
276   {
277     TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *lineIt );
278
279     // The next character is in a new line.
280     bool isNewLine = true;
281
282     // Reset the index of words.
283     indices.mWordIndex = 0u;
284
285     for( TextViewProcessor::WordLayoutInfoContainer::iterator wordIt = lineLayoutInfo.mWordsLayoutInfo.begin(), wordEndIt = lineLayoutInfo.mWordsLayoutInfo.end();
286          wordIt != wordEndIt;
287          ++wordIt, ++indices.mWordIndex )
288     {
289       TextViewProcessor::WordLayoutInfo& wordLayoutInfo( *wordIt );
290
291       // Reset the index of the character.
292       indices.mCharacterIndex = 0u;
293
294       // Whether current character is the first of the word.
295       bool isFirstCharOfWord = true;
296       const float wordOffset = previousPosition.x + previousSize.width;
297
298       isLastCharacterNewLineChar = ( TextViewProcessor::LineSeparator == wordLayoutInfo.mType );
299
300       for( TextViewProcessor::CharacterLayoutInfoContainer::iterator charIt = wordLayoutInfo.mCharactersLayoutInfo.begin(), charEndIt = wordLayoutInfo.mCharactersLayoutInfo.end();
301            charIt != charEndIt;
302            ++charIt, ++indices.mCharacterIndex )
303       {
304         TextViewProcessor::CharacterLayoutInfo& characterLayoutInfo( *charIt );
305         lastCharHeight = characterLayoutInfo.mSize.height * shrinkFactor;
306
307         const float previousPositionY = isFirstChar ? 0.f : previousPosition.y;
308
309         if( ( isNewLine || isFirstChar ) ||
310             ( isFirstCharOfWord && ( wordOffset + wordLayoutInfo.mSize.width * shrinkFactor > parentWidth ) ) )
311         {
312           isFirstChar = false;
313
314           // Calculates the line length and the max character height for the current line.
315           TextViewRelayout::SubLineLayoutInfo subLineInfo;
316           subLineInfo.mLineLength = 0.f;
317           subLineInfo.mMaxCharHeight = 0.f;
318           subLineInfo.mMaxAscender = 0.f;
319           TextViewRelayout::CalculateSubLineLayout( parentWidth,
320                                                     indices,
321                                                     lineLayoutInfo,
322                                                     TextViewRelayout::WrapByWord,
323                                                     shrinkFactor,
324                                                     subLineInfo );
325
326           characterLayoutInfo.mPosition = Vector3( 0.f, previousPositionY + subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset * shrinkFactor, 0.f );
327
328           newTextHeight += subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset * shrinkFactor;
329
330           Toolkit::TextView::LineLayoutInfo lineInfo;
331           lineInfo.mCharacterGlobalIndex = characterGlobalIndex;                        // Index to the first character of the next line.
332           lineInfo.mSize = Size( subLineInfo.mLineLength, subLineInfo.mMaxCharHeight ); // Size of this piece of line.
333           lineInfo.mAscender = subLineInfo.mMaxAscender;                                // Ascender of this piece of line.
334           relayoutData.mLines.push_back( lineInfo );
335
336           // Stores some info to calculate the line justification in a post-process.
337           TextView::LineJustificationInfo justificationInfo;
338
339           justificationInfo.mIndices = indices;
340           justificationInfo.mLineLength = subLineInfo.mLineLength;
341
342           relayoutData.mLineJustificationInfo.push_back( justificationInfo );
343         }
344         else
345         {
346           characterLayoutInfo.mPosition = previousPosition + Vector3( previousSize.width, 0.f, 0.f );
347         }
348
349         // Get last line info and calculate the bearing.
350         const Toolkit::TextView::LineLayoutInfo& lineInfo( *( relayoutData.mLines.end() - 1u ) );
351         const float bearingOffset = ( ( lineInfo.mSize.height - lineInfo.mAscender ) - ( characterLayoutInfo.mSize.height - characterLayoutInfo.mAscender ) ) * shrinkFactor;
352
353         previousSize = characterLayoutInfo.mSize * shrinkFactor;
354         previousPosition = characterLayoutInfo.mPosition;
355         characterLayoutInfo.mPosition.y -= bearingOffset;
356         isFirstCharOfWord = false;
357         isNewLine = false;
358
359         ++characterGlobalIndex;
360       }
361     }
362   }
363
364   if( isLastCharacterNewLineChar )
365   {
366     newTextHeight += lastCharHeight + layoutParameters.mLineHeightOffset * shrinkFactor;
367   }
368 }
369
370 float RelayoutForShrinkToFit( TextView::RelayoutData& relayoutData,
371                               const TextView::LayoutParameters& layoutParameters )
372 {
373   const Size& textViewSize = relayoutData.mTextViewSize;
374   TextViewProcessor::TextLayoutInfo& textLayoutInfo = relayoutData.mTextLayoutInfo;
375
376   // First step is assure the longest word fits in the text view width.
377   float shrinkFactor = ( textLayoutInfo.mMaxWordWidth > textViewSize.width ? textViewSize.width / textLayoutInfo.mMaxWordWidth : 1.f );
378
379   // Out parameter. Will store the new text height after relayout the text.
380   float newTextHeight = 0.f;
381
382   // Relayout the text for the given character's sizes.
383   CalculatePositionsForShrinkWhenExceed( relayoutData,
384                                          layoutParameters,
385                                          shrinkFactor,
386                                          newTextHeight );
387
388   if( newTextHeight > textViewSize.height )
389   {
390     // After relayouting, the text exceeds the text view height.
391     // Find a new scale factor to fit all the text in the text view size is needed.
392
393     // The next algorithm does some iterations to calculate an acceptable scale factor.
394     // Some magic numbers are defined.
395
396     const float MIN_RATIO( 0.90f );         // The algorithm finishes if the ratio
397     const float MAX_RATIO( 1.00f );         // new_text_height / text_view_height is between this two values
398     const unsigned int MAX_ITERATIONS( 8u ); // or max_iteration is reached.
399
400     float ratio = newTextHeight / textViewSize.height;
401
402     float maxScaleFactor = shrinkFactor;                        // bigger scale factors than maxScaleFactor will produce a too big text.
403     float minScaleFactor = shrinkFactor * ( textViewSize.height / newTextHeight ); // smaller scale factors than minScaleFactor will produce a too small text.
404
405     for( unsigned int iterations = 0u; ( ( MIN_RATIO > ratio ) || ( ratio > MAX_RATIO )  ) && ( iterations < MAX_ITERATIONS ); ++iterations )
406     {
407       // Calculates the new scale factor.
408       // The new scale factor is always between the min and max scale factors.
409       // If ratio < 1 it means the text is too small and a bigger scale factor is needed. In this case the algorithm selects a new scale factor close to
410       // minScaleFactor. Alternatively if the text is too big a new scale factor close to maxScaleFactor is selected.
411       // This allows the text shrink or grow smoothly.
412       shrinkFactor = minScaleFactor + ( ratio < 1.f ? 0.4f : 0.6f ) * ( maxScaleFactor - minScaleFactor );
413
414       CalculatePositionsForShrinkWhenExceed( relayoutData, // Relayout the text for the given character's sizes.
415                                              layoutParameters,
416                                              shrinkFactor,
417                                              newTextHeight );
418
419       // Calculates the new text size ratio. It allows update the min and max scale factors.
420       // If the ratio is not good enough a new scale factor between min and max could be used in next iteration.
421       ratio = newTextHeight / textViewSize.height;
422       if( ratio < 1.f )
423       {
424         minScaleFactor = shrinkFactor;
425       }
426       else
427       {
428         maxScaleFactor = shrinkFactor;
429       }
430     }
431
432     if( ratio > MAX_RATIO )
433     {
434       // The algorithm didn't find an acceptable scale factor.
435       // In that case the text is shrunk to fit in the boundaries of the text view actor.
436       shrinkFactor = minScaleFactor;
437
438       CalculatePositionsForShrinkWhenExceed( relayoutData,
439                                              layoutParameters,
440                                              shrinkFactor,
441                                              newTextHeight );
442     }
443   }
444
445   return shrinkFactor;
446 }
447
448 void CalculateSizeAndPosition( const TextView::LayoutParameters& layoutParameters,
449                                TextView::RelayoutData& relayoutData )
450 {
451   TextViewRelayout::RelayoutParameters relayoutParameters;
452
453   // clear
454   relayoutData.mCharacterLayoutInfoTable.clear();
455   relayoutData.mLines.clear();
456   relayoutData.mTextSizeForRelayoutOption = Size();
457
458   // Calculates the text size for split by char.
459   Vector4 minMaxXY( std::numeric_limits<float>::max(),
460                     std::numeric_limits<float>::max(),
461                     std::numeric_limits<float>::min(),
462                     std::numeric_limits<float>::min() );
463
464   relayoutData.mShrinkFactor = 1.f; // Shrink factor used when the exceed policy contains ShrinkToFit
465
466   if( TextView::Shrink == layoutParameters.mExceedPolicy )
467   {
468     // Relays-out the text for the shrink to fit policy.
469     relayoutData.mShrinkFactor = RelayoutForShrinkToFit( relayoutData, layoutParameters );
470   }
471   else if( TextView::ShrinkOriginal == layoutParameters.mExceedPolicy )
472   {
473     relayoutData.mShrinkFactor = ( relayoutData.mTextLayoutInfo.mMaxWordWidth > relayoutData.mTextViewSize.width ? relayoutData.mTextViewSize.width / relayoutData.mTextLayoutInfo.mMaxWordWidth : 1.f );
474   }
475
476   relayoutParameters.mPositionOffset = Vector3::ZERO;
477   relayoutParameters.mIsFirstCharacter = true;
478   relayoutParameters.mIndices.mLineIndex = 0u;
479   relayoutParameters.mCharacterGlobalIndex = 0u;
480
481   for( TextViewProcessor::LineLayoutInfoContainer::iterator lineLayoutIt = relayoutData.mTextLayoutInfo.mLinesLayoutInfo.begin(),
482        endLineLayoutIt = relayoutData.mTextLayoutInfo.mLinesLayoutInfo.end();
483        lineLayoutIt != endLineLayoutIt;
484        ++lineLayoutIt, ++relayoutParameters.mIndices.mLineIndex )
485   {
486     TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *lineLayoutIt );
487
488     relayoutParameters.mIsNewLine = true;
489     relayoutParameters.mLineSize = lineLayoutInfo.mSize;
490     relayoutParameters.mIndices.mWordIndex = 0u;
491
492     for( TextViewProcessor::WordLayoutInfoContainer::iterator wordLayoutIt = lineLayoutInfo.mWordsLayoutInfo.begin(),
493            endWordLayoutIt = lineLayoutInfo.mWordsLayoutInfo.end();
494          wordLayoutIt != endWordLayoutIt;
495          ++wordLayoutIt, ++relayoutParameters.mIndices.mWordIndex )
496     {
497       TextViewProcessor::WordLayoutInfo& wordLayoutInfo( *wordLayoutIt );
498       relayoutParameters.mIsWhiteSpace = TextViewProcessor::WordSeparator == wordLayoutInfo.mType;
499       relayoutParameters.mIsNewLineCharacter = TextViewProcessor::LineSeparator == wordLayoutInfo.mType;
500
501       relayoutParameters.mIsFirstCharacterOfWord = true;
502       relayoutParameters.mWordSize = wordLayoutInfo.mSize;
503       relayoutParameters.mIndices.mCharacterIndex = 0u;
504
505       for( TextViewProcessor::CharacterLayoutInfoContainer::iterator characterLayoutIt = wordLayoutInfo.mCharactersLayoutInfo.begin(),
506              endCharacterLayoutIt = wordLayoutInfo.mCharactersLayoutInfo.end();
507            ( characterLayoutIt != endCharacterLayoutIt );
508            ++characterLayoutIt, ++relayoutParameters.mIndices.mCharacterIndex )
509       {
510         TextViewProcessor::CharacterLayoutInfo& characterLayoutInfo( *characterLayoutIt );
511
512         relayoutParameters.mCharacterSize = characterLayoutInfo.mSize;
513
514         switch( layoutParameters.mExceedPolicy )
515         {
516           case TextView::OriginalShrink:
517           case TextView::SplitShrink:
518           case TextView::ShrinkFade:
519           {
520             DALI_LOG_WARNING( "SplitByWord::CalculateSizeAndPosition() policy not implemented.\n" );
521             break;
522           }
523           case TextView::Original:
524           case TextView::OriginalFade:
525           case TextView::FadeOriginal:
526           case TextView::Fade:
527           case TextView::EllipsizeEndOriginal:
528           case TextView::EllipsizeEnd: // Fall Through
529           {
530             characterLayoutInfo.mPosition = OriginalPosition( relayoutParameters,
531                                                               layoutParameters,
532                                                               relayoutData );
533
534             relayoutParameters.mPositionOffset = characterLayoutInfo.mPosition + Vector3( characterLayoutInfo.mSize.width, 0.f, 0.f );
535             break;
536           }
537           case TextView::SplitOriginal:
538           case TextView::SplitFade:
539           case TextView::SplitEllipsizeEnd:
540           {
541             characterLayoutInfo.mPosition = SplitWhenExceedPosition( relayoutParameters,
542                                                                      layoutParameters,
543                                                                      relayoutData );
544
545             relayoutParameters.mPositionOffset = characterLayoutInfo.mPosition + Vector3( characterLayoutInfo.mSize.width, 0.f, 0.f );
546             break;
547           }
548           case TextView::ShrinkOriginal:
549           {
550             characterLayoutInfo.mPosition = ShrinkWidthWhenExceedPosition( relayoutParameters,
551                                                                            layoutParameters,
552                                                                            relayoutData );
553
554             relayoutParameters.mPositionOffset = characterLayoutInfo.mPosition + Vector3( characterLayoutInfo.mSize.width * relayoutData.mShrinkFactor, 0.f, 0.f );
555             break;
556           }
557           case TextView::Shrink:
558           {
559             // Does nothing. All the job has been done in the RelayoutForShrinkToFit() function.
560             break;
561           }
562           default:
563           {
564             DALI_LOG_WARNING( "SplitByWord::CalculateSizeAndPosition() policy combination not possible.\n" );
565           }
566         }
567
568         // Get last line info and calculate the bearing (used to align glyphs with the baseline).
569         if( TextView::Shrink != layoutParameters.mExceedPolicy )
570         {
571           TextViewRelayout::CalculateBearing( characterLayoutInfo, relayoutData );
572         }
573
574         // updates min and max position to calculate the text size for split by word.
575         TextViewRelayout::UpdateLayoutInfoTable( minMaxXY,
576                                                  wordLayoutInfo,
577                                                  characterLayoutInfo,
578                                                  relayoutParameters,
579                                                  relayoutData );
580
581         ++relayoutParameters.mCharacterGlobalIndex;
582         relayoutParameters.mIsFirstCharacter = false;
583         relayoutParameters.mIsFirstCharacterOfWord = false;
584         relayoutParameters.mIsNewLine = false;
585       } // end characters
586     } // end words
587   } // end lines
588
589   if( relayoutData.mCharacterLayoutInfoTable.empty() )
590   {
591     relayoutData.mTextSizeForRelayoutOption = Size();
592   }
593   else
594   {
595     relayoutData.mTextSizeForRelayoutOption.width = minMaxXY.z - minMaxXY.x;
596     relayoutData.mTextSizeForRelayoutOption.height = minMaxXY.w - minMaxXY.y;
597   }
598
599   // Check if the last character is a new line character. In that case the height should be added.
600   if( !relayoutData.mTextLayoutInfo.mLinesLayoutInfo.empty() )
601   {
602     const TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *( relayoutData.mTextLayoutInfo.mLinesLayoutInfo.end() - 1u ) );
603
604     if( lineLayoutInfo.mWordsLayoutInfo.empty() ) // if it's empty, it means the last character is a new line character.
605     {
606       relayoutData.mTextSizeForRelayoutOption.height += lineLayoutInfo.mSize.height * relayoutData.mShrinkFactor;
607     }
608   }
609 }
610
611 } // namespace
612
613 void Relayout( Actor textView,
614                TextView::RelayoutOperationMask relayoutOperationMask,
615                const TextView::LayoutParameters& layoutParameters,
616                const TextView::VisualParameters& visualParameters,
617                TextView::RelayoutData& relayoutData )
618 {
619   if( relayoutOperationMask & TextView::RELAYOUT_SIZE_POSITION )
620   {
621     relayoutData.mLineJustificationInfo.clear();
622     CalculateSizeAndPosition( layoutParameters,
623                               relayoutData );
624
625     TextViewRelayout::SetUnderlineInfo( relayoutData );
626   }
627
628   if( relayoutOperationMask & TextView::RELAYOUT_ALIGNMENT )
629   {
630     TextViewRelayout::UpdateAlignment( layoutParameters,
631                                        relayoutData );
632   }
633
634   if( relayoutOperationMask & TextView::RELAYOUT_VISIBILITY )
635   {
636     TextViewRelayout::UpdateVisibility( layoutParameters,
637                                         visualParameters,
638                                         relayoutData );
639   }
640   const bool initializeTextActors = relayoutOperationMask & TextView::RELAYOUT_INITIALIZE_TEXT_ACTORS;
641   const bool updateTextActors = relayoutOperationMask & TextView::RELAYOUT_TEXT_ACTOR_UPDATE;
642   if( initializeTextActors || updateTextActors )
643   {
644     TextViewRelayout::UpdateTextActorInfo( visualParameters,
645                                            relayoutData,
646                                            initializeTextActors );
647   }
648
649   if( relayoutOperationMask & TextView::RELAYOUT_INSERT_TO_TEXT_VIEW )
650   {
651     TextViewRelayout::InsertToTextView( textView,
652                                         relayoutData );
653   }
654 }
655
656 } // namespace SplitByWord
657
658 } // namespace Internal
659
660 } // namespace Toolkit
661
662 } // namespace Dali