2 * Copyright (c) 2019 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 = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap;
84 const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel( data.glyphBitmap.format );
85 const uint32_t alphaIndex = glyphPixelSize - 1u;
86 const bool swapChannelsBR = Pixel::BGRA8888 == data.glyphBitmap.format;
88 // Pointer to the color glyph if there is one.
89 const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>( data.glyphBitmap.buffer ) : NULL;
91 // Initial vertical offset.
92 const int yOffset = data.verticalOffset + position->y;
94 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
96 // Traverse the pixels of the glyph line per line.
97 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
99 const int yOffsetIndex = yOffset + lineIndex;
100 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
102 // Do not write out of bounds.
106 const int verticalOffset = yOffsetIndex * data.width;
107 const int xOffset = data.horizontalOffset + position->x;
108 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
109 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
111 const int xOffsetIndex = xOffset + index;
112 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
114 // Don't write out of bounds.
120 // Retrieves the color from the color glyph.
121 uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
122 uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
124 // Update the alpha channel.
125 if( Typesetter::STYLE_MASK == style || Typesetter::STYLE_OUTLINE == style ) // Outline not shown for color glyph
127 // Create an alpha mask for color glyph.
128 *( packedColorGlyphBuffer + 3u ) = 0u;
129 *( packedColorGlyphBuffer + 2u ) = 0u;
130 *( packedColorGlyphBuffer + 1u ) = 0u;
131 *packedColorGlyphBuffer = 0u;
135 const uint8_t colorAlpha = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
136 *( packedColorGlyphBuffer + 3u ) = colorAlpha;
138 if( Typesetter::STYLE_SHADOW == style )
140 // The shadow of color glyph needs to have the shadow color.
141 *( packedColorGlyphBuffer + 2u ) = static_cast<uint8_t>( color->b * colorAlpha );
142 *( packedColorGlyphBuffer + 1u ) = static_cast<uint8_t>( color->g * colorAlpha );
143 *packedColorGlyphBuffer = static_cast<uint8_t>( color->r * colorAlpha );
149 std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
152 *( packedColorGlyphBuffer + 2u ) = ( *( packedColorGlyphBuffer + 2u ) * colorAlpha / 255 );
153 *( packedColorGlyphBuffer + 1u ) = ( *( packedColorGlyphBuffer + 1u ) * colorAlpha / 255 );
154 *packedColorGlyphBuffer = ( *( packedColorGlyphBuffer ) * colorAlpha / 255 );
156 if( data.glyphBitmap.isColorBitmap )
158 *( packedColorGlyphBuffer + 2u ) = static_cast<uint8_t>( *( packedColorGlyphBuffer + 2u ) * color->b );
159 *( packedColorGlyphBuffer + 1u ) = static_cast<uint8_t>( *( packedColorGlyphBuffer + 1u ) * color->g );
160 *packedColorGlyphBuffer = static_cast<uint8_t>( *packedColorGlyphBuffer * color->r );
165 // Set the color into the final pixel buffer.
166 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
170 // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
171 // The format is RGBA8888.
172 uint32_t packedColor = 0u;
173 uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
175 // Update the alpha channel.
176 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphPixelSize * ( glyphBufferOffset + index ) + alphaIndex );
178 // Copy non-transparent pixels only
181 // Check alpha of overlapped pixels
182 uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
183 uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( ¤tColor );
185 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
186 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
187 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
188 // happen, for example, in the RTL text when we copy glyphs from right to left).
189 uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
190 currentAlpha = std::max( currentAlpha, alpha );
192 // Color is pre-muliplied with its alpha.
193 *( packedColorBuffer + 3u ) = static_cast<uint8_t>( color->a * currentAlpha );
194 *( packedColorBuffer + 2u ) = static_cast<uint8_t>( color->b * currentAlpha );
195 *( packedColorBuffer + 1u ) = static_cast<uint8_t>( color->g * currentAlpha );
196 *( packedColorBuffer ) = static_cast<uint8_t>( color->r * currentAlpha );
198 // Set the color into the final pixel buffer.
199 currentColor = packedColor;
207 // Whether the given glyph is a color one.
208 const bool isColorGlyph = data.glyphBitmap.isColorEmoji || data.glyphBitmap.isColorBitmap;
209 const uint32_t glyphPixelSize = Pixel::GetBytesPerPixel( data.glyphBitmap.format );
210 const uint32_t alphaIndex = glyphPixelSize - 1u;
212 // Initial vertical offset.
213 const int yOffset = data.verticalOffset + position->y;
215 uint8_t* bitmapBuffer = reinterpret_cast< uint8_t* >( data.bitmapBuffer.GetBuffer() );
217 // Traverse the pixels of the glyph line per line.
218 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
220 const int yOffsetIndex = yOffset + lineIndex;
221 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
223 // Do not write out of bounds.
227 const int verticalOffset = yOffsetIndex * data.width;
228 const int xOffset = data.horizontalOffset + position->x;
229 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
230 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
232 const int xOffsetIndex = xOffset + index;
233 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
235 // Don't write out of bounds.
241 // Update the alpha channel.
242 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphPixelSize * ( glyphBufferOffset + index ) + alphaIndex );
244 // Copy non-transparent pixels only
247 // Check alpha of overlapped pixels
248 uint8_t& currentAlpha = *( bitmapBuffer + verticalOffset + xOffsetIndex );
250 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
251 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
252 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
253 // happen, for example, in the RTL text when we copy glyphs from right to left).
254 currentAlpha = std::max( currentAlpha, alpha );
262 bool IsGlyphUnderlined( GlyphIndex index,
263 const Vector<GlyphRun>& underlineRuns )
265 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
266 endIt = underlineRuns.End();
270 const GlyphRun& run = *it;
272 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
283 TypesetterPtr Typesetter::New( const ModelInterface* const model )
285 return TypesetterPtr( new Typesetter( model ) );
288 ViewModel* Typesetter::GetViewModel()
293 PixelData Typesetter::Render( const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
295 // @todo. This initial implementation for a TextLabel has only one visible page.
297 // Elides the text if needed.
298 mModel->ElideGlyphs();
300 // Retrieves the layout size.
301 const Size& layoutSize = mModel->GetLayoutSize();
303 const int outlineWidth = static_cast<int>( mModel->GetOutlineWidth() );
305 // Set the offset for the horizontal alignment according to the text direction and outline width.
308 switch( mModel->GetHorizontalAlignment() )
310 case HorizontalAlignment::BEGIN:
315 case HorizontalAlignment::CENTER:
317 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth : outlineWidth;
320 case HorizontalAlignment::END:
322 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2 : outlineWidth * 2;
327 // Set the offset for the vertical alignment.
330 switch( mModel->GetVerticalAlignment() )
332 case VerticalAlignment::TOP:
337 case VerticalAlignment::CENTER:
339 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
340 penY = penY < 0.f ? 0.f : penY;
343 case VerticalAlignment::BOTTOM:
345 penY = static_cast<int>( size.height - layoutSize.height );
350 // Calculate vertical line alignment
351 switch( mModel->GetVerticalLineAlignment() )
353 case DevelText::VerticalLineAlignment::TOP:
357 case DevelText::VerticalLineAlignment::MIDDLE:
359 const auto& line = *mModel->GetLines();
360 penY -= line.descender;
361 penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
364 case DevelText::VerticalLineAlignment::BOTTOM:
366 const auto& line = *mModel->GetLines();
367 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
368 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
373 // Generate the image buffers of the text for each different style first,
374 // then combine all of them together as one final image buffer. We try to
375 // do all of these in CPU only, so that once the final texture is generated,
376 // no calculation is needed in GPU during each frame.
378 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
379 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
381 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
382 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
384 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
386 Devel::PixelBuffer imageBuffer;
388 if( RENDER_MASK == behaviour )
390 // Generate the image buffer as an alpha mask for color glyphs.
391 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
393 else if( RENDER_NO_TEXT == behaviour )
395 // Generate an empty image buffer so that it can been combined with the image buffers for styles
396 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
397 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
401 // Generate the image buffer for the text with no style.
402 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
405 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
408 // Generate the outline if enabled
409 const uint16_t outlineWidth = mModel->GetOutlineWidth();
410 if ( outlineWidth != 0u )
412 // Create the image buffer for outline
413 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
415 // Combine the two buffers
416 imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
419 // @todo. Support shadow and underline for partial text later on.
421 // Generate the shadow if enabled
422 const Vector2& shadowOffset = mModel->GetShadowOffset();
423 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
425 // Create the image buffer for shadow
426 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
428 // Check whether it will be a soft shadow
429 const float& blurRadius = mModel->GetShadowBlurRadius();
431 if ( blurRadius > Math::MACHINE_EPSILON_1 )
433 shadowImageBuffer.ApplyGaussianBlur( blurRadius );
436 // Combine the two buffers
437 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
440 // Generate the underline if enabled
441 const bool underlineEnabled = mModel->IsUnderlineEnabled();
442 if ( underlineEnabled )
444 // Create the image buffer for underline
445 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
447 // Combine the two buffers
448 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
451 // Generate the background if enabled
452 const bool backgroundEnabled = mModel->IsBackgroundEnabled();
453 if ( backgroundEnabled )
455 Devel::PixelBuffer backgroundImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
457 // Combine the two buffers
458 imageBuffer = CombineImageBuffer( imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight );
462 // Create the final PixelData for the combined image buffer
463 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
468 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 )
470 // Retrieve lines, glyphs, positions and colors from the view model.
471 const Length modelNumberOfLines = mModel->GetNumberOfLines();
472 const LineRun* const modelLinesBuffer = mModel->GetLines();
473 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
474 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
475 const Vector2* const positionBuffer = mModel->GetLayout();
476 const Vector4* const colorsBuffer = mModel->GetColors();
477 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
479 // Whether to use the default color.
480 const bool useDefaultColor = ( NULL == colorsBuffer );
481 const Vector4& defaultColor = mModel->GetDefaultColor();
483 // Create and initialize the pixel buffer.
485 glyphData.verticalOffset = verticalOffset;
486 glyphData.width = bufferWidth;
487 glyphData.height = bufferHeight;
488 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
489 glyphData.horizontalOffset = 0;
491 if ( Pixel::RGBA8888 == pixelFormat )
493 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
494 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
495 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
499 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
502 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
503 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
505 // Traverses the lines of the text.
506 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
508 const LineRun& line = *( modelLinesBuffer + lineIndex );
510 // Sets the horizontal offset of the line.
511 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
512 glyphData.horizontalOffset += horizontalOffset;
514 // Increases the vertical offset with the line's ascender.
515 glyphData.verticalOffset += static_cast<int>( line.ascender );
517 // Include line spacing after first line
520 glyphData.verticalOffset += static_cast<int>( line.lineSpacing );
523 // Retrieves the glyph's outline width
524 float outlineWidth = static_cast<float>( mModel->GetOutlineWidth() );
526 if( style == Typesetter::STYLE_OUTLINE )
528 glyphData.horizontalOffset -= outlineWidth;
529 if( lineIndex == 0u )
531 // Only need to add the vertical outline offset for the first line
532 glyphData.verticalOffset -= outlineWidth;
535 else if ( style == Typesetter::STYLE_SHADOW )
537 const Vector2& shadowOffset = mModel->GetShadowOffset();
538 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
540 if ( lineIndex == 0u )
542 // Only need to add the vertical shadow offset for first line
543 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
547 const bool underlineEnabled = mModel->IsUnderlineEnabled();
548 const Vector4& underlineColor = mModel->GetUnderlineColor();
549 const float underlineHeight = mModel->GetUnderlineHeight();
551 // Get the underline runs.
552 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
553 Vector<GlyphRun> underlineRuns;
554 underlineRuns.Resize( numberOfUnderlineRuns );
555 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
557 bool thereAreUnderlinedGlyphs = false;
559 float currentUnderlinePosition = 0.0f;
560 float currentUnderlineThickness = underlineHeight;
561 float maxUnderlineThickness = currentUnderlineThickness;
563 FontId lastUnderlinedFontId = 0;
565 float lineExtentLeft = bufferWidth;
566 float lineExtentRight = 0.0f;
567 float baseline = 0.0f;
569 // Traverses the glyphs of the line.
570 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
571 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
573 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
575 // Ignore any glyph that out of the specified range
579 // Retrieve the glyph's info.
580 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
582 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
583 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
585 // Nothing to do if the glyph's width or height is zero.
589 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
590 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
592 // Are we still using the same fontId as previous
593 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
595 // We need to fetch fresh font underline metrics
596 FontMetrics fontMetrics;
597 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
598 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
599 const float descender = ceil( fabsf( fontMetrics.descender ) );
601 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
603 currentUnderlineThickness = fontMetrics.underlineThickness;
605 // Ensure underline will be at least a pixel high
606 if ( currentUnderlineThickness < 1.0f )
608 currentUnderlineThickness = 1.0f;
612 currentUnderlineThickness = ceil( currentUnderlineThickness );
616 // The underline thickness should be the max underline thickness of all glyphs of the line.
617 if ( currentUnderlineThickness > maxUnderlineThickness )
619 maxUnderlineThickness = currentUnderlineThickness;
622 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
623 if( currentUnderlinePosition > descender )
625 currentUnderlinePosition = descender;
628 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
630 // Move offset down by one ( EFL behavior )
631 currentUnderlinePosition = 1.0f;
634 lastUnderlinedFontId = glyphInfo->fontId;
637 // Retrieves the glyph's position.
638 const Vector2* const position = positionBuffer + glyphIndex;
639 if ( baseline < position->y + glyphInfo->yBearing )
641 baseline = position->y + glyphInfo->yBearing;
644 // Calculate the positions of leftmost and rightmost glyphs in the current line
645 if ( position->x < lineExtentLeft)
647 lineExtentLeft = position->x;
650 if ( position->x + glyphInfo->width > lineExtentRight)
652 lineExtentRight = position->x + glyphInfo->width;
655 // Retrieves the glyph's color.
656 const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndexBuffer + glyphIndex );
659 if ( style == Typesetter::STYLE_SHADOW )
661 color = mModel->GetShadowColor();
663 else if ( style == Typesetter::STYLE_OUTLINE )
665 color = mModel->GetOutlineColor();
669 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + ( colorIndex - 1u ) );
677 // Retrieves the glyph's bitmap.
678 glyphData.glyphBitmap.buffer = NULL;
679 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
680 glyphData.glyphBitmap.height = glyphInfo->height;
682 if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
684 // Don't render outline for other styles
688 if( style != Typesetter::STYLE_UNDERLINE )
690 fontClient.CreateBitmap( glyphInfo->fontId,
692 glyphInfo->isItalicRequired,
693 glyphInfo->isBoldRequired,
694 glyphData.glyphBitmap,
695 static_cast<int>( outlineWidth ) );
698 // Sets the glyph's bitmap into the bitmap of the whole text.
699 if( NULL != glyphData.glyphBitmap.buffer )
701 if ( style == Typesetter::STYLE_OUTLINE )
703 // Set the position offset for the current glyph
704 glyphData.horizontalOffset -= glyphData.glyphBitmap.outlineOffsetX;
705 glyphData.verticalOffset -= glyphData.glyphBitmap.outlineOffsetY;
708 // Set the buffer of the glyph's bitmap into the final bitmap's buffer
709 TypesetGlyph( glyphData,
715 if ( style == Typesetter::STYLE_OUTLINE )
717 // Reset the position offset for the next glyph
718 glyphData.horizontalOffset += glyphData.glyphBitmap.outlineOffsetX;
719 glyphData.verticalOffset += glyphData.glyphBitmap.outlineOffsetY;
722 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
723 delete []glyphData.glyphBitmap.buffer;
724 glyphData.glyphBitmap.buffer = NULL;
728 // Draw the underline from the leftmost glyph to the rightmost glyph
729 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
731 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
733 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
735 if( y > bufferHeight - 1 )
737 // Do not write out of bounds.
741 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
743 if( x > bufferWidth - 1 )
745 // Do not write out of bounds.
749 // Always RGBA image for text with styles
750 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
751 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
752 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
754 // Write the underline color to the pixel buffer
755 uint8_t colorAlpha = static_cast< uint8_t >( underlineColor.a * 255.f );
756 *( underlinePixelBuffer + 3u ) = colorAlpha;
757 *( underlinePixelBuffer + 2u ) = static_cast< uint8_t >( underlineColor.b * colorAlpha );
758 *( underlinePixelBuffer + 1u ) = static_cast< uint8_t >( underlineColor.g * colorAlpha );
759 *( underlinePixelBuffer ) = static_cast< uint8_t >( underlineColor.r * colorAlpha );
761 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
766 // Draw the background color from the leftmost glyph to the rightmost glyph
767 if ( style == Typesetter::STYLE_BACKGROUND )
769 Vector4 backgroundColor = mModel->GetBackgroundColor();
771 for( int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++ )
773 if( ( y < 0 ) || ( y > static_cast<int>(bufferHeight - 1) ) )
775 // Do not write out of bounds.
779 for( int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
781 if( ( x < 0 ) || ( x > static_cast<int>(bufferWidth - 1) ) )
783 // Do not write out of bounds.
787 // Always RGBA image for text with styles
788 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
789 uint32_t backgroundPixel = *( bitmapBuffer + y * glyphData.width + x );
790 uint8_t* backgroundPixelBuffer = reinterpret_cast<uint8_t*>( &backgroundPixel );
792 // Write the background color to the pixel buffer
793 uint8_t colorAlpha = static_cast< uint8_t >( backgroundColor.a * 255.f );
794 *( backgroundPixelBuffer + 3u ) = colorAlpha;
795 *( backgroundPixelBuffer + 2u ) = static_cast< uint8_t >( backgroundColor.b * colorAlpha );
796 *( backgroundPixelBuffer + 1u ) = static_cast< uint8_t >( backgroundColor.g * colorAlpha );
797 *( backgroundPixelBuffer ) = static_cast< uint8_t >( backgroundColor.r * colorAlpha );
799 *( bitmapBuffer + y * glyphData.width + x ) = backgroundPixel;
804 // Increases the vertical offset with the line's descender.
805 glyphData.verticalOffset += static_cast<int>( -line.descender );
808 return glyphData.bitmapBuffer;
811 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
813 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
814 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
816 Devel::PixelBuffer combinedPixelBuffer;
818 if ( topBuffer == NULL && bottomBuffer == NULL )
820 // Nothing to do if both buffers are empty.
821 return combinedPixelBuffer;
824 if ( topBuffer == NULL )
826 // Nothing to do if topBuffer is empty.
827 return bottomPixelBuffer;
830 if ( bottomBuffer == NULL )
832 // Nothing to do if bottomBuffer is empty.
833 return topPixelBuffer;
836 // Always combine two RGBA images
837 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
838 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
840 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
841 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
842 memset( combinedBuffer, 0u, bufferSizeChar );
844 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
846 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
847 // Otherwise, copy pixel from topBuffer to combinedBuffer.
849 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
851 if ( alphaBuffer1 != 255 )
853 // At least one pixel is not fully opaque
854 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
855 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4] + ( bottomBuffer[pixelIndex*4] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
856 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1] + ( bottomBuffer[pixelIndex*4+1] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
857 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2] + ( bottomBuffer[pixelIndex*4+2] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
858 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
862 // Copy the pixel from topBuffer to combinedBuffer
863 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
864 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
865 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
866 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
870 return combinedPixelBuffer;
873 Typesetter::Typesetter( const ModelInterface* const model )
874 : mModel( new ViewModel( model ) )
878 Typesetter::~Typesetter()
885 } // namespace Toolkit