[dali_1.2.53] Merge branch 'devel/master'
[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 const Vector2& ViewModel::GetShadowOffset() const
147 {
148   return mModel->GetShadowOffset();
149 }
150
151 const Vector4& ViewModel::GetShadowColor() const
152 {
153   return mModel->GetShadowColor();
154 }
155
156 const Vector4& ViewModel::GetUnderlineColor() const
157 {
158   return mModel->GetUnderlineColor();
159 }
160
161 bool ViewModel::IsUnderlineEnabled() const
162 {
163   return mModel->IsUnderlineEnabled();
164 }
165
166 float ViewModel::GetUnderlineHeight() const
167 {
168   return mModel->GetUnderlineHeight();
169 }
170
171 Length ViewModel::GetNumberOfUnderlineRuns() const
172 {
173   return mModel->GetNumberOfUnderlineRuns();
174 }
175
176 void ViewModel::GetUnderlineRuns( GlyphRun* underlineRuns, UnderlineRunIndex index, Length numberOfRuns ) const
177 {
178   mModel->GetUnderlineRuns( underlineRuns, index, numberOfRuns );
179 }
180
181 void ViewModel::ElideGlyphs()
182 {
183   mIsTextElided = false;
184
185   if( mModel->IsTextElideEnabled() )
186   {
187     const Length numberOfLines = mModel->GetNumberOfLines();
188     if( 0u != numberOfLines )
189     {
190       const LineRun* const lines = mModel->GetLines();
191
192       const LineRun& lastLine = *( lines + ( numberOfLines - 1u ) );
193       const Length numberOfLaidOutGlyphs = lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs;
194
195       if( lastLine.ellipsis && ( 0u != numberOfLaidOutGlyphs ) )
196       {
197         mIsTextElided = true;
198         TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
199
200         const GlyphInfo* const glyphs = mModel->GetGlyphs();
201         const Vector2* const positions = mModel->GetLayout();
202
203         // Copy the glyphs to be elided.
204         mElidedGlyphs.Resize( numberOfLaidOutGlyphs );
205         mElidedLayout.Resize( numberOfLaidOutGlyphs );
206
207         GlyphInfo* elidedGlyphsBuffer = mElidedGlyphs.Begin();
208         Vector2* elidedPositionsBuffer = mElidedLayout.Begin();
209
210         memcpy( elidedGlyphsBuffer, glyphs, numberOfLaidOutGlyphs * sizeof( GlyphInfo ) );
211         memcpy( elidedPositionsBuffer, positions, numberOfLaidOutGlyphs * sizeof( Vector2 ) );
212
213         const Size& controlSize = mModel->GetControlSize();
214
215         if( ( 1u == numberOfLines ) &&
216             ( lastLine.ascender - lastLine.descender > controlSize.height ) )
217         {
218           // Get the first glyph which is going to be replaced and the ellipsis glyph.
219           GlyphInfo& glyphToRemove = *elidedGlyphsBuffer;
220           const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
221
222           // Change the 'x' and 'y' position of the ellipsis glyph.
223           Vector2& position = *elidedPositionsBuffer;
224
225           position.x = ellipsisGlyph.xBearing;
226           position.y = -lastLine.ascender + controlSize.height - ellipsisGlyph.yBearing;
227
228           // Replace the glyph by the ellipsis glyph and resize the buffers.
229           glyphToRemove = ellipsisGlyph;
230
231           mElidedGlyphs.Resize( 1u );
232           mElidedLayout.Resize( 1u );
233
234           return;
235         }
236
237         // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
238         float firstPenX = 0.f; // Used if rtl text is elided.
239         float penY = 0.f;
240         bool firstPenSet = false;
241
242         // Add the ellipsis glyph.
243         bool inserted = false;
244         float removedGlypsWidth = 0.f;
245         Length numberOfRemovedGlyphs = 0u;
246         GlyphIndex index = numberOfLaidOutGlyphs - 1u;
247
248         // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
249         while( !inserted )
250         {
251           const GlyphInfo& glyphToRemove = *( elidedGlyphsBuffer + index );
252
253           if( 0u != glyphToRemove.fontId )
254           {
255             // i.e. The font id of the glyph shaped from the '\n' character is zero.
256
257             // Need to reshape the glyph as the font may be different in size.
258             const GlyphInfo& ellipsisGlyph = fontClient.GetEllipsisGlyph( fontClient.GetPointSize( glyphToRemove.fontId ) );
259
260             if( !firstPenSet )
261             {
262               const Vector2& position = *( elidedPositionsBuffer + index );
263
264               // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
265               penY = position.y + glyphToRemove.yBearing;
266
267               // Calculates the first penX which will be used if rtl text is elided.
268               firstPenX = position.x - glyphToRemove.xBearing;
269               if( firstPenX < -ellipsisGlyph.xBearing )
270               {
271                 // Avoids to exceed the bounding box when rtl text is elided.
272                 firstPenX = -ellipsisGlyph.xBearing;
273               }
274
275               removedGlypsWidth = -ellipsisGlyph.xBearing;
276
277               firstPenSet = true;
278             }
279
280             removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
281
282             // Calculate the width of the ellipsis glyph and check if it fits.
283             const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
284
285             if( ellipsisGlyphWidth < removedGlypsWidth )
286             {
287               GlyphInfo& glyphInfo = *( elidedGlyphsBuffer + index );
288               Vector2& position = *( elidedPositionsBuffer + index );
289               position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
290
291               // Replace the glyph by the ellipsis glyph.
292               glyphInfo = ellipsisGlyph;
293
294               // Change the 'x' and 'y' position of the ellipsis glyph.
295
296               if( position.x > firstPenX )
297               {
298                 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
299               }
300
301               position.x += ellipsisGlyph.xBearing;
302               position.y = penY - ellipsisGlyph.yBearing;
303
304               inserted = true;
305             }
306           }
307
308           if( !inserted )
309           {
310             if( index > 0u )
311             {
312               --index;
313             }
314             else
315             {
316               // No space for the ellipsis.
317               inserted = true;
318             }
319             ++numberOfRemovedGlyphs;
320           }
321         } // while( !inserted )
322
323           // 'Removes' all the glyphs after the ellipsis glyph.
324         const Length numberOfGlyphs = numberOfLaidOutGlyphs - numberOfRemovedGlyphs;
325         mElidedGlyphs.Resize( numberOfGlyphs );
326         mElidedLayout.Resize( numberOfGlyphs );
327       }
328     }
329   }
330 }
331
332 } // namespace Text
333
334 } // namespace Toolkit
335
336 } // namespace Dali