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-processor.h>
30 namespace TextProcessor
33 void SplitInParagraphs( const MarkupProcessor::StyledTextArray& styledTextArray,
34 std::vector<Text>& paragraphs,
35 std::vector< Vector<TextStyle*> >& styles )
37 // Stores the text for a paragraph.
40 // Stores the styles for each character of the paragraph.
41 Vector<TextStyle*> stylesForParagraph;
43 // Traverses all styled texts of the array.
44 for( MarkupProcessor::StyledTextArray::const_iterator it = styledTextArray.begin(), endIt = styledTextArray.end(); it != endIt; ++it )
46 const MarkupProcessor::StyledText& styledText( *it );
48 // Traverses all the characters of the styled text (It may have more than one).
49 for( size_t i = 0u, length = styledText.mText.GetLength(); i < length; ++i )
51 const Dali::Character character = styledText.mText[i];
53 if( character.IsNewLine() ) // LF
55 // The character is a new paragraph character.
57 // Append the new paragraph character.
58 paragraph.Append( character );
60 // Creates a new text style for the character and insert it to the vector of styles for that paragraph.
61 TextStyle* style = new TextStyle( styledText.mStyle );
62 stylesForParagraph.PushBack( style );
64 // Inserts the paragraph and the styles to the vector of paragraphs and the vector of styles.
65 paragraphs.push_back( paragraph );
66 styles.push_back( stylesForParagraph );
68 // Clears the text and the vector of styles for the next paragraph.
70 stylesForParagraph.Clear();
74 // The character is not a new paragraph character.
76 // Append it to the paragraph's text
77 paragraph.Append( character );
79 // Add the style to the vector of styles for that paragraph.
80 TextStyle* style = new TextStyle( styledText.mStyle );
81 stylesForParagraph.PushBack( style );
86 // This paragraph could be empty if the last character of the previous paragraph is a 'new paragraph' character
87 // and is the last of the text.
88 paragraphs.push_back( paragraph );
89 styles.push_back( stylesForParagraph );
92 void SplitInWords( const Dali::Text& paragraph,
93 Vector<std::size_t>& positions )
95 const std::size_t length = paragraph.GetLength();
97 // Magic number: Let's soupose there is ~6 characters per word. Used to do less memory reallocation inside Vector.
98 const size_t magicNumberOfWords = ( length / 6u ) + 1u;
100 // Find the positions of the new paragraph characters.
101 positions.Reserve( magicNumberOfWords );
103 // Find the position of all white spaces. A new paragraph character is also considered a white space but it doesn't matter at this point.
104 paragraph.Find( Text::WHITE_SPACE, 0u, length - 1u, positions );
108 * Wheather the character of the text pointed by the given offset is a white space.
110 * @param[in] text The text.
111 * @param[in] offset Offset pointing the character.
113 * @return \e true if the character pointed by the offset is a white space.
115 bool IsWhiteSpace( const MarkupProcessor::StyledTextArray& text, size_t offset )
117 DALI_ASSERT_DEBUG( offset < text.size() );
119 // assume 1 Character per StyledText
120 return ( *( text.begin() + offset ) ).mText[0u].IsWhiteSpace();
123 void FindNearestWord( const MarkupProcessor::StyledTextArray& text, size_t offset, size_t& start, size_t& end)
125 const size_t size(text.size());
126 offset = std::min(offset, size-1u);
131 // if currently looking at whitespace, then search left and right for non-whitespace.
132 if(IsWhiteSpace(text, offset))
134 // scan left until non-white space / beginning of string.
135 while(i > 0u && IsWhiteSpace(text, i))
140 // scan right until non-white space / end of string.
141 while(j < size && IsWhiteSpace(text, j))
147 // check if r.h.s. word is closer than l.h.s. word
148 if( (j - offset) < // distance to closest right word <
149 (offset - i) ) // distance to closest left word
151 // point left and right markers on start of right word
156 // point left and right markers on end of left word
160 // expand left and right markers to encompase entire word
161 while(i > 0u && !IsWhiteSpace(text, i-1u))
166 while(j < size && !IsWhiteSpace(text, j))
171 // If both markers at same position and is whitespace then word is a whitespace word
174 while(j < size && IsWhiteSpace(text, j))
179 while(i > 0 && IsWhiteSpace(text, i-1))
189 } // namespace TextProcessor
191 } // namespace Internal
193 } // namespace DaliToolkit