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;
73 positionsList.Clear();
75 if(charactersToGlyphBuffer == nullptr || glyphsPerCharacterBuffer == nullptr || charactersPerGlyphBuffer == nullptr || glyphToCharacterBuffer == nullptr )
80 if(startIndex >= logicalModel->mText.Count() && endIndex >= logicalModel->mText.Count())
85 if(startIndex >= logicalModel->mText.Count())
87 startIndex = logicalModel->mText.Count() - 1;
90 if(endIndex >= logicalModel->mText.Count())
92 endIndex = logicalModel->mText.Count() - 1;
95 if(startIndex > endIndex)
97 std::swap(startIndex, endIndex);
100 LineRun* lineRun = visualModel->mLines.Begin();
101 GlyphIndex glyphStart = *(charactersToGlyphBuffer + startIndex);
103 //if glyph not in the first line (in some ellipsis cases)
104 if(glyphStart < lineRun->glyphRun.glyphIndex)
106 glyphStart = lineRun->glyphRun.glyphIndex;
107 startIndex = *(glyphToCharacterBuffer + glyphStart);
109 if(startIndex > endIndex)
111 std::swap(startIndex, endIndex);
115 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + endIndex);
116 GlyphIndex glyphEnd = *(charactersToGlyphBuffer + endIndex) + ((numberOfGlyphs > 0) ? numberOfGlyphs - 1u : 0u);
117 LineIndex lineIndex = visualModel->GetLineOfCharacter(startIndex);
118 Length numberOfLines = visualModel->GetTotalNumberOfLines();
119 bool isLastLine = lineIndex + 1 == numberOfLines;
121 LineIndex firstLineIndex = lineIndex;
123 Vector2 textInLinePosition;
125 lineRun += firstLineIndex;
127 //get the first line and its vertical offset
128 float currentLineOffset = CalculateLineOffset(visualModel->mLines, firstLineIndex);
129 float currentLineHeight = GetLineHeight(*lineRun, isLastLine);
130 GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1;
132 // Check if the first/last glyph is a ligature that needs be splitted like English fi or Arabic ﻻ.
133 const Length numberOfCharactersStart = *(charactersPerGlyphBuffer + glyphStart);
134 const Length numberOfCharactersEnd = *(charactersPerGlyphBuffer + glyphEnd);
136 bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(startIndex));
137 bool splitEndGlyph = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(endIndex));
140 Vector2 currentPosition;
143 CharacterDirection isCurrentRightToLeft;
145 CharacterDirection isPrevoiusRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + startIndex) : false);
146 const bool isEllipsisEnabled = textModel->mElideEnabled;
147 const GlyphIndex startIndexOfGlyphs = textModel->GetStartIndexOfElidedGlyphs();
148 const GlyphIndex endIndexOfGlyphs = textModel->GetEndIndexOfElidedGlyphs();
149 const GlyphIndex firstMiddleIndexOfElidedGlyphs = textModel->GetFirstMiddleIndexOfElidedGlyphs();
150 const GlyphIndex secondMiddleIndexOfElidedGlyphs = textModel->GetSecondMiddleIndexOfElidedGlyphs();
151 const DevelText::EllipsisPosition::Type ellipsisPosition = textModel->GetEllipsisPosition();
153 for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
155 if(isEllipsisEnabled)
157 if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
159 if(index >= firstMiddleIndexOfElidedGlyphs &&
160 index < secondMiddleIndexOfElidedGlyphs)
162 if((index - 1 == firstMiddleIndexOfElidedGlyphs) && (firstMiddleIndexOfElidedGlyphs != 0))
164 sizesList.PushBack(blockSize);
165 positionsList.PushBack(blockPos);
168 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
170 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
172 // Ignore any glyph that was removed
178 if((ellipsisPosition == DevelText::EllipsisPosition::END) && (index > endIndexOfGlyphs))
180 //skip remaining elided glyphs
183 else if((ellipsisPosition == DevelText::EllipsisPosition::START) && (index <= startIndexOfGlyphs))
185 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
187 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
195 const GlyphInfo& glyph = *(glyphsBuffer + index);
196 const Vector2& position = *(positionsBuffer + index);
198 // If NULL, means all of the characters is left to right.
199 isCurrentRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + *(glyphToCharacterBuffer + index)) : false);
201 if(splitStartGlyph && (index == glyphStart))
203 // If the first glyph is a ligature that needs to be splitted, we may need only to add part of the glyph.
204 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersStart);
205 const CharacterIndex interGlyphIndex = startIndex - *(glyphToCharacterBuffer + glyphStart);
206 const Length numberOfCharacters = (glyphStart == glyphEnd) ? (endIndex - startIndex) + 1 : (numberOfCharactersStart - interGlyphIndex);
208 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + glyphAdvance * static_cast<float>(isCurrentRightToLeft ? (numberOfCharactersStart - interGlyphIndex - numberOfCharacters) : interGlyphIndex);
209 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
210 currentSize.x = static_cast<float>(numberOfCharacters) * glyphAdvance;
211 currentSize.y = currentLineHeight;
212 splitStartGlyph = false;
214 else if(splitEndGlyph && (index == glyphEnd))
216 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersEnd);
217 const CharacterIndex interGlyphIndex = endIndex - *(glyphToCharacterBuffer + glyphEnd);
218 const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex - 1;
220 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + (isCurrentRightToLeft ? (glyphAdvance * static_cast<float>(numberOfCharacters)) : 0.f);
221 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
222 currentSize.x = static_cast<float>(interGlyphIndex + 1) * glyphAdvance;
223 currentSize.y = currentLineHeight;
224 splitEndGlyph = false;
228 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x;
229 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
230 currentSize.x = glyph.advance;
231 currentSize.y = currentLineHeight;
233 // if there is next line to retrieve.
234 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
236 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
240 if((index == glyphStart) || (isEllipsisEnabled && (((ellipsisPosition == DevelText::EllipsisPosition::MIDDLE) && (index == secondMiddleIndexOfElidedGlyphs)) || ((ellipsisPosition == DevelText::EllipsisPosition::START) && (index - 1 == startIndexOfGlyphs)))))
242 blockPos = currentPosition;
243 blockSize = currentSize;
245 else if((isPrevoiusRightToLeft != isCurrentRightToLeft) || (blockPos.y != currentPosition.y)) //new direction or new line
247 sizesList.PushBack(blockSize);
248 positionsList.PushBack(blockPos);
250 blockPos = currentPosition;
251 blockSize = currentSize;
255 if(isCurrentRightToLeft)
257 blockPos.x -= currentSize.x;
260 blockSize.x += currentSize.x;
263 isPrevoiusRightToLeft = isCurrentRightToLeft;
267 sizesList.PushBack(blockSize);
268 positionsList.PushBack(blockPos);
273 } // namespace Toolkit