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