License conversion from Flora to Apache 2.0
[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 Text::Text()
34 : mString()
35 {
36 }
37
38 Text::Text( const std::string& text )
39 : mString()
40 {
41   if( !text.empty() )
42   {
43     // minimize allocations for ascii strings
44     mString.reserve( text.size() );
45
46     // break string into UTF-8 tokens
47     UTF8Tokenize( reinterpret_cast<const unsigned char*>( text.c_str() ), text.size(), mString );
48   }
49 }
50
51 Text::Text( const Character& character )
52 : mString()
53 {
54   mString.push_back( character.GetCharacter() );
55 }
56
57 Text::Text( const Text& text )
58 : mString( text.mString )
59 {
60 }
61
62 void Text::GetText( std::string& text ) const
63 {
64   // minimize allocations for ascii strings
65   text.reserve( mString.size() );
66
67   for( TextArray::const_iterator it = mString.begin(), endIt = mString.end(); it != endIt; ++it )
68   {
69     unsigned char utf8Data[4];
70     unsigned int utf8Length;
71
72     utf8Length = UTF8Write( *it, utf8Data );
73
74     text.append( reinterpret_cast<const char*>( utf8Data ), utf8Length );
75   }
76 }
77
78 void Text::SetText( const Text& text )
79 {
80   mString = text.mString;
81 }
82
83 Text& Text::operator=( const Text& text )
84 {
85   mString = text.mString;
86
87   return *this;
88 }
89
90 Text::~Text()
91 {
92   mString.clear();
93 }
94
95 Dali::Character Text::operator[]( size_t position ) const
96 {
97   DALI_ASSERT_ALWAYS( position < mString.size() && "Text::operator[]: Character position is out of bounds" );
98
99   const uint32_t c = *( mString.begin() + position );
100
101   Dali::Character character( new Character( c ) );
102
103   return character;
104 }
105
106 bool Text::IsEmpty() const
107 {
108   return mString.empty();
109 }
110
111 size_t Text::GetLength() const
112 {
113   return mString.size();
114 }
115
116 void Text::Append( const Dali::Text& text )
117 {
118   const TextArray& utfCodes = text.GetImplementation().GetTextArray();
119
120   mString.insert( mString.end(), utfCodes.begin(), utfCodes.end() );
121 }
122
123 void Text::Remove( size_t position, size_t numberOfCharacters )
124 {
125   if( numberOfCharacters == 0 )
126   {
127     DALI_ASSERT_DEBUG( ( numberOfCharacters != 0 ) && ( "Text::Remove: numberOfCharacters is zero." ) );
128     return;
129   }
130   DALI_ASSERT_ALWAYS( position < mString.size() && "Text::Remove: Character position is out of bounds" );
131   DALI_ASSERT_ALWAYS( position + numberOfCharacters <= mString.size() && "Text::Remove: Character position + numberOfCharacters is out of bounds" );
132
133   mString.erase( mString.begin() + position, mString.begin() + position + numberOfCharacters );
134 }
135
136 void Text::SetTextArray( const TextArray& textArray )
137 {
138   mString = textArray;
139 }
140
141 void Text::GetTextArray( TextArray& textArray ) const
142 {
143   textArray = mString;
144 }
145
146 const TextArray& Text::GetTextArray() const
147 {
148   return mString;
149 }
150
151 } // namespace Internal
152
153 } // namespace Dali