9fa5ad11a5c193611021158b7bfc7b73134dfa5d
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-geometry.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-geometry.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
26
27 using namespace Dali;
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Text
34 {
35 bool GetNextLine(GlyphIndex index, LineIndex& lineIndex, LineRun*& lineRun, GlyphIndex& lastGlyphOfLine, Length numberOfLines)
36 {
37   if(index == lastGlyphOfLine)
38   {
39     ++lineIndex;
40     if(lineIndex < numberOfLines)
41     {
42       ++lineRun;
43       return true;
44     }
45   }
46
47   return false;
48 }
49
50 void UpdateLineInfo(const LineRun* lineRun, float& currentLineOffset, float& currentLineHeight, GlyphIndex& lastGlyphOfLine)
51 {
52   lastGlyphOfLine   = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
53   currentLineOffset = currentLineOffset + currentLineHeight;
54   currentLineHeight = GetLineHeight(*lineRun);
55 }
56
57 void GetTextGeometry(ModelPtr textModel, CharacterIndex startIndex, CharacterIndex endIndex, Vector<Vector2>& sizesList, Vector<Vector2>& positionsList)
58 {
59   VisualModelPtr&  visualModel  = textModel->mVisualModel;
60   LogicalModelPtr& logicalModel = textModel->mLogicalModel;
61
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;
69
70   if(startIndex >= logicalModel->mText.Count() && endIndex >= logicalModel->mText.Count())
71     return;
72
73   if(startIndex >= logicalModel->mText.Count())
74     startIndex = logicalModel->mText.Count() - 1;
75
76   if(endIndex >= logicalModel->mText.Count())
77     endIndex = logicalModel->mText.Count() - 1;
78
79   if(startIndex > endIndex)
80   {
81     std::swap(startIndex, endIndex);
82   }
83
84   LineRun*   lineRun    = visualModel->mLines.Begin();
85   GlyphIndex glyphStart = *(charactersToGlyphBuffer + startIndex);
86
87   //if glyph not in the first line (in some ellipsis cases)
88   if(glyphStart < lineRun->glyphRun.glyphIndex)
89   {
90     glyphStart = lineRun->glyphRun.glyphIndex;
91     startIndex = *(glyphToCharacterBuffer + glyphStart);
92
93     if(startIndex > endIndex)
94     {
95       std::swap(startIndex, endIndex);
96     }
97   }
98
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();
103
104   LineIndex firstLineIndex = lineIndex;
105   Size      textInLineSize;
106   Vector2   textInLinePosition;
107
108   lineRun += firstLineIndex;
109
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;
114
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);
118
119   bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(startIndex));
120   bool splitEndGlyph   = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(endIndex));
121
122   Vector2            currentSize;
123   Vector2            currentPosition;
124   Vector2            blockSize;
125   Vector2            blockPos;
126   CharacterDirection isCurrentRightToLeft;
127
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();
135
136   for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
137   {
138     if(isEllipsisEnabled)
139     {
140       if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
141       {
142         if(index >= firstMiddleIndexOfElidedGlyphs &&
143            index < secondMiddleIndexOfElidedGlyphs)
144         {
145           if((index - 1 == firstMiddleIndexOfElidedGlyphs) && (firstMiddleIndexOfElidedGlyphs != 0))
146           {
147             sizesList.PushBack(blockSize);
148             positionsList.PushBack(blockPos);
149           }
150
151           if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines))
152           {
153             UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine);
154           }
155           // Ignore any glyph that was removed
156           continue;
157         }
158       }
159       else
160       {
161         if((ellipsisPosition == DevelText::EllipsisPosition::END) && (index >= endIndexOfGlyphs))
162         {
163           //skip remaining elided glyphs
164           break;
165         }
166         else if((ellipsisPosition == DevelText::EllipsisPosition::START) && (index <= startIndexOfGlyphs))
167         {
168           if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines))
169           {
170             UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine);
171           }
172
173           continue;
174         }
175       }
176     }
177
178     const GlyphInfo& glyph    = *(glyphsBuffer + index);
179     const Vector2&   position = *(positionsBuffer + index);
180
181     // If NULL, means all of the characters is left to right.
182     isCurrentRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + *(glyphToCharacterBuffer + index)) : false);
183
184     if(splitStartGlyph && (index == glyphStart))
185     {
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);
190
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;
196     }
197     else if(splitEndGlyph && (index == glyphEnd))
198     {
199       const float          glyphAdvance       = glyph.advance / static_cast<float>(numberOfCharactersEnd);
200       const CharacterIndex interGlyphIndex    = endIndex - *(glyphToCharacterBuffer + glyphEnd);
201       const Length         numberOfCharacters = numberOfCharactersEnd - interGlyphIndex - 1;
202
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;
208     }
209     else
210     {
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;
215
216       // if there is next line to retrieve.
217       if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines))
218       {
219         UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine);
220       }
221     }
222
223     if((index == glyphStart) || (isEllipsisEnabled && (((ellipsisPosition == DevelText::EllipsisPosition::MIDDLE) && (index == secondMiddleIndexOfElidedGlyphs)) || ((ellipsisPosition == DevelText::EllipsisPosition::START) && (index - 1 == startIndexOfGlyphs)))))
224     {
225       blockPos  = currentPosition;
226       blockSize = currentSize;
227     }
228     else if((isPrevoiusRightToLeft != isCurrentRightToLeft) || (blockPos.y != currentPosition.y)) //new direction or new line
229     {
230       sizesList.PushBack(blockSize);
231       positionsList.PushBack(blockPos);
232
233       blockPos  = currentPosition;
234       blockSize = currentSize;
235     }
236     else
237     {
238       if(isCurrentRightToLeft)
239       {
240         blockPos.x -= currentSize.x;
241       }
242
243       blockSize.x += currentSize.x;
244     }
245
246     isPrevoiusRightToLeft = isCurrentRightToLeft;
247   }
248
249   //add last block
250   sizesList.PushBack(blockSize);
251   positionsList.PushBack(blockPos);
252 }
253
254 } // namespace Text
255
256 } // namespace Toolkit
257
258 } // namespace Dali