Merge "Merge branch 'tizen' into devel/new_mesh" into devel/new_mesh
[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           // Need to reshape the glyph as the font may be different in size.
120           const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
121
122           if( !firstPenSet )
123           {
124             const Vector2& position = *( glyphPositions + index );
125
126             // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
127             penY = position.y + glyphToRemove.yBearing;
128
129             // Calculates the first penX which will be used if rtl text is elided.
130             firstPenX = position.x - glyphToRemove.xBearing;
131             if( firstPenX < -ellipsisGlyph.xBearing )
132             {
133               // Avoids to exceed the bounding box when rtl text is elided.
134               firstPenX = -ellipsisGlyph.xBearing;
135             }
136
137             removedGlypsWidth = -ellipsisGlyph.xBearing;
138
139             firstPenSet = true;
140           }
141
142           removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
143
144           // Calculate the width of the ellipsis glyph and check if it fits.
145           const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
146           if( ellipsisGlyphWidth < removedGlypsWidth )
147           {
148             GlyphInfo& glyphInfo = *( glyphs + index );
149             Vector2& position = *( glyphPositions + index );
150             position.x -= glyphInfo.xBearing;
151
152             // Replace the glyph by the ellipsis glyph.
153             glyphInfo = ellipsisGlyph;
154
155             // Change the 'x' and 'y' position of the ellipsis glyph.
156
157             if( position.x > firstPenX )
158             {
159               position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
160             }
161
162             position.x += ellipsisGlyph.xBearing;
163             position.y = penY - ellipsisGlyph.yBearing;
164
165             inserted = true;
166           }
167           else
168           {
169             if( index > 0u )
170             {
171               --index;
172             }
173             else
174             {
175               // No space for the ellipsis.
176               inserted = true;
177             }
178             ++numberOfRemovedGlyphs;
179           }
180         }
181
182         // 'Removes' all the glyphs after the ellipsis glyph.
183         numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
184       }
185     }
186   }
187
188   return numberOfLaidOutGlyphs;
189 }
190
191 const Vector4& View::GetTextColor() const
192 {
193   if ( mImpl->mVisualModel )
194   {
195     VisualModel& model = *mImpl->mVisualModel;
196     return model.GetTextColor();
197   }
198   return Vector4::ZERO;
199 }
200
201 const Vector2& View::GetShadowOffset() const
202 {
203   if ( mImpl->mVisualModel )
204   {
205     VisualModel& model = *mImpl->mVisualModel;
206     return model.GetShadowOffset();
207   }
208   return Vector2::ZERO;
209 }
210
211 const Vector4& View::GetShadowColor() const
212 {
213   if ( mImpl->mVisualModel )
214   {
215     VisualModel& model = *mImpl->mVisualModel;
216     return model.GetShadowColor();
217   }
218   return Vector4::ZERO;
219 }
220
221 const Vector4& View::GetUnderlineColor() const
222 {
223   if ( mImpl->mVisualModel )
224   {
225     VisualModel& model = *mImpl->mVisualModel;
226     return model.GetUnderlineColor();
227   }
228   return Vector4::ZERO;
229 }
230
231 bool View::IsUnderlineEnabled() const
232 {
233   if ( mImpl->mVisualModel )
234   {
235     VisualModel& model = *mImpl->mVisualModel;
236     return model.IsUnderlineEnabled();
237   }
238   return false;
239 }
240
241 float View::GetUnderlineHeight() const
242 {
243   if ( mImpl->mVisualModel )
244   {
245     VisualModel& model = *mImpl->mVisualModel;
246     return model.GetUnderlineHeight();
247   }
248   return 0.0f;
249 }
250
251 Length View::GetNumberOfGlyphs() const
252 {
253   if( mImpl->mVisualModel )
254   {
255     VisualModel& model = *mImpl->mVisualModel;
256
257     Length glyphCount = model.GetNumberOfGlyphs();
258     Length positionCount = model.GetNumberOfGlyphPositions();
259
260     DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
261
262     return (positionCount < glyphCount) ? positionCount : glyphCount;
263   }
264
265   return 0;
266 }
267
268 } // namespace Text
269
270 } // namespace Toolkit
271
272 } // namespace Dali