2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
22 #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>
42 * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
46 Devel::PixelBuffer bitmapBuffer; ///< The buffer of the whole bitmap. The format is RGBA8888.
47 Vector2* position; ///< The position of the glyph.
48 TextAbstraction::FontClient::GlyphBufferData glyphBitmap; ///< The glyph's bitmap.
49 unsigned int width; ///< The bitmap's width.
50 unsigned int height; ///< The bitmap's height.
51 int horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position.
52 int verticalOffset; ///< The vertical offset to be added to the 'y' glyph's position.
56 * @brief Sets the glyph's buffer into the bitmap's buffer.
58 * @param[in] data Struct which contains the glyph's data and the bitmap's data.
59 * @param[in] position The position of the glyph.
60 * @param[in] color The color of the glyph.
61 * @param[in] style The style of the text.
63 void TypesetGlyph( GlyphData& data,
64 const Vector2* const position,
65 const Vector4* const color,
66 Typesetter::Style style)
68 if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) )
70 // Nothing to do if the width or height of the buffer is zero.
74 const int widthMinusOne = static_cast<int>( data.width - 1u );
75 const int heightMinusOne = static_cast<int>( data.height - 1u );
77 // Whether the given glyph is a color one.
78 const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
80 // Pointer to the color glyph if there is one.
81 const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>( data.glyphBitmap.buffer ) : NULL;
83 // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
84 // The format is RGBA8888.
85 uint32_t packedColor = 0u;
86 uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
87 *( packedColorBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
88 *( packedColorBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
89 *packedColorBuffer = static_cast<uint8_t>( color->r * 255.f );
91 // Initial vertical offset.
92 const int yOffset = data.verticalOffset + position->y;
94 // Traverse the pixels of the glyph line per line.
95 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
97 const int yOffsetIndex = yOffset + lineIndex;
98 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
100 // Do not write out of bounds.
104 const int verticalOffset = yOffsetIndex * data.width;
105 const int xOffset = data.horizontalOffset + position->x;
106 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
107 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
109 const int xOffsetIndex = xOffset + index;
110 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
112 // Don't write out of bounds.
116 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( data.bitmapBuffer.GetBuffer() );
120 // Retrieves the color from the color glyph. The format is BGRA8888.
121 uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
122 uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
124 if( Typesetter::STYLE_SHADOW == style )
126 // The shadow of color glyph needs to have the shadow color.
127 *( packedColorGlyphBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
128 *( packedColorGlyphBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
129 *packedColorGlyphBuffer = static_cast<uint8_t>( color->r * 255.f );
133 std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
136 // Update the alpha channel.
137 if( Typesetter::STYLE_MASK == style )
139 // Create an alpha mask for color glyph.
140 *( packedColorGlyphBuffer + 3u ) = 0u;
144 *( packedColorGlyphBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
147 // Set the color into the final pixel buffer.
148 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
152 // Update the alpha channel.
153 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
155 // Copy non-transparent pixels only
158 // Check alpha of overlapped pixels
159 uint32_t& currentColor = *( bitmapBuffer + verticalOffset + xOffsetIndex );
160 uint8_t* packedCurrentColorBuffer = reinterpret_cast<uint8_t*>( ¤tColor );
162 uint8_t currentAlpha = *( packedCurrentColorBuffer + 3u );
163 uint8_t newAlpha = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
165 // For any pixel overlapped with the pixel in previous glyphs, make sure we don't
166 // overwrite a previous bigger alpha with a smaller alpha (in order to avoid
167 // semi-transparent gaps between joint glyphs with overlapped pixels, which could
168 // happen, for example, in the RTL text when we copy glyphs from right to left).
169 *( packedColorBuffer + 3u ) = std::max( currentAlpha, newAlpha );
171 // Set the color into the final pixel buffer.
172 currentColor = packedColor;
179 bool IsGlyphUnderlined( GlyphIndex index,
180 const Vector<GlyphRun>& underlineRuns )
182 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
183 endIt = underlineRuns.End();
187 const GlyphRun& run = *it;
189 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
200 TypesetterPtr Typesetter::New( const ModelInterface* const model )
202 return TypesetterPtr( new Typesetter( model ) );
205 ViewModel* Typesetter::GetViewModel()
210 PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment )
212 // @todo. This initial implementation for a TextLabel has only one visible page.
214 // Elides the text if needed.
215 mModel->ElideGlyphs();
217 // Retrieves the layout size.
218 const Size& layoutSize = mModel->GetLayoutSize();
220 // Set the offset for the vertical alignment.
223 switch( mModel->GetVerticalAlignment() )
225 case Layout::VERTICAL_ALIGN_TOP:
230 case Layout::VERTICAL_ALIGN_CENTER:
232 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
235 case Layout::VERTICAL_ALIGN_BOTTOM:
237 penY = static_cast<int>( size.height - layoutSize.height );
242 // Generate the image buffers of the text for each different style first,
243 // then combine all of them together as one final image buffer. We try to
244 // do all of these in CPU only, so that once the final texture is generated,
245 // no calculation is needed in GPU during each frame.
247 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
248 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
250 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
251 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
253 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
255 Devel::PixelBuffer imageBuffer;
257 if( RENDER_MASK == behaviour )
259 // Generate the image buffer as an alpha mask for color glyphs.
260 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs - 1 );
262 else if( RENDER_NO_TEXT == behaviour )
264 // Generate an empty image buffer so that it can been combined with the image buffers for styles
265 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
266 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
270 // Generate the image buffer for the text with no style.
271 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs -1 );
274 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
276 // @todo. Support shadow and underline for partial text later on.
278 // Generate the shadow if enabled
279 const Vector2& shadowOffset = mModel->GetShadowOffset();
280 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
282 // Create the image buffer for shadow
283 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs - 1 );
285 // Combine the two buffers
286 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
289 // Generate the underline if enabled
290 const bool underlineEnabled = mModel->IsUnderlineEnabled();
291 if ( underlineEnabled )
293 // Create the image buffer for underline
294 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs - 1 );
296 // Combine the two buffers
297 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
301 // Create the final PixelData for the combined image buffer
302 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
307 Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex )
309 // Retrieve lines, glyphs, positions and colors from the view model.
310 const Length modelNumberOfLines = mModel->GetNumberOfLines();
311 const LineRun* const modelLinesBuffer = mModel->GetLines();
312 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
313 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
314 const Vector2* const positionBuffer = mModel->GetLayout();
315 const Vector4* const colorsBuffer = mModel->GetColors();
316 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
318 // Whether to use the default color.
319 const bool useDefaultColor = ( NULL == colorsBuffer );
320 const Vector4& defaultColor = mModel->GetDefaultColor();
322 // Create and initialize the pixel buffer.
324 glyphData.verticalOffset = verticalOffset;
326 glyphData.width = bufferWidth;
327 glyphData.height = bufferHeight;
328 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
329 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
330 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
331 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
333 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
334 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
336 // Traverses the lines of the text.
337 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
339 const LineRun& line = *( modelLinesBuffer + lineIndex );
341 // Sets the horizontal offset of the line.
342 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
344 // Increases the vertical offset with the line's ascender.
345 glyphData.verticalOffset += static_cast<int>( line.ascender );
347 if ( style == Typesetter::STYLE_SHADOW )
349 const Vector2& shadowOffset = mModel->GetShadowOffset();
350 glyphData.horizontalOffset += shadowOffset.x;
351 if ( lineIndex == 0u )
353 // Only need to add the vertical shadow offset for once
354 glyphData.verticalOffset += shadowOffset.y;
358 const bool underlineEnabled = mModel->IsUnderlineEnabled();
359 const Vector4& underlineColor = mModel->GetUnderlineColor();
360 const float underlineHeight = mModel->GetUnderlineHeight();
362 // Get the underline runs.
363 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
364 Vector<GlyphRun> underlineRuns;
365 underlineRuns.Resize( numberOfUnderlineRuns );
366 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
368 bool thereAreUnderlinedGlyphs = false;
370 float currentUnderlinePosition = 0.0f;
371 float currentUnderlineThickness = underlineHeight;
372 float maxUnderlineThickness = currentUnderlineThickness;
374 FontId lastUnderlinedFontId = 0;
376 float lineExtentLeft = bufferWidth;
377 float lineExtentRight = 0.0f;
378 float baseline = 0.0f;
380 // Traverses the glyphs of the line.
381 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
382 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
384 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
386 // Ignore any glyph that out of the specified range
390 // Retrieve the glyph's info.
391 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
393 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
394 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
396 // Nothing to do if the glyph's width or height is zero.
400 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
401 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
403 // Are we still using the same fontId as previous
404 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
406 // We need to fetch fresh font underline metrics
407 FontMetrics fontMetrics;
408 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
409 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
410 const float descender = ceil( fabsf( fontMetrics.descender ) );
412 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
414 currentUnderlineThickness = fontMetrics.underlineThickness;
416 // Ensure underline will be at least a pixel high
417 if ( currentUnderlineThickness < 1.0f )
419 currentUnderlineThickness = 1.0f;
423 currentUnderlineThickness = ceil( currentUnderlineThickness );
427 // The underline thickness should be the max underline thickness of all glyphs of the line.
428 if ( currentUnderlineThickness > maxUnderlineThickness )
430 maxUnderlineThickness = currentUnderlineThickness;
433 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
434 if( currentUnderlinePosition > descender )
436 currentUnderlinePosition = descender;
439 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
441 // Move offset down by one ( EFL behavior )
442 currentUnderlinePosition = 1.0f;
445 lastUnderlinedFontId = glyphInfo->fontId;
448 // Retrieves the glyph's position.
449 const Vector2* const position = positionBuffer + glyphIndex;
450 if ( baseline < position->y + glyphInfo->yBearing )
452 baseline = position->y + glyphInfo->yBearing;
455 // Calculate the positions of leftmost and rightmost glyphs in the current line
456 if ( position->x < lineExtentLeft)
458 lineExtentLeft = position->x;
461 if ( position->x + glyphInfo->width > lineExtentRight)
463 lineExtentRight = position->x + glyphInfo->width;
466 // Retrieves the glyph's color.
467 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
469 const Vector4* color;
470 if ( style == Typesetter::STYLE_SHADOW )
472 color = &( mModel->GetShadowColor() );
476 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
479 // Retrieves the glyph's bitmap.
480 glyphData.glyphBitmap.buffer = NULL;
481 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
482 glyphData.glyphBitmap.height = glyphInfo->height;
483 fontClient.CreateBitmap( glyphInfo->fontId,
485 glyphData.glyphBitmap );
487 // Sets the glyph's bitmap into the bitmap of the whole text.
488 if( NULL != glyphData.glyphBitmap.buffer )
490 TypesetGlyph( glyphData,
494 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
495 delete []glyphData.glyphBitmap.buffer;
496 glyphData.glyphBitmap.buffer = NULL;
500 // Draw the underline from the leftmost glyph to the rightmost glyph
501 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
503 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
505 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
507 if( ( y < 0 ) || ( y > bufferHeight - 1 ) )
509 // Do not write out of bounds.
513 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
515 if( ( x < 0 ) || ( x > bufferWidth - 1 ) )
517 // Do not write out of bounds.
521 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
522 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
523 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
525 // Write the underline color to the pixel buffer
526 *( underlinePixelBuffer ) = static_cast<uint8_t>( underlineColor.r * 255.f );
527 *( underlinePixelBuffer + 1u ) = static_cast<uint8_t>( underlineColor.g * 255.f );
528 *( underlinePixelBuffer + 2u ) = static_cast<uint8_t>( underlineColor.b * 255.f );
529 *( underlinePixelBuffer + 3u ) = static_cast<uint8_t>( underlineColor.a * 255.f );
531 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
536 // Increases the vertical offset with the line's descender.
537 glyphData.verticalOffset += static_cast<int>( -line.descender );
540 return glyphData.bitmapBuffer;
543 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
545 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
546 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
548 Devel::PixelBuffer combinedPixelBuffer;
550 if ( topBuffer == NULL && bottomBuffer == NULL )
552 // Nothing to do if both buffers are empty.
553 return combinedPixelBuffer;
556 if ( topBuffer == NULL )
558 // Nothing to do if topBuffer is empty.
559 return bottomPixelBuffer;
562 if ( bottomBuffer == NULL )
564 // Nothing to do if bottomBuffer is empty.
565 return topPixelBuffer;
568 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
569 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
571 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
572 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
573 memset( combinedBuffer, 0u, bufferSizeChar );
575 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
577 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
578 // Otherwise, copy pixel from topBuffer to combinedBuffer.
580 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
581 unsigned int alphaBuffer2 = bottomBuffer[pixelIndex*4+3];
583 if ( alphaBuffer1 != 255 || alphaBuffer2 != 255 )
585 // At least one pixel is not fully opaque
586 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
587 combinedBuffer[pixelIndex*4] = ( topBuffer[pixelIndex*4] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
588 combinedBuffer[pixelIndex*4+1] = ( topBuffer[pixelIndex*4+1] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4+1] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
589 combinedBuffer[pixelIndex*4+2] = ( topBuffer[pixelIndex*4+2] * topBuffer[pixelIndex*4+3] / 255 ) + ( bottomBuffer[pixelIndex*4+2] * bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / ( 255*255 ) );
590 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
594 // Copy the pixel from topBuffer to combinedBuffer
595 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
596 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
597 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
598 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
602 return combinedPixelBuffer;
605 Typesetter::Typesetter( const ModelInterface* const model )
606 : mModel( new ViewModel( model ) )
610 Typesetter::~Typesetter()
617 } // namespace Toolkit