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>
28 #include <dali-toolkit/internal/text/rendering/styles/character-spacing-helper-functions.h>
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
36 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction.
40 FindWordData(const Dali::Toolkit::Text::Character* const textBuffer,
41 Dali::Toolkit::Text::Length totalNumberOfCharacters,
42 Dali::Toolkit::Text::CharacterIndex hitCharacter,
45 : textBuffer(textBuffer),
46 totalNumberOfCharacters(totalNumberOfCharacters),
47 hitCharacter(hitCharacter),
49 isWhiteSpace(isWhiteSpace),
50 isNewParagraph(isNewParagraph)
58 const Dali::Toolkit::Text::Character* const textBuffer;
59 Dali::Toolkit::Text::Length totalNumberOfCharacters;
60 Dali::Toolkit::Text::CharacterIndex hitCharacter;
61 Dali::Toolkit::Text::CharacterIndex foundIndex;
62 bool isWhiteSpace : 1u;
63 bool isNewParagraph : 1u;
66 bool IsWhiteSpaceOrNewParagraph(Dali::Toolkit::Text::Character character,
68 bool isHitWhiteSpaceOrNewParagraph)
70 bool isWhiteSpaceOrNewParagraph = false;
71 if(isHitWhiteSpaceOrNewParagraph)
75 // Whether the current character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
76 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace(character) && !Dali::TextAbstraction::IsNewParagraph(character);
80 // Whether the current character is a new paragraph character.
81 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsNewParagraph(character);
86 // Whether the current character is a white space or a new paragraph character (note the new paragraph character is a white space as well).
87 isWhiteSpaceOrNewParagraph = Dali::TextAbstraction::IsWhiteSpace(character);
90 return isWhiteSpaceOrNewParagraph;
93 void FindStartOfWord(FindWordData& data)
95 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
97 for(data.foundIndex = data.hitCharacter; data.foundIndex > 0; --data.foundIndex)
99 const Dali::Toolkit::Text::Character character = *(data.textBuffer + data.foundIndex - 1u);
101 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph(character,
103 isHitWhiteSpaceOrNewParagraph);
105 if(isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph)
112 void FindEndOfWord(FindWordData& data)
114 const bool isHitWhiteSpaceOrNewParagraph = data.isWhiteSpace || data.isNewParagraph;
116 for(data.foundIndex = data.hitCharacter + 1u; data.foundIndex < data.totalNumberOfCharacters; ++data.foundIndex)
118 const Dali::Toolkit::Text::Character character = *(data.textBuffer + data.foundIndex);
120 const bool isWhiteSpaceOrNewParagraph = IsWhiteSpaceOrNewParagraph(character,
122 isHitWhiteSpaceOrNewParagraph);
124 if(isHitWhiteSpaceOrNewParagraph != isWhiteSpaceOrNewParagraph)
139 LineIndex GetClosestLine(VisualModelPtr visualModel,
143 float totalHeight = 0.f;
144 LineIndex lineIndex = 0;
152 const Vector<LineRun>& lines = visualModel->mLines;
154 for(Vector<LineRun>::ConstIterator it = lines.Begin(),
159 const LineRun& lineRun = *it;
160 bool isLastLine = (it + 1 == endIt);
162 totalHeight += GetLineHeight(lineRun, isLastLine);
164 if(visualY < totalHeight)
176 return lineIndex - 1u;
179 float CalculateLineOffset(const Vector<LineRun>& lines,
184 for(Vector<LineRun>::ConstIterator it = lines.Begin(),
185 endIt = lines.Begin() + lineIndex;
189 const LineRun& lineRun = *it;
190 bool isLastLine = (it + 1 == lines.End());
192 offset += GetLineHeight(lineRun, isLastLine);
198 CharacterIndex GetClosestCursorIndex(VisualModelPtr visualModel,
199 LogicalModelPtr logicalModel,
203 CharacterHitTest::Mode mode,
204 bool& matchedCharacter)
206 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY);
208 // Whether there is a hit on a glyph.
209 matchedCharacter = false;
211 CharacterIndex logicalIndex = 0;
213 const Length totalNumberOfGlyphs = visualModel->mGlyphs.Count();
214 const Length totalNumberOfLines = visualModel->mLines.Count();
215 if((0 == totalNumberOfGlyphs) ||
216 (0 == totalNumberOfLines))
221 // Get the character-spacing runs.
222 const Vector<CharacterSpacingGlyphRun>& characterSpacingGlyphRuns = visualModel->GetCharacterSpacingGlyphRuns();
223 const float modelCharacterSpacing = visualModel->GetCharacterSpacing();
225 // Whether there is a hit on a line.
226 bool matchedLine = false;
228 // Find which line is closest.
229 const LineIndex lineIndex = Text::GetClosestLine(visualModel,
233 if(!matchedLine && (CharacterHitTest::TAP == mode))
235 // Return the first or the last character if the touch point doesn't hit a line.
236 return (visualY < 0.f) ? 0 : logicalModel->mText.Count();
239 // Convert from text's coords to line's coords.
240 const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
242 // Transform the tap point from text's coords to line's coords.
243 visualX -= line.alignmentOffset;
245 // Get the positions of the glyphs.
246 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
248 // Get the character to glyph conversion table.
249 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
251 // Get the glyphs per character table.
252 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
254 // Get the characters per glyph table.
255 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
257 // Get the glyph's info buffer.
258 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
260 const CharacterIndex startCharacter = line.characterRun.characterIndex;
261 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
262 DALI_ASSERT_DEBUG(endCharacter <= logicalModel->mText.Count() && "Invalid line info");
264 // Whether this line is a bidirectional line.
265 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo(startCharacter);
267 // The character's direction buffer.
268 const CharacterDirection* const directionsBuffer = bidiLineFetched ? logicalModel->mCharacterDirections.Begin() : NULL;
270 // Whether the touch point if before the first glyph.
271 bool isBeforeFirstGlyph = false;
273 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
274 CharacterIndex visualIndex = startCharacter;
275 Length numberOfVisualCharacters = 0;
276 float calculatedAdvance = 0.f;
277 Vector<CharacterIndex>& glyphToCharacterMap = visualModel->mGlyphsToCharacters;
278 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
279 for(; visualIndex < endCharacter; ++visualIndex)
281 // The character in logical order.
282 const CharacterIndex characterLogicalOrderIndex = (bidiLineFetched ? logicalModel->GetLogicalCharacterIndex(visualIndex) : visualIndex);
283 const CharacterDirection direction = (bidiLineFetched ? *(directionsBuffer + characterLogicalOrderIndex) : LTR);
285 // The number of glyphs for that character
286 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + characterLogicalOrderIndex);
287 ++numberOfVisualCharacters;
289 if(0 != numberOfGlyphs)
291 // Get the first character/glyph of the group of glyphs.
292 const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfVisualCharacters;
293 const CharacterIndex firstLogicalCharacterIndex = (bidiLineFetched ? logicalModel->GetLogicalCharacterIndex(firstVisualCharacterIndex) : firstVisualCharacterIndex);
294 const GlyphIndex firstLogicalGlyphIndex = *(charactersToGlyphBuffer + firstLogicalCharacterIndex);
296 // Get the metrics for the group of glyphs.
297 GlyphMetrics glyphMetrics;
298 const float characterSpacing = GetGlyphCharacterSpacing(firstLogicalGlyphIndex, characterSpacingGlyphRuns, modelCharacterSpacing);
299 calculatedAdvance = GetCalculatedAdvance(*(logicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + firstLogicalGlyphIndex))), characterSpacing, (*(visualModel->mGlyphs.Begin() + firstLogicalGlyphIndex)).advance);
300 GetGlyphsMetrics(firstLogicalGlyphIndex,
307 // Get the position of the first glyph.
308 const Vector2& position = *(positionsBuffer + firstLogicalGlyphIndex);
310 if(startCharacter == visualIndex)
312 const float glyphPosition = -glyphMetrics.xBearing + position.x;
314 if(visualX < glyphPosition)
316 isBeforeFirstGlyph = true;
321 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic (ل + ا).
322 Length numberOfCharacters = *(charactersPerGlyphBuffer + firstLogicalGlyphIndex);
325 // As characters are being traversed in visual order,
326 // for right to left ligatures, the character which contains the
327 // number of glyphs in the table is found first.
328 // Jump the number of characters to the next glyph is needed.
330 if(0 == numberOfCharacters)
332 // TODO: This is a workaround to fix an issue with complex characters in the arabic
333 // script like i.e. رّ or الأَبْجَدِيَّة العَرَبِيَّة
334 // There are characters that are not shaped in one glyph but in combination with
335 // the next one generates two of them.
336 // The visual to logical conversion table have characters in different order than
337 // expected even if all of them are arabic.
339 // The workaround doesn't fix the issue completely but it prevents the application
340 // to hang in an infinite loop.
342 // Find the number of characters.
343 for(GlyphIndex index = firstLogicalGlyphIndex + 1u;
344 (0 == numberOfCharacters) && (index < totalNumberOfGlyphs);
347 numberOfCharacters = *(charactersPerGlyphBuffer + index);
350 if(2u > numberOfCharacters)
355 --numberOfCharacters;
358 visualIndex += numberOfCharacters - 1u;
361 // Get the script of the character.
362 const Script script = logicalModel->GetScript(characterLogicalOrderIndex);
364 const bool isInterglyphIndex = (numberOfCharacters > numberOfGlyphs) && HasLigatureMustBreak(script);
365 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
366 const float glyphAdvance = glyphMetrics.advance / static_cast<float>(numberOfBlocks);
368 CharacterIndex index = 0;
369 for(; index < numberOfBlocks; ++index)
371 // Find the mid-point of the area containing the glyph
372 const float glyphCenter = -glyphMetrics.xBearing + position.x + (static_cast<float>(index) + 0.5f) * glyphAdvance;
374 if(visualX < glyphCenter)
376 matchedCharacter = true;
383 // If the glyph is shaped from more than one character, it matches the character of the glyph.
384 visualIndex = firstVisualCharacterIndex + index;
388 numberOfVisualCharacters = 0;
390 } // for characters in visual order.
392 // The number of characters of the whole text.
393 const Length totalNumberOfCharacters = logicalModel->mText.Count();
395 // Return the logical position of the cursor in characters.
397 if(!matchedCharacter)
399 if(isBeforeFirstGlyph)
401 // If no character is matched, then the first character (in visual order) of the line is used.
402 visualIndex = startCharacter;
406 // If no character is matched, then the last character (in visual order) of the line is used.
407 visualIndex = endCharacter;
411 // Get the paragraph direction.
412 const CharacterDirection paragraphDirection = line.direction;
414 if(totalNumberOfCharacters != visualIndex)
416 // The visual index is not at the end of the text.
418 if(LTR == paragraphDirection)
420 // The paragraph direction is left to right.
422 if(visualIndex == endCharacter)
424 // It places the cursor just before the last character in visual order.
425 // i.e. it places the cursor just before the '\n' or before the last character
426 // if there is a long line with no word breaks which is wrapped.
428 // It doesn't check if the closest line is the last one like the RTL branch below
429 // because the total number of characters is different than the visual index and
430 // the visual index is the last character of the line.
436 // The paragraph direction is right to left.
438 if((lineIndex != totalNumberOfLines - 1u) && // is not the last line.
439 (visualIndex == startCharacter))
441 // It places the cursor just after the first character in visual order.
442 // i.e. it places the cursor just after the '\n' or after the last character
443 // if there is a long line with no word breaks which is wrapped.
445 // If the last line doesn't end with '\n' it won't increase the visual index
446 // placing the cursor at the beginning of the line (in visual order).
453 // The visual index is at the end of text.
455 // 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.
456 // This branch checks if the closest line is the one with the last '\n'. If it is, it decrements the visual index to place
457 // the cursor just before the last '\n'.
459 if((lineIndex != totalNumberOfLines - 1u) &&
460 TextAbstraction::IsNewParagraph(*(logicalModel->mText.Begin() + visualIndex - 1u)))
466 logicalIndex = (bidiLineFetched ? logicalModel->GetLogicalCursorIndex(visualIndex) : visualIndex);
468 // Handle Emoji clustering for cursor handling:
470 // - When there is Emoji contains multi unicodes and it is layoutted at the end of line (LineWrap case , is not new line case)
471 // - Try to click at the center or at the end of Emoji then the cursor appears inside Emoji
472 // - Example:"FamilyManWomanGirlBoy 👨‍👩‍👧‍👦"
473 const Script script = logicalModel->GetScript(logicalIndex);
474 if(IsOneOfEmojiScripts(script))
476 //TODO: Use this clustering for Emoji cases only. This needs more testing to generalize to all scripts.
477 CharacterRun emojiClusteredCharacters = RetrieveClusteredCharactersOfCharacterIndex(visualModel, logicalModel, logicalIndex);
478 logicalIndex = emojiClusteredCharacters.characterIndex;
481 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex);
483 DALI_ASSERT_DEBUG((logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0) && "GetClosestCursorIndex - Out of bounds index");
488 void GetCursorPosition(GetCursorPositionParameters& parameters,
489 float defaultFontLineHeight,
490 CursorInfo& cursorInfo)
492 const LineRun* const modelLines = parameters.visualModel->mLines.Begin();
493 if(NULL == modelLines)
499 // Whether the logical cursor position is at the end of the whole text.
500 const bool isLastPosition = parameters.logicalModel->mText.Count() == parameters.logical;
502 // Get the line where the character is laid-out.
503 const CharacterIndex characterOfLine = isLastPosition ? (parameters.logical - 1u) : parameters.logical;
505 // Whether the cursor is in the last position and the last position is a new paragraph character.
506 const bool isLastNewParagraph = parameters.isMultiline && isLastPosition && TextAbstraction::IsNewParagraph(*(parameters.logicalModel->mText.Begin() + characterOfLine));
508 const LineIndex lineIndex = parameters.visualModel->GetLineOfCharacter(characterOfLine);
509 const LineRun& line = *(modelLines + lineIndex);
511 CharacterIndex index;
512 GlyphMetrics glyphMetrics;
513 MetricsPtr& metrics = parameters.metrics;
514 GlyphIndex glyphIndex = 0u;
515 Length numberOfGlyphs = 0u;
517 if(isLastNewParagraph)
519 // The cursor is in a new line with no characters. Place the cursor in that line.
520 const LineIndex newLineIndex = lineIndex + 1u;
521 const LineRun& newLine = *(modelLines + newLineIndex);
523 cursorInfo.isSecondaryCursor = false;
525 // Set the line offset and height.
526 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
529 // The line height is the addition of the line ascender and the line descender.
530 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in cursor height.
531 cursorInfo.lineHeight = newLine.ascender - newLine.descender;
534 const Length totalNumberOfCharacters = parameters.logicalModel->mText.Count();
535 if(totalNumberOfCharacters > 0u)
537 index = totalNumberOfCharacters - 1u;
540 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
542 // Set the primary cursor's height.
543 // 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.
544 cursorInfo.primaryCursorHeight = (totalNumberOfCharacters > 0) ? (cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight) : defaultFontLineHeight;
546 // Set the primary cursor's position.
547 cursorInfo.primaryPosition.x = (LTR == line.direction) ? newLine.alignmentOffset : parameters.visualModel->mControlSize.width - newLine.alignmentOffset;
548 cursorInfo.primaryPosition.y = cursorInfo.lineOffset;
552 // Whether this line is a bidirectional line.
553 const bool bidiLineFetched = parameters.logicalModel->FetchBidirectionalLineInfo(characterOfLine);
555 // Check if the logical position is the first or the last one of the line.
556 const bool isFirstPositionOfLine = line.characterRun.characterIndex == parameters.logical;
557 const bool isLastPositionOfLine = line.characterRun.characterIndex + line.characterRun.numberOfCharacters == parameters.logical;
559 // 'logical' is the logical 'cursor' index.
560 // Get the next and current logical 'character' index.
561 const CharacterIndex characterIndex = isFirstPositionOfLine ? parameters.logical : parameters.logical - 1u;
562 const CharacterIndex nextCharacterIndex = isLastPositionOfLine ? characterIndex : parameters.logical;
564 // The character's direction buffer.
565 const CharacterDirection* const directionsBuffer = bidiLineFetched ? parameters.logicalModel->mCharacterDirections.Begin() : NULL;
567 CharacterDirection isCurrentRightToLeft = false;
568 CharacterDirection isNextRightToLeft = false;
569 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
571 isCurrentRightToLeft = *(directionsBuffer + characterIndex);
572 isNextRightToLeft = *(directionsBuffer + nextCharacterIndex);
575 // Get the paragraph's direction.
576 const CharacterDirection isRightToLeftParagraph = line.direction;
578 // Check whether there is an alternative position:
579 cursorInfo.isSecondaryCursor = ((!isLastPositionOfLine && (isCurrentRightToLeft != isNextRightToLeft)) ||
580 (isLastPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)) ||
581 (isFirstPositionOfLine && (isRightToLeftParagraph != isCurrentRightToLeft)));
583 // Set the line offset and height.
584 cursorInfo.lineOffset = CalculateLineOffset(parameters.visualModel->mLines,
587 // The line height is the addition of the line ascender and the line descender.
588 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in cursor height.
589 cursorInfo.lineHeight = line.ascender - line.descender;
591 // Calculate the primary cursor.
593 index = characterIndex;
594 if(cursorInfo.isSecondaryCursor)
596 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
598 if(isLastPositionOfLine)
600 // The position of the cursor after the last character needs special
601 // care depending on its direction and the direction of the paragraph.
603 // Need to find the first character after the last character with the paragraph's direction.
604 // i.e l0 l1 l2 r0 r1 should find r0.
606 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
609 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
612 else if(isFirstPositionOfLine)
614 index = isRightToLeftParagraph ? line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u : line.characterRun.characterIndex;
617 index = parameters.logicalModel->GetLogicalCharacterIndex(index);
622 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? characterIndex : nextCharacterIndex;
626 const Length* const charactersPerGlyphBuffer = parameters.visualModel->mCharactersPerGlyph.Begin();
627 const CharacterIndex* const glyphsToCharactersBuffer = parameters.visualModel->mGlyphsToCharacters.Begin();
628 const Vector2* const glyphPositionsBuffer = parameters.visualModel->mGlyphPositions.Begin();
629 const float modelCharacterSpacing = parameters.visualModel->GetCharacterSpacing();
631 const Vector<CharacterSpacingGlyphRun>& characterSpacingGlyphRuns = parameters.visualModel->GetCharacterSpacingGlyphRuns();
633 // Get the metrics for the group of glyphs.
634 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
636 // Convert the cursor position into the glyph position.
637 const GlyphIndex primaryGlyphIndex = glyphIndex;
638 const Length primaryNumberOfCharacters = *(charactersPerGlyphBuffer + primaryGlyphIndex);
640 // Whether to add the glyph's advance to the cursor position.
641 // 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,
642 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
643 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
664 // Where F -> isFirstPosition
665 // L -> isLastPosition
666 // C -> isCurrentRightToLeft
667 // P -> isRightToLeftParagraph
668 // A -> Whether to add the glyph's advance.
670 const bool addGlyphAdvance = ((isLastPositionOfLine && !isRightToLeftParagraph) ||
671 (isFirstPositionOfLine && isRightToLeftParagraph) ||
672 (!isFirstPositionOfLine && !isLastPosition && !isCurrentRightToLeft));
674 float glyphAdvance = addGlyphAdvance ? (glyphMetrics.advance) : 0.f;
676 if(!isLastPositionOfLine &&
677 (primaryNumberOfCharacters > 1u))
679 const CharacterIndex firstIndex = *(glyphsToCharactersBuffer + primaryGlyphIndex);
681 bool isCurrentRightToLeft = false;
682 if(bidiLineFetched) // If bidiLineFetched is false, it means the whole text is left to right.
684 isCurrentRightToLeft = *(directionsBuffer + index);
687 Length numberOfGlyphAdvance = (isFirstPositionOfLine ? 0 : 1u) + characterIndex - firstIndex;
688 if(isCurrentRightToLeft)
690 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
693 glyphAdvance = static_cast<float>(numberOfGlyphAdvance) * (glyphMetrics.advance) / static_cast<float>(primaryNumberOfCharacters);
696 // Get the glyph position and x bearing (in the line's coords).
697 const Vector2& primaryPosition = *(glyphPositionsBuffer + primaryGlyphIndex);
699 // Set the primary cursor's height.
700 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
702 cursorInfo.glyphOffset = line.ascender - glyphMetrics.ascender;
703 // Set the primary cursor's position.
704 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
705 cursorInfo.primaryPosition.y = cursorInfo.lineOffset + cursorInfo.glyphOffset;
707 // Transform the cursor info from line's coords to text's coords.
708 cursorInfo.primaryPosition.x += line.alignmentOffset;
710 // Calculate the secondary cursor.
711 if(cursorInfo.isSecondaryCursor)
713 // Set the secondary cursor's height.
714 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
716 CharacterIndex index = characterIndex;
717 if(!isLastPositionOfLine)
719 index = (isRightToLeftParagraph == isCurrentRightToLeft) ? nextCharacterIndex : characterIndex;
722 GetGlyphMetricsFromCharacterIndex(index, parameters.visualModel, parameters.logicalModel, metrics, glyphMetrics, glyphIndex, numberOfGlyphs);
724 const GlyphIndex secondaryGlyphIndex = glyphIndex;
725 const Vector2& secondaryPosition = *(glyphPositionsBuffer + secondaryGlyphIndex);
727 // Set the secondary cursor's position.
740 // Where F -> isFirstPosition
741 // C -> isCurrentRightToLeft
742 // P -> isRightToLeftParagraph
743 // A -> Whether to add the glyph's advance.
745 const bool addGlyphAdvance = ((!isFirstPositionOfLine && !isCurrentRightToLeft) ||
746 (isFirstPositionOfLine && !isRightToLeftParagraph));
748 const float characterSpacing = GetGlyphCharacterSpacing(secondaryGlyphIndex, characterSpacingGlyphRuns, modelCharacterSpacing);
749 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + (addGlyphAdvance ? (glyphMetrics.advance + characterSpacing) : 0.f);
750 cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight;
752 // Transform the cursor info from line's coords to text's coords.
753 cursorInfo.secondaryPosition.x += line.alignmentOffset;
758 bool FindSelectionIndices(VisualModelPtr visualModel,
759 LogicalModelPtr logicalModel,
763 CharacterIndex& startIndex,
764 CharacterIndex& endIndex,
765 CharacterIndex& noTextHitIndex)
769 |-------------------------------------------------------|------------------------------------------|
770 | On a word | The word |
771 | On a single white space between words | The word before or after the white space |
772 | On one of the multiple contiguous white spaces | The white spaces |
773 | On a single white space which is in the position zero | The white space and the next word |
774 | On a new paragraph character | The word or group of white spaces before |
775 |-------------------------------------------------------|------------------------------------------|
777 const Length totalNumberOfCharacters = logicalModel->mText.Count();
782 if(0 == totalNumberOfCharacters)
784 // Nothing to do if the model is empty.
788 bool matchedCharacter = false;
789 CharacterIndex hitCharacter = Text::GetClosestCursorIndex(visualModel,
794 CharacterHitTest::TAP,
797 if(!matchedCharacter)
799 noTextHitIndex = hitCharacter;
802 DALI_ASSERT_DEBUG((hitCharacter <= totalNumberOfCharacters) && "GetClosestCursorIndex returned out of bounds index");
804 if(hitCharacter >= totalNumberOfCharacters)
806 // Closest hit character is the last character.
807 if(hitCharacter == totalNumberOfCharacters)
809 hitCharacter--; //Hit character index set to last character in logical model
813 // hitCharacter is out of bounds
818 const Character* const textBuffer = logicalModel->mText.Begin();
820 startIndex = hitCharacter;
821 endIndex = hitCharacter;
823 // Whether the hit character is a new paragraph character.
824 const bool isHitCharacterNewParagraph = TextAbstraction::IsNewParagraph(*(textBuffer + hitCharacter));
826 // Whether the hit character is a white space. Note a new paragraph character is a white space as well but here is not wanted.
827 const bool isHitCharacterWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + hitCharacter)) && !isHitCharacterNewParagraph;
829 FindWordData data(textBuffer,
830 totalNumberOfCharacters,
832 isHitCharacterWhiteSpace,
833 isHitCharacterNewParagraph);
835 if(isHitCharacterNewParagraph)
837 // Find the first character before the hit one which is not a new paragraph character.
841 endIndex = hitCharacter - 1u;
842 for(; endIndex > 0; --endIndex)
844 const Dali::Toolkit::Text::Character character = *(data.textBuffer + endIndex);
846 if(!Dali::TextAbstraction::IsNewParagraph(character))
853 data.hitCharacter = endIndex;
854 data.isNewParagraph = false;
855 data.isWhiteSpace = TextAbstraction::IsWhiteSpace(*(textBuffer + data.hitCharacter));
858 // Find the start of the word.
859 FindStartOfWord(data);
860 startIndex = data.foundIndex;
862 // Find the end of the word.
864 endIndex = data.foundIndex;
866 if(1u == (endIndex - startIndex))
868 if(isHitCharacterWhiteSpace)
870 // Select the word before or after the white space
872 if(0 == hitCharacter)
874 data.isWhiteSpace = false;
876 endIndex = data.foundIndex;
878 else if(hitCharacter > 0)
880 // Find the start of the word.
881 data.hitCharacter = hitCharacter - 1u;
882 data.isWhiteSpace = false;
883 FindStartOfWord(data);
884 startIndex = data.foundIndex;
891 return matchedCharacter;
896 } // namespace Toolkit