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