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-geometry.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
26 #include <dali-toolkit/internal/text/line-run.h>
27 #include <dali-toolkit/internal/text/visual-model-impl.h>
38 bool GetNextLine(GlyphIndex index, LineIndex& lineIndex, LineRun*& lineRun, GlyphIndex& lastGlyphOfLine, Length numberOfLines, bool& isLastLine)
40 if(index == lastGlyphOfLine)
43 if(lineIndex < numberOfLines)
45 isLastLine = (lineIndex + 1 == numberOfLines);
54 void UpdateLineInfo(const LineRun* lineRun, float& currentLineOffset, float& currentLineHeight, GlyphIndex& lastGlyphOfLine, bool isLastLine)
56 lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
57 currentLineOffset = currentLineOffset + currentLineHeight;
58 currentLineHeight = GetLineHeight(*lineRun, isLastLine);
61 void GetTextGeometry(ModelPtr textModel, CharacterIndex startIndex, CharacterIndex endIndex, Vector<Vector2>& sizesList, Vector<Vector2>& positionsList)
63 VisualModelPtr& visualModel = textModel->mVisualModel;
64 LogicalModelPtr& logicalModel = textModel->mLogicalModel;
66 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
67 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
68 const GlyphInfo* const glyphsBuffer = visualModel->mGlyphs.Begin();
69 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
70 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
71 const CharacterIndex* const glyphToCharacterBuffer = visualModel->mGlyphsToCharacters.Begin();
72 const CharacterDirection* const modelCharacterDirectionsBuffer = (0u != logicalModel->mCharacterDirections.Count()) ? logicalModel->mCharacterDirections.Begin() : NULL;
76 positionsList.Clear();
78 if(charactersToGlyphBuffer == nullptr || glyphsPerCharacterBuffer == nullptr || charactersPerGlyphBuffer == nullptr || glyphToCharacterBuffer == nullptr )
83 if(startIndex >= logicalModel->mText.Count() && endIndex >= logicalModel->mText.Count())
88 if(startIndex >= logicalModel->mText.Count())
90 startIndex = static_cast<CharacterIndex>(logicalModel->mText.Count() - 1u);
93 if(endIndex >= logicalModel->mText.Count())
95 endIndex = static_cast<CharacterIndex>(logicalModel->mText.Count() - 1u);
98 if(startIndex > endIndex)
100 std::swap(startIndex, endIndex);
103 LineRun* lineRun = visualModel->mLines.Begin();
104 GlyphIndex glyphStart = *(charactersToGlyphBuffer + startIndex);
106 //if glyph not in the first line (in some ellipsis cases)
107 if(glyphStart < lineRun->glyphRun.glyphIndex)
109 glyphStart = lineRun->glyphRun.glyphIndex;
110 startIndex = *(glyphToCharacterBuffer + glyphStart);
112 if(startIndex > endIndex)
114 std::swap(startIndex, endIndex);
118 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + endIndex);
119 GlyphIndex glyphEnd = *(charactersToGlyphBuffer + endIndex) + ((numberOfGlyphs > 0) ? numberOfGlyphs - 1u : 0u);
120 LineIndex lineIndex = visualModel->GetLineOfCharacter(startIndex);
121 Length numberOfLines = visualModel->GetTotalNumberOfLines();
122 bool isLastLine = lineIndex + 1 == numberOfLines;
124 LineIndex firstLineIndex = lineIndex;
126 Vector2 textInLinePosition;
128 lineRun += firstLineIndex;
130 //get the first line and its vertical offset
131 float currentLineOffset = CalculateLineOffset(visualModel->mLines, firstLineIndex);
132 float currentLineHeight = GetLineHeight(*lineRun, isLastLine);
133 GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1;
135 // Check if the first/last glyph is a ligature that needs be splitted like English fi or Arabic ﻻ.
136 const Length numberOfCharactersStart = *(charactersPerGlyphBuffer + glyphStart);
137 const Length numberOfCharactersEnd = *(charactersPerGlyphBuffer + glyphEnd);
139 bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(startIndex));
140 bool splitEndGlyph = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(endIndex));
143 Vector2 currentPosition;
146 CharacterDirection isCurrentRightToLeft;
148 CharacterDirection isPrevoiusRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + startIndex) : false);
149 const bool isEllipsisEnabled = textModel->mElideEnabled;
150 const GlyphIndex startIndexOfGlyphs = textModel->GetStartIndexOfElidedGlyphs();
151 const GlyphIndex endIndexOfGlyphs = textModel->GetEndIndexOfElidedGlyphs();
152 const GlyphIndex firstMiddleIndexOfElidedGlyphs = textModel->GetFirstMiddleIndexOfElidedGlyphs();
153 const GlyphIndex secondMiddleIndexOfElidedGlyphs = textModel->GetSecondMiddleIndexOfElidedGlyphs();
154 const DevelText::EllipsisPosition::Type ellipsisPosition = textModel->GetEllipsisPosition();
156 for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
158 if(isEllipsisEnabled)
160 if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
162 if(index >= firstMiddleIndexOfElidedGlyphs &&
163 index < secondMiddleIndexOfElidedGlyphs)
165 if((index - 1 == firstMiddleIndexOfElidedGlyphs) && (firstMiddleIndexOfElidedGlyphs != 0))
167 sizesList.PushBack(blockSize);
168 positionsList.PushBack(blockPos);
171 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
173 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
175 // Ignore any glyph that was removed
181 if((ellipsisPosition == DevelText::EllipsisPosition::END) && (index > endIndexOfGlyphs))
183 //skip remaining elided glyphs
186 else if((ellipsisPosition == DevelText::EllipsisPosition::START) && (index <= startIndexOfGlyphs))
188 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
190 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
198 const GlyphInfo& glyph = *(glyphsBuffer + index);
199 const Vector2& position = *(positionsBuffer + index);
201 // If NULL, means all of the characters is left to right.
202 isCurrentRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + *(glyphToCharacterBuffer + index)) : false);
204 if(splitStartGlyph && (index == glyphStart))
206 // If the first glyph is a ligature that needs to be splitted, we may need only to add part of the glyph.
207 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersStart);
208 const CharacterIndex interGlyphIndex = startIndex - *(glyphToCharacterBuffer + glyphStart);
209 const Length numberOfCharacters = (glyphStart == glyphEnd) ? (endIndex - startIndex) + 1 : (numberOfCharactersStart - interGlyphIndex);
211 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + glyphAdvance * static_cast<float>(isCurrentRightToLeft ? (numberOfCharactersStart - interGlyphIndex - numberOfCharacters) : interGlyphIndex);
212 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
213 currentSize.x = static_cast<float>(numberOfCharacters) * glyphAdvance;
214 currentSize.y = currentLineHeight;
215 splitStartGlyph = false;
217 else if(splitEndGlyph && (index == glyphEnd))
219 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersEnd);
220 const CharacterIndex interGlyphIndex = endIndex - *(glyphToCharacterBuffer + glyphEnd);
221 const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex - 1;
223 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + (isCurrentRightToLeft ? (glyphAdvance * static_cast<float>(numberOfCharacters)) : 0.f);
224 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
225 currentSize.x = static_cast<float>(interGlyphIndex + 1) * glyphAdvance;
226 currentSize.y = currentLineHeight;
227 splitEndGlyph = false;
231 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x;
232 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
233 currentSize.x = glyph.advance;
234 currentSize.y = currentLineHeight;
236 // if there is next line to retrieve.
237 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
239 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
243 if((index == glyphStart) || (isEllipsisEnabled && (((ellipsisPosition == DevelText::EllipsisPosition::MIDDLE) && (index == secondMiddleIndexOfElidedGlyphs)) || ((ellipsisPosition == DevelText::EllipsisPosition::START) && (index - 1 == startIndexOfGlyphs)))))
245 blockPos = currentPosition;
246 blockSize = currentSize;
248 else if((isPrevoiusRightToLeft != isCurrentRightToLeft) || (blockPos.y != currentPosition.y)) //new direction or new line
250 sizesList.PushBack(blockSize);
251 positionsList.PushBack(blockPos);
253 blockPos = currentPosition;
254 blockSize = currentSize;
258 if(isCurrentRightToLeft)
260 blockPos.x -= currentSize.x;
263 blockSize.x += currentSize.x;
266 isPrevoiusRightToLeft = isCurrentRightToLeft;
270 sizesList.PushBack(blockSize);
271 positionsList.PushBack(blockPos);
274 float GetLineLeft(const LineRun& lineRun)
276 return lineRun.alignmentOffset;
279 float GetLineTop(const Vector<LineRun>& lines, const LineRun& lineRun)
282 const int numberOfLines = (int)lines.Count();
284 int currentLineIndex = 0;
285 Vector<LineRun>::ConstIterator endIt = (&lineRun);
286 for(Vector<LineRun>::Iterator it = lines.Begin();
288 ++it, ++currentLineIndex)
291 bool isLastLine = (currentLineIndex + 1) == numberOfLines;
292 lineTop += GetLineHeight(line, isLastLine);
298 float GetLineWidth(const LineRun& lineRun)
300 return lineRun.width;
303 Rect<float> GetLineBoundingRect(ModelPtr textModel, const uint32_t lineIndex)
306 if(textModel->mVisualModel == nullptr)
311 Length numberOfLines = textModel->mVisualModel->GetTotalNumberOfLines();
313 if(lineIndex >= numberOfLines)
318 const Vector<LineRun>& lines = textModel->mVisualModel->mLines;
319 const LineRun& lineRun = lines[lineIndex];
320 bool isFirstLine = lineIndex == 0;
321 bool isLastLine = (lineIndex + 1) == numberOfLines;
323 // Calculate the Left(lineX) = X position.
324 float lineX = GetLineLeft(lineRun) + textModel->mScrollPosition.x;
326 // Calculate the Top(lineY) = PreviousHeights.
327 // If the line is the first line of the text; its top = 0.
328 float lineY = (isFirstLine ? 0 : GetLineTop(lines, lineRun)) + textModel->mScrollPosition.y;
330 // The rectangle contains the width and height:
331 float lineWidth = GetLineWidth(lineRun);
332 float lineHeight = GetLineHeight(lineRun, isLastLine);
334 return {lineX, lineY, lineWidth, lineHeight};
337 float GetCharacterLeft(const GlyphInfo& glyph, const Vector2& characterPosition)
339 return characterPosition.x - glyph.xBearing;
342 float GetCharacterTop(const float& yPosition)
344 return (-1 * yPosition);
347 float GetCharacterHeight(const GlyphInfo& glyph)
352 float GetCharacterWidth(const GlyphInfo& glyph)
354 return glyph.advance;
357 Rect<> GetCharacterBoundingRect(ModelPtr textModel, const uint32_t charIndex)
359 if(textModel->mVisualModel == nullptr)
364 VisualModelPtr& visualModel = textModel->mVisualModel;
365 LogicalModelPtr& logicalModel = textModel->mLogicalModel;
367 if(charIndex >= logicalModel->mText.Count() || visualModel->mLines.Empty())
372 const Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
373 const Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
374 const Vector<LineRun>& lines = visualModel->mLines;
376 //For each character, the index of the first glyph.
377 const GlyphIndex glyphIndex = visualModel->mCharactersToGlyph[charIndex]; //took its glyphs
379 const Vector2& characterPosition = glyphPositions[glyphIndex];
380 const GlyphInfo& glyphInfo = glyphs[glyphIndex];
382 // GetLineOfCharacter function returns 0 if the lines are empty
383 const int lineIndex = visualModel->GetLineOfCharacter(charIndex);
384 const LineRun& lineRun = lines[lineIndex];
386 //Calculate the left: x position of the glyph + alignmentOffset of the line + mScrollPosition.x.
387 float characterX = lineRun.alignmentOffset + GetCharacterLeft(glyphInfo, characterPosition) + textModel->mScrollPosition.x;
389 //Calculate the Top(characterY): position.Y + previouse lines height + mScrollPosition.y.
390 bool isFirstLine = lineIndex == 0;
392 //If the line is the first line of the text; its top = 0.
393 float lineY = (isFirstLine ? 0 : GetLineTop(lines, lineRun));
395 float characterY = lineY + GetCharacterTop(characterPosition.y) + textModel->mScrollPosition.y;
397 //The rectangle contains the width and height:
398 float characterWidth = GetCharacterWidth(glyphInfo);
399 float characterHeight = GetCharacterHeight(glyphInfo);
401 return {characterX, characterY, characterWidth, characterHeight};
404 int GetCharIndexAtPosition(ModelPtr textModel, float visualX, float visualY)
406 if(textModel == nullptr)
411 VisualModelPtr& visualModel = textModel->mVisualModel;
413 const int totalNumberOfGlyphs = visualModel->mGlyphs.Count();
414 const int totalNumberOfLines = visualModel->mLines.Count();
416 if((0 == totalNumberOfGlyphs) ||
417 (0 == totalNumberOfLines))
422 //The top point of the view = 0.
428 const Vector<LineRun>& lines = visualModel->mLines;
432 int high = totalNumberOfLines;
436 // Searching for the correct line.
437 while(high - low > 1)
439 guess = (high + low) / 2;
440 Vector<LineRun>::ConstIterator it = lines.Begin() + guess;
442 lineTop = GetLineTop(lines, *it);
444 if(lineTop > visualY)
463 bool isLastLine = lineIndex + 1 == totalNumberOfLines;
467 const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
468 float lineHeight = GetLineHeight(line, isLastLine);
470 // If the visualY is placed after the last line.
471 if(visualY > lineTop + lineHeight)
477 // Check if a line is not found; return -1.
483 // Start searching for the visualX
484 const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
486 visualX -= line.alignmentOffset;
488 // Positions of the glyphs
489 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
491 const CharacterIndex startCharacter = line.characterRun.characterIndex;
492 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1;
494 CharacterIndex characterIndexAtPosition = -1;
495 CharacterIndex characterIndex = startCharacter;
496 float characterPosition;
497 float rightMostCharacterPosition;
499 for(; characterIndex != endCharacter; characterIndex++)
501 characterPosition = positionsBuffer[characterIndex].x;
502 rightMostCharacterPosition = positionsBuffer[characterIndex+1].x;
504 if(visualX < rightMostCharacterPosition && visualX >= characterPosition)
506 characterIndexAtPosition = characterIndex;
511 if(characterIndex == endCharacter)
513 // If the visualX is within the last character position or it comes after the last character; we return the last character.
514 rightMostCharacterPosition = positionsBuffer[endCharacter].x + GetCharacterWidth(visualModel->mGlyphs[endCharacter]);
516 if(visualX >= positionsBuffer[endCharacter].x && visualX < rightMostCharacterPosition)
518 characterIndexAtPosition = endCharacter;
522 return characterIndexAtPosition;
526 } // namespace Toolkit