2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
19 #include "split-by-char-policies.h"
25 #include "relayout-utilities.h"
26 #include "text-view-processor.h"
27 #include <dali/integration-api/debug.h>
44 Vector3 NoShrinkWhenExceedPosition( const TextViewRelayout::RelayoutParameters& relayoutParameters,
45 const TextView::LayoutParameters& layoutParameters,
46 TextView::RelayoutData& relayoutData )
48 const float wordOffset = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.x );
49 const float previousPositionY = ( relayoutParameters.mIsFirstCharacter ? 0.f : relayoutParameters.mPositionOffset.y );
51 if( ( relayoutParameters.mIsNewLine ||
52 relayoutParameters.mIsFirstCharacter ||
53 ( wordOffset + relayoutParameters.mCharacterSize.width > relayoutData.mTextViewSize.width ) ) )
55 if( !relayoutParameters.mIsNewLine &&
56 ( relayoutParameters.mIsWhiteSpace || relayoutParameters.mIsNewLineCharacter ) )
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 );
64 // Calculate the line length 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 ) );
71 TextViewRelayout::CalculateSubLineLayout( relayoutData.mTextViewSize.width,
72 relayoutParameters.mIndices,
74 TextViewRelayout::WrapByCharacter,
78 // Stores some info to calculate the line justification in a post-process.
79 TextView::LineJustificationInfo justificationInfo;
81 justificationInfo.mIndices = relayoutParameters.mIndices;
82 justificationInfo.mLineLength = subLineInfo.mLineLength;
84 relayoutData.mLineJustificationInfo.push_back( justificationInfo );
86 Toolkit::TextView::LineLayoutInfo lineInfo;
87 lineInfo.mCharacterGlobalIndex = relayoutParameters.mCharacterGlobalIndex; // Index to the first character of the next line.
88 lineInfo.mSize = Size( subLineInfo.mLineLength, subLineInfo.mMaxCharHeight ); // Size of this piece of line.
89 lineInfo.mAscender = subLineInfo.mMaxAscender; // Ascender of this piece of line.
90 relayoutData.mLines.push_back( lineInfo );
92 return Vector3( 0.f, previousPositionY + subLineInfo.mMaxCharHeight + layoutParameters.mLineHeightOffset, 0.f );
97 return Vector3( wordOffset, previousPositionY, 0.f );
101 void CalculateSizeAndPosition( const TextView::LayoutParameters& layoutParameters,
102 TextView::RelayoutData& relayoutData )
104 TextViewRelayout::RelayoutParameters relayoutParameters;
107 relayoutData.mCharacterLayoutInfoTable.clear();
108 relayoutData.mLines.clear();
109 relayoutData.mTextSizeForRelayoutOption = Size();
111 // Calculate the text size for split by char.
112 Vector4 minMaxXY( std::numeric_limits<float>::max(),
113 std::numeric_limits<float>::max(),
114 std::numeric_limits<float>::min(),
115 std::numeric_limits<float>::min() );
117 relayoutData.mShrinkFactor = 1.f; // Shrink factor used when the exceed policy contains ShrinkToFit
119 relayoutParameters.mPositionOffset = Vector3::ZERO;
120 relayoutParameters.mIsFirstCharacter = true;
121 relayoutParameters.mIndices.mLineIndex = 0;
122 relayoutParameters.mCharacterGlobalIndex = 0;
124 for( TextViewProcessor::LineLayoutInfoContainer::iterator lineLayoutIt = relayoutData.mTextLayoutInfo.mLinesLayoutInfo.begin(),
125 endLineLayoutIt = relayoutData.mTextLayoutInfo.mLinesLayoutInfo.end();
126 lineLayoutIt != endLineLayoutIt;
127 ++lineLayoutIt, ++relayoutParameters.mIndices.mLineIndex )
129 TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *lineLayoutIt );
131 relayoutParameters.mIsNewLine = true;
132 relayoutParameters.mLineSize = lineLayoutInfo.mSize;
133 relayoutParameters.mIndices.mGroupIndex = 0;
135 for( TextViewProcessor::WordGroupLayoutInfoContainer::iterator groupLayoutIt = lineLayoutInfo.mWordGroupsLayoutInfo.begin(),
136 endGroupLayoutIt = lineLayoutInfo.mWordGroupsLayoutInfo.end();
137 groupLayoutIt != endGroupLayoutIt;
138 ++groupLayoutIt, ++relayoutParameters.mIndices.mGroupIndex )
140 TextViewProcessor::WordGroupLayoutInfo& wordGroupLayoutInfo( *groupLayoutIt );
142 relayoutParameters.mIndices.mWordIndex = 0;
144 for( TextViewProcessor::WordLayoutInfoContainer::iterator wordLayoutIt = wordGroupLayoutInfo.mWordsLayoutInfo.begin(),
145 endWordLayoutIt = wordGroupLayoutInfo.mWordsLayoutInfo.end();
146 wordLayoutIt != endWordLayoutIt;
147 ++wordLayoutIt, ++relayoutParameters.mIndices.mWordIndex )
149 TextViewProcessor::WordLayoutInfo& wordLayoutInfo( *wordLayoutIt );
150 relayoutParameters.mIsWhiteSpace = TextViewProcessor::WordSeparator == wordLayoutInfo.mType;
151 relayoutParameters.mIsNewLineCharacter = TextViewProcessor::LineSeparator == wordLayoutInfo.mType;
153 relayoutParameters.mIsFirstCharacterOfWord = true;
154 relayoutParameters.mWordSize = wordLayoutInfo.mSize;
155 relayoutParameters.mIndices.mCharacterIndex = 0;
157 for( TextViewProcessor::CharacterLayoutInfoContainer::iterator characterLayoutIt = wordLayoutInfo.mCharactersLayoutInfo.begin(),
158 endCharacterLayoutIt = wordLayoutInfo.mCharactersLayoutInfo.end();
159 characterLayoutIt != endCharacterLayoutIt;
160 ++characterLayoutIt, ++relayoutParameters.mIndices.mCharacterIndex )
162 TextViewProcessor::CharacterLayoutInfo& characterLayoutInfo( *characterLayoutIt );
164 relayoutParameters.mCharacterSize = characterLayoutInfo.mSize;
166 switch( layoutParameters.mExceedPolicy )
168 case TextView::OriginalShrink:
169 case TextView::SplitOriginal:
170 case TextView::SplitFade:
171 case TextView::SplitEllipsizeEnd:
172 case TextView::SplitShrink:
173 case TextView::ShrinkOriginal:
174 case TextView::ShrinkFade:
175 case TextView::Shrink:
176 case TextView::EllipsizeEndOriginal:
177 case TextView::EllipsizeEnd: // Fall Through
179 DALI_LOG_WARNING( "SplitByChar::CalculateSizeAndPosition() policy not implemented.\n" );
182 case TextView::OriginalFade:
183 case TextView::FadeOriginal:
184 case TextView::Original:
185 case TextView::Fade: // Fall Through
187 characterLayoutInfo.mPosition = NoShrinkWhenExceedPosition( relayoutParameters,
191 relayoutParameters.mPositionOffset = characterLayoutInfo.mPosition + Vector3( characterLayoutInfo.mSize.width, 0.f, 0.f );
196 DALI_LOG_WARNING( "SplitByChar::CalculateSizeAndPosition() policy combination not possible.\n" );
200 // Get last line info and calculate the bearing (used to align glyphs with the baseline).
201 TextViewRelayout::CalculateBearing( characterLayoutInfo, relayoutData );
203 // updates min and max position to calculate the text size for split by char.
204 TextViewRelayout::UpdateLayoutInfoTable( minMaxXY,
211 ++relayoutParameters.mCharacterGlobalIndex;
212 relayoutParameters.mIsFirstCharacter = false;
213 relayoutParameters.mIsNewLine = false;
216 } // end group of words
219 if( relayoutData.mCharacterLayoutInfoTable.empty() )
221 relayoutData.mTextSizeForRelayoutOption = Size();
225 relayoutData.mTextSizeForRelayoutOption.width = minMaxXY.z - minMaxXY.x;
226 relayoutData.mTextSizeForRelayoutOption.height = minMaxXY.w - minMaxXY.y;
229 // Check if the last character is a new line character. In that case the height should be added.
230 if( !relayoutData.mTextLayoutInfo.mLinesLayoutInfo.empty() )
232 const TextViewProcessor::LineLayoutInfo& lineLayoutInfo( *( relayoutData.mTextLayoutInfo.mLinesLayoutInfo.end() - 1 ) );
234 if( lineLayoutInfo.mWordGroupsLayoutInfo.empty() ) // if it's empty, it means the last character is a new line character.
236 relayoutData.mTextSizeForRelayoutOption.height += lineLayoutInfo.mSize.height;
243 void Relayout( Actor textView,
244 TextView::RelayoutOperationMask relayoutOperationMask,
245 const TextView::LayoutParameters& layoutParameters,
246 const TextView::VisualParameters& visualParameters,
247 TextView::RelayoutData& relayoutData )
249 if( relayoutOperationMask & TextView::RELAYOUT_SIZE_POSITION )
251 relayoutData.mLineJustificationInfo.clear();
252 CalculateSizeAndPosition( layoutParameters,
255 TextViewRelayout::SetUnderlineInfo( relayoutData );
258 if( relayoutOperationMask & TextView::RELAYOUT_ALIGNMENT )
260 TextViewRelayout::UpdateAlignment( layoutParameters,
264 if( relayoutOperationMask & TextView::RELAYOUT_VISIBILITY )
266 TextViewRelayout::UpdateVisibility( layoutParameters,
271 if( relayoutOperationMask & TextView::RELAYOUT_INITIALIZE_TEXT_ACTORS )
273 TextViewProcessor::InitializeTextActorInfo( relayoutData );
276 if( relayoutOperationMask & TextView::RELAYOUT_TEXT_ACTOR_UPDATE )
278 TextViewRelayout::UpdateTextActorInfo( visualParameters,
282 if( ( relayoutOperationMask & TextView::RELAYOUT_INSERT_TO_TEXT_VIEW ) ||
283 ( relayoutOperationMask & TextView::RELAYOUT_INSERT_TO_TEXT_ACTOR_LIST ) )
285 TextViewRelayout::InsertToTextView( relayoutOperationMask,
291 } // namespace SplitByChar
293 } // namespace Internal
295 } // namespace Toolkit