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 numberOfGlyphs = visualModel->mGlyphs.Count();
111 const Length numberOfLines = visualModel->mLines.Count();
112 if( ( 0 == numberOfGlyphs ) ||
113 ( 0 == numberOfLines ) )
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 const 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.
195 visualIndex += numberOfCharacters - 1u;
198 // Get the script of the character.
199 const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
201 const bool isInterglyphIndex = ( numberOfCharacters > numberOfGlyphs ) && HasLigatureMustBreak( script );
202 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
203 const float glyphAdvance = glyphMetrics.advance / static_cast<float>( numberOfBlocks );
205 CharacterIndex index = 0u;
206 for( ; index < numberOfBlocks; ++index )
208 // Find the mid-point of the area containing the glyph
209 const float glyphCenter = -glyphMetrics.xBearing + position.x + ( static_cast<float>( index ) + 0.5f ) * glyphAdvance;
211 if( visualX < glyphCenter )
220 // If the glyph is shaped from more than one character, it matches the character of the glyph.
221 visualIndex = firstVisualCharacterIndex + index;
225 numberOfVisualCharacters = 0u;
229 // The number of characters of the whole text.
230 const Length totalNumberOfCharacters = logicalModel->mText.Count();
232 // Return the logical position of the cursor in characters.
236 // If no character is matched, then the last character (in visual order) of the line is used.
237 visualIndex = endCharacter;
240 // Get the paragraph direction.
241 const CharacterDirection paragraphDirection = line.direction;
243 if( totalNumberOfCharacters != visualIndex )
245 // The visual index is not at the end of the text.
247 if( LTR == paragraphDirection )
249 // The paragraph direction is left to right.
251 if( visualIndex == endCharacter )
253 // It places the cursor just before the last character in visual order.
254 // i.e. it places the cursor just before the '\n' or before the last character
255 // if there is a long line with no word breaks which is wrapped.
257 // It doesn't check if the closest line is the last one like the RTL branch below
258 // because the total number of characters is different than the visual index and
259 // the visual index is the last character of the line.
265 // The paragraph direction is right to left.
267 if( ( lineIndex != numberOfLines - 1u ) && // is not the last line.
268 ( visualIndex == startCharacter ) )
270 // It places the cursor just after the first character in visual order.
271 // i.e. it places the cursor just after the '\n' or after the last character
272 // if there is a long line with no word breaks which is wrapped.
274 // If the last line doesn't end with '\n' it won't increase the visual index
275 // placing the cursor at the beginning of the line (in visual order).
282 // The visual index is at the end of text.
284 // 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.
285 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
286 // the cursor just before the last '\n'.
288 if( ( lineIndex != numberOfLines - 1u ) &&
289 TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + visualIndex - 1u ) ) )
295 logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
297 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
299 DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
305 void GetCursorPosition( VisualModelPtr visualModel,
306 LogicalModelPtr logicalModel,
308 CharacterIndex logical,
309 CursorInfo& cursorInfo )
311 // Whether the logical cursor position is at the end of the whole text.
312 const bool isLastPosition = logicalModel->mText.Count() == logical;
314 // Get the line where the character is laid-out.
315 const CharacterIndex characterOfLine = isLastPosition ? ( logical - 1u ) : logical;
317 // Whether the cursor is in the last position and the last position is a new paragraph character.
318 const bool isLastNewParagraph = isLastPosition && TextAbstraction::IsNewParagraph( *( logicalModel->mText.Begin() + characterOfLine ) );
320 const LineRun* const modelLines = visualModel->mLines.Begin();
322 const LineIndex lineIndex = visualModel->GetLineOfCharacter( characterOfLine );
323 const LineRun& line = *( modelLines + lineIndex );
325 if( isLastNewParagraph )
327 // The cursor is in a new line with no characters. Place the cursor in that line.
328 const LineIndex newLineIndex = lineIndex + 1u;
329 const LineRun& newLine = *( modelLines + newLineIndex );
331 cursorInfo.isSecondaryCursor = false;
333 // Set the line offset and height.
334 cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
337 // The line height is the addition of the line ascender and the line descender.
338 // However, the line descender has a negative value, hence the subtraction.
339 cursorInfo.lineHeight = newLine.ascender - newLine.descender;
341 // Set the primary cursor's height.
342 cursorInfo.primaryCursorHeight = cursorInfo.lineHeight;
344 // Set the primary cursor's position.
345 cursorInfo.primaryPosition.x = 0.f;
346 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
348 // Transform the cursor info from line's coords to text's coords.
349 cursorInfo.primaryPosition.x += ( LTR == line.direction ) ? 0.f : visualModel->mControlSize.width;
353 // Whether this line is a bidirectional line.
354 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( characterOfLine );
356 // Check if the logical position is the first or the last one of the line.
357 const bool isFirstPositionOfLine = line.characterRun.characterIndex == logical;
358 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == logical;
360 // 'logical' is the logical 'cursor' index.
361 // Get the next and current logical 'character' index.
362 const CharacterIndex characterIndex = isFirstPositionOfLine ? logical : logical - 1u;
363 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : logical;
365 // The character's direction buffer.
366 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
368 CharacterDirection isCurrentRightToLeft = false;
369 CharacterDirection isNextRightToLeft = false;
370 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
372 isCurrentRightToLeft = *( directionsBuffer + characterIndex );
373 isNextRightToLeft = *( directionsBuffer + nextCharacterIndex );
376 // Get the paragraph's direction.
377 const CharacterDirection isRightToLeftParagraph = line.direction;
379 // Check whether there is an alternative position:
380 cursorInfo.isSecondaryCursor = ( ( !isLastPositionOfLine && ( isCurrentRightToLeft != isNextRightToLeft ) ) ||
381 ( isLastPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) ||
382 ( isFirstPositionOfLine && ( isRightToLeftParagraph != isCurrentRightToLeft ) ) );
384 // Set the line offset and height.
385 cursorInfo.lineOffset = CalculateLineOffset( visualModel->mLines,
388 // The line height is the addition of the line ascender and the line descender.
389 // However, the line descender has a negative value, hence the subtraction.
390 cursorInfo.lineHeight = line.ascender - line.descender;
392 // Calculate the primary cursor.
394 CharacterIndex index = characterIndex;
395 if( cursorInfo.isSecondaryCursor )
397 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
399 if( isLastPositionOfLine )
401 // The position of the cursor after the last character needs special
402 // care depending on its direction and the direction of the paragraph.
404 // Need to find the first character after the last character with the paragraph's direction.
405 // i.e l0 l1 l2 r0 r1 should find r0.
407 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
408 if( bidiLineFetched )
410 index = logicalModel->GetLogicalCharacterIndex( index );
413 else if( isFirstPositionOfLine )
415 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
416 if( bidiLineFetched )
418 index = logicalModel->GetLogicalCharacterIndex( index );
423 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
427 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
428 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
429 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
430 const CharacterIndex* const glyphsToCharactersBuffer = visualModel->mGlyphsToCharacters.Begin();
431 const Vector2* const glyphPositionsBuffer = visualModel->mGlyphPositions.Begin();
432 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
434 // Convert the cursor position into the glyph position.
435 const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
436 const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
437 const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
439 // Get the metrics for the group of glyphs.
440 GlyphMetrics glyphMetrics;
441 GetGlyphsMetrics( primaryGlyphIndex,
442 primaryNumberOfGlyphs,
447 // Whether to add the glyph's advance to the cursor position.
448 // 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,
449 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
450 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
471 // Where F -> isFirstPosition
472 // L -> isLastPosition
473 // C -> isCurrentRightToLeft
474 // P -> isRightToLeftParagraph
475 // A -> Whether to add the glyph's advance.
477 const bool addGlyphAdvance = ( ( isLastPositionOfLine && !isRightToLeftParagraph ) ||
478 ( isFirstPositionOfLine && isRightToLeftParagraph ) ||
479 ( !isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft ) );
481 float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
483 if( !isLastPositionOfLine &&
484 ( primaryNumberOfCharacters > 1u ) )
486 const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
488 bool isCurrentRightToLeft = false;
489 if( bidiLineFetched ) // If bidiLineFetched is false, it means the whole text is left to right.
491 isCurrentRightToLeft = *( directionsBuffer + index );
494 Length numberOfGlyphAdvance = ( isFirstPositionOfLine ? 0u : 1u ) + characterIndex - firstIndex;
495 if( isCurrentRightToLeft )
497 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
500 glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
503 // Get the glyph position and x bearing (in the line's coords).
504 const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
506 // Set the primary cursor's height.
507 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
509 // Set the primary cursor's position.
510 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
511 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + line.ascender - glyphMetrics.ascender;
513 // Transform the cursor info from line's coords to text's coords.
514 cursorInfo.primaryPosition.x += line.alignmentOffset;
516 // Calculate the secondary cursor.
517 if( cursorInfo.isSecondaryCursor )
519 // Set the secondary cursor's height.
520 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
522 CharacterIndex index = characterIndex;
523 if( !isLastPositionOfLine )
525 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
528 const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
529 const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
531 const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
533 GetGlyphsMetrics( secondaryGlyphIndex,
534 secondaryNumberOfGlyphs,
539 // Set the secondary cursor's position.
552 // Where F -> isFirstPosition
553 // C -> isCurrentRightToLeft
554 // P -> isRightToLeftParagraph
555 // A -> Whether to add the glyph's advance.
557 const bool addGlyphAdvance = ( ( !isFirstPositionOfLine && !isCurrentRightToLeft ) ||
558 ( isFirstPositionOfLine && !isRightToLeftParagraph ) );
560 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( addGlyphAdvance ? glyphMetrics.advance : 0.f );
561 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight - line.descender - ( glyphMetrics.fontHeight - glyphMetrics.ascender );
563 // Transform the cursor info from line's coords to text's coords.
564 cursorInfo.secondaryPosition.x += line.alignmentOffset;
569 void FindSelectionIndices( VisualModelPtr visualModel,
570 LogicalModelPtr logicalModel,
574 CharacterIndex& startIndex,
575 CharacterIndex& endIndex )
577 CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
582 DALI_ASSERT_DEBUG( hitCharacter <= logicalModel->mText.Count() && "GetClosestCursorIndex returned out of bounds index" );
584 if( logicalModel->mText.Count() == 0 )
586 return; // if model empty
589 if( hitCharacter >= logicalModel->mText.Count() )
591 // Closest hit character is the last character.
592 if( hitCharacter == logicalModel->mText.Count() )
594 hitCharacter--; //Hit character index set to last character in logical model
598 // hitCharacter is out of bounds
603 startIndex = hitCharacter;
604 endIndex = hitCharacter;
605 bool isHitCharacterWhitespace = TextAbstraction::IsWhiteSpace( logicalModel->mText[hitCharacter] );
607 // Find the start and end of the text
608 for( startIndex = hitCharacter; startIndex > 0; --startIndex )
610 if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ startIndex-1 ] ) )
615 const CharacterIndex pastTheEnd = logicalModel->mText.Count();
616 for( endIndex = hitCharacter + 1u; endIndex < pastTheEnd; ++endIndex )
618 if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ endIndex ] ) )
627 } // namespace Toolkit