2 * Copyright (c) 2020 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 ) )
281 /// Helper method to fetch the underline metrics for the specified font glyph
282 void FetchFontUnderlineMetrics(
283 TextAbstraction::FontClient& fontClient,
284 const GlyphInfo* const glyphInfo,
285 float& currentUnderlinePosition,
286 const float underlineHeight,
287 float& currentUnderlineThickness,
288 float& maxUnderlineThickness,
289 FontId& lastUnderlinedFontId)
291 FontMetrics fontMetrics;
292 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
293 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
294 const float descender = ceil( fabsf( fontMetrics.descender ) );
296 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
298 currentUnderlineThickness = fontMetrics.underlineThickness;
300 // Ensure underline will be at least a pixel high
301 if ( currentUnderlineThickness < 1.0f )
303 currentUnderlineThickness = 1.0f;
307 currentUnderlineThickness = ceil( currentUnderlineThickness );
311 // The underline thickness should be the max underline thickness of all glyphs of the line.
312 if ( currentUnderlineThickness > maxUnderlineThickness )
314 maxUnderlineThickness = currentUnderlineThickness;
317 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
318 if( currentUnderlinePosition > descender )
320 currentUnderlinePosition = descender;
323 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
325 // Move offset down by one ( EFL behavior )
326 currentUnderlinePosition = 1.0f;
329 lastUnderlinedFontId = glyphInfo->fontId;
332 /// Draws the specified color to the pixel buffer
333 void WriteColorToPixelBuffer(
334 GlyphData& glyphData,
335 uint32_t* bitmapBuffer,
336 const Vector4& color,
337 const unsigned int x,
338 const unsigned int y)
340 // Always RGBA image for text with styles
341 uint32_t pixel = *( bitmapBuffer + y * glyphData.width + x );
342 uint8_t* pixelBuffer = reinterpret_cast<uint8_t*>( &pixel );
344 // Write the color to the pixel buffer
345 uint8_t colorAlpha = static_cast< uint8_t >( color.a * 255.f );
346 *( pixelBuffer + 3u ) = colorAlpha;
347 *( pixelBuffer + 2u ) = static_cast< uint8_t >( color.b * colorAlpha );
348 *( pixelBuffer + 1u ) = static_cast< uint8_t >( color.g * colorAlpha );
349 *( pixelBuffer ) = static_cast< uint8_t >( color.r * colorAlpha );
351 *( bitmapBuffer + y * glyphData.width + x ) = pixel;
354 /// Draws the specified underline color to the buffer
356 const Vector4& underlineColor,
357 const unsigned int bufferWidth,
358 const unsigned int bufferHeight,
359 GlyphData& glyphData,
360 const float baseline,
361 const float currentUnderlinePosition,
362 const float maxUnderlineThickness,
363 const float lineExtentLeft,
364 const float lineExtentRight)
366 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
367 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
369 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
371 if( y > bufferHeight - 1 )
373 // Do not write out of bounds.
377 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
379 if( x > bufferWidth - 1 )
381 // Do not write out of bounds.
385 WriteColorToPixelBuffer(glyphData, bitmapBuffer, underlineColor, x, y);
390 /// Draws the background color to the buffer
391 void DrawBackgroundColor(
392 Vector4 backgroundColor,
393 const unsigned int bufferWidth,
394 const unsigned int bufferHeight,
395 GlyphData& glyphData,
396 const float baseline,
398 const float lineExtentLeft,
399 const float lineExtentRight)
401 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
403 for( int y = glyphData.verticalOffset + baseline - line.ascender; y < glyphData.verticalOffset + baseline - line.descender; y++ )
405 if( ( y < 0 ) || ( y > static_cast<int>(bufferHeight - 1) ) )
407 // Do not write out of bounds.
411 for( int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
413 if( ( x < 0 ) || ( x > static_cast<int>(bufferWidth - 1) ) )
415 // Do not write out of bounds.
419 WriteColorToPixelBuffer(glyphData, bitmapBuffer, backgroundColor, x, y);
426 TypesetterPtr Typesetter::New( const ModelInterface* const model )
428 return TypesetterPtr( new Typesetter( model ) );
431 ViewModel* Typesetter::GetViewModel()
436 PixelData Typesetter::Render( const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat )
438 // @todo. This initial implementation for a TextLabel has only one visible page.
440 // Elides the text if needed.
441 mModel->ElideGlyphs();
443 // Retrieves the layout size.
444 const Size& layoutSize = mModel->GetLayoutSize();
446 const int outlineWidth = static_cast<int>( mModel->GetOutlineWidth() );
448 // Set the offset for the horizontal alignment according to the text direction and outline width.
451 switch( mModel->GetHorizontalAlignment() )
453 case HorizontalAlignment::BEGIN:
458 case HorizontalAlignment::CENTER:
460 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth : outlineWidth;
463 case HorizontalAlignment::END:
465 penX += ( textDirection == Toolkit::DevelText::TextDirection::LEFT_TO_RIGHT ) ? -outlineWidth * 2 : outlineWidth * 2;
470 // Set the offset for the vertical alignment.
473 switch( mModel->GetVerticalAlignment() )
475 case VerticalAlignment::TOP:
480 case VerticalAlignment::CENTER:
482 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
483 penY = penY < 0.f ? 0.f : penY;
486 case VerticalAlignment::BOTTOM:
488 penY = static_cast<int>( size.height - layoutSize.height );
493 // Calculate vertical line alignment
494 switch( mModel->GetVerticalLineAlignment() )
496 case DevelText::VerticalLineAlignment::TOP:
500 case DevelText::VerticalLineAlignment::MIDDLE:
502 const auto& line = *mModel->GetLines();
503 penY -= line.descender;
504 penY += static_cast<int>(line.lineSpacing*0.5f + line.descender);
507 case DevelText::VerticalLineAlignment::BOTTOM:
509 const auto& line = *mModel->GetLines();
510 const auto lineHeight = line.ascender + (-line.descender) + line.lineSpacing;
511 penY += static_cast<int>(lineHeight - (line.ascender - line.descender));
516 // Generate the image buffers of the text for each different style first,
517 // then combine all of them together as one final image buffer. We try to
518 // do all of these in CPU only, so that once the final texture is generated,
519 // no calculation is needed in GPU during each frame.
521 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
522 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
524 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
525 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
527 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
529 Devel::PixelBuffer imageBuffer;
531 if( RENDER_MASK == behaviour )
533 // Generate the image buffer as an alpha mask for color glyphs.
534 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
536 else if( RENDER_NO_TEXT == behaviour )
538 // Generate an empty image buffer so that it can been combined with the image buffers for styles
539 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
540 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
544 // Generate the image buffer for the text with no style.
545 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
548 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
551 // Generate the outline if enabled
552 const uint16_t outlineWidth = mModel->GetOutlineWidth();
553 if ( outlineWidth != 0u )
555 // Create the image buffer for outline
556 Devel::PixelBuffer outlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_OUTLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
558 // Combine the two buffers
559 imageBuffer = CombineImageBuffer( imageBuffer, outlineImageBuffer, bufferWidth, bufferHeight );
562 // @todo. Support shadow and underline for partial text later on.
564 // Generate the shadow if enabled
565 const Vector2& shadowOffset = mModel->GetShadowOffset();
566 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
568 // Create the image buffer for shadow
569 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
571 // Check whether it will be a soft shadow
572 const float& blurRadius = mModel->GetShadowBlurRadius();
574 if ( blurRadius > Math::MACHINE_EPSILON_1 )
576 shadowImageBuffer.ApplyGaussianBlur( blurRadius );
579 // Combine the two buffers
580 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
583 // Generate the underline if enabled
584 const bool underlineEnabled = mModel->IsUnderlineEnabled();
585 if ( underlineEnabled )
587 // Create the image buffer for underline
588 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs - 1 );
590 // Combine the two buffers
591 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
594 // Generate the background if enabled
595 const bool backgroundEnabled = mModel->IsBackgroundEnabled();
596 if ( backgroundEnabled )
598 Devel::PixelBuffer backgroundImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_BACKGROUND, ignoreHorizontalAlignment, pixelFormat, penX, penY, 0u, numberOfGlyphs -1 );
600 // Combine the two buffers
601 imageBuffer = CombineImageBuffer( imageBuffer, backgroundImageBuffer, bufferWidth, bufferHeight );
605 // Create the final PixelData for the combined image buffer
606 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
611 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 )
613 // Retrieve lines, glyphs, positions and colors from the view model.
614 const Length modelNumberOfLines = mModel->GetNumberOfLines();
615 const LineRun* const modelLinesBuffer = mModel->GetLines();
616 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
617 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
618 const Vector2* const positionBuffer = mModel->GetLayout();
619 const Vector4* const colorsBuffer = mModel->GetColors();
620 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
622 // Whether to use the default color.
623 const bool useDefaultColor = ( NULL == colorsBuffer );
624 const Vector4& defaultColor = mModel->GetDefaultColor();
626 // Create and initialize the pixel buffer.
628 glyphData.verticalOffset = verticalOffset;
629 glyphData.width = bufferWidth;
630 glyphData.height = bufferHeight;
631 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, pixelFormat );
632 glyphData.horizontalOffset = 0;
634 if ( Pixel::RGBA8888 == pixelFormat )
636 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
637 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
638 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
642 memset( glyphData.bitmapBuffer.GetBuffer(), 0, bufferWidth * bufferHeight );
645 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
646 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
648 // Traverses the lines of the text.
649 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
651 const LineRun& line = *( modelLinesBuffer + lineIndex );
653 // Sets the horizontal offset of the line.
654 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
655 glyphData.horizontalOffset += horizontalOffset;
657 // Increases the vertical offset with the line's ascender.
658 glyphData.verticalOffset += static_cast<int>( line.ascender );
660 // Include line spacing after first line
663 glyphData.verticalOffset += static_cast<int>( line.lineSpacing );
666 // Retrieves the glyph's outline width
667 float outlineWidth = static_cast<float>( mModel->GetOutlineWidth() );
669 if( style == Typesetter::STYLE_OUTLINE )
671 glyphData.horizontalOffset -= outlineWidth;
672 if( lineIndex == 0u )
674 // Only need to add the vertical outline offset for the first line
675 glyphData.verticalOffset -= outlineWidth;
678 else if ( style == Typesetter::STYLE_SHADOW )
680 const Vector2& shadowOffset = mModel->GetShadowOffset();
681 glyphData.horizontalOffset += shadowOffset.x - outlineWidth; // if outline enabled then shadow should offset from outline
683 if ( lineIndex == 0u )
685 // Only need to add the vertical shadow offset for first line
686 glyphData.verticalOffset += shadowOffset.y - outlineWidth;
690 const bool underlineEnabled = mModel->IsUnderlineEnabled();
691 const Vector4& underlineColor = mModel->GetUnderlineColor();
692 const float underlineHeight = mModel->GetUnderlineHeight();
694 // Get the underline runs.
695 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
696 Vector<GlyphRun> underlineRuns;
697 underlineRuns.Resize( numberOfUnderlineRuns );
698 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
700 bool thereAreUnderlinedGlyphs = false;
702 float currentUnderlinePosition = 0.0f;
703 float currentUnderlineThickness = underlineHeight;
704 float maxUnderlineThickness = currentUnderlineThickness;
706 FontId lastUnderlinedFontId = 0;
708 float lineExtentLeft = bufferWidth;
709 float lineExtentRight = 0.0f;
710 float baseline = 0.0f;
712 // Traverses the glyphs of the line.
713 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
714 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
716 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
718 // Ignore any glyph that out of the specified range
722 // Retrieve the glyph's info.
723 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
725 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
726 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
728 // Nothing to do if the glyph's width or height is zero.
732 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
733 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
735 // Are we still using the same fontId as previous
736 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
738 // We need to fetch fresh font underline metrics
739 FetchFontUnderlineMetrics(fontClient, glyphInfo, currentUnderlinePosition, underlineHeight, currentUnderlineThickness, maxUnderlineThickness, lastUnderlinedFontId);
742 // Retrieves the glyph's position.
743 const Vector2* const position = positionBuffer + glyphIndex;
744 if ( baseline < position->y + glyphInfo->yBearing )
746 baseline = position->y + glyphInfo->yBearing;
749 // Calculate the positions of leftmost and rightmost glyphs in the current line
750 if ( position->x < lineExtentLeft)
752 lineExtentLeft = position->x;
755 if ( position->x + glyphInfo->width > lineExtentRight)
757 lineExtentRight = position->x + glyphInfo->width;
760 // Retrieves the glyph's color.
761 const ColorIndex colorIndex = useDefaultColor ? 0u : *( colorIndexBuffer + glyphIndex );
764 if ( style == Typesetter::STYLE_SHADOW )
766 color = mModel->GetShadowColor();
768 else if ( style == Typesetter::STYLE_OUTLINE )
770 color = mModel->GetOutlineColor();
774 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? defaultColor : *( colorsBuffer + ( colorIndex - 1u ) );
782 // Retrieves the glyph's bitmap.
783 glyphData.glyphBitmap.buffer = NULL;
784 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
785 glyphData.glyphBitmap.height = glyphInfo->height;
787 if( style != Typesetter::STYLE_OUTLINE && style != Typesetter::STYLE_SHADOW )
789 // Don't render outline for other styles
793 if( style != Typesetter::STYLE_UNDERLINE )
795 fontClient.CreateBitmap( glyphInfo->fontId,
797 glyphInfo->isItalicRequired,
798 glyphInfo->isBoldRequired,
799 glyphData.glyphBitmap,
800 static_cast<int>( outlineWidth ) );
803 // Sets the glyph's bitmap into the bitmap of the whole text.
804 if( NULL != glyphData.glyphBitmap.buffer )
806 if ( style == Typesetter::STYLE_OUTLINE )
808 // Set the position offset for the current glyph
809 glyphData.horizontalOffset -= glyphData.glyphBitmap.outlineOffsetX;
810 glyphData.verticalOffset -= glyphData.glyphBitmap.outlineOffsetY;
813 // Set the buffer of the glyph's bitmap into the final bitmap's buffer
814 TypesetGlyph( glyphData,
820 if ( style == Typesetter::STYLE_OUTLINE )
822 // Reset the position offset for the next glyph
823 glyphData.horizontalOffset += glyphData.glyphBitmap.outlineOffsetX;
824 glyphData.verticalOffset += glyphData.glyphBitmap.outlineOffsetY;
827 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
828 delete []glyphData.glyphBitmap.buffer;
829 glyphData.glyphBitmap.buffer = NULL;
833 // Draw the underline from the leftmost glyph to the rightmost glyph
834 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
836 DrawUnderline(underlineColor, bufferWidth, bufferHeight, glyphData, baseline, currentUnderlinePosition, maxUnderlineThickness, lineExtentLeft, lineExtentRight);
839 // Draw the background color from the leftmost glyph to the rightmost glyph
840 if ( style == Typesetter::STYLE_BACKGROUND )
842 DrawBackgroundColor(mModel->GetBackgroundColor(), bufferWidth, bufferHeight, glyphData, baseline, line, lineExtentLeft, lineExtentRight);
845 // Increases the vertical offset with the line's descender.
846 glyphData.verticalOffset += static_cast<int>( -line.descender );
849 return glyphData.bitmapBuffer;
852 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
854 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
855 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
857 Devel::PixelBuffer combinedPixelBuffer;
859 if ( topBuffer == NULL && bottomBuffer == NULL )
861 // Nothing to do if both buffers are empty.
862 return combinedPixelBuffer;
865 if ( topBuffer == NULL )
867 // Nothing to do if topBuffer is empty.
868 return bottomPixelBuffer;
871 if ( bottomBuffer == NULL )
873 // Nothing to do if bottomBuffer is empty.
874 return topPixelBuffer;
877 // Always combine two RGBA images
878 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
879 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
881 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
882 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
883 memset( combinedBuffer, 0u, bufferSizeChar );
885 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
887 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
888 // Otherwise, copy pixel from topBuffer to combinedBuffer.
890 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
892 if ( alphaBuffer1 != 255 )
894 // At least one pixel is not fully opaque
895 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
896 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4] + ( bottomBuffer[pixelIndex*4] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
897 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1] + ( bottomBuffer[pixelIndex*4+1] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
898 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2] + ( bottomBuffer[pixelIndex*4+2] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
899 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
903 // Copy the pixel from topBuffer to combinedBuffer
904 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
905 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
906 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
907 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
911 return combinedPixelBuffer;
914 Typesetter::Typesetter( const ModelInterface* const model )
915 : mModel( new ViewModel( model ) )
919 Typesetter::~Typesetter()
926 } // namespace Toolkit