1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
5 * Copyright (c) 2021 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/object/base-object.h>
23 #include <dali/public-api/object/weak-handle.h>
26 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
27 #include <dali-toolkit/internal/text/text-controller.h>
28 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
37 typedef IntrusivePtr<TextVisual> TextVisualPtr;
40 * The visual which renders text
42 * The following properties are optional:
44 * | %Property Name | Type |
45 * |---------------------|---------|
46 * | renderingBackend | INTEGER |
48 * | fontFamily | STRING |
49 * | fontStyle | STRING |
50 * | pointSize | FLOAT |
51 * | multiLine | BOOLEAN |
52 * | horizontalAlignment | STRING |
53 * | verticalAlignment | STRING |
54 * | textColor | VECTOR4 |
55 * | enableMarkup | BOOLEAN |
56 * | enableAutoScroll | BOOLEAN |
57 * | autoScrollSpeed | INTEGER |
58 * | autoScrollLoopCount | INTEGER |
59 * | autoScrollGap | INTEGER |
60 * | lineSpacing | FLOAT |
61 * | underline | STRING |
63 * | outline | STRING |
66 class TextVisual : public Visual::Base
70 * @brief Create a new text visual.
72 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
73 * @param[in] properties A Property::Map containing settings for this visual
74 * @return A smart-pointer to the newly allocated visual.
76 static TextVisualPtr New(VisualFactoryCache& factoryCache, const Property::Map& properties);
79 * @brief Converts all strings keys in property map to index keys. Property Map can then be merged correctly.
80 * @param[in] propertyMap containing string keys or a mix of strings and indexes.
81 * @return Property::Map containing index keys.
83 static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
86 * @brief Retrieve the text's controller.
87 * @param[in] visual The text visual.
88 * @return The text controller
90 static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
92 return GetVisualObject(visual).mController;
96 * @brief Set the index of the animatable text color property.
97 * @param[in] visual The text visual.
98 * @param[in] animatablePropertyIndex The index of the animatable property
100 static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
102 GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
106 * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
107 * @param[in] visual The text visual.
109 static void EnableRendererUpdate(Toolkit::Visual::Base visual)
111 GetVisualObject(visual).mRendererUpdateNeeded = true;
115 * @brief Instantly updates the renderer
116 * @param[in] visual The text visual.
118 static void UpdateRenderer(Toolkit::Visual::Base visual)
120 GetVisualObject(visual).UpdateRenderer();
123 public: // from Visual::Base
125 * @copydoc Visual::Base::GetHeightForWidth()
127 float GetHeightForWidth(float width) override;
130 * @copydoc Visual::Base::GetNaturalSize()
132 void GetNaturalSize(Vector2& naturalSize) override;
135 * @copydoc Visual::Base::CreatePropertyMap()
137 void DoCreatePropertyMap(Property::Map& map) const override;
140 * @copydoc Visual::Base::CreateInstancePropertyMap
142 void DoCreateInstancePropertyMap(Property::Map& map) const override;
146 * @brief Constructor.
148 * @param[in] factoryCache The VisualFactoryCache object
150 TextVisual(VisualFactoryCache& factoryCache);
153 * @brief A reference counted object may only be deleted by calling Unreference().
155 virtual ~TextVisual();
158 * @copydoc Visual::Base::OnInitialize
160 void OnInitialize() override;
165 * @copydoc Visual::Base::DoSetProperties()
167 void DoSetProperties(const Property::Map& propertyMap) override;
170 * @copydoc Visual::Base::DoSetOnScene()
172 void DoSetOnScene(Actor& actor) override;
175 * @copydoc Visual::Base::DoSetOffScene()
177 void DoSetOffScene(Actor& actor) override;
180 * @copydoc Visual::Base::OnSetTransform
182 void OnSetTransform() override;
187 unsigned char* textBuffer;
188 unsigned char* styleBuffer;
189 unsigned char* maskBuffer;
192 Pixel::Format textPixelFormat;
196 TilingInfo(int width, int height, Pixel::Format textPixelFormat)
202 textPixelFormat(textPixelFormat),
226 * @brief Set the individual property to the given value.
228 * @param[in] index The index key used to reference this value within the initial property map.
230 * @param[in] propertyValue The value to set.
232 void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
235 * @brief Updates the text's renderer.
237 void UpdateRenderer();
240 * @brief Removes the text's renderer.
242 void RemoveRenderer(Actor& actor);
245 * @brief Create a texture in textureSet and add it.
246 * @param[in] textureSet The textureSet to which the texture will be added.
247 * @param[in] data The PixelData to be uploaded to texture
248 * @param[in] sampler The sampler.
249 * @param[in] textureSetIndex The Index of TextureSet.
251 void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
254 * @brief Convert the buffer to pixelData.
255 * @param[in] buffer The Buffer to be converted to pixelData.
256 * @param[in] width The width of pixel data.
257 * @param[in] height The height of pixel data.
258 * @param[in] offsetPosition The The buffer's start position.
259 * @param[in] textPixelFormat The PixelForma of text.
261 PixelData ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat);
264 * @brief Create the text's texture.
265 * @param[in] info This is the information you need to create a Tiling.
266 * @param[in] renderer The renderer to which the TextureSet will be added.
267 * @param[in] sampler The sampler.
268 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
269 * @param[in] containsColorGlyph Whether the text contains color glyph.
270 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
272 void CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
275 * Create renderer of the text for rendering.
276 * @param[in] actor The actor.
277 * @param[in] size The texture size.
278 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
279 * @param[in] containsColorGlyph Whether the text contains color glyph.
280 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
282 void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
285 * Get the texture of the text for rendering.
286 * @param[in] size The texture size.
287 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
288 * @param[in] containsColorGlyph Whether the text contains color glyph.
289 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
291 TextureSet GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
294 * Get the text rendering shader.
295 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
296 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
297 * @param[in] containsColorGlyph Whether the text contains color glyph.
298 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
300 Shader GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
303 * @brief Retrieve the TextVisual object.
304 * @param[in] visual A handle to the TextVisual
305 * @return The TextVisual object
307 static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
309 return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
313 typedef std::vector<Renderer> RendererContainer;
316 * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
322 SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
323 MULTI_COLOR_TEXT = 1, ///< The text contains multiple colors.
324 NO_EMOJI = 0, ///< The text contains no emoji.
325 HAS_EMOJI = 1, ///< The text contains emoji.
326 NO_STYLES = 0, ///< The text contains contains no styles.
327 HAS_SYLES = 1 ///< The text contains contains styles.
332 Text::ControllerPtr mController; ///< The text's controller.
333 Text::TypesetterPtr mTypesetter; ///< The text's typesetter.
334 WeakHandle<Actor> mControl; ///< The control where the renderer is added.
335 Property::Index mAnimatableTextColorPropertyIndex; ///< The index of animatable text color property registered by the control.
336 bool mRendererUpdateNeeded : 1; ///< The flag to indicate whether the renderer needs to be updated.
337 RendererContainer mRendererList;
340 } // namespace Internal
342 } // namespace Toolkit
346 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */