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