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>
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 // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
89 // The format is RGBA8888.
90 uint32_t packedColor = 0u;
91 uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
92 *( packedColorBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
93 *( packedColorBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
94 *packedColorBuffer = static_cast<uint8_t>( color->r * 255.f );
96 // Initial vertical offset.
97 const int yOffset = data.verticalOffset + position->y;
99 // Traverse the pixels of the glyph line per line.
100 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
102 const int yOffsetIndex = yOffset + lineIndex;
103 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
105 // Do not write out of bounds.
109 const int verticalOffset = yOffsetIndex * data.width;
110 const int xOffset = data.horizontalOffset + position->x;
111 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
112 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
114 const int xOffsetIndex = xOffset + index;
115 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
117 // Don't write out of bounds.
121 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
125 // Retrieves the color from the color glyph. The format is BGRA8888.
126 uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
127 uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
129 if( Typesetter::STYLE_SHADOW == style )
131 // The shadow of color glyph needs to have the shadow color.
132 *( packedColorGlyphBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
133 *( packedColorGlyphBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
134 *packedColorGlyphBuffer = static_cast<uint8_t>( color->r * 255.f );
138 std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
141 // Update the alpha channel.
142 if( Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style ) // Outline not shown for color glyph
144 // Create an alpha mask for color glyph.
145 *( packedColorGlyphBuffer + 3u ) = 0u;
149 *( packedColorGlyphBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
152 // Set the color into the final pixel buffer.
153 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
157 // Update the alpha channel.
158 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
160 // Copy non-transparent pixels only
163 // Check alpha of overlapped pixels
164 uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
165 uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( ¤tColor );
167 uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
168 uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
170 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
171 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
172 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
173 // happen, for example, in the RTL text when we copy glyphs from right to left).
174 *( packedColorBuffer + 3u ) = std::max( currentAlpha, newAlpha );
176 // Set the color into the final pixel buffer.
177 currentColor = packedColor;
185 // Whether the given glyph is a color one.
186 const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
188 // Initial vertical offset.
189 const int yOffset = data.verticalOffset + position->y;
191 // Traverse the pixels of the glyph line per line.
192 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
194 const int yOffsetIndex = yOffset + lineIndex;
195 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
197 // Do not write out of bounds.
201 const int verticalOffset = yOffsetIndex * data.width;
202 const int xOffset = data.horizontalOffset + position->x;
203 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
204 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
206 const int xOffsetIndex = xOffset + index;
207 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
209 // Don't write out of bounds.
213 uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
217 // Update the alpha channel.
218 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
220 // Copy non-transparent pixels only
223 // Check alpha of overlapped pixels
224 uint8_t& currentAlpha = *( bitmapBuffer + verticalOffset + xOffsetIndex );
225 uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
227 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
228 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
229 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
230 // happen, for example, in the RTL text when we copy glyphs from right to left).
231 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = std::max( currentAlpha, newAlpha );
239 bool IsGlyphUnderlined( GlyphIndex index,
240 const Vector<GlyphRun>& underlineRuns )
242 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
243 endIt = underlineRuns.End();
247 const GlyphRun& run = *it;
249 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
260 TypesetterPtr Typesetter::New( const ModelInterface* const model )
262 return TypesetterPtr( new Typesetter( model ) );
265 ViewModel* Typesetter::GetViewModel()
270 PixelData Typesetter::Render( const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
272 // @todo. This initial implementation for a TextLabel has only one visible page.
274 // Elides the text if needed.
275 mModel->ElideGlyphs();
277 // Retrieves the layout size.
278 const Size& layoutSize = mModel->GetLayoutSize();
280 const float outlineWidth = mModel->GetOutlineWidth();
282 // Set the offset for the horizontal alignment according to the text direction and outline width.
285 switch( mModel->GetHorizontalAlignment() )
287 case HorizontalAlignment::BEGIN:
292 case HorizontalAlignment::CENTER:
294 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth : outlineWidth;
297 case HorizontalAlignment::END:
299 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2.0f : outlineWidth * 2.0f;
304 // Set the offset for the vertical alignment.
307 switch( mModel->GetVerticalAlignment() )
309 case VerticalAlignment::TOP:
314 case VerticalAlignment::CENTER:
316 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
319 case VerticalAlignment::BOTTOM:
321 penY = static_cast<int>( size.height - layoutSize.height );
326 // Calculate vertical line alignment
327 switch( mModel->GetVerticalLineAlignment() )
329 case DevelText::VerticalLineAlignment::TOP:
333 case DevelText::VerticalLineAlignment::MIDDLE:
335 const auto& line = *mModel->GetLines();
336 penY -= line.descender;
337 penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
340 case DevelText::VerticalLineAlignment::BOTTOM:
342 const auto& line = *mModel->GetLines();
343 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
344 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
349 // Generate the image buffers of the text for each different style first,
350 // then combine all of them together as one final image buffer. We try to
351 // do all of these in CPU only, so that once the final texture is generated,
352 // no calculation is needed in GPU during each frame.
354 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
355 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
357 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
358 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
360 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
362 Devel::PixelBuffer imageBuffer;
364 if( RENDER_MASK == behaviour )
366 // Generate the image buffer as an alpha mask for color glyphs.
367 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
369 else if( RENDER_NO_TEXT == behaviour )
371 // Generate an empty image buffer so that it can been combined with the image buffers for styles
372 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
373 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
377 // Generate the image buffer for the text with no style.
378 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
381 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
384 // Generate the outline if enabled
385 const float outlineWidth = mModel->GetOutlineWidth();
386 if ( outlineWidth > Math::MACHINE_EPSILON_1 )
388 // Create the image buffer for outline
389 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
391 // Combine the two buffers
392 imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
395 // @todo. Support shadow and underline for partial text later on.
397 // Generate the shadow if enabled
398 const Vector2& shadowOffset = mModel->GetShadowOffset();
399 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
401 // Create the image buffer for shadow
402 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
404 // Check whether it will be a soft shadow
405 const float& blurRadius = mModel->GetShadowBlurRadius();
407 if ( blurRadius > Math::MACHINE_EPSILON_1 )
409 shadowImageBuffer.ApplyGaussianBlur( blurRadius );
412 // Combine the two buffers
413 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
416 // Generate the underline if enabled
417 const bool underlineEnabled = mModel->IsUnderlineEnabled();
418 if ( underlineEnabled )
420 // Create the image buffer for underline
421 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
423 // Combine the two buffers
424 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
428 // Create the final PixelData for the combined image buffer
429 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
434 Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex )
436 // Retrieve lines, glyphs, positions and colors from the view model.
437 const Length modelNumberOfLines = mModel->GetNumberOfLines();
438 const LineRun* const modelLinesBuffer = mModel->GetLines();
439 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
440 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
441 const Vector2* const positionBuffer = mModel->GetLayout();
442 const Vector4* const colorsBuffer = mModel->GetColors();
443 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
445 // Whether to use the default color.
446 const bool useDefaultColor = ( NULL == colorsBuffer );
447 const Vector4& defaultColor = mModel->GetDefaultColor();
449 // Create and initialize the pixel buffer.
451 glyphData.verticalOffset = verticalOffset;
452 glyphData.width = bufferWidth;
453 glyphData.height = bufferHeight;
454 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
455 glyphData.horizontalOffset = 0;
457 if ( Pixel::RGBA8888 == pixelFormat )
459 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
460 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
461 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
465 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
468 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
469 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
471 // Traverses the lines of the text.
472 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
474 const LineRun& line = *( modelLinesBuffer + lineIndex );
476 // Sets the horizontal offset of the line.
477 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
478 glyphData.horizontalOffset += horizontalOffset;
480 // Increases the vertical offset with the line's ascender.
481 glyphData.verticalOffset += static_cast<int>( line.ascender );
483 // Include line spacing after first line
486 glyphData.verticalOffset += static_cast<int>( line.lineSpacing );
489 // Retrieves the glyph's outline width
490 float outlineWidth = mModel->GetOutlineWidth();
492 if( style == Typesetter::STYLE_OUTLINE )
494 glyphData.horizontalOffset -= outlineWidth;
495 if( lineIndex == 0u )
497 // Only need to add the vertical outline offset for the first line
498 glyphData.verticalOffset -= outlineWidth;
501 else if ( style == Typesetter::STYLE_SHADOW )
503 const Vector2& shadowOffset = mModel->GetShadowOffset();
504 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
506 if ( lineIndex == 0u )
508 // Only need to add the vertical shadow offset for first line
509 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
513 const bool underlineEnabled = mModel->IsUnderlineEnabled();
514 const Vector4& underlineColor = mModel->GetUnderlineColor();
515 const float underlineHeight = mModel->GetUnderlineHeight();
517 // Get the underline runs.
518 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
519 Vector<GlyphRun> underlineRuns;
520 underlineRuns.Resize( numberOfUnderlineRuns );
521 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
523 bool thereAreUnderlinedGlyphs = false;
525 float currentUnderlinePosition = 0.0f;
526 float currentUnderlineThickness = underlineHeight;
527 float maxUnderlineThickness = currentUnderlineThickness;
529 FontId lastUnderlinedFontId = 0;
531 float lineExtentLeft = bufferWidth;
532 float lineExtentRight = 0.0f;
533 float baseline = 0.0f;
535 // Traverses the glyphs of the line.
536 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
537 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
539 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
541 // Ignore any glyph that out of the specified range
545 // Retrieve the glyph's info.
546 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
548 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
549 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
551 // Nothing to do if the glyph's width or height is zero.
555 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
556 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
558 // Are we still using the same fontId as previous
559 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
561 // We need to fetch fresh font underline metrics
562 FontMetrics fontMetrics;
563 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
564 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
565 const float descender = ceil( fabsf( fontMetrics.descender ) );
567 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
569 currentUnderlineThickness = fontMetrics.underlineThickness;
571 // Ensure underline will be at least a pixel high
572 if ( currentUnderlineThickness < 1.0f )
574 currentUnderlineThickness = 1.0f;
578 currentUnderlineThickness = ceil( currentUnderlineThickness );
582 // The underline thickness should be the max underline thickness of all glyphs of the line.
583 if ( currentUnderlineThickness > maxUnderlineThickness )
585 maxUnderlineThickness = currentUnderlineThickness;
588 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
589 if( currentUnderlinePosition > descender )
591 currentUnderlinePosition = descender;
594 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
596 // Move offset down by one ( EFL behavior )
597 currentUnderlinePosition = 1.0f;
600 lastUnderlinedFontId = glyphInfo->fontId;
603 // Retrieves the glyph's position.
604 const Vector2* const position = positionBuffer + glyphIndex;
605 if ( baseline < position->y + glyphInfo->yBearing )
607 baseline = position->y + glyphInfo->yBearing;
610 // Calculate the positions of leftmost and rightmost glyphs in the current line
611 if ( position->x < lineExtentLeft)
613 lineExtentLeft = position->x;
616 if ( position->x + glyphInfo->width > lineExtentRight)
618 lineExtentRight = position->x + glyphInfo->width;
621 // Retrieves the glyph's color.
622 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
624 const Vector4* color;
625 if ( style == Typesetter::STYLE_SHADOW )
627 color = &( mModel->GetShadowColor() );
629 else if ( style == Typesetter::STYLE_OUTLINE )
631 color = &( mModel->GetOutlineColor() );
635 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
638 // Retrieves the glyph's bitmap.
639 glyphData.glyphBitmap.buffer = NULL;
640 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
641 glyphData.glyphBitmap.height = glyphInfo->height;
643 if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
645 // Don't render outline for other styles
648 if( style != Typesetter::STYLE_UNDERLINE )
650 fontClient.CreateBitmap( glyphInfo->fontId,
652 glyphData.glyphBitmap,
657 // Sets the glyph's bitmap into the bitmap of the whole text.
658 if( NULL != glyphData.glyphBitmap.buffer )
660 TypesetGlyph( glyphData,
665 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
666 delete []glyphData.glyphBitmap.buffer;
667 glyphData.glyphBitmap.buffer = NULL;
671 // Draw the underline from the leftmost glyph to the rightmost glyph
672 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
674 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
676 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
678 if( y > bufferHeight - 1 )
680 // Do not write out of bounds.
684 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
686 if( x > bufferWidth - 1 )
688 // Do not write out of bounds.
692 // Always RGBA image for text with styles
693 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
694 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
695 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
697 // Write the underline color to the pixel buffer
698 *( underlinePixelBuffer ) = static_cast<uint8_t>( underlineColor.r * 255.f );
699 *( underlinePixelBuffer + 1u ) = static_cast<uint8_t>( underlineColor.g * 255.f );
700 *( underlinePixelBuffer + 2u ) = static_cast<uint8_t>( underlineColor.b * 255.f );
701 *( underlinePixelBuffer + 3u ) = static_cast<uint8_t>( underlineColor.a * 255.f );
703 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
708 // Increases the vertical offset with the line's descender.
709 glyphData.verticalOffset += static_cast<int>( -line.descender );
712 return glyphData.bitmapBuffer;
715 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
717 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
718 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
720 Devel::PixelBuffer combinedPixelBuffer;
722 if ( topBuffer == NULL && bottomBuffer == NULL )
724 // Nothing to do if both buffers are empty.
725 return combinedPixelBuffer;
728 if ( topBuffer == NULL )
730 // Nothing to do if topBuffer is empty.
731 return bottomPixelBuffer;
734 if ( bottomBuffer == NULL )
736 // Nothing to do if bottomBuffer is empty.
737 return topPixelBuffer;
740 // Always combine two RGBA images
741 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
742 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
744 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
745 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
746 memset( combinedBuffer, 0u, bufferSizeChar );
748 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
750 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
751 // Otherwise, copy pixel from topBuffer to combinedBuffer.
753 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
754 unsigned int alphaBuffer2 = bottomBuffer[pixelIndex*4+3];
756 if ( alphaBuffer1 != 255 || alphaBuffer2 != 255 )
758 // At least one pixel is not fully opaque
759 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
760 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 ) );
761 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 ) );
762 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 ) );
763 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
767 // Copy the pixel from topBuffer to combinedBuffer
768 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
769 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
770 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
771 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
775 return combinedPixelBuffer;
778 Typesetter::Typesetter( const ModelInterface* const model )
779 : mModel( new ViewModel( model ) )
783 Typesetter::~Typesetter()
790 } // namespace Toolkit