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