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