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;
158 totalHeight += GetLineHeight(lineRun);
160 if(visualY < totalHeight)
172 return lineIndex - 1u;
175 float CalculateLineOffset(const Vector<LineRun>& lines,
180 for(Vector<LineRun>::ConstIterator it = lines.Begin(),
181 endIt = lines.Begin() + lineIndex;
185 const LineRun& lineRun = *it;
187 offset += GetLineHeight(lineRun);
193 CharacterIndex GetClosestCursorIndex(VisualModelPtr visualModel,
194 LogicalModelPtr logicalModel,
198 CharacterHitTest::Mode mode,
199 bool& matchedCharacter)
201 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY);
203 // Whether there is a hit on a glyph.
204 matchedCharacter = false;
206 CharacterIndex logicalIndex = 0;
208 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
209 const Length totalNumberOfLines = visualModel->mLines.Count();
210 if((0 == totalNumberOfGlyphs) ||
211 (0 == totalNumberOfLines))
216 // Whether there is a hit on a line.
217 bool matchedLine = false;
219 // Find which line is closest.
220 const LineIndex lineIndex = Text::GetClosestLine(visualModel,
224 if(!matchedLine && (CharacterHitTest::TAP == mode))
226 // Return the first or the last character if the touch point doesn't hit a line.
227 return (visualY < 0.f) ? 0 : logicalModel->mText.Count();
230 // Convert from text's coords to line's coords.
231 const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
233 // Transform the tap point from text's coords to line's coords.
234 visualX -= line.alignmentOffset;
236 // Get the positions of the glyphs.
237 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
239 // Get the character to glyph conversion table.
240 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
242 // Get the glyphs per character table.
243 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
245 // Get the characters per glyph table.
246 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
248 // Get the glyph's info buffer.
249 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
251 const CharacterIndex startCharacter = line.characterRun.characterIndex;
252 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
253 DALI_ASSERT_DEBUG(endCharacter <= logicalModel->mText.Count() && "Invalid line info");
255 // Whether this line is a bidirectional line.
256 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo(startCharacter);
258 // The character's direction buffer.
259 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
261 // Whether the touch point if before the first glyph.
262 bool isBeforeFirstGlyph = false;
264 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
265 CharacterIndex visualIndex = startCharacter;
266 Length numberOfVisualCharacters = 0;
267 for(; visualIndex < endCharacter; ++visualIndex)
269 // The character in logical order.
270 const CharacterIndex characterLogicalOrderIndex = (bidiLineFetched ? logicalModel->GetLogicalCharacterIndex(visualIndex) : visualIndex);
271 const CharacterDirection direction = (bidiLineFetched ? *(directionsBuffer + characterLogicalOrderIndex) : LTR);
273 // The number of glyphs for that character
274 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + characterLogicalOrderIndex);
275 ++numberOfVisualCharacters;
277 if(0 != numberOfGlyphs)
279 // Get the first character/glyph of the group of glyphs.
280 const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfVisualCharacters;
281 const CharacterIndex firstLogicalCharacterIndex = (bidiLineFetched ? logicalModel->GetLogicalCharacterIndex(firstVisualCharacterIndex) : firstVisualCharacterIndex);
282 const GlyphIndex firstLogicalGlyphIndex = *(charactersToGlyphBuffer + firstLogicalCharacterIndex);
284 // Get the metrics for the group of glyphs.
285 GlyphMetrics glyphMetrics;
286 GetGlyphsMetrics(firstLogicalGlyphIndex,
292 // Get the position of the first glyph.
293 const Vector2& position = *(positionsBuffer + firstLogicalGlyphIndex);
295 if(startCharacter == visualIndex)
297 const float glyphPosition = -glyphMetrics.xBearing + position.x;
299 if(visualX < glyphPosition)
301 isBeforeFirstGlyph = true;
306 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
307 Length numberOfCharacters = *(charactersPerGlyphBuffer + firstLogicalGlyphIndex);
310 // As characters are being traversed in visual order,
311 // for right to left ligatures, the character which contains the
312 // number of glyphs in the table is found first.
313 // Jump the number of characters to the next glyph is needed.
315 if(0 == numberOfCharacters)
317 // TODO: This is a workaround to fix an issue with complex characters in the arabic
318 // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
319 // There are characters that are not shaped in one glyph but in combination with
320 // the next one generates two of them.
321 // The visual to logical conversion table have characters in different order than
322 // expected even if all of them are arabic.
324 // The workaround doesn't fix the issue completely but it prevents the application
325 // to hang in an infinite loop.
327 // Find the number of characters.
328 for(GlyphIndex index = firstLogicalGlyphIndex + 1u;
329 (0 == numberOfCharacters) && (index < totalNumberOfGlyphs);
332 numberOfCharacters = *(charactersPerGlyphBuffer + index);
335 if(2u > numberOfCharacters)
340 --numberOfCharacters;
343 visualIndex += numberOfCharacters - 1u;
346 // Get the script of the character.
347 const Script script = logicalModel->GetScript(characterLogicalOrderIndex);
349 const bool isInterglyphIndex = (numberOfCharacters > numberOfGlyphs) && HasLigatureMustBreak(script);
350 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
351 const float glyphAdvance = glyphMetrics.advance / static_cast<float>(numberOfBlocks);
353 CharacterIndex index = 0;
354 for(; index < numberOfBlocks; ++index)
356 // Find the mid-point of the area containing the glyph
357 const float glyphCenter = -glyphMetrics.xBearing + position.x + (static_cast<float>(index) + 0.5f) * glyphAdvance;
359 if(visualX < glyphCenter)
361 matchedCharacter = true;
368 // If the glyph is shaped from more than one character, it matches the character of the glyph.
369 visualIndex = firstVisualCharacterIndex + index;
373 numberOfVisualCharacters = 0;
375 } // for characters in visual order.
377 // The number of characters of the whole text.
378 const Length totalNumberOfCharacters = logicalModel->mText.Count();
380 // Return the logical position of the cursor in characters.
382 if(!matchedCharacter)
384 if(isBeforeFirstGlyph)
386 // If no character is matched, then the first character (in visual order) of the line is used.
387 visualIndex = startCharacter;
391 // If no character is matched, then the last character (in visual order) of the line is used.
392 visualIndex = endCharacter;
396 // Get the paragraph direction.
397 const CharacterDirection paragraphDirection = line.direction;
399 if(totalNumberOfCharacters != visualIndex)
401 // The visual index is not at the end of the text.
403 if(LTR == paragraphDirection)
405 // The paragraph direction is left to right.
407 if(visualIndex == endCharacter)
409 // It places the cursor just before the last character in visual order.
410 // i.e. it places the cursor just before the '\n' or before the last character
411 // if there is a long line with no word breaks which is wrapped.
413 // It doesn't check if the closest line is the last one like the RTL branch below
414 // because the total number of characters is different than the visual index and
415 // the visual index is the last character of the line.
421 // The paragraph direction is right to left.
423 if((lineIndex != totalNumberOfLines - 1u) && // is not the last line.
424 (visualIndex == startCharacter))
426 // It places the cursor just after the first character in visual order.
427 // i.e. it places the cursor just after the '\n' or after the last character
428 // if there is a long line with no word breaks which is wrapped.
430 // If the last line doesn't end with '\n' it won't increase the visual index
431 // placing the cursor at the beginning of the line (in visual order).
438 // The visual index is at the end of text.
440 // 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.
441 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
442 // the cursor just before the last '\n'.
444 if((lineIndex != totalNumberOfLines - 1u) &&
445 TextAbstraction::IsNewParagraph(*(logicalModel->mText.Begin() + visualIndex - 1u)))
451 logicalIndex = (bidiLineFetched ? logicalModel->GetLogicalCursorIndex(visualIndex) : visualIndex);
453 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex);
455 DALI_ASSERT_DEBUG((logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0) && "GetClosestCursorIndex - Out of bounds index");
460 void GetCursorPosition(GetCursorPositionParameters& parameters,
461 float defaultFontLineHeight,
462 CursorInfo& cursorInfo)
464 const LineRun* const modelLines = parameters.visualModel->mLines.Begin();
465 if(NULL == modelLines)
471 // Whether the logical cursor position is at the end of the whole text.
472 const bool isLastPosition = parameters.logicalModel->mText.Count() == parameters.logical;
474 // Get the line where the character is laid-out.
475 const CharacterIndex characterOfLine = isLastPosition ? (parameters.logical - 1u) : parameters.logical;
477 // Whether the cursor is in the last position and the last position is a new paragraph character.
478 const bool isLastNewParagraph = parameters.isMultiline && isLastPosition && TextAbstraction::IsNewParagraph(*(parameters.logicalModel->mText.Begin() + characterOfLine));
480 const LineIndex lineIndex = parameters.visualModel->GetLineOfCharacter(characterOfLine);
481 const LineRun& line = *(modelLines + lineIndex);
483 const GlyphIndex* const charactersToGlyphBuffer = parameters.visualModel->mCharactersToGlyph.Begin();
484 const Length* const glyphsPerCharacterBuffer = parameters.visualModel->mGlyphsPerCharacter.Begin();
485 const GlyphInfo* const glyphInfoBuffer = parameters.visualModel->mGlyphs.Begin();
486 CharacterIndex index;
487 GlyphMetrics glyphMetrics;
488 MetricsPtr& metrics = parameters.metrics;
489 GlyphIndex glyphIndex = 0u;
490 Length numberOfGlyphs = 0u;
492 if(isLastNewParagraph)
494 // The cursor is in a new line with no characters. Place the cursor in that line.
495 const LineIndex newLineIndex = lineIndex + 1u;
496 const LineRun& newLine = *(modelLines + newLineIndex);
498 cursorInfo.isSecondaryCursor = false;
500 // Set the line offset and height.
501 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
504 cursorInfo.lineHeight = GetLineHeight(newLine);
507 const Length totalNumberOfCharacters = parameters.logicalModel->mText.Count();
508 if(totalNumberOfCharacters > 0u)
510 index = totalNumberOfCharacters - 1u;
513 GetGlyphMetricsFromCharacterIndex(index, glyphInfoBuffer, charactersToGlyphBuffer, glyphsPerCharacterBuffer, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
515 // Set the primary cursor's height.
516 // 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.
517 cursorInfo.primaryCursorHeight = (totalNumberOfCharacters > 0) ? (cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight) : defaultFontLineHeight;
519 // Set the primary cursor's position.
520 cursorInfo.primaryPosition.x = (LTR == line.direction) ? newLine.alignmentOffset : parameters.visualModel->mControlSize.width - newLine.alignmentOffset;
521 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
525 // Whether this line is a bidirectional line.
526 const bool bidiLineFetched = parameters.logicalModel->FetchBidirectionalLineInfo(characterOfLine);
528 // Check if the logical position is the first or the last one of the line.
529 const bool isFirstPositionOfLine = line.characterRun.characterIndex == parameters.logical;
530 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == parameters.logical;
532 // 'logical' is the logical 'cursor' index.
533 // Get the next and current logical 'character' index.
534 const CharacterIndex characterIndex = isFirstPositionOfLine ? parameters.logical : parameters.logical - 1u;
535 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : parameters.logical;
537 // The character's direction buffer.
538 const CharacterDirection* const directionsBuffer = bidiLineFetched ? parameters.logicalModel->mCharacterDirections.Begin() : NULL;
540 CharacterDirection isCurrentRightToLeft = false;
541 CharacterDirection isNextRightToLeft = false;
542 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
544 isCurrentRightToLeft = *(directionsBuffer + characterIndex);
545 isNextRightToLeft = *(directionsBuffer + nextCharacterIndex);
548 // Get the paragraph's direction.
549 const CharacterDirection isRightToLeftParagraph = line.direction;
551 // Check whether there is an alternative position:
552 cursorInfo.isSecondaryCursor = ((!isLastPositionOfLine && (isCurrentRightToLeft != isNextRightToLeft)) ||
553 (isLastPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)) ||
554 (isFirstPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)));
556 // Set the line offset and height.
557 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
560 cursorInfo.lineHeight = GetLineHeight(line);
562 // Calculate the primary cursor.
564 index = characterIndex;
565 if(cursorInfo.isSecondaryCursor)
567 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
569 if(isLastPositionOfLine)
571 // The position of the cursor after the last character needs special
572 // care depending on its direction and the direction of the paragraph.
574 // Need to find the first character after the last character with the paragraph's direction.
575 // i.e l0 l1 l2 r0 r1 should find r0.
577 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
580 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
583 else if(isFirstPositionOfLine)
585 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
588 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
593 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? characterIndex : nextCharacterIndex;
597 const Length* const charactersPerGlyphBuffer = parameters.visualModel->mCharactersPerGlyph.Begin();
598 const CharacterIndex* const glyphsToCharactersBuffer = parameters.visualModel->mGlyphsToCharacters.Begin();
599 const Vector2* const glyphPositionsBuffer = parameters.visualModel->mGlyphPositions.Begin();
601 GetGlyphMetricsFromCharacterIndex(index, glyphInfoBuffer, charactersToGlyphBuffer, glyphsPerCharacterBuffer, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
602 const GlyphIndex primaryGlyphIndex = glyphIndex;
603 const Length primaryNumberOfCharacters = *(charactersPerGlyphBuffer + primaryGlyphIndex);
605 // Whether to add the glyph's advance to the cursor position.
606 // 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,
607 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
608 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
629 // Where F -> isFirstPosition
630 // L -> isLastPosition
631 // C -> isCurrentRightToLeft
632 // P -> isRightToLeftParagraph
633 // A -> Whether to add the glyph's advance.
635 const bool addGlyphAdvance = ((isLastPositionOfLine && !isRightToLeftParagraph) ||
636 (isFirstPositionOfLine && isRightToLeftParagraph) ||
637 (!isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft));
639 float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
641 if(!isLastPositionOfLine &&
642 (primaryNumberOfCharacters > 1u))
644 const CharacterIndex firstIndex = *(glyphsToCharactersBuffer + primaryGlyphIndex);
646 bool isCurrentRightToLeft = false;
647 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
649 isCurrentRightToLeft = *(directionsBuffer + index);
652 Length numberOfGlyphAdvance = (isFirstPositionOfLine ? 0 : 1u) + characterIndex - firstIndex;
653 if(isCurrentRightToLeft)
655 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
658 glyphAdvance = static_cast<float>(numberOfGlyphAdvance) * glyphMetrics.advance / static_cast<float>(primaryNumberOfCharacters);
661 // Get the glyph position and x bearing (in the line's coords).
662 const Vector2& primaryPosition = *(glyphPositionsBuffer + primaryGlyphIndex);
664 // Set the primary cursor's height.
665 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
667 cursorInfo.glyphOffset = line.ascender - glyphMetrics.ascender;
668 // Set the primary cursor's position.
669 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
670 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + cursorInfo.glyphOffset;
672 // Transform the cursor info from line's coords to text's coords.
673 cursorInfo.primaryPosition.x += line.alignmentOffset;
675 // Calculate the secondary cursor.
676 if(cursorInfo.isSecondaryCursor)
678 // Set the secondary cursor's height.
679 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
681 CharacterIndex index = characterIndex;
682 if(!isLastPositionOfLine)
684 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? nextCharacterIndex : characterIndex;
687 GetGlyphMetricsFromCharacterIndex(index, glyphInfoBuffer, charactersToGlyphBuffer, glyphsPerCharacterBuffer, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
688 const GlyphIndex secondaryGlyphIndex = glyphIndex;
689 const Vector2& secondaryPosition = *(glyphPositionsBuffer + secondaryGlyphIndex);
691 // Set the secondary cursor's position.
704 // Where F -> isFirstPosition
705 // C -> isCurrentRightToLeft
706 // P -> isRightToLeftParagraph
707 // A -> Whether to add the glyph's advance.
709 const bool addGlyphAdvance = ((!isFirstPositionOfLine && !isCurrentRightToLeft) ||
710 (isFirstPositionOfLine && !isRightToLeftParagraph));
712 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + (addGlyphAdvance ? glyphMetrics.advance : 0.f);
713 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
715 // Transform the cursor info from line's coords to text's coords.
716 cursorInfo.secondaryPosition.x += line.alignmentOffset;
721 bool FindSelectionIndices(VisualModelPtr visualModel,
722 LogicalModelPtr logicalModel,
726 CharacterIndex& startIndex,
727 CharacterIndex& endIndex,
728 CharacterIndex& noTextHitIndex)
732 |-------------------------------------------------------|------------------------------------------|
733 | On a word | The word |
734 | On a single white space between words | The word before or after the white space |
735 | On one of the multiple contiguous white spaces | The white spaces |
736 | On a single white space which is in the position zero | The white space and the next word |
737 | On a new paragraph character | The word or group of white spaces before |
738 |-------------------------------------------------------|------------------------------------------|
740 const Length totalNumberOfCharacters = logicalModel->mText.Count();
745 if(0 == totalNumberOfCharacters)
747 // Nothing to do if the model is empty.
751 bool matchedCharacter = false;
752 CharacterIndex hitCharacter = Text::GetClosestCursorIndex(visualModel,
757 CharacterHitTest::TAP,
760 if(!matchedCharacter)
762 noTextHitIndex = hitCharacter;
765 DALI_ASSERT_DEBUG((hitCharacter <= totalNumberOfCharacters) && "GetClosestCursorIndex returned out of bounds index");
767 if(hitCharacter >= totalNumberOfCharacters)
769 // Closest hit character is the last character.
770 if(hitCharacter == totalNumberOfCharacters)
772 hitCharacter--; //Hit character index set to last character in logical model
776 // hitCharacter is out of bounds
781 const Character* const textBuffer = logicalModel->mText.Begin();
783 startIndex = hitCharacter;
784 endIndex = hitCharacter;
786 // Whether the hit character is a new paragraph character.
787 const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + hitCharacter));
789 // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
790 const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + hitCharacter)) && !isHitCharacterNewParagraph;
792 FindWordData data(textBuffer,
793 totalNumberOfCharacters,
795 isHitCharacterWhiteSpace,
796 isHitCharacterNewParagraph);
798 if(isHitCharacterNewParagraph)
800 // Find the first character before the hit one which is not a new paragraph character.
804 endIndex = hitCharacter - 1u;
805 for(; endIndex > 0; --endIndex)
807 const Dali::Toolkit::Text::Character character = *(data.textBuffer + endIndex);
809 if(!Dali::TextAbstraction::IsNewParagraph(character))
816 data.hitCharacter = endIndex;
817 data.isNewParagraph = false;
818 data.isWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + data.hitCharacter));
821 // Find the start of the word.
822 FindStartOfWord(data);
823 startIndex = data.foundIndex;
825 // Find the end of the word.
827 endIndex = data.foundIndex;
829 if(1u == (endIndex - startIndex))
831 if(isHitCharacterWhiteSpace)
833 // Select the word before or after the white space
835 if(0 == hitCharacter)
837 data.isWhiteSpace = false;
839 endIndex = data.foundIndex;
841 else if(hitCharacter > 0)
843 // Find the start of the word.
844 data.hitCharacter = hitCharacter - 1u;
845 data.isWhiteSpace = false;
846 FindStartOfWord(data);
847 startIndex = data.foundIndex;
854 return matchedCharacter;
859 } // namespace Toolkit