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/internal/text/text-controller.h>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/internal/text/bidirectional-support.h>
29 #include <dali-toolkit/internal/text/character-set-conversion.h>
30 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
31 #include <dali-toolkit/internal/text/multi-language-support.h>
32 #include <dali-toolkit/internal/text/script-run.h>
33 #include <dali-toolkit/internal/text/segmentation.h>
34 #include <dali-toolkit/internal/text/shaper.h>
35 #include <dali-toolkit/internal/text/text-controller-impl.h>
36 #include <dali-toolkit/internal/text/text-io.h>
37 #include <dali-toolkit/internal/text/text-view.h>
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
46 const float MAX_FLOAT = std::numeric_limits<float>::max();
48 const std::string EMPTY_STRING("");
61 ControllerPtr Controller::New( ControlInterface& controlInterface )
63 return ControllerPtr( new Controller( controlInterface ) );
66 void Controller::EnableTextInput( DecoratorPtr decorator )
68 if( !mImpl->mEventData )
70 mImpl->mEventData = new EventData( decorator );
74 void Controller::SetText( const std::string& text )
76 // Cancel previously queued inserts etc.
77 mImpl->mModifyEvents.clear();
79 // Remove the previously set text
84 // Convert text into UTF-32
85 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
86 utf32Characters.Resize( text.size() );
88 // This is a bit horrible but std::string returns a (signed) char*
89 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
91 // Transform a text array encoded in utf8 into an array encoded in utf32.
92 // It returns the actual number of characters.
93 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
94 utf32Characters.Resize( characterCount );
96 DALI_ASSERT_DEBUG( text.size() >= characterCount && "Invalid UTF32 conversion length" );
97 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, text.size(), mImpl->mLogicalModel->mText.Count() );
99 // Reset the cursor position
100 if( mImpl->mEventData )
102 mImpl->mEventData->mPrimaryCursorPosition = characterCount;
105 // Update the rest of the model during size negotiation
106 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
110 ShowPlaceholderText();
113 if( mImpl->mEventData )
115 // Cancel previously queued events
116 mImpl->mEventData->mEventQueue.clear();
119 // Reset keyboard as text changed
120 mImpl->PreEditReset();
123 void Controller::GetText( std::string& text ) const
125 if( ! mImpl->IsShowingPlaceholderText() )
127 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
129 if( 0u != utf32Characters.Count() )
131 uint32_t numberOfBytes = GetNumberOfUtf8Bytes( &utf32Characters[0], utf32Characters.Count() );
133 text.resize( numberOfBytes );
135 // This is a bit horrible but std::string returns a (signed) char*
136 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), reinterpret_cast<uint8_t*>(&text[0]) );
141 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
145 unsigned int Controller::GetLogicalCursorPosition() const
147 if( mImpl->mEventData )
149 return mImpl->mEventData->mPrimaryCursorPosition;
155 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
157 if( mImpl->mEventData )
159 if( PLACEHOLDER_TYPE_INACTIVE == type )
161 mImpl->mEventData->mPlaceholderTextInactive = text;
165 mImpl->mEventData->mPlaceholderTextActive = text;
168 ShowPlaceholderText();
172 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
174 if( mImpl->mEventData )
176 if( PLACEHOLDER_TYPE_INACTIVE == type )
178 text = mImpl->mEventData->mPlaceholderTextInactive;
182 text = mImpl->mEventData->mPlaceholderTextActive;
187 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
189 if ( maxCharacters >= 0 )
191 mImpl->mMaximumNumberOfCharacters = maxCharacters;
195 int Controller::GetMaximumNumberOfCharacters()
197 return mImpl->mMaximumNumberOfCharacters;
200 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
202 if( !mImpl->mFontDefaults )
204 mImpl->mFontDefaults = new FontDefaults();
207 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
209 // Clear the font-specific data
212 mImpl->mOperationsPending = ALL_OPERATIONS;
213 mImpl->mRecalculateNaturalSize = true;
215 mImpl->RequestRelayout();
218 const std::string& Controller::GetDefaultFontFamily() const
220 if( mImpl->mFontDefaults )
222 return mImpl->mFontDefaults->mDefaultFontFamily;
228 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
230 if( !mImpl->mFontDefaults )
232 mImpl->mFontDefaults = new FontDefaults();
235 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
237 // Clear the font-specific data
240 mImpl->mOperationsPending = ALL_OPERATIONS;
241 mImpl->mRecalculateNaturalSize = true;
243 mImpl->RequestRelayout();
246 const std::string& Controller::GetDefaultFontStyle() const
248 if( mImpl->mFontDefaults )
250 return mImpl->mFontDefaults->mDefaultFontStyle;
256 void Controller::SetDefaultPointSize( float pointSize )
258 if( !mImpl->mFontDefaults )
260 mImpl->mFontDefaults = new FontDefaults();
263 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
265 // Clear the font-specific data
268 mImpl->mOperationsPending = ALL_OPERATIONS;
269 mImpl->mRecalculateNaturalSize = true;
271 mImpl->RequestRelayout();
274 float Controller::GetDefaultPointSize() const
276 if( mImpl->mFontDefaults )
278 return mImpl->mFontDefaults->mDefaultPointSize;
284 void Controller::SetTextColor( const Vector4& textColor )
286 mImpl->mTextColor = textColor;
288 if( ! mImpl->IsShowingPlaceholderText() )
290 mImpl->mVisualModel->SetTextColor( textColor );
294 const Vector4& Controller::GetTextColor() const
296 return mImpl->mTextColor;
299 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
301 bool removed( false );
303 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
304 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
306 if( ! mImpl->IsShowingPlaceholderText() )
308 // Delete at current cursor position
309 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
310 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
312 CharacterIndex cursorIndex = oldCursorIndex;
314 // Validate the cursor position & number of characters
315 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
317 cursorIndex = oldCursorIndex + cursorOffset;
320 if( (cursorIndex + numberOfChars) > currentText.Count() )
322 numberOfChars = currentText.Count() - cursorIndex;
325 if( cursorIndex >= 0 &&
326 (cursorIndex + numberOfChars) <= currentText.Count() )
328 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
329 Vector<Character>::Iterator last = first + numberOfChars;
331 currentText.Erase( first, last );
333 // Cursor position retreat
334 oldCursorIndex = cursorIndex;
336 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
344 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
346 if( mImpl->mEventData )
348 mImpl->mEventData->mPlaceholderTextColor = textColor;
351 if( mImpl->IsShowingPlaceholderText() )
353 mImpl->mVisualModel->SetTextColor( textColor );
357 const Vector4& Controller::GetPlaceholderTextColor() const
359 if( mImpl->mEventData )
361 return mImpl->mEventData->mPlaceholderTextColor;
367 void Controller::SetShadowOffset( const Vector2& shadowOffset )
369 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
372 const Vector2& Controller::GetShadowOffset() const
374 return mImpl->mVisualModel->GetShadowOffset();
377 void Controller::SetShadowColor( const Vector4& shadowColor )
379 mImpl->mVisualModel->SetShadowColor( shadowColor );
382 const Vector4& Controller::GetShadowColor() const
384 return mImpl->mVisualModel->GetShadowColor();
387 void Controller::SetUnderlineColor( const Vector4& color )
389 mImpl->mVisualModel->SetUnderlineColor( color );
392 const Vector4& Controller::GetUnderlineColor() const
394 return mImpl->mVisualModel->GetUnderlineColor();
397 void Controller::SetUnderlineEnabled( bool enabled )
399 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
402 bool Controller::IsUnderlineEnabled() const
404 return mImpl->mVisualModel->IsUnderlineEnabled();
407 void Controller::SetUnderlineHeight( float height )
409 mImpl->mVisualModel->SetUnderlineHeight( height );
412 float Controller::GetUnderlineHeight() const
414 return mImpl->mVisualModel->GetUnderlineHeight();
417 void Controller::SetEnableCursorBlink( bool enable )
419 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
421 if( mImpl->mEventData )
423 mImpl->mEventData->mCursorBlinkEnabled = enable;
426 mImpl->mEventData->mDecorator )
428 mImpl->mEventData->mDecorator->StopCursorBlink();
433 bool Controller::GetEnableCursorBlink() const
435 if( mImpl->mEventData )
437 return mImpl->mEventData->mCursorBlinkEnabled;
443 const Vector2& Controller::GetScrollPosition() const
445 if( mImpl->mEventData )
447 return mImpl->mEventData->mScrollPosition;
450 return Vector2::ZERO;
453 const Vector2& Controller::GetAlignmentOffset() const
455 return mImpl->mAlignmentOffset;
458 Vector3 Controller::GetNaturalSize()
462 // Make sure the model is up-to-date before layouting
463 ProcessModifyEvents();
465 if( mImpl->mRecalculateNaturalSize )
467 // Operations that can be done only once until the text changes.
468 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
476 // Make sure the model is up-to-date before layouting
477 mImpl->UpdateModel( onlyOnceOperations );
479 // Operations that need to be done if the size changes.
480 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
484 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
485 static_cast<OperationsMask>( onlyOnceOperations |
487 naturalSize.GetVectorXY() );
489 // Do not do again the only once operations.
490 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
492 // Do the size related operations again.
493 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
495 // Stores the natural size to avoid recalculate it again
496 // unless the text/style changes.
497 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
499 mImpl->mRecalculateNaturalSize = false;
503 naturalSize = mImpl->mVisualModel->GetNaturalSize();
509 float Controller::GetHeightForWidth( float width )
511 // Make sure the model is up-to-date before layouting
512 ProcessModifyEvents();
515 if( width != mImpl->mControlSize.width )
517 // Operations that can be done only once until the text changes.
518 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
526 // Make sure the model is up-to-date before layouting
527 mImpl->UpdateModel( onlyOnceOperations );
529 // Operations that need to be done if the size changes.
530 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
534 DoRelayout( Size( width, MAX_FLOAT ),
535 static_cast<OperationsMask>( onlyOnceOperations |
539 // Do not do again the only once operations.
540 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
542 // Do the size related operations again.
543 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
547 layoutSize = mImpl->mVisualModel->GetActualSize();
550 return layoutSize.height;
553 bool Controller::Relayout( const Size& size )
555 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::Relayout %p --> size %f,%f\n", this, size.width, size.height );
557 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
559 bool glyphsRemoved( false );
560 if( 0u != mImpl->mVisualModel->GetNumberOfGlyphPositions() )
562 mImpl->mVisualModel->SetGlyphPositions( NULL, 0u );
563 glyphsRemoved = true;
565 // Not worth to relayout if width or height is equal to zero.
566 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::Relayout <-- (skipped)\n" );
567 return glyphsRemoved;
570 if( size != mImpl->mControlSize )
572 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mControlSize.width, mImpl->mControlSize.height );
574 // Operations that need to be done if the size changes.
575 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
581 mImpl->mControlSize = size;
584 // Make sure the model is up-to-date before layouting
585 ProcessModifyEvents();
586 mImpl->UpdateModel( mImpl->mOperationsPending );
589 bool updated = DoRelayout( mImpl->mControlSize,
590 mImpl->mOperationsPending,
593 // Do not re-do any operation until something changes.
594 mImpl->mOperationsPending = NO_OPERATION;
596 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
597 CalculateTextAlignment( size );
599 if( mImpl->mEventData )
601 // Move the cursor, grab handle etc.
602 updated = mImpl->ProcessInputEvents() || updated;
605 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::Relayout <--\n" );
609 void Controller::ProcessModifyEvents()
611 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
613 for( unsigned int i=0; i<events.size(); ++i )
615 if( ModifyEvent::TEXT_REPLACED == events[0].type )
617 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
618 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
622 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
626 else if( ModifyEvent::TEXT_DELETED == events[0].type )
628 // Placeholder-text cannot be deleted
629 if( !mImpl->IsShowingPlaceholderText() )
636 // Discard temporary text
640 void Controller::ResetText()
643 mImpl->mLogicalModel->mText.Clear();
646 // Reset the cursor position
647 if( mImpl->mEventData )
649 mImpl->mEventData->mPrimaryCursorPosition = 0;
652 // We have cleared everything including the placeholder-text
653 mImpl->PlaceholderCleared();
655 // The natural size needs to be re-calculated.
656 mImpl->mRecalculateNaturalSize = true;
658 // Apply modifications to the model
659 mImpl->mOperationsPending = ALL_OPERATIONS;
662 void Controller::TextReplacedEvent()
667 // The natural size needs to be re-calculated.
668 mImpl->mRecalculateNaturalSize = true;
670 // Apply modifications to the model
671 mImpl->mOperationsPending = ALL_OPERATIONS;
672 mImpl->UpdateModel( ALL_OPERATIONS );
673 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
679 void Controller::TextInsertedEvent()
681 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
683 // TODO - Optimize this
686 // The natural size needs to be re-calculated.
687 mImpl->mRecalculateNaturalSize = true;
689 // Apply modifications to the model; TODO - Optimize this
690 mImpl->mOperationsPending = ALL_OPERATIONS;
691 mImpl->UpdateModel( ALL_OPERATIONS );
692 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
697 // Queue a cursor reposition event; this must wait until after DoRelayout()
698 mImpl->mEventData->mUpdateCursorPosition = true;
699 mImpl->mEventData->mScrollAfterUpdateCursorPosition = true;
702 void Controller::TextDeletedEvent()
704 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
706 // TODO - Optimize this
709 // The natural size needs to be re-calculated.
710 mImpl->mRecalculateNaturalSize = true;
712 // Apply modifications to the model; TODO - Optimize this
713 mImpl->mOperationsPending = ALL_OPERATIONS;
714 mImpl->UpdateModel( ALL_OPERATIONS );
715 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
720 // Queue a cursor reposition event; this must wait until after DoRelayout()
721 mImpl->mEventData->mUpdateCursorPosition = true;
722 mImpl->mEventData->mScrollAfterUpdateCursorPosition = true;
725 bool Controller::DoRelayout( const Size& size,
726 OperationsMask operationsRequired,
729 bool viewUpdated( false );
731 // Calculate the operations to be done.
732 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
734 if( LAYOUT & operations )
736 // Some vectors with data needed to layout and reorder may be void
737 // after the first time the text has been laid out.
738 // Fill the vectors again.
740 Length numberOfGlyphs = mImpl->mVisualModel->GetNumberOfGlyphs();
742 if( 0u == numberOfGlyphs )
744 // Nothing else to do if there is no glyphs.
748 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
749 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
750 Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
751 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
752 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
753 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
755 // Set the layout parameters.
756 LayoutParameters layoutParameters( size,
757 mImpl->mLogicalModel->mText.Begin(),
758 lineBreakInfo.Begin(),
759 wordBreakInfo.Begin(),
760 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
763 glyphsToCharactersMap.Begin(),
764 charactersPerGlyph.Begin() );
766 // The laid-out lines.
767 // It's not possible to know in how many lines the text is going to be laid-out,
768 // but it can be resized at least with the number of 'paragraphs' to avoid
769 // some re-allocations.
770 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
772 // Delete any previous laid out lines before setting the new ones.
775 // The capacity of the bidirectional paragraph info is the number of paragraphs.
776 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
778 // Resize the vector of positions to have the same size than the vector of glyphs.
779 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
780 glyphPositions.Resize( numberOfGlyphs );
782 // Update the visual model.
783 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
791 if( REORDER & operations )
793 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
795 // Check first if there are paragraphs with bidirectional info.
796 if( 0u != bidirectionalInfo.Count() )
799 const Length numberOfLines = mImpl->mVisualModel->GetNumberOfLines();
801 // Reorder the lines.
802 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
803 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
804 ReorderLines( bidirectionalInfo,
806 lineBidirectionalInfoRuns );
808 // Set the bidirectional info into the model.
809 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
810 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
811 numberOfBidirectionalInfoRuns );
813 // Set the bidirectional info per line into the layout parameters.
814 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
815 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
817 // Get the character to glyph conversion table and set into the layout.
818 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
820 // Get the glyphs per character table and set into the layout.
821 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
823 // Re-layout the text. Reorder those lines with right to left characters.
824 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
827 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
828 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
829 endIt = lineBidirectionalInfoRuns.End();
833 BidirectionalLineInfoRun& bidiLineInfo = *it;
835 free( bidiLineInfo.visualToLogicalMap );
840 if( ALIGN & operations )
842 mImpl->mLayoutEngine.Align( layoutParameters,
848 // Sets the actual size.
849 if( UPDATE_ACTUAL_SIZE & operations )
851 mImpl->mVisualModel->SetActualSize( layoutSize );
857 layoutSize = mImpl->mVisualModel->GetActualSize();
863 void Controller::CalculateTextAlignment( const Size& size )
865 // Get the direction of the first character.
866 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
868 const Size& actualSize = mImpl->mVisualModel->GetActualSize();
870 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
871 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
872 if( firstParagraphDirection &&
873 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
875 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
877 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
881 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
885 switch( horizontalAlignment )
887 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
889 mImpl->mAlignmentOffset.x = 0.f;
892 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
894 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
895 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
898 case LayoutEngine::HORIZONTAL_ALIGN_END:
900 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
905 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
906 switch( verticalAlignment )
908 case LayoutEngine::VERTICAL_ALIGN_TOP:
910 mImpl->mAlignmentOffset.y = 0.f;
913 case LayoutEngine::VERTICAL_ALIGN_CENTER:
915 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
916 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
919 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
921 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
927 LayoutEngine& Controller::GetLayoutEngine()
929 return mImpl->mLayoutEngine;
932 View& Controller::GetView()
937 void Controller::KeyboardFocusGainEvent()
939 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
941 if( mImpl->mEventData )
943 Event event( Event::KEYBOARD_FOCUS_GAIN_EVENT );
944 mImpl->mEventData->mEventQueue.push_back( event );
946 mImpl->RequestRelayout();
950 void Controller::KeyboardFocusLostEvent()
952 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
954 if( mImpl->mEventData )
956 Event event( Event::KEYBOARD_FOCUS_LOST_EVENT );
957 mImpl->mEventData->mEventQueue.push_back( event );
959 mImpl->RequestRelayout();
963 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
965 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
967 if( mImpl->mEventData &&
968 keyEvent.state == KeyEvent::Down )
970 int keyCode = keyEvent.keyCode;
971 const std::string& keyString = keyEvent.keyPressed;
973 // Pre-process to separate modifying events from non-modifying input events.
974 if( Dali::DALI_KEY_ESCAPE == keyCode )
976 // Escape key is a special case which causes focus loss
977 KeyboardFocusLostEvent();
979 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
980 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
981 Dali::DALI_KEY_CURSOR_UP == keyCode ||
982 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
984 Event event( Event::CURSOR_KEY_EVENT );
985 event.p1.mInt = keyCode;
986 mImpl->mEventData->mEventQueue.push_back( event );
988 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
990 // Remove the character before the current cursor position
991 bool removed = RemoveText( -1, 1 );
995 if( 0u == mImpl->mLogicalModel->mText.Count() )
997 ShowPlaceholderText();
1001 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1007 InsertText( keyString, COMMIT );
1010 mImpl->ChangeState( EventData::EDITING ); // todo Confirm this is the best place to change the state of
1012 mImpl->RequestRelayout();
1018 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1020 bool removedPreEdit( false );
1021 bool maxLengthReached( false );
1023 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1024 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPreEditFlag %d cursor %d\n",
1025 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"), mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPrimaryCursorPosition );
1027 if( ! text.empty() )
1029 if( mImpl->IsShowingPlaceholderText() )
1035 if( mImpl->mEventData )
1037 if( COMMIT == type )
1039 mImpl->mEventData->mPreEditFlag = false;
1043 if( mImpl->mEventData->mPreEditFlag &&
1044 0 != mImpl->mEventData->mPreEditLength )
1046 // Remove previous pre-edit text
1047 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1048 removedPreEdit = RemoveText( -1, mImpl->mEventData->mPreEditLength );
1052 // Record the start of the pre-edit text
1053 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1054 mImpl->mEventData->mPreEditLength = text.size();
1056 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1059 mImpl->mEventData->mPreEditFlag = true;
1063 if( ! text.empty() )
1065 // Convert text into UTF-32
1066 Vector<Character> utf32Characters;
1067 utf32Characters.Resize( text.size() );
1069 // This is a bit horrible but std::string returns a (signed) char*
1070 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1072 // Transform a text array encoded in utf8 into an array encoded in utf32.
1073 // It returns the actual number of characters.
1074 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1075 utf32Characters.Resize( characterCount );
1077 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1078 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1080 const Length numberOfCharactersInModel = mImpl->mLogicalModel->GetNumberOfCharacters();
1082 // Restrict new text to fit within Maximum characters setting
1083 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1084 maxLengthReached = ( characterCount > maxSizeOfNewText );
1086 // Insert at current cursor position
1087 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1089 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1091 if( cursorIndex < numberOfCharactersInModel )
1093 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1097 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1100 cursorIndex += maxSizeOfNewText;
1102 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1105 if( removedPreEdit || !text.empty() )
1107 // Queue an inserted event
1108 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1111 if( maxLengthReached )
1113 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1115 mImpl->mControlInterface.MaxLengthReached();
1117 mImpl->PreEditReset();
1121 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1123 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1125 if( NULL != mImpl->mEventData )
1127 if( 1u == tapCount )
1129 bool tapDuringEditMode( EventData::EDITING == mImpl->mEventData->mState );
1131 mImpl->ChangeState( EventData::EDITING );
1133 if( mImpl->IsShowingPlaceholderText() )
1135 // Alternative placeholder-text is used when editing
1136 ShowPlaceholderText();
1138 else if( EventData::EDITING == mImpl->mEventData->mState )
1140 // Grab handle is not shown until a tap is received whilst EDITING
1141 if( tapDuringEditMode )
1143 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
1145 mImpl->mEventData->mDecorator->SetPopupActive( false );
1148 else if( mImpl->mEventData->mSelectionEnabled &&
1149 ( 2u == tapCount ) )
1151 mImpl->ChangeState( EventData::SELECTING );
1155 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1156 if( mImpl->mEventData )
1158 Event event( Event::TAP_EVENT );
1159 event.p1.mUint = tapCount;
1160 event.p2.mFloat = x;
1161 event.p3.mFloat = y;
1162 mImpl->mEventData->mEventQueue.push_back( event );
1164 mImpl->RequestRelayout();
1167 // Reset keyboard as tap event has occurred.
1168 mImpl->PreEditReset();
1171 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1173 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1175 if( mImpl->mEventData )
1177 Event event( Event::PAN_EVENT );
1178 event.p1.mInt = state;
1179 event.p2.mFloat = displacement.x;
1180 event.p3.mFloat = displacement.y;
1181 mImpl->mEventData->mEventQueue.push_back( event );
1183 mImpl->RequestRelayout();
1187 void Controller::HandleEvent( HandleType handleType, HandleState state, float x, float y )
1189 DALI_ASSERT_DEBUG( mImpl->mEventData && "Controller::HandleEvent. Unexpected HandleEvent" );
1191 if( mImpl->mEventData )
1193 switch( handleType )
1197 Event event( Event::GRAB_HANDLE_EVENT );
1198 event.p1.mUint = state;
1199 event.p2.mFloat = x;
1200 event.p3.mFloat = y;
1202 mImpl->mEventData->mEventQueue.push_back( event );
1205 case LEFT_SELECTION_HANDLE:
1207 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1208 event.p1.mUint = state;
1209 event.p2.mFloat = x;
1210 event.p3.mFloat = y;
1212 mImpl->mEventData->mEventQueue.push_back( event );
1215 case RIGHT_SELECTION_HANDLE:
1217 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1218 event.p1.mUint = state;
1219 event.p2.mFloat = x;
1220 event.p3.mFloat = y;
1222 mImpl->mEventData->mEventQueue.push_back( event );
1225 case HANDLE_TYPE_COUNT:
1227 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1231 mImpl->RequestRelayout();
1235 Controller::~Controller()
1240 void Controller::ShowPlaceholderText()
1242 if( mImpl->IsPlaceholderAvailable() )
1244 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1246 mImpl->mEventData->mIsShowingPlaceholderText = true;
1248 // Cancel previously queued inserts etc.
1249 mImpl->mModifyEvents.clear();
1251 // Disable handles when showing place-holder text
1252 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1253 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1254 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1256 const char* text( NULL );
1259 // TODO - Switch placeholder text styles when changing state
1260 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1261 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1263 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1264 size = mImpl->mEventData->mPlaceholderTextActive.size();
1268 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1269 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1272 // Reset model for showing placeholder.
1273 mImpl->mLogicalModel->mText.Clear();
1275 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1277 // Convert text into UTF-32
1278 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1279 utf32Characters.Resize( size );
1281 // This is a bit horrible but std::string returns a (signed) char*
1282 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1284 // Transform a text array encoded in utf8 into an array encoded in utf32.
1285 // It returns the actual number of characters.
1286 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1287 utf32Characters.Resize( characterCount );
1289 // Reset the cursor position
1290 mImpl->mEventData->mPrimaryCursorPosition = 0;
1292 // The natural size needs to be re-calculated.
1293 mImpl->mRecalculateNaturalSize = true;
1295 // Apply modifications to the model
1296 mImpl->mOperationsPending = ALL_OPERATIONS;
1297 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1301 void Controller::ClearModelData()
1303 // n.b. This does not Clear the mText from mLogicalModel
1304 mImpl->mLogicalModel->mScriptRuns.Clear();
1305 mImpl->mLogicalModel->mFontRuns.Clear();
1306 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1307 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1308 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1309 mImpl->mLogicalModel->mCharacterDirections.Clear();
1310 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1311 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1312 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1313 mImpl->mVisualModel->mGlyphs.Clear();
1314 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1315 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1316 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1317 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1318 mImpl->mVisualModel->mGlyphPositions.Clear();
1319 mImpl->mVisualModel->mLines.Clear();
1320 mImpl->mVisualModel->ClearCaches();
1323 void Controller::ClearFontData()
1325 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1326 mImpl->mLogicalModel->mFontRuns.Clear();
1327 mImpl->mVisualModel->mGlyphs.Clear();
1328 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1329 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1330 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1331 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1332 mImpl->mVisualModel->mGlyphPositions.Clear();
1333 mImpl->mVisualModel->mLines.Clear();
1334 mImpl->mVisualModel->ClearCaches();
1337 Controller::Controller( ControlInterface& controlInterface )
1340 mImpl = new Controller::Impl( controlInterface );
1345 } // namespace Toolkit