+ // 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 )
+{
+ DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::UpdateModel\n" );
+
+ // Calculate the operations to be done.
+ const OperationsMask operations = static_cast<OperationsMask>( mOperationsPending & operationsRequired );
+
+ if( NO_OPERATION == operations )
+ {
+ // Nothing to do if no operations are pending and required.
+ return false;
+ }
+
+ Vector<Character>& utf32Characters = mLogicalModel->mText;
+
+ const Length numberOfCharacters = utf32Characters.Count();
+
+ // Index to the first character of the first paragraph to be updated.
+ CharacterIndex startIndex = 0u;
+ // Number of characters of the paragraphs to be removed.
+ Length paragraphCharacters = 0u;
+
+ CalculateTextUpdateIndices( paragraphCharacters );
+ startIndex = mTextUpdateInfo.mParagraphCharacterIndex;
+
+ if( mTextUpdateInfo.mClearAll ||
+ ( 0u != paragraphCharacters ) )
+ {
+ ClearModelData( startIndex, startIndex + ( ( paragraphCharacters > 0u ) ? paragraphCharacters - 1u : 0u ), operations );
+ }
+
+ mTextUpdateInfo.mClearAll = false;
+
+ // Whether the model is updated.
+ bool updated = false;
+
+ Vector<LineBreakInfo>& lineBreakInfo = mLogicalModel->mLineBreakInfo;
+ const Length requestedNumberOfCharacters = mTextUpdateInfo.mRequestedNumberOfCharacters;
+
+ if( NO_OPERATION != ( GET_LINE_BREAKS & operations ) )
+ {
+ // Retrieves the line break info. The line break info is used to split the text in 'paragraphs' to
+ // calculate the bidirectional info for each 'paragraph'.
+ // It's also used to layout the text (where it should be a new line) or to shape the text (text in different lines
+ // is not shaped together).
+ lineBreakInfo.Resize( numberOfCharacters, TextAbstraction::LINE_NO_BREAK );
+
+ SetLineBreakInfo( utf32Characters,
+ startIndex,
+ requestedNumberOfCharacters,
+ lineBreakInfo );
+
+ // Create the paragraph info.
+ mLogicalModel->CreateParagraphInfo( startIndex,
+ requestedNumberOfCharacters );
+ updated = true;
+ }
+
+ Vector<WordBreakInfo>& wordBreakInfo = mLogicalModel->mWordBreakInfo;
+ if( NO_OPERATION != ( GET_WORD_BREAKS & operations ) )
+ {
+ // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
+ wordBreakInfo.Resize( numberOfCharacters, TextAbstraction::WORD_NO_BREAK );
+
+ SetWordBreakInfo( utf32Characters,
+ startIndex,
+ requestedNumberOfCharacters,
+ wordBreakInfo );
+ updated = true;
+ }
+
+ const bool getScripts = NO_OPERATION != ( GET_SCRIPTS & operations );
+ const bool validateFonts = NO_OPERATION != ( VALIDATE_FONTS & operations );
+
+ Vector<ScriptRun>& scripts = mLogicalModel->mScriptRuns;
+ Vector<FontRun>& validFonts = mLogicalModel->mFontRuns;
+
+ if( getScripts || validateFonts )
+ {
+ // Validates the fonts assigned by the application or assigns default ones.
+ // It makes sure all the characters are going to be rendered by the correct font.
+ MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
+
+ if( getScripts )
+ {
+ // Retrieves the scripts used in the text.
+ multilanguageSupport.SetScripts( utf32Characters,
+ startIndex,
+ requestedNumberOfCharacters,
+ scripts );
+ }
+
+ if( validateFonts )
+ {
+ // Validate the fonts set through the mark-up string.
+ Vector<FontDescriptionRun>& fontDescriptionRuns = mLogicalModel->mFontDescriptionRuns;
+
+ // Get the default font id.
+ const FontId defaultFontId = ( NULL == mFontDefaults ) ? 0u : mFontDefaults->GetFontId( mFontClient );
+
+ // Validates the fonts. If there is a character with no assigned font it sets a default one.
+ // After this call, fonts are validated.
+ multilanguageSupport.ValidateFonts( utf32Characters,
+ scripts,
+ fontDescriptionRuns,
+ defaultFontId,
+ startIndex,
+ requestedNumberOfCharacters,
+ validFonts );
+ }
+ updated = true;
+ }
+
+ Vector<Character> mirroredUtf32Characters;
+ bool textMirrored = false;
+ const Length numberOfParagraphs = mLogicalModel->mParagraphInfo.Count();
+ if( NO_OPERATION != ( BIDI_INFO & operations ) )
+ {
+ Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mLogicalModel->mBidirectionalParagraphInfo;
+ bidirectionalInfo.Reserve( numberOfParagraphs );
+
+ // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
+ SetBidirectionalInfo( utf32Characters,
+ scripts,
+ lineBreakInfo,
+ startIndex,
+ requestedNumberOfCharacters,
+ bidirectionalInfo );
+
+ if( 0u != bidirectionalInfo.Count() )
+ {
+ // Only set the character directions if there is right to left characters.
+ Vector<CharacterDirection>& directions = mLogicalModel->mCharacterDirections;
+ GetCharactersDirection( bidirectionalInfo,
+ numberOfCharacters,
+ startIndex,
+ requestedNumberOfCharacters,
+ directions );
+
+ // This paragraph has right to left text. Some characters may need to be mirrored.
+ // TODO: consider if the mirrored string can be stored as well.
+
+ textMirrored = GetMirroredText( utf32Characters,
+ directions,
+ bidirectionalInfo,
+ startIndex,
+ requestedNumberOfCharacters,
+ mirroredUtf32Characters );
+ }
+ else
+ {
+ // There is no right to left characters. Clear the directions vector.
+ mLogicalModel->mCharacterDirections.Clear();
+ }
+ updated = true;
+ }
+
+ Vector<GlyphInfo>& glyphs = mVisualModel->mGlyphs;
+ Vector<CharacterIndex>& glyphsToCharactersMap = mVisualModel->mGlyphsToCharacters;
+ Vector<Length>& charactersPerGlyph = mVisualModel->mCharactersPerGlyph;
+ Vector<GlyphIndex> newParagraphGlyphs;
+ newParagraphGlyphs.Reserve( numberOfParagraphs );
+
+ const Length currentNumberOfGlyphs = glyphs.Count();
+ if( NO_OPERATION != ( SHAPE_TEXT & operations ) )
+ {
+ const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
+ // Shapes the text.
+ ShapeText( textToShape,
+ lineBreakInfo,
+ scripts,
+ validFonts,
+ startIndex,
+ mTextUpdateInfo.mStartGlyphIndex,
+ requestedNumberOfCharacters,
+ glyphs,
+ glyphsToCharactersMap,
+ charactersPerGlyph,
+ newParagraphGlyphs );
+
+ // Create the 'number of glyphs' per character and the glyph to character conversion tables.
+ mVisualModel->CreateGlyphsPerCharacterTable( startIndex, mTextUpdateInfo.mStartGlyphIndex, requestedNumberOfCharacters );
+ mVisualModel->CreateCharacterToGlyphTable( startIndex, mTextUpdateInfo.mStartGlyphIndex, requestedNumberOfCharacters );
+ updated = true;
+ }
+
+ const Length numberOfGlyphs = glyphs.Count() - currentNumberOfGlyphs;
+
+ if( NO_OPERATION != ( GET_GLYPH_METRICS & operations ) )
+ {
+ GlyphInfo* glyphsBuffer = glyphs.Begin();
+ mMetrics->GetGlyphMetrics( glyphsBuffer + mTextUpdateInfo.mStartGlyphIndex, numberOfGlyphs );
+
+ // Update the width and advance of all new paragraph characters.
+ for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(), endIt = newParagraphGlyphs.End(); it != endIt; ++it )
+ {
+ const GlyphIndex index = *it;
+ GlyphInfo& glyph = *( glyphsBuffer + index );
+
+ glyph.xBearing = 0.f;
+ glyph.width = 0.f;
+ glyph.advance = 0.f;
+ }
+ updated = true;
+ }
+
+ if( NO_OPERATION != ( COLOR & operations ) )
+ {
+ // Set the color runs in glyphs.
+ SetColorSegmentationInfo( mLogicalModel->mColorRuns,
+ mVisualModel->mCharactersToGlyph,
+ mVisualModel->mGlyphsPerCharacter,
+ startIndex,
+ mTextUpdateInfo.mStartGlyphIndex,
+ requestedNumberOfCharacters,
+ mVisualModel->mColors,
+ mVisualModel->mColorIndices );
+
+ updated = true;
+ }
+
+ if( ( NULL != mEventData ) &&
+ mEventData->mPreEditFlag &&
+ ( 0u != mVisualModel->mCharactersToGlyph.Count() ) )
+ {
+ // Add the underline for the pre-edit text.
+ const GlyphIndex* const charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
+ const Length* const glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
+
+ const GlyphIndex glyphStart = *( charactersToGlyphBuffer + mEventData->mPreEditStartPosition );
+ const CharacterIndex lastPreEditCharacter = mEventData->mPreEditStartPosition + ( ( mEventData->mPreEditLength > 0u ) ? mEventData->mPreEditLength - 1u : 0u );
+ const Length numberOfGlyphsLastCharacter = *( glyphsPerCharacterBuffer + lastPreEditCharacter );
+ const GlyphIndex glyphEnd = *( charactersToGlyphBuffer + lastPreEditCharacter ) + ( numberOfGlyphsLastCharacter > 1u ? numberOfGlyphsLastCharacter - 1u : 0u );
+
+ GlyphRun underlineRun;
+ underlineRun.glyphIndex = glyphStart;
+ underlineRun.numberOfGlyphs = 1u + glyphEnd - glyphStart;
+
+ // TODO: At the moment the underline runs are only for pre-edit.
+ mVisualModel->mUnderlineRuns.PushBack( underlineRun );
+ }
+
+ // The estimated number of lines. Used to avoid reallocations when layouting.
+ mTextUpdateInfo.mEstimatedNumberOfLines = std::max( mVisualModel->mLines.Count(), mLogicalModel->mParagraphInfo.Count() );
+
+ // Set the previous number of characters for the next time the text is updated.
+ mTextUpdateInfo.mPreviousNumberOfCharacters = numberOfCharacters;
+
+ return updated;
+}
+
+void Controller::Impl::RetrieveDefaultInputStyle( InputStyle& inputStyle )
+{
+ // Sets the default text's color.
+ inputStyle.textColor = mTextColor;
+ inputStyle.isDefaultColor = true;
+
+ inputStyle.familyName.clear();
+ inputStyle.weight = TextAbstraction::FontWeight::NORMAL;
+ inputStyle.width = TextAbstraction::FontWidth::NORMAL;
+ inputStyle.slant = TextAbstraction::FontSlant::NORMAL;
+ inputStyle.size = 0.f;
+
+ inputStyle.familyDefined = false;
+ inputStyle.weightDefined = false;
+ inputStyle.widthDefined = false;
+ inputStyle.slantDefined = false;
+ inputStyle.sizeDefined = false;
+
+ // Sets the default font's family name, weight, width, slant and size.
+ if( mFontDefaults )
+ {
+ if( mFontDefaults->familyDefined )
+ {
+ inputStyle.familyName = mFontDefaults->mFontDescription.family;
+ inputStyle.familyDefined = true;
+ }
+
+ if( mFontDefaults->weightDefined )
+ {
+ inputStyle.weight = mFontDefaults->mFontDescription.weight;
+ inputStyle.weightDefined = true;
+ }
+
+ if( mFontDefaults->widthDefined )
+ {
+ inputStyle.width = mFontDefaults->mFontDescription.width;
+ inputStyle.widthDefined = true;
+ }
+
+ if( mFontDefaults->slantDefined )
+ {
+ inputStyle.slant = mFontDefaults->mFontDescription.slant;
+ inputStyle.slantDefined = true;
+ }
+
+ if( mFontDefaults->sizeDefined )
+ {
+ inputStyle.size = mFontDefaults->mDefaultPointSize;
+ inputStyle.sizeDefined = true;
+ }
+ }
+}
+
+float Controller::Impl::GetDefaultFontLineHeight()
+{
+ FontId defaultFontId = 0u;
+ if( NULL == mFontDefaults )
+ {
+ TextAbstraction::FontDescription fontDescription;
+ defaultFontId = mFontClient.GetFontId( fontDescription );
+ }
+ else
+ {
+ defaultFontId = mFontDefaults->GetFontId( mFontClient );
+ }
+
+ Text::FontMetrics fontMetrics;
+ mMetrics->GetFontMetrics( defaultFontId, fontMetrics );
+
+ return( fontMetrics.ascender - fontMetrics.descender );
+}
+
+void Controller::Impl::OnCursorKeyEvent( const Event& event )
+{
+ if( NULL == mEventData )
+ {
+ // Nothing to do if there is no text input.
+ return;