[dali_1.2.58] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / text-typesetter.cpp
1 /*
2  * Copyright (c) 2017 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/rendering/text-typesetter.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/text-abstraction/font-client.h>
23 #include <memory.h>
24 #include <dali/public-api/common/constants.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/text/rendering/view-model.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Text
36 {
37
38 namespace
39 {
40
41 /**
42  * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
43  */
44 struct GlyphData
45 {
46   Devel::PixelBuffer                           bitmapBuffer;     ///< The buffer of the whole bitmap. The format is RGBA8888.
47   Vector2*                                     position;         ///< The position of the glyph.
48   TextAbstraction::FontClient::GlyphBufferData glyphBitmap;      ///< The glyph's bitmap.
49   unsigned int                                 width;            ///< The bitmap's width.
50   unsigned int                                 height;           ///< The bitmap's height.
51   int                                          horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position.
52   int                                          verticalOffset;   ///< The vertical offset to be added to the 'y' glyph's position.
53 };
54
55 /**
56  * @brief Sets the glyph's buffer into the bitmap's buffer.
57  *
58  * @param[in] data Struct which contains the glyph's data and the bitmap's data.
59  * @param[in] position The position of the glyph.
60  * @param[in] color The color of the glyph.
61  * @param[in] style The style of the text.
62  * @param[in] pixelFormat The format of the pixel in the image that the text is rendered as (i.e. either Pixel::BGRA8888 or Pixel::L8).
63  */
64 void TypesetGlyph( GlyphData& data,
65                    const Vector2* const position,
66                    const Vector4* const color,
67                    Typesetter::Style style,
68                    Pixel::Format pixelFormat )
69 {
70   if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) )
71   {
72     // Nothing to do if the width or height of the buffer is zero.
73     return;
74   }
75
76   const int widthMinusOne = static_cast<int>( data.width - 1u );
77   const int heightMinusOne = static_cast<int>( data.height - 1u );
78
79   if ( Pixel::RGBA8888 == pixelFormat )
80   {
81     // Whether the given glyph is a color one.
82     const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
83
84     // Pointer to the color glyph if there is one.
85     const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>( data.glyphBitmap.buffer ) : NULL;
86
87     // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
88     // The format is RGBA8888.
89     uint32_t packedColor = 0u;
90     uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
91     *( packedColorBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
92     *( packedColorBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
93       *packedColorBuffer       = static_cast<uint8_t>( color->r * 255.f );
94
95     // Initial vertical offset.
96     const int yOffset = data.verticalOffset + position->y;
97
98     // Traverse the pixels of the glyph line per line.
99     for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
100     {
101       const int yOffsetIndex = yOffset + lineIndex;
102       if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
103       {
104         // Do not write out of bounds.
105         continue;
106       }
107
108       const int verticalOffset = yOffsetIndex * data.width;
109       const int xOffset = data.horizontalOffset + position->x;
110       const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
111       for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
112       {
113         const int xOffsetIndex = xOffset + index;
114         if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
115         {
116           // Don't write out of bounds.
117           continue;
118         }
119
120         uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
121
122         if( isColorGlyph )
123         {
124           // Retrieves the color from the color glyph. The format is BGRA8888.
125           uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
126           uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
127
128           if( Typesetter::STYLE_SHADOW == style )
129           {
130             // The shadow of color glyph needs to have the shadow color.
131             *( packedColorGlyphBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
132             *( packedColorGlyphBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
133               *packedColorGlyphBuffer       = static_cast<uint8_t>( color->r * 255.f );
134           }
135           else
136           {
137             std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
138           }
139
140           // Update the alpha channel.
141           if( Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style ) // Outline not shown for color glyph
142           {
143             // Create an alpha mask for color glyph.
144             *( packedColorGlyphBuffer + 3u ) = 0u;
145           }
146           else
147           {
148             *( packedColorGlyphBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
149           }
150
151           // Set the color into the final pixel buffer.
152           *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
153         }
154         else
155         {
156           // Update the alpha channel.
157           const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
158
159           // Copy non-transparent pixels only
160           if ( alpha > 0u )
161           {
162             // Check alpha of overlapped pixels
163             uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
164             uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( &currentColor );
165
166             uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
167             uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
168
169             // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
170             // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
171             // semi-transparent gaps between joint glyphs with overlapped pixels, which could
172             // happen, for example, in the RTL text when we copy glyphs from right to left).
173             *( packedColorBuffer + 3u ) = std::max( currentAlpha, newAlpha );
174
175             // Set the color into the final pixel buffer.
176             currentColor = packedColor;
177           }
178         }
179       }
180     }
181   }
182   else
183   {
184     // Initial vertical offset.
185     const int yOffset = data.verticalOffset + position->y;
186
187     // Traverse the pixels of the glyph line per line.
188     for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
189     {
190       const int yOffsetIndex = yOffset + lineIndex;
191       if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
192       {
193         // Do not write out of bounds.
194         continue;
195       }
196
197       const int verticalOffset = yOffsetIndex * data.width;
198       const int xOffset = data.horizontalOffset + position->x;
199       const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
200       for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
201       {
202         const int xOffsetIndex = xOffset + index;
203         if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
204         {
205           // Don't write out of bounds.
206           continue;
207         }
208
209         uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
210
211         // Update the alpha channel.
212         const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
213
214         // Copy non-transparent pixels only
215         if ( alpha > 0u )
216         {
217           // Check alpha of overlapped pixels
218           uint8_t& currentAlpha = *( bitmapBuffer + verticalOffset + xOffsetIndex );
219           uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
220
221           // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
222           // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
223           // semi-transparent gaps between joint glyphs with overlapped pixels, which could
224           // happen, for example, in the RTL text when we copy glyphs from right to left).
225           *( bitmapBuffer + verticalOffset + xOffsetIndex ) = std::max( currentAlpha, newAlpha );
226         }
227       }
228     }
229   }
230 }
231
232 bool IsGlyphUnderlined( GlyphIndex index,
233                          const Vector<GlyphRun>& underlineRuns )
234 {
235   for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
236          endIt = underlineRuns.End();
237          it != endIt;
238        ++it )
239   {
240     const GlyphRun& run = *it;
241
242     if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
243     {
244       return true;
245     }
246   }
247
248   return false;
249 }
250
251 } // namespace
252
253 TypesetterPtr Typesetter::New( const ModelInterface* const model )
254 {
255   return TypesetterPtr( new Typesetter( model ) );
256 }
257
258 ViewModel* Typesetter::GetViewModel()
259 {
260   return mModel;
261 }
262
263 PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
264 {
265   // @todo. This initial implementation for a TextLabel has only one visible page.
266
267   // Elides the text if needed.
268   mModel->ElideGlyphs();
269
270   // Retrieves the layout size.
271   const Size& layoutSize = mModel->GetLayoutSize();
272
273   // Set the offset for the vertical alignment.
274   int penY = 0u;
275
276   switch( mModel->GetVerticalAlignment() )
277   {
278     case Layout::VERTICAL_ALIGN_TOP:
279     {
280       // No offset to add.
281       break;
282     }
283     case Layout::VERTICAL_ALIGN_CENTER:
284     {
285       penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
286       break;
287     }
288     case Layout::VERTICAL_ALIGN_BOTTOM:
289     {
290       penY = static_cast<int>( size.height - layoutSize.height );
291       break;
292     }
293   }
294
295   // Generate the image buffers of the text for each different style first,
296   // then combine all of them together as one final image buffer. We try to
297   // do all of these in CPU only, so that once the final texture is generated,
298   // no calculation is needed in GPU during each frame.
299
300   const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
301   const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
302
303   const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
304   const unsigned int bufferSizeChar = 4u * bufferSizeInt;
305
306   Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
307
308   Devel::PixelBuffer imageBuffer;
309
310   if( RENDER_MASK == behaviour )
311   {
312     // Generate the image buffer as an alpha mask for color glyphs.
313     imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
314   }
315   else if( RENDER_NO_TEXT == behaviour )
316   {
317     // Generate an empty image buffer so that it can been combined with the image buffers for styles
318     imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
319     memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
320   }
321   else
322   {
323     // Generate the image buffer for the text with no style.
324     imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
325   }
326
327   if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
328   {
329
330     // Generate the outline if enabled
331     const float outlineWidth = mModel->GetOutlineWidth();
332     if ( outlineWidth > Math::MACHINE_EPSILON_1 )
333     {
334       // Create the image buffer for outline
335       Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
336
337       // Combine the two buffers
338       imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
339     }
340
341     // @todo. Support shadow and underline for partial text later on.
342
343     // Generate the shadow if enabled
344     const Vector2& shadowOffset = mModel->GetShadowOffset();
345     if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
346     {
347       // Create the image buffer for shadow
348       Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
349
350       // Combine the two buffers
351       imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
352     }
353
354     // Generate the underline if enabled
355     const bool underlineEnabled = mModel->IsUnderlineEnabled();
356     if ( underlineEnabled )
357     {
358       // Create the image buffer for underline
359       Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
360
361       // Combine the two buffers
362       imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
363     }
364   }
365
366   // Create the final PixelData for the combined image buffer
367   PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
368
369   return pixelData;
370 }
371
372 Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex )
373 {
374   // Retrieve lines, glyphs, positions and colors from the view model.
375   const Length modelNumberOfLines = mModel->GetNumberOfLines();
376   const LineRun* const modelLinesBuffer = mModel->GetLines();
377   const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
378   const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
379   const Vector2* const positionBuffer = mModel->GetLayout();
380   const Vector4* const colorsBuffer = mModel->GetColors();
381   const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
382
383   // Whether to use the default color.
384   const bool useDefaultColor = ( NULL == colorsBuffer );
385   const Vector4& defaultColor = mModel->GetDefaultColor();
386
387   // Create and initialize the pixel buffer.
388   GlyphData glyphData;
389   glyphData.verticalOffset = verticalOffset;
390   glyphData.width = bufferWidth;
391   glyphData.height = bufferHeight;
392   glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
393   glyphData.horizontalOffset = 0;
394
395   if ( Pixel::RGBA8888 == pixelFormat )
396   {
397     const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
398     const unsigned int bufferSizeChar = 4u * bufferSizeInt;
399     memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
400   }
401   else
402   {
403     memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
404   }
405
406   // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
407   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
408
409   // Traverses the lines of the text.
410   for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
411   {
412     const LineRun& line = *( modelLinesBuffer + lineIndex );
413
414     // Sets the horizontal offset of the line.
415     glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
416
417     // Increases the vertical offset with the line's ascender.
418     glyphData.verticalOffset += static_cast<int>( line.ascender );
419
420     // Retrieves the glyph's outline width
421     float outlineWidth = mModel->GetOutlineWidth();
422
423     if( style == Typesetter::STYLE_OUTLINE )
424     {
425       glyphData.horizontalOffset -= outlineWidth;
426       if( lineIndex == 0u )
427       {
428         // Only need to add the vertical outline offset for the first line
429         glyphData.verticalOffset -= outlineWidth;
430       }
431     }
432     else if ( style == Typesetter::STYLE_SHADOW )
433     {
434       const Vector2& shadowOffset = mModel->GetShadowOffset();
435       glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
436
437       if ( lineIndex == 0u )
438       {
439         // Only need to add the vertical shadow offset for first line
440         glyphData.verticalOffset += shadowOffset.y - outlineWidth;
441       }
442     }
443
444     const bool underlineEnabled = mModel->IsUnderlineEnabled();
445     const Vector4& underlineColor = mModel->GetUnderlineColor();
446     const float underlineHeight = mModel->GetUnderlineHeight();
447
448     // Get the underline runs.
449     const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
450     Vector<GlyphRun> underlineRuns;
451     underlineRuns.Resize( numberOfUnderlineRuns );
452     mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
453
454     bool thereAreUnderlinedGlyphs = false;
455
456     float currentUnderlinePosition = 0.0f;
457     float currentUnderlineThickness = underlineHeight;
458     float maxUnderlineThickness = currentUnderlineThickness;
459
460     FontId lastUnderlinedFontId = 0;
461
462     float lineExtentLeft = bufferWidth;
463     float lineExtentRight = 0.0f;
464     float baseline = 0.0f;
465
466     // Traverses the glyphs of the line.
467     const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
468     for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
469     {
470       if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
471       {
472         // Ignore any glyph that out of the specified range
473         continue;
474       }
475
476       // Retrieve the glyph's info.
477       const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
478
479       if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
480           ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
481       {
482         // Nothing to do if the glyph's width or height is zero.
483         continue;
484       }
485
486       const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
487       thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
488
489       // Are we still using the same fontId as previous
490       if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
491       {
492         // We need to fetch fresh font underline metrics
493         FontMetrics fontMetrics;
494         fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
495         currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
496         const float descender = ceil( fabsf( fontMetrics.descender ) );
497
498         if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
499         {
500           currentUnderlineThickness = fontMetrics.underlineThickness;
501
502           // Ensure underline will be at least a pixel high
503           if ( currentUnderlineThickness < 1.0f )
504           {
505             currentUnderlineThickness = 1.0f;
506           }
507           else
508           {
509             currentUnderlineThickness = ceil( currentUnderlineThickness );
510           }
511         }
512
513         // The underline thickness should be the max underline thickness of all glyphs of the line.
514         if ( currentUnderlineThickness > maxUnderlineThickness )
515         {
516           maxUnderlineThickness = currentUnderlineThickness;
517         }
518
519         // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
520         if( currentUnderlinePosition > descender )
521         {
522           currentUnderlinePosition = descender;
523         }
524
525         if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
526         {
527           // Move offset down by one ( EFL behavior )
528           currentUnderlinePosition = 1.0f;
529         }
530
531         lastUnderlinedFontId = glyphInfo->fontId;
532       } // underline
533
534       // Retrieves the glyph's position.
535       const Vector2* const position = positionBuffer + glyphIndex;
536       if ( baseline < position->y + glyphInfo->yBearing )
537       {
538         baseline = position->y + glyphInfo->yBearing;
539       }
540
541       // Calculate the positions of leftmost and rightmost glyphs in the current line
542       if ( position->x < lineExtentLeft)
543       {
544         lineExtentLeft = position->x;
545       }
546
547       if ( position->x + glyphInfo->width > lineExtentRight)
548       {
549         lineExtentRight = position->x + glyphInfo->width;
550       }
551
552       // Retrieves the glyph's color.
553       const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
554
555       const Vector4* color;
556       if ( style == Typesetter::STYLE_SHADOW )
557       {
558         color = &( mModel->GetShadowColor() );
559       }
560       else if ( style == Typesetter::STYLE_OUTLINE )
561       {
562         color = &( mModel->GetOutlineColor() );
563       }
564       else
565       {
566         color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
567       }
568
569       // Retrieves the glyph's bitmap.
570       glyphData.glyphBitmap.buffer = NULL;
571       glyphData.glyphBitmap.width = glyphInfo->width;   // Desired width and height.
572       glyphData.glyphBitmap.height = glyphInfo->height;
573
574       if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
575       {
576         // Don't render outline for other styles
577         outlineWidth = 0.0f;
578       }
579
580       fontClient.CreateBitmap( glyphInfo->fontId,
581                                glyphInfo->index,
582                                glyphData.glyphBitmap,
583                                outlineWidth );
584
585       // Sets the glyph's bitmap into the bitmap of the whole text.
586       if( NULL != glyphData.glyphBitmap.buffer )
587       {
588         TypesetGlyph( glyphData,
589                       position,
590                       color,
591                       style,
592                       pixelFormat);
593         // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
594         delete []glyphData.glyphBitmap.buffer;
595         glyphData.glyphBitmap.buffer = NULL;
596       }
597     }
598
599     // Draw the underline from the leftmost glyph to the rightmost glyph
600     if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
601     {
602       int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
603
604       for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
605       {
606         if( ( y < 0 ) || ( y > bufferHeight - 1 ) )
607         {
608           // Do not write out of bounds.
609           break;
610         }
611
612         for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
613         {
614           if( ( x < 0 ) || ( x > bufferWidth - 1 ) )
615           {
616             // Do not write out of bounds.
617             break;
618           }
619
620           // Always RGBA image for text with styles
621           uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
622           uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
623           uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
624
625           // Write the underline color to the pixel buffer
626           *( underlinePixelBuffer ) = static_cast<uint8_t>( underlineColor.r * 255.f );
627           *( underlinePixelBuffer + 1u ) = static_cast<uint8_t>( underlineColor.g * 255.f );
628           *( underlinePixelBuffer + 2u ) = static_cast<uint8_t>( underlineColor.b * 255.f );
629           *( underlinePixelBuffer + 3u ) = static_cast<uint8_t>( underlineColor.a * 255.f );
630
631           *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
632         }
633       }
634     }
635
636     // Increases the vertical offset with the line's descender.
637     glyphData.verticalOffset += static_cast<int>( -line.descender );
638   }
639
640   return glyphData.bitmapBuffer;
641 }
642
643 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
644 {
645   unsigned char* topBuffer = topPixelBuffer.GetBuffer();
646   unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
647
648   Devel::PixelBuffer combinedPixelBuffer;
649
650   if ( topBuffer == NULL && bottomBuffer == NULL )
651   {
652     // Nothing to do if both buffers are empty.
653     return combinedPixelBuffer;
654   }
655
656   if ( topBuffer == NULL )
657   {
658     // Nothing to do if topBuffer is empty.
659     return bottomPixelBuffer;
660   }
661
662   if ( bottomBuffer == NULL )
663   {
664     // Nothing to do if bottomBuffer is empty.
665     return topPixelBuffer;
666   }
667
668   // Always combine two RGBA images
669   const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
670   const unsigned int bufferSizeChar = 4u * bufferSizeInt;
671
672   combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
673   uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
674   memset( combinedBuffer, 0u, bufferSizeChar );
675
676   for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
677   {
678     // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
679     // Otherwise, copy pixel from topBuffer to combinedBuffer.
680
681     unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
682     unsigned int alphaBuffer2 = bottomBuffer[pixelIndex*4+3];
683
684     if ( alphaBuffer1 != 255 || alphaBuffer2 != 255 )
685     {
686       // At least one pixel is not fully opaque
687       // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
688       combinedBuffer[pixelIndex*4] = ( topBuffer[pixelIndex*4] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
689       combinedBuffer[pixelIndex*4+1] = ( topBuffer[pixelIndex*4+1] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4+1] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
690       combinedBuffer[pixelIndex*4+2] = ( topBuffer[pixelIndex*4+2] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4+2] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
691       combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
692     }
693     else
694     {
695       // Copy the pixel from topBuffer to combinedBuffer
696       combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
697       combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
698       combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
699       combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
700     }
701   }
702
703   return combinedPixelBuffer;
704 }
705
706 Typesetter::Typesetter( const ModelInterface* const model )
707 : mModel( new ViewModel( model ) )
708 {
709 }
710
711 Typesetter::~Typesetter()
712 {
713   delete mModel;
714 }
715
716 } // namespace Text
717
718 } // namespace Toolkit
719
720 } // namespace Dali