Reduce memory consumption of TextLabel by reducing texture size
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / text-typesetter.h
1 #ifndef DALI_TOOLKIT_TEXT_TYPESETTER_H
2 #define DALI_TOOLKIT_TEXT_TYPESETTER_H
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/images/pixel.h>
25 #include <dali/public-api/images/pixel-data.h>
26 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
27 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
28 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Text
37 {
38
39 class ModelInterface;
40 class ViewModel;
41 class Typesetter;
42
43 typedef IntrusivePtr<Typesetter> TypesetterPtr;
44
45 /**
46  * @brief This class is responsible of controlling the data flow of the text's rendering process.
47  */
48 class Typesetter : public RefObject
49 {
50 public:
51
52   /**
53    * @brief Behaviours of how to render the text.
54    */
55   enum RenderBehaviour
56   {
57     RENDER_TEXT_AND_STYLES,  ///< Render both the text and its styles
58     RENDER_NO_TEXT,          ///< Do not render the text itself
59     RENDER_NO_STYLES,        ///< Do not render any styles
60     RENDER_MASK              ///< Render an alpha mask (for color glyphs with no color animation, e.g. emoji)
61   };
62
63   /**
64    * @brief Styles of the text.
65    */
66   enum Style
67   {
68     STYLE_NONE,              ///< No style
69     STYLE_MASK,              ///< Alpha mask
70     STYLE_SHADOW,            ///< Hard shadow
71     STYLE_SOFT_SHADOW,       ///< Soft shadow
72     STYLE_UNDERLINE,         ///< Underline
73     STYLE_OUTLINE,           ///< Outline
74     STYLE_BACKGROUND         ///< Text background
75   };
76
77 public: // Constructor.
78   /**
79    * @brief Creates a Typesetter instance.
80    *
81    * The typesetter composes the final text retrieving the glyphs and the
82    * styles from the text's model.
83    *
84    * @param[in] model Pointer to the text's data model.
85    */
86   static TypesetterPtr New( const ModelInterface* const model );
87
88 public:
89   /**
90    * @brief Retrieves the pointer to the view model.
91    *
92    * @return A pointer to the view model.
93    */
94   ViewModel* GetViewModel();
95
96   /**
97    * @brief Renders the text.
98    *
99    * Does the following operations:
100    * - Finds the visible pages needed to be rendered.
101    * - Elide glyphs if needed.
102    * - Creates image buffers for diffrent text styles with the given size.
103    * - Combines different image buffers to create the pixel data used to generate the final image
104    *
105    * @param[in] size The renderer size.
106    * @param[in] textDirection The direction of the text.
107    * @param[in] behaviour The behaviour of how to render the text (i.e. whether to render the text only or the styles only or both).
108    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment (i.e. always render as if HORIZONTAL_ALIGN_BEGIN).
109    * @param[in] pixelFormat The format of the pixel in the image that the text is rendered as (i.e. either Pixel::BGRA8888 or Pixel::L8).
110    *
111    * @return A pixel data with the text rendered.
112    */
113   PixelData Render( const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour = RENDER_TEXT_AND_STYLES, bool ignoreHorizontalAlignment = false, Pixel::Format pixelFormat = Pixel::RGBA8888 );
114
115 private:
116   /**
117    * @brief Private constructor.
118    *
119    * @param[in] model Pointer to the text's data model.
120    */
121   Typesetter( const ModelInterface* const model );
122
123   // Declared private and left undefined to avoid copies.
124   Typesetter( const Typesetter& handle );
125
126   // Declared private and left undefined to avoid copies.
127   Typesetter& operator=( const Typesetter& handle );
128
129   /**
130    * @brief Create the image buffer for the given range of the glyphs in the given style.
131    *
132    * Does the following operations:
133    * - Retrieves the data buffers from the text model.
134    * - Creates the pixel data used to generate the final image with the given size.
135    * - Traverse the visible glyphs, retrieve their bitmaps and compose the final pixel data.
136    *
137    * @param[in] bufferWidth The width of the image buffer.
138    * @param[in] bufferHeight The height of the image buffer.
139    * @param[in] style The style of the text.
140    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
141    * @param[in] pixelFormat The format of the pixel in the image that the text is rendered as (i.e. either Pixel::BGRA8888 or Pixel::L8).
142    * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
143    * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
144    * @param[in] fromGlyphIndex The index of the first glyph within the text to be drawn
145    * @param[in] toGlyphIndex The index of the last glyph within the text to be drawn
146    *
147    * @return An image buffer with the text.
148    */
149   Devel::PixelBuffer CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, int horizontalOffset, int verticalOffset, TextAbstraction::GlyphIndex fromGlyphIndex, TextAbstraction::GlyphIndex toGlyphIndex );
150
151   /**
152    * @brief Combine the two RGBA image buffers together.
153    *
154    * The top layer buffer will blend over the bottom layer buffer:
155    * - If the pixel is not fully opaque from either buffer, it will be blended with
156    *   the pixel from the other buffer and copied to the combined buffer.
157    * - If the pixels from both buffers are fully opaque, the pixels from the top layer
158    *   buffer will be copied to the combined buffer.
159    *
160    * @param[in] topPixelBuffer The top layer buffer.
161    * @param[in] bottomPixelBuffer The bottom layer buffer.
162    * @param[in] bufferWidth The width of the image buffer.
163    * @param[in] bufferHeight The height of the image buffer.
164    *
165    * @return The combined image buffer with the text.
166    *
167    */
168   Devel::PixelBuffer CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeightbool );
169
170 protected:
171
172   /**
173    * @brief A reference counted object may only be deleted by calling Unreference().
174    *
175    * Destroys the visual model.
176    */
177   virtual ~Typesetter();
178
179 private:
180
181    ViewModel* mModel;
182 };
183
184 } // namespace Text
185
186 } // namespace Toolkit
187
188 } // namespace Dali
189
190 #endif // DALI_TOOLKIT_TEXT_TYPESETTER_H