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>
35 bool GetNextLine(GlyphIndex index, LineIndex& lineIndex, LineRun*& lineRun, GlyphIndex& lastGlyphOfLine, Length numberOfLines, bool& isLastLine)
37 if(index == lastGlyphOfLine)
40 if(lineIndex < numberOfLines)
42 isLastLine = (lineIndex + 1 == numberOfLines);
51 void UpdateLineInfo(const LineRun* lineRun, float& currentLineOffset, float& currentLineHeight, GlyphIndex& lastGlyphOfLine, bool isLastLine)
53 lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
54 currentLineOffset = currentLineOffset + currentLineHeight;
55 currentLineHeight = GetLineHeight(*lineRun, isLastLine);
58 void GetTextGeometry(ModelPtr textModel, CharacterIndex startIndex, CharacterIndex endIndex, Vector<Vector2>& sizesList, Vector<Vector2>& positionsList)
60 VisualModelPtr& visualModel = textModel->mVisualModel;
61 LogicalModelPtr& logicalModel = textModel->mLogicalModel;
63 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
64 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
65 const GlyphInfo* const glyphsBuffer = visualModel->mGlyphs.Begin();
66 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
67 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
68 const CharacterIndex* const glyphToCharacterBuffer = visualModel->mGlyphsToCharacters.Begin();
69 const CharacterDirection* const modelCharacterDirectionsBuffer = (0u != logicalModel->mCharacterDirections.Count()) ? logicalModel->mCharacterDirections.Begin() : NULL;
71 if(startIndex >= logicalModel->mText.Count() && endIndex >= logicalModel->mText.Count())
74 if(startIndex >= logicalModel->mText.Count())
75 startIndex = logicalModel->mText.Count() - 1;
77 if(endIndex >= logicalModel->mText.Count())
78 endIndex = logicalModel->mText.Count() - 1;
80 if(startIndex > endIndex)
82 std::swap(startIndex, endIndex);
85 LineRun* lineRun = visualModel->mLines.Begin();
86 GlyphIndex glyphStart = *(charactersToGlyphBuffer + startIndex);
88 //if glyph not in the first line (in some ellipsis cases)
89 if(glyphStart < lineRun->glyphRun.glyphIndex)
91 glyphStart = lineRun->glyphRun.glyphIndex;
92 startIndex = *(glyphToCharacterBuffer + glyphStart);
94 if(startIndex > endIndex)
96 std::swap(startIndex, endIndex);
100 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + endIndex);
101 GlyphIndex glyphEnd = *(charactersToGlyphBuffer + endIndex) + ((numberOfGlyphs > 0) ? numberOfGlyphs - 1u : 0u);
102 LineIndex lineIndex = visualModel->GetLineOfCharacter(startIndex);
103 Length numberOfLines = visualModel->GetTotalNumberOfLines();
104 bool isLastLine = lineIndex + 1 == numberOfLines;
106 LineIndex firstLineIndex = lineIndex;
108 Vector2 textInLinePosition;
110 lineRun += firstLineIndex;
112 //get the first line and its vertical offset
113 float currentLineOffset = CalculateLineOffset(visualModel->mLines, firstLineIndex);
114 float currentLineHeight = GetLineHeight(*lineRun, isLastLine);
115 GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1;
117 // Check if the first/last glyph is a ligature that needs be splitted like English fi or Arabic ﻻ.
118 const Length numberOfCharactersStart = *(charactersPerGlyphBuffer + glyphStart);
119 const Length numberOfCharactersEnd = *(charactersPerGlyphBuffer + glyphEnd);
121 bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(startIndex));
122 bool splitEndGlyph = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(endIndex));
125 Vector2 currentPosition;
128 CharacterDirection isCurrentRightToLeft;
130 CharacterDirection isPrevoiusRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + startIndex) : false);
131 const bool isEllipsisEnabled = textModel->mElideEnabled;
132 const GlyphIndex startIndexOfGlyphs = textModel->GetStartIndexOfElidedGlyphs();
133 const GlyphIndex endIndexOfGlyphs = textModel->GetEndIndexOfElidedGlyphs();
134 const GlyphIndex firstMiddleIndexOfElidedGlyphs = textModel->GetFirstMiddleIndexOfElidedGlyphs();
135 const GlyphIndex secondMiddleIndexOfElidedGlyphs = textModel->GetSecondMiddleIndexOfElidedGlyphs();
136 const DevelText::EllipsisPosition::Type ellipsisPosition = textModel->GetEllipsisPosition();
138 for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
140 if(isEllipsisEnabled)
142 if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
144 if(index >= firstMiddleIndexOfElidedGlyphs &&
145 index < secondMiddleIndexOfElidedGlyphs)
147 if((index - 1 == firstMiddleIndexOfElidedGlyphs) && (firstMiddleIndexOfElidedGlyphs != 0))
149 sizesList.PushBack(blockSize);
150 positionsList.PushBack(blockPos);
153 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
155 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
157 // Ignore any glyph that was removed
163 if((ellipsisPosition == DevelText::EllipsisPosition::END) && (index > endIndexOfGlyphs))
165 //skip remaining elided glyphs
168 else if((ellipsisPosition == DevelText::EllipsisPosition::START) && (index <= startIndexOfGlyphs))
170 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
172 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
180 const GlyphInfo& glyph = *(glyphsBuffer + index);
181 const Vector2& position = *(positionsBuffer + index);
183 // If NULL, means all of the characters is left to right.
184 isCurrentRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + *(glyphToCharacterBuffer + index)) : false);
186 if(splitStartGlyph && (index == glyphStart))
188 // If the first glyph is a ligature that needs to be splitted, we may need only to add part of the glyph.
189 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersStart);
190 const CharacterIndex interGlyphIndex = startIndex - *(glyphToCharacterBuffer + glyphStart);
191 const Length numberOfCharacters = (glyphStart == glyphEnd) ? (endIndex - startIndex) + 1 : (numberOfCharactersStart - interGlyphIndex);
193 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + glyphAdvance * static_cast<float>(isCurrentRightToLeft ? (numberOfCharactersStart - interGlyphIndex - numberOfCharacters) : interGlyphIndex);
194 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
195 currentSize.x = static_cast<float>(numberOfCharacters) * glyphAdvance;
196 currentSize.y = currentLineHeight;
197 splitStartGlyph = false;
199 else if(splitEndGlyph && (index == glyphEnd))
201 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersEnd);
202 const CharacterIndex interGlyphIndex = endIndex - *(glyphToCharacterBuffer + glyphEnd);
203 const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex - 1;
205 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + (isCurrentRightToLeft ? (glyphAdvance * static_cast<float>(numberOfCharacters)) : 0.f);
206 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
207 currentSize.x = static_cast<float>(interGlyphIndex + 1) * glyphAdvance;
208 currentSize.y = currentLineHeight;
209 splitEndGlyph = false;
213 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x;
214 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
215 currentSize.x = glyph.advance;
216 currentSize.y = currentLineHeight;
218 // if there is next line to retrieve.
219 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
221 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
225 if((index == glyphStart) || (isEllipsisEnabled && (((ellipsisPosition == DevelText::EllipsisPosition::MIDDLE) && (index == secondMiddleIndexOfElidedGlyphs)) || ((ellipsisPosition == DevelText::EllipsisPosition::START) && (index - 1 == startIndexOfGlyphs)))))
227 blockPos = currentPosition;
228 blockSize = currentSize;
230 else if((isPrevoiusRightToLeft != isCurrentRightToLeft) || (blockPos.y != currentPosition.y)) //new direction or new line
232 sizesList.PushBack(blockSize);
233 positionsList.PushBack(blockPos);
235 blockPos = currentPosition;
236 blockSize = currentSize;
240 if(isCurrentRightToLeft)
242 blockPos.x -= currentSize.x;
245 blockSize.x += currentSize.x;
248 isPrevoiusRightToLeft = isCurrentRightToLeft;
252 sizesList.PushBack(blockSize);
253 positionsList.PushBack(blockPos);
258 } // namespace Toolkit