1 #ifndef DALI_TOOLKIT_TEXT_TYPESETTER_H
2 #define DALI_TOOLKIT_TEXT_TYPESETTER_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
40 typedef IntrusivePtr<Typesetter> TypesetterPtr;
43 * @brief This class is responsible of controlling the data flow of the text's rendering process.
45 class Typesetter : public RefObject
49 * @brief Behaviours of how to render the text.
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)
61 * @brief Styles of the text.
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
75 public: // Constructor.
77 * @brief Creates a Typesetter instance.
79 * The typesetter composes the final text retrieving the glyphs and the
80 * styles from the text's model.
82 * @param[in] model Pointer to the text's data model.
84 static TypesetterPtr New(const ModelInterface* const model);
88 * @brief Retrieves the pointer to the view model.
90 * @return A pointer to the view model.
92 ViewModel* GetViewModel();
95 * @brief Renders the text.
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
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).
109 * @return A pixel data with the text rendered.
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);
114 * @brief After the Render, use the pixel information of the given cutoutBuffer to make the part where the pixel is drawn transparent.
116 * @param[in] size The renderer size.
117 * @param[in] textDirection The direction of the text.
118 * @param[in] cutoutBuffer The buffer to use pixel information to cutout.
119 * @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).
120 * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment (i.e. always render as if HORIZONTAL_ALIGN_BEGIN).
121 * @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).
122 * @param[in] originAlpha The original alpha of text.
124 * @return A pixel data with the text rendered.
126 PixelData RenderWithCutout(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, Devel::PixelBuffer cutoutBuffer, RenderBehaviour behaviour = RENDER_TEXT_AND_STYLES, bool ignoreHorizontalAlignment = false, Pixel::Format pixelFormat = Pixel::RGBA8888, float originAlpha = 1.f);
129 * @brief Renders the text, return as Devel::PixelBuffer.
131 * This function is used to obtain the PixelBuffer required for cutout.
133 * @param[in] size The renderer size.
134 * @param[in] textDirection The direction of the text.
135 * @param[in] cutoutBuffer The buffer to use pixel information to cutout.
136 * @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).
137 * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment (i.e. always render as if HORIZONTAL_ALIGN_BEGIN).
138 * @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 * @return A pixel data with the text rendered.
142 Devel::PixelBuffer RenderWithPixelBuffer(const Vector2& size, Toolkit::DevelText::TextDirection::Type textDirection, RenderBehaviour behaviour = RENDER_TEXT_AND_STYLES, bool ignoreHorizontalAlignment = false, Pixel::Format pixelFormat = Pixel::RGBA8888);
145 * @brief Create & draw the image buffer of single background color.
147 * @param[in] bufferWidth The width of the image buffer.
148 * @param[in] bufferHeight The height of the image buffer.
149 * @param[in] backgroundColor The backgroundColor of image buffer.
151 * @return An image buffer with the text.
153 Devel::PixelBuffer CreateFullBackgroundBuffer(const uint32_t bufferWidth, const uint32_t bufferHeight, const Vector4& backgroundColor);
156 * @brief Set Mask for two pixel buffer.
158 * The alpha value of bottomPixelBuffer is decreased as the alpha value of topPixelBuffer is higher.
160 * @param[in, out] topPixelBuffer The top layer buffer.
161 * @param[in, out] 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 * @param[in] originAlpha The original alpha value of the text.
166 void SetMaskForImageBuffer(Devel::PixelBuffer& __restrict__ topPixelBuffer, Devel::PixelBuffer& __restrict__ bottomPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, float originAlpha);
170 * @brief Private constructor.
172 * @param[in] model Pointer to the text's data model.
174 Typesetter(const ModelInterface* const model);
176 // Declared private and left undefined to avoid copies.
177 Typesetter(const Typesetter& handle);
179 // Declared private and left undefined to avoid copies.
180 Typesetter& operator=(const Typesetter& handle);
183 * @brief Create & draw the image buffer for the given range of the glyphs in the given style.
185 * Does the following operations:
186 * - Retrieves the data buffers from the text model.
187 * - Creates the pixel data used to generate the final image with the given size.
188 * - Traverse the visible glyphs, retrieve their bitmaps and compose the final pixel data.
190 * @param[in] bufferWidth The width of the image buffer.
191 * @param[in] bufferHeight The height of the image buffer.
192 * @param[in] style The style of the text.
193 * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
194 * @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).
195 * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
196 * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
197 * @param[in] fromGlyphIndex The index of the first glyph within the text to be drawn
198 * @param[in] toGlyphIndex The index of the last glyph within the text to be drawn
200 * @return An image buffer with the text.
202 Devel::PixelBuffer CreateImageBuffer(const uint32_t bufferWidth, const uint32_t bufferHeight, const Typesetter::Style style, const bool ignoreHorizontalAlignment, const Pixel::Format pixelFormat, const int32_t horizontalOffset, const int32_t verticalOffset, const TextAbstraction::GlyphIndex fromGlyphIndex, const TextAbstraction::GlyphIndex toGlyphIndex);
205 * @brief Apply markup underline tags.
207 * The properties on TextLabel override the behavior of Markup.
208 * Because the markup will be the bottom layer buffer
209 * - i.e: If you set property UNDERLINE to enabled and blue.
210 * And the TEXT is "<color value='green'>Hello</color> <u>World</u> <i>Hello</i> <b>World</b>".
211 * Then the output of the whole text is underlined by blue line.
213 * @param[in] topPixelBuffer The top layer buffer.
214 * @param[in] bufferWidth The width of the image buffer.
215 * @param[in] bufferHeight The height of the image buffer.
216 * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
217 * @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).
218 * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
219 * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
221 * @return The image buffer with the markup.
223 Devel::PixelBuffer ApplyUnderlineMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, const bool ignoreHorizontalAlignment, const Pixel::Format pixelFormat, const int32_t horizontalOffset, const int32_t verticalOffset);
226 * @brief Apply markup strikethrough tags.
228 * The properties on TextLabel override the behavior of Markup.
229 * Because the markup will be the bottom layer buffer
230 * - i.e: If you set property STRIKETHROUGH to enabled and blue.
231 * And the TEXT is "<color value='green'>Hello</color> <s>World</s> <i>Hello</i> <b>World</b>".
232 * Then the whole text will have a blue line strikethrough.
234 * @param[in] topPixelBuffer The top layer buffer.
235 * @param[in] bufferWidth The width of the image buffer.
236 * @param[in] bufferHeight The height of the image buffer.
237 * @param[in] ignoreHorizontalAlignment Whether to ignore the horizontal alignment, not ignored by default.
238 * @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).
239 * @param[in] horizontalOffset The horizontal offset to be added to the glyph's position.
240 * @param[in] verticalOffset The vertical offset to be added to the glyph's position.
242 * @return The image buffer with the markup.
244 Devel::PixelBuffer ApplyStrikethroughMarkupImageBuffer(Devel::PixelBuffer topPixelBuffer, const uint32_t bufferWidth, const uint32_t bufferHeight, const bool ignoreHorizontalAlignment, const Pixel::Format pixelFormat, const int32_t horizontalOffset, const int32_t verticalOffset);
248 * @brief A reference counted object may only be deleted by calling Unreference().
250 * Destroys the visual model.
252 virtual ~Typesetter();
260 } // namespace Toolkit
264 #endif // DALI_TOOLKIT_TEXT_TYPESETTER_H