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/logical-model-impl.h>
33 LogicalModelPtr LogicalModel::New()
35 return LogicalModelPtr( new LogicalModel() );
38 Script LogicalModel::GetScript( CharacterIndex characterIndex ) const
40 // If this operation is too slow, consider a binary search.
42 for( Length index = 0u, length = mScriptRuns.Count(); index < length; ++index )
44 const ScriptRun* const scriptRun = mScriptRuns.Begin() + index;
46 if( ( scriptRun->characterRun.characterIndex <= characterIndex ) &&
47 ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) )
49 return scriptRun->script;
53 return TextAbstraction::UNKNOWN;
56 CharacterDirection LogicalModel::GetCharacterDirection( CharacterIndex characterIndex ) const
58 if( characterIndex >= mCharacterDirections.Count() )
60 // The model has no right to left characters, so the vector of directions is void.
64 return *( mCharacterDirections.Begin() + characterIndex );
67 void LogicalModel::SetVisualToLogicalMap( const BidirectionalLineInfoRun* const bidirectionalInfo,
70 if( 0u == numberOfRuns )
72 mVisualToLogicalMap.Clear();
73 mLogicalToVisualMap.Clear();
74 mVisualToLogicalCursorMap.Clear();
78 const Length numberOfCharacters = mText.Count();
79 mVisualToLogicalMap.Resize( numberOfCharacters );
80 mLogicalToVisualMap.Resize( numberOfCharacters );
82 const Length numberOfCharactersPlus = numberOfCharacters + 1u;
83 mVisualToLogicalCursorMap.Resize( numberOfCharactersPlus );
85 CharacterIndex* modelVisualToLogicalMapBuffer = mVisualToLogicalMap.Begin();
86 CharacterIndex* modelLogicalToVisualMapBuffer = mLogicalToVisualMap.Begin();
88 CharacterIndex* modelVisualToLogicalCursorMap = mVisualToLogicalCursorMap.Begin();
90 CharacterIndex lastIndex = 0u;
91 for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
93 const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
95 if( lastIndex < bidiLineInfo.characterRun.characterIndex )
97 // Fill with the identity.
98 for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
100 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
104 // Fill the conversion table of the run.
105 for( CharacterIndex index = 0u;
106 index < bidiLineInfo.characterRun.numberOfCharacters;
107 ++index, ++lastIndex )
109 *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
113 // Complete with the identity if there are some left to right characters after the last right to left.
114 for( ; lastIndex < numberOfCharacters; ++lastIndex )
116 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
119 // Sets the logical to visual conversion map.
120 for( CharacterIndex index = 0u; index < numberOfCharacters; ++index )
122 *( modelLogicalToVisualMapBuffer + *( modelVisualToLogicalMapBuffer + index ) ) = index;
125 // Sets the visual to logical conversion map for cursor positions.
127 const Length numberOfBidirectionalParagraphs = mBidirectionalParagraphInfo.Count();
128 BidirectionalParagraphInfoRun* bidirectionalParagraphInfoBuffer = mBidirectionalParagraphInfo.Begin();
129 BidirectionalParagraphInfoRun* bidirectionalParagraph = bidirectionalParagraphInfoBuffer;
131 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
133 Length bidirectionalParagraphIndex = 0u;
134 bool isRightToLeftParagraph = false;
135 for( CharacterIndex index = 0u; index < numberOfCharactersPlus; ++index )
137 if( bidirectionalParagraph &&
138 ( bidirectionalParagraph->characterRun.characterIndex == index ) )
140 isRightToLeftParagraph = *( modelCharacterDirections + index );
145 if( isRightToLeftParagraph )
147 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
149 else // else logical position is zero.
151 *( modelVisualToLogicalCursorMap + index ) = 0u;
154 else if( numberOfCharacters == index )
156 if( isRightToLeftParagraph )
158 *( modelVisualToLogicalCursorMap + index ) = 0u;
160 else // else logical position is the number of characters.
162 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
167 // Get the character indexed by index - 1 and index
168 // and calculate the logical position according the directions of
169 // both characters and the direction of the paragraph.
171 const CharacterIndex previousIndex = index - 1u;
172 const CharacterIndex logicalPosition0 = *( modelVisualToLogicalMapBuffer + previousIndex );
173 const CharacterIndex logicalPosition1 = *( modelVisualToLogicalMapBuffer + index );
175 const CharacterDirection direction0 = *( modelCharacterDirections + logicalPosition0 );
176 const CharacterDirection direction1 = *( modelCharacterDirections + logicalPosition1 );
178 if( direction0 == direction1 )
180 // Both glyphs have the same direction.
183 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
187 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
192 if( isRightToLeftParagraph )
196 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1 + 1u;
200 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
207 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
211 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0 + 1u;
217 if( bidirectionalParagraph &&
218 ( bidirectionalParagraph->characterRun.characterIndex + bidirectionalParagraph->characterRun.numberOfCharacters == index ) )
220 isRightToLeftParagraph = false;
221 ++bidirectionalParagraphIndex;
222 if( bidirectionalParagraphIndex < numberOfBidirectionalParagraphs )
224 bidirectionalParagraph = bidirectionalParagraphInfoBuffer + bidirectionalParagraphIndex;
228 bidirectionalParagraph = NULL;
235 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
237 if( 0u == mVisualToLogicalMap.Count() )
239 // If there is no visual to logical info is because the whole text is left to right.
240 // Return the identity.
241 return visualCharacterIndex;
244 return *( mVisualToLogicalMap.Begin() + visualCharacterIndex );
247 LogicalModel::~LogicalModel()
251 LogicalModel::LogicalModel()
257 } // namespace Toolkit