TextActor - New constructor with a parameter object replaces previous constructors.
[platform/core/uifw/dali-core.git] / dali / internal / event / text / text-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // HEADER CLASS
19 #include <dali/internal/event/text/text-impl.h>
20
21 // INTERNAL INCLUDES
22
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>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace
34 {
35 static const TextArray VOID_TEXT_ARRAY; ///< A void text array to be used in the helper Internal::GetTextArray() function.
36 } // namespace
37
38 Text::Text()
39 : mString()
40 {
41 }
42
43 Text::Text( const std::string& text )
44 : mString()
45 {
46   // minimize allocations for ascii strings
47   mString.reserve( text.size() );
48
49   // break string into UTF-8 tokens
50   UTF8Tokenize( reinterpret_cast<const unsigned char*>( text.c_str() ), text.size(), mString );
51 }
52
53 Text::Text( const Character& character )
54 : mString()
55 {
56   mString.push_back( character.GetCharacter() );
57 }
58
59 Text::Text( const Text& text )
60 : mString( text.mString )
61 {
62 }
63
64 void Text::GetText( std::string& text ) const
65 {
66   // minimize allocations for ascii strings
67   text.reserve( mString.size() );
68
69   for( TextArray::const_iterator it = mString.begin(), endIt = mString.end(); it != endIt; ++it )
70   {
71     unsigned char utf8Data[4];
72     unsigned int utf8Length;
73
74     utf8Length = UTF8Write( *it, utf8Data );
75
76     text.append( reinterpret_cast<const char*>( utf8Data ), utf8Length );
77   }
78 }
79
80 Text& Text::operator=( const Text& text )
81 {
82   mString = text.mString;
83
84   return *this;
85 }
86
87 Text::~Text()
88 {
89   Clear();
90 }
91
92 void Text::Clear()
93 {
94   mString.clear();
95 }
96
97 Dali::Character Text::operator[]( size_t position ) const
98 {
99   DALI_ASSERT_ALWAYS( position < mString.size() && "Text::operator[]: Character position is out of bounds" );
100
101   const uint32_t c = *( mString.begin() + position );
102
103   Dali::Character character( new Character( c ) );
104
105   return character;
106 }
107
108 bool Text::IsEmpty() const
109 {
110   return mString.empty();
111 }
112
113 size_t Text::GetLength() const
114 {
115   return mString.size();
116 }
117
118 void Text::Append( const Dali::Text& text )
119 {
120   const TextArray& utfCodes = text.GetImplementation().GetTextArray();
121
122   mString.insert( mString.end(), utfCodes.begin(), utfCodes.end() );
123 }
124
125 void Text::Remove( size_t position, size_t numberOfCharacters )
126 {
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" );
129
130   mString.erase( mString.begin() + position, mString.begin() + position + numberOfCharacters );
131 }
132
133 void Text::Find( uint32_t character, std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
134 {
135   std::size_t position = from;
136
137   for( TextArray::const_iterator it = mString.begin() + from, endIt = mString.begin() + to + 1u; it != endIt; ++position, ++it )
138   {
139     if( *it == character )
140     {
141       positions.PushBack( position );
142     }
143   }
144 }
145
146 void Text::FindWhiteSpace( std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
147 {
148   std::size_t position = from;
149
150   for( TextArray::const_iterator it = mString.begin() + from, endIt = mString.begin() + to + 1u; it != endIt; ++position, ++it )
151   {
152     if( Character::IsWhiteSpace( *it ) )
153     {
154       positions.PushBack( position );
155     }
156   }
157 }
158
159 void Text::FindNewLine( std::size_t from, std::size_t to, Vector<std::size_t>& positions ) const
160 {
161   std::size_t position = from;
162
163   for( TextArray::const_iterator it = mString.begin() + from, endIt = mString.begin() + to + 1u; it != endIt; ++position, ++it )
164   {
165     if( Character::IsNewLine( *it ) )
166     {
167       positions.PushBack( position );
168     }
169   }
170 }
171
172 void Text::GetSubText( std::size_t from, std::size_t to, Text* subText ) const
173 {
174   if( to < from )
175   {
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 );
180   }
181   else
182   {
183     subText->mString.insert( subText->mString.end(), mString.begin() + from, mString.begin() + to + 1u );
184   }
185 }
186
187 bool Text::IsWhiteSpace( std::size_t index ) const
188 {
189   if( index < mString.size() )
190   {
191     return Character::IsWhiteSpace( *( mString.begin() + index ) );
192   }
193
194   return false;
195 }
196
197 bool Text::IsNewLine( std::size_t index ) const
198 {
199   if( index < mString.size() )
200   {
201     return Character::IsNewLine( *( mString.begin() + index ) );
202   }
203
204   return false;
205 }
206
207 const TextArray& Text::GetTextArray() const
208 {
209   return mString;
210 }
211
212 const TextArray& GetTextArray( const Dali::Text& text )
213 {
214   if( text.IsEmpty() )
215   {
216     return VOID_TEXT_ARRAY;
217   }
218
219   return text.GetImplementation().GetTextArray();
220 }
221
222 } // namespace Internal
223
224 } // namespace Dali