2 * Copyright (c) 2021 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)
37 if(index == lastGlyphOfLine)
40 if(lineIndex < numberOfLines)
50 void UpdateLineInfo(const LineRun* lineRun, float& currentLineOffset, float& currentLineHeight, GlyphIndex& lastGlyphOfLine)
52 lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
53 currentLineOffset = currentLineOffset + currentLineHeight;
54 currentLineHeight = GetLineHeight(*lineRun);
57 void GetTextGeometry(ModelPtr textModel, CharacterIndex startIndex, CharacterIndex endIndex, Vector<Vector2>& sizesList, Vector<Vector2>& positionsList)
59 VisualModelPtr& visualModel = textModel->mVisualModel;
60 LogicalModelPtr& logicalModel = textModel->mLogicalModel;
62 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
63 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
64 const GlyphInfo* const glyphsBuffer = visualModel->mGlyphs.Begin();
65 const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
66 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
67 const CharacterIndex* const glyphToCharacterBuffer = visualModel->mGlyphsToCharacters.Begin();
68 const CharacterDirection* const modelCharacterDirectionsBuffer = (0u != logicalModel->mCharacterDirections.Count()) ? logicalModel->mCharacterDirections.Begin() : NULL;
70 if(startIndex >= logicalModel->mText.Count() && endIndex >= logicalModel->mText.Count())
73 if(startIndex >= logicalModel->mText.Count())
74 startIndex = logicalModel->mText.Count() - 1;
76 if(endIndex >= logicalModel->mText.Count())
77 endIndex = logicalModel->mText.Count() - 1;
79 if(startIndex > endIndex)
81 std::swap(startIndex, endIndex);
84 LineRun* lineRun = visualModel->mLines.Begin();
85 GlyphIndex glyphStart = *(charactersToGlyphBuffer + startIndex);
87 //if glyph not in the first line (in some ellipsis cases)
88 if(glyphStart < lineRun->glyphRun.glyphIndex)
90 glyphStart = lineRun->glyphRun.glyphIndex;
91 startIndex = *(glyphToCharacterBuffer + glyphStart);
93 if(startIndex > endIndex)
95 std::swap(startIndex, endIndex);
99 const Length numberOfGlyphs = *(glyphsPerCharacterBuffer + endIndex);
100 GlyphIndex glyphEnd = *(charactersToGlyphBuffer + endIndex) + ((numberOfGlyphs > 0) ? numberOfGlyphs - 1u : 0u);
101 LineIndex lineIndex = visualModel->GetLineOfCharacter(startIndex);
102 Length numberOfLines = visualModel->GetTotalNumberOfLines();
104 LineIndex firstLineIndex = lineIndex;
106 Vector2 textInLinePosition;
108 lineRun += firstLineIndex;
110 //get the first line and its vertical offset
111 float currentLineOffset = CalculateLineOffset(visualModel->mLines, firstLineIndex);
112 float currentLineHeight = GetLineHeight(*lineRun);
113 GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1;
115 // Check if the first/last glyph is a ligature that needs be splitted like English fi or Arabic ﻻ.
116 const Length numberOfCharactersStart = *(charactersPerGlyphBuffer + glyphStart);
117 const Length numberOfCharactersEnd = *(charactersPerGlyphBuffer + glyphEnd);
119 bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(startIndex));
120 bool splitEndGlyph = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(endIndex));
123 Vector2 currentPosition;
126 CharacterDirection isCurrentRightToLeft;
128 CharacterDirection isPrevoiusRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + startIndex) : false);
129 const bool isEllipsisEnabled = textModel->mElideEnabled;
130 const GlyphIndex startIndexOfGlyphs = textModel->GetStartIndexOfElidedGlyphs();
131 const GlyphIndex endIndexOfGlyphs = textModel->GetEndIndexOfElidedGlyphs();
132 const GlyphIndex firstMiddleIndexOfElidedGlyphs = textModel->GetFirstMiddleIndexOfElidedGlyphs();
133 const GlyphIndex secondMiddleIndexOfElidedGlyphs = textModel->GetSecondMiddleIndexOfElidedGlyphs();
134 const DevelText::EllipsisPosition::Type ellipsisPosition = textModel->GetEllipsisPosition();
136 for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
138 if(isEllipsisEnabled)
140 if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
142 if(index >= firstMiddleIndexOfElidedGlyphs &&
143 index < secondMiddleIndexOfElidedGlyphs)
145 if((index - 1 == firstMiddleIndexOfElidedGlyphs) && (firstMiddleIndexOfElidedGlyphs != 0))
147 sizesList.PushBack(blockSize);
148 positionsList.PushBack(blockPos);
151 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines))
153 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine);
155 // Ignore any glyph that was removed
161 if((ellipsisPosition == DevelText::EllipsisPosition::END) && (index >= endIndexOfGlyphs))
163 //skip remaining elided glyphs
166 else if((ellipsisPosition == DevelText::EllipsisPosition::START) && (index <= startIndexOfGlyphs))
168 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines))
170 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine);
178 const GlyphInfo& glyph = *(glyphsBuffer + index);
179 const Vector2& position = *(positionsBuffer + index);
181 // If NULL, means all of the characters is left to right.
182 isCurrentRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + *(glyphToCharacterBuffer + index)) : false);
184 if(splitStartGlyph && (index == glyphStart))
186 // If the first glyph is a ligature that needs to be splitted, we may need only to add part of the glyph.
187 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersStart);
188 const CharacterIndex interGlyphIndex = startIndex - *(glyphToCharacterBuffer + glyphStart);
189 const Length numberOfCharacters = (glyphStart == glyphEnd) ? (endIndex - startIndex) + 1 : (numberOfCharactersStart - interGlyphIndex);
191 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + glyphAdvance * static_cast<float>(isCurrentRightToLeft ? (numberOfCharactersStart - interGlyphIndex - numberOfCharacters) : interGlyphIndex);
192 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
193 currentSize.x = static_cast<float>(numberOfCharacters) * glyphAdvance;
194 currentSize.y = currentLineHeight;
195 splitStartGlyph = false;
197 else if(splitEndGlyph && (index == glyphEnd))
199 const float glyphAdvance = glyph.advance / static_cast<float>(numberOfCharactersEnd);
200 const CharacterIndex interGlyphIndex = endIndex - *(glyphToCharacterBuffer + glyphEnd);
201 const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex - 1;
203 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x + (isCurrentRightToLeft ? (glyphAdvance * static_cast<float>(numberOfCharacters)) : 0.f);
204 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
205 currentSize.x = static_cast<float>(interGlyphIndex + 1) * glyphAdvance;
206 currentSize.y = currentLineHeight;
207 splitEndGlyph = false;
211 currentPosition.x = lineRun->alignmentOffset + position.x - glyph.xBearing + textModel->mScrollPosition.x;
212 currentPosition.y = currentLineOffset + textModel->mScrollPosition.y;
213 currentSize.x = glyph.advance;
214 currentSize.y = currentLineHeight;
216 // if there is next line to retrieve.
217 if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines))
219 UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine);
223 if((index == glyphStart) || (isEllipsisEnabled && (((ellipsisPosition == DevelText::EllipsisPosition::MIDDLE) && (index == secondMiddleIndexOfElidedGlyphs)) || ((ellipsisPosition == DevelText::EllipsisPosition::START) && (index - 1 == startIndexOfGlyphs)))))
225 blockPos = currentPosition;
226 blockSize = currentSize;
228 else if((isPrevoiusRightToLeft != isCurrentRightToLeft) || (blockPos.y != currentPosition.y)) //new direction or new line
230 sizesList.PushBack(blockSize);
231 positionsList.PushBack(blockPos);
233 blockPos = currentPosition;
234 blockSize = currentSize;
238 if(isCurrentRightToLeft)
240 blockPos.x -= currentSize.x;
243 blockSize.x += currentSize.x;
246 isPrevoiusRightToLeft = isCurrentRightToLeft;
250 sizesList.PushBack(blockSize);
251 positionsList.PushBack(blockPos);
256 } // namespace Toolkit