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->ResetImfManager();
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 Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
136 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
140 unsigned int Controller::GetLogicalCursorPosition() const
142 if( mImpl->mEventData )
144 return mImpl->mEventData->mPrimaryCursorPosition;
150 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
152 if( mImpl->mEventData )
154 if( PLACEHOLDER_TYPE_INACTIVE == type )
156 mImpl->mEventData->mPlaceholderTextInactive = text;
160 mImpl->mEventData->mPlaceholderTextActive = text;
163 // Update placeholder if there is no text
164 if( mImpl->IsShowingPlaceholderText() ||
165 0u == mImpl->mLogicalModel->mText.Count() )
167 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()
460 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
463 // Make sure the model is up-to-date before layouting
464 ProcessModifyEvents();
466 if( mImpl->mRecalculateNaturalSize )
468 // Operations that can be done only once until the text changes.
469 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
477 // Make sure the model is up-to-date before layouting
478 mImpl->UpdateModel( onlyOnceOperations );
480 // Operations that need to be done if the size changes.
481 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
485 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
486 static_cast<OperationsMask>( onlyOnceOperations |
488 naturalSize.GetVectorXY() );
490 // Do not do again the only once operations.
491 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
493 // Do the size related operations again.
494 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
496 // Stores the natural size to avoid recalculate it again
497 // unless the text/style changes.
498 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
500 mImpl->mRecalculateNaturalSize = false;
502 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
506 naturalSize = mImpl->mVisualModel->GetNaturalSize();
508 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
514 float Controller::GetHeightForWidth( float width )
516 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
517 // Make sure the model is up-to-date before layouting
518 ProcessModifyEvents();
521 if( width != mImpl->mControlSize.width )
523 // Operations that can be done only once until the text changes.
524 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
532 // Make sure the model is up-to-date before layouting
533 mImpl->UpdateModel( onlyOnceOperations );
535 // Operations that need to be done if the size changes.
536 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
540 DoRelayout( Size( width, MAX_FLOAT ),
541 static_cast<OperationsMask>( onlyOnceOperations |
545 // Do not do again the only once operations.
546 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
548 // Do the size related operations again.
549 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
550 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
554 layoutSize = mImpl->mVisualModel->GetActualSize();
555 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
558 return layoutSize.height;
561 bool Controller::Relayout( const Size& size )
563 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
565 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
567 bool glyphsRemoved( false );
568 if( 0u != mImpl->mVisualModel->GetNumberOfGlyphPositions() )
570 mImpl->mVisualModel->SetGlyphPositions( NULL, 0u );
571 glyphsRemoved = true;
573 // Not worth to relayout if width or height is equal to zero.
574 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
575 return glyphsRemoved;
578 if( size != mImpl->mControlSize )
580 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mControlSize.width, mImpl->mControlSize.height );
582 // Operations that need to be done if the size changes.
583 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
589 mImpl->mControlSize = size;
592 // Make sure the model is up-to-date before layouting
593 ProcessModifyEvents();
594 mImpl->UpdateModel( mImpl->mOperationsPending );
597 bool updated = DoRelayout( mImpl->mControlSize,
598 mImpl->mOperationsPending,
601 // Do not re-do any operation until something changes.
602 mImpl->mOperationsPending = NO_OPERATION;
604 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
605 CalculateTextAlignment( size );
607 if( mImpl->mEventData )
609 // Move the cursor, grab handle etc.
610 updated = mImpl->ProcessInputEvents() || updated;
613 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
617 void Controller::ProcessModifyEvents()
619 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
621 for( unsigned int i=0; i<events.size(); ++i )
623 if( ModifyEvent::TEXT_REPLACED == events[0].type )
625 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
626 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
630 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
634 else if( ModifyEvent::TEXT_DELETED == events[0].type )
636 // Placeholder-text cannot be deleted
637 if( !mImpl->IsShowingPlaceholderText() )
644 // Discard temporary text
648 void Controller::ResetText()
651 mImpl->mLogicalModel->mText.Clear();
654 // Reset the cursor position
655 if( mImpl->mEventData )
657 mImpl->mEventData->mPrimaryCursorPosition = 0;
660 // We have cleared everything including the placeholder-text
661 mImpl->PlaceholderCleared();
663 // The natural size needs to be re-calculated.
664 mImpl->mRecalculateNaturalSize = true;
666 // Apply modifications to the model
667 mImpl->mOperationsPending = ALL_OPERATIONS;
670 void Controller::TextReplacedEvent()
675 // The natural size needs to be re-calculated.
676 mImpl->mRecalculateNaturalSize = true;
678 // Apply modifications to the model
679 mImpl->mOperationsPending = ALL_OPERATIONS;
680 mImpl->UpdateModel( ALL_OPERATIONS );
681 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
687 void Controller::TextInsertedEvent()
689 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
691 // TODO - Optimize this
694 // The natural size needs to be re-calculated.
695 mImpl->mRecalculateNaturalSize = true;
697 // Apply modifications to the model; TODO - Optimize this
698 mImpl->mOperationsPending = ALL_OPERATIONS;
699 mImpl->UpdateModel( ALL_OPERATIONS );
700 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
705 // Queue a cursor reposition event; this must wait until after DoRelayout()
706 mImpl->mEventData->mUpdateCursorPosition = true;
707 mImpl->mEventData->mScrollAfterUpdateCursorPosition = true;
710 void Controller::TextDeletedEvent()
712 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
714 // TODO - Optimize this
717 // The natural size needs to be re-calculated.
718 mImpl->mRecalculateNaturalSize = true;
720 // Apply modifications to the model; TODO - Optimize this
721 mImpl->mOperationsPending = ALL_OPERATIONS;
722 mImpl->UpdateModel( ALL_OPERATIONS );
723 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
728 // Queue a cursor reposition event; this must wait until after DoRelayout()
729 mImpl->mEventData->mUpdateCursorPosition = true;
730 mImpl->mEventData->mScrollAfterUpdateCursorPosition = true;
733 bool Controller::DoRelayout( const Size& size,
734 OperationsMask operationsRequired,
737 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
738 bool viewUpdated( false );
740 // Calculate the operations to be done.
741 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
743 if( LAYOUT & operations )
745 // Some vectors with data needed to layout and reorder may be void
746 // after the first time the text has been laid out.
747 // Fill the vectors again.
749 Length numberOfGlyphs = mImpl->mVisualModel->GetNumberOfGlyphs();
751 if( 0u == numberOfGlyphs )
753 // Nothing else to do if there is no glyphs.
754 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
758 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
759 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
760 Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
761 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
762 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
763 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
765 // Set the layout parameters.
766 LayoutParameters layoutParameters( size,
767 mImpl->mLogicalModel->mText.Begin(),
768 lineBreakInfo.Begin(),
769 wordBreakInfo.Begin(),
770 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
773 glyphsToCharactersMap.Begin(),
774 charactersPerGlyph.Begin() );
776 // The laid-out lines.
777 // It's not possible to know in how many lines the text is going to be laid-out,
778 // but it can be resized at least with the number of 'paragraphs' to avoid
779 // some re-allocations.
780 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
782 // Delete any previous laid out lines before setting the new ones.
785 // The capacity of the bidirectional paragraph info is the number of paragraphs.
786 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
788 // Resize the vector of positions to have the same size than the vector of glyphs.
789 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
790 glyphPositions.Resize( numberOfGlyphs );
792 // Update the visual model.
793 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
801 if( REORDER & operations )
803 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
805 // Check first if there are paragraphs with bidirectional info.
806 if( 0u != bidirectionalInfo.Count() )
809 const Length numberOfLines = mImpl->mVisualModel->GetNumberOfLines();
811 // Reorder the lines.
812 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
813 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
814 ReorderLines( bidirectionalInfo,
816 lineBidirectionalInfoRuns );
818 // Set the bidirectional info into the model.
819 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
820 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
821 numberOfBidirectionalInfoRuns );
823 // Set the bidirectional info per line into the layout parameters.
824 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
825 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
827 // Get the character to glyph conversion table and set into the layout.
828 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
830 // Get the glyphs per character table and set into the layout.
831 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
833 // Re-layout the text. Reorder those lines with right to left characters.
834 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
837 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
838 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
839 endIt = lineBidirectionalInfoRuns.End();
843 BidirectionalLineInfoRun& bidiLineInfo = *it;
845 free( bidiLineInfo.visualToLogicalMap );
850 if( ALIGN & operations )
852 mImpl->mLayoutEngine.Align( layoutParameters,
858 // Sets the actual size.
859 if( UPDATE_ACTUAL_SIZE & operations )
861 mImpl->mVisualModel->SetActualSize( layoutSize );
867 layoutSize = mImpl->mVisualModel->GetActualSize();
870 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
874 void Controller::CalculateTextAlignment( const Size& size )
876 // Get the direction of the first character.
877 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
879 const Size& actualSize = mImpl->mVisualModel->GetActualSize();
881 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
882 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
883 if( firstParagraphDirection &&
884 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
886 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
888 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
892 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
896 switch( horizontalAlignment )
898 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
900 mImpl->mAlignmentOffset.x = 0.f;
903 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
905 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
906 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
909 case LayoutEngine::HORIZONTAL_ALIGN_END:
911 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
916 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
917 switch( verticalAlignment )
919 case LayoutEngine::VERTICAL_ALIGN_TOP:
921 mImpl->mAlignmentOffset.y = 0.f;
924 case LayoutEngine::VERTICAL_ALIGN_CENTER:
926 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
927 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
930 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
932 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
938 LayoutEngine& Controller::GetLayoutEngine()
940 return mImpl->mLayoutEngine;
943 View& Controller::GetView()
948 void Controller::KeyboardFocusGainEvent()
950 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
952 if( mImpl->mEventData )
954 mImpl->ChangeState( EventData::EDITING );
956 if( mImpl->IsShowingPlaceholderText() )
958 // Show alternative placeholder-text when editing
959 ShowPlaceholderText();
962 mImpl->RequestRelayout();
966 void Controller::KeyboardFocusLostEvent()
968 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
970 if( mImpl->mEventData )
972 mImpl->ChangeState( EventData::INACTIVE );
974 if( mImpl->IsShowingPlaceholderText() )
976 // Revert to regular placeholder-text when not editing
977 ShowPlaceholderText();
980 mImpl->RequestRelayout();
984 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
986 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
988 if( mImpl->mEventData &&
989 keyEvent.state == KeyEvent::Down )
991 int keyCode = keyEvent.keyCode;
992 const std::string& keyString = keyEvent.keyPressed;
994 // Pre-process to separate modifying events from non-modifying input events.
995 if( Dali::DALI_KEY_ESCAPE == keyCode )
997 // Escape key is a special case which causes focus loss
998 KeyboardFocusLostEvent();
1000 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1001 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1002 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1003 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1005 Event event( Event::CURSOR_KEY_EVENT );
1006 event.p1.mInt = keyCode;
1007 mImpl->mEventData->mEventQueue.push_back( event );
1009 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1011 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
1013 // IMF manager is no longer handling key-events
1014 mImpl->ClearPreEditFlag();
1016 // Remove the character before the current cursor position
1017 bool removed = RemoveText( -1, 1 );
1021 if( 0u == mImpl->mLogicalModel->mText.Count() )
1023 ShowPlaceholderText();
1024 mImpl->mEventData->mUpdateCursorPosition = true;
1028 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1034 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1036 // IMF manager is no longer handling key-events
1037 mImpl->ClearPreEditFlag();
1039 InsertText( keyString, COMMIT );
1042 mImpl->ChangeState( EventData::EDITING ); // todo Confirm this is the best place to change the state of
1044 mImpl->RequestRelayout();
1050 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1052 bool removedPreEdit( false );
1053 bool maxLengthReached( false );
1055 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1056 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1057 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1058 mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1060 Vector<Character> utf32Characters;
1061 Length characterCount( 0u );
1063 if( ! text.empty() )
1065 // The placeholder text is no longer needed
1066 if( mImpl->IsShowingPlaceholderText() )
1071 // Convert text into UTF-32
1072 utf32Characters.Resize( text.size() );
1074 // This is a bit horrible but std::string returns a (signed) char*
1075 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1077 // Transform a text array encoded in utf8 into an array encoded in utf32.
1078 // It returns the actual number of characters.
1079 characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1080 utf32Characters.Resize( characterCount );
1082 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1083 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1086 if( 0u != utf32Characters.Count() )
1088 // Handle the IMF (predicitive text) state changes
1089 if( mImpl->mEventData )
1091 if( mImpl->mEventData->mPreEditFlag &&
1092 0 != mImpl->mEventData->mPreEditLength )
1094 // Remove previous pre-edit text
1095 CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1096 removedPreEdit = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1098 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1099 mImpl->mEventData->mPreEditLength = 0;
1102 if( COMMIT == type )
1104 // IMF manager is no longer handling key-events
1105 mImpl->ClearPreEditFlag();
1109 if( ! mImpl->mEventData->mPreEditFlag )
1111 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1113 // Record the start of the pre-edit text
1114 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1117 mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1118 mImpl->mEventData->mPreEditFlag = true;
1120 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1124 const Length numberOfCharactersInModel = mImpl->mLogicalModel->GetNumberOfCharacters();
1126 // Restrict new text to fit within Maximum characters setting
1127 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1128 maxLengthReached = ( characterCount > maxSizeOfNewText );
1130 // Insert at current cursor position
1131 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1133 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1135 if( cursorIndex < numberOfCharactersInModel )
1137 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1141 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1144 cursorIndex += maxSizeOfNewText;
1146 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1149 if( removedPreEdit ||
1150 0 != utf32Characters.Count() )
1152 // Queue an inserted event
1153 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1156 if( maxLengthReached )
1158 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1160 mImpl->mControlInterface.MaxLengthReached();
1162 mImpl->ResetImfManager();
1166 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1168 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1170 if( NULL != mImpl->mEventData )
1172 if( 1u == tapCount )
1174 bool tapDuringEditMode( EventData::EDITING == mImpl->mEventData->mState );
1176 if( ! mImpl->IsShowingPlaceholderText() &&
1177 EventData::EDITING == mImpl->mEventData->mState )
1179 // Grab handle is not shown until a tap is received whilst EDITING
1180 if( tapDuringEditMode )
1182 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
1184 mImpl->mEventData->mDecorator->SetPopupActive( false );
1187 mImpl->ChangeState( EventData::EDITING );
1189 else if( mImpl->mEventData->mSelectionEnabled &&
1190 ( 2u == tapCount ) )
1192 mImpl->ChangeState( EventData::SELECTING );
1196 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1197 if( mImpl->mEventData )
1199 Event event( Event::TAP_EVENT );
1200 event.p1.mUint = tapCount;
1201 event.p2.mFloat = x;
1202 event.p3.mFloat = y;
1203 mImpl->mEventData->mEventQueue.push_back( event );
1205 mImpl->RequestRelayout();
1208 // Reset keyboard as tap event has occurred.
1209 mImpl->ResetImfManager();
1212 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1214 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1216 if( mImpl->mEventData )
1218 Event event( Event::PAN_EVENT );
1219 event.p1.mInt = state;
1220 event.p2.mFloat = displacement.x;
1221 event.p3.mFloat = displacement.y;
1222 mImpl->mEventData->mEventQueue.push_back( event );
1224 mImpl->RequestRelayout();
1228 void Controller::HandleEvent( HandleType handleType, HandleState state, float x, float y )
1230 DALI_ASSERT_DEBUG( mImpl->mEventData && "Controller::HandleEvent. Unexpected HandleEvent" );
1232 if( mImpl->mEventData )
1234 switch( handleType )
1238 Event event( Event::GRAB_HANDLE_EVENT );
1239 event.p1.mUint = state;
1240 event.p2.mFloat = x;
1241 event.p3.mFloat = y;
1243 mImpl->mEventData->mEventQueue.push_back( event );
1246 case LEFT_SELECTION_HANDLE:
1248 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1249 event.p1.mUint = state;
1250 event.p2.mFloat = x;
1251 event.p3.mFloat = y;
1253 mImpl->mEventData->mEventQueue.push_back( event );
1256 case RIGHT_SELECTION_HANDLE:
1258 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1259 event.p1.mUint = state;
1260 event.p2.mFloat = x;
1261 event.p3.mFloat = y;
1263 mImpl->mEventData->mEventQueue.push_back( event );
1266 case HANDLE_TYPE_COUNT:
1268 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1272 mImpl->RequestRelayout();
1276 Controller::~Controller()
1281 void Controller::ShowPlaceholderText()
1283 if( mImpl->IsPlaceholderAvailable() )
1285 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1287 mImpl->mEventData->mIsShowingPlaceholderText = true;
1289 // Cancel previously queued inserts etc.
1290 mImpl->mModifyEvents.clear();
1292 // Disable handles when showing place-holder text
1293 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1294 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1295 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1297 const char* text( NULL );
1300 // TODO - Switch placeholder text styles when changing state
1301 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1302 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1304 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1305 size = mImpl->mEventData->mPlaceholderTextActive.size();
1309 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1310 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1313 // Reset model for showing placeholder.
1314 mImpl->mLogicalModel->mText.Clear();
1316 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1318 // Convert text into UTF-32
1319 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1320 utf32Characters.Resize( size );
1322 // This is a bit horrible but std::string returns a (signed) char*
1323 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1325 // Transform a text array encoded in utf8 into an array encoded in utf32.
1326 // It returns the actual number of characters.
1327 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1328 utf32Characters.Resize( characterCount );
1330 // Reset the cursor position
1331 mImpl->mEventData->mPrimaryCursorPosition = 0;
1333 // The natural size needs to be re-calculated.
1334 mImpl->mRecalculateNaturalSize = true;
1336 // Apply modifications to the model
1337 mImpl->mOperationsPending = ALL_OPERATIONS;
1338 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1342 void Controller::ClearModelData()
1344 // n.b. This does not Clear the mText from mLogicalModel
1345 mImpl->mLogicalModel->mScriptRuns.Clear();
1346 mImpl->mLogicalModel->mFontRuns.Clear();
1347 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1348 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1349 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1350 mImpl->mLogicalModel->mCharacterDirections.Clear();
1351 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1352 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1353 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1354 mImpl->mVisualModel->mGlyphs.Clear();
1355 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1356 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1357 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1358 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1359 mImpl->mVisualModel->mGlyphPositions.Clear();
1360 mImpl->mVisualModel->mLines.Clear();
1361 mImpl->mVisualModel->ClearCaches();
1364 void Controller::ClearFontData()
1366 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1367 mImpl->mLogicalModel->mFontRuns.Clear();
1368 mImpl->mVisualModel->mGlyphs.Clear();
1369 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1370 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1371 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1372 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1373 mImpl->mVisualModel->mGlyphPositions.Clear();
1374 mImpl->mVisualModel->mLines.Clear();
1375 mImpl->mVisualModel->ClearCaches();
1378 Controller::Controller( ControlInterface& controlInterface )
1381 mImpl = new Controller::Impl( controlInterface );
1386 } // namespace Toolkit