Merge "Fix resource package install issue." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / text-typesetter.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/text-abstraction/font-client.h>
23 #include <memory.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/rendering/view-model.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Text
35 {
36
37 namespace
38 {
39
40 /**
41  * @brief Data struct used to set the buffer of the glyph's bitmap into the final bitmap's buffer.
42  */
43 struct GlyphData
44 {
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.
52 };
53
54 /**
55  * @brief Sets the glyph's buffer into the bitmap's buffer.
56  *
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.
60  */
61 void TypesetGlyph( const GlyphData& data,
62                    const Vector2* const position,
63                    const Vector4* const color )
64 {
65   if( ( 0u == data.glyphBitmap.width ) || ( 0u == data.glyphBitmap.height ) )
66   {
67     // Nothing to do if the width or height of the buffer is zero.
68     return;
69   }
70
71   const int widthMinusOne = static_cast<int>( data.width - 1u );
72   const int heightMinusOne = static_cast<int>( data.height - 1u );
73
74   // Whether the given glyph is a color one.
75   const bool isColorGlyph = Pixel::BGRA8888 == data.glyphBitmap.format;
76
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;
79
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 );
87
88   // Initial vertical offset.
89   const int yOffset = data.verticalOffset + position->y;
90
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 )
93   {
94     const int yOffsetIndex = yOffset + lineIndex;
95     if( ( 0 > yOffsetIndex ) || ( yOffsetIndex > heightMinusOne ) )
96     {
97       // Do not write out of bounds.
98       break;
99     }
100
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 )
105     {
106       const int xOffsetIndex = xOffset + index;
107       if( ( 0 > xOffsetIndex ) || ( xOffsetIndex > widthMinusOne ) )
108       {
109         // Don't write out of bounds.
110         break;
111       }
112
113       if( isColorGlyph )
114       {
115         // Retrieves the color from the glyph. The format is BGRA8888.
116         uint32_t packedColorGlyph = *( colorGlyphBuffer + glyphBufferOffset + index );
117
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 ) ) );
122
123         // Set the color into the final pixel buffer.
124         *( data.bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColorGlyph;
125       }
126       else
127       {
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 ) );
131
132         // Set the color into the final pixel buffer.
133         *( data.bitmapBuffer + verticalOffset + xOffsetIndex ) = packedColor;
134       }
135     }
136   }
137 }
138
139 } // namespace
140
141 TypesetterPtr Typesetter::New( const ModelInterface* const model )
142 {
143   return TypesetterPtr( new Typesetter( model ) );
144 }
145
146 ViewModel* Typesetter::GetViewModel()
147 {
148   return mModel;
149 }
150
151 PixelData Typesetter::Render( const Vector2& size )
152 {
153   // @todo. This initial implementation for a TextLabel has only one visible page.
154
155   // Elides the text if needed.
156   mModel->ElideGlyphs();
157
158   // Retrieves the layout size.
159   const Size& layoutSize = mModel->GetLayoutSize();
160
161   // Set the offset for the vertical alignment.
162   int penY = 0u;
163
164   switch( mModel->GetVerticalAlignment() )
165   {
166     case Layout::VERTICAL_ALIGN_TOP:
167     {
168       // No offset to add.
169       break;
170     }
171     case Layout::VERTICAL_ALIGN_CENTER:
172     {
173       penY = static_cast<int>( 0.5f * ( size.height - layoutSize.height ) );
174       break;
175     }
176     case Layout::VERTICAL_ALIGN_BOTTOM:
177     {
178       penY = static_cast<int>( size.height - layoutSize.height );
179       break;
180     }
181   }
182
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();
191
192   // Whether to use the default color.
193   const bool useDefaultColor = NULL == colorsBuffer;
194   const Vector4& defaultColor = mModel->GetDefaultColor();
195
196   // Create and initialize the pixel buffer.
197   GlyphData glyphData;
198   glyphData.verticalOffset = penY;
199
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 );
206
207   PixelData pixelData = PixelData::New( reinterpret_cast<uint8_t*>( glyphData.bitmapBuffer ),
208                                         bufferSizeChar,
209                                         glyphData.width,
210                                         glyphData.height,
211                                         Pixel::RGBA8888, // The format is RGBA8888 because is the format accepted by the image atlas manager.
212                                         PixelData::DELETE_ARRAY );
213
214   // Get a handle of the font client. Used to retrieve the bitmaps of the glyphs.
215   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
216
217   // Traverses the lines of the text.
218   for( LineIndex lineIndex = 0u; lineIndex < modelNumberOfLines; ++lineIndex )
219   {
220     const LineRun& line = *( modelLinesBuffer + lineIndex );
221
222     // Sets the horizontal offset of the line.
223     glyphData.horizontalOffset = static_cast<int>( line.alignmentOffset );
224
225     // Increases the vertical offset with the line's ascender.
226     glyphData.verticalOffset += static_cast<int>( line.ascender );
227
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 )
231     {
232       // Retrieve the glyph's info.
233       const GlyphInfo* const glyphInfo = glyphsBuffer + glyphIndex;
234
235       if( ( glyphInfo->width < Math::MACHINE_EPSILON_1000 ) ||
236           ( glyphInfo->height < Math::MACHINE_EPSILON_1000 ) )
237       {
238         // Nothing to do if the glyph's width or height is zero.
239         continue;
240       }
241
242       // Retrieves the glyph's position.
243       const Vector2* const position = positionBuffer + glyphIndex;
244
245       // Retrieves the glyph's color.
246       const ColorIndex colorIndex = *( colorIndexBuffer + glyphIndex );
247       const Vector4* const color = ( useDefaultColor || ( 0u == colorIndex ) ) ? &defaultColor : colorsBuffer + ( colorIndex - 1u );
248
249       // Retrieves the glyph's bitmap.
250       glyphData.glyphBitmap.buffer = NULL;
251       glyphData.glyphBitmap.width = glyphInfo->width;   // Desired width and height.
252       glyphData.glyphBitmap.height = glyphInfo->height;
253       fontClient.CreateBitmap( glyphInfo->fontId,
254                                glyphInfo->index,
255                                glyphData.glyphBitmap );
256
257       // Sets the glyph's bitmap into the bitmap of the whole text.
258       if( NULL != glyphData.glyphBitmap.buffer )
259       {
260         TypesetGlyph( glyphData,
261                       position,
262                       color );
263         // delete the glyphBitmap.buffer as it is now copied into glyphData.bitmapBuffer
264         delete []glyphData.glyphBitmap.buffer;
265         glyphData.glyphBitmap.buffer = NULL;
266
267       }
268     }
269
270     // Increases the vertical offset with the line's descender.
271     glyphData.verticalOffset += static_cast<int>( -line.descender );
272   }
273
274   return pixelData;
275 }
276
277 Typesetter::Typesetter( const ModelInterface* const model )
278 : mModel( new ViewModel( model ) )
279 {
280 }
281
282 Typesetter::~Typesetter()
283 {
284   delete mModel;
285 }
286
287 } // namespace Text
288
289 } // namespace Toolkit
290
291 } // namespace Dali