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>
22 #include <dali/internal/event/text/character-impl.h>
23 #include <dali/internal/event/text/utf8-impl.h>
36 static const Integration::TextArray VOID_TEXT_ARRAY; ///< A void text array to be used in the helper Internal::GetTextArray() function.
44 Text::Text( const std::string& text )
47 const std::size_t length = text.size();
49 // minimize allocations for ascii strings
50 mString.Reserve( length );
52 // break string into UTF-8 tokens
53 UTF8Tokenize( reinterpret_cast<const unsigned char*>( text.c_str() ), length, mString );
56 Text::Text( const Character& character )
59 mString.PushBack( character.GetCharacter() );
62 Text::Text( const Text& text )
63 : mString( text.mString )
67 void Text::GetText( std::string& text ) const
69 // minimize allocations for ascii strings
70 text.reserve( mString.Count() );
72 for( Integration::TextArray::ConstIterator it = mString.Begin(), endIt = mString.End(); it != endIt; ++it )
74 unsigned char utf8Data[4];
75 unsigned int utf8Length;
77 utf8Length = UTF8Write( *it, utf8Data );
79 text.append( reinterpret_cast<const char*>( utf8Data ), utf8Length );
83 Text& Text::operator=( const Text& text )
85 mString = text.mString;
101 Dali::Character Text::operator[]( size_t position ) const
103 DALI_ASSERT_ALWAYS( position < mString.Count() && "Text::operator[]: Character position is out of bounds" );
105 const uint32_t c = *( mString.Begin() + position );
107 Dali::Character character( new Character( c ) );
112 bool Text::IsEmpty() const
114 return 0u == mString.Count();
117 size_t Text::GetLength() const
119 return mString.Count();
122 void Text::Append( const Dali::Text& text )
124 const Integration::TextArray& utfCodes = text.GetImplementation().GetTextArray();
126 mString.Insert( mString.End(), utfCodes.Begin(), utfCodes.End() );
129 void Text::Remove( size_t position, size_t numberOfCharacters )
131 DALI_ASSERT_ALWAYS( position < mString.Count() && "Text::Remove: Character position is out of bounds" );
132 DALI_ASSERT_ALWAYS( position + numberOfCharacters <= mString.Count() && "Text::Remove: Character position + numberOfCharacters is out of bounds" );
134 mString.Erase( mString.Begin() + position, mString.Begin() + position + numberOfCharacters );
137 void Text::Find( uint32_t character, std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
139 std::size_t position = from;
141 for( Integration::TextArray::ConstIterator it = mString.Begin() + from, endIt = mString.Begin() + to + 1u; it != endIt; ++position, ++it )
143 if( *it == character )
145 positions.PushBack( position );
150 void Text::FindWhiteSpace( std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
152 std::size_t position = from;
154 for( Integration::TextArray::ConstIterator it = mString.Begin() + from, endIt = mString.Begin() + to + 1u; it != endIt; ++position, ++it )
156 if( Character::IsWhiteSpace( *it ) )
158 positions.PushBack( position );
163 void Text::FindNewLine( std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
165 std::size_t position = from;
167 for( Integration::TextArray::ConstIterator it = mString.Begin() + from, endIt = mString.Begin() + to + 1u; it != endIt; ++position, ++it )
169 if( Character::IsNewLine( *it ) )
171 positions.PushBack( position );
176 void Text::GetSubText( std::size_t from, std::size_t to, Text* subText ) const
180 std::swap( from, to );
181 subText->mString.Insert( subText->mString.End(), mString.Begin() + from, mString.Begin() + to + 1u );
182 std::reverse( subText->mString.Begin(), subText->mString.End() );
186 subText->mString.Insert( subText->mString.End(), mString.Begin() + from, mString.Begin() + to + 1u );
190 bool Text::IsWhiteSpace( std::size_t index ) const
192 if( index < mString.Count() )
194 return Character::IsWhiteSpace( *( mString.Begin() + index ) );
200 bool Text::IsNewLine( std::size_t index ) const
202 if( index < mString.Count() )
204 return Character::IsNewLine( *( mString.Begin() + index ) );
210 const Integration::TextArray& Text::GetTextArray() const
215 const Integration::TextArray& GetTextArray( const Dali::Text& text )
219 return VOID_TEXT_ARRAY;
222 return text.GetImplementation().GetTextArray();
225 } // namespace Internal