[dali_2.3.19] Merge branch 'devel/master'
[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) 2019 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/visuals/visual-base-impl.h>
27 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
28 #include <dali-toolkit/internal/text/text-controller.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
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   /**
74    * @brief Create a new text visual.
75    *
76    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache 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, 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,out] propertyMap containing string keys or a mix of strings and indexes. Will be changed to index keys.
85    */
86   static void ConvertStringKeysToIndexKeys( Property::Map& propertyMap );
87
88   /**
89    * @brief Retrieve the text's controller.
90    * @param[in] visual The text visual.
91    * @return The text controller
92    */
93   static Text::ControllerPtr GetController( Toolkit::Visual::Base visual )
94   {
95     return GetVisualObject( visual ).mController;
96   };
97
98   /**
99    * @brief Set the index of the animatable text color property.
100    * @param[in] visual The text visual.
101    * @param[in] animatablePropertyIndex The index of the animatable property
102    */
103   static void SetAnimatableTextColorProperty( Toolkit::Visual::Base visual, Property::Index animatablePropertyIndex )
104   {
105     GetVisualObject( visual ).mAnimatableTextColorPropertyIndex = animatablePropertyIndex;
106   };
107
108   /**
109    * @brief Set the flag to trigger the textures to be initialized and renderer to be added to the control.
110    * @param[in] visual The text visual.
111    */
112   static void EnableRendererUpdate( Toolkit::Visual::Base visual )
113   {
114     GetVisualObject( visual ).mRendererUpdateNeeded = true;
115   };
116
117   /**
118    * @brief Instantly updates the renderer
119    * @param[in] visual The text visual.
120    */
121   static void UpdateRenderer( Toolkit::Visual::Base visual )
122   {
123     GetVisualObject( visual ).UpdateRenderer();
124   };
125
126 public: // from Visual::Base
127
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 protected:
149
150   /**
151    * @brief Constructor.
152    *
153    * @param[in] factoryCache The VisualFactoryCache object
154    */
155   TextVisual( VisualFactoryCache& factoryCache );
156
157   /**
158    * @brief A reference counted object may only be deleted by calling Unreference().
159    */
160   virtual ~TextVisual();
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::DoSetOnStage()
171    */
172   void DoSetOnStage( Actor& actor ) override;
173
174   /**
175    * @copydoc Visual::Base::DoSetOffStage()
176    */
177   void DoSetOffStage( Actor& actor ) override;
178
179   /**
180    * @copydoc Visual::Base::OnSetTransform
181    */
182   void OnSetTransform() override;
183
184 private:
185
186   struct TilingInfo
187   {
188     unsigned char* textBuffer;
189     unsigned char* styleBuffer;
190     unsigned char* maskBuffer;
191     int width;
192     int height;
193     Pixel::Format textPixelFormat;
194     int offsetPosition;
195     Vector2 offSet;
196
197     TilingInfo( int width, int height, Pixel::Format textPixelFormat )
198     : textBuffer( NULL ),
199       styleBuffer( NULL ),
200       maskBuffer( NULL ),
201       width( width ),
202       height( height ),
203       textPixelFormat( textPixelFormat ),
204       offsetPosition( 0 ),
205       offSet( 0.f, 0.f )
206     {
207     }
208
209     ~TilingInfo()
210     {
211       if( textBuffer )
212       {
213         free( textBuffer );
214       }
215       if( styleBuffer )
216       {
217         free( styleBuffer );
218       }
219       if( maskBuffer )
220       {
221         free( maskBuffer );
222       }
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    */
274   void CreateTextureSet( TilingInfo& info, Renderer& renderer, Sampler& sampler,  bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
275
276   /**
277    * Create renderer of the text for rendering.
278    * @param[in] actor The actor.
279    * @param[in] size The texture size.
280    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
281    * @param[in] containsColorGlyph Whether the text contains color glyph.
282    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
283    */
284   void AddRenderer( Actor& actor, const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
285
286
287   /**
288    * Get the texture of the text for rendering.
289    * @param[in] size The texture size.
290    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
291    * @param[in] containsColorGlyph Whether the text contains color glyph.
292    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
293    */
294   TextureSet GetTextTexture( const Vector2& size, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
295
296   /**
297    * Get the text rendering shader.
298    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
299    * @param[in] hasMultipleTextColors Whether the text contains multiple colors.
300    * @param[in] containsColorGlyph Whether the text contains color glyph.
301    * @param[in] styleEnabled Whether the text contains any styles (e.g. shadow, underline, etc.).
302    */
303   Shader GetTextShader( VisualFactoryCache& factoryCache, bool hasMultipleTextColors, bool containsColorGlyph, bool styleEnabled );
304
305   /**
306    * @brief Retrieve the TextVisual object.
307    * @param[in] visual A handle to the TextVisual
308    * @return The TextVisual object
309    */
310   static TextVisual& GetVisualObject( Toolkit::Visual::Base visual )
311   {
312     return static_cast< TextVisual& >( Toolkit::GetImplementation( visual ).GetVisualObject() );
313   };
314
315 private:
316
317   typedef std::vector< Renderer > RendererContainer;
318
319   /**
320    * Used as an alternative to boolean so that it is obvious whether the text contains single or multiple text colors, and emoji and styles.
321    */
322   struct TextType
323   {
324     enum Type
325     {
326       SINGLE_COLOR_TEXT = 0, ///< The text contains single color only.
327       MULTI_COLOR_TEXT = 1,  ///< The text contains multiple colors.
328       NO_EMOJI = 0,          ///< The text contains no emoji.
329       HAS_EMOJI = 1,         ///< The text contains emoji.
330       NO_STYLES = 0,         ///< The text contains contains no styles.
331       HAS_SYLES = 1          ///< The text contains contains styles.
332     };
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   Property::Index     mAnimatableTextColorPropertyIndex;  ///< The index of animatable text color property registered by the control.
341   bool                mRendererUpdateNeeded:1;            ///< The flag to indicate whether the renderer needs to be updated.
342   RendererContainer   mRendererList;
343 };
344
345 } // namespace Internal
346
347 } // namespace Toolkit
348
349 } // namespace Dali
350
351 #endif /* DALI_TOOLKIT_INTERNAL_TEXT_VISUAL_H */