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