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