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