2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 #include "text-view-processor-helper-functions.h"
19 #include "text-view-processor-dbg.h"
30 namespace TextViewProcessor
33 void UpdateSize( Size& size1, const Size& size2, const SizeGrowType type )
39 size1.width += size2.width;
40 size1.height = std::max( size1.height, size2.height );
45 size1.width = std::max( size1.width, size2.width );
46 size1.height += size2.height;
52 TextSeparatorType GetTextSeparatorType( const Character& character )
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 ) );
58 void ChooseFontFamilyName( MarkupProcessor::StyledText& text )
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() );
63 bool userDefinedFontFamilyName = false;
65 // First check if there is a font defined in the style and it supports the given text.
66 if( !text.mStyle.GetFontName().empty() )
68 const FontParameters fontParams( text.mStyle.GetFontName(), text.mStyle.GetFontStyle() , text.mStyle.GetFontPointSize() );
69 const Font font = Font::New( fontParams );
71 if( !font.IsDefaultSystemFont() && font.AllGlyphsSupported( text.mText ) )
73 userDefinedFontFamilyName = true;
77 if( !userDefinedFontFamilyName )
79 const Font defaultSystemFont = Font::New();
81 // At this point no font is set or doesn't support the given text.
82 if( !defaultSystemFont.AllGlyphsSupported( text.mText ) )
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.
91 // All characters are supported with default font, so use it
92 text.mStyle.SetFontName( "" );
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" );
99 void GetIndicesFromGlobalCharacterIndex( const std::size_t index,
100 const TextLayoutInfo& textLayoutInfo,
101 TextInfoIndices& indices )
103 // TODO : Check for mixed LTR and RTL.
106 indices = TextInfoIndices();
109 if( textLayoutInfo.mLinesLayoutInfo.empty() )
111 // Text is empty. All indices are 0.
115 std::size_t currentIndex = 0; // stores how many characters have been traversed.
117 // Traverse all lines, groups of words and words until global index is found.
119 for( LineLayoutInfoContainer::const_iterator lineIt = textLayoutInfo.mLinesLayoutInfo.begin(),
120 lineEndIt = textLayoutInfo.mLinesLayoutInfo.end();
121 ( !found ) && ( lineIt != lineEndIt );
122 ++lineIt, ++indices.mLineIndex )
124 const LineLayoutInfo& lineLayoutInfo( *lineIt );
126 if( currentIndex + lineLayoutInfo.mNumberOfCharacters > index )
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 )
134 const WordGroupLayoutInfo& wordGroupLayoutInfo( *groupIt );
136 if( currentIndex + wordGroupLayoutInfo.mNumberOfCharacters > index )
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 )
144 const WordLayoutInfo& wordLayoutInfo( *wordIt );
146 if( currentIndex + wordLayoutInfo.mCharactersLayoutInfo.size() > index )
148 // The character is in this word
149 indices.mCharacterIndex = index - currentIndex;
154 // check in the next word.
155 currentIndex += wordLayoutInfo.mCharactersLayoutInfo.size();
158 if( !wordGroupLayoutInfo.mWordsLayoutInfo.empty() )
160 --indices.mWordIndex;
165 // check in the next group of words
166 currentIndex += wordGroupLayoutInfo.mNumberOfCharacters;
168 } // end groups of words.
169 if( !lineLayoutInfo.mWordGroupsLayoutInfo.empty() )
171 --indices.mGroupIndex;
176 // check in the next line
177 currentIndex += lineLayoutInfo.mNumberOfCharacters;
181 // Need to decrease indices as they have been increased in the last loop.
182 if( !textLayoutInfo.mLinesLayoutInfo.empty() )
184 --indices.mLineIndex;
188 void ClearText( std::vector<TextActor>& textActors )
190 for( std::vector<TextActor>::iterator it = textActors.begin(), endIt = textActors.end(); it != endIt; ++it )
192 (*it).SetText( std::string( "" ) );
196 } //namespace TextViewProcessor
198 } //namespace Internal
200 } //namespace Toolkit