2 * Copyright (c) 2016 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>
26 #include <dali-toolkit/internal/text/rendering/view-model.h>
41 * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
45 uint32_t* bitmapBuffer; ///< The buffer of the whole bitmap. The format is RGBA8888.
46 Vector2* position; ///< The position of the glyph.
47 TextAbstraction::FontClient::GlyphBufferData glyphBitmap; ///< The glyph's bitmap.
48 unsigned int width; ///< The bitmap's width.
49 unsigned int height; ///< The bitmap's height.
50 int horizontalOffset; ///< The horizontal offset to be added to the 'x' glyph's position.
51 int verticalOffset; ///< The vertical offset to be added to the 'y' glyph's position.
55 * @brief Sets the glyph's buffer into the bitmap's buffer.
57 * @param[in] data Struct which contains the glyph's data and the bitmap's data.
58 * @param[in] position The position of the glyph.
59 * @param[in] color The color of the glyph.
61 void TypesetGlyph( const GlyphData& data,
62 const Vector2* const position,
63 const Vector4* const color )
65 if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) )
67 // Nothing to do if the width or height of the buffer is zero.
71 const int widthMinusOne = static_cast<int>( data.width - 1u );
72 const int heightMinusOne = static_cast<int>( data.height - 1u );
74 // Whether the given glyph is a color one.
75 const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
77 // Pointer to the color glyph if there is one.
78 const uint32_t* const colorGlyphBuffer = isColorGlyph ? reinterpret_cast<uint32_t*>( data.glyphBitmap.buffer ) : NULL;
80 // Pack the given color into a 32bit buffer. The alpha channel will be updated later for each pixel.
81 // The format is RGBA8888.
82 uint32_t packedColor = 0u;
83 uint8_t* packedColorBuffer = reinterpret_cast<uint8_t*>( &packedColor );
84 *( packedColorBuffer + 2 ) = static_cast<uint8_t>( color->b * 255.f );
85 *( packedColorBuffer + 1 ) = static_cast<uint8_t>( color->g * 255.f );
86 *packedColorBuffer = static_cast<uint8_t>( color->r * 255.f );
88 // Initial vertical offset.
89 const int yOffset = data.verticalOffset + position->y;
91 // Traverse the pixels of the glyph line per line.
92 for( int lineIndex = 0, glyphHeight = static_cast<int>( data.glyphBitmap.height ); lineIndex < glyphHeight; ++lineIndex )
94 const int yOffsetIndex = yOffset + lineIndex;
95 if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
97 // Do not write out of bounds.
101 const int verticalOffset = yOffsetIndex * data.width;
102 const int xOffset = data.horizontalOffset + position->x;
103 const int glyphBufferOffset = lineIndex * static_cast<int>( data.glyphBitmap.width );
104 for( int index = 0, glyphWidth = static_cast<int>( data.glyphBitmap.width ); index < glyphWidth; ++index )
106 const int xOffsetIndex = xOffset + index;
107 if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
109 // Don't write out of bounds.
115 // Retrieves the color from the glyph. The format is BGRA8888.
116 uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
118 // Update the alpha channel.
119 uint8_t* packedColorGlyphBuffer = reinterpret_cast<uint8_t*>( &packedColorGlyph );
120 std::swap( *packedColorGlyphBuffer, *( packedColorGlyphBuffer + 2u ) ); // Swap B and R.
121 *( packedColorGlyphBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( *( packedColorGlyphBuffer + 3u ) ) );
123 // Set the color into the final pixel buffer.
124 *( data.bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
128 // Update the alpha channel.
129 const uint8_t alpha = *( data.glyphBitmap.buffer + glyphBufferOffset + index );
130 *( packedColorBuffer + 3u ) = static_cast<uint8_t>( color->a * static_cast<float>( alpha ) );
132 // Set the color into the final pixel buffer.
133 *( data.bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColor;
141 TypesetterPtr Typesetter::New( const ModelInterface* const model )
143 return TypesetterPtr( new Typesetter( model ) );
146 ViewModel* Typesetter::GetViewModel()
151 PixelData Typesetter::Render( const Vector2& size )
153 // @todo. This initial implementation for a TextLabel has only one visible page.
155 // Elides the text if needed.
156 mModel->ElideGlyphs();
158 // Retrieves the layout size.
159 const Size& layoutSize = mModel->GetLayoutSize();
161 // Set the offset for the vertical alignment.
164 switch( mModel->GetVerticalAlignment() )
166 case Layout::VERTICAL_ALIGN_TOP:
171 case Layout::VERTICAL_ALIGN_CENTER:
173 penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
176 case Layout::VERTICAL_ALIGN_BOTTOM:
178 penY = static_cast<int>( size.height - layoutSize.height );
183 // Retrieve lines, glyphs, positions and colors from the view model.
184 const Length modelNumberOfLines = mModel->GetNumberOfLines();
185 const LineRun* const modelLinesBuffer = mModel->GetLines();
186 const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
187 const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
188 const Vector2* const positionBuffer = mModel->GetLayout();
189 const Vector4* const colorsBuffer = mModel->GetColors();
190 const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
192 // Whether to use the default color.
193 const bool useDefaultColor = NULL == colorsBuffer;
194 const Vector4& defaultColor = mModel->GetDefaultColor();
196 // Create and initialize the pixel buffer.
198 glyphData.verticalOffset = penY;
200 glyphData.width = static_cast<unsigned int>( size.width );
201 glyphData.height = static_cast<unsigned int>( size.height );
202 const unsigned int bufferSizeInt = glyphData.width * glyphData.height;
203 const unsigned int bufferSizeChar = 4u * bufferSizeInt;
204 glyphData.bitmapBuffer = new uint32_t[ bufferSizeInt ]; // This array will get deleted by PixelData because of the DELETE_ARRAY parameter.
205 memset( glyphData.bitmapBuffer, 0u, bufferSizeChar );
207 PixelData pixelData = PixelData::New( reinterpret_cast<uint8_t*>( glyphData.bitmapBuffer ),
211 Pixel::RGBA8888, // The format is RGBA8888 because is the format accepted by the image atlas manager.
212 PixelData::DELETE_ARRAY );
214 // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
215 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
217 // Traverses the lines of the text.
218 for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
220 const LineRun& line = *( modelLinesBuffer + lineIndex );
222 // Sets the horizontal offset of the line.
223 glyphData.horizontalOffset = static_cast<int>( line.alignmentOffset );
225 // Increases the vertical offset with the line's ascender.
226 glyphData.verticalOffset += static_cast<int>( line.ascender );
228 // Traverses the glyphs of the line.
229 const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
230 for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
232 // Retrieve the glyph's info.
233 const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
235 if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
236 ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
238 // Nothing to do if the glyph's width or height is zero.
242 // Retrieves the glyph's position.
243 const Vector2* const position = positionBuffer + glyphIndex;
245 // Retrieves the glyph's color.
246 const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
247 const Vector4* const color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
249 // Retrieves the glyph's bitmap.
250 glyphData.glyphBitmap.buffer = NULL;
251 fontClient.CreateBitmap( glyphInfo->fontId,
253 glyphData.glyphBitmap );
255 // Sets the glyph's bitmap into the bitmap of the whole text.
256 if( NULL != glyphData.glyphBitmap.buffer )
258 TypesetGlyph( glyphData,
264 // Increases the vertical offset with the line's descender.
265 glyphData.verticalOffset += static_cast<int>( -line.descender );
271 Typesetter::Typesetter( const ModelInterface* const model )
272 : mModel( new ViewModel( model ) )
276 Typesetter::~Typesetter()
283 } // namespace Toolkit