1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_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/public-api/animation/constraint.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/object/weak-handle.h>
25 #include <dali/public-api/rendering/visual-renderer.h>
28 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
29 #include <dali-toolkit/internal/text/text-controller.h>
30 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
39 typedef IntrusivePtr<TextVisual> TextVisualPtr;
42 * The visual which renders text
44 * The following properties are optional:
46 * | %Property Name | Type |
47 * |---------------------|---------|
48 * | renderingBackend | INTEGER |
50 * | fontFamily | STRING |
51 * | fontStyle | STRING |
52 * | pointSize | FLOAT |
53 * | multiLine | BOOLEAN |
54 * | horizontalAlignment | STRING |
55 * | verticalAlignment | STRING |
56 * | textColor | VECTOR4 |
57 * | enableMarkup | BOOLEAN |
58 * | enableAutoScroll | BOOLEAN |
59 * | autoScrollSpeed | INTEGER |
60 * | autoScrollLoopCount | INTEGER |
61 * | autoScrollGap | INTEGER |
62 * | lineSpacing | FLOAT |
63 * | underline | STRING |
65 * | outline | STRING |
68 class TextVisual : public Visual::Base
72 * @brief Create a new text visual.
74 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
75 * @param[in] properties A Property::Map containing settings for this visual
76 * @return A smart-pointer to the newly allocated visual.
78 static TextVisualPtr New(VisualFactoryCache& factoryCache, const Property::Map& properties);
81 * @brief Converts all strings keys in property map to index keys. Property Map can then be merged correctly.
82 * @param[in] propertyMap containing string keys or a mix of strings and indexes.
83 * @return Property::Map containing index keys.
85 static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
88 * @brief Retrieve the text's controller.
89 * @param[in] visual The text visual.
90 * @return The text controller
92 static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
94 return GetVisualObject(visual).mController;
98 * @brief Set the index of the animatable text color property.
99 * @param[in] visual The text visual.
100 * @param[in] animatablePropertyIndex The index of the animatable property
102 static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
104 GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
108 * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
109 * @param[in] visual The text visual.
111 static void EnableRendererUpdate(Toolkit::Visual::Base visual)
113 GetVisualObject(visual).mRendererUpdateNeeded = true;
117 * @brief Instantly updates the renderer
118 * @param[in] visual The text visual.
120 static void UpdateRenderer(Toolkit::Visual::Base visual)
122 GetVisualObject(visual).UpdateRenderer();
125 public: // from Visual::Base
127 * @copydoc Visual::Base::GetHeightForWidth()
129 float GetHeightForWidth(float width) override;
132 * @copydoc Visual::Base::GetNaturalSize()
134 void GetNaturalSize(Vector2& naturalSize) override;
137 * @copydoc Visual::Base::CreatePropertyMap()
139 void DoCreatePropertyMap(Property::Map& map) const override;
142 * @copydoc Visual::Base::CreateInstancePropertyMap
144 void DoCreateInstancePropertyMap(Property::Map& map) const override;
147 * @copydoc Visual::Base::EnablePreMultipliedAlpha
149 void EnablePreMultipliedAlpha(bool preMultiplied) override;
153 * @brief Constructor.
155 * @param[in] factoryCache The VisualFactoryCache object
157 TextVisual(VisualFactoryCache& factoryCache);
160 * @brief A reference counted object may only be deleted by calling Unreference().
162 virtual ~TextVisual();
165 * @copydoc Visual::Base::OnInitialize
167 void OnInitialize() override;
172 * @copydoc Visual::Base::DoSetProperties()
174 void DoSetProperties(const Property::Map& propertyMap) override;
177 * @copydoc Visual::Base::DoSetOnScene()
179 void DoSetOnScene(Actor& actor) override;
182 * @copydoc Visual::Base::DoSetOffScene()
184 void DoSetOffScene(Actor& actor) override;
187 * @copydoc Visual::Base::OnSetTransform
189 void OnSetTransform() override;
194 unsigned char* textBuffer;
195 unsigned char* styleBuffer;
196 unsigned char* overlayStyleBuffer;
197 unsigned char* maskBuffer;
200 Pixel::Format textPixelFormat;
204 TilingInfo(int width, int height, Pixel::Format textPixelFormat)
207 overlayStyleBuffer(NULL),
211 textPixelFormat(textPixelFormat),
227 if(overlayStyleBuffer)
229 free(overlayStyleBuffer);
239 * @brief Set the individual property to the given value.
241 * @param[in] index The index key used to reference this value within the initial property map.
243 * @param[in] propertyValue The value to set.
245 void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
248 * @brief Updates the text's renderer.
250 void UpdateRenderer();
253 * @brief Removes the text's renderer.
255 void RemoveRenderer(Actor& actor);
258 * @brief Create a texture in textureSet and add it.
259 * @param[in] textureSet The textureSet to which the texture will be added.
260 * @param[in] data The PixelData to be uploaded to texture
261 * @param[in] sampler The sampler.
262 * @param[in] textureSetIndex The Index of TextureSet.
264 void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
267 * @brief Convert the buffer to pixelData.
268 * @param[in] buffer The Buffer to be converted to pixelData.
269 * @param[in] width The width of pixel data.
270 * @param[in] height The height of pixel data.
271 * @param[in] offsetPosition The The buffer's start position.
272 * @param[in] textPixelFormat The PixelForma of text.
274 PixelData ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat);
277 * @brief Create the text's texture.
278 * @param[in] info This is the information you need to create a Tiling.
279 * @param[in] renderer The renderer to which the TextureSet will be added.
280 * @param[in] sampler The sampler.
281 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
282 * @param[in] containsColorGlyph Whether the text contains color glyph.
283 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
284 * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
286 void CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
289 * Create renderer of the text for rendering.
290 * @param[in] actor The actor.
291 * @param[in] size The texture size.
292 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
293 * @param[in] containsColorGlyph Whether the text contains color glyph.
294 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
295 * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
297 void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
300 * Get the texture of the text for rendering.
301 * @param[in] size The texture size.
302 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
303 * @param[in] containsColorGlyph Whether the text contains color glyph.
304 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
305 * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
307 TextureSet GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
310 * Get the text rendering shader.
311 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
312 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
313 * @param[in] containsColorGlyph Whether the text contains color glyph.
314 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
316 Shader GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
319 * @brief Retrieve the TextVisual object.
320 * @param[in] visual A handle to the TextVisual
321 * @return The TextVisual object
323 static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
325 return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
329 typedef std::vector<Renderer> RendererContainer;
332 * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
338 SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
339 MULTI_COLOR_TEXT = 1, ///< The text contains multiple colors.
340 NO_EMOJI = 0, ///< The text contains no emoji.
341 HAS_EMOJI = 1, ///< The text contains emoji.
342 NO_STYLES = 0, ///< The text contains contains no styles.
343 HAS_SYLES = 1 ///< The text contains contains styles.
348 Text::ControllerPtr mController; ///< The text's controller.
349 Text::TypesetterPtr mTypesetter; ///< The text's typesetter.
350 WeakHandle<Actor> mControl; ///< The control where the renderer is added.
351 Constraint mColorConstraint{}; ///< Color constraint
352 Constraint mOpacityConstraint{}; ///< Opacity constraint
353 Property::Index mAnimatableTextColorPropertyIndex; ///< The index of animatable text color property registered by the control.
354 Property::Index mTextColorAnimatableIndex; ///< The index of uTextColorAnimatable property.
355 bool mRendererUpdateNeeded : 1; ///< The flag to indicate whether the renderer needs to be updated.
356 RendererContainer mRendererList;
359 } // namespace Internal
361 } // namespace Toolkit
365 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */