+void Controller::Impl::NotifyImfManager()
+{
+ if( mEventData && mEventData->mImfManager )
+ {
+ CharacterIndex cursorPosition = GetLogicalCursorPosition();
+
+ const Length numberOfWhiteSpaces = GetNumberOfWhiteSpaces( 0u );
+
+ // Update the cursor position by removing the initial white spaces.
+ if( cursorPosition < numberOfWhiteSpaces )
+ {
+ cursorPosition = 0u;
+ }
+ else
+ {
+ cursorPosition -= numberOfWhiteSpaces;
+ }
+
+ mEventData->mImfManager.SetCursorPosition( cursorPosition );
+ mEventData->mImfManager.NotifyCursorPosition();
+ }
+}
+
+CharacterIndex Controller::Impl::GetLogicalCursorPosition() const
+{
+ CharacterIndex cursorPosition = 0u;
+
+ if( mEventData )
+ {
+ if( ( EventData::SELECTING == mEventData->mState ) ||
+ ( EventData::SELECTION_HANDLE_PANNING == mEventData->mState ) )
+ {
+ cursorPosition = std::min( mEventData->mRightSelectionPosition, mEventData->mLeftSelectionPosition );
+ }
+ else
+ {
+ cursorPosition = mEventData->mPrimaryCursorPosition;
+ }
+ }
+
+ return cursorPosition;
+}
+
+Length Controller::Impl::GetNumberOfWhiteSpaces( CharacterIndex index ) const
+{
+ Length numberOfWhiteSpaces = 0u;
+
+ // Get the buffer to the text.
+ Character* utf32CharacterBuffer = mLogicalModel->mText.Begin();
+
+ const Length totalNumberOfCharacters = mLogicalModel->mText.Count();
+ for( ; index < totalNumberOfCharacters; ++index, ++numberOfWhiteSpaces )
+ {
+ if( !TextAbstraction::IsWhiteSpace( *( utf32CharacterBuffer + index ) ) )
+ {
+ break;
+ }
+ }
+
+ return numberOfWhiteSpaces;
+}
+
+void Controller::Impl::GetText( CharacterIndex index, std::string& text ) const
+{
+ // Get the total number of characters.
+ Length numberOfCharacters = mLogicalModel->mText.Count();
+
+ // Retrieve the text.
+ if( 0u != numberOfCharacters )
+ {
+ Utf32ToUtf8( mLogicalModel->mText.Begin() + index, numberOfCharacters - index, text );
+ }
+}
+
+void Controller::Impl::CalculateTextUpdateIndices( Length& numberOfCharacters )
+{
+ mTextUpdateInfo.mParagraphCharacterIndex = 0u;
+ mTextUpdateInfo.mStartGlyphIndex = 0u;
+ mTextUpdateInfo.mStartLineIndex = 0u;
+ numberOfCharacters = 0u;
+
+ const Length numberOfParagraphs = mLogicalModel->mParagraphInfo.Count();
+ if( 0u == numberOfParagraphs )
+ {
+ mTextUpdateInfo.mParagraphCharacterIndex = 0u;
+ numberOfCharacters = 0u;
+
+ mTextUpdateInfo.mRequestedNumberOfCharacters = mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
+
+ // Nothing else to do if there are no paragraphs.
+ return;
+ }
+
+ // Find the paragraphs to be updated.
+ Vector<ParagraphRunIndex> paragraphsToBeUpdated;
+ if( mTextUpdateInfo.mCharacterIndex >= mTextUpdateInfo.mPreviousNumberOfCharacters )
+ {
+ // Text is being added at the end of the current text.
+ if( mTextUpdateInfo.mIsLastCharacterNewParagraph )
+ {
+ // Text is being added in a new paragraph after the last character of the text.
+ mTextUpdateInfo.mParagraphCharacterIndex = mTextUpdateInfo.mPreviousNumberOfCharacters;
+ numberOfCharacters = 0u;
+ mTextUpdateInfo.mRequestedNumberOfCharacters = mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
+
+ mTextUpdateInfo.mStartGlyphIndex = mVisualModel->mGlyphs.Count();
+ mTextUpdateInfo.mStartLineIndex = mVisualModel->mLines.Count() - 1u;
+
+ // Nothing else to do;
+ return;
+ }
+
+ paragraphsToBeUpdated.PushBack( numberOfParagraphs - 1u );
+ }
+ else
+ {
+ Length numberOfCharactersToUpdate = 0u;
+ if( mTextUpdateInfo.mFullRelayoutNeeded )
+ {
+ numberOfCharactersToUpdate = mTextUpdateInfo.mPreviousNumberOfCharacters;
+ }
+ else
+ {
+ numberOfCharactersToUpdate = ( mTextUpdateInfo.mNumberOfCharactersToRemove > 0u ) ? mTextUpdateInfo.mNumberOfCharactersToRemove : 1u;
+ }
+ mLogicalModel->FindParagraphs( mTextUpdateInfo.mCharacterIndex,
+ numberOfCharactersToUpdate,
+ paragraphsToBeUpdated );
+ }
+
+ if( 0u != paragraphsToBeUpdated.Count() )
+ {
+ const ParagraphRunIndex firstParagraphIndex = *( paragraphsToBeUpdated.Begin() );
+ const ParagraphRun& firstParagraph = *( mLogicalModel->mParagraphInfo.Begin() + firstParagraphIndex );
+ mTextUpdateInfo.mParagraphCharacterIndex = firstParagraph.characterRun.characterIndex;
+
+ ParagraphRunIndex lastParagraphIndex = *( paragraphsToBeUpdated.End() - 1u );
+ const ParagraphRun& lastParagraph = *( mLogicalModel->mParagraphInfo.Begin() + lastParagraphIndex );
+
+ if( ( mTextUpdateInfo.mNumberOfCharactersToRemove > 0u ) && // Some character are removed.
+ ( lastParagraphIndex < numberOfParagraphs - 1u ) && // There is a next paragraph.
+ ( ( lastParagraph.characterRun.characterIndex + lastParagraph.characterRun.numberOfCharacters ) == // The last removed character is the new paragraph character.
+ ( mTextUpdateInfo.mCharacterIndex + mTextUpdateInfo.mNumberOfCharactersToRemove ) ) )
+ {
+ // The new paragraph character of the last updated paragraph has been removed so is going to be merged with the next one.
+ const ParagraphRun& lastParagraph = *( mLogicalModel->mParagraphInfo.Begin() + lastParagraphIndex + 1u );
+
+ numberOfCharacters = lastParagraph.characterRun.characterIndex + lastParagraph.characterRun.numberOfCharacters - mTextUpdateInfo.mParagraphCharacterIndex;
+ }
+ else
+ {
+ numberOfCharacters = lastParagraph.characterRun.characterIndex + lastParagraph.characterRun.numberOfCharacters - mTextUpdateInfo.mParagraphCharacterIndex;
+ }
+ }
+
+ mTextUpdateInfo.mRequestedNumberOfCharacters = numberOfCharacters + mTextUpdateInfo.mNumberOfCharactersToAdd - mTextUpdateInfo.mNumberOfCharactersToRemove;
+ mTextUpdateInfo.mStartGlyphIndex = *( mVisualModel->mCharactersToGlyph.Begin() + mTextUpdateInfo.mParagraphCharacterIndex );
+}
+
+void Controller::Impl::ClearFullModelData( OperationsMask operations )
+{
+ if( NO_OPERATION != ( GET_LINE_BREAKS & operations ) )
+ {
+ mLogicalModel->mLineBreakInfo.Clear();
+ mLogicalModel->mParagraphInfo.Clear();
+ }
+
+ if( NO_OPERATION != ( GET_WORD_BREAKS & operations ) )
+ {
+ mLogicalModel->mLineBreakInfo.Clear();
+ }
+
+ if( NO_OPERATION != ( GET_SCRIPTS & operations ) )
+ {
+ mLogicalModel->mScriptRuns.Clear();
+ }
+
+ if( NO_OPERATION != ( VALIDATE_FONTS & operations ) )
+ {
+ mLogicalModel->mFontRuns.Clear();
+ }
+
+ if( 0u != mLogicalModel->mBidirectionalParagraphInfo.Count() )
+ {
+ if( NO_OPERATION != ( BIDI_INFO & operations ) )
+ {
+ mLogicalModel->mBidirectionalParagraphInfo.Clear();
+ mLogicalModel->mCharacterDirections.Clear();
+ }
+
+ if( NO_OPERATION != ( REORDER & operations ) )
+ {
+ // Free the allocated memory used to store the conversion table in the bidirectional line info run.
+ for( Vector<BidirectionalLineInfoRun>::Iterator it = mLogicalModel->mBidirectionalLineInfo.Begin(),
+ endIt = mLogicalModel->mBidirectionalLineInfo.End();
+ it != endIt;
+ ++it )
+ {
+ BidirectionalLineInfoRun& bidiLineInfo = *it;
+
+ free( bidiLineInfo.visualToLogicalMap );
+ bidiLineInfo.visualToLogicalMap = NULL;
+ }
+ mLogicalModel->mBidirectionalLineInfo.Clear();
+ }
+ }
+
+ if( NO_OPERATION != ( SHAPE_TEXT & operations ) )
+ {
+ mVisualModel->mGlyphs.Clear();
+ mVisualModel->mGlyphsToCharacters.Clear();
+ mVisualModel->mCharactersToGlyph.Clear();
+ mVisualModel->mCharactersPerGlyph.Clear();
+ mVisualModel->mGlyphsPerCharacter.Clear();
+ mVisualModel->mGlyphPositions.Clear();
+ }
+
+ if( NO_OPERATION != ( LAYOUT & operations ) )
+ {
+ mVisualModel->mLines.Clear();
+ }
+
+ if( NO_OPERATION != ( COLOR & operations ) )
+ {
+ mVisualModel->mColorIndices.Clear();
+ }
+}
+
+void Controller::Impl::ClearCharacterModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations )
+{
+ const CharacterIndex endIndexPlusOne = endIndex + 1u;
+
+ if( NO_OPERATION != ( GET_LINE_BREAKS & operations ) )
+ {
+ // Clear the line break info.
+ LineBreakInfo* lineBreakInfoBuffer = mLogicalModel->mLineBreakInfo.Begin();
+
+ mLogicalModel->mLineBreakInfo.Erase( lineBreakInfoBuffer + startIndex,
+ lineBreakInfoBuffer + endIndexPlusOne );
+
+ // Clear the paragraphs.
+ ClearCharacterRuns( startIndex,
+ endIndex,
+ mLogicalModel->mParagraphInfo );
+ }
+
+ if( NO_OPERATION != ( GET_WORD_BREAKS & operations ) )
+ {
+ // Clear the word break info.
+ WordBreakInfo* wordBreakInfoBuffer = mLogicalModel->mWordBreakInfo.Begin();
+
+ mLogicalModel->mWordBreakInfo.Erase( wordBreakInfoBuffer + startIndex,
+ wordBreakInfoBuffer + endIndexPlusOne );
+ }
+
+ if( NO_OPERATION != ( GET_SCRIPTS & operations ) )
+ {
+ // Clear the scripts.
+ ClearCharacterRuns( startIndex,
+ endIndex,
+ mLogicalModel->mScriptRuns );
+ }
+
+ if( NO_OPERATION != ( VALIDATE_FONTS & operations ) )
+ {
+ // Clear the fonts.
+ ClearCharacterRuns( startIndex,
+ endIndex,
+ mLogicalModel->mFontRuns );
+ }
+
+ if( 0u != mLogicalModel->mBidirectionalParagraphInfo.Count() )
+ {
+ if( NO_OPERATION != ( BIDI_INFO & operations ) )
+ {
+ // Clear the bidirectional paragraph info.
+ ClearCharacterRuns( startIndex,
+ endIndex,
+ mLogicalModel->mBidirectionalParagraphInfo );
+
+ // Clear the character's directions.
+ CharacterDirection* characterDirectionsBuffer = mLogicalModel->mCharacterDirections.Begin();
+
+ mLogicalModel->mCharacterDirections.Erase( characterDirectionsBuffer + startIndex,
+ characterDirectionsBuffer + endIndexPlusOne );
+ }
+
+ if( NO_OPERATION != ( REORDER & operations ) )
+ {
+ uint32_t startRemoveIndex = mLogicalModel->mBidirectionalLineInfo.Count();
+ uint32_t endRemoveIndex = startRemoveIndex;
+ ClearCharacterRuns( startIndex,
+ endIndex,
+ mLogicalModel->mBidirectionalLineInfo,
+ startRemoveIndex,
+ endRemoveIndex );
+
+ BidirectionalLineInfoRun* bidirectionalLineInfoBuffer = mLogicalModel->mBidirectionalLineInfo.Begin();
+
+ // Free the allocated memory used to store the conversion table in the bidirectional line info run.
+ for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfoBuffer + startRemoveIndex,
+ endIt = bidirectionalLineInfoBuffer + endRemoveIndex;
+ it != endIt;
+ ++it )
+ {
+ BidirectionalLineInfoRun& bidiLineInfo = *it;
+
+ free( bidiLineInfo.visualToLogicalMap );
+ bidiLineInfo.visualToLogicalMap = NULL;
+ }
+
+ mLogicalModel->mBidirectionalLineInfo.Erase( bidirectionalLineInfoBuffer + startRemoveIndex,
+ bidirectionalLineInfoBuffer + endRemoveIndex );
+ }
+ }
+}
+
+void Controller::Impl::ClearGlyphModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations )
+{
+ const CharacterIndex endIndexPlusOne = endIndex + 1u;
+ const Length numberOfCharactersRemoved = endIndexPlusOne - startIndex;
+
+ // Convert the character index to glyph index before deleting the character to glyph and the glyphs per character buffers.
+ GlyphIndex* charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
+ Length* glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
+
+ const GlyphIndex endGlyphIndexPlusOne = *( charactersToGlyphBuffer + endIndex ) + *( glyphsPerCharacterBuffer + endIndex );
+ const Length numberOfGlyphsRemoved = endGlyphIndexPlusOne - mTextUpdateInfo.mStartGlyphIndex;
+
+ if( NO_OPERATION != ( SHAPE_TEXT & operations ) )
+ {
+ // Update the character to glyph indices.
+ for( Vector<GlyphIndex>::Iterator it = charactersToGlyphBuffer + endIndexPlusOne,
+ endIt = charactersToGlyphBuffer + mVisualModel->mCharactersToGlyph.Count();
+ it != endIt;
+ ++it )
+ {
+ CharacterIndex& index = *it;
+ index -= numberOfGlyphsRemoved;
+ }
+
+ // Clear the character to glyph conversion table.
+ mVisualModel->mCharactersToGlyph.Erase( charactersToGlyphBuffer + startIndex,
+ charactersToGlyphBuffer + endIndexPlusOne );
+
+ // Clear the glyphs per character table.
+ mVisualModel->mGlyphsPerCharacter.Erase( glyphsPerCharacterBuffer + startIndex,
+ glyphsPerCharacterBuffer + endIndexPlusOne );
+
+ // Clear the glyphs buffer.
+ GlyphInfo* glyphsBuffer = mVisualModel->mGlyphs.Begin();
+ mVisualModel->mGlyphs.Erase( glyphsBuffer + mTextUpdateInfo.mStartGlyphIndex,
+ glyphsBuffer + endGlyphIndexPlusOne );
+
+ CharacterIndex* glyphsToCharactersBuffer = mVisualModel->mGlyphsToCharacters.Begin();
+
+ // Update the glyph to character indices.
+ for( Vector<CharacterIndex>::Iterator it = glyphsToCharactersBuffer + endGlyphIndexPlusOne,
+ endIt = glyphsToCharactersBuffer + mVisualModel->mGlyphsToCharacters.Count();
+ it != endIt;
+ ++it )
+ {
+ CharacterIndex& index = *it;
+ index -= numberOfCharactersRemoved;
+ }
+
+ // Clear the glyphs to characters buffer.
+ mVisualModel->mGlyphsToCharacters.Erase( glyphsToCharactersBuffer + mTextUpdateInfo.mStartGlyphIndex,
+ glyphsToCharactersBuffer + endGlyphIndexPlusOne );
+
+ // Clear the characters per glyph buffer.
+ Length* charactersPerGlyphBuffer = mVisualModel->mCharactersPerGlyph.Begin();
+ mVisualModel->mCharactersPerGlyph.Erase( charactersPerGlyphBuffer + mTextUpdateInfo.mStartGlyphIndex,
+ charactersPerGlyphBuffer + endGlyphIndexPlusOne );
+
+ // Clear the positions buffer.
+ Vector2* positionsBuffer = mVisualModel->mGlyphPositions.Begin();
+ mVisualModel->mGlyphPositions.Erase( positionsBuffer + mTextUpdateInfo.mStartGlyphIndex,
+ positionsBuffer + endGlyphIndexPlusOne );
+ }
+
+ if( NO_OPERATION != ( LAYOUT & operations ) )
+ {
+ // Clear the lines.
+ uint32_t startRemoveIndex = mVisualModel->mLines.Count();
+ uint32_t endRemoveIndex = startRemoveIndex;
+ ClearCharacterRuns( startIndex,
+ endIndex,
+ mVisualModel->mLines,
+ startRemoveIndex,
+ endRemoveIndex );
+
+ // Will update the glyph runs.
+ startRemoveIndex = mVisualModel->mLines.Count();
+ endRemoveIndex = startRemoveIndex;
+ ClearGlyphRuns( mTextUpdateInfo.mStartGlyphIndex,
+ endGlyphIndexPlusOne - 1u,
+ mVisualModel->mLines,
+ startRemoveIndex,
+ endRemoveIndex );
+
+ // Set the line index from where to insert the new laid-out lines.
+ mTextUpdateInfo.mStartLineIndex = startRemoveIndex;
+
+ LineRun* linesBuffer = mVisualModel->mLines.Begin();
+ mVisualModel->mLines.Erase( linesBuffer + startRemoveIndex,
+ linesBuffer + endRemoveIndex );
+ }
+
+ if( NO_OPERATION != ( COLOR & operations ) )
+ {
+ if( 0u != mVisualModel->mColorIndices.Count() )
+ {
+ ColorIndex* colorIndexBuffer = mVisualModel->mColorIndices.Begin();
+ mVisualModel->mColorIndices.Erase( colorIndexBuffer + mTextUpdateInfo.mStartGlyphIndex,
+ colorIndexBuffer + endGlyphIndexPlusOne );
+ }
+ }
+}
+
+void Controller::Impl::ClearModelData( CharacterIndex startIndex, CharacterIndex endIndex, OperationsMask operations )
+{
+ if( mTextUpdateInfo.mClearAll ||
+ ( ( 0u == startIndex ) &&
+ ( mTextUpdateInfo.mPreviousNumberOfCharacters == endIndex + 1u ) ) )
+ {
+ ClearFullModelData( operations );
+ }
+ else
+ {
+ // Clear the model data related with characters.
+ ClearCharacterModelData( startIndex, endIndex, operations );
+
+ // Clear the model data related with glyphs.
+ ClearGlyphModelData( startIndex, endIndex, operations );
+ }
+
+ // The estimated number of lines. Used to avoid reallocations when layouting.
+ mTextUpdateInfo.mEstimatedNumberOfLines = std::max( mVisualModel->mLines.Count(), mLogicalModel->mParagraphInfo.Count() );
+
+ mVisualModel->ClearCaches();
+}
+
+bool Controller::Impl::UpdateModel( OperationsMask operationsRequired )