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.
47 LineIndex GetClosestLine( VisualModelPtr visualModel,
50 float totalHeight = 0.f;
51 LineIndex lineIndex = 0u;
53 const Vector<LineRun>& lines = visualModel->mLines;
55 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
60 const LineRun& lineRun = *it;
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;
66 if( visualY < totalHeight )
80 float CalculateLineOffset( const Vector<LineRun>& lines,
85 for( Vector<LineRun>::ConstIterator it = lines.Begin(),
86 endIt = lines.Begin() + lineIndex;
90 const LineRun& lineRun = *it;
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;
100 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
101 LogicalModelPtr logicalModel,
106 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY );
108 CharacterIndex logicalIndex = 0u;
110 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
111 const Length totalNumberOfLines = visualModel->mLines.Count();
112 if( ( 0 == totalNumberOfGlyphs ) ||
113 ( 0 == totalNumberOfLines ) )
118 // Find which line is closest.
119 const LineIndex lineIndex = Text::GetClosestLine( visualModel,
122 // Convert from text's coords to line's coords.
123 const LineRun& line = *( visualModel->mLines.Begin() + lineIndex );
125 // Transform the tap point from text's coords to line's coords.
126 visualX -= line.alignmentOffset;
128 // Get the positions of the glyphs.
129 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
131 // Get the character to glyph conversion table.
132 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
134 // Get the glyphs per character table.
135 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
137 // Get the characters per glyph table.
138 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
140 // Get the glyph's info buffer.
141 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
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" );
147 // Whether this line is a bidirectional line.
148 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( startCharacter );
150 // The character's direction buffer.
151 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
153 // Whether there is a hit on a glyph.
154 bool matched = false;
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 )
161 // The character in logical order.
162 const CharacterIndex characterLogicalOrderIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( visualIndex ) : visualIndex );
163 const CharacterDirection direction = ( bidiLineFetched ? *( directionsBuffer + characterLogicalOrderIndex ) : LTR );
165 // The number of glyphs for that character
166 const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + characterLogicalOrderIndex );
167 ++numberOfVisualCharacters;
169 if( 0u != numberOfGlyphs )
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 );
176 // Get the metrics for the group of glyphs.
177 GlyphMetrics glyphMetrics;
178 GetGlyphsMetrics( firstLogicalGlyphIndex,
184 // Get the position of the first glyph.
185 const Vector2& position = *( positionsBuffer + firstLogicalGlyphIndex );
187 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
188 Length numberOfCharacters = *( charactersPerGlyphBuffer + firstLogicalGlyphIndex );
189 if( direction != LTR )
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.
196 if( 0u == numberOfCharacters )
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.
205 // The workaround doesn't fix the issue completely but it prevents the application
206 // to hang in an infinite loop.
208 // Find the number of characters.
209 for( GlyphIndex index = firstLogicalGlyphIndex + 1u;
210 ( 0u == numberOfCharacters ) && ( index < totalNumberOfGlyphs ) ;
213 numberOfCharacters = *( charactersPerGlyphBuffer + index );
216 if( 2u > numberOfCharacters )
221 --numberOfCharacters;
224 visualIndex += numberOfCharacters - 1u;
227 // Get the script of the character.
228 const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
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 );
234 CharacterIndex index = 0u;
235 for( ; index < numberOfBlocks; ++index )
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;
240 if( visualX < glyphCenter )
249 // If the glyph is shaped from more than one character, it matches the character of the glyph.
250 visualIndex = firstVisualCharacterIndex + index;
254 numberOfVisualCharacters = 0u;
258 // The number of characters of the whole text.
259 const Length totalNumberOfCharacters = logicalModel->mText.Count();
261 // Return the logical position of the cursor in characters.
265 // If no character is matched, then the last character (in visual order) of the line is used.
266 visualIndex = endCharacter;
269 // Get the paragraph direction.
270 const CharacterDirection paragraphDirection = line.direction;
272 if( totalNumberOfCharacters != visualIndex )
274 // The visual index is not at the end of the text.
276 if( LTR == paragraphDirection )
278 // The paragraph direction is left to right.
280 if( visualIndex == endCharacter )
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.
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.
294 // The paragraph direction is right to left.
296 if( ( lineIndex != totalNumberOfLines - 1u ) && // is not the last line.
297 ( visualIndex == startCharacter ) )
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.
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).
311 // The visual index is at the end of text.
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'.
317 if( ( lineIndex != totalNumberOfLines - 1u ) &&
318 TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + visualIndex - 1u ) ) )
324 logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
326 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
328 DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
334 void GetCursorPosition( VisualModelPtr visualModel,
335 LogicalModelPtr logicalModel,
337 CharacterIndex logical,
338 CursorInfo& cursorInfo )
340 // Whether the logical cursor position is at the end of the whole text.
341 const bool isLastPosition = logicalModel->mText.Count() == logical;
343 // Get the line where the character is laid-out.
344 const CharacterIndex characterOfLine = isLastPosition ? ( logical - 1u ) : logical;
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 ) );
349 const LineRun* const modelLines = visualModel->mLines.Begin();
351 const LineIndex lineIndex = visualModel->GetLineOfCharacter( characterOfLine );
352 const LineRun& line = *( modelLines + lineIndex );
354 if( isLastNewParagraph )
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 );
360 cursorInfo.isSecondaryCursor = false;
362 // Set the line offset and height.
363 cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
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;
370 // Set the primary cursor's height.
371 cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
373 // Set the primary cursor's position.
374 cursorInfo.primaryPosition.x = 0.f;
375 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
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;
382 // Whether this line is a bidirectional line.
383 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( characterOfLine );
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;
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;
394 // The character's direction buffer.
395 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
397 CharacterDirection isCurrentRightToLeft = false;
398 CharacterDirection isNextRightToLeft = false;
399 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
401 isCurrentRightToLeft = *( directionsBuffer + characterIndex );
402 isNextRightToLeft = *( directionsBuffer + nextCharacterIndex );
405 // Get the paragraph's direction.
406 const CharacterDirection isRightToLeftParagraph = line.direction;
408 // Check whether there is an alternative position:
409 cursorInfo.isSecondaryCursor = ( ( !isLastPositionOfLine && ( isCurrentRightToLeft != isNextRightToLeft ) ) ||
410 ( isLastPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) ||
411 ( isFirstPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) );
413 // Set the line offset and height.
414 cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
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;
421 // Calculate the primary cursor.
423 CharacterIndex index = characterIndex;
424 if( cursorInfo.isSecondaryCursor )
426 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
428 if( isLastPositionOfLine )
430 // The position of the cursor after the last character needs special
431 // care depending on its direction and the direction of the paragraph.
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.
436 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
437 if( bidiLineFetched )
439 index = logicalModel->GetLogicalCharacterIndex( index );
442 else if( isFirstPositionOfLine )
444 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
445 if( bidiLineFetched )
447 index = logicalModel->GetLogicalCharacterIndex( index );
452 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
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();
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 );
468 // Get the metrics for the group of glyphs.
469 GlyphMetrics glyphMetrics;
470 GetGlyphsMetrics( primaryGlyphIndex,
471 primaryNumberOfGlyphs,
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.
500 // Where F -> isFirstPosition
501 // L -> isLastPosition
502 // C -> isCurrentRightToLeft
503 // P -> isRightToLeftParagraph
504 // A -> Whether to add the glyph's advance.
506 const bool addGlyphAdvance = ( ( isLastPositionOfLine && !isRightToLeftParagraph ) ||
507 ( isFirstPositionOfLine && isRightToLeftParagraph ) ||
508 ( !isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft ) );
510 float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
512 if( !isLastPositionOfLine &&
513 ( primaryNumberOfCharacters > 1u ) )
515 const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
517 bool isCurrentRightToLeft = false;
518 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
520 isCurrentRightToLeft = *( directionsBuffer + index );
523 Length numberOfGlyphAdvance = ( isFirstPositionOfLine ? 0u : 1u ) + characterIndex - firstIndex;
524 if( isCurrentRightToLeft )
526 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
529 glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
532 // Get the glyph position and x bearing (in the line's coords).
533 const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
535 // Set the primary cursor's height.
536 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
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;
542 // Transform the cursor info from line's coords to text's coords.
543 cursorInfo.primaryPosition.x += line.alignmentOffset;
545 // Calculate the secondary cursor.
546 if( cursorInfo.isSecondaryCursor )
548 // Set the secondary cursor's height.
549 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
551 CharacterIndex index = characterIndex;
552 if( !isLastPositionOfLine )
554 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
557 const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
558 const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
560 const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
562 GetGlyphsMetrics( secondaryGlyphIndex,
563 secondaryNumberOfGlyphs,
568 // Set the secondary cursor's position.
581 // Where F -> isFirstPosition
582 // C -> isCurrentRightToLeft
583 // P -> isRightToLeftParagraph
584 // A -> Whether to add the glyph's advance.
586 const bool addGlyphAdvance = ( ( !isFirstPositionOfLine && !isCurrentRightToLeft ) ||
587 ( isFirstPositionOfLine && !isRightToLeftParagraph ) );
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 );
592 // Transform the cursor info from line's coords to text's coords.
593 cursorInfo.secondaryPosition.x += line.alignmentOffset;
598 void FindSelectionIndices( VisualModelPtr visualModel,
599 LogicalModelPtr logicalModel,
603 CharacterIndex& startIndex,
604 CharacterIndex& endIndex )
606 CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
611 DALI_ASSERT_DEBUG( hitCharacter <= logicalModel->mText.Count() && "GetClosestCursorIndex returned out of bounds index" );
613 if( logicalModel->mText.Count() == 0 )
615 return; // if model empty
618 if( hitCharacter >= logicalModel->mText.Count() )
620 // Closest hit character is the last character.
621 if( hitCharacter == logicalModel->mText.Count() )
623 hitCharacter--; //Hit character index set to last character in logical model
627 // hitCharacter is out of bounds
632 startIndex = hitCharacter;
633 endIndex = hitCharacter;
634 bool isHitCharacterWhitespace = TextAbstraction::IsWhiteSpace( logicalModel->mText[hitCharacter] );
636 // Find the start and end of the text
637 for( startIndex = hitCharacter; startIndex > 0; --startIndex )
639 if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ startIndex-1 ] ) )
644 const CharacterIndex pastTheEnd = logicalModel->mText.Count();
645 for( endIndex = hitCharacter + 1u; endIndex < pastTheEnd; ++endIndex )
647 if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ endIndex ] ) )
656 } // namespace Toolkit