2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/text-view/text-view-processor-helper-functions.h>
22 #include <dali-toolkit/internal/controls/text-view/text-view-processor-dbg.h>
33 namespace TextViewProcessor
36 void UpdateSize( Size& size1, const Size& size2, const SizeGrowType type )
42 size1.width += size2.width;
43 size1.height = std::max( size1.height, size2.height );
48 size1.width = std::max( size1.width, size2.width );
49 size1.height += size2.height;
55 TextSeparatorType GetTextSeparatorType( const Character& character )
57 // returns if the given character is a paragraph separator '\n', a word separator ' ' or if is not a separator (any other character).
58 return ( character.IsNewLine() ? ParagraphSeparator : ( character.IsWhiteSpace() ? WordSeparator : NoSeparator ) );
61 void ChooseFontFamilyName( const Character& character, TextStyle& style )
63 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "-->TextViewProcessor::ChooseFontFamilyName\n" );
64 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, " input font name: [%s]\n", style.GetFontName().c_str() );
66 bool userDefinedFontFamilyName = false;
68 // First check if there is a font defined in the style and it supports the given text.
69 if( !style.GetFontName().empty() )
71 const FontParameters fontParams( style.GetFontName(), style.GetFontStyle() , style.GetFontPointSize() );
72 const Font font = Font::New( fontParams );
74 if( !font.IsDefaultSystemFont() && font.AllGlyphsSupported( character ) )
76 userDefinedFontFamilyName = true;
80 if( !userDefinedFontFamilyName )
82 const Font defaultSystemFont = Font::New();
84 // At this point no font is set or doesn't support the given text.
85 if( !defaultSystemFont.AllGlyphsSupported( character ) )
87 // If the default system font doesn't support the given text,
88 // an appropiate font is selected.
89 style.SetFontName( Font::GetFamilyForText( character ) );
90 // @TODO Font::GetFamilyForText() should return font family and font style.
94 // All characters are supported with default font, so use it
95 style.SetFontName( "" );
98 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, " output font name: [%s]\n", style.GetFontName().c_str() );
99 DALI_LOG_INFO( gTextViewProcessorLogFilter, Debug::General, "<--TextViewProcessor::ChooseFontFamilyName\n" );
102 void GetIndicesFromGlobalCharacterIndex( const std::size_t index,
103 const TextLayoutInfo& textLayoutInfo,
104 TextInfoIndices& indices )
107 indices = TextInfoIndices();
110 if( textLayoutInfo.mParagraphsLayoutInfo.empty() )
112 // Text is empty. All indices are 0.
116 std::size_t currentIndex = 0u; // stores how many characters have been traversed (within the whole text).
118 // Traverse all paragraphs and words until global index is found.
120 for( ParagraphLayoutInfoContainer::const_iterator paragraphIt = textLayoutInfo.mParagraphsLayoutInfo.begin(),
121 paragraphEndIt = textLayoutInfo.mParagraphsLayoutInfo.end();
122 ( !found ) && ( paragraphIt != paragraphEndIt );
123 ++paragraphIt, ++indices.mParagraphIndex )
125 const ParagraphLayoutInfo& paragraphLayoutInfo( *paragraphIt );
126 std::size_t currentCharactersTraversed = currentIndex; // stores how many characters have been traversed until this paragraph.
128 if( currentIndex + paragraphLayoutInfo.mNumberOfCharacters > index )
130 // The character is in this paragraph
131 for( WordLayoutInfoContainer::const_iterator wordIt = paragraphLayoutInfo.mWordsLayoutInfo.begin(),
132 wordEndIt = paragraphLayoutInfo.mWordsLayoutInfo.end();
133 ( !found ) && ( wordIt != wordEndIt );
134 ++wordIt, ++indices.mWordIndex )
136 const WordLayoutInfo& wordLayoutInfo( *wordIt );
138 if( currentIndex + wordLayoutInfo.mCharactersLayoutInfo.size() > index )
140 // The character is in this word
141 indices.mCharacterIndex = index - currentIndex;
142 indices.mCharacterParagraphIndex = index - currentCharactersTraversed;
147 // check in the next word.
148 currentIndex += wordLayoutInfo.mCharactersLayoutInfo.size();
151 if( !paragraphLayoutInfo.mWordsLayoutInfo.empty() )
153 --indices.mWordIndex;
158 // check in the next paragraph
159 currentIndex += paragraphLayoutInfo.mNumberOfCharacters;
163 // Need to decrease indices as they have been increased in the last loop.
164 if( !textLayoutInfo.mParagraphsLayoutInfo.empty() )
166 --indices.mParagraphIndex;
170 void ClearText( std::vector<TextActor>& textActors )
172 for( std::vector<TextActor>::iterator it = textActors.begin(), endIt = textActors.end(); it != endIt; ++it )
174 (*it).SetText( std::string( "" ) );
178 } //namespace TextViewProcessor
180 } //namespace Internal
182 } //namespace Toolkit