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