TextView - Remove groups of words.
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / text-view / split-by-char-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-char-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 SplitByChar
36 {
37
38 namespace
39 {
40
41 Vector3 NoShrinkWhenExceedPosition( 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         ( wordOffset + relayoutParameters.mCharacterSize.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       // Calculate the line length 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::WrapByCharacter,
72                                                 1.f, // Shrink factor
73                                                 subLineInfo );
74
75       // Stores some info to calculate the line justification in a post-process.
76       TextView::LineJustificationInfo justificationInfo;
77
78       justificationInfo.mIndices = relayoutParameters.mIndices;
79       justificationInfo.mLineLength = subLineInfo.mLineLength;
80
81       relayoutData.mLineJustificationInfo.push_back( justificationInfo );
82
83       Toolkit::TextView::LineLayoutInfo lineInfo;
84       lineInfo.mCharacterGlobalIndex = relayoutParameters.mCharacterGlobalIndex;    // Index to the first character of the next line.
85       lineInfo.mSize = Size( subLineInfo.mLineLength, subLineInfo.mMaxCharHeight ); // Size of this piece of line.
86       lineInfo.mAscender = subLineInfo.mMaxAscender;                                // Ascender of this piece of line.
87       relayoutData.mLines.push_back( lineInfo );
88
89       return Vector3( 0.f, previousPositionY + subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset, 0.f );
90     }
91   }
92   else
93   {
94     return Vector3( wordOffset, previousPositionY, 0.f );
95   }
96 }
97
98 void CalculateSizeAndPosition( const TextView::LayoutParameters& layoutParameters,
99                                TextView::RelayoutData& relayoutData )
100 {
101   TextViewRelayout::RelayoutParameters relayoutParameters;
102
103   // clear
104   relayoutData.mCharacterLayoutInfoTable.clear();
105   relayoutData.mLines.clear();
106   relayoutData.mTextSizeForRelayoutOption = Size();
107
108   // Calculate the text size for split by char.
109   Vector4 minMaxXY( std::numeric_limits<float>::max(),
110                     std::numeric_limits<float>::max(),
111                     std::numeric_limits<float>::min(),
112                     std::numeric_limits<float>::min() );
113
114   relayoutData.mShrinkFactor = 1.f; // Shrink factor used when the exceed policy contains ShrinkToFit
115
116   relayoutParameters.mPositionOffset = Vector3::ZERO;
117   relayoutParameters.mIsFirstCharacter = true;
118   relayoutParameters.mIndices.mLineIndex = 0;
119   relayoutParameters.mCharacterGlobalIndex = 0;
120
121   for( TextViewProcessor::LineLayoutInfoContainer::iterator lineLayoutIt = relayoutData.mTextLayoutInfo.mLinesLayoutInfo.begin(),
122          endLineLayoutIt = relayoutData.mTextLayoutInfo.mLinesLayoutInfo.end();
123        lineLayoutIt != endLineLayoutIt;
124        ++lineLayoutIt, ++relayoutParameters.mIndices.mLineIndex )
125   {
126     TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *lineLayoutIt );
127
128     relayoutParameters.mIsNewLine = true;
129     relayoutParameters.mLineSize = lineLayoutInfo.mSize;
130     relayoutParameters.mIndices.mWordIndex = 0;
131
132     for( TextViewProcessor::WordLayoutInfoContainer::iterator wordLayoutIt = lineLayoutInfo.mWordsLayoutInfo.begin(),
133            endWordLayoutIt = lineLayoutInfo.mWordsLayoutInfo.end();
134          wordLayoutIt != endWordLayoutIt;
135          ++wordLayoutIt, ++relayoutParameters.mIndices.mWordIndex )
136     {
137       TextViewProcessor::WordLayoutInfo& wordLayoutInfo( *wordLayoutIt );
138       relayoutParameters.mIsWhiteSpace = TextViewProcessor::WordSeparator == wordLayoutInfo.mType;
139       relayoutParameters.mIsNewLineCharacter = TextViewProcessor::LineSeparator == wordLayoutInfo.mType;
140
141       relayoutParameters.mIsFirstCharacterOfWord = true;
142       relayoutParameters.mWordSize = wordLayoutInfo.mSize;
143       relayoutParameters.mIndices.mCharacterIndex = 0;
144
145       for( TextViewProcessor::CharacterLayoutInfoContainer::iterator characterLayoutIt = wordLayoutInfo.mCharactersLayoutInfo.begin(),
146              endCharacterLayoutIt = wordLayoutInfo.mCharactersLayoutInfo.end();
147            characterLayoutIt != endCharacterLayoutIt;
148            ++characterLayoutIt, ++relayoutParameters.mIndices.mCharacterIndex )
149       {
150         TextViewProcessor::CharacterLayoutInfo& characterLayoutInfo( *characterLayoutIt );
151
152         relayoutParameters.mCharacterSize = characterLayoutInfo.mSize;
153
154         switch( layoutParameters.mExceedPolicy )
155         {
156           case TextView::OriginalShrink:
157           case TextView::SplitOriginal:
158           case TextView::SplitFade:
159           case TextView::SplitEllipsizeEnd:
160           case TextView::SplitShrink:
161           case TextView::ShrinkOriginal:
162           case TextView::ShrinkFade:
163           case TextView::Shrink:
164           case TextView::EllipsizeEndOriginal:
165           case TextView::EllipsizeEnd: // Fall Through
166           {
167             DALI_LOG_WARNING( "SplitByChar::CalculateSizeAndPosition() policy not implemented.\n" );
168             break;
169           }
170           case TextView::OriginalFade:
171           case TextView::FadeOriginal:
172           case TextView::Original:
173           case TextView::Fade: // Fall Through
174           {
175             characterLayoutInfo.mPosition = NoShrinkWhenExceedPosition( relayoutParameters,
176                                                                         layoutParameters,
177                                                                         relayoutData );
178
179             relayoutParameters.mPositionOffset = characterLayoutInfo.mPosition + Vector3( characterLayoutInfo.mSize.width, 0.f, 0.f );
180             break;
181           }
182           default:
183           {
184             DALI_LOG_WARNING( "SplitByChar::CalculateSizeAndPosition() policy combination not possible.\n" );
185           }
186         }
187
188         // Get last line info and calculate the bearing (used to align glyphs with the baseline).
189         TextViewRelayout::CalculateBearing( characterLayoutInfo, relayoutData );
190
191         // updates min and max position to calculate the text size for split by char.
192         TextViewRelayout::UpdateLayoutInfoTable( minMaxXY,
193                                                  wordLayoutInfo,
194                                                  characterLayoutInfo,
195                                                  relayoutParameters,
196                                                  relayoutData );
197
198         ++relayoutParameters.mCharacterGlobalIndex;
199         relayoutParameters.mIsFirstCharacter = false;
200         relayoutParameters.mIsNewLine = false;
201       } // end characters
202     } // end words
203   } // end lines
204
205   if( relayoutData.mCharacterLayoutInfoTable.empty() )
206   {
207     relayoutData.mTextSizeForRelayoutOption = Size();
208   }
209   else
210   {
211     relayoutData.mTextSizeForRelayoutOption.width = minMaxXY.z - minMaxXY.x;
212     relayoutData.mTextSizeForRelayoutOption.height = minMaxXY.w - minMaxXY.y;
213   }
214
215   // Check if the last character is a new line character. In that case the height should be added.
216   if( !relayoutData.mTextLayoutInfo.mLinesLayoutInfo.empty() )
217   {
218     const TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *( relayoutData.mTextLayoutInfo.mLinesLayoutInfo.end() - 1 ) );
219
220     if( lineLayoutInfo.mWordsLayoutInfo.empty() ) // if it's empty, it means the last character is a new line character.
221     {
222       relayoutData.mTextSizeForRelayoutOption.height += lineLayoutInfo.mSize.height;
223     }
224   }
225 }
226
227 } // namespace
228
229 void Relayout( Actor textView,
230                TextView::RelayoutOperationMask relayoutOperationMask,
231                const TextView::LayoutParameters& layoutParameters,
232                const TextView::VisualParameters& visualParameters,
233                TextView::RelayoutData& relayoutData )
234 {
235   if( relayoutOperationMask & TextView::RELAYOUT_SIZE_POSITION )
236   {
237     relayoutData.mLineJustificationInfo.clear();
238     CalculateSizeAndPosition( layoutParameters,
239                               relayoutData );
240
241     TextViewRelayout::SetUnderlineInfo( relayoutData );
242   }
243
244   if( relayoutOperationMask & TextView::RELAYOUT_ALIGNMENT )
245   {
246     TextViewRelayout::UpdateAlignment( layoutParameters,
247                                        relayoutData );
248   }
249
250   if( relayoutOperationMask & TextView::RELAYOUT_VISIBILITY )
251   {
252     TextViewRelayout::UpdateVisibility( layoutParameters,
253                                         visualParameters,
254                                         relayoutData );
255   }
256
257   if( relayoutOperationMask & TextView::RELAYOUT_INITIALIZE_TEXT_ACTORS )
258   {
259     TextViewProcessor::InitializeTextActorInfo( relayoutData );
260   }
261
262   if( relayoutOperationMask & TextView::RELAYOUT_TEXT_ACTOR_UPDATE )
263   {
264     TextViewRelayout::UpdateTextActorInfo( visualParameters,
265                                            relayoutData );
266   }
267
268   if( ( relayoutOperationMask & TextView::RELAYOUT_INSERT_TO_TEXT_VIEW ) ||
269       ( relayoutOperationMask & TextView::RELAYOUT_INSERT_TO_TEXT_ACTOR_LIST ) )
270   {
271     TextViewRelayout::InsertToTextView( relayoutOperationMask,
272                                         textView,
273                                         relayoutData );
274   }
275 }
276
277 } // namespace SplitByChar
278
279 } // namespace Internal
280
281 } // namespace Toolkit
282
283 } // namespace Dali