Merge "DALi Version 1.2.49" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / segmentation.cpp
1 /*
2  * Copyright (c) 2015 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 // FILE HEADER
19 #include <dali-toolkit/internal/text/segmentation.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/text-abstraction/segmentation.h>
23 #ifdef DEBUG_ENABLED
24 #include <string>
25 #include <dali/integration-api/debug.h>
26 #endif
27
28 // INTERNAL INCLUDES
29 #ifdef DEBUG_ENABLED
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #endif
32
33 namespace
34 {
35
36 #if defined(DEBUG_ENABLED)
37   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_SEGMENTATION");
38 #endif
39
40 } // namespace
41
42
43 namespace Dali
44 {
45
46 namespace Toolkit
47 {
48
49 namespace Text
50 {
51
52 void SetLineBreakInfo( const Vector<Character>& text,
53                        CharacterIndex startIndex,
54                        Length numberOfCharacters,
55                        Vector<LineBreakInfo>& lineBreakInfo )
56 {
57   const Length totalNumberOfCharacters = text.Count();
58
59   if( 0u == totalNumberOfCharacters )
60   {
61     // Nothing to do if there are no characters.
62     return;
63   }
64
65   // Retrieve the line break info.
66   lineBreakInfo.Resize( totalNumberOfCharacters );
67
68   // Whether the current buffer is being updated or is set from scratch.
69   const bool updateCurrentBuffer = numberOfCharacters < totalNumberOfCharacters;
70
71   LineBreakInfo* lineBreakInfoBuffer = NULL;
72   Vector<LineBreakInfo> newLineBreakInfo;
73
74   if( updateCurrentBuffer )
75   {
76     newLineBreakInfo.Resize( numberOfCharacters );
77     lineBreakInfoBuffer = newLineBreakInfo.Begin();
78   }
79   else
80   {
81     lineBreakInfoBuffer = lineBreakInfo.Begin();
82   }
83
84   // Retrieve the line break info.
85   TextAbstraction::Segmentation::Get().GetLineBreakPositions( text.Begin() + startIndex,
86                                                               numberOfCharacters,
87                                                               lineBreakInfoBuffer );
88
89   // If the line break info is updated, it needs to be inserted in the model.
90   if( updateCurrentBuffer )
91   {
92     lineBreakInfo.Insert( lineBreakInfo.Begin() + startIndex,
93                           newLineBreakInfo.Begin(),
94                           newLineBreakInfo.End() );
95     lineBreakInfo.Resize( totalNumberOfCharacters );
96   }
97
98 #ifdef DEBUG_ENABLED
99   if( gLogFilter->IsEnabledFor(Debug::Verbose) )
100   {
101     std::string utf8;
102     Utf32ToUtf8( text.Begin(), numberOfCharacters, utf8 );
103
104     std::string info;
105     info.reserve( numberOfCharacters );
106     for( unsigned int i=0; i<lineBreakInfo.Count(); ++i )
107     {
108       info.push_back( static_cast<char>('0' + lineBreakInfo[i]) );
109     }
110
111     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SetLineBreakInfo Characters: %s\n", utf8.c_str() );
112     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SetLineBreakInfo Break info: %s\n", info.c_str() );
113   }
114 #endif
115 }
116
117 void SetWordBreakInfo( const Vector<Character>& text,
118                        CharacterIndex startIndex,
119                        Length numberOfCharacters,
120                        Vector<WordBreakInfo>& wordBreakInfo )
121 {
122   const Length totalNumberOfCharacters = text.Count();
123
124   if( 0u == totalNumberOfCharacters )
125   {
126     // Nothing to do if there are no characters.
127     return;
128   }
129
130   // Resize the vector.
131   wordBreakInfo.Resize( totalNumberOfCharacters );
132
133   // Whether the current buffer is being updated or is set from scratch.
134   const bool updateCurrentBuffer = numberOfCharacters < totalNumberOfCharacters;
135
136   WordBreakInfo* wordBreakInfoBuffer = NULL;
137   Vector<WordBreakInfo> newWordBreakInfo;
138
139   if( updateCurrentBuffer )
140   {
141     newWordBreakInfo.Resize( numberOfCharacters );
142     wordBreakInfoBuffer = newWordBreakInfo.Begin();
143   }
144   else
145   {
146     wordBreakInfoBuffer = wordBreakInfo.Begin();
147   }
148
149   // Retrieve the word break info.
150   TextAbstraction::Segmentation::Get().GetWordBreakPositions( text.Begin() + startIndex,
151                                                               numberOfCharacters,
152                                                               wordBreakInfoBuffer );
153
154   // If the word break info is updated, it needs to be inserted in the model.
155   if( updateCurrentBuffer )
156   {
157     wordBreakInfo.Insert( wordBreakInfo.Begin() + startIndex,
158                           newWordBreakInfo.Begin(),
159                           newWordBreakInfo.End() );
160     wordBreakInfo.Resize( totalNumberOfCharacters );
161   }
162
163 #ifdef DEBUG_ENABLED
164   if( gLogFilter->IsEnabledFor(Debug::Verbose) )
165   {
166     std::string utf8;
167     Utf32ToUtf8( text.Begin(), totalNumberOfCharacters, utf8 );
168
169     std::string info;
170     info.reserve( totalNumberOfCharacters );
171     for( unsigned int i=0; i<wordBreakInfo.Count(); ++i )
172     {
173       info.push_back( static_cast<char>('0' + wordBreakInfo[i]) );
174     }
175
176     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SetWordBreakInfo Characters: %s\n", utf8.c_str() );
177     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SetWordBreakInfo Break info: %s\n", info.c_str() );
178   }
179 #endif
180 }
181
182 } // namespace Text
183
184 } // namespace Toolkit
185
186 } // namespace Dali