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/characters-helper-functions.h>
26 #include <dali-toolkit/internal/text/emoji-helper.h>
27 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
31 #if defined(DEBUG_ENABLED)
32 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
35 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction.
39 FindWordData(const Dali::Toolkit::Text::Character* const textBuffer,
40 Dali::Toolkit::Text::Length totalNumberOfCharacters,
41 Dali::Toolkit::Text::CharacterIndex hitCharacter,
44 : textBuffer(textBuffer),
45 totalNumberOfCharacters(totalNumberOfCharacters),
46 hitCharacter(hitCharacter),
48 isWhiteSpace(isWhiteSpace),
49 isNewParagraph(isNewParagraph)
57 const Dali::Toolkit::Text::Character* const textBuffer;
58 Dali::Toolkit::Text::Length totalNumberOfCharacters;
59 Dali::Toolkit::Text::CharacterIndex hitCharacter;
60 Dali::Toolkit::Text::CharacterIndex foundIndex;
61 bool isWhiteSpace : 1u;
62 bool isNewParagraph : 1u;
65 bool IsWhiteSpaceOrNewParagraph(Dali::Toolkit::Text::Character character,
67 bool isHitWhiteSpaceOrNewParagraph)
69 bool isWhiteSpaceOrNewParagraph = false;
70 if(isHitWhiteSpaceOrNewParagraph)
74 // Whether the current character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
75 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace(character) && !Dali::TextAbstraction::IsNewParagraph(character);
79 // Whether the current character is a new paragraph character.
80 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsNewParagraph(character);
85 // Whether the current character is a white space or a new paragraph character (note the new paragraph character is a white space as well).
86 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace(character);
89 return isWhiteSpaceOrNewParagraph;
92 void FindStartOfWord(FindWordData& data)
94 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
96 for(data.foundIndex = data.hitCharacter; data.foundIndex > 0; --data.foundIndex)
98 const Dali::Toolkit::Text::Character character = *(data.textBuffer + data.foundIndex - 1u);
100 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph(character,
102 isHitWhiteSpaceOrNewParagraph);
104 if(isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph)
111 void FindEndOfWord(FindWordData& data)
113 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
115 for(data.foundIndex = data.hitCharacter + 1u; data.foundIndex < data.totalNumberOfCharacters; ++data.foundIndex)
117 const Dali::Toolkit::Text::Character character = *(data.textBuffer + data.foundIndex);
119 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph(character,
121 isHitWhiteSpaceOrNewParagraph);
123 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;
159 bool isLastLine = (it + 1 == endIt);
161 totalHeight += GetLineHeight(lineRun, isLastLine);
163 if(visualY < totalHeight)
175 return lineIndex - 1u;
178 float CalculateLineOffset(const Vector<LineRun>& lines,
183 for(Vector<LineRun>::ConstIterator it = lines.Begin(),
184 endIt = lines.Begin() + lineIndex;
188 const LineRun& lineRun = *it;
189 bool isLastLine = (it + 1 == lines.End());
191 offset += GetLineHeight(lineRun, isLastLine);
197 CharacterIndex GetClosestCursorIndex(VisualModelPtr visualModel,
198 LogicalModelPtr logicalModel,
202 CharacterHitTest::Mode mode,
203 bool& matchedCharacter)
205 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY);
207 // Whether there is a hit on a glyph.
208 matchedCharacter = false;
210 CharacterIndex logicalIndex = 0;
212 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
213 const Length totalNumberOfLines = visualModel->mLines.Count();
214 if((0 == totalNumberOfGlyphs) ||
215 (0 == totalNumberOfLines))
219 const float characterSpacing = visualModel->GetCharacterSpacing();
221 // Whether there is a hit on a line.
222 bool matchedLine = false;
224 // Find which line is closest.
225 const LineIndex lineIndex = Text::GetClosestLine(visualModel,
229 if(!matchedLine && (CharacterHitTest::TAP == mode))
231 // Return the first or the last character if the touch point doesn't hit a line.
232 return (visualY < 0.f) ? 0 : logicalModel->mText.Count();
235 // Convert from text's coords to line's coords.
236 const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
238 // Transform the tap point from text's coords to line's coords.
239 visualX -= line.alignmentOffset;
241 // Get the positions of the glyphs.
242 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
244 // Get the character to glyph conversion table.
245 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
247 // Get the glyphs per character table.
248 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
250 // Get the characters per glyph table.
251 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
253 // Get the glyph's info buffer.
254 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
256 const CharacterIndex startCharacter = line.characterRun.characterIndex;
257 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
258 DALI_ASSERT_DEBUG(endCharacter <= logicalModel->mText.Count() && "Invalid line info");
260 // Whether this line is a bidirectional line.
261 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo(startCharacter);
263 // The character's direction buffer.
264 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
266 // Whether the touch point if before the first glyph.
267 bool isBeforeFirstGlyph = false;
269 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
270 CharacterIndex visualIndex = startCharacter;
271 Length numberOfVisualCharacters = 0;
272 float calculatedAdvance = 0.f;
273 Vector<CharacterIndex>& glyphToCharacterMap = visualModel->mGlyphsToCharacters;
274 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
275 for(; visualIndex < endCharacter; ++visualIndex)
277 // The character in logical order.
278 const CharacterIndex characterLogicalOrderIndex = (bidiLineFetched ? logicalModel->GetLogicalCharacterIndex(visualIndex) : visualIndex);
279 const CharacterDirection direction = (bidiLineFetched ? *(directionsBuffer + characterLogicalOrderIndex) : LTR);
281 // The number of glyphs for that character
282 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + characterLogicalOrderIndex);
283 ++numberOfVisualCharacters;
285 if(0 != numberOfGlyphs)
287 // Get the first character/glyph of the group of glyphs.
288 const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfVisualCharacters;
289 const CharacterIndex firstLogicalCharacterIndex = (bidiLineFetched ? logicalModel->GetLogicalCharacterIndex(firstVisualCharacterIndex) : firstVisualCharacterIndex);
290 const GlyphIndex firstLogicalGlyphIndex = *(charactersToGlyphBuffer + firstLogicalCharacterIndex);
292 // Get the metrics for the group of glyphs.
293 GlyphMetrics glyphMetrics;
294 calculatedAdvance = GetCalculatedAdvance(*(logicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + firstLogicalGlyphIndex))), characterSpacing, (*(visualModel->mGlyphs.Begin() + firstLogicalGlyphIndex)).advance);
295 GetGlyphsMetrics(firstLogicalGlyphIndex,
302 // Get the position of the first glyph.
303 const Vector2& position = *(positionsBuffer + firstLogicalGlyphIndex);
305 if(startCharacter == visualIndex)
307 const float glyphPosition = -glyphMetrics.xBearing + position.x;
309 if(visualX < glyphPosition)
311 isBeforeFirstGlyph = true;
316 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
317 Length numberOfCharacters = *(charactersPerGlyphBuffer + firstLogicalGlyphIndex);
320 // As characters are being traversed in visual order,
321 // for right to left ligatures, the character which contains the
322 // number of glyphs in the table is found first.
323 // Jump the number of characters to the next glyph is needed.
325 if(0 == numberOfCharacters)
327 // TODO: This is a workaround to fix an issue with complex characters in the arabic
328 // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
329 // There are characters that are not shaped in one glyph but in combination with
330 // the next one generates two of them.
331 // The visual to logical conversion table have characters in different order than
332 // expected even if all of them are arabic.
334 // The workaround doesn't fix the issue completely but it prevents the application
335 // to hang in an infinite loop.
337 // Find the number of characters.
338 for(GlyphIndex index = firstLogicalGlyphIndex + 1u;
339 (0 == numberOfCharacters) && (index < totalNumberOfGlyphs);
342 numberOfCharacters = *(charactersPerGlyphBuffer + index);
345 if(2u > numberOfCharacters)
350 --numberOfCharacters;
353 visualIndex += numberOfCharacters - 1u;
356 // Get the script of the character.
357 const Script script = logicalModel->GetScript(characterLogicalOrderIndex);
359 const bool isInterglyphIndex = (numberOfCharacters > numberOfGlyphs) && HasLigatureMustBreak(script);
360 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
361 const float glyphAdvance = glyphMetrics.advance / static_cast<float>(numberOfBlocks);
363 CharacterIndex index = 0;
364 for(; index < numberOfBlocks; ++index)
366 // Find the mid-point of the area containing the glyph
367 const float glyphCenter = -glyphMetrics.xBearing + position.x + (static_cast<float>(index) + 0.5f) * glyphAdvance;
369 if(visualX < glyphCenter)
371 matchedCharacter = true;
378 // If the glyph is shaped from more than one character, it matches the character of the glyph.
379 visualIndex = firstVisualCharacterIndex + index;
383 numberOfVisualCharacters = 0;
385 } // for characters in visual order.
387 // The number of characters of the whole text.
388 const Length totalNumberOfCharacters = logicalModel->mText.Count();
390 // Return the logical position of the cursor in characters.
392 if(!matchedCharacter)
394 if(isBeforeFirstGlyph)
396 // If no character is matched, then the first character (in visual order) of the line is used.
397 visualIndex = startCharacter;
401 // If no character is matched, then the last character (in visual order) of the line is used.
402 visualIndex = endCharacter;
406 // Get the paragraph direction.
407 const CharacterDirection paragraphDirection = line.direction;
409 if(totalNumberOfCharacters != visualIndex)
411 // The visual index is not at the end of the text.
413 if(LTR == paragraphDirection)
415 // The paragraph direction is left to right.
417 if(visualIndex == endCharacter)
419 // It places the cursor just before the last character in visual order.
420 // i.e. it places the cursor just before the '\n' or before the last character
421 // if there is a long line with no word breaks which is wrapped.
423 // It doesn't check if the closest line is the last one like the RTL branch below
424 // because the total number of characters is different than the visual index and
425 // the visual index is the last character of the line.
431 // The paragraph direction is right to left.
433 if((lineIndex != totalNumberOfLines - 1u) && // is not the last line.
434 (visualIndex == startCharacter))
436 // It places the cursor just after the first character in visual order.
437 // i.e. it places the cursor just after the '\n' or after the last character
438 // if there is a long line with no word breaks which is wrapped.
440 // If the last line doesn't end with '\n' it won't increase the visual index
441 // placing the cursor at the beginning of the line (in visual order).
448 // The visual index is at the end of text.
450 // 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.
451 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
452 // the cursor just before the last '\n'.
454 if((lineIndex != totalNumberOfLines - 1u) &&
455 TextAbstraction::IsNewParagraph(*(logicalModel->mText.Begin() + visualIndex - 1u)))
461 logicalIndex = (bidiLineFetched ? logicalModel->GetLogicalCursorIndex(visualIndex) : visualIndex);
463 // Handle Emoji clustering for cursor handling:
465 // - When there is Emoji contains multi unicodes and it is layoutted at the end of line (LineWrap case , is not new line case)
466 // - Try to click at the center or at the end of Emoji then the cursor appears inside Emoji
467 // - Example:"FamilyManWomanGirlBoy 👨‍👩‍👧‍👦"
468 const Script script = logicalModel->GetScript(logicalIndex);
469 if(IsOneOfEmojiScripts(script))
471 //TODO: Use this clustering for Emoji cases only. This needs more testing to generalize to all scripts.
472 CharacterRun emojiClusteredCharacters = RetrieveClusteredCharactersOfCharacterIndex(visualModel, logicalModel, logicalIndex);
473 logicalIndex = emojiClusteredCharacters.characterIndex;
476 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex);
478 DALI_ASSERT_DEBUG((logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0) && "GetClosestCursorIndex - Out of bounds index");
483 void GetCursorPosition(GetCursorPositionParameters& parameters,
484 float defaultFontLineHeight,
485 CursorInfo& cursorInfo)
487 const LineRun* const modelLines = parameters.visualModel->mLines.Begin();
488 if(NULL == modelLines)
494 // Whether the logical cursor position is at the end of the whole text.
495 const bool isLastPosition = parameters.logicalModel->mText.Count() == parameters.logical;
497 // Get the line where the character is laid-out.
498 const CharacterIndex characterOfLine = isLastPosition ? (parameters.logical - 1u) : parameters.logical;
500 // Whether the cursor is in the last position and the last position is a new paragraph character.
501 const bool isLastNewParagraph = parameters.isMultiline && isLastPosition && TextAbstraction::IsNewParagraph(*(parameters.logicalModel->mText.Begin() + characterOfLine));
503 const LineIndex lineIndex = parameters.visualModel->GetLineOfCharacter(characterOfLine);
504 const LineRun& line = *(modelLines + lineIndex);
506 CharacterIndex index;
507 GlyphMetrics glyphMetrics;
508 MetricsPtr& metrics = parameters.metrics;
509 GlyphIndex glyphIndex = 0u;
510 Length numberOfGlyphs = 0u;
512 if(isLastNewParagraph)
514 // The cursor is in a new line with no characters. Place the cursor in that line.
515 const LineIndex newLineIndex = lineIndex + 1u;
516 const LineRun& newLine = *(modelLines + newLineIndex);
518 cursorInfo.isSecondaryCursor = false;
520 // Set the line offset and height.
521 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
524 // The line height is the addition of the line ascender and the line descender.
525 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in cursor height.
526 cursorInfo.lineHeight = newLine.ascender - newLine.descender;
529 const Length totalNumberOfCharacters = parameters.logicalModel->mText.Count();
530 if(totalNumberOfCharacters > 0u)
532 index = totalNumberOfCharacters - 1u;
535 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
537 // Set the primary cursor's height.
538 // 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.
539 cursorInfo.primaryCursorHeight = (totalNumberOfCharacters > 0) ? (cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight) : defaultFontLineHeight;
541 // Set the primary cursor's position.
542 cursorInfo.primaryPosition.x = (LTR == line.direction) ? newLine.alignmentOffset : parameters.visualModel->mControlSize.width - newLine.alignmentOffset;
543 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
547 // Whether this line is a bidirectional line.
548 const bool bidiLineFetched = parameters.logicalModel->FetchBidirectionalLineInfo(characterOfLine);
550 // Check if the logical position is the first or the last one of the line.
551 const bool isFirstPositionOfLine = line.characterRun.characterIndex == parameters.logical;
552 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == parameters.logical;
554 // 'logical' is the logical 'cursor' index.
555 // Get the next and current logical 'character' index.
556 const CharacterIndex characterIndex = isFirstPositionOfLine ? parameters.logical : parameters.logical - 1u;
557 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : parameters.logical;
559 // The character's direction buffer.
560 const CharacterDirection* const directionsBuffer = bidiLineFetched ? parameters.logicalModel->mCharacterDirections.Begin() : NULL;
562 CharacterDirection isCurrentRightToLeft = false;
563 CharacterDirection isNextRightToLeft = false;
564 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
566 isCurrentRightToLeft = *(directionsBuffer + characterIndex);
567 isNextRightToLeft = *(directionsBuffer + nextCharacterIndex);
570 // Get the paragraph's direction.
571 const CharacterDirection isRightToLeftParagraph = line.direction;
573 // Check whether there is an alternative position:
574 cursorInfo.isSecondaryCursor = ((!isLastPositionOfLine && (isCurrentRightToLeft != isNextRightToLeft)) ||
575 (isLastPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)) ||
576 (isFirstPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)));
578 // Set the line offset and height.
579 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
582 // The line height is the addition of the line ascender and the line descender.
583 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in cursor height.
584 cursorInfo.lineHeight = line.ascender - line.descender;
586 // Calculate the primary cursor.
588 index = characterIndex;
589 if(cursorInfo.isSecondaryCursor)
591 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
593 if(isLastPositionOfLine)
595 // The position of the cursor after the last character needs special
596 // care depending on its direction and the direction of the paragraph.
598 // Need to find the first character after the last character with the paragraph's direction.
599 // i.e l0 l1 l2 r0 r1 should find r0.
601 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
604 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
607 else if(isFirstPositionOfLine)
609 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
612 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
617 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? characterIndex : nextCharacterIndex;
621 const Length* const charactersPerGlyphBuffer = parameters.visualModel->mCharactersPerGlyph.Begin();
622 const CharacterIndex* const glyphsToCharactersBuffer = parameters.visualModel->mGlyphsToCharacters.Begin();
623 const Vector2* const glyphPositionsBuffer = parameters.visualModel->mGlyphPositions.Begin();
624 const float characterSpacing = parameters.visualModel->GetCharacterSpacing();
626 // Get the metrics for the group of glyphs.
627 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
629 // Convert the cursor position into the glyph position.
630 const GlyphIndex primaryGlyphIndex = glyphIndex;
631 const Length primaryNumberOfCharacters = *(charactersPerGlyphBuffer + primaryGlyphIndex);
633 // Whether to add the glyph's advance to the cursor position.
634 // 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,
635 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
636 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
657 // Where F -> isFirstPosition
658 // L -> isLastPosition
659 // C -> isCurrentRightToLeft
660 // P -> isRightToLeftParagraph
661 // A -> Whether to add the glyph's advance.
663 const bool addGlyphAdvance = ((isLastPositionOfLine && !isRightToLeftParagraph) ||
664 (isFirstPositionOfLine && isRightToLeftParagraph) ||
665 (!isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft));
667 float glyphAdvance = addGlyphAdvance ? (glyphMetrics.advance) : 0.f;
669 if(!isLastPositionOfLine &&
670 (primaryNumberOfCharacters > 1u))
672 const CharacterIndex firstIndex = *(glyphsToCharactersBuffer + primaryGlyphIndex);
674 bool isCurrentRightToLeft = false;
675 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
677 isCurrentRightToLeft = *(directionsBuffer + index);
680 Length numberOfGlyphAdvance = (isFirstPositionOfLine ? 0 : 1u) + characterIndex - firstIndex;
681 if(isCurrentRightToLeft)
683 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
686 glyphAdvance = static_cast<float>(numberOfGlyphAdvance) * (glyphMetrics.advance) / static_cast<float>(primaryNumberOfCharacters);
689 // Get the glyph position and x bearing (in the line's coords).
690 const Vector2& primaryPosition = *(glyphPositionsBuffer + primaryGlyphIndex);
692 // Set the primary cursor's height.
693 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
695 cursorInfo.glyphOffset = line.ascender - glyphMetrics.ascender;
696 // Set the primary cursor's position.
697 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
698 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + cursorInfo.glyphOffset;
700 // Transform the cursor info from line's coords to text's coords.
701 cursorInfo.primaryPosition.x += line.alignmentOffset;
703 // Calculate the secondary cursor.
704 if(cursorInfo.isSecondaryCursor)
706 // Set the secondary cursor's height.
707 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
709 CharacterIndex index = characterIndex;
710 if(!isLastPositionOfLine)
712 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? nextCharacterIndex : characterIndex;
715 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
717 const GlyphIndex secondaryGlyphIndex = glyphIndex;
718 const Vector2& secondaryPosition = *(glyphPositionsBuffer + secondaryGlyphIndex);
720 // Set the secondary cursor's position.
733 // Where F -> isFirstPosition
734 // C -> isCurrentRightToLeft
735 // P -> isRightToLeftParagraph
736 // A -> Whether to add the glyph's advance.
738 const bool addGlyphAdvance = ((!isFirstPositionOfLine && !isCurrentRightToLeft) ||
739 (isFirstPositionOfLine && !isRightToLeftParagraph));
741 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + (addGlyphAdvance ? (glyphMetrics.advance + characterSpacing) : 0.f);
742 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
744 // Transform the cursor info from line's coords to text's coords.
745 cursorInfo.secondaryPosition.x += line.alignmentOffset;
750 bool FindSelectionIndices(VisualModelPtr visualModel,
751 LogicalModelPtr logicalModel,
755 CharacterIndex& startIndex,
756 CharacterIndex& endIndex,
757 CharacterIndex& noTextHitIndex)
761 |-------------------------------------------------------|------------------------------------------|
762 | On a word | The word |
763 | On a single white space between words | The word before or after the white space |
764 | On one of the multiple contiguous white spaces | The white spaces |
765 | On a single white space which is in the position zero | The white space and the next word |
766 | On a new paragraph character | The word or group of white spaces before |
767 |-------------------------------------------------------|------------------------------------------|
769 const Length totalNumberOfCharacters = logicalModel->mText.Count();
774 if(0 == totalNumberOfCharacters)
776 // Nothing to do if the model is empty.
780 bool matchedCharacter = false;
781 CharacterIndex hitCharacter = Text::GetClosestCursorIndex(visualModel,
786 CharacterHitTest::TAP,
789 if(!matchedCharacter)
791 noTextHitIndex = hitCharacter;
794 DALI_ASSERT_DEBUG((hitCharacter <= totalNumberOfCharacters) && "GetClosestCursorIndex returned out of bounds index");
796 if(hitCharacter >= totalNumberOfCharacters)
798 // Closest hit character is the last character.
799 if(hitCharacter == totalNumberOfCharacters)
801 hitCharacter--; //Hit character index set to last character in logical model
805 // hitCharacter is out of bounds
810 const Character* const textBuffer = logicalModel->mText.Begin();
812 startIndex = hitCharacter;
813 endIndex = hitCharacter;
815 // Whether the hit character is a new paragraph character.
816 const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + hitCharacter));
818 // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
819 const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + hitCharacter)) && !isHitCharacterNewParagraph;
821 FindWordData data(textBuffer,
822 totalNumberOfCharacters,
824 isHitCharacterWhiteSpace,
825 isHitCharacterNewParagraph);
827 if(isHitCharacterNewParagraph)
829 // Find the first character before the hit one which is not a new paragraph character.
833 endIndex = hitCharacter - 1u;
834 for(; endIndex > 0; --endIndex)
836 const Dali::Toolkit::Text::Character character = *(data.textBuffer + endIndex);
838 if(!Dali::TextAbstraction::IsNewParagraph(character))
845 data.hitCharacter = endIndex;
846 data.isNewParagraph = false;
847 data.isWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + data.hitCharacter));
850 // Find the start of the word.
851 FindStartOfWord(data);
852 startIndex = data.foundIndex;
854 // Find the end of the word.
856 endIndex = data.foundIndex;
858 if(1u == (endIndex - startIndex))
860 if(isHitCharacterWhiteSpace)
862 // Select the word before or after the white space
864 if(0 == hitCharacter)
866 data.isWhiteSpace = false;
868 endIndex = data.foundIndex;
870 else if(hitCharacter > 0)
872 // Find the start of the word.
873 data.hitCharacter = hitCharacter - 1u;
874 data.isWhiteSpace = false;
875 FindStartOfWord(data);
876 startIndex = data.foundIndex;
883 return matchedCharacter;
888 } // namespace Toolkit