2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
22 #include <dali/devel-api/text-abstraction/font-client.h>
24 #include <dali/public-api/common/constants.h>
27 #include <dali-toolkit/internal/text/rendering/view-model.h>
42 * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
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.
56 * @brief Sets the glyph's buffer into the bitmap's buffer.
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).
64 void TypesetGlyph( GlyphData& data,
65 const Vector2* const position,
66 const Vector4* const color,
67 Typesetter::Style style,
68 Pixel::Format pixelFormat )
70 if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) )
72 // Nothing to do if the width or height of the buffer is zero.
76 const int widthMinusOne = static_cast<int>( data.width - 1u );
77 const int heightMinusOne = static_cast<int>( data.height - 1u );
79 if ( Pixel::RGBA8888 == pixelFormat )
81 // Whether the given glyph is a color one.
82 const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
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;
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 );
95 // Initial vertical offset.
96 const int yOffset = data.verticalOffset + position->y;
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 )
101 const int yOffsetIndex = yOffset + lineIndex;
102 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
104 // Do not write out of bounds.
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 )
113 const int xOffsetIndex = xOffset + index;
114 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
116 // Don't write out of bounds.
120 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
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 );
128 if( Typesetter::STYLE_SHADOW == style )
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 );
137 std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
140 // Update the alpha channel.
141 if( Typesetter::STYLE_MASK == style )
143 // Create an alpha mask for color glyph.
144 *( packedColorGlyphBuffer + 3u ) = 0u;
148 *( packedColorGlyphBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
151 // Set the color into the final pixel buffer.
152 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
156 // Update the alpha channel.
157 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
159 // Copy non-transparent pixels only
162 // Check alpha of overlapped pixels
163 uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
164 uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( ¤tColor );
166 uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
167 uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
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 );
175 // Set the color into the final pixel buffer.
176 currentColor = packedColor;
184 // Initial vertical offset.
185 const int yOffset = data.verticalOffset + position->y;
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 )
190 const int yOffsetIndex = yOffset + lineIndex;
191 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
193 // Do not write out of bounds.
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 )
202 const int xOffsetIndex = xOffset + index;
203 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
205 // Don't write out of bounds.
209 uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
211 // Update the alpha channel.
212 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
214 // Copy non-transparent pixels only
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 // printf("y: %d, x: %d: alpha: %u, currentAlpha: %u, newAlpha: %u, a: %u\n", yOffsetIndex, xOffsetIndex, alpha, currentAlpha, newAlpha, std::max( currentAlpha, newAlpha ) );
222 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
223 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
224 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
225 // happen, for example, in the RTL text when we copy glyphs from right to left).
226 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = std::max( currentAlpha, newAlpha );
233 bool IsGlyphUnderlined( GlyphIndex index,
234 const Vector<GlyphRun>& underlineRuns )
236 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
237 endIt = underlineRuns.End();
241 const GlyphRun& run = *it;
243 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
254 TypesetterPtr Typesetter::New( const ModelInterface* const model )
256 return TypesetterPtr( new Typesetter( model ) );
259 ViewModel* Typesetter::GetViewModel()
264 PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
266 // @todo. This initial implementation for a TextLabel has only one visible page.
268 // Elides the text if needed.
269 mModel->ElideGlyphs();
271 // Retrieves the layout size.
272 const Size& layoutSize = mModel->GetLayoutSize();
274 // Set the offset for the vertical alignment.
277 switch( mModel->GetVerticalAlignment() )
279 case Layout::VERTICAL_ALIGN_TOP:
284 case Layout::VERTICAL_ALIGN_CENTER:
286 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
289 case Layout::VERTICAL_ALIGN_BOTTOM:
291 penY = static_cast<int>( size.height - layoutSize.height );
296 // Generate the image buffers of the text for each different style first,
297 // then combine all of them together as one final image buffer. We try to
298 // do all of these in CPU only, so that once the final texture is generated,
299 // no calculation is needed in GPU during each frame.
301 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
302 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
304 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
305 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
307 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
309 Devel::PixelBuffer imageBuffer;
311 if( RENDER_MASK == behaviour )
313 // Generate the image buffer as an alpha mask for color glyphs.
314 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
316 else if( RENDER_NO_TEXT == behaviour )
318 // Generate an empty image buffer so that it can been combined with the image buffers for styles
319 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
320 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
324 // Generate the image buffer for the text with no style.
325 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
328 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
330 // @todo. Support shadow and underline for partial text later on.
332 // Generate the shadow if enabled
333 const Vector2& shadowOffset = mModel->GetShadowOffset();
334 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
336 // Create the image buffer for shadow
337 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
339 // Combine the two buffers
340 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
343 // Generate the underline if enabled
344 const bool underlineEnabled = mModel->IsUnderlineEnabled();
345 if ( underlineEnabled )
347 // Create the image buffer for underline
348 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
350 // Combine the two buffers
351 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
355 // Create the final PixelData for the combined image buffer
356 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
361 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 )
363 // Retrieve lines, glyphs, positions and colors from the view model.
364 const Length modelNumberOfLines = mModel->GetNumberOfLines();
365 const LineRun* const modelLinesBuffer = mModel->GetLines();
366 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
367 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
368 const Vector2* const positionBuffer = mModel->GetLayout();
369 const Vector4* const colorsBuffer = mModel->GetColors();
370 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
372 // Whether to use the default color.
373 const bool useDefaultColor = ( NULL == colorsBuffer );
374 const Vector4& defaultColor = mModel->GetDefaultColor();
376 // Create and initialize the pixel buffer.
378 glyphData.verticalOffset = verticalOffset;
379 glyphData.width = bufferWidth;
380 glyphData.height = bufferHeight;
381 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
383 if ( Pixel::RGBA8888 == pixelFormat )
385 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
386 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
387 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
391 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
394 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
395 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
397 // Traverses the lines of the text.
398 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
400 const LineRun& line = *( modelLinesBuffer + lineIndex );
402 // Sets the horizontal offset of the line.
403 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
405 // Increases the vertical offset with the line's ascender.
406 glyphData.verticalOffset += static_cast<int>( line.ascender );
408 if ( style == Typesetter::STYLE_SHADOW )
410 const Vector2& shadowOffset = mModel->GetShadowOffset();
411 glyphData.horizontalOffset += shadowOffset.x;
412 if ( lineIndex == 0u )
414 // Only need to add the vertical shadow offset for once
415 glyphData.verticalOffset += shadowOffset.y;
419 const bool underlineEnabled = mModel->IsUnderlineEnabled();
420 const Vector4& underlineColor = mModel->GetUnderlineColor();
421 const float underlineHeight = mModel->GetUnderlineHeight();
423 // Get the underline runs.
424 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
425 Vector<GlyphRun> underlineRuns;
426 underlineRuns.Resize( numberOfUnderlineRuns );
427 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
429 bool thereAreUnderlinedGlyphs = false;
431 float currentUnderlinePosition = 0.0f;
432 float currentUnderlineThickness = underlineHeight;
433 float maxUnderlineThickness = currentUnderlineThickness;
435 FontId lastUnderlinedFontId = 0;
437 float lineExtentLeft = bufferWidth;
438 float lineExtentRight = 0.0f;
439 float baseline = 0.0f;
441 // Traverses the glyphs of the line.
442 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
443 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
445 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
447 // Ignore any glyph that out of the specified range
451 // Retrieve the glyph's info.
452 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
454 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
455 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
457 // Nothing to do if the glyph's width or height is zero.
461 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
462 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
464 // Are we still using the same fontId as previous
465 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
467 // We need to fetch fresh font underline metrics
468 FontMetrics fontMetrics;
469 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
470 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
471 const float descender = ceil( fabsf( fontMetrics.descender ) );
473 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
475 currentUnderlineThickness = fontMetrics.underlineThickness;
477 // Ensure underline will be at least a pixel high
478 if ( currentUnderlineThickness < 1.0f )
480 currentUnderlineThickness = 1.0f;
484 currentUnderlineThickness = ceil( currentUnderlineThickness );
488 // The underline thickness should be the max underline thickness of all glyphs of the line.
489 if ( currentUnderlineThickness > maxUnderlineThickness )
491 maxUnderlineThickness = currentUnderlineThickness;
494 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
495 if( currentUnderlinePosition > descender )
497 currentUnderlinePosition = descender;
500 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
502 // Move offset down by one ( EFL behavior )
503 currentUnderlinePosition = 1.0f;
506 lastUnderlinedFontId = glyphInfo->fontId;
509 // Retrieves the glyph's position.
510 const Vector2* const position = positionBuffer + glyphIndex;
511 if ( baseline < position->y + glyphInfo->yBearing )
513 baseline = position->y + glyphInfo->yBearing;
516 // Calculate the positions of leftmost and rightmost glyphs in the current line
517 if ( position->x < lineExtentLeft)
519 lineExtentLeft = position->x;
522 if ( position->x + glyphInfo->width > lineExtentRight)
524 lineExtentRight = position->x + glyphInfo->width;
527 // Retrieves the glyph's color.
528 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
530 const Vector4* color;
531 if ( style == Typesetter::STYLE_SHADOW )
533 color = &( mModel->GetShadowColor() );
537 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
540 // Retrieves the glyph's bitmap.
541 glyphData.glyphBitmap.buffer = NULL;
542 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
543 glyphData.glyphBitmap.height = glyphInfo->height;
544 fontClient.CreateBitmap( glyphInfo->fontId,
546 glyphData.glyphBitmap );
548 // Sets the glyph's bitmap into the bitmap of the whole text.
549 if( NULL != glyphData.glyphBitmap.buffer )
551 TypesetGlyph( glyphData,
556 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
557 delete []glyphData.glyphBitmap.buffer;
558 glyphData.glyphBitmap.buffer = NULL;
562 // Draw the underline from the leftmost glyph to the rightmost glyph
563 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
565 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
567 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
569 if( ( y < 0 ) || ( y > bufferHeight - 1 ) )
571 // Do not write out of bounds.
575 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
577 if( ( x < 0 ) || ( x > bufferWidth - 1 ) )
579 // Do not write out of bounds.
583 // Always RGBA image for text with styles
584 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
585 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
586 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
588 // Write the underline color to the pixel buffer
589 *( underlinePixelBuffer ) = static_cast<uint8_t>( underlineColor.r * 255.f );
590 *( underlinePixelBuffer + 1u ) = static_cast<uint8_t>( underlineColor.g * 255.f );
591 *( underlinePixelBuffer + 2u ) = static_cast<uint8_t>( underlineColor.b * 255.f );
592 *( underlinePixelBuffer + 3u ) = static_cast<uint8_t>( underlineColor.a * 255.f );
594 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
599 // Increases the vertical offset with the line's descender.
600 glyphData.verticalOffset += static_cast<int>( -line.descender );
603 return glyphData.bitmapBuffer;
606 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
608 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
609 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
611 Devel::PixelBuffer combinedPixelBuffer;
613 if ( topBuffer == NULL && bottomBuffer == NULL )
615 // Nothing to do if both buffers are empty.
616 return combinedPixelBuffer;
619 if ( topBuffer == NULL )
621 // Nothing to do if topBuffer is empty.
622 return bottomPixelBuffer;
625 if ( bottomBuffer == NULL )
627 // Nothing to do if bottomBuffer is empty.
628 return topPixelBuffer;
631 // Always combine two RGBA images
632 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
633 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
635 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
636 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
637 memset( combinedBuffer, 0u, bufferSizeChar );
639 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
641 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
642 // Otherwise, copy pixel from topBuffer to combinedBuffer.
644 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
645 unsigned int alphaBuffer2 = bottomBuffer[pixelIndex*4+3];
647 if ( alphaBuffer1 != 255 || alphaBuffer2 != 255 )
649 // At least one pixel is not fully opaque
650 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
651 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 ) );
652 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 ) );
653 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 ) );
654 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
658 // Copy the pixel from topBuffer to combinedBuffer
659 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
660 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
661 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
662 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
666 return combinedPixelBuffer;
669 Typesetter::Typesetter( const ModelInterface* const model )
670 : mModel( new ViewModel( model ) )
674 Typesetter::~Typesetter()
681 } // namespace Toolkit