2 * Copyright (c) 2016 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/cursor-helper-functions.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
34 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction.
38 FindWordData( const Dali::Toolkit::Text::Character* const textBuffer,
39 Dali::Toolkit::Text::Length totalNumberOfCharacters,
40 Dali::Toolkit::Text::CharacterIndex hitCharacter,
43 : textBuffer( textBuffer ),
44 totalNumberOfCharacters( totalNumberOfCharacters ),
45 hitCharacter( hitCharacter ),
47 isWhiteSpace( isWhiteSpace ),
48 isNewParagraph( isNewParagraph )
54 const Dali::Toolkit::Text::Character* const textBuffer;
55 Dali::Toolkit::Text::Length totalNumberOfCharacters;
56 Dali::Toolkit::Text::CharacterIndex hitCharacter;
57 Dali::Toolkit::Text::CharacterIndex foundIndex;
58 bool isWhiteSpace : 1u;
59 bool isNewParagraph : 1u;
62 bool IsWhiteSpaceOrNewParagraph( Dali::Toolkit::Text::Character character,
64 bool isHitWhiteSpaceOrNewParagraph )
66 bool isWhiteSpaceOrNewParagraph = false;
67 if( isHitWhiteSpaceOrNewParagraph )
71 // Whether the current character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
72 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace( character ) && !Dali::TextAbstraction::IsNewParagraph( character );
76 // Whether the current character is a new paragraph character.
77 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsNewParagraph( character );
82 // Whether the current character is a white space or a new paragraph character (note the new paragraph character is a white space as well).
83 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace( character );
86 return isWhiteSpaceOrNewParagraph;
89 void FindStartOfWord( FindWordData& data )
91 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
93 for( data.foundIndex = data.hitCharacter; data.foundIndex > 0; --data.foundIndex )
95 const Dali::Toolkit::Text::Character character = *( data.textBuffer + data.foundIndex - 1u );
97 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph( character,
99 isHitWhiteSpaceOrNewParagraph );
101 if( isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph )
108 void FindEndOfWord( FindWordData& data )
110 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
112 for( data.foundIndex = data.hitCharacter + 1u; data.foundIndex < data.totalNumberOfCharacters; ++data.foundIndex )
114 const Dali::Toolkit::Text::Character character = *( data.textBuffer + data.foundIndex );
116 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph( character,
118 isHitWhiteSpaceOrNewParagraph );
120 if( isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph )
138 LineIndex GetClosestLine( VisualModelPtr visualModel,
141 float totalHeight = 0.f;
142 LineIndex lineIndex = 0u;
144 const Vector<LineRun>& lines = visualModel->mLines;
146 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
151 const LineRun& lineRun = *it;
153 // The line height is the addition of the line ascender and the line descender.
154 // However, the line descender has a negative value, hence the subtraction.
155 totalHeight += lineRun.ascender - lineRun.descender;
157 if( visualY < totalHeight )
163 if( lineIndex == 0u )
171 float CalculateLineOffset( const Vector<LineRun>& lines,
172 LineIndex lineIndex )
176 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
177 endIt = lines.Begin() + lineIndex;
181 const LineRun& lineRun = *it;
183 // The line height is the addition of the line ascender and the line descender.
184 // However, the line descender has a negative value, hence the subtraction.
185 offset += lineRun.ascender - lineRun.descender;
191 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
192 LogicalModelPtr logicalModel,
197 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY );
199 CharacterIndex logicalIndex = 0u;
201 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
202 const Length totalNumberOfLines = visualModel->mLines.Count();
203 if( ( 0 == totalNumberOfGlyphs ) ||
204 ( 0 == totalNumberOfLines ) )
209 // Find which line is closest.
210 const LineIndex lineIndex = Text::GetClosestLine( visualModel,
213 // Convert from text's coords to line's coords.
214 const LineRun& line = *( visualModel->mLines.Begin() + lineIndex );
216 // Transform the tap point from text's coords to line's coords.
217 visualX -= line.alignmentOffset;
219 // Get the positions of the glyphs.
220 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
222 // Get the character to glyph conversion table.
223 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
225 // Get the glyphs per character table.
226 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
228 // Get the characters per glyph table.
229 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
231 // Get the glyph's info buffer.
232 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
234 const CharacterIndex startCharacter = line.characterRun.characterIndex;
235 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
236 DALI_ASSERT_DEBUG( endCharacter <= logicalModel->mText.Count() && "Invalid line info" );
238 // Whether this line is a bidirectional line.
239 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( startCharacter );
241 // The character's direction buffer.
242 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
244 // Whether there is a hit on a glyph.
245 bool matched = false;
247 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
248 CharacterIndex visualIndex = startCharacter;
249 Length numberOfVisualCharacters = 0u;
250 for( ; visualIndex < endCharacter; ++visualIndex )
252 // The character in logical order.
253 const CharacterIndex characterLogicalOrderIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( visualIndex ) : visualIndex );
254 const CharacterDirection direction = ( bidiLineFetched ? *( directionsBuffer + characterLogicalOrderIndex ) : LTR );
256 // The number of glyphs for that character
257 const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + characterLogicalOrderIndex );
258 ++numberOfVisualCharacters;
260 if( 0u != numberOfGlyphs )
262 // Get the first character/glyph of the group of glyphs.
263 const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfVisualCharacters;
264 const CharacterIndex firstLogicalCharacterIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( firstVisualCharacterIndex ) : firstVisualCharacterIndex );
265 const GlyphIndex firstLogicalGlyphIndex = *( charactersToGlyphBuffer + firstLogicalCharacterIndex );
267 // Get the metrics for the group of glyphs.
268 GlyphMetrics glyphMetrics;
269 GetGlyphsMetrics( firstLogicalGlyphIndex,
275 // Get the position of the first glyph.
276 const Vector2& position = *( positionsBuffer + firstLogicalGlyphIndex );
278 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
279 Length numberOfCharacters = *( charactersPerGlyphBuffer + firstLogicalGlyphIndex );
280 if( direction != LTR )
282 // As characters are being traversed in visual order,
283 // for right to left ligatures, the character which contains the
284 // number of glyphs in the table is found first.
285 // Jump the number of characters to the next glyph is needed.
287 if( 0u == numberOfCharacters )
289 // TODO: This is a workaround to fix an issue with complex characters in the arabic
290 // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
291 // There are characters that are not shaped in one glyph but in combination with
292 // the next one generates two of them.
293 // The visual to logical conversion table have characters in different order than
294 // expected even if all of them are arabic.
296 // The workaround doesn't fix the issue completely but it prevents the application
297 // to hang in an infinite loop.
299 // Find the number of characters.
300 for( GlyphIndex index = firstLogicalGlyphIndex + 1u;
301 ( 0u == numberOfCharacters ) && ( index < totalNumberOfGlyphs ) ;
304 numberOfCharacters = *( charactersPerGlyphBuffer + index );
307 if( 2u > numberOfCharacters )
312 --numberOfCharacters;
315 visualIndex += numberOfCharacters - 1u;
318 // Get the script of the character.
319 const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
321 const bool isInterglyphIndex = ( numberOfCharacters > numberOfGlyphs ) && HasLigatureMustBreak( script );
322 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
323 const float glyphAdvance = glyphMetrics.advance / static_cast<float>( numberOfBlocks );
325 CharacterIndex index = 0u;
326 for( ; index < numberOfBlocks; ++index )
328 // Find the mid-point of the area containing the glyph
329 const float glyphCenter = -glyphMetrics.xBearing + position.x + ( static_cast<float>( index ) + 0.5f ) * glyphAdvance;
331 if( visualX < glyphCenter )
340 // If the glyph is shaped from more than one character, it matches the character of the glyph.
341 visualIndex = firstVisualCharacterIndex + index;
345 numberOfVisualCharacters = 0u;
349 // The number of characters of the whole text.
350 const Length totalNumberOfCharacters = logicalModel->mText.Count();
352 // Return the logical position of the cursor in characters.
356 // If no character is matched, then the last character (in visual order) of the line is used.
357 visualIndex = endCharacter;
360 // Get the paragraph direction.
361 const CharacterDirection paragraphDirection = line.direction;
363 if( totalNumberOfCharacters != visualIndex )
365 // The visual index is not at the end of the text.
367 if( LTR == paragraphDirection )
369 // The paragraph direction is left to right.
371 if( visualIndex == endCharacter )
373 // It places the cursor just before the last character in visual order.
374 // i.e. it places the cursor just before the '\n' or before the last character
375 // if there is a long line with no word breaks which is wrapped.
377 // It doesn't check if the closest line is the last one like the RTL branch below
378 // because the total number of characters is different than the visual index and
379 // the visual index is the last character of the line.
385 // The paragraph direction is right to left.
387 if( ( lineIndex != totalNumberOfLines - 1u ) && // is not the last line.
388 ( visualIndex == startCharacter ) )
390 // It places the cursor just after the first character in visual order.
391 // i.e. it places the cursor just after the '\n' or after the last character
392 // if there is a long line with no word breaks which is wrapped.
394 // If the last line doesn't end with '\n' it won't increase the visual index
395 // placing the cursor at the beginning of the line (in visual order).
402 // The visual index is at the end of text.
404 // If the text ends with a new paragraph character i.e. a '\n', an extra line with no characters is added at the end of the text.
405 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
406 // the cursor just before the last '\n'.
408 if( ( lineIndex != totalNumberOfLines - 1u ) &&
409 TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + visualIndex - 1u ) ) )
415 logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
417 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
419 DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
425 void GetCursorPosition( VisualModelPtr visualModel,
426 LogicalModelPtr logicalModel,
428 CharacterIndex logical,
429 CursorInfo& cursorInfo )
431 // Whether the logical cursor position is at the end of the whole text.
432 const bool isLastPosition = logicalModel->mText.Count() == logical;
434 // Get the line where the character is laid-out.
435 const CharacterIndex characterOfLine = isLastPosition ? ( logical - 1u ) : logical;
437 // Whether the cursor is in the last position and the last position is a new paragraph character.
438 const bool isLastNewParagraph = isLastPosition && TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + characterOfLine ) );
440 const LineRun* const modelLines = visualModel->mLines.Begin();
442 const LineIndex lineIndex = visualModel->GetLineOfCharacter( characterOfLine );
443 const LineRun& line = *( modelLines + lineIndex );
445 if( isLastNewParagraph )
447 // The cursor is in a new line with no characters. Place the cursor in that line.
448 const LineIndex newLineIndex = lineIndex + 1u;
449 const LineRun& newLine = *( modelLines + newLineIndex );
451 cursorInfo.isSecondaryCursor = false;
453 // Set the line offset and height.
454 cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
457 // The line height is the addition of the line ascender and the line descender.
458 // However, the line descender has a negative value, hence the subtraction.
459 cursorInfo.lineHeight = newLine.ascender - newLine.descender;
461 // Set the primary cursor's height.
462 cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
464 // Set the primary cursor's position.
465 cursorInfo.primaryPosition.x = 0.f;
466 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
468 // Transform the cursor info from line's coords to text's coords.
469 cursorInfo.primaryPosition.x += ( LTR == line.direction ) ? 0.f : visualModel->mControlSize.width;
473 // Whether this line is a bidirectional line.
474 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( characterOfLine );
476 // Check if the logical position is the first or the last one of the line.
477 const bool isFirstPositionOfLine = line.characterRun.characterIndex == logical;
478 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == logical;
480 // 'logical' is the logical 'cursor' index.
481 // Get the next and current logical 'character' index.
482 const CharacterIndex characterIndex = isFirstPositionOfLine ? logical : logical - 1u;
483 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : logical;
485 // The character's direction buffer.
486 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
488 CharacterDirection isCurrentRightToLeft = false;
489 CharacterDirection isNextRightToLeft = false;
490 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
492 isCurrentRightToLeft = *( directionsBuffer + characterIndex );
493 isNextRightToLeft = *( directionsBuffer + nextCharacterIndex );
496 // Get the paragraph's direction.
497 const CharacterDirection isRightToLeftParagraph = line.direction;
499 // Check whether there is an alternative position:
500 cursorInfo.isSecondaryCursor = ( ( !isLastPositionOfLine && ( isCurrentRightToLeft != isNextRightToLeft ) ) ||
501 ( isLastPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) ||
502 ( isFirstPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) );
504 // Set the line offset and height.
505 cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
508 // The line height is the addition of the line ascender and the line descender.
509 // However, the line descender has a negative value, hence the subtraction.
510 cursorInfo.lineHeight = line.ascender - line.descender;
512 // Calculate the primary cursor.
514 CharacterIndex index = characterIndex;
515 if( cursorInfo.isSecondaryCursor )
517 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
519 if( isLastPositionOfLine )
521 // The position of the cursor after the last character needs special
522 // care depending on its direction and the direction of the paragraph.
524 // Need to find the first character after the last character with the paragraph's direction.
525 // i.e l0 l1 l2 r0 r1 should find r0.
527 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
528 if( bidiLineFetched )
530 index = logicalModel->GetLogicalCharacterIndex( index );
533 else if( isFirstPositionOfLine )
535 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
536 if( bidiLineFetched )
538 index = logicalModel->GetLogicalCharacterIndex( index );
543 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
547 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
548 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
549 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
550 const CharacterIndex* const glyphsToCharactersBuffer = visualModel->mGlyphsToCharacters.Begin();
551 const Vector2* const glyphPositionsBuffer = visualModel->mGlyphPositions.Begin();
552 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
554 // Convert the cursor position into the glyph position.
555 const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
556 const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
557 const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
559 // Get the metrics for the group of glyphs.
560 GlyphMetrics glyphMetrics;
561 GetGlyphsMetrics( primaryGlyphIndex,
562 primaryNumberOfGlyphs,
567 // Whether to add the glyph's advance to the cursor position.
568 // i.e if the paragraph is left to right and the logical cursor is zero, the position is the position of the first glyph and the advance is not added,
569 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
570 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
591 // Where F -> isFirstPosition
592 // L -> isLastPosition
593 // C -> isCurrentRightToLeft
594 // P -> isRightToLeftParagraph
595 // A -> Whether to add the glyph's advance.
597 const bool addGlyphAdvance = ( ( isLastPositionOfLine && !isRightToLeftParagraph ) ||
598 ( isFirstPositionOfLine && isRightToLeftParagraph ) ||
599 ( !isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft ) );
601 float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
603 if( !isLastPositionOfLine &&
604 ( primaryNumberOfCharacters > 1u ) )
606 const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
608 bool isCurrentRightToLeft = false;
609 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
611 isCurrentRightToLeft = *( directionsBuffer + index );
614 Length numberOfGlyphAdvance = ( isFirstPositionOfLine ? 0u : 1u ) + characterIndex - firstIndex;
615 if( isCurrentRightToLeft )
617 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
620 glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
623 // Get the glyph position and x bearing (in the line's coords).
624 const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
626 // Set the primary cursor's height.
627 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
629 // Set the primary cursor's position.
630 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
631 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + line.ascender - glyphMetrics.ascender;
633 // Transform the cursor info from line's coords to text's coords.
634 cursorInfo.primaryPosition.x += line.alignmentOffset;
636 // Calculate the secondary cursor.
637 if( cursorInfo.isSecondaryCursor )
639 // Set the secondary cursor's height.
640 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
642 CharacterIndex index = characterIndex;
643 if( !isLastPositionOfLine )
645 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
648 const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
649 const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
651 const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
653 GetGlyphsMetrics( secondaryGlyphIndex,
654 secondaryNumberOfGlyphs,
659 // Set the secondary cursor's position.
672 // Where F -> isFirstPosition
673 // C -> isCurrentRightToLeft
674 // P -> isRightToLeftParagraph
675 // A -> Whether to add the glyph's advance.
677 const bool addGlyphAdvance = ( ( !isFirstPositionOfLine && !isCurrentRightToLeft ) ||
678 ( isFirstPositionOfLine && !isRightToLeftParagraph ) );
680 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( addGlyphAdvance ? glyphMetrics.advance : 0.f );
681 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
683 // Transform the cursor info from line's coords to text's coords.
684 cursorInfo.secondaryPosition.x += line.alignmentOffset;
689 bool FindSelectionIndices( VisualModelPtr visualModel,
690 LogicalModelPtr logicalModel,
694 CharacterIndex& startIndex,
695 CharacterIndex& endIndex )
700 |-------------------------------------------------------|------------------------------------------|
701 | On a word | The word |
702 | On a single white space between words | The word before or after the white space |
703 | On one of the multiple contiguous white spaces | The white spaces |
704 | On a single white space which is in the position zero | The white space and the next word |
705 | On a new paragraph character | The word or group of white spaces before |
706 |-------------------------------------------------------|------------------------------------------|
709 CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
715 const Length totalNumberOfCharacters = logicalModel->mText.Count();
717 DALI_ASSERT_DEBUG( ( hitCharacter <= totalNumberOfCharacters ) && "GetClosestCursorIndex returned out of bounds index" );
719 if( 0u == totalNumberOfCharacters )
721 // Nothing to do if the model is empty.
725 if( hitCharacter >= totalNumberOfCharacters )
727 // Closest hit character is the last character.
728 if( hitCharacter == totalNumberOfCharacters )
730 hitCharacter--; //Hit character index set to last character in logical model
734 // hitCharacter is out of bounds
739 const Character* const textBuffer = logicalModel->mText.Begin();
741 startIndex = hitCharacter;
742 endIndex = hitCharacter;
744 // Whether the hit character is a new paragraph character.
745 const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + hitCharacter ) );
747 // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
748 const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace( *( textBuffer + hitCharacter ) ) && !isHitCharacterNewParagraph;
750 FindWordData data( textBuffer,
751 totalNumberOfCharacters,
753 isHitCharacterWhiteSpace,
754 isHitCharacterNewParagraph );
756 if( isHitCharacterNewParagraph )
758 // Find the first character before the hit one which is not a new paragraph character.
760 if( hitCharacter > 0u )
762 endIndex = hitCharacter - 1u;
763 for( ; endIndex > 0; --endIndex )
765 const Dali::Toolkit::Text::Character character = *( data.textBuffer + endIndex );
767 if( !Dali::TextAbstraction::IsNewParagraph( character ) )
774 data.hitCharacter = endIndex;
775 data.isNewParagraph = false;
776 data.isWhiteSpace = TextAbstraction::IsWhiteSpace( *( textBuffer + data.hitCharacter ) );
779 // Find the start of the word.
780 FindStartOfWord( data );
781 startIndex = data.foundIndex;
783 // Find the end of the word.
784 FindEndOfWord( data );
785 endIndex = data.foundIndex;
787 if( 1u == ( endIndex - startIndex ) )
789 if( isHitCharacterWhiteSpace )
791 // Select the word before or after the white space
793 if( 0u == hitCharacter )
795 data.isWhiteSpace = false;
796 FindEndOfWord( data );
797 endIndex = data.foundIndex;
799 else if( hitCharacter > 0u )
801 // Find the start of the word.
802 data.hitCharacter = hitCharacter - 1u;
803 data.isWhiteSpace = false;
804 FindStartOfWord( data );
805 startIndex = data.foundIndex;
817 } // namespace Toolkit