DALi Version 2.1.32
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / text / text-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_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/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>
26
27 // INTERNAL INCLUDES
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>
32
33 namespace Dali
34 {
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39 class TextVisual;
40 typedef IntrusivePtr<TextVisual> TextVisualPtr;
41
42 /**
43  * The visual which renders text
44  *
45  * The following properties are optional:
46  *
47  * | %Property Name      | Type    |
48  * |---------------------|---------|
49  * | renderingBackend    | INTEGER |
50  * | text                | STRING  |
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  |
65  * | shadow              | STRING  |
66  * | outline             | STRING  |
67  *
68  */
69 class TextVisual : public Visual::Base
70 {
71 public:
72   /**
73    * @brief Create a new text visual.
74    *
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.
79    */
80   static TextVisualPtr New(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory, const Property::Map& properties);
81
82   /**
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.
86    */
87   static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
88
89   /**
90    * @brief Retrieve the text's controller.
91    * @param[in] visual The text visual.
92    * @return The text controller
93    */
94   static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
95   {
96     return GetVisualObject(visual).mController;
97   };
98
99   /**
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
103    */
104   static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
105   {
106     GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
107   };
108
109   /**
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.
112    */
113   static void EnableRendererUpdate(Toolkit::Visual::Base visual)
114   {
115     GetVisualObject(visual).mRendererUpdateNeeded = true;
116   };
117
118   /**
119    * @brief Instantly updates the renderer
120    * @param[in] visual The text visual.
121    */
122   static void UpdateRenderer(Toolkit::Visual::Base visual)
123   {
124     GetVisualObject(visual).UpdateRenderer();
125   };
126
127 public: // from Visual::Base
128   /**
129    * @copydoc Visual::Base::GetHeightForWidth()
130    */
131   float GetHeightForWidth(float width) override;
132
133   /**
134    * @copydoc Visual::Base::GetNaturalSize()
135    */
136   void GetNaturalSize(Vector2& naturalSize) override;
137
138   /**
139    * @copydoc Visual::Base::CreatePropertyMap()
140    */
141   void DoCreatePropertyMap(Property::Map& map) const override;
142
143   /**
144    * @copydoc Visual::Base::CreateInstancePropertyMap
145    */
146   void DoCreateInstancePropertyMap(Property::Map& map) const override;
147
148   /**
149    * @copydoc Visual::Base::EnablePreMultipliedAlpha
150    */
151   void EnablePreMultipliedAlpha(bool preMultiplied) override;
152
153 protected:
154   /**
155    * @brief Constructor.
156    *
157    * @param[in] factoryCache The VisualFactoryCache object
158    * @param[in] shaderFactory The TextVisualShaderFactory object
159    */
160   TextVisual(VisualFactoryCache& factoryCache, TextVisualShaderFactory& shaderFactory);
161
162   /**
163    * @brief A reference counted object may only be deleted by calling Unreference().
164    */
165   virtual ~TextVisual();
166
167   /**
168    * @copydoc Visual::Base::OnInitialize
169    */
170   void OnInitialize() override;
171
172   // from Visual::Base
173
174   /**
175    * @copydoc Visual::Base::DoSetProperties()
176    */
177   void DoSetProperties(const Property::Map& propertyMap) override;
178
179   /**
180    * @copydoc Visual::Base::DoSetOnScene()
181    */
182   void DoSetOnScene(Actor& actor) override;
183
184   /**
185    * @copydoc Visual::Base::DoSetOffScene()
186    */
187   void DoSetOffScene(Actor& actor) override;
188
189   /**
190    * @copydoc Visual::Base::OnSetTransform
191    */
192   void OnSetTransform() override;
193
194 private:
195   struct TilingInfo
196   {
197     unsigned char* textBuffer;
198     unsigned char* styleBuffer;
199     unsigned char* overlayStyleBuffer;
200     unsigned char* maskBuffer;
201     int            width;
202     int            height;
203     Pixel::Format  textPixelFormat;
204     int            offsetPosition;
205     Vector2        offSet;
206
207     TilingInfo(int width, int height, Pixel::Format textPixelFormat)
208     : textBuffer(NULL),
209       styleBuffer(NULL),
210       overlayStyleBuffer(NULL),
211       maskBuffer(NULL),
212       width(width),
213       height(height),
214       textPixelFormat(textPixelFormat),
215       offsetPosition(0),
216       offSet(0.f, 0.f)
217     {
218     }
219
220     ~TilingInfo()
221     {
222       if(textBuffer)
223       {
224         free(textBuffer);
225       }
226       if(styleBuffer)
227       {
228         free(styleBuffer);
229       }
230       if(overlayStyleBuffer)
231       {
232         free(overlayStyleBuffer);
233       }
234       if(maskBuffer)
235       {
236         free(maskBuffer);
237       }
238     }
239   };
240
241   /**
242    * @brief Set the individual property to the given value.
243    *
244    * @param[in] index The index key used to reference this value within the initial property map.
245    *
246    * @param[in] propertyValue The value to set.
247    */
248   void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
249
250   /**
251    * @brief Updates the text's renderer.
252    */
253   void UpdateRenderer();
254
255   /**
256    * @brief Removes the text's renderer.
257    */
258   void RemoveRenderer(Actor& actor);
259
260   /**
261    * @brief Create a texture in textureSet and add it.
262    * @param[in] textureSet The textureSet to which the texture will be added.
263    * @param[in] data The PixelData to be uploaded to texture
264    * @param[in] sampler The sampler.
265    * @param[in] textureSetIndex The Index of TextureSet.
266    */
267   void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
268
269   /**
270    * @brief Convert the buffer to pixelData.
271    * @param[in] buffer The Buffer to be converted to pixelData.
272    * @param[in] width The width of pixel data.
273    * @param[in] height The height of pixel data.
274    * @param[in] offsetPosition The The buffer's start position.
275    * @param[in] textPixelFormat The PixelForma of text.
276    */
277   PixelData ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat);
278
279   /**
280    * @brief Create the text's texture. It will use cached shader feature for text visual.
281    * @param[in] info This is the information you need to create a Tiling.
282    * @param[in] renderer The renderer to which the TextureSet will be added.
283    * @param[in] sampler The sampler.
284    */
285   void CreateTextureSet(TilingInfo& info, VisualRenderer& renderer, Sampler& sampler);
286
287   /**
288    * Create renderer of the text for rendering.
289    * @param[in] actor The actor.
290    * @param[in] size The texture size.
291    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
292    * @param[in] containsColorGlyph Whether the text contains color glyph.
293    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
294    * @param[in] isOverlayStyle Whether the style needs to overlay on the text (e.g. strikethrough, underline, etc.).
295    */
296   void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled, bool isOverlayStyle);
297
298   /**
299    * Get the texture of the text for rendering. It will use cached shader feature for text visual.
300    * @param[in] size The texture size.
301    */
302   TextureSet GetTextTexture(const Vector2& size);
303
304   /**
305    * Get the text rendering shader.
306    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
307    * @param[in] featureBuilder Collection of current text shader's features. It will be cached as text visual.
308    */
309   Shader GetTextShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder);
310
311   /**
312    * @brief Retrieve the TextVisual object.
313    * @param[in] visual A handle to the TextVisual
314    * @return The TextVisual object
315    */
316   static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
317   {
318     return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
319   };
320
321 private:
322   typedef std::vector<Renderer> RendererContainer;
323
324 private:
325   Text::ControllerPtr mController; ///< The text's controller.
326   Text::TypesetterPtr mTypesetter; ///< The text's typesetter.
327
328   TextVisualShaderFactory&                mTextVisualShaderFactory; ///< The shader factory for text visual.
329   TextVisualShaderFeature::FeatureBuilder mTextShaderFeatureCache;  ///< The cached shader feature for text visual.
330
331   WeakHandle<Actor> mControl;                          ///< The control where the renderer is added.
332   Constraint        mColorConstraint{};                ///< Color constraint
333   Constraint        mOpacityConstraint{};              ///< Opacity constraint
334   Property::Index   mAnimatableTextColorPropertyIndex; ///< The index of animatable text color property registered by the control.
335   Property::Index   mTextColorAnimatableIndex;         ///< The index of uTextColorAnimatable property.
336   bool              mRendererUpdateNeeded : 1;         ///< The flag to indicate whether the renderer needs to be updated.
337   RendererContainer mRendererList;
338 };
339
340 } // namespace Internal
341
342 } // namespace Toolkit
343
344 } // namespace Dali
345
346 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */