+PixelData Typesetter::Render( const Vector2& size )
+{
+ // @todo. This initial implementation for a TextLabel has only one visible page.
+
+ // Elides the text if needed.
+ mModel->ElideGlyphs();
+
+ // Retrieves the layout size.
+ const Size& layoutSize = mModel->GetLayoutSize();
+
+ // Set the offset for the vertical alignment.
+ int penY = 0u;
+
+ switch( mModel->GetVerticalAlignment() )
+ {
+ case Layout::VERTICAL_ALIGN_TOP:
+ {
+ // No offset to add.
+ break;
+ }
+ case Layout::VERTICAL_ALIGN_CENTER:
+ {
+ penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
+ break;
+ }
+ case Layout::VERTICAL_ALIGN_BOTTOM:
+ {
+ penY = static_cast<int>( size.height - layoutSize.height );
+ break;
+ }
+ }
+
+ // Retrieve lines, glyphs, positions and colors from the view model.
+ const Length modelNumberOfLines = mModel->GetNumberOfLines();
+ const LineRun* const modelLinesBuffer = mModel->GetLines();
+ const Length numberOfGlyphs = mModel->GetNumberOfGlyphs();
+ const GlyphInfo* const glyphsBuffer = mModel->GetGlyphs();
+ const Vector2* const positionBuffer = mModel->GetLayout();
+ const Vector4* const colorsBuffer = mModel->GetColors();
+ const ColorIndex* const colorIndexBuffer = mModel->GetColorIndices();
+
+ // Whether to use the default color.
+ const bool useDefaultColor = NULL == colorsBuffer;
+ const Vector4& defaultColor = mModel->GetDefaultColor();
+
+ // Create and initialize the pixel buffer.
+ GlyphData glyphData;
+ glyphData.verticalOffset = penY;
+
+ glyphData.width = static_cast<unsigned int>( size.width );
+ glyphData.height = static_cast<unsigned int>( size.height );
+ const unsigned int bufferSizeInt = glyphData.width * glyphData.height;
+ const unsigned int bufferSizeChar = 4u * bufferSizeInt;
+ glyphData.bitmapBuffer = new uint32_t[ bufferSizeInt ]; // This array will get deleted by PixelData because of the DELETE_ARRAY parameter.
+ memset( glyphData.bitmapBuffer, 0u, bufferSizeChar );
+
+ PixelData pixelData = PixelData::New( reinterpret_cast<uint8_t*>( glyphData.bitmapBuffer ),
+ bufferSizeChar,
+ glyphData.width,
+ glyphData.height,
+ Pixel::RGBA8888, // The format is RGBA8888 because is the format accepted by the image atlas manager.
+ PixelData::DELETE_ARRAY );
+
+ // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
+ TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
+
+ // Traverses the lines of the text.
+ for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
+ {
+ const LineRun& line = *( modelLinesBuffer + lineIndex );
+
+ // Sets the horizontal offset of the line.
+ glyphData.horizontalOffset = static_cast<int>( line.alignmentOffset );
+
+ // Increases the vertical offset with the line's ascender.
+ glyphData.verticalOffset += static_cast<int>( line.ascender );
+
+ // Traverses the glyphs of the line.
+ const GlyphIndex endGlyphIndex = std::min( numberOfGlyphs, line.glyphRun.glyphIndex + line.glyphRun.numberOfGlyphs );
+ for( GlyphIndex glyphIndex = line.glyphRun.glyphIndex; glyphIndex < endGlyphIndex; ++glyphIndex )
+ {
+ // Retrieve the glyph's info.
+ const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
+
+ if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
+ ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
+ {
+ // Nothing to do if the glyph's width or height is zero.
+ continue;
+ }
+
+ // Retrieves the glyph's position.
+ const Vector2* const position = positionBuffer + glyphIndex;
+
+ // Retrieves the glyph's color.
+ const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
+ const Vector4* const color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
+
+ // Retrieves the glyph's bitmap.
+ glyphData.glyphBitmap.buffer = NULL;
+ glyphData.glyphBitmap.width = glyphInfo->width; // Desired width and height.
+ glyphData.glyphBitmap.height = glyphInfo->height;
+ fontClient.CreateBitmap( glyphInfo->fontId,
+ glyphInfo->index,
+ glyphData.glyphBitmap );
+
+ // Sets the glyph's bitmap into the bitmap of the whole text.
+ if( NULL != glyphData.glyphBitmap.buffer )
+ {
+ TypesetGlyph( glyphData,
+ position,
+ color );
+ // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
+ delete []glyphData.glyphBitmap.buffer;
+ glyphData.glyphBitmap.buffer = NULL;
+
+ }
+ }
+
+ // Increases the vertical offset with the line's descender.
+ glyphData.verticalOffset += static_cast<int>( -line.descender );
+ }
+
+ return pixelData;
+}
+