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 );
154 *( packedColorBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
156 // Set the color into the final pixel buffer.
157 *( bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColor;
163 bool IsGlyphUnderlined( GlyphIndex index,
164 const Vector<GlyphRun>& underlineRuns )
166 for( Vector<GlyphRun>::ConstIterator it = underlineRuns.Begin(),
167 endIt = underlineRuns.End();
171 const GlyphRun& run = *it;
173 if( ( run.glyphIndex <= index ) && ( index < run.glyphIndex + run.numberOfGlyphs ) )
184 TypesetterPtr Typesetter::New( const ModelInterface* const model )
186 return TypesetterPtr( new Typesetter( model ) );
189 ViewModel* Typesetter::GetViewModel()
194 PixelData Typesetter::Render( const Vector2& size, RenderBehaviour behaviour, bool ignoreHorizontalAlignment )
196 // @todo. This initial implementation for a TextLabel has only one visible page.
198 // Elides the text if needed.
199 mModel->ElideGlyphs();
201 // Retrieves the layout size.
202 const Size& layoutSize = mModel->GetLayoutSize();
204 // Set the offset for the vertical alignment.
207 switch( mModel->GetVerticalAlignment() )
209 case Layout::VERTICAL_ALIGN_TOP:
214 case Layout::VERTICAL_ALIGN_CENTER:
216 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
219 case Layout::VERTICAL_ALIGN_BOTTOM:
221 penY = static_cast<int>( size.height - layoutSize.height );
226 // Generate the image buffers of the text for each different style first,
227 // then combine all of them together as one final image buffer. We try to
228 // do all of these in CPU only, so that once the final texture is generated,
229 // no calculation is needed in GPU during each frame.
231 const unsigned int bufferWidth = static_cast<unsigned int>( size.width );
232 const unsigned int bufferHeight = static_cast<unsigned int>( size.height );
234 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
235 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
237 Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
239 Devel::PixelBuffer imageBuffer;
241 if( RENDER_MASK == behaviour )
243 // Generate the image buffer as an alpha mask for color glyphs.
244 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_MASK, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs - 1 );
246 else if( RENDER_NO_TEXT == behaviour )
248 // Generate an empty image buffer so that it can been combined with the image buffers for styles
249 imageBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
250 memset( imageBuffer.GetBuffer(), 0u, bufferSizeChar );
254 // Generate the image buffer for the text with no style.
255 imageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_NONE, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs -1 );
258 if ( ( RENDER_NO_STYLES != behaviour ) && ( RENDER_MASK != behaviour ) )
260 // @todo. Support shadow and underline for partial text later on.
262 // Generate the shadow if enabled
263 const Vector2& shadowOffset = mModel->GetShadowOffset();
264 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
266 // Create the image buffer for shadow
267 Devel::PixelBuffer shadowImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_SHADOW, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs - 1 );
269 // Combine the two buffers
270 imageBuffer = CombineImageBuffer( imageBuffer, shadowImageBuffer, bufferWidth, bufferHeight );
273 // Generate the underline if enabled
274 const bool underlineEnabled = mModel->IsUnderlineEnabled();
275 if ( underlineEnabled )
277 // Create the image buffer for underline
278 Devel::PixelBuffer underlineImageBuffer = CreateImageBuffer( bufferWidth, bufferHeight, Typesetter::STYLE_UNDERLINE, ignoreHorizontalAlignment, penY, 0u, numberOfGlyphs - 1 );
280 // Combine the two buffers
281 imageBuffer = CombineImageBuffer( imageBuffer, underlineImageBuffer, bufferWidth, bufferHeight );
285 // Create the final PixelData for the combined image buffer
286 PixelData pixelData = Devel::PixelBuffer::Convert( imageBuffer );
291 Devel::PixelBuffer Typesetter::CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, int verticalOffset, GlyphIndex fromGlyphIndex, GlyphIndex toGlyphIndex )
293 // Retrieve lines, glyphs, positions and colors from the view model.
294 const Length modelNumberOfLines = mModel->GetNumberOfLines();
295 const LineRun* const modelLinesBuffer = mModel->GetLines();
296 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
297 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
298 const Vector2* const positionBuffer = mModel->GetLayout();
299 const Vector4* const colorsBuffer = mModel->GetColors();
300 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
302 // Whether to use the default color.
303 const bool useDefaultColor = ( NULL == colorsBuffer );
304 const Vector4& defaultColor = mModel->GetDefaultColor();
306 // Create and initialize the pixel buffer.
308 glyphData.verticalOffset = verticalOffset;
310 glyphData.width = bufferWidth;
311 glyphData.height = bufferHeight;
312 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
313 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
314 glyphData.bitmapBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
315 memset( glyphData.bitmapBuffer.GetBuffer(), 0u, bufferSizeChar );
317 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
318 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
320 // Traverses the lines of the text.
321 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
323 const LineRun& line = *( modelLinesBuffer + lineIndex );
325 // Sets the horizontal offset of the line.
326 glyphData.horizontalOffset = ignoreHorizontalAlignment ? 0 : static_cast<int>( line.alignmentOffset );
328 // Increases the vertical offset with the line's ascender.
329 glyphData.verticalOffset += static_cast<int>( line.ascender );
331 if ( style == Typesetter::STYLE_SHADOW )
333 const Vector2& shadowOffset = mModel->GetShadowOffset();
334 glyphData.horizontalOffset += shadowOffset.x;
335 if ( lineIndex == 0u )
337 // Only need to add the vertical shadow offset for once
338 glyphData.verticalOffset += shadowOffset.y;
342 const bool underlineEnabled = mModel->IsUnderlineEnabled();
343 const Vector4& underlineColor = mModel->GetUnderlineColor();
344 const float underlineHeight = mModel->GetUnderlineHeight();
346 // Get the underline runs.
347 const Length numberOfUnderlineRuns = mModel->GetNumberOfUnderlineRuns();
348 Vector<GlyphRun> underlineRuns;
349 underlineRuns.Resize( numberOfUnderlineRuns );
350 mModel->GetUnderlineRuns( underlineRuns.Begin(), 0u, numberOfUnderlineRuns );
352 bool thereAreUnderlinedGlyphs = false;
354 float currentUnderlinePosition = 0.0f;
355 float currentUnderlineThickness = underlineHeight;
356 float maxUnderlineThickness = currentUnderlineThickness;
358 FontId lastUnderlinedFontId = 0;
360 float lineExtentLeft = bufferWidth;
361 float lineExtentRight = 0.0f;
362 float baseline = 0.0f;
364 // Traverses the glyphs of the line.
365 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
366 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
368 if ( glyphIndex < fromGlyphIndex || glyphIndex > toGlyphIndex )
370 // Ignore any glyph that out of the specified range
374 // Retrieve the glyph's info.
375 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
377 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
378 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
380 // Nothing to do if the glyph's width or height is zero.
384 const bool underlineGlyph = underlineEnabled || IsGlyphUnderlined( glyphIndex, underlineRuns );
385 thereAreUnderlinedGlyphs = thereAreUnderlinedGlyphs || underlineGlyph;
387 // Are we still using the same fontId as previous
388 if( underlineGlyph && ( glyphInfo->fontId != lastUnderlinedFontId ) )
390 // We need to fetch fresh font underline metrics
391 FontMetrics fontMetrics;
392 fontClient.GetFontMetrics( glyphInfo->fontId, fontMetrics );
393 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
394 const float descender = ceil( fabsf( fontMetrics.descender ) );
396 if( fabsf( underlineHeight ) < Math::MACHINE_EPSILON_1000 )
398 currentUnderlineThickness = fontMetrics.underlineThickness;
400 // Ensure underline will be at least a pixel high
401 if ( currentUnderlineThickness < 1.0f )
403 currentUnderlineThickness = 1.0f;
407 currentUnderlineThickness = ceil( currentUnderlineThickness );
411 // The underline thickness should be the max underline thickness of all glyphs of the line.
412 if ( currentUnderlineThickness > maxUnderlineThickness )
414 maxUnderlineThickness = currentUnderlineThickness;
417 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
418 if( currentUnderlinePosition > descender )
420 currentUnderlinePosition = descender;
423 if( fabsf( currentUnderlinePosition ) < Math::MACHINE_EPSILON_1000 )
425 // Move offset down by one ( EFL behavior )
426 currentUnderlinePosition = 1.0f;
429 lastUnderlinedFontId = glyphInfo->fontId;
432 // Retrieves the glyph's position.
433 const Vector2* const position = positionBuffer + glyphIndex;
434 if ( baseline < position->y + glyphInfo->yBearing )
436 baseline = position->y + glyphInfo->yBearing;
439 // Calculate the positions of leftmost and rightmost glyphs in the current line
440 if ( position->x < lineExtentLeft)
442 lineExtentLeft = position->x;
445 if ( position->x + glyphInfo->width > lineExtentRight)
447 lineExtentRight = position->x + glyphInfo->width;
450 // Retrieves the glyph's color.
451 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
453 const Vector4* color;
454 if ( style == Typesetter::STYLE_SHADOW )
456 color = &( mModel->GetShadowColor() );
460 color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
463 // Retrieves the glyph's bitmap.
464 glyphData.glyphBitmap.buffer = NULL;
465 glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
466 glyphData.glyphBitmap.height = glyphInfo->height;
467 fontClient.CreateBitmap( glyphInfo->fontId,
469 glyphData.glyphBitmap );
471 // Sets the glyph's bitmap into the bitmap of the whole text.
472 if( NULL != glyphData.glyphBitmap.buffer )
474 TypesetGlyph( glyphData,
478 // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
479 delete []glyphData.glyphBitmap.buffer;
480 glyphData.glyphBitmap.buffer = NULL;
484 // Draw the underline from the leftmost glyph to the rightmost glyph
485 if ( thereAreUnderlinedGlyphs && style == Typesetter::STYLE_UNDERLINE )
487 int underlineYOffset = glyphData.verticalOffset + baseline + currentUnderlinePosition;
489 for( unsigned int y = underlineYOffset; y < underlineYOffset + maxUnderlineThickness; y++ )
491 if( ( y < 0 ) || ( y > bufferHeight - 1 ) )
493 // Do not write out of bounds.
497 for( unsigned int x = glyphData.horizontalOffset + lineExtentLeft; x <= glyphData.horizontalOffset + lineExtentRight; x++ )
499 if( ( x < 0 ) || ( x > bufferWidth - 1 ) )
501 // Do not write out of bounds.
505 uint32_t* bitmapBuffer = reinterpret_cast< uint32_t* >( glyphData.bitmapBuffer.GetBuffer() );
506 uint32_t underlinePixel = *( bitmapBuffer + y * glyphData.width + x );
507 uint8_t* underlinePixelBuffer = reinterpret_cast<uint8_t*>( &underlinePixel );
509 // Write the underline color to the pixel buffer
510 *( underlinePixelBuffer ) = static_cast<uint8_t>( underlineColor.r * 255.f );
511 *( underlinePixelBuffer + 1u ) = static_cast<uint8_t>( underlineColor.g * 255.f );
512 *( underlinePixelBuffer + 2u ) = static_cast<uint8_t>( underlineColor.b * 255.f );
513 *( underlinePixelBuffer + 3u ) = static_cast<uint8_t>( underlineColor.a * 255.f );
515 *( bitmapBuffer + y * glyphData.width + x ) = underlinePixel;
520 // Increases the vertical offset with the line's descender.
521 glyphData.verticalOffset += static_cast<int>( -line.descender );
524 return glyphData.bitmapBuffer;
527 Devel::PixelBuffer Typesetter::CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight )
529 unsigned char* topBuffer = topPixelBuffer.GetBuffer();
530 unsigned char* bottomBuffer = bottomPixelBuffer.GetBuffer();
532 Devel::PixelBuffer combinedPixelBuffer;
534 if ( topBuffer == NULL && bottomBuffer == NULL )
536 // Nothing to do if both buffers are empty.
537 return combinedPixelBuffer;
540 if ( topBuffer == NULL )
542 // Nothing to do if topBuffer is empty.
543 return bottomPixelBuffer;
546 if ( bottomBuffer == NULL )
548 // Nothing to do if bottomBuffer is empty.
549 return topPixelBuffer;
552 const unsigned int bufferSizeInt = bufferWidth * bufferHeight;
553 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
555 combinedPixelBuffer = Devel::PixelBuffer::New( bufferWidth, bufferHeight, Pixel::RGBA8888 );
556 uint8_t* combinedBuffer = reinterpret_cast< uint8_t* >( combinedPixelBuffer.GetBuffer() );
557 memset( combinedBuffer, 0u, bufferSizeChar );
559 for (unsigned int pixelIndex = 0; pixelIndex < bufferSizeInt; pixelIndex++)
561 // If the alpha of the pixel in either buffer is not fully opaque, blend the two pixels.
562 // Otherwise, copy pixel from topBuffer to combinedBuffer.
564 unsigned int alphaBuffer1 = topBuffer[pixelIndex*4+3];
565 unsigned int alphaBuffer2 = bottomBuffer[pixelIndex*4+3];
567 if ( alphaBuffer1 != 255 || alphaBuffer2 != 255 )
569 // At least one pixel is not fully opaque
570 // "Over" blend the the pixel from topBuffer with the pixel in bottomBuffer
571 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 ) );
572 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 ) );
573 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 ) );
574 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3] + ( bottomBuffer[pixelIndex*4+3] * ( 255 - topBuffer[pixelIndex*4+3] ) / 255 );
578 // Copy the pixel from topBuffer to combinedBuffer
579 combinedBuffer[pixelIndex*4] = topBuffer[pixelIndex*4];
580 combinedBuffer[pixelIndex*4+1] = topBuffer[pixelIndex*4+1];
581 combinedBuffer[pixelIndex*4+2] = topBuffer[pixelIndex*4+2];
582 combinedBuffer[pixelIndex*4+3] = topBuffer[pixelIndex*4+3];
586 return combinedPixelBuffer;
589 Typesetter::Typesetter( const ModelInterface* const model )
590 : mModel( new ViewModel( model ) )
594 Typesetter::~Typesetter()
601 } // namespace Toolkit