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/text-selection-handle-controller.h>
21 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
26 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
27 #include <dali-toolkit/internal/text/text-controller-impl-event-handler.h>
34 * @brief Struct used to calculate the selection box.
36 struct SelectionBoxInfo
44 #if defined(DEBUG_ENABLED)
45 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
48 const float MAX_FLOAT = std::numeric_limits<float>::max();
49 const float MIN_FLOAT = std::numeric_limits<float>::min();
50 const Dali::Toolkit::Text::CharacterDirection LTR = false; ///< Left To Right direction
60 void SelectionHandleController::Reposition(Controller::Impl& impl)
62 EventData*& eventData = impl.mEventData;
64 CharacterIndex selectionStart = eventData->mLeftSelectionPosition;
65 CharacterIndex selectionEnd = eventData->mRightSelectionPosition;
67 DecoratorPtr& decorator = eventData->mDecorator;
69 if(selectionStart == selectionEnd)
71 // Nothing to select if handles are in the same place.
72 // So, deactive Highlight box.
73 decorator->SetHighlightActive(false);
77 decorator->ClearHighlights();
79 ModelPtr& model = impl.mModel;
80 VisualModelPtr& visualModel = model->mVisualModel;
81 LogicalModelPtr& logicalModel = model->mLogicalModel;
83 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
84 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
85 const GlyphInfo* const glyphsBuffer = visualModel->mGlyphs.Begin();
86 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
87 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
88 const CharacterIndex* const glyphToCharacterBuffer = visualModel->mGlyphsToCharacters.Begin();
89 const CharacterDirection* const modelCharacterDirectionsBuffer = (0u != logicalModel->mCharacterDirections.Count()) ? logicalModel->mCharacterDirections.Begin() : NULL;
91 const bool isLastCharacter = selectionEnd >= logicalModel->mText.Count();
92 const CharacterDirection startDirection = ((NULL == modelCharacterDirectionsBuffer) ? false : *(modelCharacterDirectionsBuffer + selectionStart));
93 const CharacterDirection endDirection = ((NULL == modelCharacterDirectionsBuffer) ? false : *(modelCharacterDirectionsBuffer + (selectionEnd - (isLastCharacter ? 1u : 0u))));
95 // Swap the indices if the start is greater than the end.
96 const bool indicesSwapped = selectionStart > selectionEnd;
98 // Tell the decorator to flip the selection handles if needed.
99 decorator->SetSelectionHandleFlipState(indicesSwapped, startDirection, endDirection);
103 std::swap(selectionStart, selectionEnd);
106 // Get the indices to the first and last selected glyphs.
107 const CharacterIndex selectionEndMinusOne = selectionEnd - 1u;
108 const GlyphIndex glyphStart = *(charactersToGlyphBuffer + selectionStart);
109 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + selectionEndMinusOne);
110 const GlyphIndex glyphEnd = *(charactersToGlyphBuffer + selectionEndMinusOne) + ((numberOfGlyphs > 0) ? numberOfGlyphs - 1u : 0u);
111 const float characterSpacing = visualModel->GetCharacterSpacing();
112 Vector<CharacterIndex>& glyphToCharacterMap = visualModel->mGlyphsToCharacters;
113 const CharacterIndex* glyphToCharacterMapBuffer = glyphToCharacterMap.Begin();
115 // Get the lines where the glyphs are laid-out.
116 const LineRun* lineRun = visualModel->mLines.Begin();
118 LineIndex lineIndex = 0u;
119 Length numberOfLines = 0u;
120 visualModel->GetNumberOfLines(glyphStart,
121 1u + glyphEnd - glyphStart,
124 const LineIndex firstLineIndex = lineIndex;
126 // Create the structure to store some selection box info.
127 Vector<SelectionBoxInfo> selectionBoxLinesInfo;
128 selectionBoxLinesInfo.Resize(numberOfLines);
130 SelectionBoxInfo* selectionBoxInfo = selectionBoxLinesInfo.Begin();
131 selectionBoxInfo->minX = MAX_FLOAT;
132 selectionBoxInfo->maxX = MIN_FLOAT;
134 // Keep the min and max 'x' position to calculate the size and position of the highlighed text.
135 float minHighlightX = std::numeric_limits<float>::max();
136 float maxHighlightX = std::numeric_limits<float>::min();
138 Vector2 highLightPosition; // The highlight position in decorator's coords.
140 // Retrieve the first line and get the line's vertical offset, the line's height and the index to the last glyph.
142 // The line's vertical offset of all the lines before the line where the first glyph is laid-out.
143 selectionBoxInfo->lineOffset = CalculateLineOffset(visualModel->mLines,
146 // Transform to decorator's (control) coords.
147 selectionBoxInfo->lineOffset += model->mScrollPosition.y;
149 lineRun += firstLineIndex;
151 // The line height is the addition of the line ascender and the line descender.
152 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in selection height.
153 selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender;
155 GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
157 // Check if the first glyph is a ligature that must be broken like Latin ff, fi, or Arabic ﻻ, etc which needs special code.
158 const Length numberOfCharactersStart = *(charactersPerGlyphBuffer + glyphStart);
159 bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(selectionStart));
161 // Check if the last glyph is a ligature that must be broken like Latin ff, fi, or Arabic ﻻ, etc which needs special code.
162 const Length numberOfCharactersEnd = *(charactersPerGlyphBuffer + glyphEnd);
163 bool splitEndGlyph = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(selectionEndMinusOne));
165 // The number of quads of the selection box.
166 const unsigned int numberOfQuads = 1u + (glyphEnd - glyphStart) + ((numberOfLines > 1u) ? 2u * numberOfLines : 0u);
167 decorator->ResizeHighlightQuads(numberOfQuads);
169 // Count the actual number of quads.
170 unsigned int actualNumberOfQuads = 0u;
172 float calculatedAdvance = 0.f;
174 // Traverse the glyphs.
175 for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
177 const GlyphInfo& glyph = *(glyphsBuffer + index);
178 const Vector2& position = *(positionsBuffer + index);
179 calculatedAdvance = GetCalculatedAdvance(*(logicalModel->mText.Begin() + (*(glyphToCharacterMapBuffer + index))), characterSpacing, glyph.advance);
183 // If the first glyph is a ligature that must be broken it may be needed to add only part of the glyph to the highlight box.
185 const float glyphAdvance = calculatedAdvance / static_cast<float>(numberOfCharactersStart);
186 const CharacterIndex interGlyphIndex = selectionStart - *(glyphToCharacterBuffer + glyphStart);
187 // Get the direction of the character.
188 CharacterDirection isCurrentRightToLeft = false;
189 if(nullptr != modelCharacterDirectionsBuffer) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
191 isCurrentRightToLeft = *(modelCharacterDirectionsBuffer + selectionStart);
194 // The end point could be in the middle of the ligature.
195 // Calculate the number of characters selected.
196 const Length numberOfCharacters = (glyphStart == glyphEnd) ? (selectionEnd - selectionStart) : (numberOfCharactersStart - interGlyphIndex);
198 quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + model->mScrollPosition.x + glyphAdvance * static_cast<float>(isCurrentRightToLeft ? (numberOfCharactersStart - interGlyphIndex - numberOfCharacters) : interGlyphIndex);
199 quad.y = selectionBoxInfo->lineOffset;
200 quad.z = quad.x + static_cast<float>(numberOfCharacters) * glyphAdvance;
201 quad.w = selectionBoxInfo->lineOffset + selectionBoxInfo->lineHeight;
203 // Store the min and max 'x' for each line.
204 selectionBoxInfo->minX = std::min(selectionBoxInfo->minX, quad.x);
205 selectionBoxInfo->maxX = std::max(selectionBoxInfo->maxX, quad.z);
207 decorator->AddHighlight(actualNumberOfQuads, quad);
208 ++actualNumberOfQuads;
210 splitStartGlyph = false;
214 if(splitEndGlyph && (index == glyphEnd))
216 // Equally, if the last glyph is a ligature that must be broken it may be needed to add only part of the glyph to the highlight box.
218 const float glyphAdvance = calculatedAdvance / static_cast<float>(numberOfCharactersEnd);
219 const CharacterIndex interGlyphIndex = selectionEnd - *(glyphToCharacterBuffer + glyphEnd);
220 // Get the direction of the character.
221 CharacterDirection isCurrentRightToLeft = false;
222 if(nullptr != modelCharacterDirectionsBuffer) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
224 isCurrentRightToLeft = *(modelCharacterDirectionsBuffer + selectionEnd);
227 const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex;
229 quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + model->mScrollPosition.x + (isCurrentRightToLeft ? (glyphAdvance * static_cast<float>(numberOfCharacters)) : 0.f);
230 quad.y = selectionBoxInfo->lineOffset;
231 quad.z = quad.x + static_cast<float>(interGlyphIndex) * glyphAdvance;
232 quad.w = quad.y + selectionBoxInfo->lineHeight;
234 // Store the min and max 'x' for each line.
235 selectionBoxInfo->minX = std::min(selectionBoxInfo->minX, quad.x);
236 selectionBoxInfo->maxX = std::max(selectionBoxInfo->maxX, quad.z);
238 decorator->AddHighlight(actualNumberOfQuads,
240 ++actualNumberOfQuads;
242 splitEndGlyph = false;
246 quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + model->mScrollPosition.x;
247 quad.y = selectionBoxInfo->lineOffset;
248 quad.z = quad.x + calculatedAdvance;
249 quad.w = quad.y + selectionBoxInfo->lineHeight;
251 // Store the min and max 'x' for each line.
252 selectionBoxInfo->minX = std::min(selectionBoxInfo->minX, quad.x);
253 selectionBoxInfo->maxX = std::max(selectionBoxInfo->maxX, quad.z);
255 decorator->AddHighlight(actualNumberOfQuads,
257 ++actualNumberOfQuads;
259 // Whether to retrieve the next line.
260 if(index == lastGlyphOfLine)
263 if(lineIndex < firstLineIndex + numberOfLines)
265 float currentLineSpacing = lineRun->lineSpacing;
267 // Retrieve the next line.
270 // Get the last glyph of the new line.
271 lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
273 // Keep the offset and height of the current selection box.
274 const float currentLineOffset = selectionBoxInfo->lineOffset;
275 const float currentLineHeight = selectionBoxInfo->lineHeight;
277 // Get the selection box info for the next line.
280 selectionBoxInfo->minX = MAX_FLOAT;
281 selectionBoxInfo->maxX = MIN_FLOAT;
283 // Update the line's vertical offset.
284 selectionBoxInfo->lineOffset = currentLineOffset + currentLineHeight;
286 if(currentLineSpacing < 0)
288 selectionBoxInfo->lineOffset += currentLineSpacing;
291 // The line height is the addition of the line ascender and the line descender.
292 // However, the line descender has a negative value, hence the subtraction also line spacing should not be included in selection height.
293 selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender;
295 if(lineRun->lineSpacing > 0)
297 selectionBoxInfo->lineHeight += lineRun->lineSpacing;
303 // Traverses all the lines and updates the min and max 'x' positions and the total height.
304 // The final width is calculated after 'boxifying' the selection.
305 for(Vector<SelectionBoxInfo>::ConstIterator it = selectionBoxLinesInfo.Begin(),
306 endIt = selectionBoxLinesInfo.End();
310 const SelectionBoxInfo& info = *it;
312 // Update the size of the highlighted text.
313 highLightSize.height += info.lineHeight;
314 minHighlightX = std::min(minHighlightX, info.minX);
315 maxHighlightX = std::max(maxHighlightX, info.maxX);
318 // Add extra geometry to 'boxify' the selection.
320 if(1u < numberOfLines)
322 // Boxify the first line.
323 lineRun = visualModel->mLines.Begin() + firstLineIndex;
324 const SelectionBoxInfo& firstSelectionBoxLineInfo = *(selectionBoxLinesInfo.Begin());
326 bool boxifyBegin = (LTR != lineRun->direction) && (LTR != startDirection);
327 bool boxifyEnd = (LTR == lineRun->direction) && (LTR == startDirection);
332 quad.y = firstSelectionBoxLineInfo.lineOffset;
333 quad.z = firstSelectionBoxLineInfo.minX;
334 quad.w = firstSelectionBoxLineInfo.lineOffset + firstSelectionBoxLineInfo.lineHeight;
336 // Boxify at the beginning of the line.
337 decorator->AddHighlight(actualNumberOfQuads,
339 ++actualNumberOfQuads;
341 // Update the size of the highlighted text.
347 quad.x = firstSelectionBoxLineInfo.maxX;
348 quad.y = firstSelectionBoxLineInfo.lineOffset;
349 quad.z = visualModel->mControlSize.width;
350 quad.w = firstSelectionBoxLineInfo.lineOffset + firstSelectionBoxLineInfo.lineHeight;
352 // Boxify at the end of the line.
353 decorator->AddHighlight(actualNumberOfQuads,
355 ++actualNumberOfQuads;
357 // Update the size of the highlighted text.
358 maxHighlightX = visualModel->mControlSize.width;
361 // Boxify the central lines.
362 if(2u < numberOfLines)
364 for(Vector<SelectionBoxInfo>::ConstIterator it = selectionBoxLinesInfo.Begin() + 1u,
365 endIt = selectionBoxLinesInfo.End() - 1u;
369 const SelectionBoxInfo& info = *it;
372 quad.y = info.lineOffset;
374 quad.w = info.lineOffset + info.lineHeight;
376 decorator->AddHighlight(actualNumberOfQuads,
378 ++actualNumberOfQuads;
381 quad.y = info.lineOffset;
382 quad.z = visualModel->mControlSize.width;
383 quad.w = info.lineOffset + info.lineHeight;
385 decorator->AddHighlight(actualNumberOfQuads,
387 ++actualNumberOfQuads;
390 // Update the size of the highlighted text.
392 maxHighlightX = visualModel->mControlSize.width;
395 // Boxify the last line.
396 lineRun = visualModel->mLines.Begin() + firstLineIndex + numberOfLines - 1u;
397 const SelectionBoxInfo& lastSelectionBoxLineInfo = *(selectionBoxLinesInfo.End() - 1u);
399 boxifyBegin = (LTR == lineRun->direction) && (LTR == endDirection);
400 boxifyEnd = (LTR != lineRun->direction) && (LTR != endDirection);
405 quad.y = lastSelectionBoxLineInfo.lineOffset;
406 quad.z = lastSelectionBoxLineInfo.minX;
407 quad.w = lastSelectionBoxLineInfo.lineOffset + lastSelectionBoxLineInfo.lineHeight;
409 // Boxify at the beginning of the line.
410 decorator->AddHighlight(actualNumberOfQuads,
412 ++actualNumberOfQuads;
414 // Update the size of the highlighted text.
420 quad.x = lastSelectionBoxLineInfo.maxX;
421 quad.y = lastSelectionBoxLineInfo.lineOffset;
422 quad.z = visualModel->mControlSize.width;
423 quad.w = lastSelectionBoxLineInfo.lineOffset + lastSelectionBoxLineInfo.lineHeight;
425 // Boxify at the end of the line.
426 decorator->AddHighlight(actualNumberOfQuads, quad);
427 ++actualNumberOfQuads;
429 // Update the size of the highlighted text.
430 maxHighlightX = visualModel->mControlSize.width;
434 // Set the actual number of quads.
435 decorator->ResizeHighlightQuads(actualNumberOfQuads);
437 // Sets the highlight's size and position. In decorator's coords.
438 // The highlight's height has been calculated above (before 'boxifying' the highlight).
439 highLightSize.width = maxHighlightX - minHighlightX;
441 highLightPosition.x = minHighlightX;
442 const SelectionBoxInfo& firstSelectionBoxLineInfo = *(selectionBoxLinesInfo.Begin());
443 highLightPosition.y = firstSelectionBoxLineInfo.lineOffset;
445 decorator->SetHighLightBox(highLightPosition, highLightSize, static_cast<float>(model->GetOutlineWidth()));
447 if(!decorator->IsSmoothHandlePanEnabled())
449 CursorInfo primaryCursorInfo;
450 impl.GetCursorPosition(eventData->mLeftSelectionPosition, primaryCursorInfo);
452 const Vector2 primaryPosition = primaryCursorInfo.primaryPosition + model->mScrollPosition;
454 decorator->SetPosition(LEFT_SELECTION_HANDLE,
456 primaryCursorInfo.lineOffset + model->mScrollPosition.y,
457 primaryCursorInfo.lineHeight);
459 CursorInfo secondaryCursorInfo;
460 impl.GetCursorPosition(eventData->mRightSelectionPosition, secondaryCursorInfo);
462 const Vector2 secondaryPosition = secondaryCursorInfo.primaryPosition + model->mScrollPosition;
464 decorator->SetPosition(RIGHT_SELECTION_HANDLE,
466 secondaryCursorInfo.lineOffset + model->mScrollPosition.y,
467 secondaryCursorInfo.lineHeight);
470 // Set the flag to update the decorator.
471 eventData->mDecoratorUpdated = true;
474 void SelectionHandleController::Reposition(Controller::Impl& impl, float visualX, float visualY, Controller::NoTextTap::Action action)
476 EventData*& eventData = impl.mEventData;
477 if(nullptr == eventData)
479 // Nothing to do if there is no text input.
483 if(impl.IsShowingPlaceholderText())
485 // Nothing to do if there is the place-holder text.
489 ModelPtr& model = impl.mModel;
490 VisualModelPtr& visualModel = model->mVisualModel;
491 const Length numberOfGlyphs = visualModel->mGlyphs.Count();
492 const Length numberOfLines = visualModel->mLines.Count();
493 if((0 == numberOfGlyphs) ||
494 (0 == numberOfLines))
496 // Nothing to do if there is no text.
500 // Find which word was selected
501 CharacterIndex selectionStart(0);
502 CharacterIndex selectionEnd(0);
503 CharacterIndex noTextHitIndex(0);
504 const bool characterHit = FindSelectionIndices(visualModel,
505 model->mLogicalModel,
512 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "%p selectionStart %d selectionEnd %d\n", &impl, selectionStart, selectionEnd);
514 if(characterHit || (Controller::NoTextTap::HIGHLIGHT == action))
516 uint32_t oldStart = eventData->mLeftSelectionPosition;
517 uint32_t oldEnd = eventData->mRightSelectionPosition;
519 impl.ChangeState(EventData::SELECTING);
521 eventData->mLeftSelectionPosition = selectionStart;
522 eventData->mRightSelectionPosition = selectionEnd;
524 eventData->mUpdateLeftSelectionPosition = true;
525 eventData->mUpdateRightSelectionPosition = true;
526 eventData->mUpdateHighlightBox = true;
528 // It may happen an InputMethodContext commit event arrives before the selection event
529 // if the InputMethodContext is in pre-edit state. The commit event will set the
530 // eventData->mUpdateCursorPosition flag to true. If it's not set back
531 // to false, the highlight box won't be updated.
532 eventData->mUpdateCursorPosition = false;
534 eventData->mScrollAfterUpdatePosition = (eventData->mLeftSelectionPosition != eventData->mRightSelectionPosition);
536 // Cursor to be positioned at end of selection so if selection interrupted and edit mode restarted the cursor will be at end of selection
537 eventData->mPrimaryCursorPosition = std::max(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition);
539 if(impl.mSelectableControlInterface != nullptr)
541 impl.mSelectableControlInterface->SelectionChanged(oldStart, oldEnd, eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition);
544 else if(Controller::NoTextTap::SHOW_SELECTION_POPUP == action)
546 // Nothing to select. i.e. a white space, out of bounds
547 impl.ChangeState(EventData::EDITING_WITH_POPUP);
549 eventData->mPrimaryCursorPosition = noTextHitIndex;
551 eventData->mUpdateCursorPosition = true;
552 eventData->mUpdateGrabHandlePosition = true;
553 eventData->mScrollAfterUpdatePosition = true;
554 eventData->mUpdateInputStyle = true;
556 else if(Controller::NoTextTap::NO_ACTION == action)
558 // Nothing to select. i.e. a white space, out of bounds
559 eventData->mPrimaryCursorPosition = noTextHitIndex;
561 eventData->mUpdateCursorPosition = true;
562 eventData->mUpdateGrabHandlePosition = true;
563 eventData->mScrollAfterUpdatePosition = true;
564 eventData->mUpdateInputStyle = true;
568 void SelectionHandleController::Update(Controller::Impl& impl, HandleType handleType, const CursorInfo& cursorInfo)
570 if((LEFT_SELECTION_HANDLE != handleType) &&
571 (RIGHT_SELECTION_HANDLE != handleType))
576 ModelPtr& model = impl.mModel;
577 const Vector2 cursorPosition = cursorInfo.primaryPosition + model->mScrollPosition;
579 // Sets the handle's position.
580 EventData*& eventData = impl.mEventData;
581 eventData->mDecorator->SetPosition(handleType,
583 cursorInfo.lineOffset + model->mScrollPosition.y,
584 cursorInfo.lineHeight);
586 // If selection handle at start of the text and other at end of the text then all text is selected.
587 const CharacterIndex startOfSelection = std::min(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition);
588 const CharacterIndex endOfSelection = std::max(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition);
589 eventData->mAllTextSelected = (startOfSelection == 0) && (endOfSelection == model->mLogicalModel->mText.Count());
594 } // namespace Toolkit