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