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