Change MatchSystemLanguageDirection is true.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / view-model.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/rendering/view-model.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/text-abstraction/font-client.h>
23 #include <memory.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/line-run.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Text
33 {
34 ViewModel::ViewModel(const ModelInterface* const model)
35 : mModel(model),
36   mElidedGlyphs(),
37   mElidedLayout(),
38   mIsTextElided(false)
39 {
40 }
41
42 ViewModel::~ViewModel()
43 {
44 }
45
46 const Size& ViewModel::GetControlSize() const
47 {
48   return mModel->GetControlSize();
49 }
50
51 const Size& ViewModel::GetLayoutSize() const
52 {
53   return mModel->GetLayoutSize();
54 }
55
56 const Vector2& ViewModel::GetScrollPosition() const
57 {
58   return mModel->GetScrollPosition();
59 }
60
61 HorizontalAlignment::Type ViewModel::GetHorizontalAlignment() const
62 {
63   return mModel->GetHorizontalAlignment();
64 }
65
66 VerticalAlignment::Type ViewModel::GetVerticalAlignment() const
67 {
68   return mModel->GetVerticalAlignment();
69 }
70
71 DevelText::VerticalLineAlignment::Type ViewModel::GetVerticalLineAlignment() const
72 {
73   return mModel->GetVerticalLineAlignment();
74 }
75
76 bool ViewModel::IsTextElideEnabled() const
77 {
78   return mModel->IsTextElideEnabled();
79 }
80
81 Length ViewModel::GetNumberOfLines() const
82 {
83   return mModel->GetNumberOfLines();
84 }
85
86 const LineRun* const ViewModel::GetLines() const
87 {
88   return mModel->GetLines();
89 }
90
91 Length ViewModel::GetNumberOfScripts() const
92 {
93   return mModel->GetNumberOfScripts();
94 }
95
96 const ScriptRun* const ViewModel::GetScriptRuns() const
97 {
98   return mModel->GetScriptRuns();
99 }
100
101 Length ViewModel::GetNumberOfGlyphs() const
102 {
103   if(mIsTextElided && mModel->IsTextElideEnabled())
104   {
105     return mElidedGlyphs.Count();
106   }
107   else
108   {
109     return mModel->GetNumberOfGlyphs();
110   }
111
112   return 0u;
113 }
114
115 const GlyphInfo* const ViewModel::GetGlyphs() const
116 {
117   if(mIsTextElided && mModel->IsTextElideEnabled())
118   {
119     return mElidedGlyphs.Begin();
120   }
121   else
122   {
123     return mModel->GetGlyphs();
124   }
125
126   return NULL;
127 }
128
129 const Vector2* const ViewModel::GetLayout() const
130 {
131   if(mIsTextElided && mModel->IsTextElideEnabled())
132   {
133     return mElidedLayout.Begin();
134   }
135   else
136   {
137     return mModel->GetLayout();
138   }
139
140   return NULL;
141 }
142
143 const Vector4* const ViewModel::GetColors() const
144 {
145   return mModel->GetColors();
146 }
147
148 const ColorIndex* const ViewModel::GetColorIndices() const
149 {
150   return mModel->GetColorIndices();
151 }
152
153 const Vector4* const ViewModel::GetBackgroundColors() const
154 {
155   return mModel->GetBackgroundColors();
156 }
157
158 const ColorIndex* const ViewModel::GetBackgroundColorIndices() const
159 {
160   return mModel->GetBackgroundColorIndices();
161 }
162
163 const Vector4& ViewModel::GetDefaultColor() const
164 {
165   return mModel->GetDefaultColor();
166 }
167
168 const Vector2& ViewModel::GetShadowOffset() const
169 {
170   return mModel->GetShadowOffset();
171 }
172
173 const Vector4& ViewModel::GetShadowColor() const
174 {
175   return mModel->GetShadowColor();
176 }
177
178 const float& ViewModel::GetShadowBlurRadius() const
179 {
180   return mModel->GetShadowBlurRadius();
181 }
182
183 const Vector4& ViewModel::GetUnderlineColor() const
184 {
185   return mModel->GetUnderlineColor();
186 }
187
188 bool ViewModel::IsUnderlineEnabled() const
189 {
190   return mModel->IsUnderlineEnabled();
191 }
192
193 float ViewModel::GetUnderlineHeight() const
194 {
195   return mModel->GetUnderlineHeight();
196 }
197
198 Length ViewModel::GetNumberOfUnderlineRuns() const
199 {
200   return mModel->GetNumberOfUnderlineRuns();
201 }
202
203 void ViewModel::GetUnderlineRuns(GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns) const
204 {
205   mModel->GetUnderlineRuns(underlineRuns, index, numberOfRuns);
206 }
207
208 const Vector4& ViewModel::GetOutlineColor() const
209 {
210   return mModel->GetOutlineColor();
211 }
212
213 uint16_t ViewModel::GetOutlineWidth() const
214 {
215   return mModel->GetOutlineWidth();
216 }
217
218 const Vector4& ViewModel::GetBackgroundColor() const
219 {
220   return mModel->GetBackgroundColor();
221 }
222
223 bool ViewModel::IsBackgroundEnabled() const
224 {
225   return mModel->IsBackgroundEnabled();
226 }
227
228 bool ViewModel::IsMarkupProcessorEnabled() const
229 {
230   return mModel->IsMarkupProcessorEnabled();
231 }
232
233 const GlyphInfo* ViewModel::GetHyphens() const
234 {
235   return mModel->GetHyphens();
236 }
237
238 const Length* ViewModel::GetHyphenIndices() const
239 {
240   return mModel->GetHyphenIndices();
241 }
242
243 Length ViewModel::GetHyphensCount() const
244 {
245   return mModel->GetHyphensCount();
246 }
247
248 void ViewModel::ElideGlyphs()
249 {
250   mIsTextElided = false;
251
252   if(mModel->IsTextElideEnabled())
253   {
254     const Length numberOfLines = mModel->GetNumberOfLines();
255     if(0u != numberOfLines)
256     {
257       const LineRun* const lines = mModel->GetLines();
258
259       const LineRun& lastLine              = *(lines + (numberOfLines - 1u));
260       const Length   numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
261
262       if(lastLine.ellipsis && (0u != numberOfLaidOutGlyphs))
263       {
264         mIsTextElided                          = true;
265         TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
266
267         const GlyphInfo* const glyphs    = mModel->GetGlyphs();
268         const Vector2* const   positions = mModel->GetLayout();
269
270         // Copy the glyphs to be elided.
271         mElidedGlyphs.Resize(numberOfLaidOutGlyphs);
272         mElidedLayout.Resize(numberOfLaidOutGlyphs);
273
274         GlyphInfo* elidedGlyphsBuffer    = mElidedGlyphs.Begin();
275         Vector2*   elidedPositionsBuffer = mElidedLayout.Begin();
276
277         memcpy(elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof(GlyphInfo));
278         memcpy(elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof(Vector2));
279
280         const Size& controlSize = mModel->GetControlSize();
281
282         if((1u == numberOfLines) &&
283            (lastLine.ascender - lastLine.descender > controlSize.height))
284         {
285           // Get the first glyph which is going to be replaced and the ellipsis glyph.
286           GlyphInfo&       glyphToRemove = *elidedGlyphsBuffer;
287           const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
288
289           // Change the 'x' and 'y' position of the ellipsis glyph.
290           Vector2& position = *elidedPositionsBuffer;
291
292           position.x = ellipsisGlyph.xBearing;
293           position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
294
295           // Replace the glyph by the ellipsis glyph and resize the buffers.
296           glyphToRemove = ellipsisGlyph;
297
298           mElidedGlyphs.Resize(1u);
299           mElidedLayout.Resize(1u);
300
301           return;
302         }
303
304         // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
305         float firstPenX   = 0.f; // Used if rtl text is elided.
306         float penY        = 0.f;
307         bool  firstPenSet = false;
308
309         // Add the ellipsis glyph.
310         bool       inserted              = false;
311         float      removedGlypsWidth     = 0.f;
312         Length     numberOfRemovedGlyphs = 0u;
313         GlyphIndex index                 = numberOfLaidOutGlyphs - 1u;
314
315         // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
316         while(!inserted)
317         {
318           const GlyphInfo& glyphToRemove = *(elidedGlyphsBuffer + index);
319
320           if(0u != glyphToRemove.fontId)
321           {
322             // i.e. The font id of the glyph shaped from the '\n' character is zero.
323
324             // Need to reshape the glyph as the font may be different in size.
325             const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph(fontClient.GetPointSize(glyphToRemove.fontId));
326
327             if(!firstPenSet || EqualsZero(glyphToRemove.advance))
328             {
329               const Vector2& position = *(elidedPositionsBuffer + index);
330
331               // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
332               penY = position.y + glyphToRemove.yBearing;
333
334               // Calculates the first penX which will be used if rtl text is elided.
335               firstPenX = position.x - glyphToRemove.xBearing;
336               if(firstPenX < -ellipsisGlyph.xBearing)
337               {
338                 // Avoids to exceed the bounding box when rtl text is elided.
339                 firstPenX = -ellipsisGlyph.xBearing;
340               }
341
342               removedGlypsWidth = -ellipsisGlyph.xBearing;
343
344               if(!EqualsZero(firstPenX))
345               {
346                 firstPenSet = true;
347               }
348             }
349
350             removedGlypsWidth += std::min(glyphToRemove.advance, (glyphToRemove.xBearing + glyphToRemove.width));
351
352             // Calculate the width of the ellipsis glyph and check if it fits.
353             const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
354
355             // If it is the last glyph to remove, add the ellipsis glyph without checking its width.
356             if((ellipsisGlyphWidth < removedGlypsWidth) || (index == 0u))
357             {
358               GlyphInfo& glyphInfo = *(elidedGlyphsBuffer + index);
359               Vector2&   position  = *(elidedPositionsBuffer + index);
360               position.x -= (0.f > glyphInfo.xBearing) ? glyphInfo.xBearing : 0.f;
361
362               // Replace the glyph by the ellipsis glyph.
363               glyphInfo = ellipsisGlyph;
364
365               // Change the 'x' and 'y' position of the ellipsis glyph.
366               if(position.x > firstPenX)
367               {
368                 position.x = firstPenX;
369                 if(ellipsisGlyphWidth < removedGlypsWidth)
370                 {
371                   position.x += removedGlypsWidth - ellipsisGlyphWidth;
372                 }
373               }
374
375               position.x += ellipsisGlyph.xBearing;
376               position.y = penY - ellipsisGlyph.yBearing;
377
378               inserted = true;
379             }
380           }
381
382           if(!inserted)
383           {
384             if(index > 0u)
385             {
386               --index;
387             }
388             else
389             {
390               // No space for the ellipsis.
391               inserted = true;
392             }
393             ++numberOfRemovedGlyphs;
394           }
395         } // while( !inserted )
396
397         // 'Removes' all the glyphs after the ellipsis glyph.
398         const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
399         mElidedGlyphs.Resize(numberOfGlyphs);
400         mElidedLayout.Resize(numberOfGlyphs);
401       }
402     }
403   }
404 }
405
406 } // namespace Text
407
408 } // namespace Toolkit
409
410 } // namespace Dali