2 * Copyright (c) 2022 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>
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
33 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction.
37 FindWordData(const Dali::Toolkit::Text::Character* const textBuffer,
38 Dali::Toolkit::Text::Length totalNumberOfCharacters,
39 Dali::Toolkit::Text::CharacterIndex hitCharacter,
42 : textBuffer(textBuffer),
43 totalNumberOfCharacters(totalNumberOfCharacters),
44 hitCharacter(hitCharacter),
46 isWhiteSpace(isWhiteSpace),
47 isNewParagraph(isNewParagraph)
55 const Dali::Toolkit::Text::Character* const textBuffer;
56 Dali::Toolkit::Text::Length totalNumberOfCharacters;
57 Dali::Toolkit::Text::CharacterIndex hitCharacter;
58 Dali::Toolkit::Text::CharacterIndex foundIndex;
59 bool isWhiteSpace : 1u;
60 bool isNewParagraph : 1u;
63 bool IsWhiteSpaceOrNewParagraph(Dali::Toolkit::Text::Character character,
65 bool isHitWhiteSpaceOrNewParagraph)
67 bool isWhiteSpaceOrNewParagraph = false;
68 if(isHitWhiteSpaceOrNewParagraph)
72 // Whether the current character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
73 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace(character) && !Dali::TextAbstraction::IsNewParagraph(character);
77 // Whether the current character is a new paragraph character.
78 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsNewParagraph(character);
83 // Whether the current character is a white space or a new paragraph character (note the new paragraph character is a white space as well).
84 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace(character);
87 return isWhiteSpaceOrNewParagraph;
90 void FindStartOfWord(FindWordData& data)
92 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
94 for(data.foundIndex = data.hitCharacter; data.foundIndex > 0; --data.foundIndex)
96 const Dali::Toolkit::Text::Character character = *(data.textBuffer + data.foundIndex - 1u);
98 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph(character,
100 isHitWhiteSpaceOrNewParagraph);
102 if(isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph)
109 void FindEndOfWord(FindWordData& data)
111 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
113 for(data.foundIndex = data.hitCharacter + 1u; data.foundIndex < data.totalNumberOfCharacters; ++data.foundIndex)
115 const Dali::Toolkit::Text::Character character = *(data.textBuffer + data.foundIndex);
117 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph(character,
119 isHitWhiteSpaceOrNewParagraph);
121 if(isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph)
136 LineIndex GetClosestLine(VisualModelPtr visualModel,
140 float totalHeight = 0.f;
141 LineIndex lineIndex = 0;
149 const Vector<LineRun>& lines = visualModel->mLines;
151 for(Vector<LineRun>::ConstIterator it = lines.Begin(),
156 const LineRun& lineRun = *it;
157 bool isLastLine = (it + 1 == endIt);
159 totalHeight += GetLineHeight(lineRun, isLastLine);
161 if(visualY < totalHeight)
173 return lineIndex - 1u;
176 float CalculateLineOffset(const Vector<LineRun>& lines,
181 for(Vector<LineRun>::ConstIterator it = lines.Begin(),
182 endIt = lines.Begin() + lineIndex;
186 const LineRun& lineRun = *it;
187 bool isLastLine = (it + 1 == lines.End());
189 offset += GetLineHeight(lineRun, isLastLine);
195 CharacterIndex GetClosestCursorIndex(VisualModelPtr visualModel,
196 LogicalModelPtr logicalModel,
200 CharacterHitTest::Mode mode,
201 bool& matchedCharacter)
203 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY);
205 // Whether there is a hit on a glyph.
206 matchedCharacter = false;
208 CharacterIndex logicalIndex = 0;
210 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
211 const Length totalNumberOfLines = visualModel->mLines.Count();
212 if((0 == totalNumberOfGlyphs) ||
213 (0 == totalNumberOfLines))
217 const float characterSpacing = visualModel->GetCharacterSpacing();
219 // Whether there is a hit on a line.
220 bool matchedLine = false;
222 // Find which line is closest.
223 const LineIndex lineIndex = Text::GetClosestLine(visualModel,
227 if(!matchedLine && (CharacterHitTest::TAP == mode))
229 // Return the first or the last character if the touch point doesn't hit a line.
230 return (visualY < 0.f) ? 0 : logicalModel->mText.Count();
233 // Convert from text's coords to line's coords.
234 const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
236 // Transform the tap point from text's coords to line's coords.
237 visualX -= line.alignmentOffset;
239 // Get the positions of the glyphs.
240 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
242 // Get the character to glyph conversion table.
243 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
245 // Get the glyphs per character table.
246 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
248 // Get the characters per glyph table.
249 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
251 // Get the glyph's info buffer.
252 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
254 const CharacterIndex startCharacter = line.characterRun.characterIndex;
255 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
256 DALI_ASSERT_DEBUG(endCharacter <= logicalModel->mText.Count() && "Invalid line info");
258 // Whether this line is a bidirectional line.
259 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo(startCharacter);
261 // The character's direction buffer.
262 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
264 // Whether the touch point if before the first glyph.
265 bool isBeforeFirstGlyph = false;
267 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
268 CharacterIndex visualIndex = startCharacter;
269 Length numberOfVisualCharacters = 0;
270 float calculatedAdvance = 0.f;
271 Vector<CharacterIndex>& glyphToCharacterMap = visualModel->mGlyphsToCharacters;
272 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
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 calculatedAdvance = GetCalculatedAdvance(*(logicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + firstLogicalGlyphIndex))), characterSpacing, (*(visualModel->mGlyphs.Begin() + firstLogicalGlyphIndex)).advance);
293 GetGlyphsMetrics(firstLogicalGlyphIndex,
300 // Get the position of the first glyph.
301 const Vector2& position = *(positionsBuffer + firstLogicalGlyphIndex);
303 if(startCharacter == visualIndex)
305 const float glyphPosition = -glyphMetrics.xBearing + position.x;
307 if(visualX < glyphPosition)
309 isBeforeFirstGlyph = true;
314 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
315 Length numberOfCharacters = *(charactersPerGlyphBuffer + firstLogicalGlyphIndex);
318 // As characters are being traversed in visual order,
319 // for right to left ligatures, the character which contains the
320 // number of glyphs in the table is found first.
321 // Jump the number of characters to the next glyph is needed.
323 if(0 == numberOfCharacters)
325 // TODO: This is a workaround to fix an issue with complex characters in the arabic
326 // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
327 // There are characters that are not shaped in one glyph but in combination with
328 // the next one generates two of them.
329 // The visual to logical conversion table have characters in different order than
330 // expected even if all of them are arabic.
332 // The workaround doesn't fix the issue completely but it prevents the application
333 // to hang in an infinite loop.
335 // Find the number of characters.
336 for(GlyphIndex index = firstLogicalGlyphIndex + 1u;
337 (0 == numberOfCharacters) && (index < totalNumberOfGlyphs);
340 numberOfCharacters = *(charactersPerGlyphBuffer + index);
343 if(2u > numberOfCharacters)
348 --numberOfCharacters;
351 visualIndex += numberOfCharacters - 1u;
354 // Get the script of the character.
355 const Script script = logicalModel->GetScript(characterLogicalOrderIndex);
357 const bool isInterglyphIndex = (numberOfCharacters > numberOfGlyphs) && HasLigatureMustBreak(script);
358 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
359 const float glyphAdvance = glyphMetrics.advance / static_cast<float>(numberOfBlocks);
361 CharacterIndex index = 0;
362 for(; index < numberOfBlocks; ++index)
364 // Find the mid-point of the area containing the glyph
365 const float glyphCenter = -glyphMetrics.xBearing + position.x + (static_cast<float>(index) + 0.5f) * glyphAdvance;
367 if(visualX < glyphCenter)
369 matchedCharacter = true;
376 // If the glyph is shaped from more than one character, it matches the character of the glyph.
377 visualIndex = firstVisualCharacterIndex + index;
381 numberOfVisualCharacters = 0;
383 } // for characters in visual order.
385 // The number of characters of the whole text.
386 const Length totalNumberOfCharacters = logicalModel->mText.Count();
388 // Return the logical position of the cursor in characters.
390 if(!matchedCharacter)
392 if(isBeforeFirstGlyph)
394 // If no character is matched, then the first character (in visual order) of the line is used.
395 visualIndex = startCharacter;
399 // If no character is matched, then the last character (in visual order) of the line is used.
400 visualIndex = endCharacter;
404 // Get the paragraph direction.
405 const CharacterDirection paragraphDirection = line.direction;
407 if(totalNumberOfCharacters != visualIndex)
409 // The visual index is not at the end of the text.
411 if(LTR == paragraphDirection)
413 // The paragraph direction is left to right.
415 if(visualIndex == endCharacter)
417 // It places the cursor just before the last character in visual order.
418 // i.e. it places the cursor just before the '\n' or before the last character
419 // if there is a long line with no word breaks which is wrapped.
421 // It doesn't check if the closest line is the last one like the RTL branch below
422 // because the total number of characters is different than the visual index and
423 // the visual index is the last character of the line.
429 // The paragraph direction is right to left.
431 if((lineIndex != totalNumberOfLines - 1u) && // is not the last line.
432 (visualIndex == startCharacter))
434 // It places the cursor just after the first character in visual order.
435 // i.e. it places the cursor just after the '\n' or after the last character
436 // if there is a long line with no word breaks which is wrapped.
438 // If the last line doesn't end with '\n' it won't increase the visual index
439 // placing the cursor at the beginning of the line (in visual order).
446 // The visual index is at the end of text.
448 // 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.
449 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
450 // the cursor just before the last '\n'.
452 if((lineIndex != totalNumberOfLines - 1u) &&
453 TextAbstraction::IsNewParagraph(*(logicalModel->mText.Begin() + visualIndex - 1u)))
459 logicalIndex = (bidiLineFetched ? logicalModel->GetLogicalCursorIndex(visualIndex) : visualIndex);
461 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex);
463 DALI_ASSERT_DEBUG((logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0) && "GetClosestCursorIndex - Out of bounds index");
468 void GetCursorPosition(GetCursorPositionParameters& parameters,
469 float defaultFontLineHeight,
470 CursorInfo& cursorInfo)
472 const LineRun* const modelLines = parameters.visualModel->mLines.Begin();
473 if(NULL == modelLines)
479 // Whether the logical cursor position is at the end of the whole text.
480 const bool isLastPosition = parameters.logicalModel->mText.Count() == parameters.logical;
482 // Get the line where the character is laid-out.
483 const CharacterIndex characterOfLine = isLastPosition ? (parameters.logical - 1u) : parameters.logical;
485 // Whether the cursor is in the last position and the last position is a new paragraph character.
486 const bool isLastNewParagraph = parameters.isMultiline && isLastPosition && TextAbstraction::IsNewParagraph(*(parameters.logicalModel->mText.Begin() + characterOfLine));
488 const LineIndex lineIndex = parameters.visualModel->GetLineOfCharacter(characterOfLine);
489 const LineRun& line = *(modelLines + lineIndex);
491 CharacterIndex index;
492 GlyphMetrics glyphMetrics;
493 MetricsPtr& metrics = parameters.metrics;
494 GlyphIndex glyphIndex = 0u;
495 Length numberOfGlyphs = 0u;
497 if(isLastNewParagraph)
499 // The cursor is in a new line with no characters. Place the cursor in that line.
500 const LineIndex newLineIndex = lineIndex + 1u;
501 const LineRun& newLine = *(modelLines + newLineIndex);
503 cursorInfo.isSecondaryCursor = false;
505 // Set the line offset and height.
506 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
509 // The line height is the addition of the line ascender and the line descender.
510 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in cursor height.
511 cursorInfo.lineHeight = newLine.ascender - newLine.descender;
514 const Length totalNumberOfCharacters = parameters.logicalModel->mText.Count();
515 if(totalNumberOfCharacters > 0u)
517 index = totalNumberOfCharacters - 1u;
520 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
522 // Set the primary cursor's height.
523 // The primary cursor height will take the font height of the last character and if there are no characters, it'll take the default font line height.
524 cursorInfo.primaryCursorHeight = (totalNumberOfCharacters > 0) ? (cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight) : defaultFontLineHeight;
526 // Set the primary cursor's position.
527 cursorInfo.primaryPosition.x = (LTR == line.direction) ? newLine.alignmentOffset : parameters.visualModel->mControlSize.width - newLine.alignmentOffset;
528 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
532 // Whether this line is a bidirectional line.
533 const bool bidiLineFetched = parameters.logicalModel->FetchBidirectionalLineInfo(characterOfLine);
535 // Check if the logical position is the first or the last one of the line.
536 const bool isFirstPositionOfLine = line.characterRun.characterIndex == parameters.logical;
537 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == parameters.logical;
539 // 'logical' is the logical 'cursor' index.
540 // Get the next and current logical 'character' index.
541 const CharacterIndex characterIndex = isFirstPositionOfLine ? parameters.logical : parameters.logical - 1u;
542 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : parameters.logical;
544 // The character's direction buffer.
545 const CharacterDirection* const directionsBuffer = bidiLineFetched ? parameters.logicalModel->mCharacterDirections.Begin() : NULL;
547 CharacterDirection isCurrentRightToLeft = false;
548 CharacterDirection isNextRightToLeft = false;
549 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
551 isCurrentRightToLeft = *(directionsBuffer + characterIndex);
552 isNextRightToLeft = *(directionsBuffer + nextCharacterIndex);
555 // Get the paragraph's direction.
556 const CharacterDirection isRightToLeftParagraph = line.direction;
558 // Check whether there is an alternative position:
559 cursorInfo.isSecondaryCursor = ((!isLastPositionOfLine && (isCurrentRightToLeft != isNextRightToLeft)) ||
560 (isLastPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)) ||
561 (isFirstPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)));
563 // Set the line offset and height.
564 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
567 // The line height is the addition of the line ascender and the line descender.
568 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in cursor height.
569 cursorInfo.lineHeight = line.ascender - line.descender;
571 // Calculate the primary cursor.
573 index = characterIndex;
574 if(cursorInfo.isSecondaryCursor)
576 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
578 if(isLastPositionOfLine)
580 // The position of the cursor after the last character needs special
581 // care depending on its direction and the direction of the paragraph.
583 // Need to find the first character after the last character with the paragraph's direction.
584 // i.e l0 l1 l2 r0 r1 should find r0.
586 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
589 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
592 else if(isFirstPositionOfLine)
594 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
597 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
602 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? characterIndex : nextCharacterIndex;
606 const Length* const charactersPerGlyphBuffer = parameters.visualModel->mCharactersPerGlyph.Begin();
607 const CharacterIndex* const glyphsToCharactersBuffer = parameters.visualModel->mGlyphsToCharacters.Begin();
608 const Vector2* const glyphPositionsBuffer = parameters.visualModel->mGlyphPositions.Begin();
609 const float characterSpacing = parameters.visualModel->GetCharacterSpacing();
611 // Get the metrics for the group of glyphs.
612 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
614 // Convert the cursor position into the glyph position.
615 const GlyphIndex primaryGlyphIndex = glyphIndex;
616 const Length primaryNumberOfCharacters = *(charactersPerGlyphBuffer + primaryGlyphIndex);
618 // Whether to add the glyph's advance to the cursor position.
619 // 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,
620 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
621 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
642 // Where F -> isFirstPosition
643 // L -> isLastPosition
644 // C -> isCurrentRightToLeft
645 // P -> isRightToLeftParagraph
646 // A -> Whether to add the glyph's advance.
648 const bool addGlyphAdvance = ((isLastPositionOfLine && !isRightToLeftParagraph) ||
649 (isFirstPositionOfLine && isRightToLeftParagraph) ||
650 (!isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft));
652 float glyphAdvance = addGlyphAdvance ? (glyphMetrics.advance) : 0.f;
654 if(!isLastPositionOfLine &&
655 (primaryNumberOfCharacters > 1u))
657 const CharacterIndex firstIndex = *(glyphsToCharactersBuffer + primaryGlyphIndex);
659 bool isCurrentRightToLeft = false;
660 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
662 isCurrentRightToLeft = *(directionsBuffer + index);
665 Length numberOfGlyphAdvance = (isFirstPositionOfLine ? 0 : 1u) + characterIndex - firstIndex;
666 if(isCurrentRightToLeft)
668 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
671 glyphAdvance = static_cast<float>(numberOfGlyphAdvance) * (glyphMetrics.advance) / static_cast<float>(primaryNumberOfCharacters);
674 // Get the glyph position and x bearing (in the line's coords).
675 const Vector2& primaryPosition = *(glyphPositionsBuffer + primaryGlyphIndex);
677 // Set the primary cursor's height.
678 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
680 cursorInfo.glyphOffset = line.ascender - glyphMetrics.ascender;
681 // Set the primary cursor's position.
682 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
683 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + cursorInfo.glyphOffset;
685 // Transform the cursor info from line's coords to text's coords.
686 cursorInfo.primaryPosition.x += line.alignmentOffset;
688 // Calculate the secondary cursor.
689 if(cursorInfo.isSecondaryCursor)
691 // Set the secondary cursor's height.
692 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
694 CharacterIndex index = characterIndex;
695 if(!isLastPositionOfLine)
697 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? nextCharacterIndex : characterIndex;
700 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
702 const GlyphIndex secondaryGlyphIndex = glyphIndex;
703 const Vector2& secondaryPosition = *(glyphPositionsBuffer + secondaryGlyphIndex);
705 // Set the secondary cursor's position.
718 // Where F -> isFirstPosition
719 // C -> isCurrentRightToLeft
720 // P -> isRightToLeftParagraph
721 // A -> Whether to add the glyph's advance.
723 const bool addGlyphAdvance = ((!isFirstPositionOfLine && !isCurrentRightToLeft) ||
724 (isFirstPositionOfLine && !isRightToLeftParagraph));
726 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + (addGlyphAdvance ? (glyphMetrics.advance + characterSpacing) : 0.f);
727 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
729 // Transform the cursor info from line's coords to text's coords.
730 cursorInfo.secondaryPosition.x += line.alignmentOffset;
735 bool FindSelectionIndices(VisualModelPtr visualModel,
736 LogicalModelPtr logicalModel,
740 CharacterIndex& startIndex,
741 CharacterIndex& endIndex,
742 CharacterIndex& noTextHitIndex)
746 |-------------------------------------------------------|------------------------------------------|
747 | On a word | The word |
748 | On a single white space between words | The word before or after the white space |
749 | On one of the multiple contiguous white spaces | The white spaces |
750 | On a single white space which is in the position zero | The white space and the next word |
751 | On a new paragraph character | The word or group of white spaces before |
752 |-------------------------------------------------------|------------------------------------------|
754 const Length totalNumberOfCharacters = logicalModel->mText.Count();
759 if(0 == totalNumberOfCharacters)
761 // Nothing to do if the model is empty.
765 bool matchedCharacter = false;
766 CharacterIndex hitCharacter = Text::GetClosestCursorIndex(visualModel,
771 CharacterHitTest::TAP,
774 if(!matchedCharacter)
776 noTextHitIndex = hitCharacter;
779 DALI_ASSERT_DEBUG((hitCharacter <= totalNumberOfCharacters) && "GetClosestCursorIndex returned out of bounds index");
781 if(hitCharacter >= totalNumberOfCharacters)
783 // Closest hit character is the last character.
784 if(hitCharacter == totalNumberOfCharacters)
786 hitCharacter--; //Hit character index set to last character in logical model
790 // hitCharacter is out of bounds
795 const Character* const textBuffer = logicalModel->mText.Begin();
797 startIndex = hitCharacter;
798 endIndex = hitCharacter;
800 // Whether the hit character is a new paragraph character.
801 const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + hitCharacter));
803 // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
804 const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + hitCharacter)) && !isHitCharacterNewParagraph;
806 FindWordData data(textBuffer,
807 totalNumberOfCharacters,
809 isHitCharacterWhiteSpace,
810 isHitCharacterNewParagraph);
812 if(isHitCharacterNewParagraph)
814 // Find the first character before the hit one which is not a new paragraph character.
818 endIndex = hitCharacter - 1u;
819 for(; endIndex > 0; --endIndex)
821 const Dali::Toolkit::Text::Character character = *(data.textBuffer + endIndex);
823 if(!Dali::TextAbstraction::IsNewParagraph(character))
830 data.hitCharacter = endIndex;
831 data.isNewParagraph = false;
832 data.isWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + data.hitCharacter));
835 // Find the start of the word.
836 FindStartOfWord(data);
837 startIndex = data.foundIndex;
839 // Find the end of the word.
841 endIndex = data.foundIndex;
843 if(1u == (endIndex - startIndex))
845 if(isHitCharacterWhiteSpace)
847 // Select the word before or after the white space
849 if(0 == hitCharacter)
851 data.isWhiteSpace = false;
853 endIndex = data.foundIndex;
855 else if(hitCharacter > 0)
857 // Find the start of the word.
858 data.hitCharacter = hitCharacter - 1u;
859 data.isWhiteSpace = false;
860 FindStartOfWord(data);
861 startIndex = data.foundIndex;
868 return matchedCharacter;
873 } // namespace Toolkit