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.
18 #include <dali/internal/event/text/text-impl.h>
22 #include <dali/internal/common/text-array.h>
23 #include <dali/internal/event/text/character-impl.h>
24 #include <dali/internal/event/text/utf8-impl.h>
37 Text::Text( const std::string& text )
42 // minimize allocations for ascii strings
43 mString.reserve( text.size() );
45 // break string into UTF-8 tokens
46 UTF8Tokenize( reinterpret_cast<const unsigned char*>( text.c_str() ), text.size(), mString );
50 Text::Text( const Character& character )
53 mString.push_back( character.GetCharacter() );
56 Text::Text( const Text& text )
57 : mString( text.mString )
61 void Text::GetText( std::string& text ) const
63 // minimize allocations for ascii strings
64 text.reserve( mString.size() );
66 for( TextArray::const_iterator it = mString.begin(), endIt = mString.end(); it != endIt; ++it )
68 unsigned char utf8Data[4];
69 unsigned int utf8Length;
71 utf8Length = UTF8Write( *it, utf8Data );
73 text.append( reinterpret_cast<const char*>( utf8Data ), utf8Length );
77 void Text::SetText( const Text& text )
79 mString = text.mString;
82 Text& Text::operator=( const Text& text )
84 mString = text.mString;
94 Dali::Character Text::operator[]( size_t position ) const
96 DALI_ASSERT_ALWAYS( position < mString.size() && "Text::operator[]: Character position is out of bounds" );
98 const uint32_t c = *( mString.begin() + position );
100 Dali::Character character( new Character( c ) );
105 bool Text::IsEmpty() const
107 return mString.empty();
110 size_t Text::GetLength() const
112 return mString.size();
115 void Text::Append( const Dali::Text& text )
117 const TextArray& utfCodes = text.GetImplementation().GetTextArray();
119 mString.insert( mString.end(), utfCodes.begin(), utfCodes.end() );
122 void Text::Remove( size_t position, size_t numberOfCharacters )
124 if( numberOfCharacters == 0 )
126 DALI_ASSERT_DEBUG( ( numberOfCharacters != 0 ) && ( "Text::Remove: numberOfCharacters is zero." ) );
129 DALI_ASSERT_ALWAYS( position < mString.size() && "Text::Remove: Character position is out of bounds" );
130 DALI_ASSERT_ALWAYS( position + numberOfCharacters <= mString.size() && "Text::Remove: Character position + numberOfCharacters is out of bounds" );
132 mString.erase( mString.begin() + position, mString.begin() + position + numberOfCharacters );
135 void Text::SetTextArray( const TextArray& textArray )
140 void Text::GetTextArray( TextArray& textArray ) const
145 const TextArray& Text::GetTextArray() const
150 } // namespace Internal