Merge branch 'devel/master' 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->mLines.Count();
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       // Get the lines for the given range of glyphs.
96       // The lines contain the alignment offset which needs to be added to the glyph's position.
97       LineIndex firstLine = 0u;
98       Length numberOfLines = 0u;
99       mImpl->mVisualModel->GetNumberOfLines( glyphIndex,
100                                              numberOfLaidOutGlyphs,
101                                              firstLine,
102                                              numberOfLines );
103
104       Vector<LineRun> lines;
105       lines.Resize( numberOfLines );
106       LineRun* lineBuffer = lines.Begin();
107
108       mImpl->mVisualModel->GetLinesOfGlyphRange( lineBuffer,
109                                                  glyphIndex,
110                                                  numberOfLaidOutGlyphs );
111
112       // Get the first line for the given glyph range.
113       LineIndex lineIndex = firstLine;
114       LineRun* line = lineBuffer + lineIndex;
115
116       // Index of the last glyph of the line.
117       GlyphIndex lastGlyphIndexOfLine = line->glyphIndex + line->numberOfGlyphs - 1u;
118
119       // Add the alignment offset to the glyph's position.
120       for( Length index = 0u; index < numberOfLaidOutGlyphs; ++index )
121       {
122         ( *( glyphPositions + index ) ).x += line->alignmentOffset;
123
124         if( lastGlyphIndexOfLine == index )
125         {
126           // Get the next line.
127           ++lineIndex;
128
129           if( lineIndex < numberOfLines )
130           {
131             line = lineBuffer + lineIndex;
132             lastGlyphIndexOfLine = line->glyphIndex + line->numberOfGlyphs - 1u;
133           }
134         }
135       }
136
137       if( 1u == numberOfLaidOutGlyphs )
138       {
139         // not a point try to do ellipsis with only one laid out character.
140         return numberOfLaidOutGlyphs;
141       }
142
143       if( lastLine.ellipsis )
144       {
145         // firstPenX, penY and firstPenSet are used to position the ellipsis glyph if needed.
146         float firstPenX = 0.f; // Used if rtl text is elided.
147         float penY = 0.f;
148         bool firstPenSet = false;
149
150         // Add the ellipsis glyph.
151         bool inserted = false;
152         float removedGlypsWidth = 0.f;
153         Length numberOfRemovedGlyphs = 0u;
154         GlyphIndex index = numberOfLaidOutGlyphs - 1u;
155
156         // The ellipsis glyph has to fit in the place where the last glyph(s) is(are) removed.
157         while( !inserted )
158         {
159           const GlyphInfo& glyphToRemove = *( glyphs + index );
160
161           if( 0u != glyphToRemove.fontId )
162           {
163             // i.e. The font id of the glyph shaped from the '\n' character is zero.
164
165             // Need to reshape the glyph as the font may be different in size.
166             const GlyphInfo& ellipsisGlyph = mImpl->mFontClient.GetEllipsisGlyph( mImpl->mFontClient.GetPointSize( glyphToRemove.fontId ) );
167
168             if( !firstPenSet )
169             {
170               const Vector2& position = *( glyphPositions + index );
171
172               // Calculates the penY of the current line. It will be used to position the ellipsis glyph.
173               penY = position.y + glyphToRemove.yBearing;
174
175               // Calculates the first penX which will be used if rtl text is elided.
176               firstPenX = position.x - glyphToRemove.xBearing;
177               if( firstPenX < -ellipsisGlyph.xBearing )
178               {
179                 // Avoids to exceed the bounding box when rtl text is elided.
180                 firstPenX = -ellipsisGlyph.xBearing;
181               }
182
183               removedGlypsWidth = -ellipsisGlyph.xBearing;
184
185               firstPenSet = true;
186             }
187
188             removedGlypsWidth += std::min( glyphToRemove.advance, ( glyphToRemove.xBearing + glyphToRemove.width ) );
189
190             // Calculate the width of the ellipsis glyph and check if it fits.
191             const float ellipsisGlyphWidth = ellipsisGlyph.width + ellipsisGlyph.xBearing;
192             if( ellipsisGlyphWidth < removedGlypsWidth )
193             {
194               GlyphInfo& glyphInfo = *( glyphs + index );
195               Vector2& position = *( glyphPositions + index );
196               position.x -= ( 0.f > glyphInfo.xBearing ) ? glyphInfo.xBearing : 0.f;
197
198               // Replace the glyph by the ellipsis glyph.
199               glyphInfo = ellipsisGlyph;
200
201               // Change the 'x' and 'y' position of the ellipsis glyph.
202
203               if( position.x > firstPenX )
204               {
205                 position.x = firstPenX + removedGlypsWidth - ellipsisGlyphWidth;
206               }
207
208               position.x += ellipsisGlyph.xBearing;
209               position.y = penY - ellipsisGlyph.yBearing;
210
211               inserted = true;
212             }
213           }
214
215           if( !inserted )
216           {
217             if( index > 0u )
218             {
219               --index;
220             }
221             else
222             {
223               // No space for the ellipsis.
224               inserted = true;
225             }
226             ++numberOfRemovedGlyphs;
227           }
228         }
229
230         // 'Removes' all the glyphs after the ellipsis glyph.
231         numberOfLaidOutGlyphs -= numberOfRemovedGlyphs;
232       }
233     }
234   }
235
236   return numberOfLaidOutGlyphs;
237 }
238
239 const Vector4& View::GetTextColor() const
240 {
241   if ( mImpl->mVisualModel )
242   {
243     VisualModel& model = *mImpl->mVisualModel;
244     return model.GetTextColor();
245   }
246   return Vector4::ZERO;
247 }
248
249 const Vector2& View::GetShadowOffset() const
250 {
251   if ( mImpl->mVisualModel )
252   {
253     VisualModel& model = *mImpl->mVisualModel;
254     return model.GetShadowOffset();
255   }
256   return Vector2::ZERO;
257 }
258
259 const Vector4& View::GetShadowColor() const
260 {
261   if ( mImpl->mVisualModel )
262   {
263     VisualModel& model = *mImpl->mVisualModel;
264     return model.GetShadowColor();
265   }
266   return Vector4::ZERO;
267 }
268
269 const Vector4& View::GetUnderlineColor() const
270 {
271   if ( mImpl->mVisualModel )
272   {
273     VisualModel& model = *mImpl->mVisualModel;
274     return model.GetUnderlineColor();
275   }
276   return Vector4::ZERO;
277 }
278
279 bool View::IsUnderlineEnabled() const
280 {
281   if ( mImpl->mVisualModel )
282   {
283     VisualModel& model = *mImpl->mVisualModel;
284     return model.IsUnderlineEnabled();
285   }
286   return false;
287 }
288
289 float View::GetUnderlineHeight() const
290 {
291   if ( mImpl->mVisualModel )
292   {
293     VisualModel& model = *mImpl->mVisualModel;
294     return model.GetUnderlineHeight();
295   }
296   return 0.0f;
297 }
298
299 Length View::GetNumberOfGlyphs() const
300 {
301   if( mImpl->mVisualModel )
302   {
303     VisualModel& model = *mImpl->mVisualModel;
304
305     const Length glyphCount = model.mGlyphs.Count();
306     const Length positionCount = model.mGlyphPositions.Count();
307
308     DALI_ASSERT_DEBUG( positionCount <= glyphCount && "Invalid glyph positions in Model" );
309
310     return (positionCount < glyphCount) ? positionCount : glyphCount;
311   }
312
313   return 0;
314 }
315
316 } // namespace Text
317
318 } // namespace Toolkit
319
320 } // namespace Dali