Add 'ExclusiveArch: armv7l' limit build to arm architecture
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-view / text-view-processor-helper-functions.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 // INTERNAL INCLUDES
18 #include "text-view-processor-helper-functions.h"
19 #include "text-view-processor-dbg.h"
20
21 namespace Dali
22 {
23
24 namespace Toolkit
25 {
26
27 namespace Internal
28 {
29
30 namespace TextViewProcessor
31 {
32
33 void UpdateSize( Size& size1, const Size& size2, const SizeGrowType type )
34 {
35   switch( type )
36   {
37     case GrowWidth:
38     {
39       size1.width += size2.width;
40       size1.height = std::max( size1.height, size2.height );
41       break;
42     }
43     case GrowHeight:
44     {
45       size1.width = std::max( size1.width, size2.width );
46       size1.height += size2.height;
47       break;
48     }
49   }
50 }
51
52 TextSeparatorType GetTextSeparatorType( const Character& character )
53 {
54   // returns if the given character is a line separator '\n', a word separator ' ' or if is not a separator (any other character).
55   return ( character.IsNewLine() ? LineSeparator : ( character.IsWhiteSpace() ? WordSeparator : NoSeparator ) );
56 }
57
58 void ChooseFontFamilyName( MarkupProcessor::StyledText& text )
59 {
60   DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "-->TextViewProcessor::ChooseFontFamilyName\n" );
61   DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "   input font name: [%s]\n", text.mStyle.GetFontName().c_str() );
62
63   bool userDefinedFontFamilyName = false;
64
65   // First check if there is a font defined in the style and it supports the given text.
66   if( !text.mStyle.GetFontName().empty() )
67   {
68     const FontParameters fontParams( text.mStyle.GetFontName(), text.mStyle.GetFontStyle() , text.mStyle.GetFontPointSize() );
69     const Font font = Font::New( fontParams );
70
71     if( !font.IsDefaultSystemFont() && font.AllGlyphsSupported( text.mText ) )
72     {
73       userDefinedFontFamilyName = true;
74     }
75   }
76
77   if( !userDefinedFontFamilyName )
78   {
79     const Font defaultSystemFont = Font::New();
80
81     // At this point no font is set or doesn't support the given text.
82     if( !defaultSystemFont.AllGlyphsSupported( text.mText ) )
83     {
84       // If the default system font doesn't support the given text,
85       // an appropiate font is selected.
86       text.mStyle.SetFontName( Font::GetFamilyForText( text.mText ) );
87       // @TODO Font::GetFamilyForText() should return font family and font style.
88     }
89     else
90     {
91       // All characters are supported with default font, so use it
92       text.mStyle.SetFontName( "" );
93     }
94   }
95   DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "  output font name: [%s]\n", text.mStyle.GetFontName().c_str() );
96   DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "<--TextViewProcessor::ChooseFontFamilyName\n" );
97 }
98
99 void GetIndicesFromGlobalCharacterIndex( const std::size_t index,
100                                          const TextLayoutInfo& textLayoutInfo,
101                                          TextInfoIndices& indices )
102 {
103   // TODO : Check for mixed LTR and RTL.
104
105   // clear all indices
106   indices = TextInfoIndices();
107
108   // Early return.
109   if( textLayoutInfo.mLinesLayoutInfo.empty() )
110   {
111     // Text is empty. All indices are 0.
112     return;
113   }
114
115   std::size_t currentIndex = 0; // stores how many characters have been traversed.
116
117   // Traverse all lines, groups of words and words until global index is found.
118   bool found = false;
119   for( LineLayoutInfoContainer::const_iterator lineIt = textLayoutInfo.mLinesLayoutInfo.begin(),
120          lineEndIt = textLayoutInfo.mLinesLayoutInfo.end();
121        ( !found ) && ( lineIt != lineEndIt );
122        ++lineIt, ++indices.mLineIndex )
123   {
124     const LineLayoutInfo& lineLayoutInfo( *lineIt );
125
126     if( currentIndex + lineLayoutInfo.mNumberOfCharacters > index )
127     {
128       // The character is in this line
129       for( WordGroupLayoutInfoContainer::const_iterator groupIt = lineLayoutInfo.mWordGroupsLayoutInfo.begin(),
130              groupEndIt = lineLayoutInfo.mWordGroupsLayoutInfo.end();
131            ( !found ) && ( groupIt != groupEndIt );
132            ++groupIt, ++indices.mGroupIndex )
133       {
134         const WordGroupLayoutInfo& wordGroupLayoutInfo( *groupIt );
135
136         if( currentIndex + wordGroupLayoutInfo.mNumberOfCharacters > index )
137         {
138           // The character is in this group of words.
139           for( WordLayoutInfoContainer::const_iterator wordIt = wordGroupLayoutInfo.mWordsLayoutInfo.begin(),
140                  wordEndIt = wordGroupLayoutInfo.mWordsLayoutInfo.end();
141                ( !found ) && ( wordIt != wordEndIt );
142                ++wordIt, ++indices.mWordIndex )
143           {
144             const WordLayoutInfo& wordLayoutInfo( *wordIt );
145
146             if( currentIndex + wordLayoutInfo.mCharactersLayoutInfo.size() > index )
147             {
148               // The character is in this word
149               indices.mCharacterIndex = index - currentIndex;
150               found = true;
151             }
152             else
153             {
154               // check in the next word.
155               currentIndex += wordLayoutInfo.mCharactersLayoutInfo.size();
156             }
157           } // end words.
158           if( !wordGroupLayoutInfo.mWordsLayoutInfo.empty() )
159           {
160             --indices.mWordIndex;
161           }
162         }
163         else
164         {
165           // check in the next group of words
166           currentIndex += wordGroupLayoutInfo.mNumberOfCharacters;
167         }
168       } // end groups of words.
169       if( !lineLayoutInfo.mWordGroupsLayoutInfo.empty() )
170       {
171         --indices.mGroupIndex;
172       }
173     }
174     else
175     {
176       // check in the next line
177       currentIndex += lineLayoutInfo.mNumberOfCharacters;
178     }
179   } // end lines.
180
181   // Need to decrease indices as they have been increased in the last loop.
182   if( !textLayoutInfo.mLinesLayoutInfo.empty() )
183   {
184     --indices.mLineIndex;
185   }
186 }
187
188 void ClearText( std::vector<TextActor>& textActors )
189 {
190     for( std::vector<TextActor>::iterator it = textActors.begin(), endIt = textActors.end(); it != endIt; ++it )
191     {
192       (*it).SetText( std::string( "" ) );
193     }
194 }
195
196 } //namespace TextViewProcessor
197
198 } //namespace Internal
199
200 } //namespace Toolkit
201
202 } //namespace Dali