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-style-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,
84 if( 0u == numberOfRuns )
86 mVisualToLogicalMap.Clear();
87 mLogicalToVisualMap.Clear();
88 mVisualToLogicalCursorMap.Clear();
92 const Length numberOfCharacters = mText.Count();
93 mVisualToLogicalMap.Resize( numberOfCharacters );
94 mLogicalToVisualMap.Resize( numberOfCharacters );
96 const Length numberOfCharactersPlus = numberOfCharacters + 1u;
97 mVisualToLogicalCursorMap.Resize( numberOfCharactersPlus );
99 CharacterIndex* modelVisualToLogicalMapBuffer = mVisualToLogicalMap.Begin();
100 CharacterIndex* modelLogicalToVisualMapBuffer = mLogicalToVisualMap.Begin();
102 CharacterIndex* modelVisualToLogicalCursorMap = mVisualToLogicalCursorMap.Begin();
104 CharacterIndex lastIndex = 0u;
105 for( unsigned int bidiIndex = 0u; bidiIndex < numberOfRuns; ++bidiIndex )
107 const BidirectionalLineInfoRun& bidiLineInfo = *( bidirectionalInfo + bidiIndex );
109 if( lastIndex < bidiLineInfo.characterRun.characterIndex )
111 // Fill with the identity.
112 for( ; lastIndex < bidiLineInfo.characterRun.characterIndex; ++lastIndex )
114 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
118 // Fill the conversion table of the run.
119 for( CharacterIndex index = 0u;
120 index < bidiLineInfo.characterRun.numberOfCharacters;
121 ++index, ++lastIndex )
123 *( modelVisualToLogicalMapBuffer + lastIndex ) = bidiLineInfo.characterRun.characterIndex + *( bidiLineInfo.visualToLogicalMap + index );
127 // Complete with the identity if there are some left to right characters after the last right to left.
128 for( ; lastIndex < numberOfCharacters; ++lastIndex )
130 *( modelVisualToLogicalMapBuffer + lastIndex ) = lastIndex;
133 // Sets the logical to visual conversion map.
134 for( CharacterIndex index = 0u; index < numberOfCharacters; ++index )
136 *( modelLogicalToVisualMapBuffer + *( modelVisualToLogicalMapBuffer + index ) ) = index;
139 // Sets the visual to logical conversion map for cursor positions.
141 const Length numberOfBidirectionalParagraphs = mBidirectionalParagraphInfo.Count();
142 BidirectionalParagraphInfoRun* bidirectionalParagraphInfoBuffer = mBidirectionalParagraphInfo.Begin();
143 BidirectionalParagraphInfoRun* bidirectionalParagraph = bidirectionalParagraphInfoBuffer;
145 const CharacterDirection* const modelCharacterDirections = mCharacterDirections.Begin();
147 Length bidirectionalParagraphIndex = 0u;
148 bool isRightToLeftParagraph = false;
149 for( CharacterIndex index = 0u; index < numberOfCharactersPlus; ++index )
151 if( bidirectionalParagraph &&
152 ( bidirectionalParagraph->characterRun.characterIndex == index ) )
154 isRightToLeftParagraph = *( modelCharacterDirections + index );
159 if( isRightToLeftParagraph )
161 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
163 else // else logical position is zero.
165 *( modelVisualToLogicalCursorMap + index ) = 0u;
168 else if( numberOfCharacters == index )
170 if( isRightToLeftParagraph )
172 *( modelVisualToLogicalCursorMap + index ) = 0u;
174 else // else logical position is the number of characters.
176 *( modelVisualToLogicalCursorMap + index ) = numberOfCharacters;
181 // Get the character indexed by index - 1 and index
182 // and calculate the logical position according the directions of
183 // both characters and the direction of the paragraph.
185 const CharacterIndex previousIndex = index - 1u;
186 const CharacterIndex logicalPosition0 = *( modelVisualToLogicalMapBuffer + previousIndex );
187 const CharacterIndex logicalPosition1 = *( modelVisualToLogicalMapBuffer + index );
189 const CharacterDirection direction0 = *( modelCharacterDirections + logicalPosition0 );
190 const CharacterDirection direction1 = *( modelCharacterDirections + logicalPosition1 );
192 if( direction0 == direction1 )
194 // Both glyphs have the same direction.
197 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
201 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
206 if( isRightToLeftParagraph )
210 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1 + 1u;
214 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0;
221 *( modelVisualToLogicalCursorMap + index ) = logicalPosition1;
225 *( modelVisualToLogicalCursorMap + index ) = logicalPosition0 + 1u;
231 if( bidirectionalParagraph &&
232 ( bidirectionalParagraph->characterRun.characterIndex + bidirectionalParagraph->characterRun.numberOfCharacters == index ) )
234 isRightToLeftParagraph = false;
235 ++bidirectionalParagraphIndex;
236 if( bidirectionalParagraphIndex < numberOfBidirectionalParagraphs )
238 bidirectionalParagraph = bidirectionalParagraphInfoBuffer + bidirectionalParagraphIndex;
242 bidirectionalParagraph = NULL;
249 CharacterIndex LogicalModel::GetLogicalCharacterIndex( CharacterIndex visualCharacterIndex ) const
251 if( 0u == mVisualToLogicalMap.Count() )
253 // If there is no visual to logical info is because the whole text is left to right.
254 // Return the identity.
255 return visualCharacterIndex;
258 return *( mVisualToLogicalMap.Begin() + visualCharacterIndex );
261 void LogicalModel::UpdateTextStyleRuns( CharacterIndex index, int numberOfCharacters )
263 const Length totalNumberOfCharacters = mText.Count();
265 // Process the color runs.
266 Vector<ColorRun> removedColorRuns;
267 UpdateCharacterRuns<ColorRun>( index,
269 totalNumberOfCharacters,
273 // Process the font description runs.
274 Vector<FontDescriptionRun> removedFontDescriptionRuns;
275 UpdateCharacterRuns<FontDescriptionRun>( index,
277 totalNumberOfCharacters,
278 mFontDescriptionRuns,
279 removedFontDescriptionRuns );
281 // Free memory allocated for the font family name.
282 FreeFontFamilyNames( removedFontDescriptionRuns );
285 void LogicalModel::RetrieveStyle( CharacterIndex index, InputStyle& style )
287 unsigned int runIndex = 0u;
289 // Set the text color.
290 bool colorOverriden = false;
291 unsigned int colorIndex = 0u;
292 const ColorRun* const colorRunsBuffer = mColorRuns.Begin();
293 for( Vector<ColorRun>::ConstIterator it = colorRunsBuffer,
294 endIt = mColorRuns.End();
298 const ColorRun& colorRun = *it;
300 if( ( colorRun.characterRun.characterIndex <= index ) &&
301 ( index < colorRun.characterRun.characterIndex + colorRun.characterRun.numberOfCharacters ) )
303 colorIndex = runIndex;
304 colorOverriden = true;
308 // Set the text's color if it's overriden.
311 style.textColor = ( *( colorRunsBuffer + colorIndex ) ).color;
314 // Reset the run index.
317 // Set the font's parameters.
318 bool nameOverriden = false;
319 bool weightOverriden = false;
320 bool widthOverriden = false;
321 bool slantOverriden = false;
322 bool sizeOverriden = false;
323 unsigned int nameIndex = 0u;
324 unsigned int weightIndex = 0u;
325 unsigned int widthIndex = 0u;
326 unsigned int slantIndex = 0u;
327 unsigned int sizeIndex = 0u;
328 const FontDescriptionRun* const fontDescriptionRunsBuffer = mFontDescriptionRuns.Begin();
329 for( Vector<FontDescriptionRun>::ConstIterator it = fontDescriptionRunsBuffer,
330 endIt = mFontDescriptionRuns.End();
334 const FontDescriptionRun& fontDescriptionRun = *it;
336 if( ( fontDescriptionRun.characterRun.characterIndex <= index ) &&
337 ( index < fontDescriptionRun.characterRun.characterIndex + fontDescriptionRun.characterRun.numberOfCharacters ) )
339 if( fontDescriptionRun.familyDefined )
341 nameIndex = runIndex;
342 nameOverriden = true;
345 if( fontDescriptionRun.weightDefined )
347 weightIndex = runIndex;
348 weightOverriden = true;
351 if( fontDescriptionRun.widthDefined )
353 widthIndex = runIndex;
354 widthOverriden = true;
357 if( fontDescriptionRun.slantDefined )
359 slantIndex = runIndex;
360 slantOverriden = true;
363 if( fontDescriptionRun.sizeDefined )
365 sizeIndex = runIndex;
366 sizeOverriden = true;
371 // Set the font's family name if it's overriden.
374 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + nameIndex );
376 style.familyName = std::string( fontDescriptionRun.familyName, fontDescriptionRun.familyLength );
377 style.familyDefined = true;
380 // Set the font's weight if it's overriden.
381 if( weightOverriden )
383 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + weightIndex );
385 style.weight = fontDescriptionRun.weight;
386 style.weightDefined = true;
389 // Set the font's width if it's overriden.
392 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + widthIndex );
394 style.width = fontDescriptionRun.width;
395 style.widthDefined = true;
398 // Set the font's slant if it's overriden.
401 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + slantIndex );
403 style.slant = fontDescriptionRun.slant;
404 style.slantDefined = true;
407 // Set the font's size if it's overriden.
410 const FontDescriptionRun& fontDescriptionRun = *( fontDescriptionRunsBuffer + sizeIndex );
412 style.size = static_cast<float>( fontDescriptionRun.size ) / 64.f;
413 style.sizeDefined = true;
416 // Reset the run index.
420 void LogicalModel::ClearFontDescriptionRuns()
422 FreeFontFamilyNames( mFontDescriptionRuns );
425 LogicalModel::~LogicalModel()
427 ClearFontDescriptionRuns();
430 LogicalModel::LogicalModel()
436 } // namespace Toolkit