2 * Copyright (c) 2018 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>
23 #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>
28 #include <dali-toolkit/devel-api/controls/text-controls/text-label-devel.h>
43 * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
47 Devel::PixelBuffer bitmapBuffer; ///< The buffer of the whole bitmap. The format is RGBA8888.
48 Vector2* position; ///< The position of the glyph.
49 TextAbstraction::FontClient::GlyphBufferData glyphBitmap; ///< The glyph's bitmap.
50 unsigned int width; ///< The bitmap's width.
51 unsigned int height; ///< The bitmap's height.
52 int horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position.
53 int verticalOffset; ///< The vertical offset to be added to the 'y' glyph's position.
57 * @brief Sets the glyph's buffer into the bitmap's buffer.
59 * @param[in] data Struct which contains the glyph's data and the bitmap's data.
60 * @param[in] position The position of the glyph.
61 * @param[in] color The color of the glyph.
62 * @param[in] style The style of the text.
63 * @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).
65 void TypesetGlyph( GlyphData& data,
66 const Vector2* const position,
67 const Vector4* const color,
68 Typesetter::Style style,
69 Pixel::Format pixelFormat )
71 if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) )
73 // Nothing to do if the width or height of the buffer is zero.
77 const int widthMinusOne = static_cast<int>( data.width - 1u );
78 const int heightMinusOne = static_cast<int>( data.height - 1u );
80 if ( Pixel::RGBA8888 == pixelFormat )
82 // Whether the given glyph is a color one.
83 const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
85 // Pointer to the color glyph if there is one.
86 const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>( data.glyphBitmap.buffer ) : NULL;
88 // Initial vertical offset.
89 const int yOffset = data.verticalOffset + position->y;
91 // Traverse the pixels of the glyph line per line.
92 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
94 const int yOffsetIndex = yOffset + lineIndex;
95 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
97 // Do not write out of bounds.
101 const int verticalOffset = yOffsetIndex * data.width;
102 const int xOffset = data.horizontalOffset + position->x;
103 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
104 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
106 const int xOffsetIndex = xOffset + index;
107 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
109 // Don't write out of bounds.
113 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
117 // Retrieves the color from the color glyph. The format is BGRA8888.
118 uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
119 uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
121 // Update the alpha channel.
122 if( Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style ) // Outline not shown for color glyph
124 // Create an alpha mask for color glyph.
125 *( packedColorGlyphBuffer + 3u ) = 0u;
126 *( packedColorGlyphBuffer + 2u ) = 0u;
127 *( packedColorGlyphBuffer + 1u ) = 0u;
128 *packedColorGlyphBuffer = 0u;
132 uint8_t colorAlpha = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
133 *( packedColorGlyphBuffer + 3u ) = colorAlpha;
135 if( Typesetter::STYLE_SHADOW == style )
137 // The shadow of color glyph needs to have the shadow color.
138 *( packedColorGlyphBuffer + 2u ) = static_cast<uint8_t>( color->b * colorAlpha );
139 *( packedColorGlyphBuffer + 1u ) = static_cast<uint8_t>( color->g * colorAlpha );
140 *packedColorGlyphBuffer = static_cast<uint8_t>( color->r * colorAlpha );
144 std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
146 *( packedColorGlyphBuffer + 2u ) = ( *( packedColorGlyphBuffer + 2u ) * colorAlpha / 255 );
147 *( packedColorGlyphBuffer + 1u ) = ( *( packedColorGlyphBuffer + 1u ) * colorAlpha / 255 );
148 *packedColorGlyphBuffer = ( *( packedColorGlyphBuffer ) * colorAlpha / 255 );
152 // Set the color into the final pixel buffer.
153 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
157 // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
158 // The format is RGBA8888.
159 uint32_t packedColor = 0u;
160 uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
162 // Update the alpha channel.
163 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
165 // Copy non-transparent pixels only
168 // Check alpha of overlapped pixels
169 uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
170 uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( ¤tColor );
172 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
173 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
174 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
175 // happen, for example, in the RTL text when we copy glyphs from right to left).
176 uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
177 currentAlpha = std::max( currentAlpha, alpha );
179 // Color is pre-muliplied with its alpha.
180 *( packedColorBuffer + 3u ) = static_cast<uint8_t>( color->a * currentAlpha );
181 *( packedColorBuffer + 2u ) = static_cast<uint8_t>( color->b * currentAlpha );
182 *( packedColorBuffer + 1u ) = static_cast<uint8_t>( color->g * currentAlpha );
183 *( packedColorBuffer ) = static_cast<uint8_t>( color->r * currentAlpha );
185 // Set the color into the final pixel buffer.
186 currentColor = packedColor;
194 // Whether the given glyph is a color one.
195 const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
197 // Initial vertical offset.
198 const int yOffset = data.verticalOffset + position->y;
200 // Traverse the pixels of the glyph line per line.
201 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
203 const int yOffsetIndex = yOffset + lineIndex;
204 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
206 // Do not write out of bounds.
210 const int verticalOffset = yOffsetIndex * data.width;
211 const int xOffset = data.horizontalOffset + position->x;
212 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
213 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
215 const int xOffsetIndex = xOffset + index;
216 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
218 // Don't write out of bounds.
222 uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
226 // Update the alpha channel.
227 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
229 // Copy non-transparent pixels only
232 // Check alpha of overlapped pixels
233 uint8_t& currentAlpha = *( bitmapBuffer + verticalOffset + xOffsetIndex );
235 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
236 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
237 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
238 // happen, for example, in the RTL text when we copy glyphs from right to left).
239 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = std::max( currentAlpha, alpha );
247 bool IsGlyphUnderlined( GlyphIndex index,
248 const Vector<GlyphRun>& underlineRuns )
250 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
251 endIt = underlineRuns.End();
255 const GlyphRun& run = *it;
257 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
268 TypesetterPtr Typesetter::New( const ModelInterface* const model )
270 return TypesetterPtr( new Typesetter( model ) );
273 ViewModel* Typesetter::GetViewModel()
278 PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
280 // @todo. This initial implementation for a TextLabel has only one visible page.
282 // Elides the text if needed.
283 mModel->ElideGlyphs();
285 // Retrieves the layout size.
286 const Size& layoutSize = mModel->GetLayoutSize();
288 // Set the offset for the vertical alignment.
291 switch( mModel->GetVerticalAlignment() )
293 case VerticalAlignment::TOP:
298 case VerticalAlignment::CENTER:
300 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
303 case VerticalAlignment::BOTTOM:
305 penY = static_cast<int>( size.height - layoutSize.height );
310 // Calculate vertical line alignment
311 switch( mModel->GetVerticalLineAlignment() )
313 case DevelText::VerticalLineAlignment::TOP:
317 case DevelText::VerticalLineAlignment::MIDDLE:
319 const auto& line = *mModel->GetLines();
320 penY -= line.descender;
321 penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
324 case DevelText::VerticalLineAlignment::BOTTOM:
326 const auto& line = *mModel->GetLines();
327 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
328 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
333 // Generate the image buffers of the text for each different style first,
334 // then combine all of them together as one final image buffer. We try to
335 // do all of these in CPU only, so that once the final texture is generated,
336 // no calculation is needed in GPU during each frame.
338 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
339 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
341 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
342 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
344 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
346 Devel::PixelBuffer imageBuffer;
348 if( RENDER_MASK == behaviour )
350 // Generate the image buffer as an alpha mask for color glyphs.
351 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
353 else if( RENDER_NO_TEXT == behaviour )
355 // Generate an empty image buffer so that it can been combined with the image buffers for styles
356 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
357 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
361 // Generate the image buffer for the text with no style.
362 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
365 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
368 // Generate the outline if enabled
369 const float outlineWidth = mModel->GetOutlineWidth();
370 if ( outlineWidth > Math::MACHINE_EPSILON_1 )
372 // Create the image buffer for outline
373 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs -1 );
375 // Combine the two buffers
376 imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
379 // @todo. Support shadow and underline for partial text later on.
381 // Generate the shadow if enabled
382 const Vector2& shadowOffset = mModel->GetShadowOffset();
383 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
385 // Create the image buffer for shadow
386 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
388 // Check whether it will be a soft shadow
389 const float& blurRadius = mModel->GetShadowBlurRadius();
391 if ( blurRadius > Math::MACHINE_EPSILON_1 )
393 shadowImageBuffer.ApplyGaussianBlur( blurRadius );
396 // Combine the two buffers
397 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
400 // Generate the underline if enabled
401 const bool underlineEnabled = mModel->IsUnderlineEnabled();
402 if ( underlineEnabled )
404 // Create the image buffer for underline
405 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penY, 0u, numberOfGlyphs - 1 );
407 // Combine the two buffers
408 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
412 // Create the final PixelData for the combined image buffer
413 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
418 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 )
420 // Retrieve lines, glyphs, positions and colors from the view model.
421 const Length modelNumberOfLines = mModel->GetNumberOfLines();
422 const LineRun* const modelLinesBuffer = mModel->GetLines();
423 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
424 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
425 const Vector2* const positionBuffer = mModel->GetLayout();
426 const Vector4* const colorsBuffer = mModel->GetColors();
427 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
429 // Whether to use the default color.
430 const bool useDefaultColor = ( NULL == colorsBuffer );
431 const Vector4& defaultColor = mModel->GetDefaultColor();
433 // Create and initialize the pixel buffer.
435 glyphData.verticalOffset = verticalOffset;
436 glyphData.width = bufferWidth;
437 glyphData.height = bufferHeight;
438 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
439 glyphData.horizontalOffset = 0;
441 if ( Pixel::RGBA8888 == pixelFormat )
443 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
444 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
445 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
449 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
452 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
453 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
455 // Traverses the lines of the text.
456 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
458 const LineRun& line = *( modelLinesBuffer + lineIndex );
460 // Sets the horizontal offset of the line.
461 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
463 // Increases the vertical offset with the line's ascender.
464 glyphData.verticalOffset += static_cast<int>( line.ascender );
466 // Include line spacing after first line
469 glyphData.verticalOffset += static_cast<int>( line.lineSpacing );
472 // Retrieves the glyph's outline width
473 float outlineWidth = mModel->GetOutlineWidth();
475 if( style == Typesetter::STYLE_OUTLINE )
477 glyphData.horizontalOffset -= outlineWidth;
478 if( lineIndex == 0u )
480 // Only need to add the vertical outline offset for the first line
481 glyphData.verticalOffset -= outlineWidth;
484 else if ( style == Typesetter::STYLE_SHADOW )
486 const Vector2& shadowOffset = mModel->GetShadowOffset();
487 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
489 if ( lineIndex == 0u )
491 // Only need to add the vertical shadow offset for first line
492 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
496 const bool underlineEnabled = mModel->IsUnderlineEnabled();
497 const Vector4& underlineColor = mModel->GetUnderlineColor();
498 const float underlineHeight = mModel->GetUnderlineHeight();
500 // Get the underline runs.
501 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
502 Vector<GlyphRun> underlineRuns;
503 underlineRuns.Resize( numberOfUnderlineRuns );
504 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
506 bool thereAreUnderlinedGlyphs = false;
508 float currentUnderlinePosition = 0.0f;
509 float currentUnderlineThickness = underlineHeight;
510 float maxUnderlineThickness = currentUnderlineThickness;
512 FontId lastUnderlinedFontId = 0;
514 float lineExtentLeft = bufferWidth;
515 float lineExtentRight = 0.0f;
516 float baseline = 0.0f;
518 // Traverses the glyphs of the line.
519 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
520 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
522 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
524 // Ignore any glyph that out of the specified range
528 // Retrieve the glyph's info.
529 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
531 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
532 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
534 // Nothing to do if the glyph's width or height is zero.
538 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
539 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
541 // Are we still using the same fontId as previous
542 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
544 // We need to fetch fresh font underline metrics
545 FontMetrics fontMetrics;
546 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
547 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
548 const float descender = ceil( fabsf( fontMetrics.descender ) );
550 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
552 currentUnderlineThickness = fontMetrics.underlineThickness;
554 // Ensure underline will be at least a pixel high
555 if ( currentUnderlineThickness < 1.0f )
557 currentUnderlineThickness = 1.0f;
561 currentUnderlineThickness = ceil( currentUnderlineThickness );
565 // The underline thickness should be the max underline thickness of all glyphs of the line.
566 if ( currentUnderlineThickness > maxUnderlineThickness )
568 maxUnderlineThickness = currentUnderlineThickness;
571 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
572 if( currentUnderlinePosition > descender )
574 currentUnderlinePosition = descender;
577 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
579 // Move offset down by one ( EFL behavior )
580 currentUnderlinePosition = 1.0f;
583 lastUnderlinedFontId = glyphInfo->fontId;
586 // Retrieves the glyph's position.
587 const Vector2* const position = positionBuffer + glyphIndex;
588 if ( baseline < position->y + glyphInfo->yBearing )
590 baseline = position->y + glyphInfo->yBearing;
593 // Calculate the positions of leftmost and rightmost glyphs in the current line
594 if ( position->x < lineExtentLeft)
596 lineExtentLeft = position->x;
599 if ( position->x + glyphInfo->width > lineExtentRight)
601 lineExtentRight = position->x + glyphInfo->width;
604 // Retrieves the glyph's color.
605 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
608 if ( style == Typesetter::STYLE_SHADOW )
610 color = mModel->GetShadowColor();
612 else if ( style == Typesetter::STYLE_OUTLINE )
614 color = mModel->GetOutlineColor();
618 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + ( colorIndex - 1u ) );
626 // Retrieves the glyph's bitmap.
627 glyphData.glyphBitmap.buffer = NULL;
628 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
629 glyphData.glyphBitmap.height = glyphInfo->height;
631 if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
633 // Don't render outline for other styles
636 if( style != Typesetter::STYLE_UNDERLINE )
638 fontClient.CreateBitmap( glyphInfo->fontId,
640 glyphInfo->softwareItalic,
641 glyphInfo->softwareBold,
642 glyphData.glyphBitmap,
647 // Sets the glyph's bitmap into the bitmap of the whole text.
648 if( NULL != glyphData.glyphBitmap.buffer )
650 TypesetGlyph( glyphData,
655 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
656 delete []glyphData.glyphBitmap.buffer;
657 glyphData.glyphBitmap.buffer = NULL;
661 // Draw the underline from the leftmost glyph to the rightmost glyph
662 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
664 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
666 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
668 if( y > bufferHeight - 1 )
670 // Do not write out of bounds.
674 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
676 if( x > bufferWidth - 1 )
678 // Do not write out of bounds.
682 // Always RGBA image for text with styles
683 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
684 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
685 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
687 // Write the underline color to the pixel buffer
688 uint8_t colorAlpha = static_cast< uint8_t >( underlineColor.a * 255.f );
689 *( underlinePixelBuffer + 3u ) = colorAlpha;
690 *( underlinePixelBuffer + 2u ) = static_cast< uint8_t >( underlineColor.b * colorAlpha );
691 *( underlinePixelBuffer + 1u ) = static_cast< uint8_t >( underlineColor.g * colorAlpha );
692 *( underlinePixelBuffer ) = static_cast< uint8_t >( underlineColor.r * colorAlpha );
694 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
699 // Increases the vertical offset with the line's descender.
700 glyphData.verticalOffset += static_cast<int>( -line.descender );
703 return glyphData.bitmapBuffer;
706 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
708 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
709 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
711 Devel::PixelBuffer combinedPixelBuffer;
713 if ( topBuffer == NULL && bottomBuffer == NULL )
715 // Nothing to do if both buffers are empty.
716 return combinedPixelBuffer;
719 if ( topBuffer == NULL )
721 // Nothing to do if topBuffer is empty.
722 return bottomPixelBuffer;
725 if ( bottomBuffer == NULL )
727 // Nothing to do if bottomBuffer is empty.
728 return topPixelBuffer;
731 // Always combine two RGBA images
732 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
733 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
735 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
736 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
737 memset( combinedBuffer, 0u, bufferSizeChar );
739 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
741 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
742 // Otherwise, copy pixel from topBuffer to combinedBuffer.
744 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
746 if ( alphaBuffer1 != 255 )
748 // At least one pixel is not fully opaque
749 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
750 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4] + ( bottomBuffer[pixelIndex*4] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
751 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1] + ( bottomBuffer[pixelIndex*4+1] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
752 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2] + ( bottomBuffer[pixelIndex*4+2] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
753 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
757 // Copy the pixel from topBuffer to combinedBuffer
758 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
759 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
760 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
761 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
765 return combinedPixelBuffer;
768 Typesetter::Typesetter( const ModelInterface* const model )
769 : mModel( new ViewModel( model ) )
773 Typesetter::~Typesetter()
780 } // namespace Toolkit