Fix for the cursor position with the arabic script.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / cursor-helper-functions.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // FILE HEADER
19 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
26
27 namespace
28 {
29
30 #if defined(DEBUG_ENABLED)
31   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
32 #endif
33
34 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction.
35
36 } //namespace
37
38 namespace Dali
39 {
40
41 namespace Toolkit
42 {
43
44 namespace Text
45 {
46
47 LineIndex GetClosestLine( VisualModelPtr visualModel,
48                           float visualY )
49 {
50   float totalHeight = 0.f;
51   LineIndex lineIndex = 0u;
52
53   const Vector<LineRun>& lines = visualModel->mLines;
54
55   for( Vector<LineRun>::ConstIterator it = lines.Begin(),
56          endIt = lines.End();
57        it != endIt;
58        ++it, ++lineIndex )
59   {
60     const LineRun& lineRun = *it;
61
62     // The line height is the addition of the line ascender and the line descender.
63     // However, the line descender has a negative value, hence the subtraction.
64     totalHeight += lineRun.ascender - lineRun.descender;
65
66     if( visualY < totalHeight )
67     {
68       return lineIndex;
69     }
70   }
71
72   if( lineIndex == 0u )
73   {
74     return 0;
75   }
76
77   return lineIndex-1;
78 }
79
80 float CalculateLineOffset( const Vector<LineRun>& lines,
81                            LineIndex lineIndex )
82 {
83   float offset = 0.f;
84
85   for( Vector<LineRun>::ConstIterator it = lines.Begin(),
86          endIt = lines.Begin() + lineIndex;
87        it != endIt;
88        ++it )
89   {
90     const LineRun& lineRun = *it;
91
92     // The line height is the addition of the line ascender and the line descender.
93     // However, the line descender has a negative value, hence the subtraction.
94     offset += lineRun.ascender - lineRun.descender;
95   }
96
97   return offset;
98 }
99
100 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
101                                       LogicalModelPtr logicalModel,
102                                       MetricsPtr metrics,
103                                       float visualX,
104                                       float visualY )
105 {
106   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY );
107
108   CharacterIndex logicalIndex = 0u;
109
110   const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
111   const Length totalNumberOfLines  = visualModel->mLines.Count();
112   if( ( 0 == totalNumberOfGlyphs ) ||
113       ( 0 == totalNumberOfLines ) )
114   {
115     return logicalIndex;
116   }
117
118   // Find which line is closest.
119   const LineIndex lineIndex = Text::GetClosestLine( visualModel,
120                                                     visualY );
121
122   // Convert from text's coords to line's coords.
123   const LineRun& line = *( visualModel->mLines.Begin() + lineIndex );
124
125   // Transform the tap point from text's coords to line's coords.
126   visualX -= line.alignmentOffset;
127
128   // Get the positions of the glyphs.
129   const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
130
131   // Get the character to glyph conversion table.
132   const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
133
134   // Get the glyphs per character table.
135   const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
136
137   // Get the characters per glyph table.
138   const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
139
140   // Get the glyph's info buffer.
141   const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
142
143   const CharacterIndex startCharacter = line.characterRun.characterIndex;
144   const CharacterIndex endCharacter   = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
145   DALI_ASSERT_DEBUG( endCharacter <= logicalModel->mText.Count() && "Invalid line info" );
146
147   // Whether this line is a bidirectional line.
148   const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( startCharacter );
149
150   // The character's direction buffer.
151   const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
152
153   // Whether there is a hit on a glyph.
154   bool matched = false;
155
156   // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
157   CharacterIndex visualIndex = startCharacter;
158   Length numberOfVisualCharacters = 0u;
159   for( ; visualIndex < endCharacter; ++visualIndex )
160   {
161     // The character in logical order.
162     const CharacterIndex characterLogicalOrderIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( visualIndex ) : visualIndex );
163     const CharacterDirection direction = ( bidiLineFetched ? *( directionsBuffer + characterLogicalOrderIndex ) : LTR );
164
165     // The number of glyphs for that character
166     const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + characterLogicalOrderIndex );
167     ++numberOfVisualCharacters;
168
169     if( 0u != numberOfGlyphs )
170     {
171       // Get the first character/glyph of the group of glyphs.
172       const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfVisualCharacters;
173       const CharacterIndex firstLogicalCharacterIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( firstVisualCharacterIndex ) : firstVisualCharacterIndex );
174       const GlyphIndex firstLogicalGlyphIndex = *( charactersToGlyphBuffer + firstLogicalCharacterIndex );
175
176       // Get the metrics for the group of glyphs.
177       GlyphMetrics glyphMetrics;
178       GetGlyphsMetrics( firstLogicalGlyphIndex,
179                         numberOfGlyphs,
180                         glyphMetrics,
181                         glyphInfoBuffer,
182                         metrics );
183
184       // Get the position of the first glyph.
185       const Vector2& position = *( positionsBuffer + firstLogicalGlyphIndex );
186
187       // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
188       Length numberOfCharacters = *( charactersPerGlyphBuffer + firstLogicalGlyphIndex );
189       if( direction != LTR )
190       {
191         // As characters are being traversed in visual order,
192         // for right to left ligatures, the character which contains the
193         // number of glyphs in the table is found first.
194         // Jump the number of characters to the next glyph is needed.
195
196         if( 0u == numberOfCharacters )
197         {
198           // TODO: This is a workaround to fix an issue with complex characters in the arabic
199           // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
200           // There are characters that are not shaped in one glyph but in combination with
201           // the next one generates two of them.
202           // The visual to logical conversion table have characters in different order than
203           // expected even if all of them are arabic.
204
205           // The workaround doesn't fix the issue completely but it prevents the application
206           // to hang in an infinite loop.
207
208           // Find the number of characters.
209           for( GlyphIndex index = firstLogicalGlyphIndex + 1u;
210                ( 0u == numberOfCharacters ) && ( index < totalNumberOfGlyphs ) ;
211                ++index )
212           {
213             numberOfCharacters = *( charactersPerGlyphBuffer + index );
214           }
215
216           if( 2u > numberOfCharacters )
217           {
218             continue;
219           }
220
221           --numberOfCharacters;
222         }
223
224         visualIndex += numberOfCharacters - 1u;
225       }
226
227       // Get the script of the character.
228       const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
229
230       const bool isInterglyphIndex = ( numberOfCharacters > numberOfGlyphs ) && HasLigatureMustBreak( script );
231       const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
232       const float glyphAdvance = glyphMetrics.advance / static_cast<float>( numberOfBlocks );
233
234       CharacterIndex index = 0u;
235       for( ; index < numberOfBlocks; ++index )
236       {
237         // Find the mid-point of the area containing the glyph
238         const float glyphCenter = -glyphMetrics.xBearing + position.x + ( static_cast<float>( index ) + 0.5f ) * glyphAdvance;
239
240         if( visualX < glyphCenter )
241         {
242           matched = true;
243           break;
244         }
245       }
246
247       if( matched )
248       {
249         // If the glyph is shaped from more than one character, it matches the character of the glyph.
250         visualIndex = firstVisualCharacterIndex + index;
251         break;
252       }
253
254       numberOfVisualCharacters = 0u;
255     }
256   }
257
258   // The number of characters of the whole text.
259   const Length totalNumberOfCharacters = logicalModel->mText.Count();
260
261   // Return the logical position of the cursor in characters.
262
263   if( !matched )
264   {
265     // If no character is matched, then the last character (in visual order) of the line is used.
266     visualIndex = endCharacter;
267   }
268
269   // Get the paragraph direction.
270   const CharacterDirection paragraphDirection = line.direction;
271
272   if( totalNumberOfCharacters != visualIndex )
273   {
274     // The visual index is not at the end of the text.
275
276     if( LTR == paragraphDirection )
277     {
278       // The paragraph direction is left to right.
279
280       if( visualIndex == endCharacter )
281       {
282         // It places the cursor just before the last character in visual order.
283         // i.e. it places the cursor just before the '\n' or before the last character
284         // if there is a long line with no word breaks which is wrapped.
285
286         // It doesn't check if the closest line is the last one like the RTL branch below
287         // because the total number of characters is different than the visual index and
288         // the visual index is the last character of the line.
289         --visualIndex;
290       }
291     }
292     else
293     {
294       // The paragraph direction is right to left.
295
296       if( ( lineIndex != totalNumberOfLines - 1u ) && // is not the last line.
297           ( visualIndex == startCharacter ) )
298       {
299         // It places the cursor just after the first character in visual order.
300         // i.e. it places the cursor just after the '\n' or after the last character
301         // if there is a long line with no word breaks which is wrapped.
302
303         // If the last line doesn't end with '\n' it won't increase the visual index
304         // placing the cursor at the beginning of the line (in visual order).
305         ++visualIndex;
306       }
307     }
308   }
309   else
310   {
311     // The visual index is at the end of text.
312
313     // 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.
314     // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
315     // the cursor just before the last '\n'.
316
317     if( ( lineIndex != totalNumberOfLines - 1u ) &&
318         TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + visualIndex - 1u ) ) )
319     {
320       --visualIndex;
321     }
322   }
323
324   logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
325
326   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
327
328   DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
329
330   return logicalIndex;
331 }
332
333
334 void GetCursorPosition( VisualModelPtr visualModel,
335                         LogicalModelPtr logicalModel,
336                         MetricsPtr metrics,
337                         CharacterIndex logical,
338                         CursorInfo& cursorInfo )
339 {
340   // Whether the logical cursor position is at the end of the whole text.
341   const bool isLastPosition = logicalModel->mText.Count() == logical;
342
343   // Get the line where the character is laid-out.
344   const CharacterIndex characterOfLine = isLastPosition ? ( logical - 1u ) : logical;
345
346   // Whether the cursor is in the last position and the last position is a new paragraph character.
347   const bool isLastNewParagraph = isLastPosition && TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + characterOfLine ) );
348
349   const LineRun* const modelLines = visualModel->mLines.Begin();
350
351   const LineIndex lineIndex = visualModel->GetLineOfCharacter( characterOfLine );
352   const LineRun& line = *( modelLines + lineIndex );
353
354   if( isLastNewParagraph )
355   {
356     // The cursor is in a new line with no characters. Place the cursor in that line.
357     const LineIndex newLineIndex = lineIndex + 1u;
358     const LineRun& newLine = *( modelLines + newLineIndex );
359
360     cursorInfo.isSecondaryCursor = false;
361
362     // Set the line offset and height.
363     cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
364                                                  newLineIndex );
365
366     // The line height is the addition of the line ascender and the line descender.
367     // However, the line descender has a negative value, hence the subtraction.
368     cursorInfo.lineHeight = newLine.ascender - newLine.descender;
369
370     // Set the primary cursor's height.
371     cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
372
373     // Set the primary cursor's position.
374     cursorInfo.primaryPosition.x = 0.f;
375     cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
376
377     // Transform the cursor info from line's coords to text's coords.
378     cursorInfo.primaryPosition.x += ( LTR == line.direction ) ? 0.f : visualModel->mControlSize.width;
379   }
380   else
381   {
382     // Whether this line is a bidirectional line.
383     const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( characterOfLine );
384
385     // Check if the logical position is the first or the last one of the line.
386     const bool isFirstPositionOfLine = line.characterRun.characterIndex == logical;
387     const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == logical;
388
389     // 'logical' is the logical 'cursor' index.
390     // Get the next and current logical 'character' index.
391     const CharacterIndex characterIndex = isFirstPositionOfLine ? logical : logical - 1u;
392     const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : logical;
393
394     // The character's direction buffer.
395     const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
396
397     CharacterDirection isCurrentRightToLeft = false;
398     CharacterDirection isNextRightToLeft = false;
399     if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
400     {
401       isCurrentRightToLeft = *( directionsBuffer + characterIndex );
402       isNextRightToLeft = *( directionsBuffer + nextCharacterIndex );
403     }
404
405     // Get the paragraph's direction.
406     const CharacterDirection isRightToLeftParagraph = line.direction;
407
408     // Check whether there is an alternative position:
409     cursorInfo.isSecondaryCursor = ( ( !isLastPositionOfLine && ( isCurrentRightToLeft != isNextRightToLeft ) )     ||
410                                      ( isLastPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) ||
411                                      ( isFirstPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) );
412
413     // Set the line offset and height.
414     cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
415                                                  lineIndex );
416
417     // The line height is the addition of the line ascender and the line descender.
418     // However, the line descender has a negative value, hence the subtraction.
419     cursorInfo.lineHeight = line.ascender - line.descender;
420
421     // Calculate the primary cursor.
422
423     CharacterIndex index = characterIndex;
424     if( cursorInfo.isSecondaryCursor )
425     {
426       // If there is a secondary position, the primary cursor may be in a different place than the logical index.
427
428       if( isLastPositionOfLine )
429       {
430         // The position of the cursor after the last character needs special
431         // care depending on its direction and the direction of the paragraph.
432
433         // Need to find the first character after the last character with the paragraph's direction.
434         // i.e l0 l1 l2 r0 r1 should find r0.
435
436         index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
437         if( bidiLineFetched )
438         {
439           index = logicalModel->GetLogicalCharacterIndex( index );
440         }
441       }
442       else if( isFirstPositionOfLine )
443       {
444         index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
445         if( bidiLineFetched )
446         {
447           index = logicalModel->GetLogicalCharacterIndex( index );
448         }
449       }
450       else
451       {
452         index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
453       }
454     }
455
456     const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
457     const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
458     const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
459     const CharacterIndex* const glyphsToCharactersBuffer = visualModel->mGlyphsToCharacters.Begin();
460     const Vector2* const glyphPositionsBuffer = visualModel->mGlyphPositions.Begin();
461     const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
462
463     // Convert the cursor position into the glyph position.
464     const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
465     const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
466     const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
467
468     // Get the metrics for the group of glyphs.
469     GlyphMetrics glyphMetrics;
470     GetGlyphsMetrics( primaryGlyphIndex,
471                       primaryNumberOfGlyphs,
472                       glyphMetrics,
473                       glyphInfoBuffer,
474                       metrics );
475
476     // Whether to add the glyph's advance to the cursor position.
477     // 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,
478     //     if the logical cursor is one, the position is the position of the first glyph and the advance is added.
479     // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
480     //
481     // FLCP A
482     // ------
483     // 0000 1
484     // 0001 1
485     // 0010 0
486     // 0011 0
487     // 0100 1
488     // 0101 0
489     // 0110 1
490     // 0111 0
491     // 1000 0
492     // 1001 1
493     // 1010 0
494     // 1011 1
495     // 1100 x
496     // 1101 x
497     // 1110 x
498     // 1111 x
499     //
500     // Where F -> isFirstPosition
501     //       L -> isLastPosition
502     //       C -> isCurrentRightToLeft
503     //       P -> isRightToLeftParagraph
504     //       A -> Whether to add the glyph's advance.
505
506     const bool addGlyphAdvance = ( ( isLastPositionOfLine && !isRightToLeftParagraph ) ||
507                                    ( isFirstPositionOfLine && isRightToLeftParagraph ) ||
508                                    ( !isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft ) );
509
510     float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
511
512     if( !isLastPositionOfLine &&
513         ( primaryNumberOfCharacters > 1u ) )
514     {
515       const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
516
517       bool isCurrentRightToLeft = false;
518       if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
519       {
520         isCurrentRightToLeft = *( directionsBuffer + index );
521       }
522
523       Length numberOfGlyphAdvance = ( isFirstPositionOfLine ? 0u : 1u ) + characterIndex - firstIndex;
524       if( isCurrentRightToLeft )
525       {
526         numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
527       }
528
529       glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
530     }
531
532     // Get the glyph position and x bearing (in the line's coords).
533     const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
534
535     // Set the primary cursor's height.
536     cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
537
538     // Set the primary cursor's position.
539     cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
540     cursorInfo.primaryPosition.y = cursorInfo.lineOffset + line.ascender - glyphMetrics.ascender;
541
542     // Transform the cursor info from line's coords to text's coords.
543     cursorInfo.primaryPosition.x += line.alignmentOffset;
544
545     // Calculate the secondary cursor.
546     if( cursorInfo.isSecondaryCursor )
547     {
548       // Set the secondary cursor's height.
549       cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
550
551       CharacterIndex index = characterIndex;
552       if( !isLastPositionOfLine )
553       {
554         index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
555       }
556
557       const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
558       const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
559
560       const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
561
562       GetGlyphsMetrics( secondaryGlyphIndex,
563                         secondaryNumberOfGlyphs,
564                         glyphMetrics,
565                         glyphInfoBuffer,
566                         metrics );
567
568       // Set the secondary cursor's position.
569
570       // FCP A
571       // ------
572       // 000 1
573       // 001 x
574       // 010 0
575       // 011 0
576       // 100 x
577       // 101 0
578       // 110 1
579       // 111 x
580       //
581       // Where F -> isFirstPosition
582       //       C -> isCurrentRightToLeft
583       //       P -> isRightToLeftParagraph
584       //       A -> Whether to add the glyph's advance.
585
586       const bool addGlyphAdvance = ( ( !isFirstPositionOfLine && !isCurrentRightToLeft ) ||
587                                      ( isFirstPositionOfLine && !isRightToLeftParagraph ) );
588
589       cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( addGlyphAdvance ? glyphMetrics.advance : 0.f );
590       cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight - line.descender - ( glyphMetrics.fontHeight - glyphMetrics.ascender );
591
592       // Transform the cursor info from line's coords to text's coords.
593       cursorInfo.secondaryPosition.x += line.alignmentOffset;
594     }
595   }
596 }
597
598 void FindSelectionIndices( VisualModelPtr visualModel,
599                            LogicalModelPtr logicalModel,
600                            MetricsPtr metrics,
601                            float visualX,
602                            float visualY,
603                            CharacterIndex& startIndex,
604                            CharacterIndex& endIndex )
605 {
606   CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
607                                                              logicalModel,
608                                                              metrics,
609                                                              visualX,
610                                                              visualY );
611   DALI_ASSERT_DEBUG( hitCharacter <= logicalModel->mText.Count() && "GetClosestCursorIndex returned out of bounds index" );
612
613   if( logicalModel->mText.Count() == 0 )
614   {
615     return;  // if model empty
616   }
617
618   if( hitCharacter >= logicalModel->mText.Count() )
619   {
620     // Closest hit character is the last character.
621     if( hitCharacter ==  logicalModel->mText.Count() )
622     {
623       hitCharacter--; //Hit character index set to last character in logical model
624     }
625     else
626     {
627       // hitCharacter is out of bounds
628       return;
629     }
630   }
631
632   startIndex = hitCharacter;
633   endIndex = hitCharacter;
634   bool isHitCharacterWhitespace = TextAbstraction::IsWhiteSpace( logicalModel->mText[hitCharacter] );
635
636   // Find the start and end of the text
637   for( startIndex = hitCharacter; startIndex > 0; --startIndex )
638   {
639     if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ startIndex-1 ] ) )
640     {
641       break;
642     }
643   }
644   const CharacterIndex pastTheEnd = logicalModel->mText.Count();
645   for( endIndex = hitCharacter + 1u; endIndex < pastTheEnd; ++endIndex )
646   {
647     if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ endIndex ] ) )
648     {
649       break;
650     }
651   }
652 }
653
654 } // namespace Text
655
656 } // namespace Toolkit
657
658 } // namespace Dali