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>
22 #include <dali-toolkit/internal/text/input-style.h>
23 #include <dali-toolkit/internal/text/text-run-container.h>
34 void FreeFontFamilyNames( Vector<FontDescriptionRun>& fontDescriptionRuns )
36 for( Vector<FontDescriptionRun>::Iterator it = fontDescriptionRuns.Begin(),
37 endIt = fontDescriptionRuns.End();
41 delete (*it).familyName;
44 fontDescriptionRuns.Clear();
47 LogicalModelPtr LogicalModel::New()
49 return LogicalModelPtr( new LogicalModel() );
52 Script LogicalModel::GetScript( CharacterIndex characterIndex ) const
54 // If this operation is too slow, consider a binary search.
56 for( Length index = 0u, length = mScriptRuns.Count(); index < length; ++index )
58 const ScriptRun* const scriptRun = mScriptRuns.Begin() + index;
60 if( ( scriptRun->characterRun.characterIndex <= characterIndex ) &&
61 ( characterIndex < scriptRun->characterRun.characterIndex + scriptRun->characterRun.numberOfCharacters ) )
63 return scriptRun->script;
67 return TextAbstraction::UNKNOWN;
70 CharacterDirection LogicalModel::GetCharacterDirection( CharacterIndex characterIndex ) const
72 if( characterIndex >= mCharacterDirections.Count() )
74 // The model has no right to left characters, so the vector of directions is void.
78 return *( mCharacterDirections.Begin() + characterIndex );
81 void LogicalModel::SetVisualToLogicalMap( const BidirectionalLineInfoRun* const bidirectionalInfo,
83 CharacterIndex startIndex,
84 Length numberOfCharacters )
86 mVisualToLogicalMap.Resize( numberOfCharacters );
87 mLogicalToVisualMap.Resize( numberOfCharacters );
89 const Length numberOfCharactersPlus = numberOfCharacters + 1u;
90 mVisualToLogicalCursorMap.Resize( numberOfCharactersPlus );
92 CharacterIndex* modelVisualToLogicalMapBuffer = mVisualToLogicalMap.Begin();
93 CharacterIndex* modelLogicalToVisualMapBuffer = mLogicalToVisualMap.Begin();
95 CharacterIndex* modelVisualToLogicalCursorMap = mVisualToLogicalCursorMap.Begin();
97 CharacterIndex lastIndex = startIndex;
98 for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
100 const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
102 if( bidiLineInfo.characterRun.characterIndex + bidiLineInfo.characterRun.numberOfCharacters <= startIndex )
104 // Skip this paragraph. It has been already processed.
108 if( lastIndex < bidiLineInfo.characterRun.characterIndex )
110 // Fill with the identity.
111 for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
113 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
117 // Fill the conversion table of the run.
118 for( CharacterIndex index = 0u;
119 index < bidiLineInfo.characterRun.numberOfCharacters;
120 ++index, ++lastIndex )
122 *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
126 // Complete with the identity if there are some left to right characters after the last right to left.
127 for( ; lastIndex < numberOfCharacters; ++lastIndex )
129 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
132 // Sets the logical to visual conversion map.
133 for( CharacterIndex index = startIndex; index < numberOfCharacters; ++index )
135 *( modelLogicalToVisualMapBuffer + *( modelVisualToLogicalMapBuffer + index ) ) = index;
138 // Sets the visual to logical conversion map for cursor positions.
140 const Length numberOfBidirectionalParagraphs = mBidirectionalParagraphInfo.Count();
141 BidirectionalParagraphInfoRun* bidirectionalParagraphInfoBuffer = mBidirectionalParagraphInfo.Begin();
142 BidirectionalParagraphInfoRun* bidirectionalParagraph = bidirectionalParagraphInfoBuffer;
144 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
146 Length bidirectionalParagraphIndex = 0u;
147 bool isRightToLeftParagraph = false;
148 for( CharacterIndex index = startIndex; index < numberOfCharactersPlus; ++index )
150 if( bidirectionalParagraph &&
151 ( bidirectionalParagraph->characterRun.characterIndex == index ) )
153 isRightToLeftParagraph = *( modelCharacterDirections + index );
158 if( isRightToLeftParagraph )
160 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
162 else // else logical position is zero.
164 *( modelVisualToLogicalCursorMap + index ) = 0u;
167 else if( numberOfCharacters == index )
169 if( isRightToLeftParagraph )
171 *( modelVisualToLogicalCursorMap + index ) = 0u;
173 else // else logical position is the number of characters.
175 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
180 // Get the character indexed by index - 1 and index
181 // and calculate the logical position according the directions of
182 // both characters and the direction of the paragraph.
184 const CharacterIndex previousIndex = index - 1u;
185 const CharacterIndex logicalPosition0 = *( modelVisualToLogicalMapBuffer + previousIndex );
186 const CharacterIndex logicalPosition1 = *( modelVisualToLogicalMapBuffer + index );
188 const CharacterDirection direction0 = *( modelCharacterDirections + logicalPosition0 );
189 const CharacterDirection direction1 = *( modelCharacterDirections + logicalPosition1 );
191 if( direction0 == direction1 )
193 // Both glyphs have the same direction.
196 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
200 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
205 if( isRightToLeftParagraph )
209 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1 + 1u;
213 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
220 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
224 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0 + 1u;
230 if( bidirectionalParagraph &&
231 ( bidirectionalParagraph->characterRun.characterIndex + bidirectionalParagraph->characterRun.numberOfCharacters == index ) )
233 isRightToLeftParagraph = false;
234 ++bidirectionalParagraphIndex;
235 if( bidirectionalParagraphIndex < numberOfBidirectionalParagraphs )
237 bidirectionalParagraph = bidirectionalParagraphInfoBuffer + bidirectionalParagraphIndex;
241 bidirectionalParagraph = NULL;
247 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
249 if( 0u == mVisualToLogicalMap.Count() )
251 // If there is no visual to logical info is because the whole text is left to right.
252 // Return the identity.
253 return visualCharacterIndex;
256 return *( mVisualToLogicalMap.Begin() + visualCharacterIndex );
259 void LogicalModel::UpdateTextStyleRuns( CharacterIndex index, int numberOfCharacters )
261 const Length totalNumberOfCharacters = mText.Count();
263 // Process the color runs.
264 Vector<ColorRun> removedColorRuns;
265 UpdateCharacterRuns<ColorRun>( index,
267 totalNumberOfCharacters,
271 // Process the font description runs.
272 Vector<FontDescriptionRun> removedFontDescriptionRuns;
273 UpdateCharacterRuns<FontDescriptionRun>( index,
275 totalNumberOfCharacters,
276 mFontDescriptionRuns,
277 removedFontDescriptionRuns );
279 // Free memory allocated for the font family name.
280 FreeFontFamilyNames( removedFontDescriptionRuns );
283 void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
285 unsigned int runIndex = 0u;
287 // Set the text color.
288 bool colorOverriden = false;
289 unsigned int colorIndex = 0u;
290 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
291 for( Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
292 endIt = mColorRuns.End();
296 const ColorRun& colorRun = *it;
298 if( ( colorRun.characterRun.characterIndex <= index ) &&
299 ( index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters ) )
301 colorIndex = runIndex;
302 colorOverriden = true;
306 // Set the text's color if it's overriden.
309 style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
312 // Reset the run index.
315 // Set the font's parameters.
316 bool nameOverriden = false;
317 bool weightOverriden = false;
318 bool widthOverriden = false;
319 bool slantOverriden = false;
320 bool sizeOverriden = false;
321 unsigned int nameIndex = 0u;
322 unsigned int weightIndex = 0u;
323 unsigned int widthIndex = 0u;
324 unsigned int slantIndex = 0u;
325 unsigned int sizeIndex = 0u;
326 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
327 for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
328 endIt = mFontDescriptionRuns.End();
332 const FontDescriptionRun& fontDescriptionRun = *it;
334 if( ( fontDescriptionRun.characterRun.characterIndex <= index ) &&
335 ( index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters ) )
337 if( fontDescriptionRun.familyDefined )
339 nameIndex = runIndex;
340 nameOverriden = true;
343 if( fontDescriptionRun.weightDefined )
345 weightIndex = runIndex;
346 weightOverriden = true;
349 if( fontDescriptionRun.widthDefined )
351 widthIndex = runIndex;
352 widthOverriden = true;
355 if( fontDescriptionRun.slantDefined )
357 slantIndex = runIndex;
358 slantOverriden = true;
361 if( fontDescriptionRun.sizeDefined )
363 sizeIndex = runIndex;
364 sizeOverriden = true;
369 // Set the font's family name if it's overriden.
372 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
374 style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
375 style.familyDefined = true;
378 // Set the font's weight if it's overriden.
379 if( weightOverriden )
381 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
383 style.weight = fontDescriptionRun.weight;
384 style.weightDefined = true;
387 // Set the font's width if it's overriden.
390 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
392 style.width = fontDescriptionRun.width;
393 style.widthDefined = true;
396 // Set the font's slant if it's overriden.
399 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
401 style.slant = fontDescriptionRun.slant;
402 style.slantDefined = true;
405 // Set the font's size if it's overriden.
408 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
410 style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
411 style.sizeDefined = true;
414 // Reset the run index.
418 void LogicalModel::ClearFontDescriptionRuns()
420 FreeFontFamilyNames( mFontDescriptionRuns );
423 LogicalModel::~LogicalModel()
425 ClearFontDescriptionRuns();
428 LogicalModel::LogicalModel()
434 } // namespace Toolkit