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