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>
23 #include <dali/public-api/adaptor-framework/key.h>
26 #include <dali-toolkit/internal/text/bidirectional-support.h>
27 #include <dali-toolkit/internal/text/character-set-conversion.h>
28 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
29 #include <dali-toolkit/internal/text/multi-language-support.h>
30 #include <dali-toolkit/internal/text/script-run.h>
31 #include <dali-toolkit/internal/text/segmentation.h>
32 #include <dali-toolkit/internal/text/shaper.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
34 #include <dali-toolkit/internal/text/text-io.h>
35 #include <dali-toolkit/internal/text/text-view.h>
40 const float MAX_FLOAT = std::numeric_limits<float>::max();
42 const std::string EMPTY_STRING("");
55 ControllerPtr Controller::New( ControlInterface& controlInterface )
57 return ControllerPtr( new Controller( controlInterface ) );
60 void Controller::SetText( const std::string& text )
62 // Cancel previously queued inserts etc.
63 mImpl->mModifyEvents.clear();
65 // Keep until size negotiation
67 event.type = ModifyEvent::REPLACE_TEXT;
69 mImpl->mModifyEvents.push_back( event );
71 if( mImpl->mEventData )
73 // Cancel previously queued events
74 mImpl->mEventData->mEventQueue.clear();
76 // TODO - Hide selection decorations
80 void Controller::GetText( std::string& text ) const
82 if( !mImpl->mModifyEvents.empty() &&
83 ModifyEvent::REPLACE_TEXT == mImpl->mModifyEvents[0].type )
85 text = mImpl->mModifyEvents[0].text;
89 // TODO - Convert from UTF-32
93 void Controller::SetPlaceholderText( const std::string& text )
95 if( !mImpl->mEventData )
97 mImpl->mEventData->mPlaceholderText = text;
101 void Controller::GetPlaceholderText( std::string& text ) const
103 if( !mImpl->mEventData )
105 text = mImpl->mEventData->mPlaceholderText;
109 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
111 if( !mImpl->mFontDefaults )
113 mImpl->mFontDefaults = new FontDefaults();
116 mImpl->mFontDefaults->mDefaultFontFamily = defaultFontFamily;
117 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
118 mImpl->mOperationsPending = ALL_OPERATIONS;
119 mImpl->mRecalculateNaturalSize = true;
121 // Clear the font-specific data
122 mImpl->mLogicalModel->mFontRuns.Clear();
123 mImpl->mVisualModel->mGlyphs.Clear();
124 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
125 mImpl->mVisualModel->mCharactersToGlyph.Clear();
126 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
127 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
128 mImpl->mVisualModel->mGlyphPositions.Clear();
129 mImpl->mVisualModel->mLines.Clear();
130 mImpl->mVisualModel->ClearCaches();
132 mImpl->RequestRelayout();
135 const std::string& Controller::GetDefaultFontFamily() const
137 if( mImpl->mFontDefaults )
139 return mImpl->mFontDefaults->mDefaultFontFamily;
145 void Controller::SetDefaultFontStyle( const std::string& defaultFontStyle )
147 if( !mImpl->mFontDefaults )
149 mImpl->mFontDefaults = new FontDefaults();
152 mImpl->mFontDefaults->mDefaultFontStyle = defaultFontStyle;
153 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
154 mImpl->mOperationsPending = ALL_OPERATIONS;
155 mImpl->mRecalculateNaturalSize = true;
157 // Clear the font-specific data
158 mImpl->mLogicalModel->mFontRuns.Clear();
159 mImpl->mVisualModel->mGlyphs.Clear();
160 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
161 mImpl->mVisualModel->mCharactersToGlyph.Clear();
162 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
163 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
164 mImpl->mVisualModel->mGlyphPositions.Clear();
165 mImpl->mVisualModel->mLines.Clear();
166 mImpl->mVisualModel->ClearCaches();
168 mImpl->RequestRelayout();
171 const std::string& Controller::GetDefaultFontStyle() const
173 if( mImpl->mFontDefaults )
175 return mImpl->mFontDefaults->mDefaultFontStyle;
181 void Controller::SetDefaultPointSize( float pointSize )
183 if( !mImpl->mFontDefaults )
185 mImpl->mFontDefaults = new FontDefaults();
188 mImpl->mFontDefaults->mDefaultPointSize = pointSize;
189 mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
190 mImpl->mOperationsPending = ALL_OPERATIONS;
191 mImpl->mRecalculateNaturalSize = true;
193 // Clear the font-specific data
194 mImpl->mLogicalModel->mFontRuns.Clear();
195 mImpl->mVisualModel->mGlyphs.Clear();
196 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
197 mImpl->mVisualModel->mCharactersToGlyph.Clear();
198 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
199 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
200 mImpl->mVisualModel->mGlyphPositions.Clear();
201 mImpl->mVisualModel->mLines.Clear();
202 mImpl->mVisualModel->ClearCaches();
204 mImpl->RequestRelayout();
207 float Controller::GetDefaultPointSize() const
209 if( mImpl->mFontDefaults )
211 return mImpl->mFontDefaults->mDefaultPointSize;
217 void Controller::GetDefaultFonts( Vector<FontRun>& fonts, Length numberOfCharacters ) const
219 if( mImpl->mFontDefaults )
222 fontRun.characterRun.characterIndex = 0;
223 fontRun.characterRun.numberOfCharacters = numberOfCharacters;
224 fontRun.fontId = mImpl->mFontDefaults->GetFontId( mImpl->mFontClient );
225 fontRun.isDefault = true;
227 fonts.PushBack( fontRun );
231 void Controller::SetTextColor( const Vector4& textColor )
233 mImpl->mVisualModel->SetTextColor( textColor );
236 const Vector4& Controller::GetTextColor() const
238 return mImpl->mVisualModel->GetTextColor();
241 void Controller::SetShadowOffset( const Vector2& shadowOffset )
243 mImpl->mVisualModel->SetShadowOffset( shadowOffset );
246 const Vector2& Controller::GetShadowOffset() const
248 return mImpl->mVisualModel->GetShadowOffset();
251 void Controller::SetShadowColor( const Vector4& shadowColor )
253 mImpl->mVisualModel->SetShadowColor( shadowColor );
256 const Vector4& Controller::GetShadowColor() const
258 return mImpl->mVisualModel->GetShadowColor();
261 void Controller::SetUnderlineColor( const Vector4& color )
263 mImpl->mVisualModel->SetUnderlineColor( color );
266 const Vector4& Controller::GetUnderlineColor() const
268 return mImpl->mVisualModel->GetUnderlineColor();
271 void Controller::SetUnderlineEnabled( bool enabled )
273 mImpl->mVisualModel->SetUnderlineEnabled( enabled );
276 bool Controller::IsUnderlineEnabled() const
278 return mImpl->mVisualModel->IsUnderlineEnabled();
281 void Controller::SetUnderlineHeight( float height )
283 mImpl->mVisualModel->SetUnderlineHeight( height );
286 float Controller::GetUnderlineHeight() const
288 return mImpl->mVisualModel->GetUnderlineHeight();
291 void Controller::EnableTextInput( DecoratorPtr decorator )
293 if( !mImpl->mEventData )
295 mImpl->mEventData = new EventData( decorator );
299 void Controller::SetEnableCursorBlink( bool enable )
301 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
303 if( mImpl->mEventData )
305 mImpl->mEventData->mCursorBlinkEnabled = enable;
308 mImpl->mEventData->mDecorator )
310 mImpl->mEventData->mDecorator->StopCursorBlink();
315 bool Controller::GetEnableCursorBlink() const
317 if( mImpl->mEventData )
319 return mImpl->mEventData->mCursorBlinkEnabled;
325 const Vector2& Controller::GetScrollPosition() const
327 if( mImpl->mEventData )
329 return mImpl->mEventData->mScrollPosition;
332 return Vector2::ZERO;
335 const Vector2& Controller::GetAlignmentOffset() const
337 return mImpl->mAlignmentOffset;
340 Vector3 Controller::GetNaturalSize()
344 // Make sure the model is up-to-date before layouting
345 ProcessModifyEvents();
347 if( mImpl->mRecalculateNaturalSize )
349 // Operations that can be done only once until the text changes.
350 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
358 // Make sure the model is up-to-date before layouting
359 UpdateModel( onlyOnceOperations );
361 // Operations that need to be done if the size changes.
362 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
366 DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
367 static_cast<OperationsMask>( onlyOnceOperations |
369 naturalSize.GetVectorXY() );
371 // Do not do again the only once operations.
372 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
374 // Do the size related operations again.
375 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
377 // Stores the natural size to avoid recalculate it again
378 // unless the text/style changes.
379 mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
381 mImpl->mRecalculateNaturalSize = false;
385 naturalSize = mImpl->mVisualModel->GetNaturalSize();
391 float Controller::GetHeightForWidth( float width )
393 // Make sure the model is up-to-date before layouting
394 ProcessModifyEvents();
397 if( width != mImpl->mControlSize.width )
399 // Operations that can be done only once until the text changes.
400 const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32 |
408 // Make sure the model is up-to-date before layouting
409 UpdateModel( onlyOnceOperations );
411 // Operations that need to be done if the size changes.
412 const OperationsMask sizeOperations = static_cast<OperationsMask>( LAYOUT |
416 DoRelayout( Size( width, MAX_FLOAT ),
417 static_cast<OperationsMask>( onlyOnceOperations |
421 // Do not do again the only once operations.
422 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
424 // Do the size related operations again.
425 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
429 layoutSize = mImpl->mVisualModel->GetActualSize();
432 return layoutSize.height;
435 bool Controller::Relayout( const Size& size )
437 if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
439 bool glyphsRemoved( false );
440 if( 0u != mImpl->mVisualModel->GetNumberOfGlyphPositions() )
442 mImpl->mVisualModel->SetGlyphPositions( NULL, 0u );
443 glyphsRemoved = true;
445 // Not worth to relayout if width or height is equal to zero.
446 return glyphsRemoved;
449 if( size != mImpl->mControlSize )
451 // Operations that need to be done if the size changes.
452 mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
458 mImpl->mControlSize = size;
461 // Make sure the model is up-to-date before layouting
462 ProcessModifyEvents();
463 UpdateModel( mImpl->mOperationsPending );
466 bool updated = DoRelayout( mImpl->mControlSize,
467 mImpl->mOperationsPending,
470 // Do not re-do any operation until something changes.
471 mImpl->mOperationsPending = NO_OPERATION;
473 // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
474 CalculateTextAlignment( size );
476 if( mImpl->mEventData )
478 // Move the cursor, grab handle etc.
479 updated = mImpl->ProcessInputEvents() || updated;
485 void Controller::ProcessModifyEvents()
487 std::vector<ModifyEvent>& events = mImpl->mModifyEvents;
489 for( unsigned int i=0; i<events.size(); ++i )
491 if( ModifyEvent::REPLACE_TEXT == events[0].type )
493 // A (single) replace event should come first, otherwise we wasted time processing NOOP events
494 DALI_ASSERT_DEBUG( 0 == i && "Unexpected REPLACE event" );
496 ReplaceTextEvent( events[0].text );
498 else if( ModifyEvent::INSERT_TEXT == events[0].type )
500 InsertTextEvent( events[0].text );
502 else if( ModifyEvent::DELETE_TEXT == events[0].type )
508 // Discard temporary text
512 void Controller::ReplaceTextEvent( const std::string& text )
515 mImpl->mLogicalModel->mText.Clear();
516 mImpl->mLogicalModel->mScriptRuns.Clear();
517 mImpl->mLogicalModel->mFontRuns.Clear();
518 mImpl->mLogicalModel->mLineBreakInfo.Clear();
519 mImpl->mLogicalModel->mWordBreakInfo.Clear();
520 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
521 mImpl->mLogicalModel->mCharacterDirections.Clear();
522 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
523 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
524 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
525 mImpl->mVisualModel->mGlyphs.Clear();
526 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
527 mImpl->mVisualModel->mCharactersToGlyph.Clear();
528 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
529 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
530 mImpl->mVisualModel->mGlyphPositions.Clear();
531 mImpl->mVisualModel->mLines.Clear();
532 mImpl->mVisualModel->ClearCaches();
534 // Convert text into UTF-32
535 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
536 utf32Characters.Resize( text.size() );
538 // This is a bit horrible but std::string returns a (signed) char*
539 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
541 // Transform a text array encoded in utf8 into an array encoded in utf32.
542 // It returns the actual number of characters.
543 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
544 utf32Characters.Resize( characterCount );
546 // Reset the cursor position
547 if( mImpl->mEventData )
549 mImpl->mEventData->mPrimaryCursorPosition = characterCount;
550 // TODO - handle secondary cursor
553 // The natural size needs to be re-calculated.
554 mImpl->mRecalculateNaturalSize = true;
556 // Apply modifications to the model
557 mImpl->mOperationsPending = ALL_OPERATIONS;
558 UpdateModel( ALL_OPERATIONS );
559 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
565 void Controller::InsertTextEvent( const std::string& text )
567 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertTextEvent" );
569 // TODO - Optimize this
570 mImpl->mLogicalModel->mScriptRuns.Clear();
571 mImpl->mLogicalModel->mFontRuns.Clear();
572 mImpl->mLogicalModel->mLineBreakInfo.Clear();
573 mImpl->mLogicalModel->mWordBreakInfo.Clear();
574 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
575 mImpl->mLogicalModel->mCharacterDirections.Clear();
576 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
577 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
578 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
579 mImpl->mVisualModel->mGlyphs.Clear();
580 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
581 mImpl->mVisualModel->mCharactersToGlyph.Clear();
582 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
583 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
584 mImpl->mVisualModel->mGlyphPositions.Clear();
585 mImpl->mVisualModel->mLines.Clear();
586 mImpl->mVisualModel->ClearCaches();
588 // Convert text into UTF-32
589 Vector<Character> utf32Characters;
590 utf32Characters.Resize( text.size() );
592 // This is a bit horrible but std::string returns a (signed) char*
593 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
595 // Transform a text array encoded in utf8 into an array encoded in utf32.
596 // It returns the actual number of characters.
597 Length characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
598 utf32Characters.Resize( characterCount );
600 // Insert at current cursor position
601 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
602 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
604 if( cursorIndex < modifyText.Count() )
606 modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.End() );
610 modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.End() );
613 // Advance the cursor position
616 // The natural size needs to be re-calculated.
617 mImpl->mRecalculateNaturalSize = true;
619 // Apply modifications to the model; TODO - Optimize this
620 mImpl->mOperationsPending = ALL_OPERATIONS;
621 UpdateModel( ALL_OPERATIONS );
622 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
627 // Queue a cursor reposition event; this must wait until after DoRelayout()
628 mImpl->mEventData->mUpdateCursorPosition = true;
631 void Controller::DeleteTextEvent()
633 DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertTextEvent" );
635 // TODO - Optimize this
636 mImpl->mLogicalModel->mScriptRuns.Clear();
637 mImpl->mLogicalModel->mFontRuns.Clear();
638 mImpl->mLogicalModel->mLineBreakInfo.Clear();
639 mImpl->mLogicalModel->mWordBreakInfo.Clear();
640 mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
641 mImpl->mLogicalModel->mCharacterDirections.Clear();
642 mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
643 mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
644 mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
645 mImpl->mVisualModel->mGlyphs.Clear();
646 mImpl->mVisualModel->mGlyphsToCharacters.Clear();
647 mImpl->mVisualModel->mCharactersToGlyph.Clear();
648 mImpl->mVisualModel->mCharactersPerGlyph.Clear();
649 mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
650 mImpl->mVisualModel->mGlyphPositions.Clear();
651 mImpl->mVisualModel->mLines.Clear();
652 mImpl->mVisualModel->ClearCaches();
654 // Delte at current cursor position
655 Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
656 CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
658 if( cursorIndex > 0 &&
659 cursorIndex-1 < modifyText.Count() )
661 modifyText.Remove( modifyText.Begin() + cursorIndex - 1 );
663 // Cursor position retreat
667 // The natural size needs to be re-calculated.
668 mImpl->mRecalculateNaturalSize = true;
670 // Apply modifications to the model; TODO - Optimize this
671 mImpl->mOperationsPending = ALL_OPERATIONS;
672 UpdateModel( ALL_OPERATIONS );
673 mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT |
678 // Queue a cursor reposition event; this must wait until after DoRelayout()
679 mImpl->mEventData->mUpdateCursorPosition = true;
682 void Controller::UpdateModel( OperationsMask operationsRequired )
684 // Calculate the operations to be done.
685 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
687 Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
689 const Length numberOfCharacters = mImpl->mLogicalModel->GetNumberOfCharacters();
691 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
692 if( GET_LINE_BREAKS & operations )
694 // Retrieves the line break info. The line break info is used to split the text in 'paragraphs' to
695 // calculate the bidirectional info for each 'paragraph'.
696 // It's also used to layout the text (where it should be a new line) or to shape the text (text in different lines
697 // is not shaped together).
698 lineBreakInfo.Resize( numberOfCharacters, TextAbstraction::LINE_NO_BREAK );
700 SetLineBreakInfo( utf32Characters,
704 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
705 if( GET_WORD_BREAKS & operations )
707 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
708 wordBreakInfo.Resize( numberOfCharacters, TextAbstraction::WORD_NO_BREAK );
710 SetWordBreakInfo( utf32Characters,
714 const bool getScripts = GET_SCRIPTS & operations;
715 const bool validateFonts = VALIDATE_FONTS & operations;
717 Vector<ScriptRun>& scripts = mImpl->mLogicalModel->mScriptRuns;
718 Vector<FontRun>& validFonts = mImpl->mLogicalModel->mFontRuns;
720 if( getScripts || validateFonts )
722 // Validates the fonts assigned by the application or assigns default ones.
723 // It makes sure all the characters are going to be rendered by the correct font.
724 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
728 // Retrieves the scripts used in the text.
729 multilanguageSupport.SetScripts( utf32Characters,
736 if( 0u == validFonts.Count() )
738 // Copy the requested font defaults received via the property system.
739 // These may not be valid i.e. may not contain glyphs for the necessary scripts.
740 GetDefaultFonts( validFonts, numberOfCharacters );
743 // Validates the fonts. If there is a character with no assigned font it sets a default one.
744 // After this call, fonts are validated.
745 multilanguageSupport.ValidateFonts( utf32Characters,
751 Vector<Character> mirroredUtf32Characters;
752 bool textMirrored = false;
753 if( BIDI_INFO & operations )
755 // Count the number of LINE_NO_BREAK to reserve some space for the vector of paragraph's
756 // bidirectional info.
758 Length numberOfParagraphs = 0u;
760 const TextAbstraction::LineBreakInfo* lineBreakInfoBuffer = lineBreakInfo.Begin();
761 for( Length index = 0u; index < numberOfCharacters; ++index )
763 if( TextAbstraction::LINE_NO_BREAK == *( lineBreakInfoBuffer + index ) )
765 ++numberOfParagraphs;
769 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
770 bidirectionalInfo.Reserve( numberOfParagraphs );
772 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
773 SetBidirectionalInfo( utf32Characters,
778 if( 0u != bidirectionalInfo.Count() )
780 // This paragraph has right to left text. Some characters may need to be mirrored.
781 // TODO: consider if the mirrored string can be stored as well.
783 textMirrored = GetMirroredText( utf32Characters, mirroredUtf32Characters );
785 // Only set the character directions if there is right to left characters.
786 Vector<CharacterDirection>& directions = mImpl->mLogicalModel->mCharacterDirections;
787 directions.Resize( numberOfCharacters );
789 GetCharactersDirection( bidirectionalInfo,
794 // There is no right to left characters. Clear the directions vector.
795 mImpl->mLogicalModel->mCharacterDirections.Clear();
800 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
801 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
802 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
803 if( SHAPE_TEXT & operations )
805 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
807 ShapeText( textToShape,
812 glyphsToCharactersMap,
813 charactersPerGlyph );
815 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
816 mImpl->mVisualModel->CreateGlyphsPerCharacterTable( numberOfCharacters );
817 mImpl->mVisualModel->CreateCharacterToGlyphTable( numberOfCharacters );
820 const Length numberOfGlyphs = glyphs.Count();
822 if( GET_GLYPH_METRICS & operations )
824 mImpl->mFontClient.GetGlyphMetrics( glyphs.Begin(), numberOfGlyphs );
828 bool Controller::DoRelayout( const Size& size,
829 OperationsMask operationsRequired,
832 bool viewUpdated( false );
834 // Calculate the operations to be done.
835 const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
837 if( LAYOUT & operations )
839 // Some vectors with data needed to layout and reorder may be void
840 // after the first time the text has been laid out.
841 // Fill the vectors again.
843 Length numberOfGlyphs = mImpl->mVisualModel->GetNumberOfGlyphs();
845 if( 0u == numberOfGlyphs )
847 // Nothing else to do if there is no glyphs.
851 Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
852 Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
853 Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
854 Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
855 Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
856 Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
858 // Set the layout parameters.
859 LayoutParameters layoutParameters( size,
860 mImpl->mLogicalModel->mText.Begin(),
861 lineBreakInfo.Begin(),
862 wordBreakInfo.Begin(),
863 ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
866 glyphsToCharactersMap.Begin(),
867 charactersPerGlyph.Begin() );
869 // The laid-out lines.
870 // It's not possible to know in how many lines the text is going to be laid-out,
871 // but it can be resized at least with the number of 'paragraphs' to avoid
872 // some re-allocations.
873 Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
875 // Delete any previous laid out lines before setting the new ones.
878 // The capacity of the bidirectional paragraph info is the number of paragraphs.
879 lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
881 // Resize the vector of positions to have the same size than the vector of glyphs.
882 Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
883 glyphPositions.Resize( numberOfGlyphs );
885 // Update the visual model.
886 viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
894 if( REORDER & operations )
896 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
898 // Check first if there are paragraphs with bidirectional info.
899 if( 0u != bidirectionalInfo.Count() )
902 const Length numberOfLines = mImpl->mVisualModel->GetNumberOfLines();
904 // Reorder the lines.
905 Vector<BidirectionalLineInfoRun> lineBidirectionalInfoRuns;
906 lineBidirectionalInfoRuns.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
907 ReorderLines( bidirectionalInfo,
909 lineBidirectionalInfoRuns );
911 // Set the bidirectional info into the model.
912 const Length numberOfBidirectionalInfoRuns = lineBidirectionalInfoRuns.Count();
913 mImpl->mLogicalModel->SetVisualToLogicalMap( lineBidirectionalInfoRuns.Begin(),
914 numberOfBidirectionalInfoRuns );
916 // Set the bidirectional info per line into the layout parameters.
917 layoutParameters.lineBidirectionalInfoRunsBuffer = lineBidirectionalInfoRuns.Begin();
918 layoutParameters.numberOfBidirectionalInfoRuns = numberOfBidirectionalInfoRuns;
920 // Get the character to glyph conversion table and set into the layout.
921 layoutParameters.charactersToGlyphsBuffer = mImpl->mVisualModel->mCharactersToGlyph.Begin();
923 // Get the glyphs per character table and set into the layout.
924 layoutParameters.glyphsPerCharacterBuffer = mImpl->mVisualModel->mGlyphsPerCharacter.Begin();
926 // Re-layout the text. Reorder those lines with right to left characters.
927 mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
930 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
931 for( Vector<BidirectionalLineInfoRun>::Iterator it = lineBidirectionalInfoRuns.Begin(),
932 endIt = lineBidirectionalInfoRuns.End();
936 BidirectionalLineInfoRun& bidiLineInfo = *it;
938 free( bidiLineInfo.visualToLogicalMap );
943 if( ALIGN & operations )
945 mImpl->mLayoutEngine.Align( layoutParameters,
951 // Sets the actual size.
952 if( UPDATE_ACTUAL_SIZE & operations )
954 mImpl->mVisualModel->SetActualSize( layoutSize );
960 layoutSize = mImpl->mVisualModel->GetActualSize();
966 void Controller::CalculateTextAlignment( const Size& size )
968 // Get the direction of the first character.
969 const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
971 const Size& actualSize = mImpl->mVisualModel->GetActualSize();
973 // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
974 LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
975 if( firstParagraphDirection &&
976 ( LayoutEngine::HORIZONTAL_ALIGN_CENTER != horizontalAlignment ) )
978 if( LayoutEngine::HORIZONTAL_ALIGN_BEGIN == horizontalAlignment )
980 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
984 horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
988 switch( horizontalAlignment )
990 case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
992 mImpl->mAlignmentOffset.x = 0.f;
995 case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
997 const int intOffset = static_cast<int>( 0.5f * ( size.width - actualSize.width ) ); // try to avoid pixel alignment.
998 mImpl->mAlignmentOffset.x = static_cast<float>( intOffset );
1001 case LayoutEngine::HORIZONTAL_ALIGN_END:
1003 mImpl->mAlignmentOffset.x = size.width - actualSize.width;
1008 const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1009 switch( verticalAlignment )
1011 case LayoutEngine::VERTICAL_ALIGN_TOP:
1013 mImpl->mAlignmentOffset.y = 0.f;
1016 case LayoutEngine::VERTICAL_ALIGN_CENTER:
1018 const int intOffset = static_cast<int>( 0.5f * ( size.height - actualSize.height ) ); // try to avoid pixel alignment.
1019 mImpl->mAlignmentOffset.y = static_cast<float>( intOffset );
1022 case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1024 mImpl->mAlignmentOffset.y = size.height - actualSize.height;
1030 LayoutEngine& Controller::GetLayoutEngine()
1032 return mImpl->mLayoutEngine;
1035 View& Controller::GetView()
1037 return mImpl->mView;
1040 void Controller::KeyboardFocusGainEvent()
1042 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1044 if( mImpl->mEventData )
1046 Event event( Event::KEYBOARD_FOCUS_GAIN_EVENT );
1047 mImpl->mEventData->mEventQueue.push_back( event );
1049 mImpl->RequestRelayout();
1053 void Controller::KeyboardFocusLostEvent()
1055 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1057 if( mImpl->mEventData )
1059 Event event( Event::KEYBOARD_FOCUS_LOST_EVENT );
1060 mImpl->mEventData->mEventQueue.push_back( event );
1062 mImpl->RequestRelayout();
1066 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1068 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1070 if( mImpl->mEventData &&
1071 keyEvent.state == KeyEvent::Down )
1073 int keyCode = keyEvent.keyCode;
1074 const std::string& keyString = keyEvent.keyPressed;
1076 // Pre-process to separate modifying events from non-modifying input events.
1077 if( Dali::DALI_KEY_ESCAPE == keyCode )
1079 // Escape key is a special case which causes focus loss
1080 KeyboardFocusLostEvent();
1082 else if( Dali::DALI_KEY_CURSOR_LEFT == keyCode ||
1083 Dali::DALI_KEY_CURSOR_RIGHT == keyCode ||
1084 Dali::DALI_KEY_CURSOR_UP == keyCode ||
1085 Dali::DALI_KEY_CURSOR_DOWN == keyCode )
1087 Event event( Event::CURSOR_KEY_EVENT );
1088 event.p1.mInt = keyCode;
1089 mImpl->mEventData->mEventQueue.push_back( event );
1091 else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1093 // Queue a delete event
1095 event.type = ModifyEvent::DELETE_TEXT;
1096 mImpl->mModifyEvents.push_back( event );
1098 else if( !keyString.empty() )
1100 // Queue an insert event
1102 event.type = ModifyEvent::INSERT_TEXT;
1103 event.text = keyString;
1104 mImpl->mModifyEvents.push_back( event );
1107 mImpl->ChangeState( EventData::EDITING ); // todo Confirm this is the best place to change the state of
1109 mImpl->RequestRelayout();
1115 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1117 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1119 if( mImpl->mEventData )
1121 Event event( Event::TAP_EVENT );
1122 event.p1.mUint = tapCount;
1123 event.p2.mFloat = x;
1124 event.p3.mFloat = y;
1125 mImpl->mEventData->mEventQueue.push_back( event );
1127 mImpl->RequestRelayout();
1131 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
1133 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
1135 if( mImpl->mEventData )
1137 Event event( Event::PAN_EVENT );
1138 event.p1.mInt = state;
1139 event.p2.mFloat = displacement.x;
1140 event.p3.mFloat = displacement.y;
1141 mImpl->mEventData->mEventQueue.push_back( event );
1143 mImpl->RequestRelayout();
1147 void Controller::GrabHandleEvent( GrabHandleState state, float x, float y )
1149 DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected GrabHandleEvent" );
1151 if( mImpl->mEventData )
1153 Event event( Event::GRAB_HANDLE_EVENT );
1154 event.p1.mUint = state;
1155 event.p2.mFloat = x;
1156 event.p3.mFloat = y;
1158 mImpl->mEventData->mEventQueue.push_back( event );
1160 mImpl->RequestRelayout();
1164 Controller::~Controller()
1169 Controller::Controller( ControlInterface& controlInterface )
1172 mImpl = new Controller::Impl( controlInterface );
1177 } // namespace Toolkit