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, Toolkit::DevelText::TextDirection::Type textDirection, 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 const float outlineWidth = mModel->GetOutlineWidth();
290 // Set the offset for the horizontal alignment according to the text direction and outline width.
293 switch( mModel->GetHorizontalAlignment() )
295 case HorizontalAlignment::BEGIN:
300 case HorizontalAlignment::CENTER:
302 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth : outlineWidth;
305 case HorizontalAlignment::END:
307 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2.0f : outlineWidth * 2.0f;
312 // Set the offset for the vertical alignment.
315 switch( mModel->GetVerticalAlignment() )
317 case VerticalAlignment::TOP:
322 case VerticalAlignment::CENTER:
324 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
327 case VerticalAlignment::BOTTOM:
329 penY = static_cast<int>( size.height - layoutSize.height );
334 // Calculate vertical line alignment
335 switch( mModel->GetVerticalLineAlignment() )
337 case DevelText::VerticalLineAlignment::TOP:
341 case DevelText::VerticalLineAlignment::MIDDLE:
343 const auto& line = *mModel->GetLines();
344 penY -= line.descender;
345 penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
348 case DevelText::VerticalLineAlignment::BOTTOM:
350 const auto& line = *mModel->GetLines();
351 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
352 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
357 // Generate the image buffers of the text for each different style first,
358 // then combine all of them together as one final image buffer. We try to
359 // do all of these in CPU only, so that once the final texture is generated,
360 // no calculation is needed in GPU during each frame.
362 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
363 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
365 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
366 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
368 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
370 Devel::PixelBuffer imageBuffer;
372 if( RENDER_MASK == behaviour )
374 // Generate the image buffer as an alpha mask for color glyphs.
375 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
377 else if( RENDER_NO_TEXT == behaviour )
379 // Generate an empty image buffer so that it can been combined with the image buffers for styles
380 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
381 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
385 // Generate the image buffer for the text with no style.
386 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
389 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
392 // Generate the outline if enabled
393 const float outlineWidth = mModel->GetOutlineWidth();
394 if ( outlineWidth > Math::MACHINE_EPSILON_1 )
396 // Create the image buffer for outline
397 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
399 // Combine the two buffers
400 imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
403 // @todo. Support shadow and underline for partial text later on.
405 // Generate the shadow if enabled
406 const Vector2& shadowOffset = mModel->GetShadowOffset();
407 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
409 // Create the image buffer for shadow
410 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
412 // Check whether it will be a soft shadow
413 const float& blurRadius = mModel->GetShadowBlurRadius();
415 if ( blurRadius > Math::MACHINE_EPSILON_1 )
417 shadowImageBuffer.ApplyGaussianBlur( blurRadius );
420 // Combine the two buffers
421 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
424 // Generate the underline if enabled
425 const bool underlineEnabled = mModel->IsUnderlineEnabled();
426 if ( underlineEnabled )
428 // Create the image buffer for underline
429 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
431 // Combine the two buffers
432 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
435 // Generate the background if enabled
436 const bool backgroundEnabled = mModel->IsBackgroundEnabled();
437 if ( backgroundEnabled )
439 Devel::PixelBuffer backgroundImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
441 // Combine the two buffers
442 imageBuffer = CombineImageBuffer( imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight );
446 // Create the final PixelData for the combined image buffer
447 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
452 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 )
454 // Retrieve lines, glyphs, positions and colors from the view model.
455 const Length modelNumberOfLines = mModel->GetNumberOfLines();
456 const LineRun* const modelLinesBuffer = mModel->GetLines();
457 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
458 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
459 const Vector2* const positionBuffer = mModel->GetLayout();
460 const Vector4* const colorsBuffer = mModel->GetColors();
461 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
463 // Whether to use the default color.
464 const bool useDefaultColor = ( NULL == colorsBuffer );
465 const Vector4& defaultColor = mModel->GetDefaultColor();
467 // Create and initialize the pixel buffer.
469 glyphData.verticalOffset = verticalOffset;
470 glyphData.width = bufferWidth;
471 glyphData.height = bufferHeight;
472 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
473 glyphData.horizontalOffset = 0;
475 if ( Pixel::RGBA8888 == pixelFormat )
477 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
478 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
479 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
483 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
486 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
487 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
489 // Traverses the lines of the text.
490 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
492 const LineRun& line = *( modelLinesBuffer + lineIndex );
494 // Sets the horizontal offset of the line.
495 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
496 glyphData.horizontalOffset += horizontalOffset;
498 // Increases the vertical offset with the line's ascender.
499 glyphData.verticalOffset += static_cast<int>( line.ascender );
501 // Include line spacing after first line
504 glyphData.verticalOffset += static_cast<int>( line.lineSpacing );
507 // Retrieves the glyph's outline width
508 float outlineWidth = mModel->GetOutlineWidth();
510 if( style == Typesetter::STYLE_OUTLINE )
512 glyphData.horizontalOffset -= outlineWidth;
513 if( lineIndex == 0u )
515 // Only need to add the vertical outline offset for the first line
516 glyphData.verticalOffset -= outlineWidth;
519 else if ( style == Typesetter::STYLE_SHADOW )
521 const Vector2& shadowOffset = mModel->GetShadowOffset();
522 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
524 if ( lineIndex == 0u )
526 // Only need to add the vertical shadow offset for first line
527 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
531 const bool underlineEnabled = mModel->IsUnderlineEnabled();
532 const Vector4& underlineColor = mModel->GetUnderlineColor();
533 const float underlineHeight = mModel->GetUnderlineHeight();
535 // Get the underline runs.
536 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
537 Vector<GlyphRun> underlineRuns;
538 underlineRuns.Resize( numberOfUnderlineRuns );
539 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
541 bool thereAreUnderlinedGlyphs = false;
543 float currentUnderlinePosition = 0.0f;
544 float currentUnderlineThickness = underlineHeight;
545 float maxUnderlineThickness = currentUnderlineThickness;
547 FontId lastUnderlinedFontId = 0;
549 float lineExtentLeft = bufferWidth;
550 float lineExtentRight = 0.0f;
551 float baseline = 0.0f;
553 // Traverses the glyphs of the line.
554 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
555 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
557 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
559 // Ignore any glyph that out of the specified range
563 // Retrieve the glyph's info.
564 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
566 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
567 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
569 // Nothing to do if the glyph's width or height is zero.
573 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
574 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
576 // Are we still using the same fontId as previous
577 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
579 // We need to fetch fresh font underline metrics
580 FontMetrics fontMetrics;
581 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
582 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
583 const float descender = ceil( fabsf( fontMetrics.descender ) );
585 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
587 currentUnderlineThickness = fontMetrics.underlineThickness;
589 // Ensure underline will be at least a pixel high
590 if ( currentUnderlineThickness < 1.0f )
592 currentUnderlineThickness = 1.0f;
596 currentUnderlineThickness = ceil( currentUnderlineThickness );
600 // The underline thickness should be the max underline thickness of all glyphs of the line.
601 if ( currentUnderlineThickness > maxUnderlineThickness )
603 maxUnderlineThickness = currentUnderlineThickness;
606 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
607 if( currentUnderlinePosition > descender )
609 currentUnderlinePosition = descender;
612 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
614 // Move offset down by one ( EFL behavior )
615 currentUnderlinePosition = 1.0f;
618 lastUnderlinedFontId = glyphInfo->fontId;
621 // Retrieves the glyph's position.
622 const Vector2* const position = positionBuffer + glyphIndex;
623 if ( baseline < position->y + glyphInfo->yBearing )
625 baseline = position->y + glyphInfo->yBearing;
628 // Calculate the positions of leftmost and rightmost glyphs in the current line
629 if ( position->x < lineExtentLeft)
631 lineExtentLeft = position->x;
634 if ( position->x + glyphInfo->width > lineExtentRight)
636 lineExtentRight = position->x + glyphInfo->width;
639 // Retrieves the glyph's color.
640 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
643 if ( style == Typesetter::STYLE_SHADOW )
645 color = mModel->GetShadowColor();
647 else if ( style == Typesetter::STYLE_OUTLINE )
649 color = mModel->GetOutlineColor();
653 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + ( colorIndex - 1u ) );
661 // Retrieves the glyph's bitmap.
662 glyphData.glyphBitmap.buffer = NULL;
663 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
664 glyphData.glyphBitmap.height = glyphInfo->height;
666 if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
668 // Don't render outline for other styles
671 if( style != Typesetter::STYLE_UNDERLINE )
673 fontClient.CreateBitmap( glyphInfo->fontId,
675 glyphInfo->softwareItalic,
676 glyphInfo->softwareBold,
677 glyphData.glyphBitmap,
682 // Sets the glyph's bitmap into the bitmap of the whole text.
683 if( NULL != glyphData.glyphBitmap.buffer )
685 TypesetGlyph( glyphData,
690 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
691 delete []glyphData.glyphBitmap.buffer;
692 glyphData.glyphBitmap.buffer = NULL;
696 // Draw the underline from the leftmost glyph to the rightmost glyph
697 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
699 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
701 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
703 if( y > bufferHeight - 1 )
705 // Do not write out of bounds.
709 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
711 if( x > bufferWidth - 1 )
713 // Do not write out of bounds.
717 // Always RGBA image for text with styles
718 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
719 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
720 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
722 // Write the underline color to the pixel buffer
723 uint8_t colorAlpha = static_cast< uint8_t >( underlineColor.a * 255.f );
724 *( underlinePixelBuffer + 3u ) = colorAlpha;
725 *( underlinePixelBuffer + 2u ) = static_cast< uint8_t >( underlineColor.b * colorAlpha );
726 *( underlinePixelBuffer + 1u ) = static_cast< uint8_t >( underlineColor.g * colorAlpha );
727 *( underlinePixelBuffer ) = static_cast< uint8_t >( underlineColor.r * colorAlpha );
729 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
734 // Draw the background color from the leftmost glyph to the rightmost glyph
735 if ( style == Typesetter::STYLE_BACKGROUND )
737 Vector4 backgroundColor = mModel->GetBackgroundColor();
739 for( int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++ )
741 if( ( y < 0 ) || ( y > static_cast<int>(bufferHeight - 1) ) )
743 // Do not write out of bounds.
747 for( int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
749 if( ( x < 0 ) || ( x > static_cast<int>(bufferWidth - 1) ) )
751 // Do not write out of bounds.
755 // Always RGBA image for text with styles
756 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
757 uint32_t backgroundPixel = *( bitmapBuffer + y * glyphData.width + x );
758 uint8_t* backgroundPixelBuffer = reinterpret_cast<uint8_t*>( &backgroundPixel );
760 // Write the background color to the pixel buffer
761 uint8_t colorAlpha = static_cast< uint8_t >( backgroundColor.a * 255.f );
762 *( backgroundPixelBuffer + 3u ) = colorAlpha;
763 *( backgroundPixelBuffer + 2u ) = static_cast< uint8_t >( backgroundColor.b * colorAlpha );
764 *( backgroundPixelBuffer + 1u ) = static_cast< uint8_t >( backgroundColor.g * colorAlpha );
765 *( backgroundPixelBuffer ) = static_cast< uint8_t >( backgroundColor.r * colorAlpha );
767 *( bitmapBuffer + y * glyphData.width + x ) = backgroundPixel;
772 // Increases the vertical offset with the line's descender.
773 glyphData.verticalOffset += static_cast<int>( -line.descender );
776 return glyphData.bitmapBuffer;
779 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
781 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
782 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
784 Devel::PixelBuffer combinedPixelBuffer;
786 if ( topBuffer == NULL && bottomBuffer == NULL )
788 // Nothing to do if both buffers are empty.
789 return combinedPixelBuffer;
792 if ( topBuffer == NULL )
794 // Nothing to do if topBuffer is empty.
795 return bottomPixelBuffer;
798 if ( bottomBuffer == NULL )
800 // Nothing to do if bottomBuffer is empty.
801 return topPixelBuffer;
804 // Always combine two RGBA images
805 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
806 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
808 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
809 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
810 memset( combinedBuffer, 0u, bufferSizeChar );
812 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
814 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
815 // Otherwise, copy pixel from topBuffer to combinedBuffer.
817 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
819 if ( alphaBuffer1 != 255 )
821 // At least one pixel is not fully opaque
822 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
823 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4] + ( bottomBuffer[pixelIndex*4] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
824 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1] + ( bottomBuffer[pixelIndex*4+1] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
825 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2] + ( bottomBuffer[pixelIndex*4+2] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
826 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
830 // Copy the pixel from topBuffer to combinedBuffer
831 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
832 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
833 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
834 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
838 return combinedPixelBuffer;
841 Typesetter::Typesetter( const ModelInterface* const model )
842 : mModel( new ViewModel( model ) )
846 Typesetter::~Typesetter()
853 } // namespace Toolkit