Updated all header files to new format
[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) 2021 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/object/base-object.h>
23 #include <dali/public-api/object/weak-handle.h>
24
25 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Internal
35 {
36 class TextVisual;
37 typedef IntrusivePtr<TextVisual> TextVisualPtr;
38
39 /**
40  * The visual which renders text
41  *
42  * The following properties are optional:
43  *
44  * | %Property Name      | Type    |
45  * |---------------------|---------|
46  * | renderingBackend    | INTEGER |
47  * | text                | STRING  |
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  |
62  * | shadow              | STRING  |
63  * | outline             | STRING  |
64  *
65  */
66 class TextVisual : public Visual::Base
67 {
68 public:
69   /**
70    * @brief Create a new text visual.
71    *
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.
75    */
76   static TextVisualPtr New(VisualFactoryCache& factoryCache, const Property::Map& properties);
77
78   /**
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.
82    */
83   static Property::Map ConvertStringKeysToIndexKeys(const Property::Map& propertyMap);
84
85   /**
86    * @brief Retrieve the text's controller.
87    * @param[in] visual The text visual.
88    * @return The text controller
89    */
90   static Text::ControllerPtr GetController(Toolkit::Visual::Base visual)
91   {
92     return GetVisualObject(visual).mController;
93   };
94
95   /**
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
99    */
100   static void SetAnimatableTextColorProperty(Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex)
101   {
102     GetVisualObject(visual).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
103   };
104
105   /**
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.
108    */
109   static void EnableRendererUpdate(Toolkit::Visual::Base visual)
110   {
111     GetVisualObject(visual).mRendererUpdateNeeded = true;
112   };
113
114   /**
115    * @brief Instantly updates the renderer
116    * @param[in] visual The text visual.
117    */
118   static void UpdateRenderer(Toolkit::Visual::Base visual)
119   {
120     GetVisualObject(visual).UpdateRenderer();
121   };
122
123 public: // from Visual::Base
124   /**
125    * @copydoc Visual::Base::GetHeightForWidth()
126    */
127   float GetHeightForWidth(float width) override;
128
129   /**
130    * @copydoc Visual::Base::GetNaturalSize()
131    */
132   void GetNaturalSize(Vector2& naturalSize) override;
133
134   /**
135    * @copydoc Visual::Base::CreatePropertyMap()
136    */
137   void DoCreatePropertyMap(Property::Map& map) const override;
138
139   /**
140    * @copydoc Visual::Base::CreateInstancePropertyMap
141    */
142   void DoCreateInstancePropertyMap(Property::Map& map) const override;
143
144 protected:
145   /**
146    * @brief Constructor.
147    *
148    * @param[in] factoryCache The VisualFactoryCache object
149    */
150   TextVisual(VisualFactoryCache& factoryCache);
151
152   /**
153    * @brief A reference counted object may only be deleted by calling Unreference().
154    */
155   virtual ~TextVisual();
156
157   /**
158    * @copydoc Visual::Base::OnInitialize
159    */
160   void OnInitialize() override;
161
162   // from Visual::Base
163
164   /**
165    * @copydoc Visual::Base::DoSetProperties()
166    */
167   void DoSetProperties(const Property::Map& propertyMap) override;
168
169   /**
170    * @copydoc Visual::Base::DoSetOnScene()
171    */
172   void DoSetOnScene(Actor& actor) override;
173
174   /**
175    * @copydoc Visual::Base::DoSetOffScene()
176    */
177   void DoSetOffScene(Actor& actor) override;
178
179   /**
180    * @copydoc Visual::Base::OnSetTransform
181    */
182   void OnSetTransform() override;
183
184 private:
185   struct TilingInfo
186   {
187     unsigned char* textBuffer;
188     unsigned char* styleBuffer;
189     unsigned char* maskBuffer;
190     int            width;
191     int            height;
192     Pixel::Format  textPixelFormat;
193     int            offsetPosition;
194     Vector2        offSet;
195
196     TilingInfo(int width, int height, Pixel::Format textPixelFormat)
197     : textBuffer(NULL),
198       styleBuffer(NULL),
199       maskBuffer(NULL),
200       width(width),
201       height(height),
202       textPixelFormat(textPixelFormat),
203       offsetPosition(0),
204       offSet(0.f, 0.f)
205     {
206     }
207
208     ~TilingInfo()
209     {
210       if(textBuffer)
211       {
212         free(textBuffer);
213       }
214       if(styleBuffer)
215       {
216         free(styleBuffer);
217       }
218       if(maskBuffer)
219       {
220         free(maskBuffer);
221       }
222     }
223   };
224
225   /**
226    * @brief Set the individual property to the given value.
227    *
228    * @param[in] index The index key used to reference this value within the initial property map.
229    *
230    * @param[in] propertyValue The value to set.
231    */
232   void DoSetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
233
234   /**
235    * @brief Updates the text's renderer.
236    */
237   void UpdateRenderer();
238
239   /**
240    * @brief Removes the text's renderer.
241    */
242   void RemoveRenderer(Actor& actor);
243
244   /**
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.
250    */
251   void AddTexture(TextureSet& textureSet, PixelData& data, Sampler& sampler, unsigned int textureSetIndex);
252
253   /**
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.
260    */
261   PixelData ConvertToPixelData(unsigned char* buffer, int width, int height, int offsetPosition, const Pixel::Format textPixelFormat);
262
263   /**
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.).
271    */
272   void CreateTextureSet(TilingInfo& info, Renderer& renderer, Sampler& sampler, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
273
274   /**
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.).
281    */
282   void AddRenderer(Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
283
284   /**
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.).
290    */
291   TextureSet GetTextTexture(const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
292
293   /**
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.).
299    */
300   Shader GetTextShader(VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled);
301
302   /**
303    * @brief Retrieve the TextVisual object.
304    * @param[in] visual A handle to the TextVisual
305    * @return The TextVisual object
306    */
307   static TextVisual& GetVisualObject(Toolkit::Visual::Base visual)
308   {
309     return static_cast<TextVisual&>(Toolkit::GetImplementation(visual).GetVisualObject());
310   };
311
312 private:
313   typedef std::vector<Renderer> RendererContainer;
314
315   /**
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.
317    */
318   struct TextType
319   {
320     enum Type
321     {
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.
328     };
329   };
330
331 private:
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;
338 };
339
340 } // namespace Internal
341
342 } // namespace Toolkit
343
344 } // namespace Dali
345
346 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */