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