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