Merge "Fix for multi-language support." into devel/master
[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-data.h>
25 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
26 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Text
35 {
36
37 class ModelInterface;
38 class ViewModel;
39 class Typesetter;
40
41 typedef IntrusivePtr<Typesetter> TypesetterPtr;
42
43 /**
44  * @brief This class is responsible of controlling the data flow of the text's rendering process.
45  */
46 class Typesetter : public RefObject
47 {
48 public:
49
50   /**
51    * @brief Behaviours of how to render the text.
52    */
53   enum RenderBehaviour
54   {
55     RENDER_TEXT_AND_STYLES,  ///< Render both the text and its styles
56     RENDER_NO_TEXT,          ///< Do not render the text itself
57     RENDER_NO_STYLES,        ///< Do not render any styles
58     RENDER_MASK              ///< Render an alpha mask (for color glyphs with no color animation, e.g. emoji)
59   };
60
61   /**
62    * @brief Styles of the text.
63    */
64   enum Style
65   {
66     STYLE_NONE,              ///< No style
67     STYLE_MASK,              ///< Alpha mask
68     STYLE_SHADOW,            ///< Hard shadow
69     STYLE_SOFT_SHADOW,       ///< Soft shadow
70     STYLE_UNDERLINE,         ///< Underline
71     STYLE_OUTLINE,           ///< Outline
72     STYLE_BACKGROUND         ///< Text background
73   };
74
75 public: // Constructor.
76   /**
77    * @brief Creates a Typesetter instance.
78    *
79    * The typesetter composes the final text retrieving the glyphs and the
80    * styles from the text's model.
81    *
82    * @param[in] model Pointer to the text's data model.
83    */
84   static TypesetterPtr New( const ModelInterface* const model );
85
86 public:
87   /**
88    * @brief Retrieves the pointer to the view model.
89    *
90    * @return A pointer to the view model.
91    */
92   ViewModel* GetViewModel();
93
94   /**
95    * @brief Renders the text.
96    *
97    * Does the following operations:
98    * - Finds the visible pages needed to be rendered.
99    * - Elide glyphs if needed.
100    * - Creates image buffers for diffrent text styles with the given size.
101    * - Combines different image buffers to create the pixel data used to generate the final image
102    *
103    * @param[in] size The renderer size.
104    * @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).
105    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment (i.e. always render as if HORIZONTAL_ALIGN_BEGIN).
106    *
107    * @return A pixel data with the text rendered.
108    */
109   PixelData Render( const Vector2& size, RenderBehaviour behaviour = RENDER_TEXT_AND_STYLES, bool ignoreHorizontalAlignment = false );
110
111 private:
112   /**
113    * @brief Private constructor.
114    *
115    * @param[in] model Pointer to the text's data model.
116    */
117   Typesetter( const ModelInterface* const model );
118
119   // Declared private and left undefined to avoid copies.
120   Typesetter( const Typesetter& handle );
121
122   // Declared private and left undefined to avoid copies.
123   Typesetter& operator=( const Typesetter& handle );
124
125   /**
126    * @brief Create the image buffer for the given range of the glyphs in the given style.
127    *
128    * Does the following operations:
129    * - Retrieves the data buffers from the text model.
130    * - Creates the pixel data used to generate the final image with the given size.
131    * - Traverse the visible glyphs, retrieve their bitmaps and compose the final pixel data.
132    *
133    * @param[in] bufferWidth The width of the image buffer.
134    * @param[in] bufferHeight The height of the image buffer.
135    * @param[in] style The style of the text.
136    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
137    * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
138    * @param[in] fromGlyphIndex The index of the first glyph within the text to be drawn
139    * @param[in] toGlyphIndex The index of the last glyph within the text to be drawn
140    *
141    * @return An image buffer with the text.
142    */
143   Devel::PixelBuffer CreateImageBuffer( const unsigned int bufferWidth, const unsigned int bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, int verticalOffset, TextAbstraction::GlyphIndex fromGlyphIndex, TextAbstraction::GlyphIndex toGlyphIndex );
144
145   /**
146    * @brief Combine the two image buffers together.
147    *
148    * The top layer buffer will blend over the bottom layer buffer:
149    * - If the pixel is not fully opaque from either buffer, it will be blended with
150    *   the pixel from the other buffer and copied to the combined buffer.
151    * - If the pixels from both buffers are fully opaque, the pixels from the top layer
152    *   buffer will be copied to the combined buffer.
153    *
154    * @param[in] topPixelBuffer The top layer buffer.
155    * @param[in] bottomPixelBuffer The bottom layer buffer.
156    * @param[in] bufferWidth The width of the image buffer.
157    * @param[in] bufferHeight The height of the image buffer.
158    *
159    * @return The combined image buffer with the text.
160    *
161    */
162   Devel::PixelBuffer CombineImageBuffer( Devel::PixelBuffer topPixelBuffer, Devel::PixelBuffer bottomPixelBuffer, const unsigned int bufferWidth, const unsigned int bufferHeight );
163
164 protected:
165
166   /**
167    * @brief A reference counted object may only be deleted by calling Unreference().
168    *
169    * Destroys the visual model.
170    */
171   virtual ~Typesetter();
172
173 private:
174
175    ViewModel* mModel;
176 };
177
178 } // namespace Text
179
180 } // namespace Toolkit
181
182 } // namespace Dali
183
184 #endif // DALI_TOOLKIT_TEXT_TYPESETTER_H