eb49465bd9dac714d0b2ba43eab8bce110d0fe38
[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::GetNumberOfGlyphs() const
90 {
91   if( mIsTextElided && mModel->IsTextElideEnabled() )
92   {
93      return mElidedGlyphs.Count();
94   }
95   else
96   {
97     return mModel->GetNumberOfGlyphs();
98   }
99
100   return 0u;
101 }
102
103 const GlyphInfo* const ViewModel::GetGlyphs() const
104 {
105   if( mIsTextElided && mModel->IsTextElideEnabled() )
106   {
107     return mElidedGlyphs.Begin();
108   }
109   else
110   {
111     return mModel->GetGlyphs();
112   }
113
114   return NULL;
115 }
116
117 const Vector2* const ViewModel::GetLayout() const
118 {
119   if( mIsTextElided && mModel->IsTextElideEnabled() )
120   {
121     return mElidedLayout.Begin();
122   }
123   else
124   {
125     return mModel->GetLayout();
126   }
127
128   return NULL;
129 }
130
131 const Vector4* const ViewModel::GetColors() const
132 {
133   return mModel->GetColors();
134 }
135
136 const ColorIndex* const ViewModel::GetColorIndices() const
137 {
138   return mModel->GetColorIndices();
139 }
140
141 const Vector4& ViewModel::GetDefaultColor() const
142 {
143   return mModel->GetDefaultColor();
144 }
145
146 void ViewModel::ElideGlyphs()
147 {
148   mIsTextElided = false;
149
150   if( mModel->IsTextElideEnabled() )
151   {
152     const Length numberOfLines = mModel->GetNumberOfLines();
153     if( 0u != numberOfLines )
154     {
155       const LineRun* const lines = mModel->GetLines();
156
157       const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
158       const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
159
160       if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
161       {
162         mIsTextElided = true;
163         TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
164
165         const GlyphInfo* const glyphs = mModel->GetGlyphs();
166         const Vector2* const positions = mModel->GetLayout();
167
168         // Copy the glyphs to be elided.
169         mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
170         mElidedLayout.Resize( numberOfLaidOutGlyphs );
171
172         GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
173         Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
174
175         memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
176         memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
177
178         const Size& controlSize = mModel->GetControlSize();
179
180         if( ( 1u == numberOfLines ) &&
181             ( lastLine.ascender - lastLine.descender > controlSize.height ) )
182         {
183           // Get the first glyph which is going to be replaced and the ellipsis glyph.
184           GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
185           const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
186
187           // Change the 'x' and 'y' position of the ellipsis glyph.
188           Vector2& position = *elidedPositionsBuffer;
189
190           position.x = ellipsisGlyph.xBearing;
191           position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
192
193           // Replace the glyph by the ellipsis glyph and resize the buffers.
194           glyphToRemove = ellipsisGlyph;
195
196           mElidedGlyphs.Resize( 1u );
197           mElidedLayout.Resize( 1u );
198
199           return;
200         }
201
202         // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
203         float firstPenX = 0.f; // Used if rtl text is elided.
204         float penY = 0.f;
205         bool firstPenSet = false;
206
207         // Add the ellipsis glyph.
208         bool inserted = false;
209         float removedGlypsWidth = 0.f;
210         Length numberOfRemovedGlyphs = 0u;
211         GlyphIndex index = numberOfLaidOutGlyphs - 1u;
212
213         // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
214         while( !inserted )
215         {
216           const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
217
218           if( 0u != glyphToRemove.fontId )
219           {
220             // i.e. The font id of the glyph shaped from the '\n' character is zero.
221
222             // Need to reshape the glyph as the font may be different in size.
223             const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
224
225             if( !firstPenSet )
226             {
227               const Vector2& position = *( elidedPositionsBuffer + index );
228
229               // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
230               penY = position.y + glyphToRemove.yBearing;
231
232               // Calculates the first penX which will be used if rtl text is elided.
233               firstPenX = position.x - glyphToRemove.xBearing;
234               if( firstPenX < -ellipsisGlyph.xBearing )
235               {
236                 // Avoids to exceed the bounding box when rtl text is elided.
237                 firstPenX = -ellipsisGlyph.xBearing;
238               }
239
240               removedGlypsWidth = -ellipsisGlyph.xBearing;
241
242               firstPenSet = true;
243             }
244
245             removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
246
247             // Calculate the width of the ellipsis glyph and check if it fits.
248             const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
249
250             if( ellipsisGlyphWidth < removedGlypsWidth )
251             {
252               GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
253               Vector2& position = *( elidedPositionsBuffer + index );
254               position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
255
256               // Replace the glyph by the ellipsis glyph.
257               glyphInfo = ellipsisGlyph;
258
259               // Change the 'x' and 'y' position of the ellipsis glyph.
260
261               if( position.x > firstPenX )
262               {
263                 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
264               }
265
266               position.x += ellipsisGlyph.xBearing;
267               position.y = penY - ellipsisGlyph.yBearing;
268
269               inserted = true;
270             }
271           }
272
273           if( !inserted )
274           {
275             if( index > 0u )
276             {
277               --index;
278             }
279             else
280             {
281               // No space for the ellipsis.
282               inserted = true;
283             }
284             ++numberOfRemovedGlyphs;
285           }
286         } // while( !inserted )
287
288           // 'Removes' all the glyphs after the ellipsis glyph.
289         const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
290         mElidedGlyphs.Resize( numberOfGlyphs );
291         mElidedLayout.Resize( numberOfGlyphs );
292       }
293     }
294   }
295 }
296
297 } // namespace Text
298
299 } // namespace Toolkit
300
301 } // namespace Dali