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