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 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 ShowPlaceholderText();
167 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
169 if( mImpl->mEventData )
171 if( PLACEHOLDER_TYPE_INACTIVE == type )
173 text = mImpl->mEventData->mPlaceholderTextInactive;
177 text = mImpl->mEventData->mPlaceholderTextActive;
182 void Controller::SetMaximumNumberOfCharacters( int maxCharacters )
184 if ( maxCharacters >= 0 )
186 mImpl->mMaximumNumberOfCharacters = maxCharacters;
190 int Controller::GetMaximumNumberOfCharacters()
192 return mImpl->mMaximumNumberOfCharacters;
195 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
197 if( !mImpl->mFontDefaults )
199 mImpl->mFontDefaults = new FontDefaults();
202 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
204 // Clear the font-specific data
207 mImpl->mOperationsPending = ALL_OPERATIONS;
208 mImpl->mRecalculateNaturalSize = true;
210 mImpl->RequestRelayout();
213 const std::string& Controller::GetDefaultFontFamily() const
215 if( mImpl->mFontDefaults )
217 return mImpl->mFontDefaults->mDefaultFontFamily;
223 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
225 if( !mImpl->mFontDefaults )
227 mImpl->mFontDefaults = new FontDefaults();
230 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
232 // Clear the font-specific data
235 mImpl->mOperationsPending = ALL_OPERATIONS;
236 mImpl->mRecalculateNaturalSize = true;
238 mImpl->RequestRelayout();
241 const std::string& Controller::GetDefaultFontStyle() const
243 if( mImpl->mFontDefaults )
245 return mImpl->mFontDefaults->mDefaultFontStyle;
251 void Controller::SetDefaultPointSize( float pointSize )
253 if( !mImpl->mFontDefaults )
255 mImpl->mFontDefaults = new FontDefaults();
258 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
260 // Clear the font-specific data
263 mImpl->mOperationsPending = ALL_OPERATIONS;
264 mImpl->mRecalculateNaturalSize = true;
266 mImpl->RequestRelayout();
269 float Controller::GetDefaultPointSize() const
271 if( mImpl->mFontDefaults )
273 return mImpl->mFontDefaults->mDefaultPointSize;
279 void Controller::SetTextColor( const Vector4& textColor )
281 mImpl->mTextColor = textColor;
283 if( ! mImpl->IsShowingPlaceholderText() )
285 mImpl->mVisualModel->SetTextColor( textColor );
289 const Vector4& Controller::GetTextColor() const
291 return mImpl->mTextColor;
294 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
296 bool removed( false );
298 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
299 this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
301 if( ! mImpl->IsShowingPlaceholderText() )
303 // Delete at current cursor position
304 Vector<Character>& currentText = mImpl->mLogicalModel->mText;
305 CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
307 CharacterIndex cursorIndex = oldCursorIndex;
309 // Validate the cursor position & number of characters
310 if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
312 cursorIndex = oldCursorIndex + cursorOffset;
315 if( (cursorIndex + numberOfChars) > currentText.Count() )
317 numberOfChars = currentText.Count() - cursorIndex;
320 if( cursorIndex >= 0 &&
321 (cursorIndex + numberOfChars) <= currentText.Count() )
323 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
324 Vector<Character>::Iterator last = first + numberOfChars;
326 currentText.Erase( first, last );
328 // Cursor position retreat
329 oldCursorIndex = cursorIndex;
331 DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
339 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
341 if( mImpl->mEventData )
343 mImpl->mEventData->mPlaceholderTextColor = textColor;
346 if( mImpl->IsShowingPlaceholderText() )
348 mImpl->mVisualModel->SetTextColor( textColor );
352 const Vector4& Controller::GetPlaceholderTextColor() const
354 if( mImpl->mEventData )
356 return mImpl->mEventData->mPlaceholderTextColor;
362 void Controller::SetShadowOffset( const Vector2& shadowOffset )
364 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
367 const Vector2& Controller::GetShadowOffset() const
369 return mImpl->mVisualModel->GetShadowOffset();
372 void Controller::SetShadowColor( const Vector4& shadowColor )
374 mImpl->mVisualModel->SetShadowColor( shadowColor );
377 const Vector4& Controller::GetShadowColor() const
379 return mImpl->mVisualModel->GetShadowColor();
382 void Controller::SetUnderlineColor( const Vector4& color )
384 mImpl->mVisualModel->SetUnderlineColor( color );
387 const Vector4& Controller::GetUnderlineColor() const
389 return mImpl->mVisualModel->GetUnderlineColor();
392 void Controller::SetUnderlineEnabled( bool enabled )
394 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
397 bool Controller::IsUnderlineEnabled() const
399 return mImpl->mVisualModel->IsUnderlineEnabled();
402 void Controller::SetUnderlineHeight( float height )
404 mImpl->mVisualModel->SetUnderlineHeight( height );
407 float Controller::GetUnderlineHeight() const
409 return mImpl->mVisualModel->GetUnderlineHeight();
412 void Controller::SetEnableCursorBlink( bool enable )
414 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
416 if( mImpl->mEventData )
418 mImpl->mEventData->mCursorBlinkEnabled = enable;
421 mImpl->mEventData->mDecorator )
423 mImpl->mEventData->mDecorator->StopCursorBlink();
428 bool Controller::GetEnableCursorBlink() const
430 if( mImpl->mEventData )
432 return mImpl->mEventData->mCursorBlinkEnabled;
438 const Vector2& Controller::GetScrollPosition() const
440 if( mImpl->mEventData )
442 return mImpl->mEventData->mScrollPosition;
445 return Vector2::ZERO;
448 const Vector2& Controller::GetAlignmentOffset() const
450 return mImpl->mAlignmentOffset;
453 Vector3 Controller::GetNaturalSize()
455 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
458 // Make sure the model is up-to-date before layouting
459 ProcessModifyEvents();
461 if( mImpl->mRecalculateNaturalSize )
463 // Operations that can be done only once until the text changes.
464 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
472 // Make sure the model is up-to-date before layouting
473 mImpl->UpdateModel( onlyOnceOperations );
475 // Operations that need to be done if the size changes.
476 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
480 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
481 static_cast<OperationsMask>( onlyOnceOperations |
483 naturalSize.GetVectorXY() );
485 // Do not do again the only once operations.
486 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
488 // Do the size related operations again.
489 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
491 // Stores the natural size to avoid recalculate it again
492 // unless the text/style changes.
493 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
495 mImpl->mRecalculateNaturalSize = false;
497 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
501 naturalSize = mImpl->mVisualModel->GetNaturalSize();
503 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
509 float Controller::GetHeightForWidth( float width )
511 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
512 // Make sure the model is up-to-date before layouting
513 ProcessModifyEvents();
516 if( width != mImpl->mControlSize.width )
518 // Operations that can be done only once until the text changes.
519 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
527 // Make sure the model is up-to-date before layouting
528 mImpl->UpdateModel( onlyOnceOperations );
530 // Operations that need to be done if the size changes.
531 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
535 DoRelayout( Size( width, MAX_FLOAT ),
536 static_cast<OperationsMask>( onlyOnceOperations |
540 // Do not do again the only once operations.
541 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
543 // Do the size related operations again.
544 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
545 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
549 layoutSize = mImpl->mVisualModel->GetActualSize();
550 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
553 return layoutSize.height;
556 bool Controller::Relayout( const Size& size )
558 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
560 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
562 bool glyphsRemoved( false );
563 if( 0u != mImpl->mVisualModel->GetNumberOfGlyphPositions() )
565 mImpl->mVisualModel->SetGlyphPositions( NULL, 0u );
566 glyphsRemoved = true;
568 // Not worth to relayout if width or height is equal to zero.
569 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
570 return glyphsRemoved;
573 if( size != mImpl->mControlSize )
575 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mControlSize.width, mImpl->mControlSize.height );
577 // Operations that need to be done if the size changes.
578 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
584 mImpl->mControlSize = size;
587 // Make sure the model is up-to-date before layouting
588 ProcessModifyEvents();
589 mImpl->UpdateModel( mImpl->mOperationsPending );
592 bool updated = DoRelayout( mImpl->mControlSize,
593 mImpl->mOperationsPending,
596 // Do not re-do any operation until something changes.
597 mImpl->mOperationsPending = NO_OPERATION;
599 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
600 CalculateTextAlignment( size );
602 if( mImpl->mEventData )
604 // Move the cursor, grab handle etc.
605 updated = mImpl->ProcessInputEvents() || updated;
608 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
612 void Controller::ProcessModifyEvents()
614 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
616 for( unsigned int i=0; i<events.size(); ++i )
618 if( ModifyEvent::TEXT_REPLACED == events[0].type )
620 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
621 DALI_ASSERT_DEBUG( 0 == i && "Unexpected TEXT_REPLACED event" );
625 else if( ModifyEvent::TEXT_INSERTED == events[0].type )
629 else if( ModifyEvent::TEXT_DELETED == events[0].type )
631 // Placeholder-text cannot be deleted
632 if( !mImpl->IsShowingPlaceholderText() )
639 // Discard temporary text
643 void Controller::ResetText()
646 mImpl->mLogicalModel->mText.Clear();
649 // Reset the cursor position
650 if( mImpl->mEventData )
652 mImpl->mEventData->mPrimaryCursorPosition = 0;
655 // We have cleared everything including the placeholder-text
656 mImpl->PlaceholderCleared();
658 // The natural size needs to be re-calculated.
659 mImpl->mRecalculateNaturalSize = true;
661 // Apply modifications to the model
662 mImpl->mOperationsPending = ALL_OPERATIONS;
665 void Controller::TextReplacedEvent()
670 // The natural size needs to be re-calculated.
671 mImpl->mRecalculateNaturalSize = true;
673 // Apply modifications to the model
674 mImpl->mOperationsPending = ALL_OPERATIONS;
675 mImpl->UpdateModel( ALL_OPERATIONS );
676 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
682 void Controller::TextInsertedEvent()
684 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
686 // TODO - Optimize this
689 // The natural size needs to be re-calculated.
690 mImpl->mRecalculateNaturalSize = true;
692 // Apply modifications to the model; TODO - Optimize this
693 mImpl->mOperationsPending = ALL_OPERATIONS;
694 mImpl->UpdateModel( ALL_OPERATIONS );
695 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
700 // Queue a cursor reposition event; this must wait until after DoRelayout()
701 mImpl->mEventData->mUpdateCursorPosition = true;
702 mImpl->mEventData->mScrollAfterUpdateCursorPosition = true;
705 void Controller::TextDeletedEvent()
707 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
709 // TODO - Optimize this
712 // The natural size needs to be re-calculated.
713 mImpl->mRecalculateNaturalSize = true;
715 // Apply modifications to the model; TODO - Optimize this
716 mImpl->mOperationsPending = ALL_OPERATIONS;
717 mImpl->UpdateModel( ALL_OPERATIONS );
718 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
723 // Queue a cursor reposition event; this must wait until after DoRelayout()
724 mImpl->mEventData->mUpdateCursorPosition = true;
725 mImpl->mEventData->mScrollAfterUpdateCursorPosition = true;
728 bool Controller::DoRelayout( const Size& size,
729 OperationsMask operationsRequired,
732 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
733 bool viewUpdated( false );
735 // Calculate the operations to be done.
736 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
738 if( LAYOUT & operations )
740 // Some vectors with data needed to layout and reorder may be void
741 // after the first time the text has been laid out.
742 // Fill the vectors again.
744 Length numberOfGlyphs = mImpl->mVisualModel->GetNumberOfGlyphs();
746 if( 0u == numberOfGlyphs )
748 // Nothing else to do if there is no glyphs.
749 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
753 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
754 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
755 Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
756 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
757 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
758 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
760 // Set the layout parameters.
761 LayoutParameters layoutParameters( size,
762 mImpl->mLogicalModel->mText.Begin(),
763 lineBreakInfo.Begin(),
764 wordBreakInfo.Begin(),
765 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
768 glyphsToCharactersMap.Begin(),
769 charactersPerGlyph.Begin() );
771 // The laid-out lines.
772 // It's not possible to know in how many lines the text is going to be laid-out,
773 // but it can be resized at least with the number of 'paragraphs' to avoid
774 // some re-allocations.
775 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
777 // Delete any previous laid out lines before setting the new ones.
780 // The capacity of the bidirectional paragraph info is the number of paragraphs.
781 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
783 // Resize the vector of positions to have the same size than the vector of glyphs.
784 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
785 glyphPositions.Resize( numberOfGlyphs );
787 // Update the visual model.
788 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
796 if( REORDER & operations )
798 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
800 // Check first if there are paragraphs with bidirectional info.
801 if( 0u != bidirectionalInfo.Count() )
804 const Length numberOfLines = mImpl->mVisualModel->GetNumberOfLines();
806 // Reorder the lines.
807 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
808 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
809 ReorderLines( bidirectionalInfo,
811 lineBidirectionalInfoRuns );
813 // Set the bidirectional info into the model.
814 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
815 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
816 numberOfBidirectionalInfoRuns );
818 // Set the bidirectional info per line into the layout parameters.
819 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
820 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
822 // Get the character to glyph conversion table and set into the layout.
823 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
825 // Get the glyphs per character table and set into the layout.
826 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
828 // Re-layout the text. Reorder those lines with right to left characters.
829 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
832 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
833 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
834 endIt = lineBidirectionalInfoRuns.End();
838 BidirectionalLineInfoRun& bidiLineInfo = *it;
840 free( bidiLineInfo.visualToLogicalMap );
845 if( ALIGN & operations )
847 mImpl->mLayoutEngine.Align( layoutParameters,
853 // Sets the actual size.
854 if( UPDATE_ACTUAL_SIZE & operations )
856 mImpl->mVisualModel->SetActualSize( layoutSize );
862 layoutSize = mImpl->mVisualModel->GetActualSize();
865 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
869 void Controller::CalculateTextAlignment( const Size& size )
871 // Get the direction of the first character.
872 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
874 const Size& actualSize = mImpl->mVisualModel->GetActualSize();
876 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
877 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
878 if( firstParagraphDirection &&
879 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
881 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
883 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
887 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
891 switch( horizontalAlignment )
893 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
895 mImpl->mAlignmentOffset.x = 0.f;
898 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
900 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
901 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
904 case LayoutEngine::HORIZONTAL_ALIGN_END:
906 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
911 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
912 switch( verticalAlignment )
914 case LayoutEngine::VERTICAL_ALIGN_TOP:
916 mImpl->mAlignmentOffset.y = 0.f;
919 case LayoutEngine::VERTICAL_ALIGN_CENTER:
921 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
922 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
925 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
927 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
933 LayoutEngine& Controller::GetLayoutEngine()
935 return mImpl->mLayoutEngine;
938 View& Controller::GetView()
943 void Controller::KeyboardFocusGainEvent()
945 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
947 if( mImpl->mEventData )
949 mImpl->ChangeState( EventData::EDITING );
951 if( mImpl->IsShowingPlaceholderText() )
953 // Show alternative placeholder-text when editing
954 ShowPlaceholderText();
957 mImpl->RequestRelayout();
961 void Controller::KeyboardFocusLostEvent()
963 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
965 if( mImpl->mEventData )
967 mImpl->ChangeState( EventData::INACTIVE );
969 if( mImpl->IsShowingPlaceholderText() )
971 // Revert to regular placeholder-text when not editing
972 ShowPlaceholderText();
975 mImpl->RequestRelayout();
979 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
981 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
983 if( mImpl->mEventData &&
984 keyEvent.state == KeyEvent::Down )
986 int keyCode = keyEvent.keyCode;
987 const std::string& keyString = keyEvent.keyPressed;
989 // Pre-process to separate modifying events from non-modifying input events.
990 if( Dali::DALI_KEY_ESCAPE == keyCode )
992 // Escape key is a special case which causes focus loss
993 KeyboardFocusLostEvent();
995 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
996 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
997 Dali::DALI_KEY_CURSOR_UP == keyCode ||
998 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1000 Event event( Event::CURSOR_KEY_EVENT );
1001 event.p1.mInt = keyCode;
1002 mImpl->mEventData->mEventQueue.push_back( event );
1004 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1006 // Remove the character before the current cursor position
1007 bool removed = RemoveText( -1, 1 );
1011 if( 0u == mImpl->mLogicalModel->mText.Count() )
1013 ShowPlaceholderText();
1014 mImpl->mEventData->mUpdateCursorPosition = true;
1018 mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
1024 InsertText( keyString, COMMIT );
1027 mImpl->ChangeState( EventData::EDITING ); // todo Confirm this is the best place to change the state of
1029 mImpl->RequestRelayout();
1035 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1037 bool removedPreEdit( false );
1038 bool maxLengthReached( false );
1040 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1041 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPreEditFlag %d cursor %d\n",
1042 this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"), mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPrimaryCursorPosition );
1044 if( ! text.empty() )
1046 if( mImpl->IsShowingPlaceholderText() )
1052 if( mImpl->mEventData )
1054 if( COMMIT == type )
1056 mImpl->mEventData->mPreEditFlag = false;
1060 if( mImpl->mEventData->mPreEditFlag &&
1061 0 != mImpl->mEventData->mPreEditLength )
1063 // Remove previous pre-edit text
1064 mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1065 removedPreEdit = RemoveText( -1, mImpl->mEventData->mPreEditLength );
1069 // Record the start of the pre-edit text
1070 mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1071 mImpl->mEventData->mPreEditLength = text.size();
1073 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1076 mImpl->mEventData->mPreEditFlag = true;
1080 if( ! text.empty() )
1082 // Convert text into UTF-32
1083 Vector<Character> utf32Characters;
1084 utf32Characters.Resize( text.size() );
1086 // This is a bit horrible but std::string returns a (signed) char*
1087 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1089 // Transform a text array encoded in utf8 into an array encoded in utf32.
1090 // It returns the actual number of characters.
1091 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1092 utf32Characters.Resize( characterCount );
1094 DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1095 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1097 const Length numberOfCharactersInModel = mImpl->mLogicalModel->GetNumberOfCharacters();
1099 // Restrict new text to fit within Maximum characters setting
1100 Length maxSizeOfNewText = std::min ( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1101 maxLengthReached = ( characterCount > maxSizeOfNewText );
1103 // Insert at current cursor position
1104 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1106 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1108 if( cursorIndex < numberOfCharactersInModel )
1110 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1114 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1117 cursorIndex += maxSizeOfNewText;
1119 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1122 if( removedPreEdit || !text.empty() )
1124 // Queue an inserted event
1125 mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1128 if( maxLengthReached )
1130 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1132 mImpl->mControlInterface.MaxLengthReached();
1134 mImpl->PreEditReset();
1138 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1140 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1142 if( NULL != mImpl->mEventData )
1144 if( 1u == tapCount )
1146 bool tapDuringEditMode( EventData::EDITING == mImpl->mEventData->mState );
1148 if( ! mImpl->IsShowingPlaceholderText() &&
1149 EventData::EDITING == mImpl->mEventData->mState )
1151 // Grab handle is not shown until a tap is received whilst EDITING
1152 if( tapDuringEditMode )
1154 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, true );
1156 mImpl->mEventData->mDecorator->SetPopupActive( false );
1159 mImpl->ChangeState( EventData::EDITING );
1161 else if( mImpl->mEventData->mSelectionEnabled &&
1162 ( 2u == tapCount ) )
1164 mImpl->ChangeState( EventData::SELECTING );
1168 // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
1169 if( mImpl->mEventData )
1171 Event event( Event::TAP_EVENT );
1172 event.p1.mUint = tapCount;
1173 event.p2.mFloat = x;
1174 event.p3.mFloat = y;
1175 mImpl->mEventData->mEventQueue.push_back( event );
1177 mImpl->RequestRelayout();
1180 // Reset keyboard as tap event has occurred.
1181 mImpl->PreEditReset();
1184 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1186 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1188 if( mImpl->mEventData )
1190 Event event( Event::PAN_EVENT );
1191 event.p1.mInt = state;
1192 event.p2.mFloat = displacement.x;
1193 event.p3.mFloat = displacement.y;
1194 mImpl->mEventData->mEventQueue.push_back( event );
1196 mImpl->RequestRelayout();
1200 void Controller::HandleEvent( HandleType handleType, HandleState state, float x, float y )
1202 DALI_ASSERT_DEBUG( mImpl->mEventData && "Controller::HandleEvent. Unexpected HandleEvent" );
1204 if( mImpl->mEventData )
1206 switch( handleType )
1210 Event event( Event::GRAB_HANDLE_EVENT );
1211 event.p1.mUint = state;
1212 event.p2.mFloat = x;
1213 event.p3.mFloat = y;
1215 mImpl->mEventData->mEventQueue.push_back( event );
1218 case LEFT_SELECTION_HANDLE:
1220 Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
1221 event.p1.mUint = state;
1222 event.p2.mFloat = x;
1223 event.p3.mFloat = y;
1225 mImpl->mEventData->mEventQueue.push_back( event );
1228 case RIGHT_SELECTION_HANDLE:
1230 Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
1231 event.p1.mUint = state;
1232 event.p2.mFloat = x;
1233 event.p3.mFloat = y;
1235 mImpl->mEventData->mEventQueue.push_back( event );
1238 case HANDLE_TYPE_COUNT:
1240 DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
1244 mImpl->RequestRelayout();
1248 Controller::~Controller()
1253 void Controller::ShowPlaceholderText()
1255 if( mImpl->IsPlaceholderAvailable() )
1257 DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
1259 mImpl->mEventData->mIsShowingPlaceholderText = true;
1261 // Cancel previously queued inserts etc.
1262 mImpl->mModifyEvents.clear();
1264 // Disable handles when showing place-holder text
1265 mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
1266 mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
1267 mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
1269 const char* text( NULL );
1272 // TODO - Switch placeholder text styles when changing state
1273 if( EventData::INACTIVE != mImpl->mEventData->mState &&
1274 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() )
1276 text = mImpl->mEventData->mPlaceholderTextActive.c_str();
1277 size = mImpl->mEventData->mPlaceholderTextActive.size();
1281 text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
1282 size = mImpl->mEventData->mPlaceholderTextInactive.size();
1285 // Reset model for showing placeholder.
1286 mImpl->mLogicalModel->mText.Clear();
1288 mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
1290 // Convert text into UTF-32
1291 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
1292 utf32Characters.Resize( size );
1294 // This is a bit horrible but std::string returns a (signed) char*
1295 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
1297 // Transform a text array encoded in utf8 into an array encoded in utf32.
1298 // It returns the actual number of characters.
1299 Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
1300 utf32Characters.Resize( characterCount );
1302 // Reset the cursor position
1303 mImpl->mEventData->mPrimaryCursorPosition = 0;
1305 // The natural size needs to be re-calculated.
1306 mImpl->mRecalculateNaturalSize = true;
1308 // Apply modifications to the model
1309 mImpl->mOperationsPending = ALL_OPERATIONS;
1310 mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
1314 void Controller::ClearModelData()
1316 // n.b. This does not Clear the mText from mLogicalModel
1317 mImpl->mLogicalModel->mScriptRuns.Clear();
1318 mImpl->mLogicalModel->mFontRuns.Clear();
1319 mImpl->mLogicalModel->mLineBreakInfo.Clear();
1320 mImpl->mLogicalModel->mWordBreakInfo.Clear();
1321 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
1322 mImpl->mLogicalModel->mCharacterDirections.Clear();
1323 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
1324 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
1325 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
1326 mImpl->mVisualModel->mGlyphs.Clear();
1327 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1328 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1329 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1330 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1331 mImpl->mVisualModel->mGlyphPositions.Clear();
1332 mImpl->mVisualModel->mLines.Clear();
1333 mImpl->mVisualModel->ClearCaches();
1336 void Controller::ClearFontData()
1338 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
1339 mImpl->mLogicalModel->mFontRuns.Clear();
1340 mImpl->mVisualModel->mGlyphs.Clear();
1341 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
1342 mImpl->mVisualModel->mCharactersToGlyph.Clear();
1343 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
1344 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
1345 mImpl->mVisualModel->mGlyphPositions.Clear();
1346 mImpl->mVisualModel->mLines.Clear();
1347 mImpl->mVisualModel->ClearCaches();
1350 Controller::Controller( ControlInterface& controlInterface )
1353 mImpl = new Controller::Impl( controlInterface );
1358 } // namespace Toolkit