Fix text selection on white spaces or new lines paragraphs.
[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 struct FindWordData
37 {
38   FindWordData( const Dali::Toolkit::Text::Character* const textBuffer,
39                 Dali::Toolkit::Text::Length totalNumberOfCharacters,
40                 Dali::Toolkit::Text::CharacterIndex hitCharacter,
41                 bool isWhiteSpace,
42                 bool isNewParagraph )
43   : textBuffer( textBuffer ),
44     totalNumberOfCharacters( totalNumberOfCharacters ),
45     hitCharacter( hitCharacter ),
46     foundIndex( 0u ),
47     isWhiteSpace( isWhiteSpace ),
48     isNewParagraph( isNewParagraph )
49   {}
50
51   ~FindWordData()
52   {}
53
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;
60 };
61
62 bool IsWhiteSpaceOrNewParagraph( Dali::Toolkit::Text::Character character,
63                                  bool isHitWhiteSpace,
64                                  bool isHitWhiteSpaceOrNewParagraph )
65 {
66   bool isWhiteSpaceOrNewParagraph = false;
67   if( isHitWhiteSpaceOrNewParagraph )
68   {
69     if( isHitWhiteSpace )
70     {
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 );
73     }
74     else
75     {
76       // Whether the current character is a new paragraph character.
77       isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsNewParagraph( character );
78     }
79   }
80   else
81   {
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 );
84   }
85
86   return isWhiteSpaceOrNewParagraph;
87 }
88
89 void FindStartOfWord( FindWordData& data )
90 {
91   const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
92
93   for( data.foundIndex = data.hitCharacter; data.foundIndex > 0; --data.foundIndex )
94   {
95     const Dali::Toolkit::Text::Character character = *( data.textBuffer + data.foundIndex - 1u );
96
97     const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph( character,
98                                                                         data.isWhiteSpace,
99                                                                         isHitWhiteSpaceOrNewParagraph );
100
101     if( isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph )
102     {
103       break;
104     }
105   }
106 }
107
108 void FindEndOfWord( FindWordData& data )
109 {
110   const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
111
112   for( data.foundIndex = data.hitCharacter + 1u; data.foundIndex < data.totalNumberOfCharacters; ++data.foundIndex )
113   {
114     const Dali::Toolkit::Text::Character character = *( data.textBuffer + data.foundIndex );
115
116     const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph( character,
117                                                                         data.isWhiteSpace,
118                                                                         isHitWhiteSpaceOrNewParagraph );
119
120     if( isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph )
121     {
122       break;
123     }
124   }
125 }
126
127 } //namespace
128
129 namespace Dali
130 {
131
132 namespace Toolkit
133 {
134
135 namespace Text
136 {
137
138 LineIndex GetClosestLine( VisualModelPtr visualModel,
139                           float visualY )
140 {
141   float totalHeight = 0.f;
142   LineIndex lineIndex = 0u;
143
144   const Vector<LineRun>& lines = visualModel->mLines;
145
146   for( Vector<LineRun>::ConstIterator it = lines.Begin(),
147          endIt = lines.End();
148        it != endIt;
149        ++it, ++lineIndex )
150   {
151     const LineRun& lineRun = *it;
152
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;
156
157     if( visualY < totalHeight )
158     {
159       return lineIndex;
160     }
161   }
162
163   if( lineIndex == 0u )
164   {
165     return 0;
166   }
167
168   return lineIndex-1;
169 }
170
171 float CalculateLineOffset( const Vector<LineRun>& lines,
172                            LineIndex lineIndex )
173 {
174   float offset = 0.f;
175
176   for( Vector<LineRun>::ConstIterator it = lines.Begin(),
177          endIt = lines.Begin() + lineIndex;
178        it != endIt;
179        ++it )
180   {
181     const LineRun& lineRun = *it;
182
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;
186   }
187
188   return offset;
189 }
190
191 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
192                                       LogicalModelPtr logicalModel,
193                                       MetricsPtr metrics,
194                                       float visualX,
195                                       float visualY )
196 {
197   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY );
198
199   CharacterIndex logicalIndex = 0u;
200
201   const Length numberOfGlyphs = visualModel->mGlyphs.Count();
202   const Length numberOfLines  = visualModel->mLines.Count();
203   if( ( 0 == numberOfGlyphs ) ||
204       ( 0 == numberOfLines ) )
205   {
206     return logicalIndex;
207   }
208
209   // Find which line is closest.
210   const LineIndex lineIndex = Text::GetClosestLine( visualModel,
211                                                     visualY );
212
213   // Convert from text's coords to line's coords.
214   const LineRun& line = *( visualModel->mLines.Begin() + lineIndex );
215
216   // Transform the tap point from text's coords to line's coords.
217   visualX -= line.alignmentOffset;
218
219   // Get the positions of the glyphs.
220   const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
221
222   // Get the character to glyph conversion table.
223   const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
224
225   // Get the glyphs per character table.
226   const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
227
228   // Get the characters per glyph table.
229   const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
230
231   // Get the glyph's info buffer.
232   const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
233
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" );
237
238   // Whether this line is a bidirectional line.
239   const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( startCharacter );
240
241   // The character's direction buffer.
242   const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
243
244   // Whether there is a hit on a glyph.
245   bool matched = false;
246
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 )
251   {
252     // The character in logical order.
253     const CharacterIndex characterLogicalOrderIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( visualIndex ) : visualIndex );
254     const CharacterDirection direction = ( bidiLineFetched ? *( directionsBuffer + characterLogicalOrderIndex ) : LTR );
255
256     // The number of glyphs for that character
257     const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + characterLogicalOrderIndex );
258     ++numberOfVisualCharacters;
259
260     if( 0u != numberOfGlyphs )
261     {
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 );
266
267       // Get the metrics for the group of glyphs.
268       GlyphMetrics glyphMetrics;
269       GetGlyphsMetrics( firstLogicalGlyphIndex,
270                         numberOfGlyphs,
271                         glyphMetrics,
272                         glyphInfoBuffer,
273                         metrics );
274
275       // Get the position of the first glyph.
276       const Vector2& position = *( positionsBuffer + firstLogicalGlyphIndex );
277
278       // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic ï»».
279       const Length numberOfCharacters = *( charactersPerGlyphBuffer + firstLogicalGlyphIndex );
280       if( direction != LTR )
281       {
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.
286         visualIndex += numberOfCharacters - 1u;
287       }
288
289       // Get the script of the character.
290       const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
291
292       const bool isInterglyphIndex = ( numberOfCharacters > numberOfGlyphs ) && HasLigatureMustBreak( script );
293       const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
294       const float glyphAdvance = glyphMetrics.advance / static_cast<float>( numberOfBlocks );
295
296       CharacterIndex index = 0u;
297       for( ; index < numberOfBlocks; ++index )
298       {
299         // Find the mid-point of the area containing the glyph
300         const float glyphCenter = -glyphMetrics.xBearing + position.x + ( static_cast<float>( index ) + 0.5f ) * glyphAdvance;
301
302         if( visualX < glyphCenter )
303         {
304           matched = true;
305           break;
306         }
307       }
308
309       if( matched )
310       {
311         // If the glyph is shaped from more than one character, it matches the character of the glyph.
312         visualIndex = firstVisualCharacterIndex + index;
313         break;
314       }
315
316       numberOfVisualCharacters = 0u;
317     }
318   }
319
320   // The number of characters of the whole text.
321   const Length totalNumberOfCharacters = logicalModel->mText.Count();
322
323   // Return the logical position of the cursor in characters.
324
325   if( !matched )
326   {
327     // If no character is matched, then the last character (in visual order) of the line is used.
328     visualIndex = endCharacter;
329   }
330
331   // Get the paragraph direction.
332   const CharacterDirection paragraphDirection = line.direction;
333
334   if( totalNumberOfCharacters != visualIndex )
335   {
336     // The visual index is not at the end of the text.
337
338     if( LTR == paragraphDirection )
339     {
340       // The paragraph direction is left to right.
341
342       if( visualIndex == endCharacter )
343       {
344         // It places the cursor just before the last character in visual order.
345         // i.e. it places the cursor just before the '\n' or before the last character
346         // if there is a long line with no word breaks which is wrapped.
347
348         // It doesn't check if the closest line is the last one like the RTL branch below
349         // because the total number of characters is different than the visual index and
350         // the visual index is the last character of the line.
351         --visualIndex;
352       }
353     }
354     else
355     {
356       // The paragraph direction is right to left.
357
358       if( ( lineIndex != numberOfLines - 1u ) && // is not the last line.
359           ( visualIndex == startCharacter ) )
360       {
361         // It places the cursor just after the first character in visual order.
362         // i.e. it places the cursor just after the '\n' or after the last character
363         // if there is a long line with no word breaks which is wrapped.
364
365         // If the last line doesn't end with '\n' it won't increase the visual index
366         // placing the cursor at the beginning of the line (in visual order).
367         ++visualIndex;
368       }
369     }
370   }
371   else
372   {
373     // The visual index is at the end of text.
374
375     // 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.
376     // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
377     // the cursor just before the last '\n'.
378
379     if( ( lineIndex != numberOfLines - 1u ) &&
380         TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + visualIndex - 1u ) ) )
381     {
382       --visualIndex;
383     }
384   }
385
386   logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
387
388   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
389
390   DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
391
392   return logicalIndex;
393 }
394
395
396 void GetCursorPosition( VisualModelPtr visualModel,
397                         LogicalModelPtr logicalModel,
398                         MetricsPtr metrics,
399                         CharacterIndex logical,
400                         CursorInfo& cursorInfo )
401 {
402   // Whether the logical cursor position is at the end of the whole text.
403   const bool isLastPosition = logicalModel->mText.Count() == logical;
404
405   // Get the line where the character is laid-out.
406   const CharacterIndex characterOfLine = isLastPosition ? ( logical - 1u ) : logical;
407
408   // Whether the cursor is in the last position and the last position is a new paragraph character.
409   const bool isLastNewParagraph = isLastPosition && TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + characterOfLine ) );
410
411   const LineRun* const modelLines = visualModel->mLines.Begin();
412
413   const LineIndex lineIndex = visualModel->GetLineOfCharacter( characterOfLine );
414   const LineRun& line = *( modelLines + lineIndex );
415
416   if( isLastNewParagraph )
417   {
418     // The cursor is in a new line with no characters. Place the cursor in that line.
419     const LineIndex newLineIndex = lineIndex + 1u;
420     const LineRun& newLine = *( modelLines + newLineIndex );
421
422     cursorInfo.isSecondaryCursor = false;
423
424     // Set the line offset and height.
425     cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
426                                                  newLineIndex );
427
428     // The line height is the addition of the line ascender and the line descender.
429     // However, the line descender has a negative value, hence the subtraction.
430     cursorInfo.lineHeight = newLine.ascender - newLine.descender;
431
432     // Set the primary cursor's height.
433     cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
434
435     // Set the primary cursor's position.
436     cursorInfo.primaryPosition.x = 0.f;
437     cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
438
439     // Transform the cursor info from line's coords to text's coords.
440     cursorInfo.primaryPosition.x += ( LTR == line.direction ) ? 0.f : visualModel->mControlSize.width;
441   }
442   else
443   {
444     // Whether this line is a bidirectional line.
445     const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( characterOfLine );
446
447     // Check if the logical position is the first or the last one of the line.
448     const bool isFirstPositionOfLine = line.characterRun.characterIndex == logical;
449     const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == logical;
450
451     // 'logical' is the logical 'cursor' index.
452     // Get the next and current logical 'character' index.
453     const CharacterIndex characterIndex = isFirstPositionOfLine ? logical : logical - 1u;
454     const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : logical;
455
456     // The character's direction buffer.
457     const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
458
459     CharacterDirection isCurrentRightToLeft = false;
460     CharacterDirection isNextRightToLeft = false;
461     if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
462     {
463       isCurrentRightToLeft = *( directionsBuffer + characterIndex );
464       isNextRightToLeft = *( directionsBuffer + nextCharacterIndex );
465     }
466
467     // Get the paragraph's direction.
468     const CharacterDirection isRightToLeftParagraph = line.direction;
469
470     // Check whether there is an alternative position:
471     cursorInfo.isSecondaryCursor = ( ( !isLastPositionOfLine && ( isCurrentRightToLeft != isNextRightToLeft ) )     ||
472                                      ( isLastPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) ||
473                                      ( isFirstPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) );
474
475     // Set the line offset and height.
476     cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
477                                                  lineIndex );
478
479     // The line height is the addition of the line ascender and the line descender.
480     // However, the line descender has a negative value, hence the subtraction.
481     cursorInfo.lineHeight = line.ascender - line.descender;
482
483     // Calculate the primary cursor.
484
485     CharacterIndex index = characterIndex;
486     if( cursorInfo.isSecondaryCursor )
487     {
488       // If there is a secondary position, the primary cursor may be in a different place than the logical index.
489
490       if( isLastPositionOfLine )
491       {
492         // The position of the cursor after the last character needs special
493         // care depending on its direction and the direction of the paragraph.
494
495         // Need to find the first character after the last character with the paragraph's direction.
496         // i.e l0 l1 l2 r0 r1 should find r0.
497
498         index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
499         if( bidiLineFetched )
500         {
501           index = logicalModel->GetLogicalCharacterIndex( index );
502         }
503       }
504       else if( isFirstPositionOfLine )
505       {
506         index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
507         if( bidiLineFetched )
508         {
509           index = logicalModel->GetLogicalCharacterIndex( index );
510         }
511       }
512       else
513       {
514         index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
515       }
516     }
517
518     const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
519     const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
520     const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
521     const CharacterIndex* const glyphsToCharactersBuffer = visualModel->mGlyphsToCharacters.Begin();
522     const Vector2* const glyphPositionsBuffer = visualModel->mGlyphPositions.Begin();
523     const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
524
525     // Convert the cursor position into the glyph position.
526     const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
527     const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
528     const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
529
530     // Get the metrics for the group of glyphs.
531     GlyphMetrics glyphMetrics;
532     GetGlyphsMetrics( primaryGlyphIndex,
533                       primaryNumberOfGlyphs,
534                       glyphMetrics,
535                       glyphInfoBuffer,
536                       metrics );
537
538     // Whether to add the glyph's advance to the cursor position.
539     // 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,
540     //     if the logical cursor is one, the position is the position of the first glyph and the advance is added.
541     // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
542     //
543     // FLCP A
544     // ------
545     // 0000 1
546     // 0001 1
547     // 0010 0
548     // 0011 0
549     // 0100 1
550     // 0101 0
551     // 0110 1
552     // 0111 0
553     // 1000 0
554     // 1001 1
555     // 1010 0
556     // 1011 1
557     // 1100 x
558     // 1101 x
559     // 1110 x
560     // 1111 x
561     //
562     // Where F -> isFirstPosition
563     //       L -> isLastPosition
564     //       C -> isCurrentRightToLeft
565     //       P -> isRightToLeftParagraph
566     //       A -> Whether to add the glyph's advance.
567
568     const bool addGlyphAdvance = ( ( isLastPositionOfLine && !isRightToLeftParagraph ) ||
569                                    ( isFirstPositionOfLine && isRightToLeftParagraph ) ||
570                                    ( !isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft ) );
571
572     float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
573
574     if( !isLastPositionOfLine &&
575         ( primaryNumberOfCharacters > 1u ) )
576     {
577       const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
578
579       bool isCurrentRightToLeft = false;
580       if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
581       {
582         isCurrentRightToLeft = *( directionsBuffer + index );
583       }
584
585       Length numberOfGlyphAdvance = ( isFirstPositionOfLine ? 0u : 1u ) + characterIndex - firstIndex;
586       if( isCurrentRightToLeft )
587       {
588         numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
589       }
590
591       glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
592     }
593
594     // Get the glyph position and x bearing (in the line's coords).
595     const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
596
597     // Set the primary cursor's height.
598     cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
599
600     // Set the primary cursor's position.
601     cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
602     cursorInfo.primaryPosition.y = cursorInfo.lineOffset + line.ascender - glyphMetrics.ascender;
603
604     // Transform the cursor info from line's coords to text's coords.
605     cursorInfo.primaryPosition.x += line.alignmentOffset;
606
607     // Calculate the secondary cursor.
608     if( cursorInfo.isSecondaryCursor )
609     {
610       // Set the secondary cursor's height.
611       cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
612
613       CharacterIndex index = characterIndex;
614       if( !isLastPositionOfLine )
615       {
616         index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
617       }
618
619       const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
620       const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
621
622       const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
623
624       GetGlyphsMetrics( secondaryGlyphIndex,
625                         secondaryNumberOfGlyphs,
626                         glyphMetrics,
627                         glyphInfoBuffer,
628                         metrics );
629
630       // Set the secondary cursor's position.
631
632       // FCP A
633       // ------
634       // 000 1
635       // 001 x
636       // 010 0
637       // 011 0
638       // 100 x
639       // 101 0
640       // 110 1
641       // 111 x
642       //
643       // Where F -> isFirstPosition
644       //       C -> isCurrentRightToLeft
645       //       P -> isRightToLeftParagraph
646       //       A -> Whether to add the glyph's advance.
647
648       const bool addGlyphAdvance = ( ( !isFirstPositionOfLine && !isCurrentRightToLeft ) ||
649                                      ( isFirstPositionOfLine && !isRightToLeftParagraph ) );
650
651       cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( addGlyphAdvance ? glyphMetrics.advance : 0.f );
652       cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
653
654       // Transform the cursor info from line's coords to text's coords.
655       cursorInfo.secondaryPosition.x += line.alignmentOffset;
656     }
657   }
658 }
659
660 bool FindSelectionIndices( VisualModelPtr visualModel,
661                            LogicalModelPtr logicalModel,
662                            MetricsPtr metrics,
663                            float visualX,
664                            float visualY,
665                            CharacterIndex& startIndex,
666                            CharacterIndex& endIndex )
667 {
668
669 /*
670   Hit character                                           Select
671 |-------------------------------------------------------|------------------------------------------|
672 | On a word                                             | The word                                 |
673 | On a single white space between words                 | The word before or after the white space |
674 | On one of the multiple contiguous white spaces        | The white spaces                         |
675 | On a single white space which is in the position zero | The white space and the next word        |
676 | On a new paragraph character                          | The word or group of white spaces before |
677 |-------------------------------------------------------|------------------------------------------|
678 */
679
680   CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
681                                                              logicalModel,
682                                                              metrics,
683                                                              visualX,
684                                                              visualY );
685
686   const Length totalNumberOfCharacters = logicalModel->mText.Count();
687
688   DALI_ASSERT_DEBUG( ( hitCharacter <= totalNumberOfCharacters ) && "GetClosestCursorIndex returned out of bounds index" );
689
690   if( 0u == totalNumberOfCharacters )
691   {
692     // Nothing to do if the model is empty.
693     return false;
694   }
695
696   if( hitCharacter >= totalNumberOfCharacters )
697   {
698     // Closest hit character is the last character.
699     if( hitCharacter == totalNumberOfCharacters )
700     {
701       hitCharacter--; //Hit character index set to last character in logical model
702     }
703     else
704     {
705       // hitCharacter is out of bounds
706       return false;
707     }
708   }
709
710   const Character* const textBuffer = logicalModel->mText.Begin();
711
712   startIndex = hitCharacter;
713   endIndex = hitCharacter;
714
715   // Whether the hit character is a new paragraph character.
716   const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + hitCharacter ) );
717
718   // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
719   const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace( *( textBuffer + hitCharacter ) ) && !isHitCharacterNewParagraph;
720
721   FindWordData data( textBuffer,
722                      totalNumberOfCharacters,
723                      hitCharacter,
724                      isHitCharacterWhiteSpace,
725                      isHitCharacterNewParagraph );
726
727   if( isHitCharacterNewParagraph )
728   {
729     // Find the first character before the hit one which is not a new paragraph character.
730
731     if( hitCharacter > 0u )
732     {
733       endIndex = hitCharacter - 1u;
734       for( ; endIndex > 0; --endIndex )
735       {
736         const Dali::Toolkit::Text::Character character = *( data.textBuffer + endIndex );
737
738         if( !Dali::TextAbstraction::IsNewParagraph( character ) )
739         {
740           break;
741         }
742       }
743     }
744
745     data.hitCharacter = endIndex;
746     data.isNewParagraph = false;
747     data.isWhiteSpace = TextAbstraction::IsWhiteSpace( *( textBuffer + data.hitCharacter ) );
748   }
749
750   // Find the start of the word.
751   FindStartOfWord( data );
752   startIndex = data.foundIndex;
753
754   // Find the end of the word.
755   FindEndOfWord( data );
756   endIndex = data.foundIndex;
757
758   if( 1u == ( endIndex - startIndex ) )
759   {
760     if( isHitCharacterWhiteSpace )
761     {
762       // Select the word before or after the white space
763
764       if( 0u == hitCharacter )
765       {
766         data.isWhiteSpace = false;
767         FindEndOfWord( data );
768         endIndex = data.foundIndex;
769       }
770       else if( hitCharacter > 0u )
771       {
772         // Find the start of the word.
773         data.hitCharacter = hitCharacter - 1u;
774         data.isWhiteSpace = false;
775         FindStartOfWord( data );
776         startIndex = data.foundIndex;
777
778         --endIndex;
779       }
780     }
781   }
782
783   return true;
784 }
785
786 } // namespace Text
787
788 } // namespace Toolkit
789
790 } // namespace Dali