Fix for text ellipsis.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-view.cpp
1 /*
2  * Copyright (c) 2015 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/text-view.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/math/vector2.h>
23 #include <dali/devel-api/text-abstraction/font-client.h>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Text
32 {
33
34 struct View::Impl
35 {
36   VisualModelPtr mVisualModel;
37   TextAbstraction::FontClient mFontClient; ///< Handle to the font client.
38 };
39
40 View::View()
41 : mImpl( NULL )
42 {
43   mImpl = new View::Impl();
44
45   mImpl->mFontClient = TextAbstraction::FontClient::Get();
46 }
47
48 View::~View()
49 {
50   delete mImpl;
51 }
52
53 void View::SetVisualModel( VisualModelPtr visualModel )
54 {
55   mImpl->mVisualModel = visualModel;
56 }
57
58 Length View::GetGlyphs( GlyphInfo* glyphs,
59                         Vector2* glyphPositions,
60                         GlyphIndex glyphIndex,
61                         Length numberOfGlyphs ) const
62 {
63   Length numberOfLaidOutGlyphs = 0u;
64
65   if( mImpl->mVisualModel )
66   {
67     // If ellipsis is enabled, the number of glyphs the layout engine has laid out may be less than 'numberOfGlyphs'.
68     // Check the last laid out line to know if the layout engine elided some text.
69
70     const Length numberOfLines = mImpl->mVisualModel->GetNumberOfLines();
71     if( numberOfLines > 0u )
72     {
73       const LineRun& lastLine = *( mImpl->mVisualModel->mLines.Begin() + ( numberOfLines - 1u ) );
74
75       // If ellipsis is enabled, calculate the number of laid out glyphs.
76       // Otherwise use the given number of glyphs.
77       if( lastLine.ellipsis )
78       {
79         numberOfLaidOutGlyphs = lastLine.glyphIndex + lastLine.numberOfGlyphs;
80       }
81       else
82       {
83         numberOfLaidOutGlyphs = numberOfGlyphs;
84       }
85
86       // Retrieve from the visual model the glyphs and positions.
87       mImpl->mVisualModel->GetGlyphs( glyphs,
88                                       glyphIndex,
89                                       numberOfLaidOutGlyphs );
90
91       mImpl->mVisualModel->GetGlyphPositions( glyphPositions,
92                                               glyphIndex,
93                                               numberOfLaidOutGlyphs );
94
95       if( 1u == numberOfLaidOutGlyphs )
96       {
97         // not a point try to do ellipsis with only one laid out character.
98         return numberOfLaidOutGlyphs;
99       }
100
101       if( lastLine.ellipsis )
102       {
103         // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
104         float firstPenX = 0.f; // Used if rtl text is elided.
105         float penY = 0.f;
106         bool firstPenSet = false;
107
108         // Add the ellipsis glyph.
109         bool inserted = false;
110         float removedGlypsWidth = 0.f;
111         Length numberOfRemovedGlyphs = 0u;
112         GlyphIndex index = numberOfLaidOutGlyphs - 1u;
113
114         // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
115         while( !inserted )
116         {
117           const GlyphInfo& glyphToRemove = *( glyphs + index );
118
119           if( 0u != glyphToRemove.fontId )
120           {
121             // i.e. The font id of the glyph shaped from the '\n' character is zero.
122
123             // Need to reshape the glyph as the font may be different in size.
124             const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
125
126             if( !firstPenSet )
127             {
128               const Vector2& position = *( glyphPositions + index );
129
130               // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
131               penY = position.y + glyphToRemove.yBearing;
132
133               // Calculates the first penX which will be used if rtl text is elided.
134               firstPenX = position.x - glyphToRemove.xBearing;
135               if( firstPenX < -ellipsisGlyph.xBearing )
136               {
137                 // Avoids to exceed the bounding box when rtl text is elided.
138                 firstPenX = -ellipsisGlyph.xBearing;
139               }
140
141               removedGlypsWidth = -ellipsisGlyph.xBearing;
142
143               firstPenSet = true;
144             }
145
146             removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
147
148             // Calculate the width of the ellipsis glyph and check if it fits.
149             const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
150             if( ellipsisGlyphWidth < removedGlypsWidth )
151             {
152               GlyphInfo& glyphInfo = *( glyphs + index );
153               Vector2& position = *( glyphPositions + index );
154               position.x -= glyphInfo.xBearing;
155
156               // Replace the glyph by the ellipsis glyph.
157               glyphInfo = ellipsisGlyph;
158
159               // Change the 'x' and 'y' position of the ellipsis glyph.
160
161               if( position.x > firstPenX )
162               {
163                 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
164               }
165
166               position.x += ellipsisGlyph.xBearing;
167               position.y = penY - ellipsisGlyph.yBearing;
168
169               inserted = true;
170             }
171           }
172
173           if( !inserted )
174           {
175             if( index > 0u )
176             {
177               --index;
178             }
179             else
180             {
181               // No space for the ellipsis.
182               inserted = true;
183             }
184             ++numberOfRemovedGlyphs;
185           }
186         }
187
188         // 'Removes' all the glyphs after the ellipsis glyph.
189         numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
190       }
191     }
192   }
193
194   return numberOfLaidOutGlyphs;
195 }
196
197 const Vector4& View::GetTextColor() const
198 {
199   if ( mImpl->mVisualModel )
200   {
201     VisualModel& model = *mImpl->mVisualModel;
202     return model.GetTextColor();
203   }
204   return Vector4::ZERO;
205 }
206
207 const Vector2& View::GetShadowOffset() const
208 {
209   if ( mImpl->mVisualModel )
210   {
211     VisualModel& model = *mImpl->mVisualModel;
212     return model.GetShadowOffset();
213   }
214   return Vector2::ZERO;
215 }
216
217 const Vector4& View::GetShadowColor() const
218 {
219   if ( mImpl->mVisualModel )
220   {
221     VisualModel& model = *mImpl->mVisualModel;
222     return model.GetShadowColor();
223   }
224   return Vector4::ZERO;
225 }
226
227 const Vector4& View::GetUnderlineColor() const
228 {
229   if ( mImpl->mVisualModel )
230   {
231     VisualModel& model = *mImpl->mVisualModel;
232     return model.GetUnderlineColor();
233   }
234   return Vector4::ZERO;
235 }
236
237 bool View::IsUnderlineEnabled() const
238 {
239   if ( mImpl->mVisualModel )
240   {
241     VisualModel& model = *mImpl->mVisualModel;
242     return model.IsUnderlineEnabled();
243   }
244   return false;
245 }
246
247 float View::GetUnderlineHeight() const
248 {
249   if ( mImpl->mVisualModel )
250   {
251     VisualModel& model = *mImpl->mVisualModel;
252     return model.GetUnderlineHeight();
253   }
254   return 0.0f;
255 }
256
257 Length View::GetNumberOfGlyphs() const
258 {
259   if( mImpl->mVisualModel )
260   {
261     VisualModel& model = *mImpl->mVisualModel;
262
263     Length glyphCount = model.GetNumberOfGlyphs();
264     Length positionCount = model.GetNumberOfGlyphPositions();
265
266     DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
267
268     return (positionCount < glyphCount) ? positionCount : glyphCount;
269   }
270
271   return 0;
272 }
273
274 } // namespace Text
275
276 } // namespace Toolkit
277
278 } // namespace Dali