2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 <dali-toolkit/internal/controls/text-view/text-view-word-processor.h>
22 #include <dali-toolkit/internal/controls/text-view/text-view-processor-helper-functions.h>
23 #include <dali-toolkit/internal/controls/text-view/text-view-processor-dbg.h>
34 namespace TextViewProcessor
40 const std::string EMOJI_FONT_NAME( "SamsungEmoji" ); // Emoticons font family name.
43 * Updates the word size and ascender.
45 * It's called after deleting some characters.
47 * @param[in] wordLayout The word layout info.
49 void UpdateLayoutInfo( WordLayoutInfo& wordLayout )
51 // Initialize layout info for the whole word.
52 wordLayout.mSize = Size();
53 wordLayout.mAscender = 0.f;
55 // Traverse the character layout info to update the word layout.
56 for( CharacterLayoutInfoContainer::iterator layoutIt = wordLayout.mCharactersLayoutInfo.begin(), layoutEndIt = wordLayout.mCharactersLayoutInfo.end();
57 layoutIt != layoutEndIt;
60 // Layout info for the current character.
61 CharacterLayoutInfo& layoutInfo( *layoutIt );
63 // Update layout info for the current word.
64 UpdateSize( wordLayout.mSize, layoutInfo.mSize );
65 wordLayout.mAscender = std::max( wordLayout.mAscender, layoutInfo.mAscender );
75 WordLayoutInfo::WordLayoutInfo()
79 mCharactersLayoutInfo()
83 WordLayoutInfo::WordLayoutInfo( const WordLayoutInfo& word )
84 : mSize( word.mSize ),
85 mAscender( word.mAscender ),
87 mCharactersLayoutInfo( word.mCharactersLayoutInfo )
91 WordLayoutInfo& WordLayoutInfo::operator=( const WordLayoutInfo& word )
94 mAscender = word.mAscender;
96 mCharactersLayoutInfo = word.mCharactersLayoutInfo;
101 void CreateWordTextInfo( const MarkupProcessor::StyledTextArray& word,
102 WordLayoutInfo& wordLayoutInfo )
104 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "-->TextViewProcessor::CreateWordTextInfo\n" );
105 // Split in characters.
106 for( MarkupProcessor::StyledTextArray::const_iterator charIt = word.begin(), charEndIt = word.end(); charIt != charEndIt; ++charIt )
108 const MarkupProcessor::StyledText& styledText( *charIt );
110 const std::size_t length = styledText.mText.GetLength();
112 // It could be a group of characters.
113 for( std::size_t index = 0; index < length; ++index )
115 MarkupProcessor::StyledText styledCharacter;
116 styledCharacter.mStyle = styledText.mStyle;
117 Character character = styledText.mText[index];
118 styledCharacter.mText.Append( character );
120 // Create layout character info.
121 CharacterLayoutInfo characterLayoutInfo;
123 characterLayoutInfo.mIsColorGlyph = GlyphImage::IsColorGlyph( character );
124 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, " Is color glyph: %s\n", ( characterLayoutInfo.mIsColorGlyph ? "True" : "False" ) );
126 if( characterLayoutInfo.mIsColorGlyph )
128 styledCharacter.mStyle.SetFontName( EMOJI_FONT_NAME );
132 //Choose the right font for the given character and style.
133 ChooseFontFamilyName( styledCharacter );
136 const Font font = Font::New( FontParameters( styledCharacter.mStyle.GetFontName(), styledCharacter.mStyle.GetFontStyle(), styledCharacter.mStyle.GetFontPointSize() ) );
137 const Font::Metrics metrics = font.GetMetrics( character );
138 const float ascender = font.GetAscender();
140 // Fill Natural size info for current character.
141 characterLayoutInfo.mHeight = font.GetLineHeight();
142 characterLayoutInfo.mAdvance = metrics.GetAdvance();
143 characterLayoutInfo.mBearing = metrics.GetBearing();
145 if( character.IsNewLine() && !characterLayoutInfo.mIsColorGlyph )
147 // A new line character doesn't have any width.
148 characterLayoutInfo.mSize.width = 0.f;
152 // Uses advance as width.
153 characterLayoutInfo.mSize.width = characterLayoutInfo.mAdvance;
155 characterLayoutInfo.mSize.height = characterLayoutInfo.mHeight;
156 characterLayoutInfo.mAscender = ascender;
158 if( styledCharacter.mStyle.IsUnderlineEnabled() )
160 characterLayoutInfo.mUnderlineThickness = font.GetUnderlineThickness(); // Both thickness and position includes the
161 characterLayoutInfo.mUnderlinePosition = font.GetUnderlinePosition(); // vertical pad adjust used in effects like glow or shadow.
164 // stores the styled text.
165 characterLayoutInfo.mStyledText.mText = styledCharacter.mText;
166 characterLayoutInfo.mStyledText.mStyle = styledCharacter.mStyle;
168 // Add character layout info to the word layout info and update it.
169 wordLayoutInfo.mCharactersLayoutInfo.push_back( characterLayoutInfo );
170 UpdateSize( wordLayoutInfo.mSize, characterLayoutInfo.mSize );
171 wordLayoutInfo.mAscender = std::max( wordLayoutInfo.mAscender, characterLayoutInfo.mAscender );
172 wordLayoutInfo.mType = GetTextSeparatorType( character );
173 } // end of each character in the group of characters.
174 } // end of characters in the word.
175 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "<--TextViewProcessor::CreateWordTextInfo\n" );
178 void RemoveCharactersFromWordInfo( TextView::RelayoutData& relayoutData,
179 const std::size_t numberOfCharacters,
182 TextInfoIndices& textInfoIndicesBegin,
183 TextInfoIndices& textInfoIndicesEnd,
184 TextInfoIndices& textInfoMergeIndicesBegin,
185 TextInfoIndices& textInfoMergeIndicesEnd,
186 LineLayoutInfo& lineLayout,
187 std::vector<TextActor>& removedTextActors )
189 const TextLayoutInfo& textLayoutInfo = relayoutData.mTextLayoutInfo;
192 WordLayoutInfo& wordLayout( *( lineLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex ) );
194 if( LineSeparator == wordLayout.mType )
196 // If the word is a line separator and there is more lines, then current line and the line after need to be merged.
197 if( textInfoIndicesBegin.mLineIndex + 1 < textLayoutInfo.mLinesLayoutInfo.size() )
199 // current line is not the last one.
201 // Update indices to merge lines.
202 textInfoMergeIndicesBegin.mLineIndex = textInfoIndicesBegin.mLineIndex;
203 textInfoMergeIndicesEnd.mLineIndex = textInfoIndicesBegin.mLineIndex + 1;
207 ++textInfoIndicesBegin.mLineIndex; // increase both indices,
208 textInfoIndicesEnd.mLineIndex +=2; // will delete last line.
211 ++textInfoIndicesEnd.mWordIndex; //will delete the line separator;
213 else if( WordSeparator == wordLayout.mType )
215 // If the word is a word separator. Check if the word before and the word after can be merged.
217 if( ( 0 < textInfoIndicesBegin.mWordIndex ) && ( lineLayout.mWordsLayoutInfo.size() > textInfoIndicesBegin.mWordIndex + 1 ) )
219 const WordLayoutInfo& wordLayoutBefore( *( lineLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex - 1 ) );
220 const WordLayoutInfo& wordLayoutAfter( *( lineLayout.mWordsLayoutInfo.begin() + textInfoIndicesBegin.mWordIndex + 1 ) );
222 if( ( NoSeparator == wordLayoutBefore.mType ) && ( NoSeparator == wordLayoutAfter.mType ) )
224 // This word is a word separator (white space) and is not the first word of the line nor the last one.
227 // Set indices to merge the words.
228 textInfoMergeIndicesBegin.mWordIndex = textInfoIndicesBegin.mWordIndex - 1; // word before word separator.
229 textInfoMergeIndicesEnd.mWordIndex = textInfoIndicesBegin.mWordIndex + 1; // word after word separator.
231 textInfoIndicesEnd.mWordIndex += 2; // will delete the word separator and the merged word.
235 ++textInfoIndicesEnd.mWordIndex; // will delete the word separator;
240 ++textInfoIndicesEnd.mWordIndex; // will delete the word separator;
243 else if( numberOfCharacters == wordLayout.mCharactersLayoutInfo.size() )
245 // The whole word needs to be removed.
246 ++textInfoIndicesEnd.mWordIndex; // will delete the current word.
250 // Store text-actors before removing them.
251 CollectTextActors( removedTextActors, wordLayout, textInfoIndicesBegin.mCharacterIndex, textInfoIndicesBegin.mCharacterIndex + numberOfCharacters );
253 // just remove some characters from current word.
254 RemoveCharactersFromWord( textInfoIndicesBegin.mCharacterIndex,
260 void RemoveCharactersFromWord( const std::size_t position,
261 const std::size_t numberOfCharacters,
262 WordLayoutInfo& wordLayout )
264 // Removes a given number of characters from the given word starting from the 'position' index.
267 if( 0 == numberOfCharacters )
269 // nothing to do if the number of characters is zero.
274 // Remove characters from layout and text-actor info.
275 wordLayout.mCharactersLayoutInfo.erase( wordLayout.mCharactersLayoutInfo.begin() + position, wordLayout.mCharactersLayoutInfo.begin() + position + numberOfCharacters );
277 // Some characters have been removed from the word. Update the layout info is needed.
278 UpdateLayoutInfo( wordLayout );
281 void SplitWord( const std::size_t position,
282 WordLayoutInfo& firstWordLayoutInfo,
283 WordLayoutInfo& lastWordLayoutInfo )
285 // Splits a word in two.
286 // It moves characters from the first part of the word to the last one.
291 // the whole word goes to the last part of the word.
292 lastWordLayoutInfo = firstWordLayoutInfo;
294 firstWordLayoutInfo = WordLayoutInfo();
299 if( position == firstWordLayoutInfo.mCharactersLayoutInfo.size() )
301 // the whole word goes to the first part of the word.
303 // Just delete whatever there is in the last part of the word.
304 lastWordLayoutInfo = WordLayoutInfo();
309 // Initialize output data structures.
312 lastWordLayoutInfo = WordLayoutInfo();
314 // Split layout info.
316 // Insert characters from the given index 'position' to the end.
317 lastWordLayoutInfo.mCharactersLayoutInfo.insert( lastWordLayoutInfo.mCharactersLayoutInfo.end(),
318 firstWordLayoutInfo.mCharactersLayoutInfo.begin() + position, firstWordLayoutInfo.mCharactersLayoutInfo.end() );
320 // Delete characters from the first part of the word.
321 firstWordLayoutInfo.mCharactersLayoutInfo.erase( firstWordLayoutInfo.mCharactersLayoutInfo.begin() + position, firstWordLayoutInfo.mCharactersLayoutInfo.end() );
323 // Update the layout info of both new words.
324 UpdateLayoutInfo( firstWordLayoutInfo );
325 UpdateLayoutInfo( lastWordLayoutInfo );
328 void MergeWord( WordLayoutInfo& firstWordLayoutInfo,
329 const WordLayoutInfo& lastWordLayoutInfo )
331 // Merges two given words.
334 if( lastWordLayoutInfo.mCharactersLayoutInfo.empty() )
340 if( firstWordLayoutInfo.mCharactersLayoutInfo.empty() )
342 // copy last to first
344 firstWordLayoutInfo = lastWordLayoutInfo;
349 if( ( NoSeparator != firstWordLayoutInfo.mType ) || ( NoSeparator != lastWordLayoutInfo.mType ) )
351 // Do not merge white spaces or new line characters.
352 DALI_ASSERT_ALWAYS( !"TextViewProcessor::MergeWord(). ERROR: White spaces or new line characters can't be merged with other words." );
356 firstWordLayoutInfo.mCharactersLayoutInfo.insert( firstWordLayoutInfo.mCharactersLayoutInfo.end(),
357 lastWordLayoutInfo.mCharactersLayoutInfo.begin(),
358 lastWordLayoutInfo.mCharactersLayoutInfo.end() );
360 // Update the word layout info.
361 UpdateSize( firstWordLayoutInfo.mSize, lastWordLayoutInfo.mSize );
362 firstWordLayoutInfo.mAscender = std::max( firstWordLayoutInfo.mAscender, lastWordLayoutInfo.mAscender );
365 CharacterLayoutInfo GetFirstCharacterLayoutInfo( const WordLayoutInfo& wordLayoutInfo )
367 CharacterLayoutInfo layoutInfo;
369 if( !wordLayoutInfo.mCharactersLayoutInfo.empty() )
371 layoutInfo = *wordLayoutInfo.mCharactersLayoutInfo.begin();
377 CharacterLayoutInfo GetLastCharacterLayoutInfo( const WordLayoutInfo& wordLayoutInfo )
379 CharacterLayoutInfo layoutInfo;
381 if( !wordLayoutInfo.mCharactersLayoutInfo.empty() )
383 layoutInfo = *( wordLayoutInfo.mCharactersLayoutInfo.end() - 1 );
389 void CollectTextActors( std::vector<TextActor>& textActors, const WordLayoutInfo& word, const std::size_t characterIndexBegin, const std::size_t characterIndexEnd )
391 for( CharacterLayoutInfoContainer::const_iterator characterIt = word.mCharactersLayoutInfo.begin() + characterIndexBegin,
392 characterEndIt = word.mCharactersLayoutInfo.begin() + characterIndexEnd;
393 characterIt != characterEndIt;
396 const CharacterLayoutInfo& characterLayout( *characterIt );
398 if( !characterLayout.mIsColorGlyph )
400 TextActor textActor = TextActor::DownCast( characterLayout.mGlyphActor );
403 textActors.push_back( textActor );
409 void CollectTextActorsFromWords( std::vector<TextActor>& textActors, const LineLayoutInfo& line, const std::size_t wordIndexBegin, const std::size_t wordIndexEnd )
411 for( WordLayoutInfoContainer::const_iterator wordIt = line.mWordsLayoutInfo.begin() + wordIndexBegin, wordEndIt = line.mWordsLayoutInfo.begin() + wordIndexEnd;
415 const WordLayoutInfo& word( *wordIt );
417 for( CharacterLayoutInfoContainer::const_iterator characterIt = word.mCharactersLayoutInfo.begin(), characterEndIt = word.mCharactersLayoutInfo.end();
418 characterIt != characterEndIt;
421 const CharacterLayoutInfo& characterLayout( *characterIt );
423 if( !characterLayout.mIsColorGlyph )
425 TextActor textActor = TextActor::DownCast( characterLayout.mGlyphActor );
428 textActors.push_back( textActor );
435 } //namespace TextViewProcessor
437 } //namespace Internal
439 } //namespace Toolkit