fix issue with text size with negative line spacing
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-geometry.cpp
1 /*
2  * Copyright (c) 2022 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, bool& isLastLine)
36 {
37   if(index == lastGlyphOfLine)
38   {
39     ++lineIndex;
40     if(lineIndex < numberOfLines)
41     {
42       isLastLine = (lineIndex + 1 == numberOfLines);
43       ++lineRun;
44       return true;
45     }
46   }
47
48   return false;
49 }
50
51 void UpdateLineInfo(const LineRun* lineRun, float& currentLineOffset, float& currentLineHeight, GlyphIndex& lastGlyphOfLine, bool isLastLine)
52 {
53   lastGlyphOfLine   = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
54   currentLineOffset = currentLineOffset + currentLineHeight;
55   currentLineHeight = GetLineHeight(*lineRun, isLastLine);
56 }
57
58 void GetTextGeometry(ModelPtr textModel, CharacterIndex startIndex, CharacterIndex endIndex, Vector<Vector2>& sizesList, Vector<Vector2>& positionsList)
59 {
60   VisualModelPtr&  visualModel  = textModel->mVisualModel;
61   LogicalModelPtr& logicalModel = textModel->mLogicalModel;
62
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;
70
71   if(startIndex >= logicalModel->mText.Count() && endIndex >= logicalModel->mText.Count())
72     return;
73
74   if(startIndex >= logicalModel->mText.Count())
75     startIndex = logicalModel->mText.Count() - 1;
76
77   if(endIndex >= logicalModel->mText.Count())
78     endIndex = logicalModel->mText.Count() - 1;
79
80   if(startIndex > endIndex)
81   {
82     std::swap(startIndex, endIndex);
83   }
84
85   LineRun*   lineRun    = visualModel->mLines.Begin();
86   GlyphIndex glyphStart = *(charactersToGlyphBuffer + startIndex);
87
88   //if glyph not in the first line (in some ellipsis cases)
89   if(glyphStart < lineRun->glyphRun.glyphIndex)
90   {
91     glyphStart = lineRun->glyphRun.glyphIndex;
92     startIndex = *(glyphToCharacterBuffer + glyphStart);
93
94     if(startIndex > endIndex)
95     {
96       std::swap(startIndex, endIndex);
97     }
98   }
99
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;
105
106   LineIndex firstLineIndex = lineIndex;
107   Size      textInLineSize;
108   Vector2   textInLinePosition;
109
110   lineRun += firstLineIndex;
111
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;
116
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);
120
121   bool splitStartGlyph = (numberOfCharactersStart > 1u) && HasLigatureMustBreak(logicalModel->GetScript(startIndex));
122   bool splitEndGlyph   = (glyphStart != glyphEnd) && (numberOfCharactersEnd > 1u) && HasLigatureMustBreak(logicalModel->GetScript(endIndex));
123
124   Vector2            currentSize;
125   Vector2            currentPosition;
126   Vector2            blockSize;
127   Vector2            blockPos;
128   CharacterDirection isCurrentRightToLeft;
129
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();
137
138   for(GlyphIndex index = glyphStart; index <= glyphEnd; ++index)
139   {
140     if(isEllipsisEnabled)
141     {
142       if(ellipsisPosition == DevelText::EllipsisPosition::MIDDLE)
143       {
144         if(index >= firstMiddleIndexOfElidedGlyphs &&
145            index < secondMiddleIndexOfElidedGlyphs)
146         {
147           if((index - 1 == firstMiddleIndexOfElidedGlyphs) && (firstMiddleIndexOfElidedGlyphs != 0))
148           {
149             sizesList.PushBack(blockSize);
150             positionsList.PushBack(blockPos);
151           }
152
153           if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
154           {
155             UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
156           }
157           // Ignore any glyph that was removed
158           continue;
159         }
160       }
161       else
162       {
163         if((ellipsisPosition == DevelText::EllipsisPosition::END) && (index >= endIndexOfGlyphs))
164         {
165           //skip remaining elided glyphs
166           break;
167         }
168         else if((ellipsisPosition == DevelText::EllipsisPosition::START) && (index <= startIndexOfGlyphs))
169         {
170           if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
171           {
172             UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
173           }
174
175           continue;
176         }
177       }
178     }
179
180     const GlyphInfo& glyph    = *(glyphsBuffer + index);
181     const Vector2&   position = *(positionsBuffer + index);
182
183     // If NULL, means all of the characters is left to right.
184     isCurrentRightToLeft = (nullptr != modelCharacterDirectionsBuffer ? *(modelCharacterDirectionsBuffer + *(glyphToCharacterBuffer + index)) : false);
185
186     if(splitStartGlyph && (index == glyphStart))
187     {
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);
192
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;
198     }
199     else if(splitEndGlyph && (index == glyphEnd))
200     {
201       const float          glyphAdvance       = glyph.advance / static_cast<float>(numberOfCharactersEnd);
202       const CharacterIndex interGlyphIndex    = endIndex - *(glyphToCharacterBuffer + glyphEnd);
203       const Length         numberOfCharacters = numberOfCharactersEnd - interGlyphIndex - 1;
204
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;
210     }
211     else
212     {
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;
217
218       // if there is next line to retrieve.
219       if(GetNextLine(index, lineIndex, lineRun, lastGlyphOfLine, numberOfLines, isLastLine))
220       {
221         UpdateLineInfo(lineRun, currentLineOffset, currentLineHeight, lastGlyphOfLine, isLastLine);
222       }
223     }
224
225     if((index == glyphStart) || (isEllipsisEnabled && (((ellipsisPosition == DevelText::EllipsisPosition::MIDDLE) && (index == secondMiddleIndexOfElidedGlyphs)) || ((ellipsisPosition == DevelText::EllipsisPosition::START) && (index - 1 == startIndexOfGlyphs)))))
226     {
227       blockPos  = currentPosition;
228       blockSize = currentSize;
229     }
230     else if((isPrevoiusRightToLeft != isCurrentRightToLeft) || (blockPos.y != currentPosition.y)) //new direction or new line
231     {
232       sizesList.PushBack(blockSize);
233       positionsList.PushBack(blockPos);
234
235       blockPos  = currentPosition;
236       blockSize = currentSize;
237     }
238     else
239     {
240       if(isCurrentRightToLeft)
241       {
242         blockPos.x -= currentSize.x;
243       }
244
245       blockSize.x += currentSize.x;
246     }
247
248     isPrevoiusRightToLeft = isCurrentRightToLeft;
249   }
250
251   //add last block
252   sizesList.PushBack(blockSize);
253   positionsList.PushBack(blockPos);
254 }
255
256 } // namespace Text
257
258 } // namespace Toolkit
259
260 } // namespace Dali