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>
35 #include <dali/public-api/text-abstraction/font-client.h>
48 struct Controller::TextInput
50 // Used to queue input events until DoRelayout()
53 KEYBOARD_FOCUS_GAIN_EVENT,
54 KEYBOARD_FOCUS_LOST_EVENT,
68 Event( EventType eventType )
88 TextInput( DecoratorPtr decorator )
89 : mDecorator( decorator ),
95 * @brief Helper to move the cursor, grab handle etc.
97 bool ProcessTouchEvents()
99 mDecoratorUpdated = false;
103 for( vector<TextInput::Event>::iterator iter = mEventQueue.begin(); iter != mEventQueue.end(); ++iter )
107 case KEYBOARD_FOCUS_GAIN_EVENT:
109 OnKeyboardFocus( true );
112 case KEYBOARD_FOCUS_LOST_EVENT:
114 OnKeyboardFocus( false );
122 case GRAB_HANDLE_EVENT:
124 OnGrabHandleEvent( *iter );
133 return mDecoratorUpdated;
136 void OnKeyboardFocus( bool hasFocus )
141 void OnTapEvent( const Event& event )
143 if( 1u == event.p1.mUint )
145 mState = TextInput::EDITING;
146 mDecorator->SetGrabHandleActive( true );
147 mDecorator->SetPosition( PRIMARY_CURSOR, 0, 0, 10 );
148 mDecoratorUpdated = true;
150 else if( 2u == event.p1.mUint )
152 mState = TextInput::SELECTING;
153 mDecorator->SetGrabHandleActive( false );
154 mDecorator->SetSelectionActive( true );
155 mDecoratorUpdated = true;
159 void OnGrabHandleEvent( const Event& event )
164 DecoratorPtr mDecorator;
165 bool mDecoratorUpdated;
170 * This is used to delay handling events until after the model has been updated.
171 * The number of updates to the model is minimized to improve performance.
173 vector<Event> mEventQueue; ///< The queue of touch events etc.
176 struct Controller::Impl
178 Impl( ControlInterface& controlInterface )
179 : mControlInterface( controlInterface ),
181 mOperations( NO_OPERATION ),
185 mLogicalModel = LogicalModel::New();
186 mVisualModel = VisualModel::New();
188 mView.SetVisualModel( mVisualModel );
190 mFontClient = TextAbstraction::FontClient::Get();
198 ControlInterface& mControlInterface;
200 std::string mNewText;
202 LogicalModelPtr mLogicalModel;
203 VisualModelPtr mVisualModel;
207 LayoutEngine mLayoutEngine;
209 TextAbstraction::FontClient mFontClient;
211 OperationsMask mOperations;
215 // Avoid allocating everything for text input until EnableTextInput()
216 Controller::TextInput* mTextInput;
219 ControllerPtr Controller::New( ControlInterface& controlInterface )
221 return ControllerPtr( new Controller( controlInterface ) );
224 void Controller::SetText( const std::string& text )
226 // Keep until size negotiation
227 mImpl->mNewText = text;
228 mImpl->mOperations = ALL_OPERATIONS;
230 if( mImpl->mTextInput )
232 // Cancel previously queued events
233 mImpl->mTextInput->mEventQueue.clear();
235 // TODO - Hide selection decorations
239 void Controller::EnableTextInput( DecoratorPtr decorator )
241 if( !mImpl->mTextInput )
243 mImpl->mTextInput = new TextInput( decorator );
247 bool Controller::Relayout( const Vector2& size )
249 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
251 // Not worth to relayout if width or height is equal to zero.
255 bool updated = false;
257 if( size != mImpl->mControlSize )
259 updated = DoRelayout( size, mImpl->mOperations );
261 // Do not re-do any operation until something changes.
262 mImpl->mOperations = NO_OPERATION;
264 mImpl->mControlSize = size;
267 if( mImpl->mTextInput )
269 // Move the cursor, grab handle etc.
270 updated = mImpl->mTextInput->ProcessTouchEvents() || updated;
276 bool Controller::DoRelayout( const Vector2& size, OperationsMask operations )
278 bool viewUpdated( false );
280 Vector<Character> utf32Characters;
281 Length characterCount = 0u;
282 if( CONVERT_TO_UTF32 & operations )
284 std::string& text = mImpl->mNewText;
286 // Convert text into UTF-32
287 utf32Characters.Resize( text.size() );
289 // This is a bit horrible but std::string returns a (signed) char*
290 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
292 // Transform a text array encoded in utf8 into an array encoded in utf32.
293 // It returns the actual number of characters.
294 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
295 utf32Characters.Resize( characterCount );
297 // Sets the text into the model.
298 mImpl->mLogicalModel->SetText( utf32Characters.Begin(), characterCount );
300 // Discard temporary text
304 Vector<LineBreakInfo> lineBreakInfo;
305 if( GET_LINE_BREAKS & operations )
307 // Retrieves the line break info. The line break info is used to split the text in 'paragraphs' to
308 // calculate the bidirectional info for each 'paragraph'.
309 // It's also used to layout the text (where it should be a new line) or to shape the text (text in different lines
310 // is not shaped together).
311 lineBreakInfo.Resize( characterCount, TextAbstraction::LINE_NO_BREAK );
313 SetLineBreakInfo( utf32Characters,
316 mImpl->mLogicalModel->SetLineBreakInfo( lineBreakInfo.Begin(), characterCount );
319 const bool getScripts = GET_SCRIPTS & operations;
320 const bool validateFonts = VALIDATE_FONTS & operations;
322 Vector<ScriptRun> scripts;
323 Vector<FontRun> fonts;
324 if( getScripts || validateFonts )
326 // Validates the fonts assigned by the application or assigns default ones.
327 // It makes sure all the characters are going to be rendered by the correct font.
328 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
332 // Retrieves the scripts used in the text.
333 multilanguageSupport.SetScripts( utf32Characters,
337 // Sets the scripts into the model.
338 mImpl->mLogicalModel->SetScripts( scripts.Begin(), scripts.Count() );
343 // Validates the fonts. If there is a character with no assigned font it sets a default one.
344 // After this call, fonts are validated.
345 multilanguageSupport.ValidateFonts( utf32Characters,
349 // Sets the fonts into the model.
350 mImpl->mLogicalModel->SetFonts( fonts.Begin(), fonts.Count() );
354 Vector<GlyphInfo> glyphs;
355 Vector<CharacterIndex> characterIndices;
356 Vector<Length> charactersPerGlyph;
357 if( SHAPE_TEXT & operations )
360 ShapeText( utf32Characters,
366 charactersPerGlyph );
369 if( GET_GLYPH_METRICS & operations )
371 TextAbstraction::FontClient::Get().GetGlyphMetrics( glyphs.Begin(), glyphs.Count() );
374 if( LAYOUT & operations )
376 if( 0u == glyphs.Count() )
378 const Length numberOfGlyphs = mImpl->mVisualModel->GetNumberOfGlyphs();
380 glyphs.Resize( numberOfGlyphs );
381 characterIndices.Resize( numberOfGlyphs );
382 charactersPerGlyph.Resize( numberOfGlyphs );
384 mImpl->mVisualModel->GetGlyphs( glyphs.Begin(),
388 mImpl->mVisualModel->GetGlyphToCharacterMap( characterIndices.Begin(),
392 mImpl->mVisualModel->GetCharactersPerGlyphMap( charactersPerGlyph.Begin(),
397 // Update the visual model
398 mImpl->mLayoutEngine.UpdateVisualModel( size,
402 *mImpl->mVisualModel );
410 Vector3 Controller::GetNaturalSize()
412 // Operations that can be done only once until the text changes.
413 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
421 // Operations that need to be done if the size or the text changes.
422 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
425 const float maxFloat = std::numeric_limits<float>::max();
426 DoRelayout( Vector2( maxFloat, maxFloat ),
427 static_cast<OperationsMask>( onlyOnceOperations |
430 // Do not do again the only once operations.
431 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations & ~onlyOnceOperations );
433 // Do the size related operations again.
434 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations | sizeOperations );
436 return Vector3( mImpl->mVisualModel->GetNaturalSize() );
439 float Controller::GetHeightForWidth( float width )
441 // Operations that can be done only once until the text changes.
442 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
450 // Operations that need to be done if the size or the text changes.
451 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
454 DoRelayout( Size( width, 0.f ),
455 static_cast<OperationsMask>( onlyOnceOperations |
458 // Do not do again the only once operations.
459 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations & ~onlyOnceOperations );
461 // Do the size related operations again.
462 mImpl->mOperations = static_cast<OperationsMask>( mImpl->mOperations | sizeOperations );
464 return mImpl->mVisualModel->GetActualSize().height;
467 View& Controller::GetView()
472 LayoutEngine& Controller::GetLayoutEngine()
474 return mImpl->mLayoutEngine;
477 void Controller::RequestRelayout()
479 mImpl->mControlInterface.RequestTextRelayout();
482 void Controller::KeyboardFocusGainEvent()
484 DALI_ASSERT_DEBUG( mImpl->mTextInput && "Unexpected KeyboardFocusGainEvent" );
486 if( mImpl->mTextInput )
488 TextInput::Event event( TextInput::KEYBOARD_FOCUS_GAIN_EVENT );
489 mImpl->mTextInput->mEventQueue.push_back( event );
495 void Controller::KeyboardFocusLostEvent()
497 DALI_ASSERT_DEBUG( mImpl->mTextInput && "Unexpected KeyboardFocusLostEvent" );
499 if( mImpl->mTextInput )
501 TextInput::Event event( TextInput::KEYBOARD_FOCUS_LOST_EVENT );
502 mImpl->mTextInput->mEventQueue.push_back( event );
508 void Controller::TapEvent( unsigned int tapCount, float x, float y )
510 DALI_ASSERT_DEBUG( mImpl->mTextInput && "Unexpected TapEvent" );
512 if( mImpl->mTextInput )
514 TextInput::Event event( TextInput::TAP_EVENT );
515 event.p1.mUint = tapCount;
518 mImpl->mTextInput->mEventQueue.push_back( event );
524 void Controller::GrabHandleEvent( GrabHandleState state, float x )
526 DALI_ASSERT_DEBUG( mImpl->mTextInput && "Unexpected GrabHandleEvent" );
528 if( mImpl->mTextInput )
530 TextInput::Event event( TextInput::GRAB_HANDLE_EVENT );
531 event.p1.mInt = state;
533 mImpl->mTextInput->mEventQueue.push_back( event );
539 Controller::~Controller()
544 Controller::Controller( ControlInterface& controlInterface )
547 mImpl = new Controller::Impl( controlInterface );
552 } // namespace Toolkit