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/internal/event/text/text-impl.h>
23 #include <dali/internal/common/text-array.h>
24 #include <dali/internal/event/text/character-impl.h>
25 #include <dali/internal/event/text/utf8-impl.h>
35 static const TextArray VOID_TEXT_ARRAY; ///< A void text array to be used in the helper Internal::GetTextArray() function.
43 Text::Text( const std::string& text )
46 // minimize allocations for ascii strings
47 mString.reserve( text.size() );
49 // break string into UTF-8 tokens
50 UTF8Tokenize( reinterpret_cast<const unsigned char*>( text.c_str() ), text.size(), mString );
53 Text::Text( const Character& character )
56 mString.push_back( character.GetCharacter() );
59 Text::Text( const Text& text )
60 : mString( text.mString )
64 void Text::GetText( std::string& text ) const
66 // minimize allocations for ascii strings
67 text.reserve( mString.size() );
69 for( TextArray::const_iterator it = mString.begin(), endIt = mString.end(); it != endIt; ++it )
71 unsigned char utf8Data[4];
72 unsigned int utf8Length;
74 utf8Length = UTF8Write( *it, utf8Data );
76 text.append( reinterpret_cast<const char*>( utf8Data ), utf8Length );
80 Text& Text::operator=( const Text& text )
82 mString = text.mString;
97 Dali::Character Text::operator[]( size_t position ) const
99 DALI_ASSERT_ALWAYS( position < mString.size() && "Text::operator[]: Character position is out of bounds" );
101 const uint32_t c = *( mString.begin() + position );
103 Dali::Character character( new Character( c ) );
108 bool Text::IsEmpty() const
110 return mString.empty();
113 size_t Text::GetLength() const
115 return mString.size();
118 void Text::Append( const Dali::Text& text )
120 const TextArray& utfCodes = text.GetImplementation().GetTextArray();
122 mString.insert( mString.end(), utfCodes.begin(), utfCodes.end() );
125 void Text::Remove( size_t position, size_t numberOfCharacters )
127 DALI_ASSERT_ALWAYS( position < mString.size() && "Text::Remove: Character position is out of bounds" );
128 DALI_ASSERT_ALWAYS( position + numberOfCharacters <= mString.size() && "Text::Remove: Character position + numberOfCharacters is out of bounds" );
130 mString.erase( mString.begin() + position, mString.begin() + position + numberOfCharacters );
133 void Text::Find( uint32_t character, std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
135 std::size_t position = from;
137 for( TextArray::const_iterator it = mString.begin() + from, endIt = mString.begin() + to + 1u; it != endIt; ++position, ++it )
139 if( *it == character )
141 positions.PushBack( position );
146 void Text::FindWhiteSpace( std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
148 std::size_t position = from;
150 for( TextArray::const_iterator it = mString.begin() + from, endIt = mString.begin() + to + 1u; it != endIt; ++position, ++it )
152 if( Character::IsWhiteSpace( *it ) )
154 positions.PushBack( position );
159 void Text::FindNewLine( std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
161 std::size_t position = from;
163 for( TextArray::const_iterator it = mString.begin() + from, endIt = mString.begin() + to + 1u; it != endIt; ++position, ++it )
165 if( Character::IsNewLine( *it ) )
167 positions.PushBack( position );
172 void Text::GetSubText( std::size_t from, std::size_t to, Text* subText ) const
176 const std::size_t length = mString.size();
177 const std::size_t rfrom = length - ( from + 1u );
178 const std::size_t rto = length - to;
179 subText->mString.insert( subText->mString.end(), mString.rbegin() + rfrom, mString.rbegin() + rto );
183 subText->mString.insert( subText->mString.end(), mString.begin() + from, mString.begin() + to + 1u );
187 bool Text::IsWhiteSpace( std::size_t index ) const
189 if( index < mString.size() )
191 return Character::IsWhiteSpace( *( mString.begin() + index ) );
197 bool Text::IsNewLine( std::size_t index ) const
199 if( index < mString.size() )
201 return Character::IsNewLine( *( mString.begin() + index ) );
207 const TextArray& Text::GetTextArray() const
212 const TextArray& GetTextArray( const Dali::Text& text )
216 return VOID_TEXT_ARRAY;
219 return text.GetImplementation().GetTextArray();
222 } // namespace Internal