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 ) );
342 case VerticalAlignment::BOTTOM:
344 penY = static_cast<int>( size.height - layoutSize.height );
349 // Calculate vertical line alignment
350 switch( mModel->GetVerticalLineAlignment() )
352 case DevelText::VerticalLineAlignment::TOP:
356 case DevelText::VerticalLineAlignment::MIDDLE:
358 const auto& line = *mModel->GetLines();
359 penY -= line.descender;
360 penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
363 case DevelText::VerticalLineAlignment::BOTTOM:
365 const auto& line = *mModel->GetLines();
366 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
367 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
372 // Generate the image buffers of the text for each different style first,
373 // then combine all of them together as one final image buffer. We try to
374 // do all of these in CPU only, so that once the final texture is generated,
375 // no calculation is needed in GPU during each frame.
377 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
378 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
380 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
381 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
383 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
385 Devel::PixelBuffer imageBuffer;
387 if( RENDER_MASK == behaviour )
389 // Generate the image buffer as an alpha mask for color glyphs.
390 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
392 else if( RENDER_NO_TEXT == behaviour )
394 // Generate an empty image buffer so that it can been combined with the image buffers for styles
395 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
396 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
400 // Generate the image buffer for the text with no style.
401 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
404 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
407 // Generate the outline if enabled
408 const uint16_t outlineWidth = mModel->GetOutlineWidth();
409 if ( outlineWidth != 0u )
411 // Create the image buffer for outline
412 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
414 // Combine the two buffers
415 imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
418 // @todo. Support shadow and underline for partial text later on.
420 // Generate the shadow if enabled
421 const Vector2& shadowOffset = mModel->GetShadowOffset();
422 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
424 // Create the image buffer for shadow
425 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
427 // Check whether it will be a soft shadow
428 const float& blurRadius = mModel->GetShadowBlurRadius();
430 if ( blurRadius > Math::MACHINE_EPSILON_1 )
432 shadowImageBuffer.ApplyGaussianBlur( blurRadius );
435 // Combine the two buffers
436 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
439 // Generate the underline if enabled
440 const bool underlineEnabled = mModel->IsUnderlineEnabled();
441 if ( underlineEnabled )
443 // Create the image buffer for underline
444 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
446 // Combine the two buffers
447 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
450 // Generate the background if enabled
451 const bool backgroundEnabled = mModel->IsBackgroundEnabled();
452 if ( backgroundEnabled )
454 Devel::PixelBuffer backgroundImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
456 // Combine the two buffers
457 imageBuffer = CombineImageBuffer( imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight );
461 // Create the final PixelData for the combined image buffer
462 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
467 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 )
469 // Retrieve lines, glyphs, positions and colors from the view model.
470 const Length modelNumberOfLines = mModel->GetNumberOfLines();
471 const LineRun* const modelLinesBuffer = mModel->GetLines();
472 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
473 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
474 const Vector2* const positionBuffer = mModel->GetLayout();
475 const Vector4* const colorsBuffer = mModel->GetColors();
476 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
478 // Whether to use the default color.
479 const bool useDefaultColor = ( NULL == colorsBuffer );
480 const Vector4& defaultColor = mModel->GetDefaultColor();
482 // Create and initialize the pixel buffer.
484 glyphData.verticalOffset = verticalOffset;
485 glyphData.width = bufferWidth;
486 glyphData.height = bufferHeight;
487 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
488 glyphData.horizontalOffset = 0;
490 if ( Pixel::RGBA8888 == pixelFormat )
492 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
493 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
494 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
498 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
501 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
502 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
504 // Traverses the lines of the text.
505 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
507 const LineRun& line = *( modelLinesBuffer + lineIndex );
509 // Sets the horizontal offset of the line.
510 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
511 glyphData.horizontalOffset += horizontalOffset;
513 // Increases the vertical offset with the line's ascender.
514 glyphData.verticalOffset += static_cast<int>( line.ascender );
516 // Include line spacing after first line
519 glyphData.verticalOffset += static_cast<int>( line.lineSpacing );
522 // Retrieves the glyph's outline width
523 float outlineWidth = static_cast<float>( mModel->GetOutlineWidth() );
525 if( style == Typesetter::STYLE_OUTLINE )
527 glyphData.horizontalOffset -= outlineWidth;
528 if( lineIndex == 0u )
530 // Only need to add the vertical outline offset for the first line
531 glyphData.verticalOffset -= outlineWidth;
534 else if ( style == Typesetter::STYLE_SHADOW )
536 const Vector2& shadowOffset = mModel->GetShadowOffset();
537 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
539 if ( lineIndex == 0u )
541 // Only need to add the vertical shadow offset for first line
542 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
546 const bool underlineEnabled = mModel->IsUnderlineEnabled();
547 const Vector4& underlineColor = mModel->GetUnderlineColor();
548 const float underlineHeight = mModel->GetUnderlineHeight();
550 // Get the underline runs.
551 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
552 Vector<GlyphRun> underlineRuns;
553 underlineRuns.Resize( numberOfUnderlineRuns );
554 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
556 bool thereAreUnderlinedGlyphs = false;
558 float currentUnderlinePosition = 0.0f;
559 float currentUnderlineThickness = underlineHeight;
560 float maxUnderlineThickness = currentUnderlineThickness;
562 FontId lastUnderlinedFontId = 0;
564 float lineExtentLeft = bufferWidth;
565 float lineExtentRight = 0.0f;
566 float baseline = 0.0f;
568 // Traverses the glyphs of the line.
569 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
570 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
572 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
574 // Ignore any glyph that out of the specified range
578 // Retrieve the glyph's info.
579 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
581 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
582 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
584 // Nothing to do if the glyph's width or height is zero.
588 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
589 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
591 // Are we still using the same fontId as previous
592 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
594 // We need to fetch fresh font underline metrics
595 FontMetrics fontMetrics;
596 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
597 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
598 const float descender = ceil( fabsf( fontMetrics.descender ) );
600 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
602 currentUnderlineThickness = fontMetrics.underlineThickness;
604 // Ensure underline will be at least a pixel high
605 if ( currentUnderlineThickness < 1.0f )
607 currentUnderlineThickness = 1.0f;
611 currentUnderlineThickness = ceil( currentUnderlineThickness );
615 // The underline thickness should be the max underline thickness of all glyphs of the line.
616 if ( currentUnderlineThickness > maxUnderlineThickness )
618 maxUnderlineThickness = currentUnderlineThickness;
621 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
622 if( currentUnderlinePosition > descender )
624 currentUnderlinePosition = descender;
627 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
629 // Move offset down by one ( EFL behavior )
630 currentUnderlinePosition = 1.0f;
633 lastUnderlinedFontId = glyphInfo->fontId;
636 // Retrieves the glyph's position.
637 const Vector2* const position = positionBuffer + glyphIndex;
638 if ( baseline < position->y + glyphInfo->yBearing )
640 baseline = position->y + glyphInfo->yBearing;
643 // Calculate the positions of leftmost and rightmost glyphs in the current line
644 if ( position->x < lineExtentLeft)
646 lineExtentLeft = position->x;
649 if ( position->x + glyphInfo->width > lineExtentRight)
651 lineExtentRight = position->x + glyphInfo->width;
654 // Retrieves the glyph's color.
655 const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndexBuffer + glyphIndex );
658 if ( style == Typesetter::STYLE_SHADOW )
660 color = mModel->GetShadowColor();
662 else if ( style == Typesetter::STYLE_OUTLINE )
664 color = mModel->GetOutlineColor();
668 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + ( colorIndex - 1u ) );
676 // Retrieves the glyph's bitmap.
677 glyphData.glyphBitmap.buffer = NULL;
678 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
679 glyphData.glyphBitmap.height = glyphInfo->height;
681 if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
683 // Don't render outline for other styles
687 if( style != Typesetter::STYLE_UNDERLINE )
689 fontClient.CreateBitmap( glyphInfo->fontId,
691 glyphInfo->isItalicRequired,
692 glyphInfo->isBoldRequired,
693 glyphData.glyphBitmap,
694 static_cast<int>( outlineWidth ) );
697 // Sets the glyph's bitmap into the bitmap of the whole text.
698 if( NULL != glyphData.glyphBitmap.buffer )
700 if ( style == Typesetter::STYLE_OUTLINE )
702 // Set the position offset for the current glyph
703 glyphData.horizontalOffset -= glyphData.glyphBitmap.outlineOffsetX;
704 glyphData.verticalOffset -= glyphData.glyphBitmap.outlineOffsetY;
707 // Set the buffer of the glyph's bitmap into the final bitmap's buffer
708 TypesetGlyph( glyphData,
714 if ( style == Typesetter::STYLE_OUTLINE )
716 // Reset the position offset for the next glyph
717 glyphData.horizontalOffset += glyphData.glyphBitmap.outlineOffsetX;
718 glyphData.verticalOffset += glyphData.glyphBitmap.outlineOffsetY;
721 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
722 delete []glyphData.glyphBitmap.buffer;
723 glyphData.glyphBitmap.buffer = NULL;
727 // Draw the underline from the leftmost glyph to the rightmost glyph
728 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
730 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
732 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
734 if( y > bufferHeight - 1 )
736 // Do not write out of bounds.
740 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
742 if( x > bufferWidth - 1 )
744 // Do not write out of bounds.
748 // Always RGBA image for text with styles
749 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
750 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
751 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
753 // Write the underline color to the pixel buffer
754 uint8_t colorAlpha = static_cast< uint8_t >( underlineColor.a * 255.f );
755 *( underlinePixelBuffer + 3u ) = colorAlpha;
756 *( underlinePixelBuffer + 2u ) = static_cast< uint8_t >( underlineColor.b * colorAlpha );
757 *( underlinePixelBuffer + 1u ) = static_cast< uint8_t >( underlineColor.g * colorAlpha );
758 *( underlinePixelBuffer ) = static_cast< uint8_t >( underlineColor.r * colorAlpha );
760 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
765 // Draw the background color from the leftmost glyph to the rightmost glyph
766 if ( style == Typesetter::STYLE_BACKGROUND )
768 Vector4 backgroundColor = mModel->GetBackgroundColor();
770 for( int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++ )
772 if( ( y < 0 ) || ( y > static_cast<int>(bufferHeight - 1) ) )
774 // Do not write out of bounds.
778 for( int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
780 if( ( x < 0 ) || ( x > static_cast<int>(bufferWidth - 1) ) )
782 // Do not write out of bounds.
786 // Always RGBA image for text with styles
787 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
788 uint32_t backgroundPixel = *( bitmapBuffer + y * glyphData.width + x );
789 uint8_t* backgroundPixelBuffer = reinterpret_cast<uint8_t*>( &backgroundPixel );
791 // Write the background color to the pixel buffer
792 uint8_t colorAlpha = static_cast< uint8_t >( backgroundColor.a * 255.f );
793 *( backgroundPixelBuffer + 3u ) = colorAlpha;
794 *( backgroundPixelBuffer + 2u ) = static_cast< uint8_t >( backgroundColor.b * colorAlpha );
795 *( backgroundPixelBuffer + 1u ) = static_cast< uint8_t >( backgroundColor.g * colorAlpha );
796 *( backgroundPixelBuffer ) = static_cast< uint8_t >( backgroundColor.r * colorAlpha );
798 *( bitmapBuffer + y * glyphData.width + x ) = backgroundPixel;
803 // Increases the vertical offset with the line's descender.
804 glyphData.verticalOffset += static_cast<int>( -line.descender );
807 return glyphData.bitmapBuffer;
810 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
812 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
813 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
815 Devel::PixelBuffer combinedPixelBuffer;
817 if ( topBuffer == NULL && bottomBuffer == NULL )
819 // Nothing to do if both buffers are empty.
820 return combinedPixelBuffer;
823 if ( topBuffer == NULL )
825 // Nothing to do if topBuffer is empty.
826 return bottomPixelBuffer;
829 if ( bottomBuffer == NULL )
831 // Nothing to do if bottomBuffer is empty.
832 return topPixelBuffer;
835 // Always combine two RGBA images
836 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
837 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
839 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
840 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
841 memset( combinedBuffer, 0u, bufferSizeChar );
843 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
845 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
846 // Otherwise, copy pixel from topBuffer to combinedBuffer.
848 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
850 if ( alphaBuffer1 != 255 )
852 // At least one pixel is not fully opaque
853 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
854 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4] + ( bottomBuffer[pixelIndex*4] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
855 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1] + ( bottomBuffer[pixelIndex*4+1] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
856 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2] + ( bottomBuffer[pixelIndex*4+2] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
857 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
861 // Copy the pixel from topBuffer to combinedBuffer
862 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
863 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
864 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
865 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
869 return combinedPixelBuffer;
872 Typesetter::Typesetter( const ModelInterface* const model )
873 : mModel( new ViewModel( model ) )
877 Typesetter::~Typesetter()
884 } // namespace Toolkit