1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
5 * Copyright (c) 2024 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/controller/text-controller.h>
29 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
30 #include <dali-toolkit/internal/visuals/text-visual-shader-factory.h>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
40 typedef IntrusivePtr<TextVisual> TextVisualPtr;
43 * The visual which renders text
45 * The following properties are optional:
47 * | %Property Name | Type |
48 * |---------------------|---------|
49 * | renderingBackend | INTEGER |
51 * | fontFamily | STRING |
52 * | fontStyle | STRING |
53 * | pointSize | FLOAT |
54 * | multiLine | BOOLEAN |
55 * | horizontalAlignment | STRING |
56 * | verticalAlignment | STRING |
57 * | textColor | VECTOR4 |
58 * | enableMarkup | BOOLEAN |
59 * | enableAutoScroll | BOOLEAN |
60 * | autoScrollSpeed | INTEGER |
61 * | autoScrollLoopCount | INTEGER |
62 * | autoScrollGap | INTEGER |
63 * | lineSpacing | FLOAT |
64 * | underline | STRING |
66 * | outline | STRING |
69 class TextVisual : public Visual::Base
73 * @brief Create a new text visual.
75 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
76 * @param[in] shaderFactory The TextVisualShaderFactory object
77 * @param[in] properties A Property::Map containing settings for this visual
78 * @return A smart-pointer to the newly allocated visual.
80 static TextVisualPtr New(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory, const Property::Map& properties);
83 * @brief Converts all strings keys in property map to index keys. Property Map can then be merged correctly.
84 * @param[in] propertyMap containing string keys or a mix of strings and indexes.
85 * @return Property::Map containing index keys.
87 static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
90 * @brief Retrieve the text's controller.
91 * @param[in] visual The text visual.
92 * @return The text controller
94 static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
96 return GetVisualObject(visual).mController;
100 * @brief Set the index of the animatable text color property.
101 * @param[in] visual The text visual.
102 * @param[in] animatablePropertyIndex The index of the animatable property
104 static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
106 GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
110 * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
111 * @param[in] visual The text visual.
113 static void EnableRendererUpdate(Toolkit::Visual::Base visual)
115 GetVisualObject(visual).mRendererUpdateNeeded = true;
119 * @brief Set the text to be always rendered
120 * @param[in] visual The text visual.
121 * @param[in] requireRender Whether to text always rendered.
123 static void SetRequireRender(Toolkit::Visual::Base visual, bool requireRender)
125 GetVisualObject(visual).SetRequireRender(requireRender);
129 * @brief Instantly updates the renderer
130 * @param[in] visual The text visual.
132 static void UpdateRenderer(Toolkit::Visual::Base visual)
134 GetVisualObject(visual).UpdateRenderer();
137 public: // from Visual::Base
139 * @copydoc Visual::Base::GetHeightForWidth()
141 float GetHeightForWidth(float width) override;
144 * @copydoc Visual::Base::GetNaturalSize()
146 void GetNaturalSize(Vector2& naturalSize) override;
149 * @copydoc Visual::Base::CreatePropertyMap()
151 void DoCreatePropertyMap(Property::Map& map) const override;
154 * @copydoc Visual::Base::CreateInstancePropertyMap
156 void DoCreateInstancePropertyMap(Property::Map& map) const override;
159 * @copydoc Visual::Base::EnablePreMultipliedAlpha
161 void EnablePreMultipliedAlpha(bool preMultiplied) override;
165 * @brief Constructor.
167 * @param[in] factoryCache The VisualFactoryCache object
168 * @param[in] shaderFactory The TextVisualShaderFactory object
170 TextVisual(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory);
173 * @brief A reference counted object may only be deleted by calling Unreference().
175 virtual ~TextVisual();
178 * @copydoc Visual::Base::OnInitialize
180 void OnInitialize() override;
185 * @copydoc Visual::Base::DoSetProperties()
187 void DoSetProperties(const Property::Map& propertyMap) override;
190 * @copydoc Visual::Base::DoSetOnScene()
192 void DoSetOnScene(Actor& actor) override;
195 * @copydoc Visual::Base::DoSetOffScene()
197 void DoSetOffScene(Actor& actor) override;
200 * @copydoc Visual::Base::OnSetTransform
202 void OnSetTransform() override;
207 PixelData textPixelData;
208 PixelData stylePixelData;
209 PixelData overlayStylePixelData;
210 PixelData maskPixelData;
213 Pixel::Format textPixelFormat;
214 uint32_t offsetHeight;
215 Vector2 transformOffset;
217 TilingInfo(int32_t width, int32_t height, Pixel::Format textPixelFormat)
220 overlayStylePixelData(),
224 textPixelFormat(textPixelFormat),
226 transformOffset(0.f, 0.f)
236 * @brief Set the individual property to the given value.
238 * @param[in] index The index key used to reference this value within the initial property map.
240 * @param[in] propertyValue The value to set.
242 void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
245 * @brief Updates the text's renderer.
247 void UpdateRenderer();
250 * @brief Removes the text's renderer.
252 void RemoveRenderer(Actor& actor, bool removeDefaultRenderer);
255 * @brief Create a texture in textureSet and add it.
256 * @param[in] textureSet The textureSet to which the texture will be added.
257 * @param[in] data The PixelData to be uploaded to texture.
258 * @param[in] sampler The sampler.
259 * @param[in] textureSetIndex The Index of TextureSet.
261 void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
264 * @brief Create a texture in textureSet and add it.
265 * @param[in] textureSet The textureSet to which the texture will be added.
266 * @param[in] tilingInfo The tiling infomation to be uploaded to texture.
267 * @param[in] data The PixelData to be uploaded to texture.
268 * @param[in] sampler The sampler.
269 * @param[in] textureSetIndex The Index of TextureSet.
271 void AddTilingTexture(TextureSet& textureSet, TilingInfo& tilingInfo, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
274 * @brief Create the text's texture. It will use cached shader feature for text visual.
275 * @param[in] info This is the information you need to create a Tiling.
276 * @param[in] renderer The renderer to which the TextureSet will be added.
277 * @param[in] sampler The sampler.
279 void CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler);
282 * Create renderer of the text for rendering.
283 * @param[in] actor The actor.
284 * @param[in] size The texture size.
285 * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
286 * @param[in] containsColorGlyph Whether the text contains color glyph.
287 * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
288 * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
290 void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
293 * Get the texture of the text for rendering. It will use cached shader feature for text visual.
294 * @param[in] size The texture size.
296 TextureSet GetTextTexture(const Vector2& size);
299 * Get the text rendering shader.
300 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
301 * @param[in] featureBuilder Collection of current text shader's features. It will be cached as text visual.
303 Shader GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder);
306 * @brief Set the text to be always rendered
307 * @param[in] requireRender Whether to text always rendered.
309 void SetRequireRender(bool requireRender);
312 * @brief Retrieve the TextVisual object.
313 * @param[in] visual A handle to the TextVisual
314 * @return The TextVisual object
316 static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
318 return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
322 typedef std::vector<Renderer> RendererContainer;
325 Text::ControllerPtr mController; ///< The text's controller.
326 Text::TypesetterPtr mTypesetter; ///< The text's typesetter.
328 TextVisualShaderFactory& mTextVisualShaderFactory; ///< The shader factory for text visual.
329 TextVisualShaderFeature::FeatureBuilder mTextShaderFeatureCache; ///< The cached shader feature for text visual.
331 WeakHandle<Actor> mControl; ///< The control where the renderer is added.
332 Constraint mColorConstraint{}; ///< Color constraint
333 Constraint mOpacityConstraint{}; ///< Opacity constraint
334 Property::Index mHasMultipleTextColorsIndex; ///< The index of uHasMultipleTextColors proeprty.
335 Property::Index mAnimatableTextColorPropertyIndex; ///< The index of animatable text color property registered by the control.
336 Property::Index mTextColorAnimatableIndex; ///< The index of uTextColorAnimatable property.
337 Property::Index mTextRequireRenderPropertyIndex; ///< The index of requireRender property.
338 bool mRendererUpdateNeeded : 1; ///< The flag to indicate whether the renderer needs to be updated.
339 bool mTextRequireRender : 1; ///< The flag to indicate whether the text needs to be rendered.
340 RendererContainer mRendererList;
343 } // namespace Internal
345 } // namespace Toolkit
349 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */