3f9dd66a029f6020d11f32ab9ba4e697bbdd1b5b
[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) 2022 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-toolkit/devel-api/text/text-enumerations-devel.h>
23 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
24 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/images/pixel-data.h>
27 #include <dali/public-api/images/pixel.h>
28 #include <dali/public-api/object/ref-object.h>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Text
35 {
36 class ModelInterface;
37 class ViewModel;
38 class Typesetter;
39
40 typedef IntrusivePtr<Typesetter> TypesetterPtr;
41
42 /**
43  * @brief This class is responsible of controlling the data flow of the text's rendering process.
44  */
45 class Typesetter : public RefObject
46 {
47 public:
48   /**
49    * @brief Behaviours of how to render the text.
50    */
51   enum RenderBehaviour
52   {
53     RENDER_TEXT_AND_STYLES, ///< Render both the text and its styles
54     RENDER_NO_TEXT,         ///< Do not render the text itself but render the background styles such as outline and background.
55     RENDER_NO_STYLES,       ///< Do not render any styles
56     RENDER_MASK,            ///< Render an alpha mask (for color glyphs with no color animation, e.g. emoji)
57     RENDER_OVERLAY_STYLE    ///< Do not render the text itself but render the style but overlay the style on the text (foreground styles such as strikethrough and underline)
58   };
59
60   /**
61    * @brief Styles of the text.
62    */
63   enum Style
64   {
65     STYLE_NONE,         ///< No style
66     STYLE_MASK,         ///< Alpha mask
67     STYLE_SHADOW,       ///< Hard shadow
68     STYLE_SOFT_SHADOW,  ///< Soft shadow
69     STYLE_UNDERLINE,    ///< Underline
70     STYLE_OUTLINE,      ///< Outline
71     STYLE_BACKGROUND,   ///< Text background
72     STYLE_STRIKETHROUGH ///< Strikethrough
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] textDirection The direction of the text.
105    * @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).
106    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment (i.e. always render as if HORIZONTAL_ALIGN_BEGIN).
107    * @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).
108    *
109    * @return A pixel data with the text rendered.
110    */
111   PixelData Render(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour = RENDER_TEXT_AND_STYLES, bool ignoreHorizontalAlignment = false, Pixel::Format pixelFormat = Pixel::RGBA8888);
112
113 private:
114   /**
115    * @brief Private constructor.
116    *
117    * @param[in] model Pointer to the text's data model.
118    */
119   Typesetter(const ModelInterface* const model);
120
121   // Declared private and left undefined to avoid copies.
122   Typesetter(const Typesetter& handle);
123
124   // Declared private and left undefined to avoid copies.
125   Typesetter& operator=(const Typesetter& handle);
126
127   /**
128    * @brief Create & draw the image buffer for the given range of the glyphs in the given style.
129    *
130    * Does the following operations:
131    * - Retrieves the data buffers from the text model.
132    * - Creates the pixel data used to generate the final image with the given size.
133    * - Traverse the visible glyphs, retrieve their bitmaps and compose the final pixel data.
134    *
135    * @param[in] bufferWidth The width of the image buffer.
136    * @param[in] bufferHeight The height of the image buffer.
137    * @param[in] style The style of the text.
138    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
139    * @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).
140    * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
141    * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
142    * @param[in] fromGlyphIndex The index of the first glyph within the text to be drawn
143    * @param[in] toGlyphIndex The index of the last glyph within the text to be drawn
144    *
145    * @return An image buffer with the text.
146    */
147   Devel::PixelBuffer CreateImageBuffer(const uint32_t& bufferWidth, const uint32_t& bufferHeight, Typesetter::Style style, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset, TextAbstraction::GlyphIndex fromGlyphIndex, TextAbstraction::GlyphIndex toGlyphIndex);
148
149   /**
150    * @brief Apply behaviour of tags if the markup-processor is enabled.
151    *
152    * @param[in] topPixelBuffer The top layer buffer.
153    * @param[in] bufferWidth The width of the image buffer.
154    * @param[in] bufferHeight The height of the image buffer.
155    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
156    * @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).
157    * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
158    * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
159    *
160    * @return The image buffer with the markup.
161    */
162   Devel::PixelBuffer ApplyMarkupProcessorOnPixelBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset);
163
164   /**
165    * @brief Apply markup underline tags.
166    *
167    * The properties on TextLabel override the behavior of Markup.
168    * Because the markup will be the bottom layer buffer
169    *  - i.e: If you set property UNDERLINE to enabled and blue.
170    *    And the TEXT is "<color value='green'>Hello</color> <u>World</u> <i>Hello</i> <b>World</b>".
171    *    Then the output of the whole text is underlined by blue line.
172    *
173    * @param[in] topPixelBuffer The top layer buffer.
174    * @param[in] bufferWidth The width of the image buffer.
175    * @param[in] bufferHeight The height of the image buffer.
176    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
177    * @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).
178    * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
179    * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
180    *
181    * @return The image buffer with the markup.
182    */
183   Devel::PixelBuffer ApplyUnderlineMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset);
184
185   /**
186    * @brief Apply markup strikethrough tags.
187    *
188    * The properties on TextLabel override the behavior of Markup.
189    * Because the markup will be the bottom layer buffer
190    *  - i.e: If you set property STRIKETHROUGH to enabled and blue.
191    *    And the TEXT is "<color value='green'>Hello</color> <s>World</s> <i>Hello</i> <b>World</b>".
192    *    Then the whole text will have a blue line strikethrough.
193    *
194    * @param[in] topPixelBuffer The top layer buffer.
195    * @param[in] bufferWidth The width of the image buffer.
196    * @param[in] bufferHeight The height of the image buffer.
197    * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
198    * @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).
199    * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
200    * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
201    *
202    * @return The image buffer with the markup.
203    */
204   Devel::PixelBuffer ApplyStrikethroughMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t& bufferWidth, const uint32_t& bufferHeight, bool ignoreHorizontalAlignment, Pixel::Format pixelFormat, const int32_t& horizontalOffset, const int32_t& verticalOffset);
205
206 protected:
207   /**
208    * @brief A reference counted object may only be deleted by calling Unreference().
209    *
210    * Destroys the visual model.
211    */
212   virtual ~Typesetter();
213
214 private:
215   ViewModel* mModel;
216 };
217
218 } // namespace Text
219
220 } // namespace Toolkit
221
222 } // namespace Dali
223
224 #endif // DALI_TOOLKIT_TEXT_TYPESETTER_H