2 * Copyright (c) 2017 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,
142 float totalHeight = 0.f;
143 LineIndex lineIndex = 0;
151 const Vector<LineRun>& lines = visualModel->mLines;
153 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
158 const LineRun& lineRun = *it;
160 // The line height is the addition of the line ascender and the line descender.
161 // However, the line descender has a negative value, hence the subtraction.
162 totalHeight += lineRun.ascender - lineRun.descender;
164 if( visualY < totalHeight )
176 return lineIndex - 1u;
179 float CalculateLineOffset( const Vector<LineRun>& lines,
180 LineIndex lineIndex )
184 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
185 endIt = lines.Begin() + lineIndex;
189 const LineRun& lineRun = *it;
191 // The line height is the addition of the line ascender and the line descender.
192 // However, the line descender has a negative value, hence the subtraction.
193 offset += lineRun.ascender - lineRun.descender;
199 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
200 LogicalModelPtr logicalModel,
204 CharacterHitTest::Mode mode,
205 bool& matchedCharacter )
207 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY );
209 // Whether there is a hit on a glyph.
210 matchedCharacter = false;
212 CharacterIndex logicalIndex = 0;
214 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
215 const Length totalNumberOfLines = visualModel->mLines.Count();
216 if( ( 0 == totalNumberOfGlyphs ) ||
217 ( 0 == totalNumberOfLines ) )
222 // Whether there is a hit on a line.
223 bool matchedLine = false;
225 // Find which line is closest.
226 const LineIndex lineIndex = Text::GetClosestLine( visualModel,
230 if( !matchedLine && ( CharacterHitTest::TAP == mode ) )
232 // Return the first or the last character if the touch point doesn't hit a line.
233 return ( visualY < 0.f ) ? 0 : logicalModel->mText.Count();
236 // Convert from text's coords to line's coords.
237 const LineRun& line = *( visualModel->mLines.Begin() + lineIndex );
239 // Transform the tap point from text's coords to line's coords.
240 visualX -= line.alignmentOffset;
242 // Get the positions of the glyphs.
243 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
245 // Get the character to glyph conversion table.
246 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
248 // Get the glyphs per character table.
249 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
251 // Get the characters per glyph table.
252 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
254 // Get the glyph's info buffer.
255 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
257 const CharacterIndex startCharacter = line.characterRun.characterIndex;
258 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
259 DALI_ASSERT_DEBUG( endCharacter <= logicalModel->mText.Count() && "Invalid line info" );
261 // Whether this line is a bidirectional line.
262 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( startCharacter );
264 // The character's direction buffer.
265 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
267 // Whether the touch point if before the first glyph.
268 bool isBeforeFirstGlyph = false;
270 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
271 CharacterIndex visualIndex = startCharacter;
272 Length numberOfVisualCharacters = 0;
273 for( ; visualIndex < endCharacter; ++visualIndex )
275 // The character in logical order.
276 const CharacterIndex characterLogicalOrderIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( visualIndex ) : visualIndex );
277 const CharacterDirection direction = ( bidiLineFetched ? *( directionsBuffer + characterLogicalOrderIndex ) : LTR );
279 // The number of glyphs for that character
280 const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + characterLogicalOrderIndex );
281 ++numberOfVisualCharacters;
283 if( 0 != numberOfGlyphs )
285 // Get the first character/glyph of the group of glyphs.
286 const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfVisualCharacters;
287 const CharacterIndex firstLogicalCharacterIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( firstVisualCharacterIndex ) : firstVisualCharacterIndex );
288 const GlyphIndex firstLogicalGlyphIndex = *( charactersToGlyphBuffer + firstLogicalCharacterIndex );
290 // Get the metrics for the group of glyphs.
291 GlyphMetrics glyphMetrics;
292 GetGlyphsMetrics( firstLogicalGlyphIndex,
298 // Get the position of the first glyph.
299 const Vector2& position = *( positionsBuffer + firstLogicalGlyphIndex );
301 if( startCharacter == visualIndex )
303 const float glyphPosition = -glyphMetrics.xBearing + position.x;
305 if( visualX < glyphPosition )
307 isBeforeFirstGlyph = true;
312 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
313 Length numberOfCharacters = *( charactersPerGlyphBuffer + firstLogicalGlyphIndex );
314 if( direction != LTR )
316 // As characters are being traversed in visual order,
317 // for right to left ligatures, the character which contains the
318 // number of glyphs in the table is found first.
319 // Jump the number of characters to the next glyph is needed.
321 if( 0 == numberOfCharacters )
323 // TODO: This is a workaround to fix an issue with complex characters in the arabic
324 // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
325 // There are characters that are not shaped in one glyph but in combination with
326 // the next one generates two of them.
327 // The visual to logical conversion table have characters in different order than
328 // expected even if all of them are arabic.
330 // The workaround doesn't fix the issue completely but it prevents the application
331 // to hang in an infinite loop.
333 // Find the number of characters.
334 for( GlyphIndex index = firstLogicalGlyphIndex + 1u;
335 ( 0 == numberOfCharacters ) && ( index < totalNumberOfGlyphs );
338 numberOfCharacters = *( charactersPerGlyphBuffer + index );
341 if( 2u > numberOfCharacters )
346 --numberOfCharacters;
349 visualIndex += numberOfCharacters - 1u;
352 // Get the script of the character.
353 const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
355 const bool isInterglyphIndex = ( numberOfCharacters > numberOfGlyphs ) && HasLigatureMustBreak( script );
356 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
357 const float glyphAdvance = glyphMetrics.advance / static_cast<float>( numberOfBlocks );
359 CharacterIndex index = 0;
360 for( ; index < numberOfBlocks; ++index )
362 // Find the mid-point of the area containing the glyph
363 const float glyphCenter = -glyphMetrics.xBearing + position.x + ( static_cast<float>( index ) + 0.5f ) * glyphAdvance;
365 if( visualX < glyphCenter )
367 matchedCharacter = true;
372 if( matchedCharacter )
374 // If the glyph is shaped from more than one character, it matches the character of the glyph.
375 visualIndex = firstVisualCharacterIndex + index;
379 numberOfVisualCharacters = 0;
381 } // for characters in visual order.
383 // The number of characters of the whole text.
384 const Length totalNumberOfCharacters = logicalModel->mText.Count();
386 // Return the logical position of the cursor in characters.
388 if( !matchedCharacter )
390 if( isBeforeFirstGlyph )
392 // If no character is matched, then the first character (in visual order) of the line is used.
393 visualIndex = startCharacter;
397 // If no character is matched, then the last character (in visual order) of the line is used.
398 visualIndex = endCharacter;
402 // Get the paragraph direction.
403 const CharacterDirection paragraphDirection = line.direction;
405 if( totalNumberOfCharacters != visualIndex )
407 // The visual index is not at the end of the text.
409 if( LTR == paragraphDirection )
411 // The paragraph direction is left to right.
413 if( visualIndex == endCharacter )
415 // It places the cursor just before the last character in visual order.
416 // i.e. it places the cursor just before the '\n' or before the last character
417 // if there is a long line with no word breaks which is wrapped.
419 // It doesn't check if the closest line is the last one like the RTL branch below
420 // because the total number of characters is different than the visual index and
421 // the visual index is the last character of the line.
427 // The paragraph direction is right to left.
429 if( ( lineIndex != totalNumberOfLines - 1u ) && // is not the last line.
430 ( visualIndex == startCharacter ) )
432 // It places the cursor just after the first character in visual order.
433 // i.e. it places the cursor just after the '\n' or after the last character
434 // if there is a long line with no word breaks which is wrapped.
436 // If the last line doesn't end with '\n' it won't increase the visual index
437 // placing the cursor at the beginning of the line (in visual order).
444 // The visual index is at the end of text.
446 // 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.
447 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
448 // the cursor just before the last '\n'.
450 if( ( lineIndex != totalNumberOfLines - 1u ) &&
451 TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + visualIndex - 1u ) ) )
457 logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
459 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
461 DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
467 void GetCursorPosition( GetCursorPositionParameters& parameters,
468 CursorInfo& cursorInfo )
470 // Whether the logical cursor position is at the end of the whole text.
471 const bool isLastPosition = parameters.logicalModel->mText.Count() == parameters.logical;
473 // Get the line where the character is laid-out.
474 const CharacterIndex characterOfLine = isLastPosition ? ( parameters.logical - 1u ) : parameters.logical;
476 // Whether the cursor is in the last position and the last position is a new paragraph character.
477 const bool isLastNewParagraph = parameters.isMultiline && isLastPosition && TextAbstraction::IsNewParagraph( *( parameters.logicalModel->mText.Begin() + characterOfLine ) );
479 const LineRun* const modelLines = parameters.visualModel->mLines.Begin();
481 const LineIndex lineIndex = parameters.visualModel->GetLineOfCharacter( characterOfLine );
482 const LineRun& line = *( modelLines + lineIndex );
484 if( isLastNewParagraph )
486 // The cursor is in a new line with no characters. Place the cursor in that line.
487 const LineIndex newLineIndex = lineIndex + 1u;
488 const LineRun& newLine = *( modelLines + newLineIndex );
490 cursorInfo.isSecondaryCursor = false;
492 // Set the line offset and height.
493 cursorInfo.lineOffset = CalculateLineOffset( parameters.visualModel->mLines,
496 // The line height is the addition of the line ascender and the line descender.
497 // However, the line descender has a negative value, hence the subtraction.
498 cursorInfo.lineHeight = newLine.ascender - newLine.descender;
500 // Set the primary cursor's height.
501 cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
503 // Set the primary cursor's position.
504 cursorInfo.primaryPosition.x = 0.f;
505 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
507 // Transform the cursor info from line's coords to text's coords.
508 cursorInfo.primaryPosition.x += ( LTR == line.direction ) ? 0.f : parameters.visualModel->mControlSize.width;
512 // Whether this line is a bidirectional line.
513 const bool bidiLineFetched = parameters.logicalModel->FetchBidirectionalLineInfo( characterOfLine );
515 // Check if the logical position is the first or the last one of the line.
516 const bool isFirstPositionOfLine = line.characterRun.characterIndex == parameters.logical;
517 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == parameters.logical;
519 // 'logical' is the logical 'cursor' index.
520 // Get the next and current logical 'character' index.
521 const CharacterIndex characterIndex = isFirstPositionOfLine ? parameters.logical : parameters.logical - 1u;
522 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : parameters.logical;
524 // The character's direction buffer.
525 const CharacterDirection* const directionsBuffer = bidiLineFetched ? parameters.logicalModel->mCharacterDirections.Begin() : NULL;
527 CharacterDirection isCurrentRightToLeft = false;
528 CharacterDirection isNextRightToLeft = false;
529 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
531 isCurrentRightToLeft = *( directionsBuffer + characterIndex );
532 isNextRightToLeft = *( directionsBuffer + nextCharacterIndex );
535 // Get the paragraph's direction.
536 const CharacterDirection isRightToLeftParagraph = line.direction;
538 // Check whether there is an alternative position:
539 cursorInfo.isSecondaryCursor = ( ( !isLastPositionOfLine && ( isCurrentRightToLeft != isNextRightToLeft ) ) ||
540 ( isLastPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) ||
541 ( isFirstPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) );
543 // Set the line offset and height.
544 cursorInfo.lineOffset = CalculateLineOffset( parameters.visualModel->mLines,
547 // The line height is the addition of the line ascender and the line descender.
548 // However, the line descender has a negative value, hence the subtraction.
549 cursorInfo.lineHeight = line.ascender - line.descender;
551 // Calculate the primary cursor.
553 CharacterIndex index = characterIndex;
554 if( cursorInfo.isSecondaryCursor )
556 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
558 if( isLastPositionOfLine )
560 // The position of the cursor after the last character needs special
561 // care depending on its direction and the direction of the paragraph.
563 // Need to find the first character after the last character with the paragraph's direction.
564 // i.e l0 l1 l2 r0 r1 should find r0.
566 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
567 if( bidiLineFetched )
569 index = parameters.logicalModel->GetLogicalCharacterIndex( index );
572 else if( isFirstPositionOfLine )
574 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
575 if( bidiLineFetched )
577 index = parameters.logicalModel->GetLogicalCharacterIndex( index );
582 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
586 const GlyphIndex* const charactersToGlyphBuffer = parameters.visualModel->mCharactersToGlyph.Begin();
587 const Length* const glyphsPerCharacterBuffer = parameters.visualModel->mGlyphsPerCharacter.Begin();
588 const Length* const charactersPerGlyphBuffer = parameters.visualModel->mCharactersPerGlyph.Begin();
589 const CharacterIndex* const glyphsToCharactersBuffer = parameters.visualModel->mGlyphsToCharacters.Begin();
590 const Vector2* const glyphPositionsBuffer = parameters.visualModel->mGlyphPositions.Begin();
591 const GlyphInfo* const glyphInfoBuffer = parameters.visualModel->mGlyphs.Begin();
593 // Convert the cursor position into the glyph position.
594 const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
595 const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
596 const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
598 // Get the metrics for the group of glyphs.
599 GlyphMetrics glyphMetrics;
600 GetGlyphsMetrics( primaryGlyphIndex,
601 primaryNumberOfGlyphs,
604 parameters.metrics );
606 // Whether to add the glyph's advance to the cursor position.
607 // 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,
608 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
609 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
630 // Where F -> isFirstPosition
631 // L -> isLastPosition
632 // C -> isCurrentRightToLeft
633 // P -> isRightToLeftParagraph
634 // A -> Whether to add the glyph's advance.
636 const bool addGlyphAdvance = ( ( isLastPositionOfLine && !isRightToLeftParagraph ) ||
637 ( isFirstPositionOfLine && isRightToLeftParagraph ) ||
638 ( !isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft ) );
640 float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
642 if( !isLastPositionOfLine &&
643 ( primaryNumberOfCharacters > 1u ) )
645 const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
647 bool isCurrentRightToLeft = false;
648 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
650 isCurrentRightToLeft = *( directionsBuffer + index );
653 Length numberOfGlyphAdvance = ( isFirstPositionOfLine ? 0 : 1u ) + characterIndex - firstIndex;
654 if( isCurrentRightToLeft )
656 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
659 glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
662 // Get the glyph position and x bearing (in the line's coords).
663 const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
665 // Set the primary cursor's height.
666 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
669 cursorInfo.glyphOffset = line.ascender - glyphMetrics.ascender;
670 // Set the primary cursor's position.
671 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
672 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + cursorInfo.glyphOffset;
674 // Transform the cursor info from line's coords to text's coords.
675 cursorInfo.primaryPosition.x += line.alignmentOffset;
677 // Calculate the secondary cursor.
678 if( cursorInfo.isSecondaryCursor )
680 // Set the secondary cursor's height.
681 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
683 CharacterIndex index = characterIndex;
684 if( !isLastPositionOfLine )
686 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
689 const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
690 const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
692 const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
694 GetGlyphsMetrics( secondaryGlyphIndex,
695 secondaryNumberOfGlyphs,
698 parameters.metrics );
700 // Set the secondary cursor's position.
713 // Where F -> isFirstPosition
714 // C -> isCurrentRightToLeft
715 // P -> isRightToLeftParagraph
716 // A -> Whether to add the glyph's advance.
718 const bool addGlyphAdvance = ( ( !isFirstPositionOfLine && !isCurrentRightToLeft ) ||
719 ( isFirstPositionOfLine && !isRightToLeftParagraph ) );
721 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( addGlyphAdvance ? glyphMetrics.advance : 0.f );
722 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
724 // Transform the cursor info from line's coords to text's coords.
725 cursorInfo.secondaryPosition.x += line.alignmentOffset;
730 bool FindSelectionIndices( VisualModelPtr visualModel,
731 LogicalModelPtr logicalModel,
735 CharacterIndex& startIndex,
736 CharacterIndex& endIndex,
737 CharacterIndex& noTextHitIndex )
741 |-------------------------------------------------------|------------------------------------------|
742 | On a word | The word |
743 | On a single white space between words | The word before or after the white space |
744 | On one of the multiple contiguous white spaces | The white spaces |
745 | On a single white space which is in the position zero | The white space and the next word |
746 | On a new paragraph character | The word or group of white spaces before |
747 |-------------------------------------------------------|------------------------------------------|
749 const Length totalNumberOfCharacters = logicalModel->mText.Count();
754 if( 0 == totalNumberOfCharacters )
756 // Nothing to do if the model is empty.
760 bool matchedCharacter = false;
761 CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
766 CharacterHitTest::TAP,
769 if( !matchedCharacter )
771 noTextHitIndex = hitCharacter;
774 DALI_ASSERT_DEBUG( ( hitCharacter <= totalNumberOfCharacters ) && "GetClosestCursorIndex returned out of bounds index" );
776 if( hitCharacter >= totalNumberOfCharacters )
778 // Closest hit character is the last character.
779 if( hitCharacter == totalNumberOfCharacters )
781 hitCharacter--; //Hit character index set to last character in logical model
785 // hitCharacter is out of bounds
790 const Character* const textBuffer = logicalModel->mText.Begin();
792 startIndex = hitCharacter;
793 endIndex = hitCharacter;
795 // Whether the hit character is a new paragraph character.
796 const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + hitCharacter ) );
798 // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
799 const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace( *( textBuffer + hitCharacter ) ) && !isHitCharacterNewParagraph;
801 FindWordData data( textBuffer,
802 totalNumberOfCharacters,
804 isHitCharacterWhiteSpace,
805 isHitCharacterNewParagraph );
807 if( isHitCharacterNewParagraph )
809 // Find the first character before the hit one which is not a new paragraph character.
811 if( hitCharacter > 0 )
813 endIndex = hitCharacter - 1u;
814 for( ; endIndex > 0; --endIndex )
816 const Dali::Toolkit::Text::Character character = *( data.textBuffer + endIndex );
818 if( !Dali::TextAbstraction::IsNewParagraph( character ) )
825 data.hitCharacter = endIndex;
826 data.isNewParagraph = false;
827 data.isWhiteSpace = TextAbstraction::IsWhiteSpace( *( textBuffer + data.hitCharacter ) );
830 // Find the start of the word.
831 FindStartOfWord( data );
832 startIndex = data.foundIndex;
834 // Find the end of the word.
835 FindEndOfWord( data );
836 endIndex = data.foundIndex;
838 if( 1u == ( endIndex - startIndex ) )
840 if( isHitCharacterWhiteSpace )
842 // Select the word before or after the white space
844 if( 0 == hitCharacter )
846 data.isWhiteSpace = false;
847 FindEndOfWord( data );
848 endIndex = data.foundIndex;
850 else if( hitCharacter > 0 )
852 // Find the start of the word.
853 data.hitCharacter = hitCharacter - 1u;
854 data.isWhiteSpace = false;
855 FindStartOfWord( data );
856 startIndex = data.foundIndex;
863 return matchedCharacter;
868 } // namespace Toolkit