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