2 * Copyright (c) 2015 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/public-api/text/text-controller.h>
22 #include <dali-toolkit/public-api/text/character-set-conversion.h>
23 #include <dali-toolkit/public-api/text/layouts/layout-engine.h>
24 #include <dali-toolkit/public-api/text/logical-model.h>
25 #include <dali-toolkit/public-api/text/multi-language-support.h>
26 #include <dali-toolkit/public-api/text/script-run.h>
27 #include <dali-toolkit/public-api/text/segmentation.h>
28 #include <dali-toolkit/public-api/text/shaper.h>
29 #include <dali-toolkit/public-api/text/text-view.h>
30 #include <dali-toolkit/public-api/text/visual-model.h>
33 #include <dali/public-api/text-abstraction/font-client.h>
45 struct Controller::Impl
49 mOperations( NO_OPERATION ),
52 mLogicalModel = LogicalModel::New();
53 mVisualModel = VisualModel::New();
55 mView.SetVisualModel( mVisualModel );
57 mFontClient = TextAbstraction::FontClient::Get();
62 LogicalModelPtr mLogicalModel;
63 VisualModelPtr mVisualModel;
67 LayoutEngine mLayoutEngine;
69 TextAbstraction::FontClient mFontClient;
71 OperationsMask mOperations;
76 ControllerPtr Controller::New()
78 return ControllerPtr( new Controller() );
81 void Controller::SetText( const std::string& text )
83 // Keep until size negotiation
84 mImpl->mNewText = text;
85 mImpl->mOperations = ALL_OPERATIONS;
88 bool Controller::Relayout( const Vector2& size )
90 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
92 // Not worth to relayout if width or height is equal to zero.
96 bool viewUpdated = false;
98 if( size != mImpl->mControlSize )
100 viewUpdated = DoRelayout( size, mImpl->mOperations );
102 // Do not re-do any operation until something changes.
103 mImpl->mOperations = NO_OPERATION;
105 mImpl->mControlSize = size;
111 bool Controller::DoRelayout( const Vector2& size, OperationsMask operations )
113 bool viewUpdated( false );
115 Vector<Character> utf32Characters;
116 Length characterCount = 0u;
117 if( CONVERT_TO_UTF32 & operations )
119 std::string& text = mImpl->mNewText;
121 // Convert text into UTF-32
122 utf32Characters.Resize( text.size() );
124 // This is a bit horrible but std::string returns a (signed) char*
125 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
127 // Transform a text array encoded in utf8 into an array encoded in utf32.
128 // It returns the actual number of characters.
129 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
130 utf32Characters.Resize( characterCount );
132 // Sets the text into the model.
133 mImpl->mLogicalModel->SetText( utf32Characters.Begin(), characterCount );
135 // Discard temporary text
139 Vector<LineBreakInfo> lineBreakInfo;
140 if( GET_LINE_BREAKS & operations )
142 // Retrieves the line break info. The line break info is used to split the text in 'paragraphs' to
143 // calculate the bidirectional info for each 'paragraph'.
144 // It's also used to layout the text (where it should be a new line) or to shape the text (text in different lines
145 // is not shaped together).
146 lineBreakInfo.Resize( characterCount, TextAbstraction::LINE_NO_BREAK );
148 SetLineBreakInfo( utf32Characters,
151 mImpl->mLogicalModel->SetLineBreakInfo( lineBreakInfo.Begin(), characterCount );
154 const bool getScripts = GET_SCRIPTS & operations;
155 const bool validateFonts = VALIDATE_FONTS & operations;
157 Vector<ScriptRun> scripts;
158 Vector<FontRun> fonts;
159 if( getScripts || validateFonts )
161 // Validates the fonts assigned by the application or assigns default ones.
162 // It makes sure all the characters are going to be rendered by the correct font.
163 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
167 // Retrieves the scripts used in the text.
168 multilanguageSupport.SetScripts( utf32Characters,
172 // Sets the scripts into the model.
173 mImpl->mLogicalModel->SetScripts( scripts.Begin(), scripts.Count() );
178 // Validates the fonts. If there is a character with no assigned font it sets a default one.
179 // After this call, fonts are validated.
180 multilanguageSupport.ValidateFonts( utf32Characters,
184 // Sets the fonts into the model.
185 mImpl->mLogicalModel->SetFonts( fonts.Begin(), fonts.Count() );
189 Vector<GlyphInfo> glyphs;
190 Vector<CharacterIndex> characterIndices;
191 Vector<Length> charactersPerGlyph;
192 if( SHAPE_TEXT & operations )
195 ShapeText( utf32Characters,
201 charactersPerGlyph );
204 if( GET_GLYPH_METRICS & operations )
206 TextAbstraction::FontClient::Get().GetGlyphMetrics( glyphs.Begin(), glyphs.Count() );
209 if( LAYOUT & operations )
211 if( 0u == glyphs.Count() )
213 const Length numberOfGlyphs = mImpl->mVisualModel->GetNumberOfGlyphs();
215 glyphs.Resize( numberOfGlyphs );
216 characterIndices.Resize( numberOfGlyphs );
217 charactersPerGlyph.Resize( numberOfGlyphs );
219 mImpl->mVisualModel->GetGlyphs( glyphs.Begin(),
223 mImpl->mVisualModel->GetGlyphToCharacterMap( characterIndices.Begin(),
227 mImpl->mVisualModel->GetCharactersPerGlyphMap( charactersPerGlyph.Begin(),
232 // Update the visual model
233 mImpl->mLayoutEngine.UpdateVisualModel( size,
237 *mImpl->mVisualModel );
245 Vector3 Controller::GetNaturalSize()
247 // Operations that can be done only once until the text changes.
248 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
256 // Operations that need to be done if the size or the text changes.
257 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
260 const float maxFloat = std::numeric_limits<float>::max();
261 DoRelayout( Vector2( maxFloat, maxFloat ),
262 static_cast<OperationsMask>( onlyOnceOperations |
265 // Do not do again the only once operations.
266 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations & ~onlyOnceOperations );
268 // Do the size related operations again.
269 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations | sizeOperations );
271 return Vector3( mImpl->mVisualModel->GetNaturalSize() );
274 float Controller::GetHeightForWidth( float width )
276 // Operations that can be done only once until the text changes.
277 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
285 // Operations that need to be done if the size or the text changes.
286 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
289 DoRelayout( Size( width, 0.f ),
290 static_cast<OperationsMask>( onlyOnceOperations |
293 // Do not do again the only once operations.
294 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations & ~onlyOnceOperations );
296 // Do the size related operations again.
297 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations | sizeOperations );
299 return mImpl->mVisualModel->GetActualSize().height;
302 View& Controller::GetView()
307 LayoutEngine& Controller::GetLayoutEngine()
309 return mImpl->mLayoutEngine;
312 Controller::~Controller()
317 Controller::Controller()
320 mImpl = new Controller::Impl();
325 } // namespace Toolkit